Path: blob/master/test/jdk/sun/security/provider/certpath/CertId/CheckCertId.java
41154 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 635529526* @summary Certificate validation using OCSP fails for a particular class of certificates27* @modules java.base/sun.security.provider.certpath28* java.base/sun.security.x50929*/3031import java.io.*;32import java.security.MessageDigest;33import java.util.Arrays;34import sun.security.provider.certpath.CertId;35import sun.security.x509.X509CertImpl;3637/*38* Checks that the hash value for a certificate's issuer name is generated39* correctly. Requires any certificate that is not self-signed.40*41* NOTE: this test uses Sun private classes which are subject to change.42*/43public class CheckCertId {4445private static final String CERT_FILENAME = "interCA.der";4647public static void main(String[] args) throws Exception {4849X509CertImpl cert = loadCert(CERT_FILENAME);5051/* Compute the hash in the same way as CertId constructor */52MessageDigest hash = MessageDigest.getInstance("SHA1");53hash.update(cert.getSubjectX500Principal().getEncoded());54byte[] expectedHash = hash.digest();5556CertId certId = new CertId(cert, null);57byte[] receivedHash = certId.getIssuerNameHash();5859if (! Arrays.equals(expectedHash, receivedHash)) {60throw new61Exception("Bad hash value for issuer name in CertId object");62}63}6465/*66* Load an X.509 certificate from a file.67* Return it as a Sun private class.68*/69private static X509CertImpl loadCert(String filename) throws Exception {7071BufferedInputStream bis =72new BufferedInputStream(73new FileInputStream(74new File(System.getProperty("test.src", "."), filename)));7576return new X509CertImpl(bis);77}78}798081