Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/validator/EndEntityExtensionCheck.java
38838 views
/*1* Copyright (c) 2015, 2019, 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*/2223/*24* @test25* @bug 807611726* @summary EndEntityChecker should not process custom extensions27* after PKIX validation28* @run main/othervm -Djdk.security.allowNonCaAnchor EndEntityExtensionCheck29*/3031import java.io.ByteArrayInputStream;32import java.io.File;33import java.io.FileInputStream;34import java.security.KeyStore;35import java.security.cert.CertPathValidatorException;36import java.security.cert.Certificate;37import java.security.cert.CertificateException;38import java.security.cert.CertificateFactory;39import java.security.cert.PKIXBuilderParameters;40import java.security.cert.PKIXCertPathChecker;41import java.security.cert.TrustAnchor;42import java.security.cert.X509Certificate;43import java.util.Collection;44import java.util.Date;45import java.util.HashSet;46import java.util.Set;47import sun.security.validator.TrustStoreUtil;48import sun.security.validator.Validator;495051public class EndEntityExtensionCheck {5253/*54* Owner: CN=TestCA55* Issuer: CN=TestCA56*/57private static final String CA =58"-----BEGIN CERTIFICATE-----\n" +59"MIICgDCCAj2gAwIBAgIEC18hWjALBgcqhkjOOAQDBQAwETEPMA0GA1UEAxMGVGVz\n" +60"dENBMB4XDTE1MDQwNzIyMzUyMFoXDTI1MDQwNjIyMzUyMFowETEPMA0GA1UEAxMG\n" +61"VGVzdENBMIIBuDCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2\n" +62"EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdr\n" +63"mVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXz\n" +64"rith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+Gghdab\n" +65"Pd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6Ewo\n" +66"FhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhR\n" +67"kImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYUAAoGBAJOWy2hVy4iNwsi/idWG\n" +68"oksr9IZxQIFR2YavoUmD+rIgfYUpiCihzftDLMMaNYqp9PPxuOyoIPGPbwmKpAs5\n" +69"nq6gLwH2lSsN+EwyV2SJ0J26PHiMuRNZWWfKR3cpEqbQVb0CmvqSpj8zYfamPzp7\n" +70"eXSWwahzgLCGJM3SgCfDFC0uoyEwHzAdBgNVHQ4EFgQU7tLD8FnWM+r6jBr+mCXs\n" +71"8G5yBpgwCwYHKoZIzjgEAwUAAzAAMC0CFQCHCtzC3S0ST0EZBucikVui4WXD8QIU\n" +72"L3Oxy6989/FhZlZWJlhqc1ungEQ=\n" +73"-----END CERTIFICATE-----";7475/*76* Owner: CN=TestEE77* Issuer: CN=TestCA78* Contains a custom critical extension with OID 1.2.3.4:79* #1: ObjectId: 1.2.3.4 Criticality=true80* 0000: 00 0081*/82private static final String EE =83"-----BEGIN CERTIFICATE-----\n" +84"MIICrTCCAmugAwIBAgIELjciKzALBgcqhkjOOAQDBQAwETEPMA0GA1UEAxMGVGVz\n" +85"dENBMB4XDTE1MDQwNzIzMDA1OFoXDTE1MDcwNjIzMDA1OFowETEPMA0GA1UEAxMG\n" +86"VGVzdEVFMIIBtzCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2\n" +87"EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdr\n" +88"mVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXz\n" +89"rith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+Gghdab\n" +90"Pd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6Ewo\n" +91"FhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhR\n" +92"kImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYQAAoGAN97otrAJEuUg/O97vScI\n" +93"01xs1jqTz5o0PGpKiDDJNB3tCCUbLqXoBQBvSefQ8vYL3mmlEJLxlwfbajRmJQp0\n" +94"tUy5SUCZHk3MdoKxSvrqYnVpYwJHFXKWs6lAawxfuWbkm9SREuepOWnVzy2ecf5z\n" +95"hvy9mgEBfi4E9Cy8Byq2TpyjUDBOMAwGAyoDBAEB/wQCAAAwHwYDVR0jBBgwFoAU\n" +96"7tLD8FnWM+r6jBr+mCXs8G5yBpgwHQYDVR0OBBYEFNRVqt5F+EAuJ5x1IZLDkoMs\n" +97"mDj4MAsGByqGSM44BAMFAAMvADAsAhQyNGhxIp5IshN1zqLs4pUY214IMAIUMmTL\n" +98"3ZMpMAjITbuHHlFNUqZ7A9s=\n" +99"-----END CERTIFICATE-----";100101public static void main(String[] args) throws Exception {102X509Certificate[] chain = createChain();103104/* Test 1: Test SimpleValidator105* SimpleValidator doesn't check for unsupported critical106* extensions in the end entity certificate, and leaves that up107* to EndEntityChecker, which should catch such extensions.108*/109KeyStore ks = KeyStore.getInstance("JKS");110ks.load(null, null);111ks.setCertificateEntry("testca", chain[chain.length - 1]);112113Validator v = Validator.getInstance(Validator.TYPE_SIMPLE,114Validator.VAR_TLS_CLIENT,115TrustStoreUtil.getTrustedCerts(ks));116try {117v.validate(chain);118throw new Exception("Chain should not have validated " +119"successfully.");120} catch (CertificateException ex) {121// EE cert has an unsupported critical extension that is not122// checked by SimpleValidator's extension checks, so this123// failure is expected124}125126/* Test 2: Test PKIXValidator without custom checker127* PKIXValidator accepts PKIXParameters that can contain128* custom PKIXCertPathCheckers, which would be run against129* each cert in the chain, including EE certs.130* Check that if PKIXValidator is not provided a custom131* PKIXCertPathChecker for an unknown critical extension in132* the EE cert, chain validation will fail.133*/134TrustAnchor ta = new TrustAnchor(chain[chain.length - 1], null);135Set<TrustAnchor> tas = new HashSet<>();136tas.add(ta);137PKIXBuilderParameters params = new PKIXBuilderParameters(tas, null);138params.setDate(new Date(115, 5, 1)); // 2015-05-01139params.setRevocationEnabled(false);140141v = Validator.getInstance(Validator.TYPE_PKIX,142Validator.VAR_TLS_CLIENT,143params);144try {145v.validate(chain);146throw new Exception("Chain should not have validated " +147"successfully.");148} catch (CertificateException ex) {149// EE cert has an unsupported critical extension and150// PKIXValidator was not provided any custom checker151// for it, so this failure ie expected.152}153154/* Test 3: Test PKIXValidator with custom checker155* Check that PKIXValidator will successfully validate a chain156* containing an EE cert with a critical custom extension, given157* a corresponding PKIXCertPathChecker for the extension.158*/159params = new PKIXBuilderParameters(tas, null);160params.addCertPathChecker(new CustomChecker());161params.setDate(new Date(115, 5, 1)); // 2015-05-01162params.setRevocationEnabled(false);163164v = Validator.getInstance(Validator.TYPE_PKIX,165Validator.VAR_TLS_CLIENT,166params);167v.validate(chain); // This should validate successfully168169System.out.println("Tests passed.");170}171172public static X509Certificate[] createChain() throws Exception {173CertificateFactory cf = CertificateFactory.getInstance("X.509");174X509Certificate ee = (X509Certificate)175cf.generateCertificate((new ByteArrayInputStream(EE.getBytes())));176X509Certificate ca = (X509Certificate)177cf.generateCertificate((new ByteArrayInputStream(CA.getBytes())));178179X509Certificate[] chain = {ee, ca};180return chain;181}182183/*184* A custom PKIXCertPathChecker. Looks for a critical extension185* in an end entity certificate with the OID 1.2.3.4.186*/187static class CustomChecker extends PKIXCertPathChecker {188189@Override190public void init(boolean forward) throws CertPathValidatorException {191// nothing to do192}193194@Override195public boolean isForwardCheckingSupported() {196return false;197}198199@Override200public Set<String> getSupportedExtensions() {201Set<String> exts = new HashSet<>();202exts.add("1.2.3.4");203return exts;204}205206@Override207public void check(Certificate cert,208Collection<String> unresolvedCritExts)209throws CertPathValidatorException {210X509Certificate currCert = (X509Certificate)cert;211// check that this is an EE cert212if (currCert.getBasicConstraints() == -1) {213if (unresolvedCritExts != null &&214!unresolvedCritExts.isEmpty()) {215unresolvedCritExts.remove("1.2.3.4");216}217}218}219220}221}222223224