test/jdk/sun/security/mscapi/PublicKeyInterop.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 49548 8bdf2b5f472d
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9505
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
     1
/*
49548
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
9505
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
     4
 *
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
     8
 *
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    13
 * accompanied this code).
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    14
 *
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    18
 *
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    21
 * questions.
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    22
 */
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    23
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    24
/**
49548
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    25
 * @test
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    26
 * @bug 6888925 8180570
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    27
 * @summary SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    28
 * @requires os.family == "windows"
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    29
 * @library /test/lib
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    30
 * @modules java.base/sun.security.util
9505
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    31
 */
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    32
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    33
import java.security.*;
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    34
import java.util.*;
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    35
import javax.crypto.*;
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    36
49548
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    37
import jdk.test.lib.SecurityTools;
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 33868
diff changeset
    38
import sun.security.util.HexDumpEncoder;
9505
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    39
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    40
/*
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    41
 * Confirm interoperability of RSA public keys between SunMSCAPI and SunJCE
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    42
 * security providers.
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    43
 */
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    44
public class PublicKeyInterop {
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    45
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    46
    public static void main(String[] arg) throws Exception {
49548
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    47
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    48
        SecurityTools.keytool("-genkeypair",
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    49
                "-storetype", "Windows-My",
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    50
                "-keyalg", "RSA",
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    51
                "-alias", "6888925",
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    52
                "-dname", "cn=6888925,c=US",
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    53
                "-noprompt").shouldHaveExitValue(0);
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    54
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    55
        try {
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    56
            run();
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    57
        } finally {
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    58
            KeyStore ks = KeyStore.getInstance("Windows-MY");
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    59
            ks.load(null, null);
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    60
            ks.deleteEntry("6888925");
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    61
            ks.store(null, null);
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    62
        }
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    63
    }
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    64
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    65
    static void run() throws Exception {
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    66
9505
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    67
        KeyStore ks = KeyStore.getInstance("Windows-MY");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    68
        ks.load(null, null);
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    69
        System.out.println("Loaded keystore: Windows-MY");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    70
49548
8bdf2b5f472d 8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents: 47216
diff changeset
    71
        PublicKey myPuKey = ks.getCertificate("6888925").getPublicKey();
9505
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    72
        System.out.println("Public key is a " + myPuKey.getClass().getName());
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    73
        PrivateKey myPrKey = (PrivateKey) ks.getKey("6888925", null);
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    74
        System.out.println("Private key is a " + myPrKey.getClass().getName());
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    75
        System.out.println();
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    76
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    77
        byte[] plain = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05};
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    78
        HexDumpEncoder hde = new HexDumpEncoder();
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    79
        System.out.println("Plaintext:\n" + hde.encode(plain) + "\n");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    80
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    81
        Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    82
        rsa.init(Cipher.ENCRYPT_MODE, myPuKey);
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    83
        byte[] encrypted = rsa.doFinal(plain);
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    84
        System.out.println("Encrypted plaintext using RSA Cipher from " +
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    85
            rsa.getProvider().getName() + " JCE provider\n");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    86
        System.out.println(hde.encode(encrypted) + "\n");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    87
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    88
        Cipher rsa2 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    89
        rsa2.init(Cipher.ENCRYPT_MODE, myPuKey);
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    90
        byte[] encrypted2 = rsa2.doFinal(plain);
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    91
        System.out.println("Encrypted plaintext using RSA Cipher from " +
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    92
            rsa2.getProvider().getName() + " JCE provider\n");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    93
        System.out.println(hde.encode(encrypted2) + "\n");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    94
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    95
        Cipher rsa3 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunMSCAPI");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    96
        rsa3.init(Cipher.DECRYPT_MODE, myPrKey);
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    97
        byte[] decrypted = rsa3.doFinal(encrypted);
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    98
        System.out.println("Decrypted first ciphertext using RSA Cipher from " +
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
    99
            rsa3.getProvider().getName() + " JCE provider\n");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   100
        System.out.println(hde.encode(decrypted) + "\n");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   101
        if (! Arrays.equals(plain, decrypted)) {
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   102
            throw new Exception("First decrypted ciphertext does not match " +
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   103
                "original plaintext");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   104
        }
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   105
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   106
        decrypted = rsa3.doFinal(encrypted2);
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   107
        System.out.println("Decrypted second ciphertext using RSA Cipher from "
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   108
            + rsa3.getProvider().getName() + " JCE provider\n");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   109
        System.out.println(hde.encode(decrypted) + "\n");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   110
        if (! Arrays.equals(plain, decrypted)) {
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   111
            throw new Exception("Second decrypted ciphertext does not match " +
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   112
                "original plaintext");
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   113
        }
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   114
    }
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents:
diff changeset
   115
}