TestKeyFactory.java revision 8729:0242fce0f717
1266692Sgshapiro/*
264562Sgshapiro * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
364562Sgshapiro * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4168515Sgshapiro *
564562Sgshapiro * This code is free software; you can redistribute it and/or modify it
664562Sgshapiro * under the terms of the GNU General Public License version 2 only, as
764562Sgshapiro * published by the Free Software Foundation.
864562Sgshapiro *
964562Sgshapiro * This code is distributed in the hope that it will be useful, but WITHOUT
1064562Sgshapiro * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1190792Sgshapiro * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1264562Sgshapiro * version 2 for more details (a copy is included in the LICENSE file that
1364562Sgshapiro * accompanied this code).
1464562Sgshapiro *
1564562Sgshapiro * 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 6405536
27 * @summary Test the P11ECKeyFactory
28 * @author Andreas Sterbenz
29 * @library ..
30 */
31
32import java.io.*;
33import java.util.*;
34
35import java.security.*;
36import java.security.interfaces.*;
37import java.security.spec.*;
38
39public class TestKeyFactory extends PKCS11Test {
40
41    /**
42     * Test that key1 (reference key) and key2 (key to be tested) are
43     * equivalent
44     */
45    private static void testKey(Key key1, Key key2) throws Exception {
46        if (key2.getAlgorithm().equals("EC") == false) {
47            throw new Exception("Algorithm not EC");
48        }
49        if (key1 instanceof PublicKey) {
50            if (key2.getFormat().equals("X.509") == false) {
51                throw new Exception("Format not X.509");
52            }
53        } else if (key1 instanceof PrivateKey) {
54            if (key2.getFormat().equals("PKCS#8") == false) {
55                throw new Exception("Format not PKCS#8");
56            }
57        }
58        if (key1.equals(key2) == false) {
59            System.out.println("key1: " + key1);
60            System.out.println("key2: " + key2);
61            System.out.println("enc1: " + toString(key1.getEncoded()));
62            System.out.println("enc2: " + toString(key2.getEncoded()));
63            throw new Exception("Keys not equal");
64        }
65        if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {
66            throw new Exception("Encodings not equal");
67        }
68    }
69
70    private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {
71        System.out.println("Testing public key...");
72        PublicKey key2 = (PublicKey)kf.translateKey(key);
73        KeySpec keySpec = kf.getKeySpec(key, ECPublicKeySpec.class);
74        PublicKey key3 = kf.generatePublic(keySpec);
75        KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
76        PublicKey key4 = kf.generatePublic(x509Spec);
77        KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());
78        PublicKey key5 = kf.generatePublic(x509Spec2);
79        testKey(key, key);
80        testKey(key, key2);
81        testKey(key, key3);
82        testKey(key, key4);
83        testKey(key, key5);
84    }
85
86    private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {
87        System.out.println("Testing private key...");
88        PrivateKey key2 = (PrivateKey)kf.translateKey(key);
89        KeySpec keySpec = kf.getKeySpec(key, ECPrivateKeySpec.class);
90        PrivateKey key3 = kf.generatePrivate(keySpec);
91        KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);
92        PrivateKey key4 = kf.generatePrivate(pkcs8Spec);
93        KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());
94        PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);
95        testKey(key, key);
96        testKey(key, key2);
97        testKey(key, key3);
98        testKey(key, key4);
99        testKey(key, key5);
100    }
101
102    private static void test(KeyFactory kf, Key key) throws Exception {
103        if (key.getAlgorithm().equals("EC") == false) {
104            throw new Exception("Not an EC key");
105        }
106        if (key instanceof PublicKey) {
107            testPublic(kf, (PublicKey)key);
108        } else if (key instanceof PrivateKey) {
109            testPrivate(kf, (PrivateKey)key);
110        }
111    }
112
113    public static void main(String[] args) throws Exception {
114        main(new TestKeyFactory());
115    }
116
117    public void main(Provider p) throws Exception {
118        if (p.getService("KeyFactory", "EC") == null) {
119            System.out.println("Provider does not support EC, skipping");
120            return;
121        }
122        int[] keyLengths = {192, 163, 409, 521};
123        int len = 0;
124        if (getNSSECC() == ECCState.Basic) {
125            System.out.println("NSS Basic ECC only. Skipping 192, 163, & 409");
126            len = 3;
127        }
128        KeyFactory kf = KeyFactory.getInstance("EC", p);
129        for (; keyLengths.length > len ; len++) {
130            System.out.println("Length "+keyLengths[len]);
131            KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
132            kpg.initialize(keyLengths[len]);
133            KeyPair kp = kpg.generateKeyPair();
134            test(kf, kp.getPrivate());
135            test(kf, kp.getPublic());
136        }
137    }
138}
139