jdk/test/sun/security/pkcs11/ec/TestKeyFactory.java
author serb
Tue, 12 Nov 2013 20:24:25 +0400
changeset 21596 0e3a39f29dbc
parent 19067 5271291b7121
child 35379 1e8e336ef66b
permissions -rw-r--r--
8027696: Incorrect copyright header in the tests Reviewed-by: alanb, malenkov, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 19067
diff changeset
     2
 * Copyright (c) 2006, 2013, 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 6405536
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test the P11ECKeyFactory
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * Test that key1 (reference key) and key2 (key to be tested) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static void testKey(Key key1, Key key2) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        if (key2.getAlgorithm().equals("EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            throw new Exception("Algorithm not EC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (key1 instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            if (key2.getFormat().equals("X.509") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                throw new Exception("Format not X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        } else if (key1 instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            if (key2.getFormat().equals("PKCS#8") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                throw new Exception("Format not PKCS#8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (key1.equals(key2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            System.out.println("key1: " + key1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            System.out.println("key2: " + key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            System.out.println("enc1: " + toString(key1.getEncoded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            System.out.println("enc2: " + toString(key2.getEncoded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            throw new Exception("Keys not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            throw new Exception("Encodings not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        System.out.println("Testing public key...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        PublicKey key2 = (PublicKey)kf.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        KeySpec keySpec = kf.getKeySpec(key, ECPublicKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        PublicKey key3 = kf.generatePublic(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        PublicKey key4 = kf.generatePublic(x509Spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        PublicKey key5 = kf.generatePublic(x509Spec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        testKey(key, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        testKey(key, key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        testKey(key, key3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        testKey(key, key4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        testKey(key, key5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        System.out.println("Testing private key...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        PrivateKey key2 = (PrivateKey)kf.translateKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        KeySpec keySpec = kf.getKeySpec(key, ECPrivateKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        PrivateKey key3 = kf.generatePrivate(keySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        PrivateKey key4 = kf.generatePrivate(pkcs8Spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        testKey(key, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        testKey(key, key2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        testKey(key, key3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        testKey(key, key4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        testKey(key, key5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static void test(KeyFactory kf, Key key) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (key.getAlgorithm().equals("EC") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new Exception("Not an EC key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (key instanceof PublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            testPublic(kf, (PublicKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } else if (key instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            testPrivate(kf, (PrivateKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        main(new TestKeyFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (p.getService("KeyFactory", "EC") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            System.out.println("Provider does not support EC, skipping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   122
        int[] keyLengths = {192, 163, 409, 521};
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   123
        int len = 0;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   124
        if (getNSSECC() == ECCState.Basic) {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   125
            System.out.println("NSS Basic ECC only. Skipping 192, 163, & 409");
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   126
            len = 3;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   127
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        KeyFactory kf = KeyFactory.getInstance("EC", p);
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   129
        for (; keyLengths.length > len ; len++) {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   130
            System.out.println("Length "+keyLengths[len]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 5506
diff changeset
   132
            kpg.initialize(keyLengths[len]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            KeyPair kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            test(kf, kp.getPrivate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            test(kf, kp.getPublic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
}