Path: blob/master/src/java.base/share/classes/sun/security/timestamp/HttpTimestamper.java
41159 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.timestamp;2627import java.io.BufferedInputStream;28import java.io.DataOutputStream;29import java.io.EOFException;30import java.io.IOException;31import java.net.URI;32import java.net.URL;33import java.net.HttpURLConnection;34import java.util.*;3536import sun.security.util.Debug;3738/**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.545* @author Vincent Ryan46*/4748public class HttpTimestamper implements Timestamper {4950private static final int CONNECT_TIMEOUT = 15000; // 15 seconds5152// The MIME type for a timestamp query53private static final String TS_QUERY_MIME_TYPE =54"application/timestamp-query";5556// The MIME type for a timestamp reply57private static final String TS_REPLY_MIME_TYPE =58"application/timestamp-reply";5960private static final Debug debug = Debug.getInstance("ts");6162/*63* HTTP URI identifying the location of the TSA64*/65private URI tsaURI = null;6667/**68* Creates a timestamper that connects to the specified TSA.69*70* @param tsaURI The location of the TSA. It must be an HTTP or HTTPS URI.71* @throws IllegalArgumentException if tsaURI is not an HTTP or HTTPS URI72*/73public HttpTimestamper(URI tsaURI) {74if (!tsaURI.getScheme().equalsIgnoreCase("http") &&75!tsaURI.getScheme().equalsIgnoreCase("https")) {76throw new IllegalArgumentException(77"TSA must be an HTTP or HTTPS URI");78}79this.tsaURI = tsaURI;80}8182/**83* Connects to the TSA and requests a timestamp.84*85* @param tsQuery The timestamp query.86* @return The result of the timestamp query.87* @throws IOException The exception is thrown if a problem occurs while88* communicating with the TSA.89*/90public TSResponse generateTimestamp(TSRequest tsQuery) throws IOException {9192HttpURLConnection connection =93(HttpURLConnection) tsaURI.toURL().openConnection();94connection.setDoOutput(true);95connection.setUseCaches(false); // ignore cache96connection.setRequestProperty("Content-Type", TS_QUERY_MIME_TYPE);97connection.setRequestMethod("POST");98// Avoids the "hang" when a proxy is required but none has been set.99connection.setConnectTimeout(CONNECT_TIMEOUT);100101if (debug != null) {102Set<Map.Entry<String, List<String>>> headers =103connection.getRequestProperties().entrySet();104debug.println(connection.getRequestMethod() + " " + tsaURI +105" HTTP/1.1");106for (Map.Entry<String, List<String>> e : headers) {107debug.println(" " + e);108}109debug.println();110}111connection.connect(); // No HTTP authentication is performed112113// Send the request114DataOutputStream output = null;115try {116output = new DataOutputStream(connection.getOutputStream());117byte[] request = tsQuery.encode();118output.write(request, 0, request.length);119output.flush();120if (debug != null) {121debug.println("sent timestamp query (length=" +122request.length + ")");123}124} finally {125if (output != null) {126output.close();127}128}129130// Receive the reply131BufferedInputStream input = null;132byte[] replyBuffer = null;133try {134input = new BufferedInputStream(connection.getInputStream());135if (debug != null) {136String header = connection.getHeaderField(0);137debug.println(header);138int i = 1;139while ((header = connection.getHeaderField(i)) != null) {140String key = connection.getHeaderFieldKey(i);141debug.println(" " + ((key==null) ? "" : key + ": ") +142header);143i++;144}145debug.println();146}147verifyMimeType(connection.getContentType());148149int clen = connection.getContentLength();150replyBuffer = input.readAllBytes();151if (clen != -1 && replyBuffer.length != clen)152throw new EOFException("Expected:" + clen +153", read:" + replyBuffer.length);154155if (debug != null) {156debug.println("received timestamp response (length=" +157replyBuffer.length + ")");158}159} finally {160if (input != null) {161input.close();162}163}164return new TSResponse(replyBuffer);165}166167/*168* Checks that the MIME content type is a timestamp reply.169*170* @param contentType The MIME content type to be checked.171* @throws IOException The exception is thrown if a mismatch occurs.172*/173private static void verifyMimeType(String contentType) throws IOException {174if (! TS_REPLY_MIME_TYPE.equalsIgnoreCase(contentType)) {175throw new IOException("MIME Content-Type is not " +176TS_REPLY_MIME_TYPE);177}178}179}180181182