jdk/test/sun/security/pkcs11/rsa/GenKeyStore.java
author skovalev
Thu, 15 Sep 2016 13:03:03 +0300
changeset 40975 680639c9b307
parent 14421 a64b2cc9d429
permissions -rw-r--r--
8165689: Fix module dependencies for sun/security/pkcs11/* tests Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 5506
diff changeset
     2
 * Copyright (c) 2012, 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
// program to generate rsakeys.ks. does not need to run during testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
// checked into the workspace so that the keystore file can be recreated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
// in the future if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
// @author Andreas Sterbenz
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.math.BigInteger;
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.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class GenKeyStore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static final char[] password = "test12".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static X509Certificate getCertificate(String suffix, PublicKey publicKey, PrivateKey privateKey) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        X500Name name = new X500Name("CN=Dummy Certificate " + suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        String algorithm = "SHA1with" + publicKey.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        Date date = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        AlgorithmId algID = AlgorithmId.getAlgorithmId(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        X509CertInfo certInfo = new X509CertInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        certInfo.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        certInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        certInfo.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algID));
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 5506
diff changeset
    57
        certInfo.set(X509CertInfo.SUBJECT, name);
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 5506
diff changeset
    58
        certInfo.set(X509CertInfo.ISSUER, name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        certInfo.set(X509CertInfo.KEY, new CertificateX509Key(publicKey));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        certInfo.set(X509CertInfo.VALIDITY, new CertificateValidity(date, date));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        X509CertImpl cert = new X509CertImpl(certInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        cert.sign(privateKey, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static void addToKeyStore(KeyStore ks, KeyPair kp, String name) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        PublicKey pubKey = kp.getPublic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        PrivateKey privKey = kp.getPrivate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        X509Certificate cert = getCertificate(name, pubKey, privKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        ks.setKeyEntry(name, privKey, password, new X509Certificate[] {cert});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static void generateKeyPair(KeyStore ks, int keyLength, String alias) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.out.println("Generating " + keyLength + " keypair " + alias + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        kpg.initialize(keyLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        KeyPair kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        addToKeyStore(ks, kp, alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        ks.load(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        generateKeyPair(ks, 512, "rsa512a");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        generateKeyPair(ks, 512, "rsa512b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        generateKeyPair(ks, 1024, "rsa1024a");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        generateKeyPair(ks, 1024, "rsa1024b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        generateKeyPair(ks, 2048, "rsa2048a");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        generateKeyPair(ks, 2048, "rsa2048b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        generateKeyPair(ks, 4096, "rsa4096a");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // only one 4096 bit keys and none longer than that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // that would slow down the other tests too much
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // on old machines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
//      generateKeyPair(ks, 4096, "rsa4096b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
//      generateKeyPair(ks, 8192, "rsa8192a");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
//      generateKeyPair(ks, 8192, "rsa8192b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        OutputStream out = new FileOutputStream("rsakeys.ks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        ks.store(out, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        long stop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.out.println("Done (" + (stop - start) + " ms).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}