jdk/test/sun/security/pkcs11/Secmod/LoadKeystore.java
author asmotrak
Tue, 26 Jan 2016 13:32:07 -0800
changeset 35379 1e8e336ef66b
parent 32635 d7e4ba3c2e0d
child 40975 680639c9b307
permissions -rw-r--r--
8144539: Update PKCS11 tests to run with security manager Reviewed-by: valeriep, ascarpino
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
     1
/*
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
     4
 *
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
     8
 *
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    13
 * accompanied this code).
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    14
 *
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    18
 *
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    21
 * questions.
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    22
 */
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    23
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    24
import java.io.File;
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    25
import java.io.IOException;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    26
import java.security.KeyStore;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    27
import java.security.KeyStoreException;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    28
import java.security.Provider;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    29
import java.security.Security;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    30
import java.security.UnrecoverableKeyException;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    31
import java.util.Collections;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    32
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    33
/*
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    34
 * @test
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    35
 * @bug 8048622 8134232
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    36
 * @summary Checks that PKCS#11 keystore can't be loaded with wrong password
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    37
 * @library ../
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    38
 * @run main/othervm LoadKeystore
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    39
 * @run main/othervm LoadKeystore sm policy
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    40
 */
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    41
public class LoadKeystore extends SecmodTest {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    42
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    44
        if (!initSecmod()) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    45
            return;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    46
        }
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    47
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    48
        String configName = BASE + SEP + "nss.cfg";
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    49
        Provider p = getSunPKCS11(configName);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    50
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    51
        System.out.println("Add provider " + p);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    52
        System.out.println();
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    53
        Security.addProvider(p);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    54
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    55
        if (args.length > 1 && "sm".equals(args[0])) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    56
            System.setProperty("java.security.policy",
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    57
                    BASE + File.separator + args[1]);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    58
            System.setSecurityManager(new SecurityManager());
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    59
        }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 32635
diff changeset
    60
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    61
        try {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    62
            System.out.println("Load keystore with wrong type");
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    63
            KeyStore.getInstance("unknown", p);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    64
            throw new RuntimeException("Expected exception not thrown");
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    65
        } catch(KeyStoreException e) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    66
            System.out.println("Expected exception: " + e);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    67
        }
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    68
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    69
        KeyStore ks = KeyStore.getInstance("PKCS11", p);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    70
        if (!"PKCS11".equals(ks.getType())) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    71
            throw new RuntimeException("Unexpected keystore type: "
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    72
                    + ks.getType());
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    73
        }
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    74
        if (!p.equals(ks.getProvider())) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    75
            throw new RuntimeException("Unexpected keystore provider: "
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    76
                    + ks.getProvider());
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    77
        }
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    78
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    79
        try {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    80
            System.out.println("Load keystore with wrong password");
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    81
            ks.load(null, "wrong".toCharArray());
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    82
            throw new RuntimeException("Expected exception not thrown");
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    83
        } catch(IOException e) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    84
            System.out.println("Expected exception: " + e);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    85
            Throwable cause = e.getCause();
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    86
            if (!(cause instanceof UnrecoverableKeyException)) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    87
                e.printStackTrace(System.out);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    88
                throw new RuntimeException("Unexpected cause: " + cause);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    89
            }
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    90
            System.out.println("Expected cause: " + cause);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    91
        }
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    92
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    93
        System.out.println("Load keystore with correct password");
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    94
        ks.load(null, password);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    95
        for (String alias : Collections.list(ks.aliases())) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    96
            System.out.println("Alias: " + alias);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    97
        }
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    98
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
    99
        System.out.println("Test passed");
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
   100
    }
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
   101
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
diff changeset
   102
}