1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8074935
  27  * @summary jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
  28  * @modules java.base/sun.security.provider
  29  */
  30 
  31 import java.io.ByteArrayOutputStream;
  32 import java.io.FileInputStream;
  33 import java.io.FileOutputStream;
  34 import java.io.PrintStream;
  35 import java.security.KeyStore;
  36 import java.security.cert.CertificateException;
  37 import java.util.Arrays;
  38 import java.util.Base64;
  39 
  40 import sun.security.provider.X509Factory;
  41 import java.security.cert.CertificateFactory;
  42 import java.io.ByteArrayInputStream;
  43 
  44 public class BadPem {
  45 
  46     public static void main(String[] args) throws Exception {
  47         String ks = System.getProperty("test.src", ".")
  48                 + "/../../../../javax/net/ssl/etc/keystore";
  49         String pass = "passphrase";
  50         String alias = "dummy";
  51 
  52         KeyStore keyStore = KeyStore.getInstance("JKS");
  53         keyStore.load(new FileInputStream(ks), pass.toCharArray());
  54         byte[] cert = keyStore.getCertificate(alias).getEncoded();
  55 
  56         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  57         PrintStream pout = new PrintStream(bout);
  58         byte[] CRLF = new byte[] {'\r', '\n'};
  59         pout.println(X509Factory.BEGIN_CERT);
  60         for (int i=0; i<cert.length; i += 48) {
  61             int blockLen = (cert.length > i + 48) ? 48 : (cert.length - i);
  62             pout.println("!" + Base64.getEncoder()
  63                     .encodeToString(Arrays.copyOfRange(cert, i, i + blockLen)));
  64         }
  65         pout.println(X509Factory.END_CERT);
  66 
  67         CertificateFactory cf = CertificateFactory.getInstance("X.509");
  68 
  69         try {
  70             cf.generateCertificate(new ByteArrayInputStream(bout.toByteArray()));
  71             throw new Exception("Should fail");
  72         } catch (CertificateException e) {
  73             // Good
  74         }
  75     }
  76 }
  77