jdk/test/sun/security/rsa/TestKeyFactory.java
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 5506 202f599c92aa
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
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 4853305
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class TestKeyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private final static String BASE = System.getProperty("test.src", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static final char[] password = "test12".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static KeyStore getKeyStore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        ks.load(in, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Test that key1 (reference key) and key2 (key to be tested) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static void testKey(Key key1, Key key2) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (key2.getAlgorithm().equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            throw new Exception("Algorithm not RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (key1 instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            if (key2.getFormat().equals("X.509") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                throw new Exception("Format not X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        } else if (key1 instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (key2.getFormat().equals("PKCS#8") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                throw new Exception("Format not PKCS#8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (key1.equals(key2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throw new Exception("Keys not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new Exception("Encodings not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.out.println("Testing public key...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        PublicKey key2 = (PublicKey)kf.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        KeySpec rsaSpec = kf.getKeySpec(key, RSAPublicKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        PublicKey key3 = kf.generatePublic(rsaSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        PublicKey key4 = kf.generatePublic(x509Spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        PublicKey key5 = kf.generatePublic(x509Spec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        testKey(key, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        testKey(key, key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        testKey(key, key3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        testKey(key, key4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        testKey(key, key5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        System.out.println("Testing private key...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        PrivateKey key2 = (PrivateKey)kf.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        KeySpec rsaSpec = kf.getKeySpec(key, RSAPrivateCrtKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        PrivateKey key3 = kf.generatePrivate(rsaSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        PrivateKey key4 = kf.generatePrivate(pkcs8Spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        testKey(key, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        testKey(key, key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        testKey(key, key3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        testKey(key, key4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        testKey(key, key5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        PrivateKey key6 = kf.generatePrivate(rsaSpec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        RSAPrivateKey rsaKey = (RSAPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        KeySpec rsaSpec3 = new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        PrivateKey key7 = kf.generatePrivate(rsaSpec3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        testKey(key6, key6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        testKey(key6, key7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static void test(KeyFactory kf, Key key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (key.getAlgorithm().equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            System.out.println("Not an RSA key, ignoring");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (key instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            testPublic(kf, (PublicKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } else if (key instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            testPrivate(kf, (PrivateKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        KeyStore ks = getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            String alias = (String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            Key key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (ks.isKeyEntry(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                test(kf, ks.getKey(alias, password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                test(kf, ks.getCertificate(alias).getPublicKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        long stop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        System.out.println("All tests passed (" + (stop - start) + " ms).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}