2010-04-16 10:13:23 +08:00
|
|
|
/*
|
2016-06-23 19:58:58 -07:00
|
|
|
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
|
2010-04-16 10:13:23 +08:00
|
|
|
* 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.
|
|
|
|
*
|
2010-05-25 15:58:33 -07:00
|
|
|
* 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.
|
2010-04-16 10:13:23 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import com.sun.net.httpserver.*;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
2016-10-20 09:24:47 +08:00
|
|
|
import java.io.File;
|
2010-04-16 10:13:23 +08:00
|
|
|
import java.io.IOException;
|
2014-04-21 10:26:03 +08:00
|
|
|
import java.io.InputStream;
|
2010-04-16 10:13:23 +08:00
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.net.InetSocketAddress;
|
2016-10-20 09:24:47 +08:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Paths;
|
2010-04-16 10:13:23 +08:00
|
|
|
import java.security.KeyStore;
|
|
|
|
import java.security.PrivateKey;
|
|
|
|
import java.security.Signature;
|
|
|
|
import java.security.cert.Certificate;
|
|
|
|
import java.security.cert.X509Certificate;
|
2016-10-20 09:24:47 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
2010-04-16 10:13:23 +08:00
|
|
|
import java.util.Calendar;
|
2016-10-20 09:24:47 +08:00
|
|
|
import java.util.List;
|
2014-04-21 10:26:03 +08:00
|
|
|
import java.util.jar.JarEntry;
|
|
|
|
import java.util.jar.JarFile;
|
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
import jdk.testlibrary.*;
|
|
|
|
import jdk.testlibrary.JarUtils;
|
2010-04-16 10:13:23 +08:00
|
|
|
import sun.security.pkcs.ContentInfo;
|
|
|
|
import sun.security.pkcs.PKCS7;
|
2014-04-21 10:26:03 +08:00
|
|
|
import sun.security.pkcs.PKCS9Attribute;
|
2010-04-16 10:13:23 +08:00
|
|
|
import sun.security.pkcs.SignerInfo;
|
2014-04-21 10:26:03 +08:00
|
|
|
import sun.security.timestamp.TimestampToken;
|
2010-04-16 10:13:23 +08:00
|
|
|
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;
|
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
/*
|
|
|
|
* @test
|
2016-11-21 22:50:22 -08:00
|
|
|
* @bug 6543842 6543440 6939248 8009636 8024302 8163304 8169911
|
2016-10-20 09:24:47 +08:00
|
|
|
* @summary checking response of timestamp
|
|
|
|
* @modules java.base/sun.security.pkcs
|
|
|
|
* java.base/sun.security.timestamp
|
|
|
|
* java.base/sun.security.x509
|
|
|
|
* java.base/sun.security.util
|
|
|
|
* java.base/sun.security.tools.keytool
|
|
|
|
* @library /lib/testlibrary
|
|
|
|
* @run main/timeout=600 TimestampCheck
|
|
|
|
*/
|
2010-04-16 10:13:23 +08:00
|
|
|
public class TimestampCheck {
|
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
static final String defaultPolicyId = "2.3.4";
|
|
|
|
static String host = null;
|
2014-04-21 10:26:03 +08:00
|
|
|
|
2015-01-26 18:13:42 +08:00
|
|
|
static class Handler implements HttpHandler, AutoCloseable {
|
|
|
|
|
|
|
|
private final HttpServer httpServer;
|
|
|
|
private final String keystore;
|
|
|
|
|
|
|
|
@Override
|
2010-04-16 10:13:23 +08:00
|
|
|
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 {
|
2016-10-20 09:24:47 +08:00
|
|
|
String path = t.getRequestURI().getPath().substring(1);
|
2010-04-16 10:13:23 +08:00
|
|
|
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
|
|
|
|
* @returns the signed
|
|
|
|
*/
|
2016-10-20 09:24:47 +08:00
|
|
|
byte[] sign(byte[] input, String path) throws Exception {
|
|
|
|
|
2010-04-16 10:13:23 +08:00
|
|
|
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);
|
|
|
|
|
2014-04-21 10:26:03 +08:00
|
|
|
ObjectIdentifier policyId = new ObjectIdentifier(defaultPolicyId);
|
2010-04-16 10:13:23 +08:00
|
|
|
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());
|
2014-04-21 10:26:03 +08:00
|
|
|
} else if (v.tag == DerValue.tag_ObjectId) {
|
|
|
|
policyId = v.getOID();
|
|
|
|
System.err.println("PolicyID: " + policyId);
|
2010-04-16 10:13:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
System.err.println("\nResponse\n===================");
|
2016-10-20 09:24:47 +08:00
|
|
|
KeyStore ks = KeyStore.getInstance(
|
|
|
|
new File(keystore), "changeit".toCharArray());
|
2010-04-16 10:13:23 +08:00
|
|
|
|
|
|
|
String alias = "ts";
|
2016-10-20 09:24:47 +08:00
|
|
|
if (path.startsWith("bad") || path.equals("weak")) {
|
|
|
|
alias = "ts" + path;
|
|
|
|
}
|
2010-04-16 10:13:23 +08:00
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
if (path.equals("diffpolicy")) {
|
2014-04-21 10:26:03 +08:00
|
|
|
policyId = new ObjectIdentifier(defaultPolicyId);
|
|
|
|
}
|
|
|
|
|
2010-04-16 10:13:23 +08:00
|
|
|
DerOutputStream statusInfo = new DerOutputStream();
|
|
|
|
statusInfo.putInteger(0);
|
|
|
|
|
|
|
|
AlgorithmId[] algorithms = {aid};
|
|
|
|
Certificate[] chain = ks.getCertificateChain(alias);
|
2016-10-20 09:24:47 +08:00
|
|
|
X509Certificate[] signerCertificateChain;
|
2010-04-16 10:13:23 +08:00
|
|
|
X509Certificate signer = (X509Certificate)chain[0];
|
2016-10-20 09:24:47 +08:00
|
|
|
|
|
|
|
if (path.equals("fullchain")) { // Only case 5 uses full chain
|
2010-04-16 10:13:23 +08:00
|
|
|
signerCertificateChain = new X509Certificate[chain.length];
|
|
|
|
for (int i=0; i<chain.length; i++) {
|
|
|
|
signerCertificateChain[i] = (X509Certificate)chain[i];
|
|
|
|
}
|
2016-10-20 09:24:47 +08:00
|
|
|
} else if (path.equals("nocert")) {
|
2010-04-16 10:13:23 +08:00
|
|
|
signerCertificateChain = new X509Certificate[0];
|
|
|
|
} else {
|
|
|
|
signerCertificateChain = new X509Certificate[1];
|
|
|
|
signerCertificateChain[0] = (X509Certificate)chain[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
DerOutputStream tst = new DerOutputStream();
|
|
|
|
|
|
|
|
tst.putInteger(1);
|
2014-04-21 10:26:03 +08:00
|
|
|
tst.putOID(policyId);
|
2010-04-16 10:13:23 +08:00
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
if (!path.equals("baddigest") && !path.equals("diffalg")) {
|
2010-04-16 10:13:23 +08:00
|
|
|
tst.putDerValue(messageImprint);
|
|
|
|
} else {
|
|
|
|
byte[] data = messageImprint.toByteArray();
|
2016-10-20 09:24:47 +08:00
|
|
|
if (path.equals("diffalg")) {
|
2010-04-16 10:13:23 +08:00
|
|
|
data[6] = (byte)0x01;
|
|
|
|
} else {
|
|
|
|
data[data.length-1] = (byte)0x01;
|
|
|
|
data[data.length-2] = (byte)0x02;
|
|
|
|
data[data.length-3] = (byte)0x03;
|
|
|
|
}
|
|
|
|
tst.write(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
tst.putInteger(1);
|
|
|
|
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
tst.putGeneralizedTime(cal.getTime());
|
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
if (path.equals("diffnonce")) {
|
2010-04-16 10:13:23 +08:00
|
|
|
tst.putInteger(1234);
|
2016-10-20 09:24:47 +08:00
|
|
|
} else if (path.equals("nononce")) {
|
|
|
|
// no noce
|
2010-04-16 10:13:23 +08:00
|
|
|
} else {
|
|
|
|
tst.putInteger(nonce);
|
|
|
|
}
|
|
|
|
|
|
|
|
DerOutputStream tstInfo = new DerOutputStream();
|
|
|
|
tstInfo.write(DerValue.tag_Sequence, tst);
|
|
|
|
|
|
|
|
DerOutputStream tstInfo2 = new DerOutputStream();
|
|
|
|
tstInfo2.putOctetString(tstInfo.toByteArray());
|
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
// Always use the same algorithm at timestamp signing
|
|
|
|
// so it is different from the hash algorithm.
|
2013-10-23 08:32:03 +08:00
|
|
|
Signature sig = Signature.getInstance("SHA1withRSA");
|
2010-04-16 10:13:23 +08:00
|
|
|
sig.initSign((PrivateKey)(ks.getKey(
|
|
|
|
alias, "changeit".toCharArray())));
|
|
|
|
sig.update(tstInfo.toByteArray());
|
|
|
|
|
|
|
|
ContentInfo contentInfo = new ContentInfo(new ObjectIdentifier(
|
|
|
|
"1.2.840.113549.1.9.16.1.4"),
|
|
|
|
new DerValue(tstInfo2.toByteArray()));
|
|
|
|
|
|
|
|
System.err.println("Signing...");
|
|
|
|
System.err.println(new X500Name(signer
|
|
|
|
.getIssuerX500Principal().getName()));
|
|
|
|
System.err.println(signer.getSerialNumber());
|
|
|
|
|
|
|
|
SignerInfo signerInfo = new SignerInfo(
|
|
|
|
new X500Name(signer.getIssuerX500Principal().getName()),
|
|
|
|
signer.getSerialNumber(),
|
2016-10-20 09:24:47 +08:00
|
|
|
AlgorithmId.get("SHA-1"), AlgorithmId.get("RSA"), sig.sign());
|
2010-04-16 10:13:23 +08:00
|
|
|
|
|
|
|
SignerInfo[] signerInfos = {signerInfo};
|
2016-10-20 09:24:47 +08:00
|
|
|
PKCS7 p7 = new PKCS7(algorithms, contentInfo,
|
|
|
|
signerCertificateChain, signerInfos);
|
2010-04-16 10:13:23 +08:00
|
|
|
ByteArrayOutputStream p7out = new ByteArrayOutputStream();
|
|
|
|
p7.encodeSignedData(p7out);
|
|
|
|
|
|
|
|
DerOutputStream response = new DerOutputStream();
|
|
|
|
response.write(DerValue.tag_Sequence, statusInfo);
|
|
|
|
response.putDerValue(new DerValue(p7out.toByteArray()));
|
|
|
|
|
|
|
|
DerOutputStream out = new DerOutputStream();
|
|
|
|
out.write(DerValue.tag_Sequence, response);
|
|
|
|
|
|
|
|
return out.toByteArray();
|
|
|
|
}
|
|
|
|
|
2015-01-26 18:13:42 +08:00
|
|
|
private Handler(HttpServer httpServer, String keystore) {
|
|
|
|
this.httpServer = httpServer;
|
|
|
|
this.keystore = keystore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize TSA instance.
|
|
|
|
*
|
|
|
|
* Extended Key Info extension of certificate that is used for
|
|
|
|
* signing TSA responses should contain timeStamping value.
|
|
|
|
*/
|
|
|
|
static Handler init(int port, String keystore) throws IOException {
|
|
|
|
HttpServer httpServer = HttpServer.create(
|
|
|
|
new InetSocketAddress(port), 0);
|
|
|
|
Handler tsa = new Handler(httpServer, keystore);
|
|
|
|
httpServer.createContext("/", tsa);
|
|
|
|
return tsa;
|
|
|
|
}
|
2010-04-16 10:13:23 +08:00
|
|
|
|
2015-01-26 18:13:42 +08:00
|
|
|
/**
|
|
|
|
* Start TSA service.
|
|
|
|
*/
|
|
|
|
void start() {
|
|
|
|
httpServer.start();
|
2014-04-25 10:57:09 +08:00
|
|
|
}
|
|
|
|
|
2015-01-26 18:13:42 +08:00
|
|
|
/**
|
|
|
|
* Stop TSA service.
|
|
|
|
*/
|
|
|
|
void stop() {
|
|
|
|
httpServer.stop(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return server port number.
|
|
|
|
*/
|
|
|
|
int getPort() {
|
|
|
|
return httpServer.getAddress().getPort();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws Exception {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
public static void main(String[] args) throws Throwable {
|
2015-01-26 18:13:42 +08:00
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
prepare();
|
2015-01-26 18:13:42 +08:00
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
try (Handler tsa = Handler.init(0, "tsks");) {
|
|
|
|
tsa.start();
|
|
|
|
int port = tsa.getPort();
|
|
|
|
host = "http://localhost:" + port + "/";
|
2010-04-16 10:13:23 +08:00
|
|
|
|
|
|
|
if (args.length == 0) { // Run this test
|
2016-10-20 09:24:47 +08:00
|
|
|
sign("none")
|
|
|
|
.shouldContain("is not timestamped")
|
|
|
|
.shouldHaveExitValue(0);
|
|
|
|
|
|
|
|
sign("badku")
|
|
|
|
.shouldHaveExitValue(0);
|
|
|
|
checkBadKU("badku.jar");
|
|
|
|
|
|
|
|
sign("normal")
|
|
|
|
.shouldNotContain("is not timestamped")
|
|
|
|
.shouldHaveExitValue(0);
|
|
|
|
|
|
|
|
sign("nononce")
|
|
|
|
.shouldHaveExitValue(1);
|
|
|
|
sign("diffnonce")
|
|
|
|
.shouldHaveExitValue(1);
|
|
|
|
sign("baddigest")
|
|
|
|
.shouldHaveExitValue(1);
|
|
|
|
sign("diffalg")
|
|
|
|
.shouldHaveExitValue(1);
|
|
|
|
sign("fullchain")
|
|
|
|
.shouldHaveExitValue(0); // Success, 6543440 solved.
|
|
|
|
sign("bad1")
|
|
|
|
.shouldHaveExitValue(1);
|
|
|
|
sign("bad2")
|
|
|
|
.shouldHaveExitValue(1);
|
|
|
|
sign("bad3")
|
|
|
|
.shouldHaveExitValue(1);
|
|
|
|
sign("nocert")
|
|
|
|
.shouldHaveExitValue(1);
|
|
|
|
|
|
|
|
sign("policy", "-tsapolicyid", "1.2.3")
|
|
|
|
.shouldHaveExitValue(0);
|
|
|
|
checkTimestamp("policy.jar", "1.2.3", "SHA-256");
|
|
|
|
|
|
|
|
sign("diffpolicy", "-tsapolicyid", "1.2.3")
|
|
|
|
.shouldHaveExitValue(1);
|
|
|
|
|
|
|
|
sign("tsaalg", "-tsadigestalg", "SHA")
|
|
|
|
.shouldHaveExitValue(0);
|
|
|
|
checkTimestamp("tsaalg.jar", defaultPolicyId, "SHA-1");
|
|
|
|
|
|
|
|
sign("weak", "-digestalg", "MD5",
|
|
|
|
"-sigalg", "MD5withRSA", "-tsadigestalg", "MD5")
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldMatch("MD5.*-digestalg.*risk")
|
|
|
|
.shouldMatch("MD5.*-tsadigestalg.*risk")
|
|
|
|
.shouldMatch("MD5withRSA.*-sigalg.*risk");
|
|
|
|
checkWeak("weak.jar");
|
|
|
|
|
2016-11-21 22:50:22 -08:00
|
|
|
signWithAliasAndTsa("halfWeak", "old.jar", "old", "-digestalg", "MD5")
|
|
|
|
.shouldHaveExitValue(0);
|
|
|
|
checkHalfWeak("halfWeak.jar");
|
|
|
|
|
|
|
|
// sign with DSA key
|
|
|
|
signWithAliasAndTsa("sign1", "old.jar", "dsakey")
|
|
|
|
.shouldHaveExitValue(0);
|
|
|
|
// sign with RSAkeysize < 1024
|
|
|
|
signWithAliasAndTsa("sign2", "sign1.jar", "weakkeysize")
|
|
|
|
.shouldHaveExitValue(0);
|
|
|
|
checkMultiple("sign2.jar");
|
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
// When .SF or .RSA is missing or invalid
|
|
|
|
checkMissingOrInvalidFiles("normal.jar");
|
2010-04-16 10:13:23 +08:00
|
|
|
} else { // Run as a standalone server
|
|
|
|
System.err.println("Press Enter to quit server");
|
|
|
|
System.in.read();
|
|
|
|
}
|
2014-04-21 10:26:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
private static void checkMissingOrInvalidFiles(String s)
|
|
|
|
throws Throwable {
|
|
|
|
JarUtils.updateJar(s, "1.jar", "-", "META-INF/OLD.SF");
|
|
|
|
verify("1.jar", "-verbose")
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("treated as unsigned")
|
|
|
|
.shouldContain("Missing signature-related file META-INF/OLD.SF");
|
|
|
|
JarUtils.updateJar(s, "2.jar", "-", "META-INF/OLD.RSA");
|
|
|
|
verify("2.jar", "-verbose")
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("treated as unsigned")
|
|
|
|
.shouldContain("Missing block file for signature-related file META-INF/OLD.SF");
|
|
|
|
JarUtils.updateJar(s, "3.jar", "META-INF/OLD.SF");
|
|
|
|
verify("3.jar", "-verbose")
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("treated as unsigned")
|
|
|
|
.shouldContain("Unparsable signature-related file META-INF/OLD.SF");
|
|
|
|
JarUtils.updateJar(s, "4.jar", "META-INF/OLD.RSA");
|
|
|
|
verify("4.jar", "-verbose")
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("treated as unsigned")
|
|
|
|
.shouldContain("Unparsable signature-related file META-INF/OLD.RSA");
|
|
|
|
}
|
|
|
|
|
|
|
|
static OutputAnalyzer jarsigner(List<String> extra)
|
|
|
|
throws Throwable {
|
|
|
|
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jarsigner")
|
|
|
|
.addVMArg("-Duser.language=en")
|
|
|
|
.addVMArg("-Duser.country=US")
|
|
|
|
.addToolArg("-keystore")
|
|
|
|
.addToolArg("tsks")
|
|
|
|
.addToolArg("-storepass")
|
|
|
|
.addToolArg("changeit");
|
|
|
|
for (String s : extra) {
|
|
|
|
if (s.startsWith("-J")) {
|
|
|
|
launcher.addVMArg(s.substring(2));
|
|
|
|
} else {
|
|
|
|
launcher.addToolArg(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ProcessTools.executeCommand(launcher.getCommand());
|
|
|
|
}
|
|
|
|
|
|
|
|
static OutputAnalyzer verify(String file, String... extra)
|
|
|
|
throws Throwable {
|
|
|
|
List<String> args = new ArrayList<>();
|
|
|
|
args.add("-verify");
|
|
|
|
args.add(file);
|
|
|
|
args.addAll(Arrays.asList(extra));
|
|
|
|
return jarsigner(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void checkBadKU(String file) throws Throwable {
|
|
|
|
verify(file)
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("treated as unsigned")
|
|
|
|
.shouldContain("re-run jarsigner with debug enabled");
|
|
|
|
verify(file, "-verbose")
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("Signed by")
|
|
|
|
.shouldContain("treated as unsigned")
|
|
|
|
.shouldContain("re-run jarsigner with debug enabled");
|
|
|
|
verify(file, "-J-Djava.security.debug=jar")
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("SignatureException: Key usage restricted")
|
|
|
|
.shouldContain("treated as unsigned")
|
|
|
|
.shouldContain("re-run jarsigner with debug enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void checkWeak(String file) throws Throwable {
|
|
|
|
verify(file)
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("treated as unsigned")
|
|
|
|
.shouldMatch("weak algorithm that is now disabled.")
|
|
|
|
.shouldMatch("Re-run jarsigner with the -verbose option for more details");
|
|
|
|
verify(file, "-verbose")
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("treated as unsigned")
|
|
|
|
.shouldMatch("weak algorithm that is now disabled by")
|
|
|
|
.shouldMatch("Digest algorithm: .*weak")
|
|
|
|
.shouldMatch("Signature algorithm: .*weak")
|
|
|
|
.shouldMatch("Timestamp digest algorithm: .*weak")
|
|
|
|
.shouldNotMatch("Timestamp signature algorithm: .*weak.*weak")
|
|
|
|
.shouldMatch("Timestamp signature algorithm: .*key.*weak");
|
|
|
|
verify(file, "-J-Djava.security.debug=jar")
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldMatch("SignatureException:.*Disabled");
|
|
|
|
}
|
|
|
|
|
2016-11-21 22:50:22 -08:00
|
|
|
static void checkHalfWeak(String file) throws Throwable {
|
|
|
|
verify(file)
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("treated as unsigned")
|
|
|
|
.shouldMatch("weak algorithm that is now disabled.")
|
|
|
|
.shouldMatch("Re-run jarsigner with the -verbose option for more details");
|
|
|
|
verify(file, "-verbose")
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("treated as unsigned")
|
|
|
|
.shouldMatch("weak algorithm that is now disabled by")
|
|
|
|
.shouldMatch("Digest algorithm: .*weak")
|
|
|
|
.shouldNotMatch("Signature algorithm: .*weak")
|
|
|
|
.shouldNotMatch("Timestamp digest algorithm: .*weak")
|
|
|
|
.shouldNotMatch("Timestamp signature algorithm: .*weak.*weak")
|
|
|
|
.shouldNotMatch("Timestamp signature algorithm: .*key.*weak");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void checkMultiple(String file) throws Throwable {
|
|
|
|
verify(file)
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("jar verified");
|
|
|
|
verify(file, "-verbose", "-certs")
|
|
|
|
.shouldHaveExitValue(0)
|
|
|
|
.shouldContain("jar verified")
|
|
|
|
.shouldMatch("X.509.*CN=dsakey")
|
|
|
|
.shouldNotMatch("X.509.*CN=weakkeysize")
|
|
|
|
.shouldMatch("Signed by .*CN=dsakey")
|
|
|
|
.shouldMatch("Signed by .*CN=weakkeysize")
|
|
|
|
.shouldMatch("Signature algorithm: .*key.*weak");
|
|
|
|
}
|
|
|
|
|
2014-04-21 10:26:03 +08:00
|
|
|
static void checkTimestamp(String file, String policyId, String digestAlg)
|
|
|
|
throws Exception {
|
|
|
|
try (JarFile jf = new JarFile(file)) {
|
|
|
|
JarEntry je = jf.getJarEntry("META-INF/OLD.RSA");
|
|
|
|
try (InputStream is = jf.getInputStream(je)) {
|
2015-10-09 14:21:02 +01:00
|
|
|
byte[] content = is.readAllBytes();
|
2014-04-21 10:26:03 +08:00
|
|
|
PKCS7 p7 = new PKCS7(content);
|
|
|
|
SignerInfo[] si = p7.getSignerInfos();
|
|
|
|
if (si == null || si.length == 0) {
|
|
|
|
throw new Exception("Not signed");
|
|
|
|
}
|
|
|
|
PKCS9Attribute p9 = si[0].getUnauthenticatedAttributes()
|
|
|
|
.getAttribute(PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_OID);
|
|
|
|
PKCS7 tsToken = new PKCS7((byte[]) p9.getValue());
|
|
|
|
TimestampToken tt =
|
|
|
|
new TimestampToken(tsToken.getContentInfo().getData());
|
|
|
|
if (!tt.getHashAlgorithm().toString().equals(digestAlg)) {
|
|
|
|
throw new Exception("Digest alg different");
|
|
|
|
}
|
|
|
|
if (!tt.getPolicyID().equals(policyId)) {
|
|
|
|
throw new Exception("policyId different");
|
|
|
|
}
|
|
|
|
}
|
2010-04-16 10:13:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
static int which = 0;
|
|
|
|
|
2010-04-16 10:13:23 +08:00
|
|
|
/**
|
2016-10-20 09:24:47 +08:00
|
|
|
* @param extra more args given to jarsigner
|
2010-04-16 10:13:23 +08:00
|
|
|
*/
|
2016-10-20 09:24:47 +08:00
|
|
|
static OutputAnalyzer sign(String path, String... extra)
|
|
|
|
throws Throwable {
|
2016-11-21 22:50:22 -08:00
|
|
|
String alias = path.equals("badku") ? "badku" : "old";
|
|
|
|
return signWithAliasAndTsa(path, "old.jar", alias, extra);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OutputAnalyzer signWithAliasAndTsa (String path, String jar,
|
|
|
|
String alias, String...extra) throws Throwable {
|
2016-10-20 09:24:47 +08:00
|
|
|
which++;
|
|
|
|
System.err.println("\n>> Test #" + which + ": " + Arrays.toString(extra));
|
|
|
|
List<String> args = List.of("-J-Djava.security.egd=file:/dev/./urandom",
|
2016-11-21 22:50:22 -08:00
|
|
|
"-debug", "-signedjar", path + ".jar", jar, alias);
|
2016-10-20 09:24:47 +08:00
|
|
|
args = new ArrayList<>(args);
|
|
|
|
if (!path.equals("none") && !path.equals("badku")) {
|
|
|
|
args.add("-tsa");
|
|
|
|
args.add(host + path);
|
2010-04-16 10:13:23 +08:00
|
|
|
}
|
2016-10-20 09:24:47 +08:00
|
|
|
args.addAll(Arrays.asList(extra));
|
|
|
|
return jarsigner(args);
|
|
|
|
}
|
2013-09-19 10:40:16 +08:00
|
|
|
|
2016-10-20 09:24:47 +08:00
|
|
|
static void prepare() throws Exception {
|
|
|
|
jdk.testlibrary.JarUtils.createJar("old.jar", "A");
|
|
|
|
Files.deleteIfExists(Paths.get("tsks"));
|
|
|
|
keytool("-alias ca -genkeypair -ext bc -dname CN=CA");
|
|
|
|
keytool("-alias old -genkeypair -dname CN=old");
|
2016-11-21 22:50:22 -08:00
|
|
|
keytool("-alias dsakey -genkeypair -keyalg DSA -dname CN=dsakey");
|
|
|
|
keytool("-alias weakkeysize -genkeypair -keysize 512 -dname CN=weakkeysize");
|
2016-10-20 09:24:47 +08:00
|
|
|
keytool("-alias badku -genkeypair -dname CN=badku");
|
|
|
|
keytool("-alias ts -genkeypair -dname CN=ts");
|
|
|
|
keytool("-alias tsweak -genkeypair -keysize 512 -dname CN=tsbad1");
|
|
|
|
keytool("-alias tsbad1 -genkeypair -dname CN=tsbad1");
|
|
|
|
keytool("-alias tsbad2 -genkeypair -dname CN=tsbad2");
|
|
|
|
keytool("-alias tsbad3 -genkeypair -dname CN=tsbad3");
|
|
|
|
|
|
|
|
gencert("old");
|
2016-11-21 22:50:22 -08:00
|
|
|
gencert("dsakey");
|
|
|
|
gencert("weakkeysize");
|
2016-10-20 09:24:47 +08:00
|
|
|
gencert("badku", "-ext ku:critical=keyAgreement");
|
|
|
|
gencert("ts", "-ext eku:critical=ts");
|
|
|
|
gencert("tsweak", "-ext eku:critical=ts");
|
|
|
|
gencert("tsbad1");
|
|
|
|
gencert("tsbad2", "-ext eku=ts");
|
|
|
|
gencert("tsbad3", "-ext eku:critical=cs");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gencert(String alias, String... extra) throws Exception {
|
|
|
|
keytool("-alias " + alias + " -certreq -file " + alias + ".req");
|
|
|
|
String genCmd = "-gencert -alias ca -infile " +
|
|
|
|
alias + ".req -outfile " + alias + ".cert";
|
|
|
|
for (String s : extra) {
|
|
|
|
genCmd += " " + s;
|
2013-09-19 10:40:16 +08:00
|
|
|
}
|
2016-10-20 09:24:47 +08:00
|
|
|
keytool(genCmd);
|
|
|
|
keytool("-alias " + alias + " -importcert -file " + alias + ".cert");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void keytool(String cmd) throws Exception {
|
|
|
|
cmd = "-keystore tsks -storepass changeit -keypass changeit " +
|
|
|
|
"-keyalg rsa -validity 200 " + cmd;
|
|
|
|
sun.security.tools.keytool.Main.main(cmd.split(" "));
|
2010-04-16 10:13:23 +08:00
|
|
|
}
|
|
|
|
}
|