Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/X509Factory/BadPem.java
38853 views
/*1* Copyright (c) 2015, 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 807493526* @summary jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did27*/2829import java.io.ByteArrayOutputStream;30import java.io.FileInputStream;31import java.io.FileOutputStream;32import java.io.PrintStream;33import java.security.KeyStore;34import java.security.cert.CertificateException;35import java.util.Arrays;36import java.util.Base64;3738import sun.security.provider.X509Factory;39import java.security.cert.CertificateFactory;40import java.io.ByteArrayInputStream;4142public class BadPem {4344public static void main(String[] args) throws Exception {45String ks = System.getProperty("test.src", ".")46+ "/../../../../javax/net/ssl/etc/keystore";47String pass = "passphrase";48String alias = "dummy";4950KeyStore keyStore = KeyStore.getInstance("JKS");51keyStore.load(new FileInputStream(ks), pass.toCharArray());52byte[] cert = keyStore.getCertificate(alias).getEncoded();5354ByteArrayOutputStream bout = new ByteArrayOutputStream();55PrintStream pout = new PrintStream(bout);56byte[] CRLF = new byte[] {'\r', '\n'};57pout.println(X509Factory.BEGIN_CERT);58for (int i=0; i<cert.length; i += 48) {59int blockLen = (cert.length > i + 48) ? 48 : (cert.length - i);60pout.println("!" + Base64.getEncoder()61.encodeToString(Arrays.copyOfRange(cert, i, i + blockLen)));62}63pout.println(X509Factory.END_CERT);6465CertificateFactory cf = CertificateFactory.getInstance("X.509");6667try {68cf.generateCertificate(new ByteArrayInputStream(bout.toByteArray()));69throw new Exception("Should fail");70} catch (CertificateException e) {71// Good72}73}74}75767778