1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8076117
  27  * @summary EndEntityChecker should not process custom extensions
  28  *          after PKIX validation
  29  */
  30 
  31 import java.io.ByteArrayInputStream;
  32 import java.io.File;
  33 import java.io.FileInputStream;
  34 import java.security.KeyStore;
  35 import java.security.cert.CertPathValidatorException;
  36 import java.security.cert.Certificate;
  37 import java.security.cert.CertificateException;
  38 import java.security.cert.CertificateFactory;
  39 import java.security.cert.PKIXBuilderParameters;
  40 import java.security.cert.PKIXCertPathChecker;
  41 import java.security.cert.TrustAnchor;
  42 import java.security.cert.X509Certificate;
  43 import java.util.Collection;
  44 import java.util.HashSet;
  45 import java.util.Set;
  46 import sun.security.validator.KeyStores;
  47 import sun.security.validator.Validator;
  48 
  49 
  50 public class EndEntityExtensionCheck {
  51 
  52     /*
  53      * Owner: CN=TestCA
  54      * Issuer: CN=TestCA
  55      */
  56     private static final String CA =
  57         "-----BEGIN CERTIFICATE-----\n" +
  58         "MIICgDCCAj2gAwIBAgIEC18hWjALBgcqhkjOOAQDBQAwETEPMA0GA1UEAxMGVGVz\n" +
  59         "dENBMB4XDTE1MDQwNzIyMzUyMFoXDTI1MDQwNjIyMzUyMFowETEPMA0GA1UEAxMG\n" +
  60         "VGVzdENBMIIBuDCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2\n" +
  61         "EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdr\n" +
  62         "mVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXz\n" +
  63         "rith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+Gghdab\n" +
  64         "Pd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6Ewo\n" +
  65         "FhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhR\n" +
  66         "kImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYUAAoGBAJOWy2hVy4iNwsi/idWG\n" +
  67         "oksr9IZxQIFR2YavoUmD+rIgfYUpiCihzftDLMMaNYqp9PPxuOyoIPGPbwmKpAs5\n" +
  68         "nq6gLwH2lSsN+EwyV2SJ0J26PHiMuRNZWWfKR3cpEqbQVb0CmvqSpj8zYfamPzp7\n" +
  69         "eXSWwahzgLCGJM3SgCfDFC0uoyEwHzAdBgNVHQ4EFgQU7tLD8FnWM+r6jBr+mCXs\n" +
  70         "8G5yBpgwCwYHKoZIzjgEAwUAAzAAMC0CFQCHCtzC3S0ST0EZBucikVui4WXD8QIU\n" +
  71         "L3Oxy6989/FhZlZWJlhqc1ungEQ=\n" +
  72         "-----END CERTIFICATE-----";
  73 
  74     /*
  75      * Owner: CN=TestEE
  76      * Issuer: CN=TestCA
  77      * Contains a custom critical extension with OID 1.2.3.4:
  78      *    #1: ObjectId: 1.2.3.4 Criticality=true
  79      *    0000: 00 00
  80      */
  81     private static final String EE =
  82         "-----BEGIN CERTIFICATE-----\n" +
  83         "MIICrTCCAmugAwIBAgIELjciKzALBgcqhkjOOAQDBQAwETEPMA0GA1UEAxMGVGVz\n" +
  84         "dENBMB4XDTE1MDQwNzIzMDA1OFoXDTE1MDcwNjIzMDA1OFowETEPMA0GA1UEAxMG\n" +
  85         "VGVzdEVFMIIBtzCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2\n" +
  86         "EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdr\n" +
  87         "mVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXz\n" +
  88         "rith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+Gghdab\n" +
  89         "Pd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6Ewo\n" +
  90         "FhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhR\n" +
  91         "kImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYQAAoGAN97otrAJEuUg/O97vScI\n" +
  92         "01xs1jqTz5o0PGpKiDDJNB3tCCUbLqXoBQBvSefQ8vYL3mmlEJLxlwfbajRmJQp0\n" +
  93         "tUy5SUCZHk3MdoKxSvrqYnVpYwJHFXKWs6lAawxfuWbkm9SREuepOWnVzy2ecf5z\n" +
  94         "hvy9mgEBfi4E9Cy8Byq2TpyjUDBOMAwGAyoDBAEB/wQCAAAwHwYDVR0jBBgwFoAU\n" +
  95         "7tLD8FnWM+r6jBr+mCXs8G5yBpgwHQYDVR0OBBYEFNRVqt5F+EAuJ5x1IZLDkoMs\n" +
  96         "mDj4MAsGByqGSM44BAMFAAMvADAsAhQyNGhxIp5IshN1zqLs4pUY214IMAIUMmTL\n" +
  97         "3ZMpMAjITbuHHlFNUqZ7A9s=\n" +
  98         "-----END CERTIFICATE-----";
  99 
 100     public static void main(String[] args) throws Exception {
 101         X509Certificate[] chain = createChain();
 102 
 103         /* Test 1: Test SimpleValidator
 104          *  SimpleValidator doesn't check for unsupported critical
 105          *  extensions in the end entity certificate, and leaves that up
 106          *  to EndEntityChecker, which should catch such extensions.
 107          */
 108         KeyStore ks = KeyStore.getInstance("JKS");
 109         ks.load(null, null);
 110         ks.setCertificateEntry("testca", chain[chain.length - 1]);
 111 
 112         Validator v = Validator.getInstance(Validator.TYPE_SIMPLE,
 113                                             Validator.VAR_TLS_CLIENT,
 114                                             KeyStores.getTrustedCerts(ks));
 115         try {
 116             v.validate(chain);
 117             throw new Exception("Chain should not have validated " +
 118                                 "successfully.");
 119         } catch (CertificateException ex) {
 120             // EE cert has an unsupported critical extension that is not
 121             // checked by SimpleValidator's extension checks, so this
 122             // failure is expected
 123         }
 124 
 125         /* Test 2: Test PKIXValidator without custom checker
 126          * PKIXValidator accepts PKIXParameters that can contain
 127          * custom PKIXCertPathCheckers, which would be run against
 128          * each cert in the chain, including EE certs.
 129          * Check that if PKIXValidator is not provided a custom
 130          * PKIXCertPathChecker for an unknown critical extension in
 131          * the EE cert, chain validation will fail.
 132          */
 133         TrustAnchor ta = new TrustAnchor(chain[chain.length - 1], null);
 134         Set<TrustAnchor> tas = new HashSet<>();
 135         tas.add(ta);
 136         PKIXBuilderParameters params = new PKIXBuilderParameters(tas, null);
 137         params.setRevocationEnabled(false);
 138 
 139         v = Validator.getInstance(Validator.TYPE_PKIX,
 140                                   Validator.VAR_TLS_CLIENT,
 141                                   params);
 142         try {
 143             v.validate(chain);
 144             throw new Exception("Chain should not have validated " +
 145                                 "successfully.");
 146         } catch (CertificateException ex) {
 147             // EE cert has an unsupported critical extension and
 148             // PKIXValidator was not provided any custom checker
 149             // for it, so this failure ie expected.
 150         }
 151 
 152         /* Test 3: Test PKIXValidator with custom checker
 153          * Check that PKIXValidator will successfully validate a chain
 154          * containing an EE cert with a critical custom extension, given
 155          * a corresponding PKIXCertPathChecker for the extension.
 156          */
 157         params = new PKIXBuilderParameters(tas, null);
 158         params.addCertPathChecker(new CustomChecker());
 159         params.setRevocationEnabled(false);
 160 
 161         v = Validator.getInstance(Validator.TYPE_PKIX,
 162                                   Validator.VAR_TLS_CLIENT,
 163                                   params);
 164         v.validate(chain); // This should validate successfully
 165 
 166         System.out.println("Tests passed.");
 167     }
 168 
 169     public static X509Certificate[] createChain() throws Exception {
 170         CertificateFactory cf = CertificateFactory.getInstance("X.509");
 171         X509Certificate ee = (X509Certificate)
 172             cf.generateCertificate((new ByteArrayInputStream(EE.getBytes())));
 173         X509Certificate ca = (X509Certificate)
 174             cf.generateCertificate((new ByteArrayInputStream(CA.getBytes())));
 175 
 176         X509Certificate[] chain = {ee, ca};
 177         return chain;
 178     }
 179 
 180     /*
 181      * A custom PKIXCertPathChecker. Looks for a critical extension
 182      * in an end entity certificate with the OID 1.2.3.4.
 183      */
 184     static class CustomChecker extends PKIXCertPathChecker {
 185 
 186         @Override
 187         public void init(boolean forward) throws CertPathValidatorException {
 188             // nothing to do
 189         }
 190 
 191         @Override
 192         public boolean isForwardCheckingSupported() {
 193             return false;
 194         }
 195 
 196         @Override
 197         public Set<String> getSupportedExtensions() {
 198             Set<String> exts = new HashSet<>();
 199             exts.add("1.2.3.4");
 200             return exts;
 201         }
 202 
 203         @Override
 204         public void check(Certificate cert,
 205                           Collection<String> unresolvedCritExts)
 206                 throws CertPathValidatorException {
 207             X509Certificate currCert = (X509Certificate)cert;
 208             // check that this is an EE cert
 209             if (currCert.getBasicConstraints() == -1) {
 210                 if (unresolvedCritExts != null &&
 211                         !unresolvedCritExts.isEmpty()) {
 212                     unresolvedCritExts.remove("1.2.3.4");
 213                 }
 214             }
 215         }
 216 
 217     }
 218 }