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,
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)) {
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 }
|
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 * @requires (os.family != "solaris")
29 * @compile -XDignore.symbol.file PKCS8Test.java
30 * @run main PKCS8Test
31 */
32
33 /*
34 * Skip Solaris since the DSAPrivateKeys returned by
35 * SunPKCS11 Provider are not subclasses of PKCS8Key
36 */
37 import java.io.IOException;
38 import java.math.BigInteger;
39 import java.security.InvalidKeyException;
40 import java.util.Arrays;
41 import sun.misc.HexDumpEncoder;
42 import sun.security.pkcs.PKCS8Key;
43 import sun.security.provider.DSAPrivateKey;
44 import sun.security.util.DerOutputStream;
45 import sun.security.util.DerValue;
46 import sun.security.x509.AlgorithmId;
47 import static java.lang.System.out;
48
49 public class PKCS8Test {
50
51 static final HexDumpEncoder hexDump = new HexDumpEncoder();
52
53 static final DerOutputStream derOutput = new DerOutputStream();
54
55 static final String FORMAT = "PKCS#8";
56 static final String EXPECTED_ALG_ID_CHRS = "DSA\n\tp: 02\n\tq: 03\n"
57 + "\tg: 04\n";
58 static final String ALGORITHM = "DSA";
59 static final String EXCEPTION_MESSAGE = "version mismatch: (supported: "
60 + "00, parsed: 01";
61
62 // test second branch in byte[] encode()
63 // DER encoding,include (empty) set of attributes
64 static final int[] NEW_ENCODED_KEY_INTS = { 0x30,
65 // length 30 = 0x1e
66 0x1e,
174 0x01,
175 // length
176 0x04,
177 // privateKey contents
178 0x04, 0x03, 0x02,
179 // 4th (optional) element -- attributes [0] IMPLICIT Attributes
180 // OPTIONAL
181 // (Attributes = SET OF Attribute) Here, it will be empty.
182 0x01,
183 // length
184 0x01 };
185
186 static void raiseException(String expected, String received) {
187 throw new RuntimeException(
188 "Expected " + expected + "; Received " + received);
189 }
190
191 public static void main(String[] args)
192 throws IOException, InvalidKeyException {
193
194 BigInteger p = BigInteger.valueOf(1);
195 BigInteger q = BigInteger.valueOf(2);
196 BigInteger g = BigInteger.valueOf(3);
197 BigInteger x = BigInteger.valueOf(4);
198
199 DSAPrivateKey priv = new DSAPrivateKey(p, q, g, x);
200
201 byte[] encodedKey = priv.getEncoded();
202 byte[] expectedBytes = new byte[EXPECTED.length];
203 for (int i = 0; i < EXPECTED.length; i++) {
204 expectedBytes[i] = (byte) EXPECTED[i];
205 }
206
207 dumpByteArray("encodedKey :", encodedKey);
208 if (!Arrays.equals(encodedKey, expectedBytes)) {
209 raiseException(new String(expectedBytes), new String(encodedKey));
210 }
211
212 PKCS8Key decodedKey = PKCS8Key.parse(new DerValue(encodedKey));
213
214 String alg = decodedKey.getAlgorithm();
215 AlgorithmId algId = decodedKey.getAlgorithmId();
216 out.println("Algorithm :" + alg);
217 out.println("AlgorithmId: " + algId);
218
219 if (!ALGORITHM.equals(alg)) {
220 raiseException(ALGORITHM, alg);
221 }
222 if (!EXPECTED_ALG_ID_CHRS.equalsIgnoreCase(algId.toString())) {
223 raiseException(EXPECTED_ALG_ID_CHRS, algId.toString());
224 }
225
226 decodedKey.encode(derOutput);
227 dumpByteArray("Stream encode: ", derOutput.toByteArray());
228 if (!Arrays.equals(derOutput.toByteArray(), expectedBytes)) {
229 raiseException(new String(expectedBytes), derOutput.toString());
230 }
231
232 dumpByteArray("byte[] encoding: ", decodedKey.getEncoded());
233 if (!Arrays.equals(decodedKey.getEncoded(), expectedBytes)) {
256
257 try {
258 byte[] newEncodedKey2 = new byte[NEW_ENCODED_KEY_INTS_2.length];
259 for (int i = 0; i < newEncodedKey2.length; i++) {
260 newEncodedKey2[i] = (byte) NEW_ENCODED_KEY_INTS_2[i];
261 }
262
263 PKCS8Key newDecodedKey2 = PKCS8Key
264 .parse(new DerValue(newEncodedKey2));
265
266 throw new RuntimeException(
267 "key2: Expected an IOException during " + "parsing");
268 } catch (IOException e) {
269 out.println("Key 2: should be illegal version");
270 out.println(e.getMessage());
271 if (!EXCEPTION_MESSAGE.equals(e.getMessage())) {
272 throw new RuntimeException("Key2: expected: "
273 + EXCEPTION_MESSAGE + " get: " + e.getMessage());
274 }
275 }
276 }
277
278 static void dumpByteArray(String nm, byte[] bytes) throws IOException {
279 out.println(nm + " length: " + bytes.length);
280 hexDump.encodeBuffer(bytes, out);
281 }
282 }
|