Path: blob/master/test/jdk/sun/security/validator/EndEntityExtensionCheck.java
41149 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* @modules java.base/sun.security.validator29* @run main/othervm -Djdk.security.allowNonCaAnchor EndEntityExtensionCheck30*/3132import java.io.ByteArrayInputStream;33import java.io.File;34import java.io.FileInputStream;35import java.security.KeyStore;36import java.security.cert.CertPathValidatorException;37import java.security.cert.Certificate;38import java.security.cert.CertificateException;39import java.security.cert.CertificateFactory;40import java.security.cert.PKIXBuilderParameters;41import java.security.cert.PKIXCertPathChecker;42import java.security.cert.TrustAnchor;43import java.security.cert.X509Certificate;44import java.util.Collection;45import java.util.Date;46import java.util.HashSet;47import java.util.Set;48import sun.security.validator.TrustStoreUtil;49import sun.security.validator.Validator;505152public class EndEntityExtensionCheck {5354/*55* Owner: CN=TestCA56* Issuer: CN=TestCA57*/58private static final String CA =59"-----BEGIN CERTIFICATE-----\n" +60"MIICgDCCAj2gAwIBAgIEC18hWjALBgcqhkjOOAQDBQAwETEPMA0GA1UEAxMGVGVz\n" +61"dENBMB4XDTE1MDQwNzIyMzUyMFoXDTI1MDQwNjIyMzUyMFowETEPMA0GA1UEAxMG\n" +62"VGVzdENBMIIBuDCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2\n" +63"EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdr\n" +64"mVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXz\n" +65"rith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+Gghdab\n" +66"Pd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6Ewo\n" +67"FhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhR\n" +68"kImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYUAAoGBAJOWy2hVy4iNwsi/idWG\n" +69"oksr9IZxQIFR2YavoUmD+rIgfYUpiCihzftDLMMaNYqp9PPxuOyoIPGPbwmKpAs5\n" +70"nq6gLwH2lSsN+EwyV2SJ0J26PHiMuRNZWWfKR3cpEqbQVb0CmvqSpj8zYfamPzp7\n" +71"eXSWwahzgLCGJM3SgCfDFC0uoyEwHzAdBgNVHQ4EFgQU7tLD8FnWM+r6jBr+mCXs\n" +72"8G5yBpgwCwYHKoZIzjgEAwUAAzAAMC0CFQCHCtzC3S0ST0EZBucikVui4WXD8QIU\n" +73"L3Oxy6989/FhZlZWJlhqc1ungEQ=\n" +74"-----END CERTIFICATE-----";7576/*77* Owner: CN=TestEE78* Issuer: CN=TestCA79* Contains a custom critical extension with OID 1.2.3.4:80* #1: ObjectId: 1.2.3.4 Criticality=true81* 0000: 00 0082*/83private static final String EE =84"-----BEGIN CERTIFICATE-----\n" +85"MIICrTCCAmugAwIBAgIELjciKzALBgcqhkjOOAQDBQAwETEPMA0GA1UEAxMGVGVz\n" +86"dENBMB4XDTE1MDQwNzIzMDA1OFoXDTE1MDcwNjIzMDA1OFowETEPMA0GA1UEAxMG\n" +87"VGVzdEVFMIIBtzCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2\n" +88"EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdr\n" +89"mVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXz\n" +90"rith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+Gghdab\n" +91"Pd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6Ewo\n" +92"FhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhR\n" +93"kImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYQAAoGAN97otrAJEuUg/O97vScI\n" +94"01xs1jqTz5o0PGpKiDDJNB3tCCUbLqXoBQBvSefQ8vYL3mmlEJLxlwfbajRmJQp0\n" +95"tUy5SUCZHk3MdoKxSvrqYnVpYwJHFXKWs6lAawxfuWbkm9SREuepOWnVzy2ecf5z\n" +96"hvy9mgEBfi4E9Cy8Byq2TpyjUDBOMAwGAyoDBAEB/wQCAAAwHwYDVR0jBBgwFoAU\n" +97"7tLD8FnWM+r6jBr+mCXs8G5yBpgwHQYDVR0OBBYEFNRVqt5F+EAuJ5x1IZLDkoMs\n" +98"mDj4MAsGByqGSM44BAMFAAMvADAsAhQyNGhxIp5IshN1zqLs4pUY214IMAIUMmTL\n" +99"3ZMpMAjITbuHHlFNUqZ7A9s=\n" +100"-----END CERTIFICATE-----";101102public static void main(String[] args) throws Exception {103X509Certificate[] chain = createChain();104105/* Test 1: Test SimpleValidator106* SimpleValidator doesn't check for unsupported critical107* extensions in the end entity certificate, and leaves that up108* to EndEntityChecker, which should catch such extensions.109*/110KeyStore ks = KeyStore.getInstance("JKS");111ks.load(null, null);112ks.setCertificateEntry("testca", chain[chain.length - 1]);113114Validator v = Validator.getInstance(Validator.TYPE_SIMPLE,115Validator.VAR_TLS_CLIENT,116TrustStoreUtil.getTrustedCerts(ks));117try {118v.validate(chain);119throw new Exception("Chain should not have validated " +120"successfully.");121} catch (CertificateException ex) {122// EE cert has an unsupported critical extension that is not123// checked by SimpleValidator's extension checks, so this124// failure is expected125}126127/* Test 2: Test PKIXValidator without custom checker128* PKIXValidator accepts PKIXParameters that can contain129* custom PKIXCertPathCheckers, which would be run against130* each cert in the chain, including EE certs.131* Check that if PKIXValidator is not provided a custom132* PKIXCertPathChecker for an unknown critical extension in133* the EE cert, chain validation will fail.134*/135TrustAnchor ta = new TrustAnchor(chain[chain.length - 1], null);136Set<TrustAnchor> tas = new HashSet<>();137tas.add(ta);138PKIXBuilderParameters params = new PKIXBuilderParameters(tas, null);139params.setDate(new Date(115, 5, 1)); // 2015-05-01140params.setRevocationEnabled(false);141142v = Validator.getInstance(Validator.TYPE_PKIX,143Validator.VAR_TLS_CLIENT,144params);145try {146v.validate(chain);147throw new Exception("Chain should not have validated " +148"successfully.");149} catch (CertificateException ex) {150// EE cert has an unsupported critical extension and151// PKIXValidator was not provided any custom checker152// for it, so this failure ie expected.153}154155/* Test 3: Test PKIXValidator with custom checker156* Check that PKIXValidator will successfully validate a chain157* containing an EE cert with a critical custom extension, given158* a corresponding PKIXCertPathChecker for the extension.159*/160params = new PKIXBuilderParameters(tas, null);161params.addCertPathChecker(new CustomChecker());162params.setDate(new Date(115, 5, 1)); // 2015-05-01163params.setRevocationEnabled(false);164165v = Validator.getInstance(Validator.TYPE_PKIX,166Validator.VAR_TLS_CLIENT,167params);168v.validate(chain); // This should validate successfully169170System.out.println("Tests passed.");171}172173public static X509Certificate[] createChain() throws Exception {174CertificateFactory cf = CertificateFactory.getInstance("X.509");175X509Certificate ee = (X509Certificate)176cf.generateCertificate((new ByteArrayInputStream(EE.getBytes())));177X509Certificate ca = (X509Certificate)178cf.generateCertificate((new ByteArrayInputStream(CA.getBytes())));179180X509Certificate[] chain = {ee, ca};181return chain;182}183184/*185* A custom PKIXCertPathChecker. Looks for a critical extension186* in an end entity certificate with the OID 1.2.3.4.187*/188static class CustomChecker extends PKIXCertPathChecker {189190@Override191public void init(boolean forward) throws CertPathValidatorException {192// nothing to do193}194195@Override196public boolean isForwardCheckingSupported() {197return false;198}199200@Override201public Set<String> getSupportedExtensions() {202Set<String> exts = new HashSet<>();203exts.add("1.2.3.4");204return exts;205}206207@Override208public void check(Certificate cert,209Collection<String> unresolvedCritExts)210throws CertPathValidatorException {211X509Certificate currCert = (X509Certificate)cert;212// check that this is an EE cert213if (currCert.getBasicConstraints() == -1) {214if (unresolvedCritExts != null &&215!unresolvedCritExts.isEmpty()) {216unresolvedCritExts.remove("1.2.3.4");217}218}219}220221}222}223224225