jdk/test/java/security/cert/CertPathValidator/trustAnchor/ValidateNC.java
author jjg
Mon, 25 Nov 2013 17:42:28 -0800
changeset 21894 3535c1819067
parent 5506 202f599c92aa
child 30820 0d4717a011d3
permissions -rw-r--r--
8028318: [doclint] doclint will reject existing user-written doc comments using custom tags that follow the recommended rules Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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 4458951
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check that Sun's PKIX implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *      CertPathValidator.validate() and CertPathBuilder.build() throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *      InvalidAlgorithmParameterException if any of the TrustAnchors specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *      contain nameConstraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.cert.CertPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.cert.CertPathBuilder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.cert.CertPathBuilderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.CertPathValidator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.cert.CertPathValidatorResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.cert.PKIXParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.cert.PKIXBuilderParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.cert.TrustAnchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.cert.X509CertSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.security.util.DerInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * ValidateNC performs a validation and build of a certification path, using any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * name constraints provided in the trust anchor's certificate. Using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Sun's provider, the validation and build should fail because name constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * on trust anchors are not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author      Steve Hanna
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author      Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public final class ValidateNC {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static CertPath path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static PKIXParameters params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static Set anchors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        String[] certs = { "sun2labs2.cer", "labs2isrg2.cer" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        createPath(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            validate(path, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new Exception("CertPathValidator should have thrown an " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
              "InvalidAlgorithmParameterException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            // success!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            X509CertSelector sel = new X509CertSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            sel.setSubject("cn=sean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            PKIXBuilderParameters bparams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                new PKIXBuilderParameters(anchors, sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            build(bparams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new Exception("CertPathBuilder should have thrown an " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
              "InvalidAlgorithmParameterException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (InvalidAlgorithmParameterException iape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // success!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static void createPath(String[] certs) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        X509Certificate anchorCert = getCertFromFile(certs[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        byte [] nameConstraints = anchorCert.getExtensionValue("2.5.29.30");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (nameConstraints != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            DerInputStream in = new DerInputStream(nameConstraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            nameConstraints = in.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        TrustAnchor anchor = new TrustAnchor(anchorCert, nameConstraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        List list = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        for (int i = 1; i < certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            list.add(0, getCertFromFile(certs[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        CertificateFactory cf = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        path = cf.generateCertPath(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        anchors = Collections.singleton(anchor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        params = new PKIXParameters(anchors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        params.setRevocationEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Get a DER-encoded X.509 certificate from a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param certFilePath path to file containing DER-encoded certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @return X509Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static X509Certificate getCertFromFile(String certFilePath)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            X509Certificate cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                File certFile = new File(System.getProperty("test.src", "."),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    certFilePath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                FileInputStream certFileInputStream =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    new FileInputStream(certFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                CertificateFactory cf = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                cert = (X509Certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    cf.generateCertificate(certFileInputStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                throw new IOException("Can't construct X509Certificate: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                      e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Perform a PKIX validation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param path CertPath to validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param params PKIXParameters to use in validation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static void validate(CertPath path, PKIXParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        CertPathValidator validator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            CertPathValidator.getInstance("PKIX", "SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        CertPathValidatorResult cpvr = validator.validate(path, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Perform a PKIX build.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param params PKIXBuilderParameters to use in the build
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public static void build(PKIXBuilderParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        CertPathBuilder builder =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            CertPathBuilder.getInstance("PKIX", "SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        CertPathBuilderResult cpbr = builder.build(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}