1 /*
   2  * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25  * @test 6948803
  26  * @summary CertPath validation regression caused by SHA1 replacement root
  27  *  and MD2 disable feature
  28  */
  29 
  30 import java.io.FileInputStream;
  31 import java.security.KeyStore;
  32 import java.security.cert.Certificate;
  33 import java.security.cert.CertificateFactory;
  34 import java.security.cert.X509Certificate;
  35 import java.util.Arrays;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 import sun.security.validator.Validator;
  39 
  40 public class CertReplace {
  41 
  42     // These two files are generated using JDK 7 keytool:
  43     // certreplace.certs includes 3 certs (user, int, ca) as
  44     // a cert chain for user, where ca's signature is MD2withRSA,
  45     // certreplace.jks includes ca's new-refreshed cert which
  46     // signature is SHA256withRSA.
  47     private final static String cacerts =
  48             System.getProperty("test.src", "./") + "/certreplace.jks";
  49     private final static String certs =
  50             System.getProperty("test.src", "./") + "/certreplace.certs";
  51 
  52     public static void main(String[] args) throws Exception {
  53 
  54         KeyStore ks = KeyStore.getInstance("JKS");
  55         ks.load(new FileInputStream(cacerts), "changeit".toCharArray());
  56         Validator v = Validator.getInstance
  57             (Validator.TYPE_PKIX, Validator.VAR_GENERIC, ks);
  58         X509Certificate[] chain = createPath();
  59         System.out.println(Arrays.toString(v.validate(chain)));
  60 
  61     }
  62 
  63     public static X509Certificate[] createPath() throws Exception {
  64         CertificateFactory cf = CertificateFactory.getInstance("X.509");
  65         List list = new ArrayList();
  66         for (Certificate c: cf.generateCertificates(
  67                 new FileInputStream(certs))) {
  68             list.add((X509Certificate)c);
  69         }
  70         return (X509Certificate[]) list.toArray(new X509Certificate[0]);
  71     }
  72 }