1 /*
   2  * Copyright (c) 2015, 2016, 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 8048357
  27  * @summary PKCS8 Standards Conformance Tests
  28  * @compile -XDignore.symbol.file PKCS8Test.java
  29  * @run main PKCS8Test
  30  */
  31 import java.io.IOException;
  32 import java.math.BigInteger;
  33 import java.security.InvalidKeyException;
  34 import java.util.Arrays;
  35 import sun.misc.HexDumpEncoder;
  36 import sun.security.pkcs.PKCS8Key;
  37 import sun.security.provider.DSAPrivateKey;
  38 import sun.security.util.DerOutputStream;
  39 import sun.security.util.DerValue;
  40 import sun.security.x509.AlgorithmId;
  41 
  42 import static java.lang.System.out;
  43 
  44 public class PKCS8Test {
  45 
  46     static final HexDumpEncoder hexDump = new HexDumpEncoder();
  47 
  48     static final DerOutputStream derOutput = new DerOutputStream();
  49 
  50     static final String FORMAT = "PKCS#8";
  51     static final String EXPECTED_ALG_ID_CHRS = "DSA\n\tp:     02\n\tq:     03\n"
  52             + "\tg:     04\n";
  53     static final String ALGORITHM = "DSA";
  54     static final String EXCEPTION_MESSAGE = "version mismatch: (supported:     "
  55             + "00, parsed:     01";
  56 
  57     // test second branch in byte[] encode()
  58     // DER encoding,include (empty) set of attributes
  59     static final int[] NEW_ENCODED_KEY_INTS = { 0x30,
  60             // length 30 = 0x1e
  61             0x1e,
  62             // first element
  63             // version Version (= INTEGER)
  64             0x02,
  65             // length 1
  66             0x01,
  67             // value 0
  68             0x00,
  69             // second element
  70             // privateKeyAlgorithmIdentifier PrivateKeyAlgorithmIdentifier
  71             // (sequence)
  72             // (an object identifier?)
  73             0x30,
  74             // length 18
  75             0x12,
  76             // contents
  77             // object identifier, 5 bytes
  78             0x06, 0x05,
  79             // { 1 3 14 3 2 12 }
  80             0x2b, 0x0e, 0x03, 0x02, 0x0c,
  81             // sequence, 9 bytes
  82             0x30, 0x09,
  83             // integer 2
  84             0x02, 0x01, 0x02,
  85             // integer 3
  86             0x02, 0x01, 0x03,
  87             // integer 4
  88             0x02, 0x01, 0x04,
  89             // third element
  90             // privateKey PrivateKey (= OCTET STRING)
  91             0x04,
  92             // length
  93             0x03,
  94             // privateKey contents
  95             0x02, 0x01, 0x01,
  96             // 4th (optional) element -- attributes [0] IMPLICIT Attributes
  97             // OPTIONAL
  98             // (Attributes = SET OF Attribute) Here, it will be empty.
  99             0xA0,
 100             // length
 101             0x00 };
 102 
 103     // encoding originally created, but with the version changed
 104     static final int[] NEW_ENCODED_KEY_INTS_2 = {
 105             // sequence
 106             0x30,
 107             // length 28 = 0x1c
 108             0x1c,
 109             // first element
 110             // version Version (= INTEGER)
 111             0x02,
 112             // length 1
 113             0x01,
 114             // value 1 (illegal)
 115             0x01,
 116             // second element
 117             // privateKeyAlgorithmIdentifier PrivateKeyAlgorithmIdentifier
 118             // (sequence)
 119             // (an object identifier?)
 120             0x30,
 121             // length 18
 122             0x12,
 123             // contents
 124             // object identifier, 5 bytes
 125             0x06, 0x05,
 126             // { 1 3 14 3 2 12 }
 127             0x2b, 0x0e, 0x03, 0x02, 0x0c,
 128             // sequence, 9 bytes
 129             0x30, 0x09,
 130             // integer 2
 131             0x02, 0x01, 0x02,
 132             // integer 3
 133             0x02, 0x01, 0x03,
 134             // integer 4
 135             0x02, 0x01, 0x04,
 136             // third element
 137             // privateKey PrivateKey (= OCTET STRING)
 138             0x04,
 139             // length
 140             0x03,
 141             // privateKey contents
 142             0x02, 0x01, 0x01 };
 143 
 144     // 0000: 30 1E 02 01 00 30 14 06 07 2A 86 48 CE 38 04 01 0....0...*.H.8..
 145     // 0010: 30 09 02 01 02 02 01 03 02 01 04 04 03 02 01 01 0...............
 146     static final int[] EXPECTED = { 0x30,
 147             // length 30 = 0x1e
 148             0x1e,
 149             // first element
 150             // version Version (= INTEGER)
 151             0x02,
 152             // length 1
 153             0x01,
 154             // value 0
 155             0x00,
 156             // second element
 157             // privateKeyAlgorithmIdentifier PrivateKeyAlgorithmIdentifier
 158             // (sequence)
 159             // (an object identifier?)
 160             0x30, 0x14, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x38, 0x04, 0x01,
 161             // integer 2
 162             0x30, 0x09, 0x02,
 163             // integer 3
 164             0x01, 0x02, 0x02,
 165             // integer 4
 166             0x01, 0x03, 0x02,
 167             // third element
 168             // privateKey PrivateKey (= OCTET STRING)
 169             0x01,
 170             // length
 171             0x04,
 172             // privateKey contents
 173             0x04, 0x03, 0x02,
 174             // 4th (optional) element -- attributes [0] IMPLICIT Attributes
 175             // OPTIONAL
 176             // (Attributes = SET OF Attribute) Here, it will be empty.
 177             0x01,
 178             // length
 179             0x01 };
 180 
 181     static void raiseException(String expected, String received) {
 182         throw new RuntimeException(
 183                 "Expected " + expected + "; Received " + received);
 184     }
 185 
 186     public static void main(String[] args)
 187             throws IOException, InvalidKeyException {
 188 
 189         byte[] encodedKey = getEncodedKey();
 190         byte[] expectedBytes = new byte[EXPECTED.length];
 191         for (int i = 0; i < EXPECTED.length; i++) {
 192             expectedBytes[i] = (byte) EXPECTED[i];
 193         }
 194 
 195         dumpByteArray("encodedKey :", encodedKey);
 196         if (!Arrays.equals(encodedKey, expectedBytes)) {
 197             raiseException(new String(expectedBytes), new String(encodedKey));
 198         }
 199 
 200         PKCS8Key decodedKey = PKCS8Key.parse(new DerValue(encodedKey));
 201         String alg = decodedKey.getAlgorithm();
 202         AlgorithmId algId = decodedKey.getAlgorithmId();
 203         out.println("Algorithm :" + alg);
 204         out.println("AlgorithmId: " + algId);
 205 
 206         if (!ALGORITHM.equals(alg)) {
 207             raiseException(ALGORITHM, alg);
 208         }
 209         if (!EXPECTED_ALG_ID_CHRS.equalsIgnoreCase(algId.toString())) {
 210             raiseException(EXPECTED_ALG_ID_CHRS, algId.toString());
 211         }
 212 
 213         decodedKey.encode(derOutput);
 214         dumpByteArray("Stream encode: ", derOutput.toByteArray());
 215         if (!Arrays.equals(derOutput.toByteArray(), expectedBytes)) {
 216             raiseException(new String(expectedBytes), derOutput.toString());
 217         }
 218 
 219         dumpByteArray("byte[] encoding: ", decodedKey.getEncoded());
 220         if (!Arrays.equals(decodedKey.getEncoded(), expectedBytes)) {
 221             raiseException(new String(expectedBytes),
 222                     new String(decodedKey.getEncoded()));
 223         }
 224 
 225         if (!FORMAT.equals(decodedKey.getFormat())) {
 226             raiseException(FORMAT, decodedKey.getFormat());
 227         }
 228 
 229         try {
 230             byte[] newEncodedKey = new byte[NEW_ENCODED_KEY_INTS.length];
 231             for (int i = 0; i < newEncodedKey.length; i++) {
 232                 newEncodedKey[i] = (byte) NEW_ENCODED_KEY_INTS[i];
 233             }
 234             PKCS8Key newDecodedKey = PKCS8Key
 235                     .parse(new DerValue(newEncodedKey));
 236 
 237             throw new RuntimeException(
 238                     "key1: Expected an IOException during " + "parsing");
 239         } catch (IOException e) {
 240             System.out.println("newEncodedKey: should have excess data due to "
 241                     + "attributes, which are not supported");
 242         }
 243 
 244         try {
 245             byte[] newEncodedKey2 = new byte[NEW_ENCODED_KEY_INTS_2.length];
 246             for (int i = 0; i < newEncodedKey2.length; i++) {
 247                 newEncodedKey2[i] = (byte) NEW_ENCODED_KEY_INTS_2[i];
 248             }
 249 
 250             PKCS8Key newDecodedKey2 = PKCS8Key
 251                     .parse(new DerValue(newEncodedKey2));
 252 
 253             throw new RuntimeException(
 254                     "key2: Expected an IOException during " + "parsing");
 255         } catch (IOException e) {
 256             out.println("Key 2: should be illegal version");
 257             out.println(e.getMessage());
 258             if (!EXCEPTION_MESSAGE.equals(e.getMessage())) {
 259                 throw new RuntimeException("Key2: expected: "
 260                         + EXCEPTION_MESSAGE + " get: " + e.getMessage());
 261             }
 262         }
 263 
 264     }
 265 
 266     // get a byte array from somewhere
 267     static byte[] getEncodedKey() throws InvalidKeyException {
 268         BigInteger p = BigInteger.valueOf(1);
 269         BigInteger q = BigInteger.valueOf(2);
 270         BigInteger g = BigInteger.valueOf(3);
 271         BigInteger x = BigInteger.valueOf(4);
 272 
 273         DSAPrivateKey priv = new DSAPrivateKey(p, q, g, x);
 274         return priv.getEncoded();
 275     }
 276 
 277     static void dumpByteArray(String nm, byte[] bytes) throws IOException {
 278         out.println(nm + " length: " + bytes.length);
 279         hexDump.encodeBuffer(bytes, out);
 280     }
 281 
 282     static String toString(PKCS8Key key) {
 283         StringBuilder builder = new StringBuilder(key.getAlgorithm());
 284         builder.append('\n').append("parameters:")
 285                 .append(key.getAlgorithmId().toString());
 286         return builder.toString();
 287     }
 288 
 289 }