Path: blob/master/test/jdk/sun/security/x509/URICertStore/ExtensionsWithLDAP.java
41153 views
/*1* Copyright (c) 2015, 2021, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* @test27* @bug 813470828* @summary Check if LDAP resources from CRLDP and AIA extensions can be loaded29* @run main/othervm -Djdk.net.hosts.file=${test.src}/CRLDP30* -Dcom.sun.security.enableCRLDP=true31* ExtensionsWithLDAP CRLDP ldap.host.for.crldp32* @modules jdk.security.auth33* @run main/othervm -Djdk.net.hosts.file=${test.src}/AIA34* -Dcom.sun.security.enableAIAcaIssuers=true35* ExtensionsWithLDAP AIA ldap.host.for.aia36*/3738import java.io.ByteArrayInputStream;39import java.io.IOException;40import java.net.InetSocketAddress;41import java.net.Socket;42import java.security.cert.CertPath;43import java.security.cert.CertPathValidator;44import java.security.cert.CertPathValidatorException;45import java.security.cert.CertificateException;46import java.security.cert.CertificateFactory;47import java.security.cert.PKIXParameters;48import java.security.cert.TrustAnchor;49import java.security.cert.X509Certificate;50import java.util.ArrayList;51import java.util.Arrays;52import java.util.HashSet;53import java.util.List;54import java.util.Set;55import java.util.function.Consumer;5657public class ExtensionsWithLDAP {5859/*60* Certificate:61* Data:62* Version: 3 (0x2)63* Serial Number: 11174053930990688938 (0x9b1236d8f9c1daaa)64* Signature Algorithm: sha512WithRSAEncryption65* Issuer: CN=Root66* Validity67* Not Before: Sep 1 18:03:59 2015 GMT68* Not After : Jan 17 18:03:59 2043 GMT69* Subject: CN=Root70*/71private static final String CA_CERT = ""72+ "-----BEGIN CERTIFICATE-----\n"73+ "MIIC8TCCAdmgAwIBAgIJAJsSNtj5wdqqMA0GCSqGSIb3DQEBDQUAMA8xDTALBgNV\n"74+ "BAMMBFJvb3QwHhcNMTUwOTAxMTgwMzU5WhcNNDMwMTE3MTgwMzU5WjAPMQ0wCwYD\n"75+ "VQQDDARSb290MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvj892vPm\n"76+ "bB++x9QqqyBveP+ZqQ2B1stV7vh5JmDnOTevkZUOcemp3SXu/esNLSbpL+fARYXH\n"77+ "V5ubnrfip6RbvcxPfVIIDJrRTLIIsU6W7M6/LJLbLkEVGy4ZV4IHkOw9W2O92rcv\n"78+ "BkoqhzZnOTGR6uT3rRcKx4RevEKBKhZO+OPPf//lnckOybmYL7t7yQrajzHro76b\n"79+ "QTXYjAUq/DKhglXfC7vF/JzlAvG2IunGmIfjGcnuDo/9X3Bxef/q5TxCS35fvb7t\n"80+ "svC+g2QhTcBkQh4uNW2jSjlTIVp1uErCfP5aCjLaez5mqmb1hxPIlcvsNR23HwU6\n"81+ "bQO7z7NBo9Do6QIDAQABo1AwTjAdBgNVHQ4EFgQUmLZNOBBkqdYoElyxklPYHmAb\n"82+ "QXIwHwYDVR0jBBgwFoAUmLZNOBBkqdYoElyxklPYHmAbQXIwDAYDVR0TBAUwAwEB\n"83+ "/zANBgkqhkiG9w0BAQ0FAAOCAQEAYV4fOhDi5q7+XNXCxO8Eil2frR9jqdP4LaQp\n"84+ "3L0evW0gvPX68s2WmkPWzIu4TJcpdGFQqxyQFSXuKBXjthyiln77QItGTHWeafES\n"85+ "q5ESrKdSaJZq1bTIrrReCIP74f+fY/F4Tnb3dCqzaljXfzpdbeRsIW6gF71xcOUQ\n"86+ "nnPEjGVPLUegN+Wn/jQpeLxxIB7FmNXncdRUfMfZ43xVSKuMCy1UUYqJqTa/pXZj\n"87+ "jCMeRPThRjRqHlJ69jStfWUQATbLyj9KN09rUaJxzmUSt61UqJi7sjcGySaCjAJc\n"88+ "IcCdVmX/DmRLsdv8W36O3MgrvpT1zR3kaAlv2d8HppnBqcL3xg==\n"89+ "-----END CERTIFICATE-----";9091/*92* Certificate:93* Data:94* Version: 3 (0x2)95* Serial Number: 7 (0x7)96* Signature Algorithm: sha512WithRSAEncryption97* Issuer: CN=Root98* Validity99* Not Before: Sep 1 18:03:59 2015 GMT100* Not After : Jan 17 18:03:59 2043 GMT101* Subject: CN=EE102* ...103* X509v3 extensions:104* X509v3 CRL Distribution Points:105* Full Name:106* URI:ldap://ldap.host.for.crldp/main.crl107* Authority Information Access:108* CA Issuers - URI:ldap://ldap.host.for.aia/dc=Root?cACertificate109*/110private static final String EE_CERT = ""111+ "-----BEGIN CERTIFICATE-----\n"112+ "MIIDHTCCAgWgAwIBAgIBBzANBgkqhkiG9w0BAQ0FADAPMQ0wCwYDVQQDDARSb290\n"113+ "MB4XDTE1MDkwMTE4MDM1OVoXDTQzMDExNzE4MDM1OVowDTELMAkGA1UEAwwCRUUw\n"114+ "ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCpyz97liuWPDYcLH9TX8Bi\n"115+ "T78olCmAfmevvch6ncXUVuCzbdaKuKXwn4EVbDszsVJLoK5zdtP+X3iDhutj+IgK\n"116+ "mLhuczF3M9VIcWr+JJUyTH4+3h/RT8cjCDZOmk9iXkb5ifruVsLqzb9g+Vp140Oz\n"117+ "7leikne7KmclHvTfvFd0WDI7Gb9vo4f5rT717BXJ/n+M6pNk8DLpLiEu6eziYvXR\n"118+ "v5x+t5Go3x0eCXdaxEQUf2j876Wfr2qHRJK7lDfFe1DDsMg/KpKGiILYZ+g2qtVM\n"119+ "ZSxtp5BZEtfB5qV/IE5kWO+mCIAGpXSZIdbERR6pZUq8GLEe1T9e+sO6H24w2F19\n"120+ "AgMBAAGjgYUwgYIwNAYDVR0fBC0wKzApoCegJYYjbGRhcDovL2xkYXAuaG9zdC5m\n"121+ "b3IuY3JsZHAvbWFpbi5jcmwwSgYIKwYBBQUHAQEEPjA8MDoGCCsGAQUFBzAChi5s\n"122+ "ZGFwOi8vbGRhcC5ob3N0LmZvci5haWEvZGM9Um9vdD9jQUNlcnRpZmljYXRlMA0G\n"123+ "CSqGSIb3DQEBDQUAA4IBAQBWDfZHpuUx0yn5d3+BuztFqoks1MkGdk+USlH0TB1/\n"124+ "gWWBd+4S4PCKlpSur0gj2rMW4fP5HQfNlHci8JV8/bG4KuKRAXW56dg1818Hl3pc\n"125+ "iIrUSRn8uUjH3p9qb+Rb/u3mmVQRyJjN2t/zceNsO8/+Dd808OB9aEwGs8lMT0nn\n"126+ "ZYaaAqYz1GIY/Ecyx1vfEZEQ1ljo6i/r70C3igbypBUShxSiGsleiVTLOGNA+MN1\n"127+ "/a/Qh0bkaQyTGqK3bwvzzMeQVqWu2EWTBD/PmND5ExkpRICdv8LBVXfLnpoBr4lL\n"128+ "hnxn9+e0Ah+t8dS5EKfn44w5bI5PCu2bqxs6RCTxNjcY\n"129+ "-----END CERTIFICATE-----";130131public static void main(String[] args) throws Exception {132String extension = args[0];133String targetHost = args[1];134135X509Certificate trustedCert = loadCertificate(CA_CERT);136X509Certificate eeCert = loadCertificate(EE_CERT);137138Set<TrustAnchor> trustedCertsSet = new HashSet<>();139trustedCertsSet.add(new TrustAnchor(trustedCert, null));140141CertPath cp = (CertPath) CertificateFactory.getInstance("X509")142.generateCertPath(Arrays.asList(eeCert));143144// CertPath validator should try to parse CRLDP and AIA extensions,145// and load CRLs/certs which they point to.146// If proxy server catches requests for resolving host names147// which extensions contain, then it means that CertPath validator148// tried to load CRLs/certs which they point to.149List<String> hosts = new ArrayList<>();150Consumer<Socket> socketConsumer = (Socket socket) -> {151InetSocketAddress remoteAddress152= (InetSocketAddress) socket.getRemoteSocketAddress();153hosts.add(remoteAddress.getHostName());154};155try (SocksProxy proxy = SocksProxy.startProxy(socketConsumer)) {156CertPathValidator.getInstance("PKIX").validate(cp,157new PKIXParameters(trustedCertsSet));158throw new RuntimeException("CertPathValidatorException not thrown");159} catch (CertPathValidatorException cpve) {160System.out.println("Expected exception: " + cpve);161}162163if (!hosts.contains(targetHost)) {164throw new RuntimeException(165String.format("The %s from %s extension is not requested",166targetHost, extension));167}168169System.out.println("Test passed");170}171172// load a X509 certificate173public static X509Certificate loadCertificate(String s)174throws IOException, CertificateException {175176try (ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes())) {177return (X509Certificate) CertificateFactory.getInstance("X509")178.generateCertificate(is);179}180}181}182183184