J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 | |
| 36 | import sun.security.pkcs.*; |
| 37 | |
| 38 | /** |
| 39 | * A timestamper that communicates with a Timestamping Authority (TSA) |
| 40 | * over HTTP. |
| 41 | * It supports the Time-Stamp Protocol defined in: |
| 42 | * <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>. |
| 43 | * |
| 44 | * @since 1.5 |
| 45 | * @author Vincent Ryan |
| 46 | */ |
| 47 | |
| 48 | public class HttpTimestamper implements Timestamper { |
| 49 | |
| 50 | private static final int CONNECT_TIMEOUT = 15000; // 15 seconds |
| 51 | |
| 52 | // The MIME type for a timestamp query |
| 53 | private static final String TS_QUERY_MIME_TYPE = |
| 54 | "application/timestamp-query"; |
| 55 | |
| 56 | // The MIME type for a timestamp reply |
| 57 | private static final String TS_REPLY_MIME_TYPE = |
| 58 | "application/timestamp-reply"; |
| 59 | |
| 60 | private static final boolean DEBUG = false; |
| 61 | |
| 62 | /* |
| 63 | * HTTP URL identifying the location of the TSA |
| 64 | */ |
| 65 | private String tsaUrl = null; |
| 66 | |
| 67 | /** |
| 68 | * Creates a timestamper that connects to the specified TSA. |
| 69 | * |
| 70 | * @param tsa The location of the TSA. It must be an HTTP URL. |
| 71 | */ |
| 72 | public HttpTimestamper(String tsaUrl) { |
| 73 | this.tsaUrl = tsaUrl; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Connects to the TSA and requests a timestamp. |
| 78 | * |
| 79 | * @param tsQuery The timestamp query. |
| 80 | * @return The result of the timestamp query. |
| 81 | * @throws IOException The exception is thrown if a problem occurs while |
| 82 | * communicating with the TSA. |
| 83 | */ |
| 84 | public TSResponse generateTimestamp(TSRequest tsQuery) throws IOException { |
| 85 | |
| 86 | HttpURLConnection connection = |
| 87 | (HttpURLConnection) new URL(tsaUrl).openConnection(); |
| 88 | connection.setDoOutput(true); |
| 89 | connection.setUseCaches(false); // ignore cache |
| 90 | connection.setRequestProperty("Content-Type", TS_QUERY_MIME_TYPE); |
| 91 | connection.setRequestMethod("POST"); |
| 92 | // Avoids the "hang" when a proxy is required but none has been set. |
| 93 | connection.setConnectTimeout(CONNECT_TIMEOUT); |
| 94 | |
| 95 | if (DEBUG) { |
| 96 | Set headers = connection.getRequestProperties().entrySet(); |
| 97 | System.out.println(connection.getRequestMethod() + " " + tsaUrl + |
| 98 | " HTTP/1.1"); |
| 99 | for (Iterator i = headers.iterator(); i.hasNext(); ) { |
| 100 | System.out.println(" " + i.next()); |
| 101 | } |
| 102 | System.out.println(); |
| 103 | } |
| 104 | connection.connect(); // No HTTP authentication is performed |
| 105 | |
| 106 | // Send the request |
| 107 | DataOutputStream output = null; |
| 108 | try { |
| 109 | output = new DataOutputStream(connection.getOutputStream()); |
| 110 | byte[] request = tsQuery.encode(); |
| 111 | output.write(request, 0, request.length); |
| 112 | output.flush(); |
| 113 | if (DEBUG) { |
| 114 | System.out.println("sent timestamp query (length=" + |
| 115 | request.length + ")"); |
| 116 | } |
| 117 | } finally { |
| 118 | if (output != null) { |
| 119 | output.close(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Receive the reply |
| 124 | BufferedInputStream input = null; |
| 125 | byte[] replyBuffer = null; |
| 126 | try { |
| 127 | input = new BufferedInputStream(connection.getInputStream()); |
| 128 | if (DEBUG) { |
| 129 | String header = connection.getHeaderField(0); |
| 130 | System.out.println(header); |
| 131 | int i = 1; |
| 132 | while ((header = connection.getHeaderField(i)) != null) { |
| 133 | String key = connection.getHeaderFieldKey(i); |
| 134 | System.out.println(" " + ((key==null) ? "" : key + ": ") + |
| 135 | header); |
| 136 | i++; |
| 137 | } |
| 138 | System.out.println(); |
| 139 | } |
| 140 | int contentLength = connection.getContentLength(); |
| 141 | if (contentLength == -1) { |
| 142 | contentLength = Integer.MAX_VALUE; |
| 143 | } |
| 144 | verifyMimeType(connection.getContentType()); |
| 145 | |
| 146 | replyBuffer = new byte[contentLength]; |
| 147 | int total = 0; |
| 148 | int count = 0; |
| 149 | while (count != -1 && total < contentLength) { |
| 150 | count = input.read(replyBuffer, total, |
| 151 | replyBuffer.length - total); |
| 152 | total += count; |
| 153 | } |
| 154 | if (DEBUG) { |
| 155 | System.out.println("received timestamp response (length=" + |
| 156 | replyBuffer.length + ")"); |
| 157 | } |
| 158 | } finally { |
| 159 | if (input != null) { |
| 160 | input.close(); |
| 161 | } |
| 162 | } |
| 163 | return new TSResponse(replyBuffer); |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Checks that the MIME content type is a timestamp reply. |
| 168 | * |
| 169 | * @param contentType The MIME content type to be checked. |
| 170 | * @throws IOException The exception is thrown if a mismatch occurs. |
| 171 | */ |
| 172 | private static void verifyMimeType(String contentType) throws IOException { |
| 173 | if (! TS_REPLY_MIME_TYPE.equalsIgnoreCase(contentType)) { |
| 174 | throw new IOException("MIME Content-Type is not " + |
| 175 | TS_REPLY_MIME_TYPE); |
| 176 | } |
| 177 | } |
| 178 | } |