jdk/test/sun/security/pkcs11/rsa/TestKeyFactory.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 35379 1e8e336ef66b
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4856966
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test KeyFactory of the new RSA provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class TestKeyFactory extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private final static String BASE = System.getProperty("test.src", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final char[] password = "test12".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static KeyStore getKeyStore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        ks.load(in, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Test that key1 (reference key) and key2 (key to be tested) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static void testKey(Key key1, Key key2) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (key2.getAlgorithm().equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            throw new Exception("Algorithm not RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (key1 instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            if (key2.getFormat().equals("X.509") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                throw new Exception("Format not X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        } else if (key1 instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            if (key2.getFormat().equals("PKCS#8") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                throw new Exception("Format not PKCS#8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (key1.equals(key2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new Exception("Keys not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new Exception("Encodings not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.out.println("Testing public key...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        PublicKey key2 = (PublicKey)kf.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        KeySpec rsaSpec = kf.getKeySpec(key, RSAPublicKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        PublicKey key3 = kf.generatePublic(rsaSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        PublicKey key4 = kf.generatePublic(x509Spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        PublicKey key5 = kf.generatePublic(x509Spec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        testKey(key, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        testKey(key, key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        testKey(key, key3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        testKey(key, key4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        testKey(key, key5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.out.println("Testing private key...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        PrivateKey key2 = (PrivateKey)kf.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        KeySpec rsaSpec = kf.getKeySpec(key, RSAPrivateCrtKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        PrivateKey key3 = kf.generatePrivate(rsaSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        PrivateKey key4 = kf.generatePrivate(pkcs8Spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        testKey(key, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        testKey(key, key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        testKey(key, key3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        testKey(key, key4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        testKey(key, key5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // XXX PKCS#11 providers may not support non-CRT keys (e.g. NSS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
//      KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
//      PrivateKey key6 = kf.generatePrivate(rsaSpec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
//      RSAPrivateKey rsaKey = (RSAPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
//      KeySpec rsaSpec3 = new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
//      PrivateKey key7 = kf.generatePrivate(rsaSpec3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
//      testKey(key6, key6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
//      testKey(key6, key7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private static void test(KeyFactory kf, Key key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (key.getAlgorithm().equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.out.println("Not an RSA key, ignoring");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (key instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            testPublic(kf, (PublicKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } else if (key instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            testPrivate(kf, (PrivateKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        main(new TestKeyFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        KeyStore ks = getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        KeyFactory kf = KeyFactory.getInstance("RSA", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            String alias = (String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            Key key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (ks.isKeyEntry(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                test(kf, ks.getKey(alias, password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                test(kf, ks.getCertificate(alias).getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        long stop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        System.out.println("All tests passed (" + (stop - start) + " ms).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}