Path: blob/master/test/jdk/sun/security/provider/X509Factory/BadPem.java
41154 views
/*1* Copyright (c) 2015, 2018, 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 8074935 820860226* @summary X.509 cert PEM format read27* @modules java.base/sun.security.provider28*/2930import java.io.ByteArrayOutputStream;31import java.io.FileInputStream;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";4950CertificateFactory cf = CertificateFactory.getInstance("X.509");51KeyStore keyStore = KeyStore.getInstance("JKS");52keyStore.load(new FileInputStream(ks), pass.toCharArray());53byte[] cert = keyStore.getCertificate(alias).getEncoded();5455// 807493556ByteArrayOutputStream bout = new ByteArrayOutputStream();57PrintStream pout = new PrintStream(bout);58byte[] CRLF = new byte[] {'\r', '\n'};59pout.println(X509Factory.BEGIN_CERT);60for (int i=0; i<cert.length; i += 48) {61int blockLen = (cert.length > i + 48) ? 48 : (cert.length - i);62pout.println("!" + Base64.getEncoder()63.encodeToString(Arrays.copyOfRange(cert, i, i + blockLen)));64}65pout.println(X509Factory.END_CERT);6667try {68cf.generateCertificate(new ByteArrayInputStream(bout.toByteArray()));69throw new Exception("Should fail");70} catch (CertificateException e) {71// Good72}7374// 820860275bout.reset();76pout.println(X509Factory.BEGIN_CERT + " ");77pout.println(Base64.getMimeEncoder().encodeToString(cert));78pout.println(X509Factory.END_CERT + " ");7980cf.generateCertificate(new ByteArrayInputStream(bout.toByteArray()));81}82}83848586