Showing the changes to java/pkg/sun/security/timestamp/HttpTimestamper.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,172 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +package sun.security.timestamp; | |
| 27 | + | |
| 28 | +import java.io.BufferedInputStream; | |
| 29 | +import java.io.DataOutputStream; | |
| 30 | +import java.io.IOException; | |
| 31 | +import java.net.URL; | |
| 32 | +import java.net.HttpURLConnection; | |
| 33 | +import java.util.Iterator; | |
| 34 | +import java.util.Set; | |
| 35 | +import java.util.Arrays; | |
| 36 | + | |
| 37 | +import sun.misc.IOUtils; | |
| 38 | +import sun.security.pkcs.*; | |
| 39 | + | |
| 40 | +/** | |
| 41 | + * A timestamper that communicates with a Timestamping Authority (TSA) | |
| 42 | + * over HTTP. | |
| 43 | + * It supports the Time-Stamp Protocol defined in: | |
| 44 | + * <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>. | |
| 45 | + * | |
| 46 | + * @since 1.5 | |
| 47 | + * @author Vincent Ryan | |
| 48 | + */ | |
| 49 | + | |
| 50 | +public class HttpTimestamper implements Timestamper { | |
| 51 | + | |
| 52 | + private static final int CONNECT_TIMEOUT = 15000; // 15 seconds | |
| 53 | + | |
| 54 | + // The MIME type for a timestamp query | |
| 55 | + private static final String TS_QUERY_MIME_TYPE = | |
| 56 | + "application/timestamp-query"; | |
| 57 | + | |
| 58 | + // The MIME type for a timestamp reply | |
| 59 | + private static final String TS_REPLY_MIME_TYPE = | |
| 60 | + "application/timestamp-reply"; | |
| 61 | + | |
| 62 | + private static final boolean DEBUG = false; | |
| 63 | + | |
| 64 | + /* | |
| 65 | + * HTTP URL identifying the location of the TSA | |
| 66 | + */ | |
| 67 | + private String tsaUrl = null; | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * Creates a timestamper that connects to the specified TSA. | |
| 71 | + * | |
| 72 | + * @param tsa The location of the TSA. It must be an HTTP URL. | |
| 73 | + */ | |
| 74 | + public HttpTimestamper(String tsaUrl) { | |
| 75 | + this.tsaUrl = tsaUrl; | |
| 76 | + } | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * Connects to the TSA and requests a timestamp. | |
| 80 | + * | |
| 81 | + * @param tsQuery The timestamp query. | |
| 82 | + * @return The result of the timestamp query. | |
| 83 | + * @throws IOException The exception is thrown if a problem occurs while | |
| 84 | + * communicating with the TSA. | |
| 85 | + */ | |
| 86 | + public TSResponse generateTimestamp(TSRequest tsQuery) throws IOException { | |
| 87 | + | |
| 88 | + HttpURLConnection connection = | |
| 89 | + (HttpURLConnection) new URL(tsaUrl).openConnection(); | |
| 90 | + connection.setDoOutput(true); | |
| 91 | + connection.setUseCaches(false); // ignore cache | |
| 92 | + connection.setRequestProperty("Content-Type", TS_QUERY_MIME_TYPE); | |
| 93 | + connection.setRequestMethod("POST"); | |
| 94 | + // Avoids the "hang" when a proxy is required but none has been set. | |
| 95 | + connection.setConnectTimeout(CONNECT_TIMEOUT); | |
| 96 | + | |
| 97 | + if (DEBUG) { | |
| 98 | + Set headers = connection.getRequestProperties().entrySet(); | |
| 99 | + System.out.println(connection.getRequestMethod() + " " + tsaUrl + | |
| 100 | + " HTTP/1.1"); | |
| 101 | + for (Iterator i = headers.iterator(); i.hasNext(); ) { | |
| 102 | + System.out.println(" " + i.next()); | |
| 103 | + } | |
| 104 | + System.out.println(); | |
| 105 | + } | |
| 106 | + connection.connect(); // No HTTP authentication is performed | |
| 107 | + | |
| 108 | + // Send the request | |
| 109 | + DataOutputStream output = null; | |
| 110 | + try { | |
| 111 | + output = new DataOutputStream(connection.getOutputStream()); | |
| 112 | + byte[] request = tsQuery.encode(); | |
| 113 | + output.write(request, 0, request.length); | |
| 114 | + output.flush(); | |
| 115 | + if (DEBUG) { | |
| 116 | + System.out.println("sent timestamp query (length=" + | |
| 117 | + request.length + ")"); | |
| 118 | + } | |
| 119 | + } finally { | |
| 120 | + if (output != null) { | |
| 121 | + output.close(); | |
| 122 | + } | |
| 123 | + } | |
| 124 | + | |
| 125 | + // Receive the reply | |
| 126 | + BufferedInputStream input = null; | |
| 127 | + byte[] replyBuffer = null; | |
| 128 | + try { | |
| 129 | + input = new BufferedInputStream(connection.getInputStream()); | |
| 130 | + if (DEBUG) { | |
| 131 | + String header = connection.getHeaderField(0); | |
| 132 | + System.out.println(header); | |
| 133 | + int i = 1; | |
| 134 | + while ((header = connection.getHeaderField(i)) != null) { | |
| 135 | + String key = connection.getHeaderFieldKey(i); | |
| 136 | + System.out.println(" " + ((key==null) ? "" : key + ": ") + | |
| 137 | + header); | |
| 138 | + i++; | |
| 139 | + } | |
| 140 | + System.out.println(); | |
| 141 | + } | |
| 142 | + verifyMimeType(connection.getContentType()); | |
| 143 | + | |
| 144 | + int total = 0; | |
| 145 | + int contentLength = connection.getContentLength(); | |
| 146 | + replyBuffer = IOUtils.readFully(input, contentLength, false); | |
| 147 | + | |
| 148 | + if (DEBUG) { | |
| 149 | + System.out.println("received timestamp response (length=" + | |
| 150 | + total + ")"); | |
| 151 | + } | |
| 152 | + } finally { | |
| 153 | + if (input != null) { | |
| 154 | + input.close(); | |
| 155 | + } | |
| 156 | + } | |
| 157 | + return new TSResponse(replyBuffer); | |
| 158 | + } | |
| 159 | + | |
| 160 | + /* | |
| 161 | + * Checks that the MIME content type is a timestamp reply. | |
| 162 | + * | |
| 163 | + * @param contentType The MIME content type to be checked. | |
| 164 | + * @throws IOException The exception is thrown if a mismatch occurs. | |
| 165 | + */ | |
| 166 | + private static void verifyMimeType(String contentType) throws IOException { | |
| 167 | + if (! TS_REPLY_MIME_TYPE.equalsIgnoreCase(contentType)) { | |
| 168 | + throw new IOException("MIME Content-Type is not " + | |
| 169 | + TS_REPLY_MIME_TYPE); | |
| 170 | + } | |
| 171 | + } | |
| 172 | +} | |