Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/x509/URIName/Parse.java
38918 views
/*1* Copyright (c) 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 800538926* @summary CRL Distribution Point URIs with spaces or backslashes should27* not be parseable28*/29import java.io.ByteArrayInputStream;30import java.io.IOException;31import java.security.cert.CertificateFactory;32import java.security.cert.X509Certificate;33import sun.security.util.DerValue;34import sun.security.x509.CRLDistributionPointsExtension;35import sun.security.x509.URIName;363738public class Parse {3940// certificate with a space in the CRLDistributionPointsExtension uri41// uri: file://crl file.crl42static String certWithSpaceInCDPStr =43"-----BEGIN CERTIFICATE-----\n" +44"MIIB8DCCAVmgAwIBAgIJAOgNnYA5nHtQMA0GCSqGSIb3DQEBBQUAMCUxETAPBgNV\n" +45"BAMTCHRlc3RuYW1lMRAwDgYDVQQLEwd0ZXN0b3JnMB4XDTEyMDgxMzIzMzgzN1oX\n" +46"DTEyMDkxMjIzMzgzN1owJTERMA8GA1UEAxMIdGVzdG5hbWUxEDAOBgNVBAsTB3Rl\n" +47"c3RvcmcwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANx02RuD/Y2pvgVnXBbJ\n" +48"Sb+8j80geuoYEyRRnP6YiL2wmZqMmTHuznFwosO57KoVbz/XEr1bOnBMnPKax5Ll\n" +49"QlDI3nmnxmUq13ORQ6GkD3M+QRzzxc66BFJbKqUzgv1P3NngyIFr03zb/opXdCTZ\n" +50"4WfJuCf7Ouz44Ch6ZGQJ+7G5AgMBAAGjKDAmMCQGA1UdHwQdMBswGaAXoBWGE2Zp\n" +51"bGU6Ly9jcmwgZmlsZS5jcmwwDQYJKoZIhvcNAQEFBQADgYEAB+ublc1l1EnXtEJE\n" +52"jYeFzAdttHKQ4mn8CXGtHSy9gpckKyLdZUc9/n6yKuNXih29faepZ8mtaftTYpgR\n" +53"AUqZ+6YYik+rIqZpnWMPR9qZvshf/KPerXiZe7kYBKNvxgmCFfhK8QN6nxUGrR2F\n" +54"d53HWct6zXqlj+vQZsGC30f764M=\n" +55"-----END CERTIFICATE-----";5657// a certificate with backslashes in the CRLDistributionPointsExtension uri58// uri: file://\\\\CRL\\crl_file.crl59static String certWithBackslashesInCDPStr =60"-----BEGIN CERTIFICATE-----\n" +61"MIIB9jCCAV+gAwIBAgIJAOQV9wTIgnc1MA0GCSqGSIb3DQEBBQUAMCUxETAPBgNV\n" +62"BAMTCHRlc3RuYW1lMRAwDgYDVQQLEwd0ZXN0b3JnMB4XDTEyMDgxMzIzMzcxM1oX\n" +63"DTEyMDkxMjIzMzcxM1owJTERMA8GA1UEAxMIdGVzdG5hbWUxEDAOBgNVBAsTB3Rl\n" +64"c3RvcmcwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALdtczqZqI6RI17mz58/\n" +65"PgFNBmb/dX/MeVcuaUp79RVUbDQ68z4JxDNv4ImcAxigKXb2jObPAxOdM+VlXROS\n" +66"AmUNgYrIPuHNnKbd+rwilc6DsGWZnZLmZE63fUvTSqjOnSlsENSgDBVL/4r+yWBB\n" +67"8KKmFGRFqkCyN1EZl03IW9i7AgMBAAGjLjAsMCoGA1UdHwQjMCEwH6AdoBuGGWZp\n" +68"bGU6Ly9cXENSTFxjcmxfZmlsZS5jcmwwDQYJKoZIhvcNAQEFBQADgYEACOgZEaST\n" +69"BCFQVeXZ5d8J3dUZ+wRRkPvrlvopxMtZb3Hyte78PNoIZ78f1gYL18HiGYwKttau\n" +70"DyPp1lrG9xKPfIeKg+aDWTtVE7pexB4qCryID0+kJfdNzkdIgdGJzJ/RmfJ5heMF\n" +71"+R46Mhpua4c6gGsE2NGBFxmtS3YHpQsKtz8=\n" +72"-----END CERTIFICATE-----";7374/*75* Create an X509Certificate then attempt to construct a76* CRLDistributionPointsExtension object from its extension value bytes.77*/78private static void CRLDistributionPointsExtensionTest(String certStr)79throws Exception {80CertificateFactory cf = CertificateFactory.getInstance("X.509");81ByteArrayInputStream is = new ByteArrayInputStream(certStr.getBytes());82X509Certificate cert = (X509Certificate) cf.generateCertificate(is);8384// oid for CRL Distribution Points = 2.5.29.3185byte[] CDPExtBytes = cert.getExtensionValue("2.5.29.31");86DerValue val = new DerValue(CDPExtBytes);87byte[] data = val.getOctetString();88CRLDistributionPointsExtension CDPExt89= new CRLDistributionPointsExtension(false, data);90}9192public static void main(String[] args) throws Exception {93/* Try to parse a CRLDistributionPointsExtension URI with a space. */94try {95CRLDistributionPointsExtensionTest(certWithSpaceInCDPStr);96throw new RuntimeException("Illegally parsed a "97+ "CRLDistributionPointsExtension uri with a space.");98} catch (IOException e) {99System.out.println("Caught the correct exception.");100101}102103/* Try to parse a CRLDistributionPointsExtension URI with backslashes. */104try {105CRLDistributionPointsExtensionTest(certWithBackslashesInCDPStr);106throw new RuntimeException("Illegally parsed a "107+ "CRLDistributionPointsExtension uri with a backslashes.");108} catch (IOException e) {109System.out.println("Caught the correct exception.");110}111112/* Try to construct a URIName from a uri with a space. */113String uriWithSpace = "file://crl file.crl";114URIName name;115try {116name = new URIName(uriWithSpace);117throw new RuntimeException("Illegally created a URIName "118+ "from a uri with a space.");119} catch (IOException e) {120System.out.println("Caught the correct exception.");121}122123/* Try to construct a URIName from a uri with backslashes. */124String uriWithBackslashes = "file://\\\\CRL\\crl_file.crl";125try {126name = new URIName(uriWithBackslashes);127throw new RuntimeException("Illegally created a URIName "128+ "from a uri with backslashes.");129} catch (IOException e) {130System.out.println("Caught the correct exception.");131}132133System.out.println("Tests passed.");134}135}136137138139