1 /*
   2  * Copyright (c) 2005, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6355295
  27  * @summary Certificate validation using OCSP fails for a particular class of certificates
  28  */
  29 
  30 import java.io.*;
  31 import java.security.MessageDigest;
  32 import java.util.Arrays;
  33 import sun.security.provider.certpath.CertId;
  34 import sun.security.x509.X509CertImpl;
  35 
  36 /*
  37  * Checks that the hash value for a certificate's issuer name is generated
  38  * correctly. Requires any certificate that is not self-signed.
  39  *
  40  * NOTE: this test uses Sun private classes which are subject to change.
  41  */
  42 public class CheckCertId {
  43 
  44     private static final String CERT_FILENAME = "interCA.der";
  45 
  46     public static void main(String[] args) throws Exception {
  47 
  48         X509CertImpl cert = loadCert(CERT_FILENAME);
  49 
  50         /* Compute the hash in the same way as CertId constructor */
  51         MessageDigest hash = MessageDigest.getInstance("SHA1");
  52         hash.update(cert.getSubjectX500Principal().getEncoded());
  53         byte[] expectedHash = hash.digest();
  54 
  55         CertId certId = new CertId(cert, null);
  56         byte[] receivedHash = certId.getIssuerNameHash();
  57 
  58         if (! Arrays.equals(expectedHash, receivedHash)) {
  59             throw new
  60                 Exception("Bad hash value for issuer name in CertId object");
  61         }
  62     }
  63 
  64     /*
  65      * Load an X.509 certificate from a file.
  66      * Return it as a Sun private class.
  67      */
  68     private static X509CertImpl loadCert(String filename) throws Exception {
  69 
  70         BufferedInputStream bis =
  71             new BufferedInputStream(
  72                 new FileInputStream(
  73                     new File(System.getProperty("test.src", "."), filename)));
  74 
  75         return new X509CertImpl(bis);
  76     }
  77 }