blob: cfc70f7615b43b4cbd65d5123d2e7932717e42ee [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 6316539
27 * @summary Basic tests for TlsRsaPremasterSecret generator
28 * @author Andreas Sterbenz
29 * @library ..
30 */
31
32import java.security.Security;
33import java.security.Provider;
34
35import javax.crypto.KeyGenerator;
36import javax.crypto.SecretKey;
37
38import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;
39
40public class TestPremaster extends PKCS11Test {
41
42 public static void main(String[] args) throws Exception {
43 main(new TestPremaster());
44 }
45
46 public void main(Provider provider) throws Exception {
47 if (provider.getService("KeyGenerator", "SunTlsRsaPremasterSecret") == null) {
48 System.out.println("Not supported by provider, skipping");
49 return;
50 }
51 KeyGenerator kg;
52 kg = KeyGenerator.getInstance("SunTlsRsaPremasterSecret", provider);
53
54 try {
55 kg.generateKey();
56 throw new Exception("no exception");
57 } catch (IllegalStateException e) {
58 System.out.println("OK: " + e);
59 }
60
61 test(kg, 3, 0);
62 test(kg, 3, 1);
63 test(kg, 3, 2);
64 test(kg, 4, 0);
65
66 System.out.println("Done.");
67 }
68
69 private static void test(KeyGenerator kg, int major, int minor) throws Exception {
70
71 kg.init(new TlsRsaPremasterSecretParameterSpec(major, minor));
72 SecretKey key = kg.generateKey();
73 byte[] encoded = key.getEncoded();
74 if (encoded.length != 48) {
75 throw new Exception("length: " + encoded.length);
76 }
77 if ((encoded[0] != major) || (encoded[1] != minor)) {
78 throw new Exception("version mismatch: " + encoded[0] + "." + encoded[1]);
79 }
80 System.out.println("OK: " + major + "." + minor);
81 }
82}