jdk/test/java/security/testlibrary/CertUtils.java
author mullan
Fri, 31 Mar 2017 13:28:26 -0400
changeset 44476 e275cd2f9319
parent 5506 202f599c92aa
permissions -rw-r--r--
8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider) Reviewed-by: weijun, vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44476
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2017, 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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Steve Hanna
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
44476
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    30
import java.io.ByteArrayInputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ByteArrayOutputStream;
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.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
44476
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    36
import java.security.cert.CertificateException;
2
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.CertPathValidator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.CertStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.cert.CollectionCertStoreParameters;
44476
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    43
import java.security.cert.CRLException;
2
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.PKIXCertPathBuilderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.cert.PKIXCertPathValidatorResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.cert.PKIXParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.security.cert.X509CRL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Static utility methods useful for testing certificate/certpath APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class CertUtils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private CertUtils() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Get a DER-encoded X.509 certificate from a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param certFilePath path to file containing DER-encoded certificate
44476
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    66
     * @return the X509Certificate
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    67
     * @throws CertificateException if the certificate type is not supported
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    68
     *                              or cannot be parsed
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    69
     * @throws IOException if the file cannot be opened
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static X509Certificate getCertFromFile(String certFilePath)
44476
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    72
        throws CertificateException, IOException {
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    73
        File certFile = new File(System.getProperty("test.src", "."),
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    74
                                 certFilePath);
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    75
        try (FileInputStream fis = new FileInputStream(certFile)) {
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    76
            return (X509Certificate)
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    77
                CertificateFactory.getInstance("X.509")
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    78
                                  .generateCertificate(fis);
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    79
        }
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    80
    }
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    81
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    82
    /**
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    83
     * Get a PEM-encoded X.509 certificate from a string.
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    84
     *
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    85
     * @param cert string containing the PEM-encoded certificate
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    86
     * @return the X509Certificate
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    87
     * @throws CertificateException if the certificate type is not supported
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    88
     *                              or cannot be parsed
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    89
     */
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    90
    public static X509Certificate getCertFromString(String cert)
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    91
        throws CertificateException {
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    92
        byte[] certBytes = cert.getBytes();
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    93
        ByteArrayInputStream bais = new ByteArrayInputStream(certBytes);
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    94
        return (X509Certificate)
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
    95
            CertificateFactory.getInstance("X.509").generateCertificate(bais);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Get a DER-encoded X.509 CRL from a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param crlFilePath path to file containing DER-encoded CRL
44476
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   102
     * @return the X509CRL
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   103
     * @throws CertificateException if the crl type is not supported
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   104
     * @throws CRLException if the crl cannot be parsed
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   105
     * @throws IOException if the file cannot be opened
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static X509CRL getCRLFromFile(String crlFilePath)
44476
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   108
        throws CertificateException, CRLException, IOException {
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   109
        File crlFile = new File(System.getProperty("test.src", "."),
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   110
                                crlFilePath);
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   111
        try (FileInputStream fis = new FileInputStream(crlFile)) {
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   112
            return (X509CRL)
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   113
                CertificateFactory.getInstance("X.509").generateCRL(fis);
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   114
        }
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   115
    }
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   116
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   117
    /**
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   118
     * Get a PEM-encoded X.509 crl from a string.
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   119
     *
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   120
     * @param crl string containing the PEM-encoded crl
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   121
     * @return the X509CRL
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   122
     * @throws CertificateException if the crl type is not supported
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   123
     * @throws CRLException if the crl cannot be parsed
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   124
     */
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   125
    public static X509CRL getCRLFromString(String crl)
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   126
        throws CertificateException, CRLException {
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   127
        byte[] crlBytes = crl.getBytes();
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   128
        ByteArrayInputStream bais = new ByteArrayInputStream(crlBytes);
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   129
        return (X509CRL)
e275cd2f9319 8175029: StackOverflowError in X509CRL and X509Certificate.verify(PublicKey, Provider)
mullan
parents: 5506
diff changeset
   130
            CertificateFactory.getInstance("X.509").generateCRL(bais);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Read a bunch of certs from files and create a CertPath from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param fileNames an array of <code>String</code>s that are file names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static CertPath buildPath(String [] fileNames) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return buildPath("", fileNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Read a bunch of certs from files and create a CertPath from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param relPath relative path containing certs (must end in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *    file.separator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param fileNames an array of <code>String</code>s that are file names
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 CertPath buildPath(String relPath, String [] fileNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        List<X509Certificate> list = new ArrayList<X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        for (int i = 0; i < fileNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            list.add(0, getCertFromFile(relPath + fileNames[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        CertificateFactory cf = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return(cf.generateCertPath(list));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Read a bunch of certs from files and create a CertStore from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param fileNames an array of <code>String</code>s that are file names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @return the <code>CertStore</code> created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public static CertStore createStore(String [] fileNames) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return createStore("", fileNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Read a bunch of certs from files and create a CertStore from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param relPath relative path containing certs (must end in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *    file.separator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param fileNames an array of <code>String</code>s that are file names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @return the <code>CertStore</code> created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static CertStore createStore(String relPath, String [] fileNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Set<X509Certificate> certs = new HashSet<X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        for (int i = 0; i < fileNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            certs.add(getCertFromFile(relPath + fileNames[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return CertStore.getInstance("Collection",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            new CollectionCertStoreParameters(certs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Read a bunch of CRLs from files and create a CertStore from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param fileNames an array of <code>String</code>s that are file names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @return the <code>CertStore</code> created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public static CertStore createCRLStore(String [] fileNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return createCRLStore("", fileNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Read a bunch of CRLs from files and create a CertStore from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param relPath relative path containing CRLs (must end in file.separator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param fileNames an array of <code>String</code>s that are file names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return the <code>CertStore</code> created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public static CertStore createCRLStore(String relPath, String [] fileNames)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        Set<X509CRL> crls = new HashSet<X509CRL>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        for (int i = 0; i < fileNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            crls.add(getCRLFromFile(relPath + fileNames[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return CertStore.getInstance("Collection",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            new CollectionCertStoreParameters(crls));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Perform a PKIX path build. On failure, throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param params PKIXBuilderParameters to use in validation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public static PKIXCertPathBuilderResult build(PKIXBuilderParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        CertPathBuilder builder =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            CertPathBuilder.getInstance("PKIX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return (PKIXCertPathBuilderResult) builder.build(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Perform a PKIX validation. On failure, throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param path CertPath to validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param params PKIXParameters to use in validation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @throws Exception on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public static PKIXCertPathValidatorResult validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        (CertPath path, PKIXParameters params) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        CertPathValidator validator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            CertPathValidator.getInstance("PKIX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return (PKIXCertPathValidatorResult) validator.validate(path, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Reads the entire input stream into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private static byte[] getTotalBytes(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
           byte[] buffer = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        while ((n = is.read(buffer, 0, buffer.length)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            baos.write(buffer, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
}