Path: blob/master/test/jdk/sun/security/pkcs/pkcs8/TestLeadingZeros.java
41153 views
/*1* Copyright (c) 2017, 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 817525126* @summary ensure that PKCS8-encoded private key with leading 0s27* can be loaded.28* @run main TestLeadingZeros29*/3031import java.io.*;32import java.security.*;33import java.security.spec.PKCS8EncodedKeySpec;34import java.security.interfaces.*;35import java.util.*;3637public class TestLeadingZeros {3839// The following test vectors are various BER encoded PKCS8 bytes40static final String[] PKCS8_ENCODINGS = {41// first is the original one from PKCS8Test42"301e020100301206052b0e03020c30090201020201030201040403020101A000",43// changed original to version w/ 1 leading 044"301f02020000301206052b0e03020c30090201020201030201040403020101A000",45// changed original to P w/ 1 leading 046"301f020100301306052b0e03020c300a020200020201030201040403020101A000",47// changed original to X w/ 2 leading 0s48"3020020100301206052b0e03020c300902010202010302010404050203000001A000"49};5051public static void main(String[] argv) throws Exception {52KeyFactory factory = KeyFactory.getInstance("DSA", "SUN");5354for (String encodings : PKCS8_ENCODINGS) {55byte[] encodingBytes = hexToBytes(encodings);56PKCS8EncodedKeySpec encodedKeySpec =57new PKCS8EncodedKeySpec(encodingBytes);58DSAPrivateKey privKey2 = (DSAPrivateKey)59factory.generatePrivate(encodedKeySpec);60System.out.println("key: " + privKey2);61}62System.out.println("Test Passed");63}6465private static byte[] hexToBytes(String hex) {66if (hex.length() % 2 != 0) {67throw new RuntimeException("Input should be even length");68}69int size = hex.length() / 2;70byte[] result = new byte[size];71for (int i = 0; i < size; i++) {72int hi = Character.digit(hex.charAt(2 * i), 16);73int lo = Character.digit(hex.charAt(2 * i + 1), 16);74if ((hi == -1) || (lo == -1)) {75throw new RuntimeException("Input should be hexadecimal");76}77result[i] = (byte) (16 * hi + lo);78}79return result;80}81}828384