Path: blob/master/test/jdk/sun/security/x509/X509CertImpl/Verify.java
41153 views
/*1* Copyright (c) 2012, 2013, 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 702634726* @summary Certificate should have27* verify(PublicKey key, Provider sigProvider)28*/2930import java.io.ByteArrayInputStream;31import java.security.*;32import java.security.cert.*;3334public class Verify {3536static String selfSignedCertStr =37"-----BEGIN CERTIFICATE-----\n" +38"MIICPjCCAaegAwIBAgIBADANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +39"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzJaFw0zMDA0MDcwMjI0MzJa\n" +40"MB8xCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMIGfMA0GCSqGSIb3DQEB\n" +41"AQUAA4GNADCBiQKBgQC4OTag24sTxL2tXTNuvpmUEtdxrYAZoFsslFQ60T+WD9wQ\n" +42"Jeiw87FSPsR2vxRuv0j8DNm2a4h7LNNIFcLurfNldbz5pvgZ7VqdbbUMPE9qP85n\n" +43"jgDl4woyRTSUeRI4A7O0CO6NpES21dtbdhroWQrEkHxpnrDPxsxrz5gf2m3gqwID\n" +44"AQABo4GJMIGGMB0GA1UdDgQWBBSCJd0hpl5PdAD9IZS+Hzng4lXLGzBHBgNVHSME\n" +45"QDA+gBSCJd0hpl5PdAD9IZS+Hzng4lXLG6EjpCEwHzELMAkGA1UEBhMCVVMxEDAO\n" +46"BgNVBAoTB0V4YW1wbGWCAQAwDwYDVR0TAQH/BAUwAwEB/zALBgNVHQ8EBAMCAgQw\n" +47"DQYJKoZIhvcNAQEEBQADgYEAluy6HIjWcq009lTLmhp+Np6dxU78pInBK8RZkza0\n" +48"484qGaxFGD3UGyZkI5uWmsH2XuMbuox5khfIq6781gmkPBHXBIEtJN8eLusOHEye\n" +49"iE8h7WI+N3qa6Pj56WionMrioqC/3X+b06o147bbhx8U0vkYv/HyPaITOFfMXTdz\n" +50"Vjw=\n" +51"-----END CERTIFICATE-----";5253static String crlIssuerCertStr =54"-----BEGIN CERTIFICATE-----\n" +55"MIICKzCCAZSgAwIBAgIBAjANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +56"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzNaFw0yOTAxMTIwMjI0MzNa\n" +57"MB8xCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMIGfMA0GCSqGSIb3DQEB\n" +58"AQUAA4GNADCBiQKBgQDMJeBMBybHykI/YpwUJ4O9euqDSLb1kpWpceBS8TVqvgBC\n" +59"SgUJWtFZL0i6bdvF6mMdlbuBkGzhXqHiVAi96/zRLbUC9F8SMEJ6MuD+YhQ0ZFTQ\n" +60"atKy8zf8O9XzztelLJ26Gqb7QPV133WY3haAqHtCXOhEKkCN16NOYNC37DTaJwID\n" +61"AQABo3cwdTAdBgNVHQ4EFgQULXSWzXzUOIpOJpzbSCpW42IJUugwRwYDVR0jBEAw\n" +62"PoAUgiXdIaZeT3QA/SGUvh854OJVyxuhI6QhMB8xCzAJBgNVBAYTAlVTMRAwDgYD\n" +63"VQQKEwdFeGFtcGxlggEAMAsGA1UdDwQEAwIBAjANBgkqhkiG9w0BAQQFAAOBgQAY\n" +64"eMnf5AHSNlyUlzXk8o2S0h4gCuvKX6C3kFfKuZcWvFAbx4yQOWLS2s15/nzR4+AP\n" +65"FGX3lgJjROyAh7fGedTQK+NFWwkM2ag1g3hXktnlnT1qHohi0w31nVBJxXEDO/Ck\n" +66"uJTpJGt8XxxbFaw5v7cHy7XuTAeU/sekvjEiNHW00Q==\n" +67"-----END CERTIFICATE-----";6869private static X509Certificate cert;70private static PublicKey selfSignedCertPubKey;71private static PublicKey crlIssuerCertPubKey;7273public static void main(String[] args) throws Exception {74setup();7576/*77* Verify certificate with its own public key.78* Should pass.79*/80verifyCert(selfSignedCertPubKey,"SunRsaSign");8182/*83* Try to verify certificate with a provider that does not have a84* Signature implementation.85* Should fail with NoSuchAlgorithmException.86*/87try {88verifyCert(selfSignedCertPubKey, "SunJCE");89throw new RuntimeException("Didn't catch the exception properly");90} catch (NoSuchAlgorithmException e) {91System.out.println("Caught the correct exception.");92}9394/*95* Try to verify certificate with a provider that has a Signature96* implementation but not of the right algorithm (MD5withRSA).97* Should fail with NoSuchAlgorithmException.98*/99try {100verifyCert(selfSignedCertPubKey, "SUN");101throw new RuntimeException("Didn't catch the exception properly");102} catch (NoSuchAlgorithmException e) {103System.out.println("Caught the correct exception.");104}105106/*107* Try to verify certificate with the wrong public key.108* Should fail with SignatureException.109*/110try {111verifyCert(crlIssuerCertPubKey, "SunRsaSign");112throw new RuntimeException("Didn't catch the exception properly");113} catch (SignatureException e) {114System.out.println("Caught the correct exception.");115}116}117118private static void setup() throws CertificateException, CRLException {119CertificateFactory cf = CertificateFactory.getInstance("X.509");120121/* Get public key of the CRL issuer cert */122ByteArrayInputStream inputStream123= new ByteArrayInputStream(crlIssuerCertStr.getBytes());124cert = (X509Certificate)cf.generateCertificate(inputStream);125crlIssuerCertPubKey = cert.getPublicKey();126127/* Get public key of the self-signed Cert */128inputStream = new ByteArrayInputStream(selfSignedCertStr.getBytes());129selfSignedCertPubKey = cf.generateCertificate(inputStream).getPublicKey();130}131132private static void verifyCert(PublicKey key, String providerName)133throws CertificateException, NoSuchAlgorithmException,134InvalidKeyException, SignatureException {135Provider provider = Security.getProvider(providerName);136if (provider == null) {137throw new RuntimeException("Provider " + providerName138+ " not found.");139}140cert.verify(key, provider);141}142}143144145