jdk/test/java/security/cert/CertPathBuilder/NoExtensions.java
author mullan
Mon, 14 Apr 2008 10:25:05 -0400
changeset 476 b23646c026ed
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6631361: Spec of AccessControlContext constructor is not complete Summary: Add NullPointerException to @throws clause and treat empty array and array of nulls as equivalent Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4519462
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Verify Sun CertPathBuilder implementation handles certificates with no extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.TrustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.CollectionCertStoreParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.CertStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.X509CertSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.CertPathBuilder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.PKIXBuilderParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.CertPathBuilderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.cert.CRL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.cert.CertPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
// Test based on user code submitted with bug by daniel.boggs@compass.net
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class NoExtensions {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            NoExtensions certs = new NoExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            // the first certificate has the Authority Key Identifier extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            certs.doBuild(getUserCertificate1());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            System.out.println("successfully built path for the first certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            // the second certificate does not have the Authority Key Identifier extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            // this will not succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            certs.doBuild(getUserCertificate2());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            System.out.println("successfully built path for the second certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private void doBuild(X509Certificate userCert) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // get the set of trusted CA certificates (only one in this instance)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        HashSet trustAnchors = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        X509Certificate trustedCert = getTrustedCertificate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        trustAnchors.add(new TrustAnchor(trustedCert, null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // put together a CertStore (repository of the certificates and CRLs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        ArrayList certs = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        certs.add(trustedCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        certs.add(userCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        CollectionCertStoreParameters certStoreParams = new CollectionCertStoreParameters(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        CertStore certStore = CertStore.getInstance("Collection", certStoreParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // specify the target certificate via a CertSelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        X509CertSelector certSelector = new X509CertSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        certSelector.setCertificate(userCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        certSelector.setSubject(userCert.getSubjectDN().getName()); // seems to be required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // build a valid cerificate path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        CertPathBuilder certPathBuilder = CertPathBuilder.getInstance("PKIX", "SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        PKIXBuilderParameters certPathBuilderParams = new PKIXBuilderParameters(trustAnchors, certSelector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        certPathBuilderParams.addCertStore(certStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        certPathBuilderParams.setRevocationEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        CertPathBuilderResult result = certPathBuilder.build(certPathBuilderParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // get and show cert path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        CertPath certPath = result.getCertPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
//        System.out.println(certPath.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static X509Certificate getTrustedCertificate() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        String sCert =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            "-----BEGIN CERTIFICATE-----\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
          + "MIIBezCCASWgAwIBAgIQyWD8dLUoqpJFyDxrfRlrsTANBgkqhkiG9w0BAQQFADAW\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
          + "MRQwEgYDVQQDEwtSb290IEFnZW5jeTAeFw0wMTEwMTkxMjU5MjZaFw0zOTEyMzEy\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
          + "MzU5NTlaMBoxGDAWBgNVBAMTD1Jvb3RDZXJ0aWZpY2F0ZTBcMA0GCSqGSIb3DQEB\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
          + "AQUAA0sAMEgCQQC+NFKszPjatUZKWmyWaFjir1wB93FX2u5SL+GMjgUsMs1JcTKQ\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
          + "Kh0cnnQKknNkV4cTW4NPn31YCoB1+0KA3mknAgMBAAGjSzBJMEcGA1UdAQRAMD6A\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
          + "EBLkCS0GHR1PAI1hIdwWZGOhGDAWMRQwEgYDVQQDEwtSb290IEFnZW5jeYIQBjds\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
          + "AKoAZIoRz7jUqlw19DANBgkqhkiG9w0BAQQFAANBACJxAfP57yqaT9N+nRgAOugM\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
          + "JG0aN3/peCIvL3p29epRL2xoWFvxpUUlsH2I39OZ6b8+twWCebhkv1I62segXAk=\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
          + "-----END CERTIFICATE-----";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        ByteArrayInputStream bytes = new ByteArrayInputStream(sCert.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return (X509Certificate)certFactory.generateCertificate(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static X509Certificate getUserCertificate1() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // this certificate includes an extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        String sCert =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            "-----BEGIN CERTIFICATE-----\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
          + "MIIBfzCCASmgAwIBAgIQWFSKzCWO2ptOAc2F3MKZSzANBgkqhkiG9w0BAQQFADAa\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
          + "MRgwFgYDVQQDEw9Sb290Q2VydGlmaWNhdGUwHhcNMDExMDE5MTMwNzQxWhcNMzkx\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
          + "MjMxMjM1OTU5WjAaMRgwFgYDVQQDEw9Vc2VyQ2VydGlmaWNhdGUwXDANBgkqhkiG\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
          + "9w0BAQEFAANLADBIAkEA24gypa2YFGZHKznEWWbqIWNVXCM35W7RwJwhGpNsuBCj\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
          + "NT6KEo66F+OOMgZmb0KrEZHBJASJ3n4Cqbt4aHm/2wIDAQABo0swSTBHBgNVHQEE\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
          + "QDA+gBBch+eYzOPgVRbMq5vGpVWooRgwFjEUMBIGA1UEAxMLUm9vdCBBZ2VuY3mC\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
          + "EMlg/HS1KKqSRcg8a30Za7EwDQYJKoZIhvcNAQEEBQADQQCYBIHBqQQJePi5Hzfo\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
          + "CxeUaYlXmvbxVNkxM65Pplsj3h4ntfZaynmlhahH3YsnnA8wk6xPt04LjSId12RB\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
          + "PeuO\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
          + "-----END CERTIFICATE-----";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        ByteArrayInputStream bytes = new ByteArrayInputStream(sCert.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return (X509Certificate)certFactory.generateCertificate(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static X509Certificate getUserCertificate2() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // this certificate does not include any extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        String sCert =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            "-----BEGIN CERTIFICATE-----\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
          + "MIIBMjCB3aADAgECAhB6225ckZVssEukPuvk1U1PMA0GCSqGSIb3DQEBBAUAMBox\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
          + "GDAWBgNVBAMTD1Jvb3RDZXJ0aWZpY2F0ZTAeFw0wMTEwMTkxNjA5NTZaFw0wMjEw\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
          + "MTkyMjA5NTZaMBsxGTAXBgNVBAMTEFVzZXJDZXJ0aWZpY2F0ZTIwXDANBgkqhkiG\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
          + "9w0BAQEFAANLADBIAkEAzicGiW9aUlUoQIZnLy1l8MMV5OvA+4VJ4T/xo/PpN8Oq\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
          + "WgZVGKeEp6JCzMlXEJk3TGLfpXL4Ytw+Ldhv0QPhLwIDAnMpMA0GCSqGSIb3DQEB\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
          + "BAUAA0EAQmj9SFHEx66JyAps3ew4pcSS3QvfVZ/6qsNUYCG75rFGcTUPHcXKql9y\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
          + "qBT83iNLJ//krjw5Ju0WRPg/buHSww==\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
          + "-----END CERTIFICATE-----";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        ByteArrayInputStream bytes = new ByteArrayInputStream(sCert.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return (X509Certificate)certFactory.generateCertificate(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}