Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/timestamp/HttpTimestamper.java
38831 views
1
/*
2
* Copyright (c) 2003, 2012, 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.EOFException;
31
import java.io.IOException;
32
import java.net.URI;
33
import java.net.URL;
34
import java.net.HttpURLConnection;
35
import java.util.*;
36
37
import sun.misc.IOUtils;
38
import sun.security.util.Debug;
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 Debug debug = Debug.getInstance("ts");
63
64
/*
65
* HTTP URI identifying the location of the TSA
66
*/
67
private URI tsaURI = 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 or HTTPS URI.
73
* @throws IllegalArgumentException if tsaURI is not an HTTP or HTTPS URI
74
*/
75
public HttpTimestamper(URI tsaURI) {
76
if (!tsaURI.getScheme().equalsIgnoreCase("http") &&
77
!tsaURI.getScheme().equalsIgnoreCase("https")) {
78
throw new IllegalArgumentException(
79
"TSA must be an HTTP or HTTPS URI");
80
}
81
this.tsaURI = tsaURI;
82
}
83
84
/**
85
* Connects to the TSA and requests a timestamp.
86
*
87
* @param tsQuery The timestamp query.
88
* @return The result of the timestamp query.
89
* @throws IOException The exception is thrown if a problem occurs while
90
* communicating with the TSA.
91
*/
92
public TSResponse generateTimestamp(TSRequest tsQuery) throws IOException {
93
94
HttpURLConnection connection =
95
(HttpURLConnection) tsaURI.toURL().openConnection();
96
connection.setDoOutput(true);
97
connection.setUseCaches(false); // ignore cache
98
connection.setRequestProperty("Content-Type", TS_QUERY_MIME_TYPE);
99
connection.setRequestMethod("POST");
100
// Avoids the "hang" when a proxy is required but none has been set.
101
connection.setConnectTimeout(CONNECT_TIMEOUT);
102
103
if (debug != null) {
104
Set<Map.Entry<String, List<String>>> headers =
105
connection.getRequestProperties().entrySet();
106
debug.println(connection.getRequestMethod() + " " + tsaURI +
107
" HTTP/1.1");
108
for (Map.Entry<String, List<String>> e : headers) {
109
debug.println(" " + e);
110
}
111
debug.println();
112
}
113
connection.connect(); // No HTTP authentication is performed
114
115
// Send the request
116
DataOutputStream output = null;
117
try {
118
output = new DataOutputStream(connection.getOutputStream());
119
byte[] request = tsQuery.encode();
120
output.write(request, 0, request.length);
121
output.flush();
122
if (debug != null) {
123
debug.println("sent timestamp query (length=" +
124
request.length + ")");
125
}
126
} finally {
127
if (output != null) {
128
output.close();
129
}
130
}
131
132
// Receive the reply
133
BufferedInputStream input = null;
134
byte[] replyBuffer = null;
135
try {
136
input = new BufferedInputStream(connection.getInputStream());
137
if (debug != null) {
138
String header = connection.getHeaderField(0);
139
debug.println(header);
140
int i = 1;
141
while ((header = connection.getHeaderField(i)) != null) {
142
String key = connection.getHeaderFieldKey(i);
143
debug.println(" " + ((key==null) ? "" : key + ": ") +
144
header);
145
i++;
146
}
147
debug.println();
148
}
149
verifyMimeType(connection.getContentType());
150
151
int clen = connection.getContentLength();
152
replyBuffer = IOUtils.readAllBytes(input);
153
if (clen != -1 && replyBuffer.length != clen)
154
throw new EOFException("Expected:" + clen +
155
", read:" + replyBuffer.length);
156
157
if (debug != null) {
158
debug.println("received timestamp response (length=" +
159
replyBuffer.length + ")");
160
}
161
} finally {
162
if (input != null) {
163
input.close();
164
}
165
}
166
return new TSResponse(replyBuffer);
167
}
168
169
/*
170
* Checks that the MIME content type is a timestamp reply.
171
*
172
* @param contentType The MIME content type to be checked.
173
* @throws IOException The exception is thrown if a mismatch occurs.
174
*/
175
private static void verifyMimeType(String contentType) throws IOException {
176
if (! TS_REPLY_MIME_TYPE.equalsIgnoreCase(contentType)) {
177
throw new IOException("MIME Content-Type is not " +
178
TS_REPLY_MIME_TYPE);
179
}
180
}
181
}
182
183