Path: blob/master/test/jdk/sun/security/pkcs11/rsa/TestKeyFactory.java
41154 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4856966 802398026* @summary Test KeyFactory of the new RSA provider27* @author Andreas Sterbenz28* @library /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm TestKeyFactory31* @run main/othervm -Djava.security.manager=allow TestKeyFactory sm rsakeys.ks.policy32*/3334import java.io.*;35import java.util.*;3637import java.security.*;38import java.security.spec.*;3940public class TestKeyFactory extends PKCS11Test {4142private static final char[] password = "test12".toCharArray();4344private static final String PKCS1_PRIV_STR =45// the BASE64 string between -----BEGIN RSA PRIVATE KEY-----46// and -----END RSA PRIVATE KEY-----47"MIIEowIBAAKCAQEA0OIArlYES4X1XMTLDordtN/XIWFE1wvhl40RsHWM2n99+Stp" +48"CCJCcUb5FJ2/kefj/XRwB6p5IMpIZrHZqC8XXzlX5fpiFaSu2xnk17oWUKoErW27" +49"Stm098pU2RoUxWPKVl+42a8iVp8tijNElBNFALCGi0zXOhcTxMh0q1Wk0UhMJqam" +50"v5YnCKmT4THwwGYn/KeK3M7Qa+o5MoVBHLbeT9LJgEmSluVzIh44Lh6weX0bw72P" +51"8X2praOhbzg2B343MqS/rMLw6On+0i7ccEgp23vX9G5w85q4A5FSIrk4S/pyv5sO" +52"rwjCQKBW1TS0/2iB9zNkFMj5/+h7l2oqTT7sSQIDAQABAoIBADn6sXOynoiUC1IP" +53"sck8lGOTSjSSujfyrVCSsJlJV6qCfuX9va6rS8QDjjnBu531PtxoSHxoPizy2Pvg" +54"W+kKATPGR/am9DjLuFlKq7GRjoYfWyMEdVtGaKvq9ng4fBF6LHyjHz0VFrPyhQJ6" +55"TovHeXzCguYBkzAlnbAeb/vqzs/kABbOuSHVi7DsaixCoEX9zOptFYQw/l8rh68+" +56"UF2bpNNH3jOC1uN3vZtuSwCupqtN+2Mpkx2h04Rk75vWIhrnPeMgmcd3yP4LNZMR" +57"mfaynb63RRzVkNis7+NVk016SQ1oL79mrBvy5rBg3HeCeArwvqZAmOaWsLSWHzCy" +58"zlVlMTECgYEA6JlnMpC956Qi8HX5ye4Hu2ovBdbNGtH/TMkZmColJz9P7CvNkNIb" +59"Od6mvLMydbPHkhdBUDWD4rhiCKHrf5zKju1i24YqWcvuSGotWj4/KQ3+87mLZM+7" +60"daBsJBmSEVB80sgA9ItqSgOyNoNFpiDgFnlszAfb0n9XXEzB/pwSw1UCgYEA5eXI" +61"d+eKugugP+n6CluQfyxfN6WWCzfqWToCTTxPn2i12AiEssXy+kyLjupJVLWSivdo" +62"83wD5LuxFRGc9P+aKQERPhb0AFaxf1llUCXla65/x2So5xjMvtuzgQ0OktPJqJXq" +63"hYGunctsr5rje33+7vlx4xWkrL2PrQWzJabn7SUCgYEAqw3FesY/Ik7u8u+P1xSZ" +64"0xXvptek1oiAu7NYgzLbR9WjrQc5kbsyEojPDg6qmSyxI5q+iYIRj3YRgk+xpJNl" +65"0154SQCNvKPghJiw6aDFSifkytA01tp9/a8QWCwF433RjiFPsoekjvHQ6Y34dofO" +66"xDhf7lwJKPBFCrfYIqocklECgYAIPI9OHHGP8NKw94UJ0fX/WGug5sHVbQ9sWvOy" +67"KLMBlxLMxqFadlUaOpvVZvdxnX++ktajwpGxJDhX9OWWsYGobm1buB7N1E1Prrg+" +68"gt0RWpMhZa3Xeb/8Jorr2Lfo8sWK0LQyTE8hQCSIthfoWL9FeJJn/GKF/dSj8kxU" +69"0QIGMQKBgG/8U/zZ87DzfXS81P1p+CmH474wmou4KD2/zXp/lDR9+dlIUeijlIbU" +70"P6Y5xJvT33Y40giW9irShgDHjZgw0ap11K3b2HzLImdPEaBiENo735rpLs8WLK9H" +71"+yeRbiP2y9To7sTihm9Jrkctzp6sqFtKyye1+S21X1tMz8NGfXen";7273private static final String PKCS1_PUB_STR =74// the BASE64 string between -----BEGIN RSA PUBLIC KEY-----75// and -----END RSA PUBLIC KEY-----76"MIIBCgKCAQEA0OIArlYES4X1XMTLDordtN/XIWFE1wvhl40RsHWM2n99+StpCCJC" +77"cUb5FJ2/kefj/XRwB6p5IMpIZrHZqC8XXzlX5fpiFaSu2xnk17oWUKoErW27Stm0" +78"98pU2RoUxWPKVl+42a8iVp8tijNElBNFALCGi0zXOhcTxMh0q1Wk0UhMJqamv5Yn" +79"CKmT4THwwGYn/KeK3M7Qa+o5MoVBHLbeT9LJgEmSluVzIh44Lh6weX0bw72P8X2p" +80"raOhbzg2B343MqS/rMLw6On+0i7ccEgp23vX9G5w85q4A5FSIrk4S/pyv5sOrwjC" +81"QKBW1TS0/2iB9zNkFMj5/+h7l2oqTT7sSQIDAQAB";828384private static final PrivateKey CUSTOM_PRIV;85private static final PublicKey CUSTOM_PUB;8687static {88byte[] encodedPriv = Base64.getDecoder().decode(PKCS1_PRIV_STR);89CUSTOM_PRIV = new PrivateKey() {90@Override91public String getAlgorithm() {92return "RSA";93}94@Override95public String getFormat() {96return "PKCS#1";97}98@Override99public byte[] getEncoded() {100return encodedPriv.clone();101}102};103byte[] encodedPub = Base64.getDecoder().decode(PKCS1_PUB_STR);104CUSTOM_PUB = new PublicKey() {105@Override106public String getAlgorithm() {107return "RSA";108}109@Override110public String getFormat() {111return "PKCS#1";112}113@Override114public byte[] getEncoded() {115return encodedPub.clone();116}117};118}119120static KeyStore getKeyStore() throws Exception {121KeyStore ks;122try (InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"))) {123ks = KeyStore.getInstance("JKS");124ks.load(in, password);125}126return ks;127}128129/**130* Test that key1 (reference key) and key2 (key to be tested) are131* equivalent132*/133private static void testKey(Key key1, Key key2) throws Exception {134if (key2.getAlgorithm().equals("RSA") == false) {135throw new Exception("Algorithm not RSA");136}137if (key1 instanceof PublicKey) {138if (key2.getFormat().equals("X.509") == false) {139throw new Exception("Format not X.509");140}141} else if (key1 instanceof PrivateKey) {142if (key2.getFormat().equals("PKCS#8") == false) {143throw new Exception("Format not PKCS#8");144}145}146// skip equals check when key1 is custom key147if (key1 != CUSTOM_PRIV && key1 != CUSTOM_PUB) {148if (!key1.equals(key2)) {149throw new Exception("Keys not equal");150}151}152// only compare encodings if keys are of the same format153if (key1.getFormat().equals(key2.getFormat()) &&154!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {155throw new Exception("Encodings not equal");156}157}158159private static void testPublic(KeyFactory kf, PublicKey key)160throws Exception {161System.out.println("Testing " + (key == CUSTOM_PUB? "PKCS#1" : "") +162" public key...");163PublicKey key2 = (PublicKey)kf.translateKey(key);164KeySpec rsaSpec = kf.getKeySpec(key, RSAPublicKeySpec.class);165PublicKey key3 = kf.generatePublic(rsaSpec);166KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);167PublicKey key4 = kf.generatePublic(x509Spec);168if (key != CUSTOM_PUB) {169testKey(key, key);170}171testKey(key, key2);172testKey(key, key3);173testKey(key, key4);174175if (key.getFormat().equalsIgnoreCase("X.509")) {176KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());177PublicKey key5 = kf.generatePublic(x509Spec2);178testKey(key, key5);179}180181}182183private static void testPrivate(KeyFactory kf, PrivateKey key)184throws Exception {185System.out.println("Testing " + (key == CUSTOM_PRIV? "PKCS#1" : "") +186" private key...");187PrivateKey key2 = (PrivateKey)kf.translateKey(key);188KeySpec rsaSpec = kf.getKeySpec(key, RSAPrivateCrtKeySpec.class);189PrivateKey key3 = kf.generatePrivate(rsaSpec);190KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);191PrivateKey key4 = kf.generatePrivate(pkcs8Spec);192if (key != CUSTOM_PRIV) {193testKey(key, key);194}195testKey(key, key2);196testKey(key, key3);197testKey(key, key4);198199if (key.getFormat().equalsIgnoreCase("PKCS#8")) {200KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());201PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);202testKey(key, key5);203}204205// XXX PKCS#11 providers may not support non-CRT keys (e.g. NSS)206// KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class);207// PrivateKey key6 = kf.generatePrivate(rsaSpec2);208// RSAPrivateKey rsaKey = (RSAPrivateKey)key;209// KeySpec rsaSpec3 = new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());210// PrivateKey key7 = kf.generatePrivate(rsaSpec3);211// testKey(key6, key6);212// testKey(key6, key7);213}214215private static void test(KeyFactory kf, Key key) throws Exception {216if (key.getAlgorithm().equals("RSA") == false) {217System.out.println("Not an RSA key, ignoring");218}219if (key instanceof PublicKey) {220testPublic(kf, (PublicKey)key);221} else if (key instanceof PrivateKey) {222testPrivate(kf, (PrivateKey)key);223}224}225226public static void main(String[] args) throws Exception {227main(new TestKeyFactory(), args);228}229230@Override231public void main(Provider p) throws Exception {232long start = System.currentTimeMillis();233KeyStore ks = getKeyStore();234KeyFactory kf = KeyFactory.getInstance("RSA", p);235for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {236String alias = (String)e.nextElement();237Key key = null;238if (ks.isKeyEntry(alias)) {239test(kf, ks.getKey(alias, password));240test(kf, ks.getCertificate(alias).getPublicKey());241}242}243// repeat the test w/ PKCS#1 RSA Private Key244test(kf, CUSTOM_PRIV);245test(kf, CUSTOM_PUB);246247long stop = System.currentTimeMillis();248System.out.println("All tests passed (" + (stop - start) + " ms).");249}250}251252253