Path: blob/master/test/micro/org/openjdk/bench/java/security/TestCertificates.java
66646 views
/*1* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package org.openjdk.bench.java.security;2425import java.io.ByteArrayInputStream;26import java.io.IOException;27import java.nio.charset.StandardCharsets;28import java.security.GeneralSecurityException;29import java.security.Key;30import java.security.KeyFactory;31import java.security.KeyStore;32import java.security.cert.Certificate;33import java.security.cert.CertificateFactory;34import java.security.spec.PKCS8EncodedKeySpec;35import java.util.Base64;3637/**38* This class contains a 3-certificate chain for use in TLS tests.39* The method {@link #getKeyStore()} returns a keystore with a single entry40* containing one server+one intermediate CA certificate.41* Server's CN and subjectAltName are both set to "client"42*43* The method {@link #getTrustStore()} returns a keystore with a single entry44* containing the root CA certificate used for signing the intermediate CA.45*/46class TestCertificates {4748// "/C=US/ST=CA/O=Test Root CA, Inc."49// basicConstraints=critical, CA:true50// subjectKeyIdentifier = hash51// authorityKeyIdentifier = keyid:always52// keyUsage = keyCertSign53private static final String ROOT_CA_CERT =54"-----BEGIN CERTIFICATE-----\n" +55"MIIB0jCCAXigAwIBAgIUE+wUdx22foJXSQzD3hpCNCqITLEwCgYIKoZIzj0EAwIw\n" +56"NzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRswGQYDVQQKDBJUZXN0IFJvb3Qg\n" +57"Q0EsIEluYy4wIBcNMjIwNDEyMDcxMzMzWhgPMjEyMjAzMTkwNzEzMzNaMDcxCzAJ\n" +58"BgNVBAYTAlVTMQswCQYDVQQIDAJDQTEbMBkGA1UECgwSVGVzdCBSb290IENBLCBJ\n" +59"bmMuMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBKye/mwO0V0WLr71tf8auFEz\n" +60"EmqhaYWauaP17Fb33fRAeG8aVp9c4B0isv/VgcqSTRMG0SJjbx7ttSYwR/JNhqNg\n" +61"MF4wDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUpfGt4bjadmVzWeXAiSMp9pLU\n" +62"RMkwHwYDVR0jBBgwFoAUpfGt4bjadmVzWeXAiSMp9pLURMkwCwYDVR0PBAQDAgIE\n" +63"MAoGCCqGSM49BAMCA0gAMEUCIBF8YyD5BBuhkFNV/3rNmvvMuvWUAECJ8rrUg8kr\n" +64"J8zpAiEAzbZQsC/IZ0wVNd4lqHn6/Ih5v7vhCgkg95KCP1NhBnU=\n" +65"-----END CERTIFICATE-----";6667// "/C=US/ST=CA/O=Test Intermediate CA, Inc."68// basicConstraints=critical, CA:true, pathlen:069// subjectKeyIdentifier = hash70// authorityKeyIdentifier = keyid:always71// keyUsage = keyCertSign72private static final String CA_CERT =73"-----BEGIN CERTIFICATE-----\n" +74"MIIB3TCCAYOgAwIBAgIUQ+lTbsDcIQ1UUg0RGdpJB6JMXpcwCgYIKoZIzj0EAwIw\n" +75"NzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRswGQYDVQQKDBJUZXN0IFJvb3Qg\n" +76"Q0EsIEluYy4wIBcNMjIwNDEyMDcxMzM0WhgPMjEyMjAzMTkwNzEzMzRaMD8xCzAJ\n" +77"BgNVBAYTAlVTMQswCQYDVQQIDAJDQTEjMCEGA1UECgwaVGVzdCBJbnRlcm1lZGlh\n" +78"dGUgQ0EsIEluYy4wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQ7DsKCSQkP5oT2\n" +79"Wx0gf40N+H/F75w1YmPm6dp2wiQ6JPMN/4En87Ylx0ISJkeXJLxrbLvu2xZ+aonM\n" +80"kckNh/ERo2MwYTASBgNVHRMBAf8ECDAGAQH/AgEAMB0GA1UdDgQWBBTqP6hB5Ibr\n" +81"aivot/zWSMKr8ZkCVzAfBgNVHSMEGDAWgBSl8a3huNp2ZXNZ5cCJIyn2ktREyTAL\n" +82"BgNVHQ8EBAMCAgQwCgYIKoZIzj0EAwIDSAAwRQIhAM0vCIV938aqGAEmELIA8Kc4\n" +83"X+kOc4LGE0R7sMiBAbXuAiBlbNVaskKYRHIEGHEtIWet6Ufi3w9NMrycEbBZ+v5o\n" +84"gA==\n" +85"-----END CERTIFICATE-----";8687// "/C=US/ST=CA/O=Test Server/CN=client"88// subjectKeyIdentifier = hash89// authorityKeyIdentifier = keyid:always90// keyUsage = digitalSignature91// subjectAltName = DNS:client92private static final String SERVER_CERT =93"-----BEGIN CERTIFICATE-----\n" +94"MIIB5TCCAYygAwIBAgIUNWe754lZoDc6wNs9Vsev/h9TMicwCgYIKoZIzj0EAwIw\n" +95"PzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMSMwIQYDVQQKDBpUZXN0IEludGVy\n" +96"bWVkaWF0ZSBDQSwgSW5jLjAgFw0yMjA0MTIwNzEzMzRaGA8yMTIyMDMxOTA3MTMz\n" +97"NFowQTELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRQwEgYDVQQKDAtUZXN0IFNl\n" +98"cnZlcjEPMA0GA1UEAwwGY2xpZW50MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE\n" +99"o6zUz5QmzmfHL2xRifvaJenggck/Dlu6KC4v4rGXug69R7tWKWuRUsbSFLy29Rii\n" +100"F7V1wjFhsyGAzNyKf/KlmaNiMGAwHQYDVR0OBBYEFHz32VSnXBF4WdLDOe7e3hF9\n" +101"yDxmMB8GA1UdIwQYMBaAFOo/qEHkhutqK+i3/NZIwqvxmQJXMAsGA1UdDwQEAwIH\n" +102"gDARBgNVHREECjAIggZjbGllbnQwCgYIKoZIzj0EAwIDRwAwRAIgWsCn2LIElgVs\n" +103"VihcQznvBemWneEcmnp/Bw+lwk86KQ8CIA3loL7P/0/Ft/xXtClxJfyxEoZ/Az1n\n" +104"HTTjbe6ZnN0Y\n" +105"-----END CERTIFICATE-----";106107private static final String serverkey =108//"-----BEGIN PRIVATE KEY-----\n" +109"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgKb9cKLH++BgA9CL1\n" +110"cdCLHpD0poPJ/uAkafGXDJBR67ChRANCAASjrNTPlCbOZ8cvbFGJ+9ol6eCByT8O\n" +111"W7ooLi/isZe6Dr1Hu1Ypa5FSxtIUvLb1GKIXtXXCMWGzIYDM3Ip/8qWZ";112// + "\n-----END PRIVATE KEY-----";113114private TestCertificates() {}115116public static KeyStore getKeyStore() throws GeneralSecurityException, IOException {117KeyStore result = KeyStore.getInstance("JKS");118result.load(null, null);119CertificateFactory cf = CertificateFactory.getInstance("X.509");120Certificate serverCert = cf.generateCertificate(121new ByteArrayInputStream(122TestCertificates.SERVER_CERT.getBytes(StandardCharsets.ISO_8859_1)));123Certificate caCert = cf.generateCertificate(124new ByteArrayInputStream(125CA_CERT.getBytes(StandardCharsets.ISO_8859_1)));126KeyFactory kf = KeyFactory.getInstance("EC");127PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(128Base64.getMimeDecoder().decode(serverkey));129Key key = kf.generatePrivate(ks);130Certificate[] chain = {serverCert, caCert};131132result.setKeyEntry("server", key, new char[0], chain);133return result;134}135136public static KeyStore getTrustStore() throws GeneralSecurityException, IOException {137KeyStore result = KeyStore.getInstance("JKS");138result.load(null, null);139CertificateFactory cf = CertificateFactory.getInstance("X.509");140Certificate rootcaCert = cf.generateCertificate(141new ByteArrayInputStream(142ROOT_CA_CERT.getBytes(StandardCharsets.ISO_8859_1)));143144result.setCertificateEntry("testca", rootcaCert);145return result;146}147}148149150