a95ee5ada2
Reviewed-by: weijun
352 lines
18 KiB
Java
352 lines
18 KiB
Java
/*
|
|
* Copyright (C) 2022 THL A29 Limited, a Tencent company. 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 javax.net.ssl.KeyManagerFactory;
|
|
import javax.net.ssl.SSLContext;
|
|
import javax.net.ssl.TrustManagerFactory;
|
|
import java.io.*;
|
|
import java.net.InetAddress;
|
|
import java.security.KeyFactory;
|
|
import java.security.KeyStore;
|
|
import java.security.PrivateKey;
|
|
import java.security.cert.Certificate;
|
|
import java.security.cert.CertificateFactory;
|
|
import java.security.spec.PKCS8EncodedKeySpec;
|
|
import java.util.Base64;
|
|
|
|
// A template to use "www.example.com" as the server name. The caller should
|
|
// set a virtual hosts file with System Property, "jdk.net.hosts.file". This
|
|
// class will map the loopback address to "www.example.com", and write to
|
|
// the specified hosts file.
|
|
public enum SSLExampleCert {
|
|
// Version: 3 (0x2)
|
|
// Serial Number: 15223159159760931473 (0xd34386999cc8ca91)
|
|
// Signature Algorithm: sha256WithRSAEncryption
|
|
// Issuer: C=US, ST=California, O=Example, OU=Test
|
|
// Validity
|
|
// Not Before: Jan 26 04:50:29 2022 GMT
|
|
// Not After : Feb 25 04:50:29 2022 GMT
|
|
// Subject: C=US, ST=California, O=Example, OU=Test
|
|
// Public Key Algorithm: rsaEncryption
|
|
CA_RSA("RSA",
|
|
"""
|
|
-----BEGIN CERTIFICATE-----
|
|
MIIDXDCCAkSgAwIBAgIJANNDhpmcyMqRMA0GCSqGSIb3DQEBCwUAMEMxCzAJBgNV
|
|
BAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRAwDgYDVQQKDAdFeGFtcGxlMQ0w
|
|
CwYDVQQLDARUZXN0MB4XDTIyMDEyNjA0NTAyOVoXDTIyMDIyNTA0NTAyOVowQzEL
|
|
MAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExEDAOBgNVBAoMB0V4YW1w
|
|
bGUxDTALBgNVBAsMBFRlc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB
|
|
AQDnOL2hB/GYyYziXo/ppxi7V1LfMMLeHt0lZbYlrmNxUlln4naI4B4Lkg75eb1Y
|
|
DgC7MZQd5nKijK9Dkq52Z2zLxaqBYnLxKJ36qKPqbtTL3I8mfUvVEeNIDN/8YTAt
|
|
suIEQi54dNtQVrB4YReMdnUq+xCKLAfEio4QLEQr7KtyCBXHZpM7RYRT0giQFvDU
|
|
2kls9lFLeqKXgocnA7VpoL0V12hpxDeJoRm1szf0M5YXGJumQLaE5qM/+P2OOhw/
|
|
T+xkupy2GF02s6FBXkH7NrFIjtuBSaVhSvCG/N7njWSn339thr3kiPEaCS4KSH5E
|
|
E6FEazxZQrTCbkQQ+v3y1pS1AgMBAAGjUzBRMA8GA1UdEwEB/wQFMAMBAf8wHQYD
|
|
VR0OBBYEFMFw2FWUvwZx3FJjm1G9TujCjAJSMB8GA1UdIwQYMBaAFMFw2FWUvwZx
|
|
3FJjm1G9TujCjAJSMA0GCSqGSIb3DQEBCwUAA4IBAQCJsJjeYcT/GtKp64C+9K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|
// Version: 3 (0x2)
|
|
// Serial Number: 14845986384898254166 (0xce0789f5ac256556)
|
|
// Signature Algorithm: sha1WithRSAEncryption
|
|
// Issuer: C=US, ST=California, O=Example, OU=Test
|
|
// Validity
|
|
// Not Before: Jan 26 04:50:29 2022 GMT
|
|
// Not After : Feb 25 04:50:29 2022 GMT
|
|
// Subject: C=US, ST=California, O=Example, OU=Test, CN=www.example.com
|
|
// Public Key Algorithm: rsaEncryption
|
|
SERVER_EXAMPLE_RSA("RSA",
|
|
"""
|
|
-----BEGIN CERTIFICATE-----
|
|
MIIDjDCCAnSgAwIBAgIJAM4HifWsJWVWMA0GCSqGSIb3DQEBBQUAMEMxCzAJBgNV
|
|
BAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRAwDgYDVQQKDAdFeGFtcGxlMQ0w
|
|
CwYDVQQLDARUZXN0MB4XDTIyMDEyNjA0NTAyOVoXDTIyMDIyNTA0NTAyOVowXTEL
|
|
MAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExEDAOBgNVBAoMB0V4YW1w
|
|
bGUxDTALBgNVBAsMBFRlc3QxGDAWBgNVBAMMD3d3dy5leGFtcGxlLmNvbTCCASIw
|
|
DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK/i4bVSAF6/aFHyzrFAON8Uwn2a
|
|
Q9jFoAMowUkH6+PCexlt4wCOEMVH9IQPa1yzeVlkYRqHggz3i6aVYvd27Q+c6vOF
|
|
FuRegWdJusyEuoXd5nwxSGiZZMLmFQswODSPmucroCG+Tq9RKY5oEKiRP8tUDqyn
|
|
eE52PaeSR2Q6mng7BM5Llj7amVgimO3jlH2AWLLpHNTkc3j/M1QrLFV2PqN4dpxT
|
|
OeFuVWzpfTWJSH+9Kq4u/zBMbYl8ON7DSgJAzc2BOw8VPIYIK6JIw6IDbLn4dIQf
|
|
GikdgHKB2K6EDOc9LuX6UqQQODDFU88muAyPgpGfUQjxKZeUoTqoAFyisI0CAwEA
|
|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LjOnaDIXEILbgqnlGfTh8vvVIKRcJAoGALF6Q1Ca2GIx4kJZ2/Az5
|
|
qx89q95wQoWVHcJCA91g/GKg/bD0ZJMWEhhVH3QcnDOU7afRGKOPGdSSBdPQnspQ
|
|
1OPpPjA3U5Mffy5PJ2bHTpPBeeHOzDO4Jt073zK3N81QovJzS8COoOwuGdcocURH
|
|
mFMnEtK7d+EK1C1NTWDyNzU=
|
|
"""),
|
|
|
|
|
|
// Version: 3 (0x2)
|
|
// Serial Number: 14845986384898254167 (0xce0789f5ac256557)
|
|
// Signature Algorithm: sha1WithRSAEncryption
|
|
// Issuer: C=US, ST=California, O=Example, OU=Test
|
|
// Validity
|
|
// Not Before: Jan 26 04:50:29 2022 GMT
|
|
// Not After : Feb 25 04:50:29 2022 GMT
|
|
// Subject: C=US, ST=California, O=Example, OU=Test, CN=Do-Not-Reply
|
|
// Public Key Algorithm: rsaEncryption
|
|
CLIENT_EXAMPLE_RSA("RSA",
|
|
"""
|
|
-----BEGIN CERTIFICATE-----
|
|
MIIDkjCCAnqgAwIBAgIJAM4HifWsJWVXMA0GCSqGSIb3DQEBBQUAMEMxCzAJBgNV
|
|
BAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRAwDgYDVQQKDAdFeGFtcGxlMQ0w
|
|
CwYDVQQLDARUZXN0MB4XDTIyMDEyNjA0NTAyOVoXDTIyMDIyNTA0NTAyOVowWjEL
|
|
MAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExEDAOBgNVBAoMB0V4YW1w
|
|
bGUxDTALBgNVBAsMBFRlc3QxFTATBgNVBAMMDERvLU5vdC1SZXBseTCCASIwDQYJ
|
|
KoZIhvcNAQEBBQADggEPADCCAQoCggEBAODbsCteHcAg3dktUO5fiPTytIahKZMg
|
|
U2h6h0+BT803tYcdN6WDHnLNlU/3iFnBMpyTwzWhYIftC9c/TIkXBAGfWl4HHQgc
|
|
x08NHPms0E+GF6CDthvHERSvRCBrIyVna0KIZP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ME
|
|
A3tFfo5Kw9hq+6z8n8eZ26BDHXiy+Tysdchksrb20JdVv4LiG+ZVzGT7hwKBgG+b
|
|
Bp0IF4JFh6PPBLWxRBeWT2MH1Mkr0R2r945W91fj72BbMeU63Oj/42u4vx5xEiZx
|
|
xxQw+t2AvzTsMAicqOr1CdvxBoz4L+neUnZ1DApBtxKhIPnG7EtGiOuftToIuL0C
|
|
gP4EmhB9RbH0mk/83vqxDUptRGl4+QiJHB76H3DXAoGASGT6tfs1/92rGqe0COm3
|
|
aHpelvW7Wwr9AuBumE7/ur/JWAAiM4pm6w7m5H5duYZtG3eWz1s+jvjy8jIPBIkN
|
|
RA2DoneC2J/tsRRNFBqZrOWon5Jv4dc9R79qR13B/Oqu8H6FYSB2oDyh67Csud3q
|
|
PRrz4o7UKM+HQ/obr1rUYqM=
|
|
""");
|
|
|
|
final String keyAlgo;
|
|
final String certStr;
|
|
final String privateKeyStr;
|
|
|
|
// Trusted certificate
|
|
private final static SSLExampleCert[] TRUSTED_CERTS = {
|
|
SSLExampleCert.CA_RSA
|
|
};
|
|
|
|
// Server certificate.
|
|
private final static SSLExampleCert[] SERVER_CERTS = {
|
|
SSLExampleCert.SERVER_EXAMPLE_RSA
|
|
};
|
|
|
|
// Client certificate.
|
|
private final static SSLExampleCert[] CLIENT_CERTS = {
|
|
SSLExampleCert.CLIENT_EXAMPLE_RSA
|
|
};
|
|
|
|
// Set "www.example.com" to loopback address.
|
|
static {
|
|
String hostsFileName = System.getProperty("jdk.net.hosts.file");
|
|
String loopbackHostname =
|
|
InetAddress.getLoopbackAddress().getHostAddress() +
|
|
" " + "www.example.com www.example.com.\n";
|
|
try (FileWriter writer= new FileWriter(hostsFileName, false)) {
|
|
writer.write(loopbackHostname);
|
|
} catch (IOException ioe) {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
SSLExampleCert(String keyAlgo, String certStr, String privateKeyStr) {
|
|
this.keyAlgo = keyAlgo;
|
|
this.certStr = certStr;
|
|
this.privateKeyStr = privateKeyStr;
|
|
}
|
|
|
|
public static SSLContext createClientSSLContext() throws Exception {
|
|
return createSSLContext(TRUSTED_CERTS, CLIENT_CERTS);
|
|
}
|
|
|
|
public static SSLContext createServerSSLContext() throws Exception {
|
|
return createSSLContext(TRUSTED_CERTS, SERVER_CERTS);
|
|
}
|
|
|
|
private static SSLContext createSSLContext(
|
|
SSLExampleCert[] trustedCerts,
|
|
SSLExampleCert[] endEntityCerts) throws Exception {
|
|
char[] passphrase = "passphrase".toCharArray();
|
|
|
|
// Generate certificate from cert string.
|
|
CertificateFactory cf = CertificateFactory.getInstance("X.509");
|
|
|
|
// Import the trusted certs.
|
|
KeyStore ts = null; // trust store
|
|
if (trustedCerts != null && trustedCerts.length != 0) {
|
|
ts = KeyStore.getInstance("PKCS12");
|
|
ts.load(null, null);
|
|
|
|
Certificate[] trustedCert = new Certificate[trustedCerts.length];
|
|
for (int i = 0; i < trustedCerts.length; i++) {
|
|
try (ByteArrayInputStream is = new ByteArrayInputStream(
|
|
trustedCerts[i].certStr.getBytes())) {
|
|
trustedCert[i] = cf.generateCertificate(is);
|
|
}
|
|
|
|
ts.setCertificateEntry("trusted-cert-" +
|
|
trustedCerts[i].name(), trustedCert[i]);
|
|
}
|
|
}
|
|
|
|
// Import the key materials.
|
|
KeyStore ks = null; // trust store
|
|
if (endEntityCerts != null && endEntityCerts.length != 0) {
|
|
ks = KeyStore.getInstance("PKCS12");
|
|
ks.load(null, null);
|
|
|
|
for (SSLExampleCert endEntityCert : endEntityCerts) {
|
|
// generate the private key.
|
|
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(
|
|
Base64.getMimeDecoder()
|
|
.decode(endEntityCert.privateKeyStr));
|
|
KeyFactory kf =
|
|
KeyFactory.getInstance(
|
|
endEntityCert.keyAlgo);
|
|
PrivateKey priKey = kf.generatePrivate(priKeySpec);
|
|
|
|
// generate certificate chain
|
|
Certificate keyCert;
|
|
try (ByteArrayInputStream is = new ByteArrayInputStream(
|
|
endEntityCert.certStr.getBytes())) {
|
|
keyCert = cf.generateCertificate(is);
|
|
}
|
|
|
|
Certificate[] chain = new Certificate[]{keyCert};
|
|
|
|
// import the key entry.
|
|
ks.setKeyEntry("end-entity-cert-" +
|
|
endEntityCert.name(),
|
|
priKey, passphrase, chain);
|
|
}
|
|
}
|
|
|
|
// Create an SSLContext object.
|
|
TrustManagerFactory tmf =
|
|
TrustManagerFactory.getInstance("PKIX");
|
|
tmf.init(ts);
|
|
|
|
SSLContext context = SSLContext.getInstance("TLS");
|
|
if (endEntityCerts != null && endEntityCerts.length != 0) {
|
|
KeyManagerFactory kmf =
|
|
KeyManagerFactory.getInstance("NewSunX509");
|
|
kmf.init(ks, passphrase);
|
|
|
|
context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
|
} else {
|
|
context.init(null, tmf.getTrustManagers(), null);
|
|
}
|
|
|
|
return context;
|
|
}
|
|
}
|