Path: blob/master/test/jdk/sun/security/x509/X509CRLImpl/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 X509CRL should have verify(PublicKey key, Provider sigProvider)27*/2829import java.io.ByteArrayInputStream;30import java.security.*;31import java.security.cert.*;3233public class Verify {3435static String selfSignedCertStr =36"-----BEGIN CERTIFICATE-----\n" +37"MIICPjCCAaegAwIBAgIBADANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +38"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzJaFw0zMDA0MDcwMjI0MzJa\n" +39"MB8xCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMIGfMA0GCSqGSIb3DQEB\n" +40"AQUAA4GNADCBiQKBgQC4OTag24sTxL2tXTNuvpmUEtdxrYAZoFsslFQ60T+WD9wQ\n" +41"Jeiw87FSPsR2vxRuv0j8DNm2a4h7LNNIFcLurfNldbz5pvgZ7VqdbbUMPE9qP85n\n" +42"jgDl4woyRTSUeRI4A7O0CO6NpES21dtbdhroWQrEkHxpnrDPxsxrz5gf2m3gqwID\n" +43"AQABo4GJMIGGMB0GA1UdDgQWBBSCJd0hpl5PdAD9IZS+Hzng4lXLGzBHBgNVHSME\n" +44"QDA+gBSCJd0hpl5PdAD9IZS+Hzng4lXLG6EjpCEwHzELMAkGA1UEBhMCVVMxEDAO\n" +45"BgNVBAoTB0V4YW1wbGWCAQAwDwYDVR0TAQH/BAUwAwEB/zALBgNVHQ8EBAMCAgQw\n" +46"DQYJKoZIhvcNAQEEBQADgYEAluy6HIjWcq009lTLmhp+Np6dxU78pInBK8RZkza0\n" +47"484qGaxFGD3UGyZkI5uWmsH2XuMbuox5khfIq6781gmkPBHXBIEtJN8eLusOHEye\n" +48"iE8h7WI+N3qa6Pj56WionMrioqC/3X+b06o147bbhx8U0vkYv/HyPaITOFfMXTdz\n" +49"Vjw=\n" +50"-----END CERTIFICATE-----";5152static String crlIssuerCertStr =53"-----BEGIN CERTIFICATE-----\n" +54"MIICKzCCAZSgAwIBAgIBAjANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQ\n" +55"MA4GA1UEChMHRXhhbXBsZTAeFw0wOTA0MjcwMjI0MzNaFw0yOTAxMTIwMjI0MzNa\n" +56"MB8xCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdFeGFtcGxlMIGfMA0GCSqGSIb3DQEB\n" +57"AQUAA4GNADCBiQKBgQDMJeBMBybHykI/YpwUJ4O9euqDSLb1kpWpceBS8TVqvgBC\n" +58"SgUJWtFZL0i6bdvF6mMdlbuBkGzhXqHiVAi96/zRLbUC9F8SMEJ6MuD+YhQ0ZFTQ\n" +59"atKy8zf8O9XzztelLJ26Gqb7QPV133WY3haAqHtCXOhEKkCN16NOYNC37DTaJwID\n" +60"AQABo3cwdTAdBgNVHQ4EFgQULXSWzXzUOIpOJpzbSCpW42IJUugwRwYDVR0jBEAw\n" +61"PoAUgiXdIaZeT3QA/SGUvh854OJVyxuhI6QhMB8xCzAJBgNVBAYTAlVTMRAwDgYD\n" +62"VQQKEwdFeGFtcGxlggEAMAsGA1UdDwQEAwIBAjANBgkqhkiG9w0BAQQFAAOBgQAY\n" +63"eMnf5AHSNlyUlzXk8o2S0h4gCuvKX6C3kFfKuZcWvFAbx4yQOWLS2s15/nzR4+AP\n" +64"FGX3lgJjROyAh7fGedTQK+NFWwkM2ag1g3hXktnlnT1qHohi0w31nVBJxXEDO/Ck\n" +65"uJTpJGt8XxxbFaw5v7cHy7XuTAeU/sekvjEiNHW00Q==\n" +66"-----END CERTIFICATE-----";6768static String crlStr =69"-----BEGIN X509 CRL-----\n" +70"MIIBGzCBhQIBATANBgkqhkiG9w0BAQQFADAfMQswCQYDVQQGEwJVUzEQMA4GA1UE\n" +71"ChMHRXhhbXBsZRcNMDkwNDI3MDIzODA0WhcNMjgwNjI2MDIzODA0WjAiMCACAQUX\n" +72"DTA5MDQyNzAyMzgwMFowDDAKBgNVHRUEAwoBBKAOMAwwCgYDVR0UBAMCAQIwDQYJ\n" +73"KoZIhvcNAQEEBQADgYEAoarfzXEtw3ZDi4f9U8eSvRIipHSyxOrJC7HR/hM5VhmY\n" +74"CErChny6x9lBVg9s57tfD/P9PSzBLusCcHwHMAbMOEcTltVVKUWZnnbumpywlYyg\n" +75"oKLrE9+yCOkYUOpiRlz43/3vkEL5hjIKMcDSZnPKBZi1h16Yj2hPe9GMibNip54=\n" +76"-----END X509 CRL-----";7778private static X509CRL crl;79private static PublicKey selfSignedCertPubKey;80private static PublicKey crlIssuerCertPubKey;8182public static void main(String[] args) throws Exception {83setup();8485/*86* Verify CRL with its own public key.87* Should pass.88*/89verifyCRL(crlIssuerCertPubKey, "SunRsaSign");9091/*92* Try to verify CRL with a provider that does not have a Signature93* implementation.94* Should fail with NoSuchAlgorithmException.95*/96try {97verifyCRL(crlIssuerCertPubKey, "SunJCE");98throw new RuntimeException("Didn't catch the exception properly");99} catch (NoSuchAlgorithmException e) {100System.out.println("Caught the correct exception.");101}102103/*104* Try to verify CRL with a provider that has a Signature implementation105* but not of the right algorithm (MD5withRSA).106* Should fail with NoSuchAlgorithmException.107*/108try {109verifyCRL(crlIssuerCertPubKey, "SUN");110throw new RuntimeException("Didn't catch the exception properly");111} catch (NoSuchAlgorithmException e) {112System.out.println("Caught the correct exception.");113}114115/*116* Try to verify CRL with the wrong public key.117* Should fail with SignatureException.118*/119try {120verifyCRL(selfSignedCertPubKey, "SunRsaSign");121throw new RuntimeException("Didn't catch the exception properly");122} catch (SignatureException e) {123System.out.println("Caught the correct exception.");124}125}126127private static void setup() throws CertificateException, CRLException {128CertificateFactory cf = CertificateFactory.getInstance("X.509");129130/* Create CRL */131ByteArrayInputStream inputStream =132new ByteArrayInputStream(crlStr.getBytes());133crl = (X509CRL)cf.generateCRL(inputStream);134135/* Get public key of the CRL issuer cert */136inputStream = new ByteArrayInputStream(crlIssuerCertStr.getBytes());137X509Certificate cert138= (X509Certificate)cf.generateCertificate(inputStream);139crlIssuerCertPubKey = cert.getPublicKey();140141/* Get public key of the self-signed Cert */142inputStream = new ByteArrayInputStream(selfSignedCertStr.getBytes());143selfSignedCertPubKey = cf.generateCertificate(inputStream).getPublicKey();144}145146private static void verifyCRL(PublicKey key, String providerName)147throws CRLException, NoSuchAlgorithmException, InvalidKeyException,148SignatureException {149Provider provider = Security.getProvider(providerName);150if (provider == null) {151throw new RuntimeException("Provider " + providerName152+ " not found.");153}154crl.verify(key, provider);155}156}157158159