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.URI;
32 import java.net.URL;
33 import java.net.HttpURLConnection;
34 import java.util.*;
35
36 import sun.misc.IOUtils;
37 import sun.security.util.Debug;
38
39 /**
40 * A timestamper that communicates with a Timestamping Authority (TSA)
41 * over HTTP.
42 * It supports the Time-Stamp Protocol defined in:
43 * <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>.
44 *
45 * @since 1.5
46 * @author Vincent Ryan
47 */
48
49 public class HttpTimestamper implements Timestamper {
50
51 private static final int CONNECT_TIMEOUT = 15000; // 15 seconds
52
53 // The MIME type for a timestamp query
54 private static final String TS_QUERY_MIME_TYPE =
55 "application/timestamp-query";
56
130
131 // Receive the reply
132 BufferedInputStream input = null;
133 byte[] replyBuffer = null;
134 try {
135 input = new BufferedInputStream(connection.getInputStream());
136 if (debug != null) {
137 String header = connection.getHeaderField(0);
138 debug.println(header);
139 int i = 1;
140 while ((header = connection.getHeaderField(i)) != null) {
141 String key = connection.getHeaderFieldKey(i);
142 debug.println(" " + ((key==null) ? "" : key + ": ") +
143 header);
144 i++;
145 }
146 debug.println();
147 }
148 verifyMimeType(connection.getContentType());
149
150 int contentLength = connection.getContentLength();
151 replyBuffer = IOUtils.readFully(input, contentLength, false);
152
153 if (debug != null) {
154 debug.println("received timestamp response (length=" +
155 replyBuffer.length + ")");
156 }
157 } finally {
158 if (input != null) {
159 input.close();
160 }
161 }
162 return new TSResponse(replyBuffer);
163 }
164
165 /*
166 * Checks that the MIME content type is a timestamp reply.
167 *
168 * @param contentType The MIME content type to be checked.
169 * @throws IOException The exception is thrown if a mismatch occurs.
170 */
171 private static void verifyMimeType(String contentType) throws IOException {
|
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.security.util.Debug;
38
39 /**
40 * A timestamper that communicates with a Timestamping Authority (TSA)
41 * over HTTP.
42 * It supports the Time-Stamp Protocol defined in:
43 * <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>.
44 *
45 * @since 1.5
46 * @author Vincent Ryan
47 */
48
49 public class HttpTimestamper implements Timestamper {
50
51 private static final int CONNECT_TIMEOUT = 15000; // 15 seconds
52
53 // The MIME type for a timestamp query
54 private static final String TS_QUERY_MIME_TYPE =
55 "application/timestamp-query";
56
130
131 // Receive the reply
132 BufferedInputStream input = null;
133 byte[] replyBuffer = null;
134 try {
135 input = new BufferedInputStream(connection.getInputStream());
136 if (debug != null) {
137 String header = connection.getHeaderField(0);
138 debug.println(header);
139 int i = 1;
140 while ((header = connection.getHeaderField(i)) != null) {
141 String key = connection.getHeaderFieldKey(i);
142 debug.println(" " + ((key==null) ? "" : key + ": ") +
143 header);
144 i++;
145 }
146 debug.println();
147 }
148 verifyMimeType(connection.getContentType());
149
150 int clen = connection.getContentLength();
151 replyBuffer = input.readAllBytes();
152 if (clen != -1 && replyBuffer.length != clen)
153 throw new EOFException("Expected:" + clen +
154 ", read:" + replyBuffer.length);
155
156 if (debug != null) {
157 debug.println("received timestamp response (length=" +
158 replyBuffer.length + ")");
159 }
160 } finally {
161 if (input != null) {
162 input.close();
163 }
164 }
165 return new TSResponse(replyBuffer);
166 }
167
168 /*
169 * Checks that the MIME content type is a timestamp reply.
170 *
171 * @param contentType The MIME content type to be checked.
172 * @throws IOException The exception is thrown if a mismatch occurs.
173 */
174 private static void verifyMimeType(String contentType) throws IOException {
|