/* * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import com.sun.net.httpserver.*; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.math.BigInteger; import java.net.InetSocketAddress; import java.security.KeyStore; import java.security.PrivateKey; import java.security.Signature; import java.security.cert.Certificate; import java.security.cert.X509Certificate; import java.util.Calendar; import java.util.jar.JarEntry; import java.util.jar.JarFile; import sun.security.pkcs.ContentInfo; import sun.security.pkcs.PKCS7; import sun.security.pkcs.PKCS9Attribute; import sun.security.pkcs.SignerInfo; import sun.security.timestamp.TimestampToken; import sun.security.util.DerOutputStream; import sun.security.util.DerValue; import sun.security.util.ObjectIdentifier; import sun.security.x509.AlgorithmId; import sun.security.x509.X500Name; public class TimestampCheck { static final String TSKS = "tsks"; static final String JAR = "old.jar"; static final String defaultPolicyId = "2.3.4.5"; static class Handler implements HttpHandler, AutoCloseable { private final HttpServer httpServer; private final String keystore; @Override public void handle(HttpExchange t) throws IOException { int len = 0; for (String h: t.getRequestHeaders().keySet()) { if (h.equalsIgnoreCase("Content-length")) { len = Integer.valueOf(t.getRequestHeaders().get(h).get(0)); } } byte[] input = new byte[len]; t.getRequestBody().read(input); try { int path = 0; if (t.getRequestURI().getPath().length() > 1) { path = Integer.parseInt( t.getRequestURI().getPath().substring(1)); } byte[] output = sign(input, path); Headers out = t.getResponseHeaders(); out.set("Content-Type", "application/timestamp-reply"); t.sendResponseHeaders(200, output.length); OutputStream os = t.getResponseBody(); os.write(output); } catch (Exception e) { e.printStackTrace(); t.sendResponseHeaders(500, 0); } t.close(); } /** * @param input The data to sign * @param path different cases to simulate, impl on URL path * 0: normal * 1: Missing nonce * 2: Different nonce * 3: Bad digets octets in messageImprint * 4: Different algorithmId in messageImprint * 5: whole chain in cert set * 6: extension is missing * 7: extension is non-critical * 8: extension does not have timestamping * 9: no cert in response * 10: normal * 11: always return default policy id * 12: normal * otherwise: normal * @returns the signed */ byte[] sign(byte[] input, int path) throws Exception { // Read TSRequest DerValue value = new DerValue(input); System.err.println("\nIncoming Request\n==================="); System.err.println("Version: " + value.data.getInteger()); DerValue messageImprint = value.data.getDerValue(); AlgorithmId aid = AlgorithmId.parse( messageImprint.data.getDerValue()); System.err.println("AlgorithmId: " + aid); ObjectIdentifier policyId = new ObjectIdentifier(defaultPolicyId); BigInteger nonce = null; while (value.data.available() > 0) { DerValue v = value.data.getDerValue(); if (v.tag == DerValue.tag_Integer) { nonce = v.getBigInteger(); System.err.println("nonce: " + nonce); } else if (v.tag == DerValue.tag_Boolean) { System.err.println("certReq: " + v.getBoolean()); } else if (v.tag == DerValue.tag_ObjectId) { policyId = v.getOID(); System.err.println("PolicyID: " + policyId); } } // Write TSResponse System.err.println("\nResponse\n==================="); KeyStore ks = KeyStore.getInstance("JKS"); try (FileInputStream fis = new FileInputStream(keystore)) { ks.load(fis, "changeit".toCharArray()); } String alias = "ts"; if (path == 6) alias = "tsbad1"; if (path == 7) alias = "tsbad2"; if (path == 8) alias = "tsbad3"; if (path == 11) { policyId = new ObjectIdentifier(defaultPolicyId); } DerOutputStream statusInfo = new DerOutputStream(); statusInfo.putInteger(0); DerOutputStream token = new DerOutputStream(); AlgorithmId[] algorithms = {aid}; Certificate[] chain = ks.getCertificateChain(alias); X509Certificate[] signerCertificateChain = null; X509Certificate signer = (X509Certificate)chain[0]; if (path == 5) { // Only case 5 uses full chain signerCertificateChain = new X509Certificate[chain.length]; for (int i=0; i= 0) { seeWarning = true; } } int result = p.waitFor(); if (expected && result != 0 || !expected && result == 0) { throw new Exception("Failed"); } if (seeWarning) { throw new Exception("See warning"); } } }