1 /*
2  * Copyright (c) 2003, 2007, 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 4900891
27  * @summary Test the EncryptedPrivateKeyInfo.getKeySpec(...)
28  * methods with scenarios where the decrypted bytes are not
29  * encoded correctly per PKCS#8 standard.
30  * @author Valerie Peng
31  */
32 import java.util.*;
33 import java.nio.*;
34 import java.io.*;
35 import java.security.*;
36 import java.util.Arrays;
37 import java.security.spec.*;
38 import javax.crypto.*;
39 import javax.crypto.spec.*;
40 
41 public class GetKeySpecInvalidEncoding {
42     private static final String cipherAlg = "PBEWithMD5AndDES";
43     private static final char[] passwd = { 'p','a','s','s', 'w', 'd' };
44     private static AlgorithmParameters GOOD_PARAMS;
45 
46     static {
47         try {
48             PBEParameterSpec goodParamSpec =
49                 new PBEParameterSpec(new byte[8], 6);
50             GOOD_PARAMS = AlgorithmParameters.getInstance
51                 (cipherAlg, "SunJCE");
52             GOOD_PARAMS.init(goodParamSpec);
53         } catch (Exception ex) {
54             // should never happen
55             GOOD_PARAMS = null;
56         }
57     }
58 
59     private static String encryptedPKCS8 = "5C:BC:13:5D:40:2F:02:28:94:3C:A9:F7:98:A6:58:DC:F9:12:B7:CB:0F:40:DD:66:AB:58:6B:23:2F:8A:5A:81:9D:55:2A:EB:3F:AA:6A:CE:AE:23:8A:96:12:21:5A:09:BF:59:65:3F:B8:48:59:69:C6:D0:9C:48:B6:78:C3:C6:B4:24:F9:BE:10:00:D5:F3:52:88:53:CD:07:CA:88:93:15:3F:BA:19:4A:E9:5D:C7:44:46:49:F6:19:83:86:E0:25:51:9E:83:6D:AE:F2:14:9C:BF:02:7B:8C:64:B4:5F:F1:7B:28:2F:39:55:32:A4:F5:41:85:9E:77:F2:07:09:CD:97:90:5A:04:81:23:30";
60 
parse(String s)61     private static byte[] parse(String s) {
62         try {
63             int n = s.length();
64             ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
65             StringReader r = new StringReader(s);
66             while (true) {
67                 int b1 = nextNibble(r);
68                 if (b1 < 0) {
69                     break;
70                 }
71                 int b2 = nextNibble(r);
72                 if (b2 < 0) {
73                     throw new RuntimeException("Invalid string " + s);
74                 }
75                 int b = (b1 << 4) | b2;
76                 out.write(b);
77             }
78             return out.toByteArray();
79         } catch (IOException e) {
80             throw new RuntimeException(e);
81         }
82     }
nextNibble(StringReader r)83     private static int nextNibble(StringReader r) throws IOException {
84         while (true) {
85             int ch = r.read();
86             if (ch == -1) {
87                 return -1;
88             } else if ((ch >= '0') && (ch <= '9')) {
89                 return ch - '0';
90             } else if ((ch >= 'a') && (ch <= 'f')) {
91                 return ch - 'a' + 10;
92             } else if ((ch >= 'A') && (ch <= 'F')) {
93                 return ch - 'A' + 10;
94             }
95         }
96     }
97 
main(String[] argv)98     public static void main(String[] argv) throws Exception {
99         if (GOOD_PARAMS == null) {
100             throw new Exception("Static parameter generation failed");
101         }
102         byte[] encryptedData = parse(encryptedPKCS8);
103 
104         Provider p = Security.getProvider("SunJCE");
105 
106         // generate encrypted data and EncryptedPrivateKeyInfo object
107         EncryptedPrivateKeyInfo epki =
108             new EncryptedPrivateKeyInfo(GOOD_PARAMS, encryptedData);
109 
110         PKCS8EncodedKeySpec pkcs8Spec;
111         // TEST#1 getKeySpec(Cipher)
112         System.out.println("Testing getKeySpec(Cipher)...");
113         // Prepare Cipher for decryption
114         PBEKeySpec pbeKeySpec = new PBEKeySpec(passwd);
115         SecretKeyFactory skf = SecretKeyFactory.getInstance(cipherAlg, p);
116         SecretKey cipherKey = skf.generateSecret(pbeKeySpec);
117         Cipher cipher = Cipher.getInstance(cipherAlg, p);
118         cipher.init(Cipher.DECRYPT_MODE, cipherKey, GOOD_PARAMS);
119         try {
120             pkcs8Spec = epki.getKeySpec(cipher);
121             throw new Exception("getKeySpec(Cipher): should throw IKSE");
122         } catch (InvalidKeySpecException ikse) {
123             // expected
124         }
125         // TEST#2 getKeySpec(Key)
126         System.out.println("Testing getKeySpec(Key)...");
127         try {
128             pkcs8Spec = epki.getKeySpec(cipherKey);
129             throw new Exception("getKeySpec(Key): should throw IKE");
130         } catch (InvalidKeyException ike) {
131             // expected
132         }
133 
134         // TEST#3 getKeySpec(Key, String)
135         System.out.println("Testing getKeySpec(Key, String)...");
136         try {
137             pkcs8Spec = epki.getKeySpec(cipherKey, p.getName());
138             throw new Exception("getKeySpec(Key, String): should throw IKE");
139         } catch (InvalidKeyException ike) {
140             // expected
141         }
142 
143         // TEST#4 getKeySpec(Key, Provider)
144         System.out.println("Testing getKeySpec(Key, Provider)...");
145         try {
146             pkcs8Spec = epki.getKeySpec(cipherKey, p);
147             throw new Exception("getKeySpec(Key, Provider): should throw IKE");
148         } catch (InvalidKeyException ike) {
149             // expected
150         }
151         System.out.println("All Tests Passed");
152     }
153 }
154