1 /*
   2  * Copyright (c) 2017, 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 8175251
  27  * @summary ensure that PKCS8-encoded private key with leading 0s
  28  * can be loaded.
  29  * @run main TestLeadingZeros
  30  */
  31 
  32 import java.io.*;
  33 import java.security.*;
  34 import java.security.spec.PKCS8EncodedKeySpec;
  35 import java.security.interfaces.*;
  36 import java.util.*;
  37 
  38 public class TestLeadingZeros {
  39 
  40     // The following test vectors are various BER encoded PKCS8 bytes
  41     static final String[] PKCS8_ENCODINGS  = {
  42         // first is the original one from PKCS8Test
  43         "301e020100301206052b0e03020c30090201020201030201040403020101A000",
  44         // changed original to version w/ 1 leading 0
  45         "301f02020000301206052b0e03020c30090201020201030201040403020101A000",
  46         // changed original to P w/ 1 leading 0
  47         "301f020100301306052b0e03020c300a020200020201030201040403020101A000",
  48         // changed original to X w/ 2 leading 0s
  49         "3020020100301206052b0e03020c300902010202010302010404050203000001A000"
  50     };
  51 
  52     public static void main(String[] argv) throws Exception {
  53         KeyFactory factory = KeyFactory.getInstance("DSA", "SUN");
  54 
  55         for (String encodings : PKCS8_ENCODINGS) {
  56             byte[] encodingBytes = hexToBytes(encodings);
  57             PKCS8EncodedKeySpec encodedKeySpec =
  58                 new PKCS8EncodedKeySpec(encodingBytes);
  59             DSAPrivateKey privKey2 = (DSAPrivateKey)
  60                 factory.generatePrivate(encodedKeySpec);
  61             System.out.println("key: " + privKey2);
  62         }
  63         System.out.println("Test Passed");
  64     }
  65 
  66     private static byte[] hexToBytes(String hex) {
  67         if (hex.length() % 2 != 0) {
  68             throw new RuntimeException("Input should be even length");
  69         }
  70         int size = hex.length() / 2;
  71         byte[] result = new byte[size];
  72         for (int i = 0; i < size; i++) {
  73             int hi = Character.digit(hex.charAt(2 * i), 16);
  74             int lo = Character.digit(hex.charAt(2 * i + 1), 16);
  75             if ((hi == -1) || (lo == -1)) {
  76                 throw new RuntimeException("Input should be hexadecimal");
  77             }
  78             result[i] = (byte) (16 * hi + lo);
  79         }
  80         return result;
  81     }
  82 }