2404N/A/*
2469N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2404N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2404N/A *
2404N/A * This code is free software; you can redistribute it and/or modify it
2404N/A * under the terms of the GNU General Public License version 2 only, as
2404N/A * published by the Free Software Foundation.
2404N/A *
2404N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2404N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2404N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2404N/A * version 2 for more details (a copy is included in the LICENSE file that
2404N/A * accompanied this code).
2404N/A *
2404N/A * You should have received a copy of the GNU General Public License version
2404N/A * 2 along with this work; if not, write to the Free Software Foundation,
2404N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2404N/A *
2469N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2469N/A * or visit www.oracle.com if you need additional information or have any
2469N/A * questions.
2404N/A */
2404N/A
2404N/A/*
2404N/A * This test is called by certreplace.sh
2404N/A */
2404N/A
2404N/Aimport java.io.FileInputStream;
2404N/Aimport java.security.KeyStore;
2404N/Aimport java.security.cert.Certificate;
2404N/Aimport java.security.cert.CertificateFactory;
2404N/Aimport java.security.cert.X509Certificate;
2404N/Aimport java.util.Arrays;
2404N/Aimport java.util.ArrayList;
2404N/Aimport java.util.List;
2404N/Aimport sun.security.validator.Validator;
2404N/A
2404N/Apublic class CertReplace {
2404N/A
2469N/A /**
2469N/A * @param args {cacerts keystore, cert chain}
2469N/A */
2404N/A public static void main(String[] args) throws Exception {
2404N/A
2404N/A KeyStore ks = KeyStore.getInstance("JKS");
2469N/A ks.load(new FileInputStream(args[0]), "changeit".toCharArray());
2404N/A Validator v = Validator.getInstance
2404N/A (Validator.TYPE_PKIX, Validator.VAR_GENERIC, ks);
2469N/A X509Certificate[] chain = createPath(args[1]);
2469N/A System.out.println("Chain: ");
2469N/A for (X509Certificate c: v.validate(chain)) {
2469N/A System.out.println(" " + c.getSubjectX500Principal() +
2469N/A " issued by " + c.getIssuerX500Principal());
2469N/A }
2404N/A }
2404N/A
2469N/A public static X509Certificate[] createPath(String chain) throws Exception {
2404N/A CertificateFactory cf = CertificateFactory.getInstance("X.509");
2404N/A List list = new ArrayList();
2404N/A for (Certificate c: cf.generateCertificates(
2469N/A new FileInputStream(chain))) {
2404N/A list.add((X509Certificate)c);
2404N/A }
2404N/A return (X509Certificate[]) list.toArray(new X509Certificate[0]);
2404N/A }
2404N/A}