Path: blob/master/test/jdk/sun/security/pkcs/pkcs8/PKCS8Test.java
41153 views
/*1* Copyright (c) 2015, 2021, 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 8048357 824456526* @summary PKCS8 Standards Conformance Tests27* @library /test/lib28* @modules java.base/sun.security.pkcs29* java.base/sun.security.util30* java.base/sun.security.provider31* java.base/sun.security.x50932* @compile -XDignore.symbol.file PKCS8Test.java33* @run testng PKCS8Test34*/3536import java.io.IOException;37import java.math.BigInteger;38import java.util.Arrays;39import java.util.HexFormat;4041import jdk.test.lib.hexdump.ASN1Formatter;42import jdk.test.lib.hexdump.HexPrinter;43import org.testng.Assert;44import org.testng.annotations.Test;45import sun.security.pkcs.PKCS8Key;46import sun.security.provider.DSAPrivateKey;47import sun.security.util.DerValue;4849public class PKCS8Test {5051static final String FORMAT = "PKCS#8";52static final String EXPECTED_ALG_ID_CHRS = "DSA, \n" +53"\tp: 02\n\tq: 03\n\tg: 04\n";54static final String ALGORITHM = "DSA";5556static final byte[] EXPECTED = HexFormat.of().parseHex(57"301e" + // SEQUENCE58"020100" + // Version int 059"3014" + // PrivateKeyAlgorithmIdentifier60"06072a8648ce380401" + // OID DSA 1.2.840.10040.4.161"3009020102020103020104" + // p=2, q=3, g=462"0403020101"); // PrivateKey OCTET int x = 16364@Test65public void test() throws IOException {6667byte[] encodedKey = new DSAPrivateKey(68BigInteger.valueOf(1),69BigInteger.valueOf(2),70BigInteger.valueOf(3),71BigInteger.valueOf(4)).getEncoded();7273Assert.assertTrue(Arrays.equals(encodedKey, EXPECTED),74HexPrinter.simple()75.formatter(ASN1Formatter.formatter())76.toString(encodedKey));7778PKCS8Key decodedKey = (PKCS8Key)PKCS8Key.parseKey(encodedKey);7980Assert.assertEquals(decodedKey.getAlgorithm(), ALGORITHM);81Assert.assertEquals(decodedKey.getFormat(), FORMAT);82Assert.assertEquals(decodedKey.getAlgorithmId().toString(),83EXPECTED_ALG_ID_CHRS);8485byte[] encodedOutput = decodedKey.getEncoded();86Assert.assertTrue(Arrays.equals(encodedOutput, EXPECTED),87HexPrinter.simple()88.formatter(ASN1Formatter.formatter())89.toString(encodedOutput));9091// Test additional fields92enlarge(0, "8000"); // attributes93enlarge(1, "810100"); // public key for v294enlarge(1, "8000", "810100"); // both9596Assert.assertThrows(() -> enlarge(2)); // bad ver97Assert.assertThrows(() -> enlarge(0, "8000", "8000")); // no dup98Assert.assertThrows(() -> enlarge(0, "810100")); // no public in v199Assert.assertThrows(() -> enlarge(1, "810100", "8000")); // bad order100Assert.assertThrows(() -> enlarge(1, "820100")); // bad tag101}102103/**104* Add more fields to EXPECTED and see if it's still valid PKCS8.105*106* @param newVersion new version107* @param fields extra fields to add, in hex108*/109static void enlarge(int newVersion, String... fields) throws IOException {110byte[] original = EXPECTED.clone();111int length = original.length;112for (String field : fields) { // append fields113byte[] add = HexFormat.of().parseHex(field);114original = Arrays.copyOf(original, length + add.length);115System.arraycopy(add, 0, original, length, add.length);116length += add.length;117}118Assert.assertTrue(length < 127);119original[1] = (byte)(length - 2); // the length field inside DER120original[4] = (byte)newVersion; // the version inside DER121PKCS8Key.parseKey(original);122}123}124125126