0N/A/*
2362N/A * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A *
0N/A * @author Sean Mullan
0N/A * @author Steve Hanna
0N/A *
0N/A */
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.File;
0N/Aimport java.io.FileInputStream;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.security.cert.CertificateFactory;
0N/Aimport java.security.cert.CertPath;
0N/Aimport java.security.cert.CertPathBuilder;
0N/Aimport java.security.cert.CertPathValidator;
0N/Aimport java.security.cert.CertStore;
0N/Aimport java.security.cert.CollectionCertStoreParameters;
0N/Aimport java.security.cert.PKIXBuilderParameters;
0N/Aimport java.security.cert.PKIXCertPathBuilderResult;
0N/Aimport java.security.cert.PKIXCertPathValidatorResult;
0N/Aimport java.security.cert.PKIXParameters;
0N/Aimport java.security.cert.X509Certificate;
0N/Aimport java.security.cert.X509CRL;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.List;
0N/Aimport java.util.Set;
0N/A
0N/A/**
0N/A * Static utility methods useful for testing certificate/certpath APIs.
0N/A */
0N/Apublic class CertUtils {
0N/A
0N/A private CertUtils() {}
0N/A
0N/A /**
0N/A * Get a DER-encoded X.509 certificate from a file.
0N/A *
0N/A * @param certFilePath path to file containing DER-encoded certificate
0N/A * @return X509Certificate
0N/A * @throws IOException on error
0N/A */
0N/A public static X509Certificate getCertFromFile(String certFilePath)
0N/A throws IOException {
0N/A X509Certificate cert = null;
0N/A try {
0N/A File certFile = new File(System.getProperty("test.src", "."),
0N/A certFilePath);
0N/A if (!certFile.canRead())
0N/A throw new IOException("File " +
0N/A certFile.toString() +
0N/A " is not a readable file.");
0N/A FileInputStream certFileInputStream =
0N/A new FileInputStream(certFile);
0N/A CertificateFactory cf = CertificateFactory.getInstance("X509");
0N/A cert = (X509Certificate)
0N/A cf.generateCertificate(certFileInputStream);
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A throw new IOException("Can't construct X509Certificate: " +
0N/A e.getMessage());
0N/A }
0N/A return cert;
0N/A }
0N/A
0N/A /**
0N/A * Get a DER-encoded X.509 CRL from a file.
0N/A *
0N/A * @param crlFilePath path to file containing DER-encoded CRL
0N/A * @return X509CRL
0N/A * @throws IOException on error
0N/A */
0N/A public static X509CRL getCRLFromFile(String crlFilePath)
0N/A throws IOException {
0N/A X509CRL crl = null;
0N/A try {
0N/A File crlFile = new File(System.getProperty("test.src", "."),
0N/A crlFilePath);
0N/A if (!crlFile.canRead())
0N/A throw new IOException("File " +
0N/A crlFile.toString() +
0N/A " is not a readable file.");
0N/A FileInputStream crlFileInputStream =
0N/A new FileInputStream(crlFile);
0N/A CertificateFactory cf = CertificateFactory.getInstance("X509");
0N/A crl = (X509CRL) cf.generateCRL(crlFileInputStream);
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A throw new IOException("Can't construct X509CRL: " +
0N/A e.getMessage());
0N/A }
0N/A return crl;
0N/A }
0N/A
0N/A /**
0N/A * Read a bunch of certs from files and create a CertPath from them.
0N/A *
0N/A * @param fileNames an array of <code>String</code>s that are file names
0N/A * @throws Exception on error
0N/A */
0N/A public static CertPath buildPath(String [] fileNames) throws Exception {
0N/A return buildPath("", fileNames);
0N/A }
0N/A
0N/A /**
0N/A * Read a bunch of certs from files and create a CertPath from them.
0N/A *
0N/A * @param relPath relative path containing certs (must end in
0N/A * file.separator)
0N/A * @param fileNames an array of <code>String</code>s that are file names
0N/A * @throws Exception on error
0N/A */
0N/A public static CertPath buildPath(String relPath, String [] fileNames)
0N/A throws Exception {
0N/A List<X509Certificate> list = new ArrayList<X509Certificate>();
0N/A for (int i = 0; i < fileNames.length; i++) {
0N/A list.add(0, getCertFromFile(relPath + fileNames[i]));
0N/A }
0N/A CertificateFactory cf = CertificateFactory.getInstance("X509");
0N/A return(cf.generateCertPath(list));
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Read a bunch of certs from files and create a CertStore from them.
0N/A *
0N/A * @param fileNames an array of <code>String</code>s that are file names
0N/A * @return the <code>CertStore</code> created
0N/A * @throws Exception on error
0N/A */
0N/A public static CertStore createStore(String [] fileNames) throws Exception {
0N/A return createStore("", fileNames);
0N/A }
0N/A
0N/A /**
0N/A * Read a bunch of certs from files and create a CertStore from them.
0N/A *
0N/A * @param relPath relative path containing certs (must end in
0N/A * file.separator)
0N/A * @param fileNames an array of <code>String</code>s that are file names
0N/A * @return the <code>CertStore</code> created
0N/A * @throws Exception on error
0N/A */
0N/A public static CertStore createStore(String relPath, String [] fileNames)
0N/A throws Exception {
0N/A Set<X509Certificate> certs = new HashSet<X509Certificate>();
0N/A for (int i = 0; i < fileNames.length; i++) {
0N/A certs.add(getCertFromFile(relPath + fileNames[i]));
0N/A }
0N/A return CertStore.getInstance("Collection",
0N/A new CollectionCertStoreParameters(certs));
0N/A }
0N/A
0N/A /**
0N/A * Read a bunch of CRLs from files and create a CertStore from them.
0N/A *
0N/A * @param fileNames an array of <code>String</code>s that are file names
0N/A * @return the <code>CertStore</code> created
0N/A * @throws Exception on error
0N/A */
0N/A public static CertStore createCRLStore(String [] fileNames)
0N/A throws Exception {
0N/A return createCRLStore("", fileNames);
0N/A }
0N/A
0N/A /**
0N/A * Read a bunch of CRLs from files and create a CertStore from them.
0N/A *
0N/A * @param relPath relative path containing CRLs (must end in file.separator)
0N/A * @param fileNames an array of <code>String</code>s that are file names
0N/A * @return the <code>CertStore</code> created
0N/A * @throws Exception on error
0N/A */
0N/A public static CertStore createCRLStore(String relPath, String [] fileNames)
0N/A throws Exception {
0N/A Set<X509CRL> crls = new HashSet<X509CRL>();
0N/A for (int i = 0; i < fileNames.length; i++) {
0N/A crls.add(getCRLFromFile(relPath + fileNames[i]));
0N/A }
0N/A return CertStore.getInstance("Collection",
0N/A new CollectionCertStoreParameters(crls));
0N/A }
0N/A
0N/A /**
0N/A * Perform a PKIX path build. On failure, throw an exception.
0N/A *
0N/A * @param params PKIXBuilderParameters to use in validation
0N/A * @throws Exception on error
0N/A */
0N/A public static PKIXCertPathBuilderResult build(PKIXBuilderParameters params)
0N/A throws Exception {
0N/A CertPathBuilder builder =
0N/A CertPathBuilder.getInstance("PKIX");
0N/A return (PKIXCertPathBuilderResult) builder.build(params);
0N/A }
0N/A
0N/A /**
0N/A * Perform a PKIX validation. On failure, throw an exception.
0N/A *
0N/A * @param path CertPath to validate
0N/A * @param params PKIXParameters to use in validation
0N/A * @throws Exception on error
0N/A */
0N/A public static PKIXCertPathValidatorResult validate
0N/A (CertPath path, PKIXParameters params) throws Exception {
0N/A CertPathValidator validator =
0N/A CertPathValidator.getInstance("PKIX");
0N/A return (PKIXCertPathValidatorResult) validator.validate(path, params);
0N/A }
0N/A
0N/A /*
0N/A * Reads the entire input stream into a byte array.
0N/A */
0N/A private static byte[] getTotalBytes(InputStream is) throws IOException {
0N/A byte[] buffer = new byte[8192];
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
0N/A int n;
0N/A baos.reset();
0N/A while ((n = is.read(buffer, 0, buffer.length)) != -1) {
0N/A baos.write(buffer, 0, n);
0N/A }
0N/A return baos.toByteArray();
0N/A }
0N/A}