Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs/pkcs8/PKCS8Test.java
38854 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 804835726* @summary PKCS8 Standards Conformance Tests27* @requires (os.family != "solaris")28* @compile -XDignore.symbol.file PKCS8Test.java29* @run main PKCS8Test30*/3132/*33* Skip Solaris since the DSAPrivateKeys returned by34* SunPKCS11 Provider are not subclasses of PKCS8Key35*/36import java.io.IOException;37import java.math.BigInteger;38import java.security.InvalidKeyException;39import java.util.Arrays;40import sun.misc.HexDumpEncoder;41import sun.security.pkcs.PKCS8Key;42import sun.security.provider.DSAPrivateKey;43import sun.security.util.DerOutputStream;44import sun.security.util.DerValue;45import sun.security.x509.AlgorithmId;46import static java.lang.System.out;4748public class PKCS8Test {4950static final HexDumpEncoder hexDump = new HexDumpEncoder();5152static final DerOutputStream derOutput = new DerOutputStream();5354static final String FORMAT = "PKCS#8";55static final String EXPECTED_ALG_ID_CHRS = "DSA, \n\tp: 02\n\tq: 03\n"56+ "\tg: 04\n";57static final String ALGORITHM = "DSA";58static final String EXCEPTION_MESSAGE = "version mismatch: (supported: "59+ "00, parsed: 01";6061// test second branch in byte[] encode()62// DER encoding,include (empty) set of attributes63static final int[] NEW_ENCODED_KEY_INTS = { 0x30,64// length 30 = 0x1e650x1e,66// first element67// version Version (= INTEGER)680x02,69// length 1700x01,71// value 0720x00,73// second element74// privateKeyAlgorithmIdentifier PrivateKeyAlgorithmIdentifier75// (sequence)76// (an object identifier?)770x30,78// length 18790x12,80// contents81// object identifier, 5 bytes820x06, 0x05,83// { 1 3 14 3 2 12 }840x2b, 0x0e, 0x03, 0x02, 0x0c,85// sequence, 9 bytes860x30, 0x09,87// integer 2880x02, 0x01, 0x02,89// integer 3900x02, 0x01, 0x03,91// integer 4920x02, 0x01, 0x04,93// third element94// privateKey PrivateKey (= OCTET STRING)950x04,96// length970x03,98// privateKey contents990x02, 0x01, 0x01,100// 4th (optional) element -- attributes [0] IMPLICIT Attributes101// OPTIONAL102// (Attributes = SET OF Attribute) Here, it will be empty.1030xA0,104// length1050x00 };106107// encoding originally created, but with the version changed108static final int[] NEW_ENCODED_KEY_INTS_2 = {109// sequence1100x30,111// length 28 = 0x1c1120x1c,113// first element114// version Version (= INTEGER)1150x02,116// length 11170x01,118// value 1 (illegal)1190x01,120// second element121// privateKeyAlgorithmIdentifier PrivateKeyAlgorithmIdentifier122// (sequence)123// (an object identifier?)1240x30,125// length 181260x12,127// contents128// object identifier, 5 bytes1290x06, 0x05,130// { 1 3 14 3 2 12 }1310x2b, 0x0e, 0x03, 0x02, 0x0c,132// sequence, 9 bytes1330x30, 0x09,134// integer 21350x02, 0x01, 0x02,136// integer 31370x02, 0x01, 0x03,138// integer 41390x02, 0x01, 0x04,140// third element141// privateKey PrivateKey (= OCTET STRING)1420x04,143// length1440x03,145// privateKey contents1460x02, 0x01, 0x01 };147148// 0000: 30 1E 02 01 00 30 14 06 07 2A 86 48 CE 38 04 01 0....0...*.H.8..149// 0010: 30 09 02 01 02 02 01 03 02 01 04 04 03 02 01 01 0...............150static final int[] EXPECTED = { 0x30,151// length 30 = 0x1e1520x1e,153// first element154// version Version (= INTEGER)1550x02,156// length 11570x01,158// value 01590x00,160// second element161// privateKeyAlgorithmIdentifier PrivateKeyAlgorithmIdentifier162// (sequence)163// (an object identifier?)1640x30, 0x14, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x38, 0x04, 0x01,165// integer 21660x30, 0x09, 0x02,167// integer 31680x01, 0x02, 0x02,169// integer 41700x01, 0x03, 0x02,171// third element172// privateKey PrivateKey (= OCTET STRING)1730x01,174// length1750x04,176// privateKey contents1770x04, 0x03, 0x02,178// 4th (optional) element -- attributes [0] IMPLICIT Attributes179// OPTIONAL180// (Attributes = SET OF Attribute) Here, it will be empty.1810x01,182// length1830x01 };184185static void raiseException(String expected, String received) {186throw new RuntimeException(187"Expected " + expected + "; Received " + received);188}189190public static void main(String[] args)191throws IOException, InvalidKeyException {192193BigInteger x = BigInteger.valueOf(1);194BigInteger p = BigInteger.valueOf(2);195BigInteger q = BigInteger.valueOf(3);196BigInteger g = BigInteger.valueOf(4);197198DSAPrivateKey priv = new DSAPrivateKey(x, p, q, g);199200byte[] encodedKey = priv.getEncoded();201byte[] expectedBytes = new byte[EXPECTED.length];202for (int i = 0; i < EXPECTED.length; i++) {203expectedBytes[i] = (byte) EXPECTED[i];204}205206dumpByteArray("encodedKey :", encodedKey);207if (!Arrays.equals(encodedKey, expectedBytes)) {208raiseException(new String(expectedBytes), new String(encodedKey));209}210211PKCS8Key decodedKey = PKCS8Key.parse(new DerValue(encodedKey));212213String alg = decodedKey.getAlgorithm();214AlgorithmId algId = decodedKey.getAlgorithmId();215out.println("Algorithm :" + alg);216out.println("AlgorithmId: " + algId);217218if (!ALGORITHM.equals(alg)) {219raiseException(ALGORITHM, alg);220}221if (!EXPECTED_ALG_ID_CHRS.equalsIgnoreCase(algId.toString())) {222raiseException(EXPECTED_ALG_ID_CHRS, algId.toString());223}224225decodedKey.encode(derOutput);226dumpByteArray("Stream encode: ", derOutput.toByteArray());227if (!Arrays.equals(derOutput.toByteArray(), expectedBytes)) {228raiseException(new String(expectedBytes), derOutput.toString());229}230231dumpByteArray("byte[] encoding: ", decodedKey.getEncoded());232if (!Arrays.equals(decodedKey.getEncoded(), expectedBytes)) {233raiseException(new String(expectedBytes),234new String(decodedKey.getEncoded()));235}236237if (!FORMAT.equals(decodedKey.getFormat())) {238raiseException(FORMAT, decodedKey.getFormat());239}240241try {242byte[] newEncodedKey = new byte[NEW_ENCODED_KEY_INTS.length];243for (int i = 0; i < newEncodedKey.length; i++) {244newEncodedKey[i] = (byte) NEW_ENCODED_KEY_INTS[i];245}246PKCS8Key newDecodedKey = PKCS8Key247.parse(new DerValue(newEncodedKey));248249throw new RuntimeException(250"key1: Expected an IOException during " + "parsing");251} catch (IOException e) {252System.out.println("newEncodedKey: should have excess data due to "253+ "attributes, which are not supported");254}255256try {257byte[] newEncodedKey2 = new byte[NEW_ENCODED_KEY_INTS_2.length];258for (int i = 0; i < newEncodedKey2.length; i++) {259newEncodedKey2[i] = (byte) NEW_ENCODED_KEY_INTS_2[i];260}261262PKCS8Key newDecodedKey2 = PKCS8Key263.parse(new DerValue(newEncodedKey2));264265throw new RuntimeException(266"key2: Expected an IOException during " + "parsing");267} catch (IOException e) {268out.println("Key 2: should be illegal version");269out.println(e.getMessage());270if (!EXCEPTION_MESSAGE.equals(e.getMessage())) {271throw new RuntimeException("Key2: expected: "272+ EXCEPTION_MESSAGE + " get: " + e.getMessage());273}274}275}276277static void dumpByteArray(String nm, byte[] bytes) throws IOException {278out.println(nm + " length: " + bytes.length);279hexDump.encodeBuffer(bytes, out);280}281}282283284