Path: blob/master/test/jdk/sun/security/x509/URIName/Parse.java
41154 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* @modules java.base/sun.security.util29* java.base/sun.security.x50930*/31import java.io.ByteArrayInputStream;32import java.io.IOException;33import java.security.cert.CertificateFactory;34import java.security.cert.X509Certificate;35import sun.security.util.DerValue;36import sun.security.x509.CRLDistributionPointsExtension;37import sun.security.x509.URIName;383940public class Parse {4142// certificate with a space in the CRLDistributionPointsExtension uri43// uri: file://crl file.crl44static String certWithSpaceInCDPStr =45"-----BEGIN CERTIFICATE-----\n" +46"MIIB8DCCAVmgAwIBAgIJAOgNnYA5nHtQMA0GCSqGSIb3DQEBBQUAMCUxETAPBgNV\n" +47"BAMTCHRlc3RuYW1lMRAwDgYDVQQLEwd0ZXN0b3JnMB4XDTEyMDgxMzIzMzgzN1oX\n" +48"DTEyMDkxMjIzMzgzN1owJTERMA8GA1UEAxMIdGVzdG5hbWUxEDAOBgNVBAsTB3Rl\n" +49"c3RvcmcwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANx02RuD/Y2pvgVnXBbJ\n" +50"Sb+8j80geuoYEyRRnP6YiL2wmZqMmTHuznFwosO57KoVbz/XEr1bOnBMnPKax5Ll\n" +51"QlDI3nmnxmUq13ORQ6GkD3M+QRzzxc66BFJbKqUzgv1P3NngyIFr03zb/opXdCTZ\n" +52"4WfJuCf7Ouz44Ch6ZGQJ+7G5AgMBAAGjKDAmMCQGA1UdHwQdMBswGaAXoBWGE2Zp\n" +53"bGU6Ly9jcmwgZmlsZS5jcmwwDQYJKoZIhvcNAQEFBQADgYEAB+ublc1l1EnXtEJE\n" +54"jYeFzAdttHKQ4mn8CXGtHSy9gpckKyLdZUc9/n6yKuNXih29faepZ8mtaftTYpgR\n" +55"AUqZ+6YYik+rIqZpnWMPR9qZvshf/KPerXiZe7kYBKNvxgmCFfhK8QN6nxUGrR2F\n" +56"d53HWct6zXqlj+vQZsGC30f764M=\n" +57"-----END CERTIFICATE-----";5859// a certificate with backslashes in the CRLDistributionPointsExtension uri60// uri: file://\\\\CRL\\crl_file.crl61static String certWithBackslashesInCDPStr =62"-----BEGIN CERTIFICATE-----\n" +63"MIIB9jCCAV+gAwIBAgIJAOQV9wTIgnc1MA0GCSqGSIb3DQEBBQUAMCUxETAPBgNV\n" +64"BAMTCHRlc3RuYW1lMRAwDgYDVQQLEwd0ZXN0b3JnMB4XDTEyMDgxMzIzMzcxM1oX\n" +65"DTEyMDkxMjIzMzcxM1owJTERMA8GA1UEAxMIdGVzdG5hbWUxEDAOBgNVBAsTB3Rl\n" +66"c3RvcmcwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALdtczqZqI6RI17mz58/\n" +67"PgFNBmb/dX/MeVcuaUp79RVUbDQ68z4JxDNv4ImcAxigKXb2jObPAxOdM+VlXROS\n" +68"AmUNgYrIPuHNnKbd+rwilc6DsGWZnZLmZE63fUvTSqjOnSlsENSgDBVL/4r+yWBB\n" +69"8KKmFGRFqkCyN1EZl03IW9i7AgMBAAGjLjAsMCoGA1UdHwQjMCEwH6AdoBuGGWZp\n" +70"bGU6Ly9cXENSTFxjcmxfZmlsZS5jcmwwDQYJKoZIhvcNAQEFBQADgYEACOgZEaST\n" +71"BCFQVeXZ5d8J3dUZ+wRRkPvrlvopxMtZb3Hyte78PNoIZ78f1gYL18HiGYwKttau\n" +72"DyPp1lrG9xKPfIeKg+aDWTtVE7pexB4qCryID0+kJfdNzkdIgdGJzJ/RmfJ5heMF\n" +73"+R46Mhpua4c6gGsE2NGBFxmtS3YHpQsKtz8=\n" +74"-----END CERTIFICATE-----";7576/*77* Create an X509Certificate then attempt to construct a78* CRLDistributionPointsExtension object from its extension value bytes.79*/80private static void CRLDistributionPointsExtensionTest(String certStr)81throws Exception {82CertificateFactory cf = CertificateFactory.getInstance("X.509");83ByteArrayInputStream is = new ByteArrayInputStream(certStr.getBytes());84X509Certificate cert = (X509Certificate) cf.generateCertificate(is);8586// oid for CRL Distribution Points = 2.5.29.3187byte[] CDPExtBytes = cert.getExtensionValue("2.5.29.31");88DerValue val = new DerValue(CDPExtBytes);89byte[] data = val.getOctetString();90CRLDistributionPointsExtension CDPExt91= new CRLDistributionPointsExtension(false, data);92}9394public static void main(String[] args) throws Exception {95/* Try to parse a CRLDistributionPointsExtension URI with a space. */96try {97CRLDistributionPointsExtensionTest(certWithSpaceInCDPStr);98throw new RuntimeException("Illegally parsed a "99+ "CRLDistributionPointsExtension uri with a space.");100} catch (IOException e) {101System.out.println("Caught the correct exception.");102103}104105/* Try to parse a CRLDistributionPointsExtension URI with backslashes. */106try {107CRLDistributionPointsExtensionTest(certWithBackslashesInCDPStr);108throw new RuntimeException("Illegally parsed a "109+ "CRLDistributionPointsExtension uri with a backslashes.");110} catch (IOException e) {111System.out.println("Caught the correct exception.");112}113114/* Try to construct a URIName from a uri with a space. */115String uriWithSpace = "file://crl file.crl";116URIName name;117try {118name = new URIName(uriWithSpace);119throw new RuntimeException("Illegally created a URIName "120+ "from a uri with a space.");121} catch (IOException e) {122System.out.println("Caught the correct exception.");123}124125/* Try to construct a URIName from a uri with backslashes. */126String uriWithBackslashes = "file://\\\\CRL\\crl_file.crl";127try {128name = new URIName(uriWithBackslashes);129throw new RuntimeException("Illegally created a URIName "130+ "from a uri with backslashes.");131} catch (IOException e) {132System.out.println("Caught the correct exception.");133}134135System.out.println("Tests passed.");136}137}138139140141