test/jdk/sun/security/pkcs11/PKCS11Test.java
author jjiang
Fri, 03 May 2019 15:57:41 +0800
changeset 54697 251090f84412
parent 51944 28085dba5d9a
child 57999 b7afd4b040d3
child 58678 9cf78a70fa4f
permissions -rw-r--r--
8204203: Many pkcs11 tests failed in Provider initialization, after compiler on Windows changed Summary: Build NSS 3.41 with VS2017 and also upgrade to this NSS for macosx Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54697
251090f84412 8204203: Many pkcs11 tests failed in Provider initialization, after compiler on Windows changed
jjiang
parents: 51944
diff changeset
     2
 * Copyright (c) 2003, 2019, 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
// common infrastructure for SunPKCS11 tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    27
import java.io.BufferedReader;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    28
import java.io.ByteArrayOutputStream;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    29
import java.io.File;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    30
import java.io.IOException;
51944
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
    31
import java.io.InputStream;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    32
import java.io.InputStreamReader;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    33
import java.io.StringReader;
48665
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
    34
import java.nio.charset.StandardCharsets;
51944
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
    35
import java.nio.file.Files;
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
    36
import java.nio.file.Path;
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
    37
import java.nio.file.Paths;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    38
import java.security.AlgorithmParameters;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    39
import java.security.InvalidAlgorithmParameterException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    40
import java.security.KeyPairGenerator;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    41
import java.security.NoSuchProviderException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    42
import java.security.Provider;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    43
import java.security.ProviderException;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    44
import java.security.Security;
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
    45
import java.security.spec.ECGenParameterSpec;
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
    46
import java.security.spec.ECParameterSpec;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    47
import java.util.ArrayList;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    48
import java.util.Arrays;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    49
import java.util.HashMap;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    50
import java.util.Iterator;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    51
import java.util.List;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    52
import java.util.Map;
50731
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
    53
import java.util.Optional;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    54
import java.util.Properties;
41556
0c49ded763a8 8168078: Remove permission to read all system properties granted to the jdk.crypto.ec module
mullan
parents: 41485
diff changeset
    55
import java.util.ServiceConfigurationError;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    56
import java.util.ServiceLoader;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    57
import java.util.Set;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
    59
import jdk.test.lib.artifacts.Artifact;
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
    60
import jdk.test.lib.artifacts.ArtifactResolver;
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
    61
import jdk.test.lib.artifacts.ArtifactResolverException;
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
    62
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public abstract class PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    65
    private boolean enableSM = false;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    66
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    67
    static final Properties props = System.getProperties();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    68
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 32138
diff changeset
    69
    static final String PKCS11 = "PKCS11";
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 32138
diff changeset
    70
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // directory of the test source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static final String BASE = System.getProperty("test.src", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static final char SEP = File.separatorChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    76
    private static final String DEFAULT_POLICY =
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    77
            BASE + SEP + ".." + SEP + "policy";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // directory corresponding to BASE in the /closed hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static final String CLOSED_BASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // hack
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        String absBase = new File(BASE).getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        int k = absBase.indexOf(SEP + "test" + SEP + "sun" + SEP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (k < 0) k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        String p1 = absBase.substring(0, k + 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        String p2 = absBase.substring(k + 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        CLOSED_BASE = p1 + "closed" + p2;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    90
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    91
        // set it as a system property to make it available in policy file
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
    92
        System.setProperty("closed.base", CLOSED_BASE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
    95
    // NSS version info
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
    96
    public static enum ECCState { None, Basic, Extended };
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
    97
    static double nss_version = -1;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
    98
    static ECCState nss_ecc_status = ECCState.Extended;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
    99
19066
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   100
    // The NSS library we need to search for in getNSSLibDir()
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   101
    // Default is "libsoftokn3.so", listed as "softokn3"
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   102
    // The other is "libnss3.so", listed as "nss3".
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   103
    static String nss_library = "softokn3";
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   104
24863
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   105
    // NSS versions of each library.  It is simplier to keep nss_version
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   106
    // for quick checking for generic testing than many if-else statements.
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   107
    static double softoken3_version = -1;
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   108
    static double nss3_version = -1;
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   109
    static Provider pkcs11;
24863
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   110
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   111
    // Goes through ServiceLoader instead of Provider.getInstance() since it
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   112
    // works on all platforms
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   113
    static {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   114
        ServiceLoader sl = ServiceLoader.load(java.security.Provider.class);
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   115
        Iterator<Provider> iter = sl.iterator();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   116
        Provider p = null;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   117
        boolean found = false;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   118
        while (iter.hasNext()) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   119
            try {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   120
                p = iter.next();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   121
                if (p.getName().equals("SunPKCS11")) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   122
                    found = true;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   123
                    break;
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   124
                }
41556
0c49ded763a8 8168078: Remove permission to read all system properties granted to the jdk.crypto.ec module
mullan
parents: 41485
diff changeset
   125
            } catch (Exception | ServiceConfigurationError e) {
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   126
                // ignore and move on to the next one
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   127
            }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   128
        }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   129
        // Nothing found through ServiceLoader; fall back to reflection
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   130
        if (!found) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   131
            try {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   132
                Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11");
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   133
                p = (Provider) clazz.newInstance();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   134
            } catch (Exception ex) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   135
                ex.printStackTrace();
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   136
            }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   137
        }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   138
        pkcs11 = p;
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   139
    }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   140
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   141
    /*
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   142
     * Use Solaris SPARC 11.2 or later to avoid an intermittent failure
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   143
     * when running SunPKCS11-Solaris (8044554)
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   144
     */
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   145
    static boolean isBadSolarisSparc(Provider p) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   146
        if ("SunPKCS11-Solaris".equals(p.getName()) && badSolarisSparc) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   147
            System.out.println("SunPKCS11-Solaris provider requires " +
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   148
                "Solaris SPARC 11.2 or later, skipping");
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   149
            return true;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   150
        }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   151
        return false;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   152
    }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   153
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   154
    // Return a SunPKCS11 provider configured with the specified config file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    static Provider getSunPKCS11(String config) throws Exception {
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   156
        if (pkcs11 == null) {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   157
            throw new NoSuchProviderException("No PKCS11 provider available");
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   158
        }
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   159
        return pkcs11.configure(config);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public abstract void main(Provider p) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
51245
f095e3bc2d41 8206258: [Test Error] sun/security/pkcs11 tests fail if NSS libs not found
jjiang
parents: 51213
diff changeset
   164
    protected boolean skipTest(Provider p) {
f095e3bc2d41 8206258: [Test Error] sun/security/pkcs11 tests fail if NSS libs not found
jjiang
parents: 51213
diff changeset
   165
        return false;
f095e3bc2d41 8206258: [Test Error] sun/security/pkcs11 tests fail if NSS libs not found
jjiang
parents: 51213
diff changeset
   166
    }
f095e3bc2d41 8206258: [Test Error] sun/security/pkcs11 tests fail if NSS libs not found
jjiang
parents: 51213
diff changeset
   167
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private void premain(Provider p) throws Exception {
51245
f095e3bc2d41 8206258: [Test Error] sun/security/pkcs11 tests fail if NSS libs not found
jjiang
parents: 51213
diff changeset
   169
        if (skipTest(p)) {
f095e3bc2d41 8206258: [Test Error] sun/security/pkcs11 tests fail if NSS libs not found
jjiang
parents: 51213
diff changeset
   170
            return;
f095e3bc2d41 8206258: [Test Error] sun/security/pkcs11 tests fail if NSS libs not found
jjiang
parents: 51213
diff changeset
   171
        }
f095e3bc2d41 8206258: [Test Error] sun/security/pkcs11 tests fail if NSS libs not found
jjiang
parents: 51213
diff changeset
   172
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   173
        // set a security manager and policy before a test case runs,
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   174
        // and disable them after the test case finished
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   175
        try {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   176
            if (enableSM) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   177
                System.setSecurityManager(new SecurityManager());
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   178
            }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   179
            long start = System.currentTimeMillis();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   180
            System.out.printf(
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   181
                    "Running test with provider %s (security manager %s) ...%n",
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   182
                        p.getName(), enableSM ? "enabled" : "disabled");
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   183
            main(p);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   184
            long stop = System.currentTimeMillis();
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   185
            System.out.println("Completed test with provider " + p.getName() +
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   186
                " (" + (stop - start) + " ms).");
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   187
        } finally {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   188
            if (enableSM) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   189
                System.setSecurityManager(null);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   190
            }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   191
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public static void main(PKCS11Test test) throws Exception {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   195
        main(test, null);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   196
    }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   197
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   198
    public static void main(PKCS11Test test, String[] args) throws Exception {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   199
        if (args != null) {
37353
e8e684b89c81 8153545: sun/security/pkcs11/Provider/Login.sh fails on Linux
amjiang
parents: 36249
diff changeset
   200
            if (args.length > 0) {
e8e684b89c81 8153545: sun/security/pkcs11/Provider/Login.sh fails on Linux
amjiang
parents: 36249
diff changeset
   201
                if ("sm".equals(args[0])) {
e8e684b89c81 8153545: sun/security/pkcs11/Provider/Login.sh fails on Linux
amjiang
parents: 36249
diff changeset
   202
                    test.enableSM = true;
e8e684b89c81 8153545: sun/security/pkcs11/Provider/Login.sh fails on Linux
amjiang
parents: 36249
diff changeset
   203
                } else {
e8e684b89c81 8153545: sun/security/pkcs11/Provider/Login.sh fails on Linux
amjiang
parents: 36249
diff changeset
   204
                    throw new RuntimeException("Unknown Command, use 'sm' as "
e8e684b89c81 8153545: sun/security/pkcs11/Provider/Login.sh fails on Linux
amjiang
parents: 36249
diff changeset
   205
                            + "first arguemtn to enable security manager");
e8e684b89c81 8153545: sun/security/pkcs11/Provider/Login.sh fails on Linux
amjiang
parents: 36249
diff changeset
   206
                }
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   207
            }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   208
            if (test.enableSM) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   209
                System.setProperty("java.security.policy",
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   210
                        (args.length > 1) ? BASE + SEP + args[1]
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   211
                                : DEFAULT_POLICY);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   212
            }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   213
        }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   214
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   215
        Provider[] oldProviders = Security.getProviders();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   216
        try {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   217
            System.out.println("Beginning test run " + test.getClass().getName() + "...");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   218
            testDefault(test);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   219
            testNSS(test);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   220
            testDeimos(test);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   221
        } finally {
19066
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   222
            // NOTE: Do not place a 'return' in any finally block
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   223
            // as it will suppress exceptions and hide test failures.
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   224
            Provider[] newProviders = Security.getProviders();
19066
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   225
            boolean found = true;
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   226
            // Do not restore providers if nothing changed. This is especailly
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   227
            // useful for ./Provider/Login.sh, where a SecurityManager exists.
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   228
            if (oldProviders.length == newProviders.length) {
19066
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   229
                found = false;
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   230
                for (int i = 0; i<oldProviders.length; i++) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   231
                    if (oldProviders[i] != newProviders[i]) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   232
                        found = true;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   233
                        break;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   234
                    }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   235
                }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   236
            }
19066
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   237
            if (found) {
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   238
                for (Provider p: newProviders) {
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   239
                    Security.removeProvider(p.getName());
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   240
                }
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   241
                for (Provider p: oldProviders) {
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   242
                    Security.addProvider(p);
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   243
                }
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   244
            }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   245
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public static void testDeimos(PKCS11Test test) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            "true".equals(System.getProperty("NO_DEIMOS"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        String base = getBase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        Provider p = getSunPKCS11(p11config);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        test.premain(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public static void testDefault(PKCS11Test test) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // run test for default configured PKCS11 providers (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if ("true".equals(System.getProperty("NO_DEFAULT"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        Provider[] providers = Security.getProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        for (int i = 0; i < providers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            Provider p = providers[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (p.getName().startsWith("SunPKCS11-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                test.premain(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private static String PKCS11_BASE;
12294
f313586fc3cc 7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents: 12042
diff changeset
   276
    static {
f313586fc3cc 7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents: 12042
diff changeset
   277
        try {
f313586fc3cc 7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents: 12042
diff changeset
   278
            PKCS11_BASE = getBase();
f313586fc3cc 7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents: 12042
diff changeset
   279
        } catch (Exception e) {
f313586fc3cc 7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents: 12042
diff changeset
   280
            // ignore
f313586fc3cc 7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents: 12042
diff changeset
   281
        }
f313586fc3cc 7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents: 12042
diff changeset
   282
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    private final static String PKCS11_REL_PATH = "sun/security/pkcs11";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public static String getBase() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (PKCS11_BASE != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return PKCS11_BASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        File cwd = new File(System.getProperty("test.src", ".")).getCanonicalFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            File file = new File(cwd, "TEST.ROOT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (file.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            cwd = cwd.getParentFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (cwd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                throw new Exception("Test root directory not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return PKCS11_BASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public static String getNSSLibDir() throws Exception {
24863
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   306
        return getNSSLibDir(nss_library);
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   307
    }
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   308
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   309
    static String getNSSLibDir(String library) throws Exception {
51944
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   310
        Path libPath = getNSSLibPath(library);
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   311
        if (libPath == null) {
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   312
            return null;
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   313
        }
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   314
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   315
        String libDir = String.valueOf(libPath.getParent()) + File.separatorChar;
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   316
        System.out.println("nssLibDir: " + libDir);
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   317
        System.setProperty("pkcs11test.nss.libdir", libDir);
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   318
        return libDir;
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   319
    }
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   320
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   321
    private static Path getNSSLibPath() throws Exception {
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   322
        return getNSSLibPath(nss_library);
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   323
    }
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   324
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   325
    static Path getNSSLibPath(String library) throws Exception {
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   326
        String osid = getOsId();
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   327
        String[] nssLibDirs = getNssLibPaths(osid);
13565
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 13250
diff changeset
   328
        if (nssLibDirs == null) {
41485
15a87b8ccd06 8164322: Test sun/security/pkcs11/PKCS11Test.java shall be updated to run on ARM platforms
tidu
parents: 40942
diff changeset
   329
            System.out.println("Warning: unsupported OS: " + osid
15a87b8ccd06 8164322: Test sun/security/pkcs11/PKCS11Test.java shall be updated to run on ARM platforms
tidu
parents: 40942
diff changeset
   330
                    + ", please initialize NSS librarys location firstly, skipping test");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
13565
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 13250
diff changeset
   333
        if (nssLibDirs.length == 0) {
41485
15a87b8ccd06 8164322: Test sun/security/pkcs11/PKCS11Test.java shall be updated to run on ARM platforms
tidu
parents: 40942
diff changeset
   334
            System.out.println("Warning: NSS not supported on this platform, skipping test");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
51944
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   337
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   338
        Path nssLibPath = null;
13565
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 13250
diff changeset
   339
        for (String dir : nssLibDirs) {
51944
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   340
            Path libPath = Paths.get(dir).resolve(System.mapLibraryName(library));
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   341
            if (Files.exists(libPath)) {
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   342
                nssLibPath = libPath;
13565
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 13250
diff changeset
   343
                break;
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 13250
diff changeset
   344
            }
6298d600104f 7190945: pkcs11 problem loading NSS libs on Ubuntu
vinnie
parents: 13250
diff changeset
   345
        }
51944
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   346
        if (nssLibPath == null) {
41485
15a87b8ccd06 8164322: Test sun/security/pkcs11/PKCS11Test.java shall be updated to run on ARM platforms
tidu
parents: 40942
diff changeset
   347
            System.out.println("Warning: can't find NSS librarys on this machine, skipping test");
15a87b8ccd06 8164322: Test sun/security/pkcs11/PKCS11Test.java shall be updated to run on ARM platforms
tidu
parents: 40942
diff changeset
   348
            return null;
15a87b8ccd06 8164322: Test sun/security/pkcs11/PKCS11Test.java shall be updated to run on ARM platforms
tidu
parents: 40942
diff changeset
   349
        }
51944
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   350
        return nssLibPath;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   353
    private static String getOsId() {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   354
        String osName = props.getProperty("os.name");
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   355
        if (osName.startsWith("Win")) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   356
            osName = "Windows";
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   357
        } else if (osName.equals("Mac OS X")) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   358
            osName = "MacOSX";
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   359
        }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   360
        String osid = osName + "-" + props.getProperty("os.arch") + "-"
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   361
                + props.getProperty("sun.arch.data.model");
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   362
        return osid;
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   363
    }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   364
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   365
    static boolean isBadNSSVersion(Provider p) {
51245
f095e3bc2d41 8206258: [Test Error] sun/security/pkcs11 tests fail if NSS libs not found
jjiang
parents: 51213
diff changeset
   366
        double nssVersion = getNSSVersion();
f095e3bc2d41 8206258: [Test Error] sun/security/pkcs11 tests fail if NSS libs not found
jjiang
parents: 51213
diff changeset
   367
        if (isNSS(p) && nssVersion >= 3.11 && nssVersion < 3.12) {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   368
            System.out.println("NSS 3.11 has a DER issue that recent " +
51245
f095e3bc2d41 8206258: [Test Error] sun/security/pkcs11 tests fail if NSS libs not found
jjiang
parents: 51213
diff changeset
   369
                    "version do not, skipping");
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   370
            return true;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   371
        }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   372
        return false;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   373
    }
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   374
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   375
    protected static void safeReload(String lib) throws Exception {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   376
        try {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   377
            System.load(lib);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   378
        } catch (UnsatisfiedLinkError e) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   379
            if (e.getMessage().contains("already loaded")) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   380
                return;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   381
            }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   382
        }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   383
    }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   384
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    static boolean loadNSPR(String libdir) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        // load NSS softoken dependencies in advance to avoid resolver issues
54697
251090f84412 8204203: Many pkcs11 tests failed in Provider initialization, after compiler on Windows changed
jjiang
parents: 51944
diff changeset
   387
        String dir = libdir.endsWith(File.separator)
251090f84412 8204203: Many pkcs11 tests failed in Provider initialization, after compiler on Windows changed
jjiang
parents: 51944
diff changeset
   388
                     ? libdir
251090f84412 8204203: Many pkcs11 tests failed in Provider initialization, after compiler on Windows changed
jjiang
parents: 51944
diff changeset
   389
                     : libdir + File.separator;
251090f84412 8204203: Many pkcs11 tests failed in Provider initialization, after compiler on Windows changed
jjiang
parents: 51944
diff changeset
   390
        safeReload(dir + System.mapLibraryName("nspr4"));
251090f84412 8204203: Many pkcs11 tests failed in Provider initialization, after compiler on Windows changed
jjiang
parents: 51944
diff changeset
   391
        safeReload(dir + System.mapLibraryName("plc4"));
251090f84412 8204203: Many pkcs11 tests failed in Provider initialization, after compiler on Windows changed
jjiang
parents: 51944
diff changeset
   392
        safeReload(dir + System.mapLibraryName("plds4"));
251090f84412 8204203: Many pkcs11 tests failed in Provider initialization, after compiler on Windows changed
jjiang
parents: 51944
diff changeset
   393
        safeReload(dir + System.mapLibraryName("sqlite3"));
251090f84412 8204203: Many pkcs11 tests failed in Provider initialization, after compiler on Windows changed
jjiang
parents: 51944
diff changeset
   394
        safeReload(dir + System.mapLibraryName("nssutil3"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   398
    // Check the provider being used is NSS
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   399
    public static boolean isNSS(Provider p) {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   400
        return p.getName().toUpperCase().equals("SUNPKCS11-NSS");
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   401
    }
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   402
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   403
    static double getNSSVersion() {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   404
        if (nss_version == -1)
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   405
            getNSSInfo();
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   406
        return nss_version;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   407
    }
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   408
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   409
    static ECCState getNSSECC() {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   410
        if (nss_version == -1)
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   411
            getNSSInfo();
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   412
        return nss_ecc_status;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   413
    }
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   414
24863
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   415
    public static double getLibsoftokn3Version() {
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   416
        if (softoken3_version == -1)
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   417
            return getNSSInfo("softokn3");
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   418
        return softoken3_version;
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   419
    }
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   420
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   421
    public static double getLibnss3Version() {
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   422
        if (nss3_version == -1)
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   423
            return getNSSInfo("nss3");
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   424
        return nss3_version;
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   425
    }
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   426
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   427
    /* Read the library to find out the verison */
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   428
    static void getNSSInfo() {
24863
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   429
        getNSSInfo(nss_library);
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   430
    }
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   431
48665
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   432
    // Try to parse the version for the specified library.
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   433
    // Assuming the library contains either of the following patterns:
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   434
    // $Header: NSS <version>
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   435
    // Version: NSS <version>
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   436
    // Here, <version> stands for NSS version.
24863
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   437
    static double getNSSInfo(String library) {
36249
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 35379
diff changeset
   438
        // look for two types of headers in NSS libraries
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 35379
diff changeset
   439
        String nssHeader1 = "$Header: NSS";
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 35379
diff changeset
   440
        String nssHeader2 = "Version: NSS";
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   441
        boolean found = false;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   442
        String s = null;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   443
        int i = 0;
51944
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   444
        Path libfile = null;
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   445
24863
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   446
        if (library.compareTo("softokn3") == 0 && softoken3_version > -1)
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   447
            return softoken3_version;
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   448
        if (library.compareTo("nss3") == 0 && nss3_version > -1)
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   449
            return nss3_version;
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   450
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   451
        try {
51944
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   452
            libfile = getNSSLibPath();
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   453
            if (libfile == null) {
51213
24c4780f69a5 8207766: [testbug] Adapt tests for Aix.
goetz
parents: 50731
diff changeset
   454
                return 0.0;
24c4780f69a5 8207766: [testbug] Adapt tests for Aix.
goetz
parents: 50731
diff changeset
   455
            }
51944
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 51460
diff changeset
   456
            try (InputStream is = Files.newInputStream(libfile)) {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   457
                byte[] data = new byte[1000];
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   458
                int read = 0;
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   459
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   460
                while (is.available() > 0) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   461
                    if (read == 0) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   462
                        read = is.read(data, 0, 1000);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   463
                    } else {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   464
                        // Prepend last 100 bytes in case the header was split
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   465
                        // between the reads.
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   466
                        System.arraycopy(data, 900, data, 0, 100);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   467
                        read = 100 + is.read(data, 100, 900);
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   468
                    }
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   469
48665
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   470
                    s = new String(data, 0, read, StandardCharsets.US_ASCII);
36249
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 35379
diff changeset
   471
                    i = s.indexOf(nssHeader1);
8b92e4bedbd4 8138653: Default key sizes for the AlgorithmParameterGenerator and KeyPairGenerator implementations should be upgraded
mullan
parents: 35379
diff changeset
   472
                    if (i > 0 || (i = s.indexOf(nssHeader2)) > 0) {
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   473
                        found = true;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   474
                        // If the nssHeader is before 920 we can break, otherwise
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   475
                        // we may not have the whole header so do another read.  If
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   476
                        // no bytes are in the stream, that is ok, found is true.
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   477
                        if (i < 920) {
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   478
                            break;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   479
                        }
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   480
                    }
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   481
                }
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   482
            }
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   483
        } catch (Exception e) {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   484
            e.printStackTrace();
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   485
        }
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   486
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   487
        if (!found) {
24863
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   488
            System.out.println("lib" + library +
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   489
                    " version not found, set to 0.0: " + libfile);
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   490
            nss_version = 0.0;
24863
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   491
            return nss_version;
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   492
        }
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   493
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   494
        // the index after whitespace after nssHeader
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   495
        int afterheader = s.indexOf("NSS", i) + 4;
48665
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   496
        String version = String.valueOf(s.charAt(afterheader));
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   497
        for (char c = s.charAt(++afterheader);
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   498
                c == '.' || (c >= '0' && c <= '9');
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   499
                c = s.charAt(++afterheader)) {
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   500
            version += c;
48547
7537c762d42d 8194864: Outputs more details for PKCS11 tests if the NSS lib version cannot be determined
jjiang
parents: 48248
diff changeset
   501
        }
7537c762d42d 8194864: Outputs more details for PKCS11 tests if the NSS lib version cannot be determined
jjiang
parents: 48248
diff changeset
   502
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   503
        // If a "dot dot" release, strip the extra dots for double parsing
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   504
        String[] dot = version.split("\\.");
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   505
        if (dot.length > 2) {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   506
            version = dot[0]+"."+dot[1];
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   507
            for (int j = 2; dot.length > j; j++) {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   508
                version += dot[j];
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   509
            }
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   510
        }
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   511
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   512
        // Convert to double for easier version value checking
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   513
        try {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   514
            nss_version = Double.parseDouble(version);
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   515
        } catch (NumberFormatException e) {
48665
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   516
            System.out.println("===== Content start =====");
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   517
            System.out.println(s);
257d7610663f 8186098: sun/security/pkcs11/KeyStore/SecretKeysBasic.sh failed due to libnss3 version cannot be parsed
jjiang
parents: 48608
diff changeset
   518
            System.out.println("===== Content end =====");
24863
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   519
            System.out.println("Failed to parse lib" + library +
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   520
                    " version. Set to 0.0");
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   521
            e.printStackTrace();
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   522
        }
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   523
24863
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   524
        System.out.print("lib" + library + " version = "+version+".  ");
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   525
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   526
        // Check for ECC
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   527
        if (s.indexOf("Basic") > 0) {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   528
            nss_ecc_status = ECCState.Basic;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   529
            System.out.println("ECC Basic.");
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   530
        } else if (s.indexOf("Extended") > 0) {
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   531
            nss_ecc_status = ECCState.Extended;
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   532
            System.out.println("ECC Extended.");
24863
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   533
        } else {
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   534
            System.out.println("ECC None.");
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   535
        }
24863
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   536
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   537
        if (library.compareTo("softokn3") == 0) {
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   538
            softoken3_version = nss_version;
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   539
        } else if (library.compareTo("nss3") == 0) {
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   540
            nss3_version = nss_version;
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   541
        }
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   542
bf6df3caafe4 8039212: SecretKeyBasic.sh needs to avoid NSS libnss3 and libsoftokn3 version mismatches
ascarpino
parents: 24629
diff changeset
   543
        return nss_version;
19067
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   544
    }
5271291b7121 8020424: The NSS version should be detected before running crypto tests
ascarpino
parents: 19066
diff changeset
   545
19066
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   546
    // Used to set the nss_library file to search for libsoftokn3.so
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   547
    public static void useNSS() {
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   548
        nss_library = "nss3";
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   549
    }
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   550
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    public static void testNSS(PKCS11Test test) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        String libdir = getNSSLibDir();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        if (libdir == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        String base = getBase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (loadNSPR(libdir) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
19066
daf668acb10e 8012971: PKCS11Test hiding exception failures
ascarpino
parents: 13565
diff changeset
   562
        String libfile = libdir + System.mapLibraryName(nss_library);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        String customDBdir = System.getProperty("CUSTOM_DB_DIR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        String dbdir = (customDBdir != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                                customDBdir :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                                base + SEP + "nss" + SEP + "db";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        // NSS always wants forward slashes for the config path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        dbdir = dbdir.replace('\\', '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        String customConfig = System.getProperty("CUSTOM_P11_CONFIG");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        String customConfigName = System.getProperty("CUSTOM_P11_CONFIG_NAME", "p11-nss.txt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        String p11config = (customConfig != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                customConfig :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                                base + SEP + "nss" + SEP + customConfigName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        System.setProperty("pkcs11test.nss.lib", libfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        System.setProperty("pkcs11test.nss.db", dbdir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        Provider p = getSunPKCS11(p11config);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        test.premain(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   583
    // Generate a vector of supported elliptic curves of a given provider
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   584
    static List<ECParameterSpec> getKnownCurves(Provider p) throws Exception {
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   585
        int index;
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   586
        int begin;
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   587
        int end;
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   588
        String curve;
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   589
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   590
        List<ECParameterSpec> results = new ArrayList<>();
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   591
        // Get Curves to test from SunEC.
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   592
        String kcProp = Security.getProvider("SunEC").
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   593
                getProperty("AlgorithmParameters.EC SupportedCurves");
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   594
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   595
        if (kcProp == null) {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   596
            throw new RuntimeException(
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   597
            "\"AlgorithmParameters.EC SupportedCurves property\" not found");
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   598
        }
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   599
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   600
        System.out.println("Finding supported curves using list from SunEC\n");
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   601
        index = 0;
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   602
        for (;;) {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   603
            // Each set of curve names is enclosed with brackets.
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   604
            begin = kcProp.indexOf('[', index);
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   605
            end = kcProp.indexOf(']', index);
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   606
            if (begin == -1 || end == -1) {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   607
                break;
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   608
            }
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   609
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   610
            /*
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   611
             * Each name is separated by a comma.
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   612
             * Just get the first name in the set.
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   613
             */
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   614
            index = end + 1;
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   615
            begin++;
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   616
            end = kcProp.indexOf(',', begin);
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   617
            if (end == -1) {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   618
                // Only one name in the set.
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   619
                end = index -1;
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   620
            }
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   621
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   622
            curve = kcProp.substring(begin, end);
50731
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   623
            getSupportedECParameterSpec(curve, p)
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   624
                .ifPresent(spec -> results.add(spec));
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   625
        }
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   626
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   627
        if (results.size() == 0) {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   628
            throw new RuntimeException("No supported EC curves found");
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   629
        }
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   630
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   631
        return results;
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   632
    }
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   633
50731
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   634
    static Optional<ECParameterSpec> getSupportedECParameterSpec(String curve,
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   635
            Provider p) throws Exception {
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   636
        ECParameterSpec e = getECParameterSpec(p, curve);
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   637
        System.out.print("\t "+ curve + ": ");
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   638
        try {
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   639
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   640
            kpg.initialize(e);
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   641
            kpg.generateKeyPair();
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   642
            System.out.println("Supported");
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   643
            return Optional.of(e);
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   644
        } catch (ProviderException ex) {
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   645
            System.out.println("Unsupported: PKCS11: " +
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   646
                    ex.getCause().getMessage());
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   647
            return Optional.empty();
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   648
        } catch (InvalidAlgorithmParameterException ex) {
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   649
            System.out.println("Unsupported: Key Length: " +
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   650
                    ex.getMessage());
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   651
            return Optional.empty();
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   652
        }
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   653
    }
ed322b4cfe49 7007966: Add Brainpool ECC support (RFC 5639)
valeriep
parents: 48665
diff changeset
   654
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   655
    private static ECParameterSpec getECParameterSpec(Provider p, String name)
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   656
            throws Exception {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   657
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   658
        AlgorithmParameters parameters =
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   659
            AlgorithmParameters.getInstance("EC", p);
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   660
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   661
        parameters.init(new ECGenParameterSpec(name));
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   662
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   663
        return parameters.getParameterSpec(ECParameterSpec.class);
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   664
    }
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   665
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   666
    // Check support for a curve with a provided Vector of EC support
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   667
    boolean checkSupport(List<ECParameterSpec> supportedEC,
21977
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   668
            ECParameterSpec curve) {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   669
        for (ECParameterSpec ec: supportedEC) {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   670
            if (ec.equals(curve)) {
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   671
                return true;
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   672
            }
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   673
        }
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   674
        return false;
17f538f05b73 8027218: TEST_BUG: sun/security/pkcs11/ec tests fail because of ever-changing key size restrictions
ascarpino
parents: 19067
diff changeset
   675
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   677
    private static Map<String,String[]> osMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
12294
f313586fc3cc 7152582: PKCS11 tests should use the NSS libraries available in the OS
vinnie
parents: 12042
diff changeset
   679
    // Location of the NSS libraries on each supported platform
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   680
    private static Map<String, String[]> getOsMap() {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   681
        if (osMap != null) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   682
            return osMap;
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   683
        }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   684
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   685
        osMap = new HashMap<>();
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   686
        osMap.put("SunOS-sparc-32", new String[] { "/usr/lib/mps/" });
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   687
        osMap.put("SunOS-sparcv9-64", new String[] { "/usr/lib/mps/64/" });
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   688
        osMap.put("SunOS-x86-32", new String[] { "/usr/lib/mps/" });
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   689
        osMap.put("SunOS-amd64-64", new String[] { "/usr/lib/mps/64/" });
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   690
        osMap.put("Linux-i386-32", new String[] {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   691
                "/usr/lib/i386-linux-gnu/",
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   692
                "/usr/lib32/",
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   693
                "/usr/lib/" });
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   694
        osMap.put("Linux-amd64-64", new String[] {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   695
                "/usr/lib/x86_64-linux-gnu/",
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   696
                "/usr/lib/x86_64-linux-gnu/nss/",
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   697
                "/usr/lib64/" });
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   698
        osMap.put("Linux-ppc64-64", new String[] { "/usr/lib64/" });
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   699
        osMap.put("Linux-ppc64le-64", new String[] { "/usr/lib64/" });
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   700
        osMap.put("Linux-s390x-64", new String[] { "/usr/lib64/" });
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   701
        osMap.put("Windows-x86-32", new String[] {});
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   702
        osMap.put("Windows-amd64-64", new String[] {});
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   703
        osMap.put("MacOSX-x86_64-64", new String[] {});
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   704
        osMap.put("Linux-arm-32", new String[] {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   705
                "/usr/lib/arm-linux-gnueabi/nss/",
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   706
                "/usr/lib/arm-linux-gnueabihf/nss/" });
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   707
        osMap.put("Linux-aarch64-64", new String[] {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   708
                "/usr/lib/aarch64-linux-gnu/",
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   709
                "/usr/lib/aarch64-linux-gnu/nss/" });
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   710
        return osMap;
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   711
    }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   712
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   713
    private static String[] getNssLibPaths(String osId) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   714
        String[] preferablePaths = getPreferableNssLibPaths(osId);
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   715
        if (preferablePaths.length != 0) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   716
            return preferablePaths;
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   717
        } else {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   718
            return getOsMap().get(osId);
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   719
        }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   720
    }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   721
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   722
    private static String[] getPreferableNssLibPaths(String osId) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   723
        List<String> nssLibPaths = new ArrayList<>();
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   724
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   725
        String customNssLibPaths = System.getProperty("test.nss.lib.paths");
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   726
        if (customNssLibPaths == null) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   727
            // If custom local NSS lib path is not provided,
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   728
            // try to download NSS libs from artifactory
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   729
            String path = fetchNssLib(osId);
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   730
            if (path != null) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   731
                nssLibPaths.add(path);
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   732
            }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   733
        } else {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   734
            String[] paths = customNssLibPaths.split(",");
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   735
            for (String path : paths) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   736
                if (!path.endsWith(File.separator)) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   737
                    nssLibPaths.add(path + File.separator);
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   738
                } else {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   739
                    nssLibPaths.add(path);
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   740
                }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   741
            }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   742
        }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   743
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   744
        return nssLibPaths.toArray(new String[nssLibPaths.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    private final static char[] hexDigits = "0123456789abcdef".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
42366
b1483ec50db1 8170523: Some PKCS11 test cases are ignored with security manager
mli
parents: 41556
diff changeset
   749
    private static final String distro = distro();
b1483ec50db1 8170523: Some PKCS11 test cases are ignored with security manager
mli
parents: 41556
diff changeset
   750
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   751
    static final boolean badSolarisSparc =
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   752
            System.getProperty("os.name").equals("SunOS") &&
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   753
            System.getProperty("os.arch").equals("sparcv9") &&
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   754
            System.getProperty("os.version").compareTo("5.11") <= 0 &&
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   755
            getDistro().compareTo("11.2") < 0;
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   756
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    public static String toString(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            return "(null)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        }
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   761
        StringBuilder sb = new StringBuilder(b.length * 3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        for (int i = 0; i < b.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            int k = b[i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            sb.append(hexDigits[k >>> 4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            sb.append(hexDigits[k & 0xf]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    public static byte[] parse(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        if (s.equals("(null)")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            StringReader r = new StringReader(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                int b1 = nextNibble(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                if (b1 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                int b2 = nextNibble(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                if (b2 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    throw new RuntimeException("Invalid string " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                int b = (b1 << 4) | b2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                out.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    private static int nextNibble(StringReader r) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            int ch = r.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            if (ch == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            } else if ((ch >= '0') && (ch <= '9')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                return ch - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            } else if ((ch >= 'a') && (ch <= 'f')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                return ch - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            } else if ((ch >= 'A') && (ch <= 'F')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                return ch - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    <T> T[] concat(T[] a, T[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        if ((b == null) || (b.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        }
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 28403
diff changeset
   818
        T[] r = Arrays.copyOf(a, a.length + b.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        System.arraycopy(b, 0, r, a.length, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
28403
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   823
    /**
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   824
     * Returns supported algorithms of specified type.
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   825
     */
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   826
    static List<String> getSupportedAlgorithms(String type, String alg,
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   827
            Provider p) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   828
        // prepare a list of supported algorithms
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   829
        List<String> algorithms = new ArrayList<>();
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   830
        Set<Provider.Service> services = p.getServices();
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   831
        for (Provider.Service service : services) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   832
            if (service.getType().equals(type)
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   833
                    && service.getAlgorithm().startsWith(alg)) {
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   834
                algorithms.add(service.getAlgorithm());
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   835
            }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   836
        }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   837
        return algorithms;
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   838
    }
02b91a87c4dd 8048603: Additional tests for MAC algorithms
asmotrak
parents: 26958
diff changeset
   839
32138
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 31270
diff changeset
   840
    /**
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 31270
diff changeset
   841
     * Get the identifier for the operating system distribution
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 31270
diff changeset
   842
     */
35379
1e8e336ef66b 8144539: Update PKCS11 tests to run with security manager
asmotrak
parents: 35248
diff changeset
   843
    static String getDistro() {
42366
b1483ec50db1 8170523: Some PKCS11 test cases are ignored with security manager
mli
parents: 41556
diff changeset
   844
        return distro;
b1483ec50db1 8170523: Some PKCS11 test cases are ignored with security manager
mli
parents: 41556
diff changeset
   845
    }
b1483ec50db1 8170523: Some PKCS11 test cases are ignored with security manager
mli
parents: 41556
diff changeset
   846
b1483ec50db1 8170523: Some PKCS11 test cases are ignored with security manager
mli
parents: 41556
diff changeset
   847
    private static String distro() {
48248
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   848
        if (props.getProperty("os.name").equals("SunOS")) {
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   849
            try (BufferedReader in =
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   850
                         new BufferedReader(new InputStreamReader(
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   851
                                 Runtime.getRuntime().exec("uname -v").getInputStream()))) {
32138
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 31270
diff changeset
   852
48248
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   853
                return in.readLine();
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   854
            } catch (Exception e) {
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   855
                throw new RuntimeException("Failed to determine distro.", e);
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   856
            }
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   857
        } else {
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   858
            // Not used outside Solaris
55b9b1e184c6 8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
weijun
parents: 47216
diff changeset
   859
            return null;
32138
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 31270
diff changeset
   860
        }
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 31270
diff changeset
   861
    }
32635
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 32138
diff changeset
   862
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 32138
diff changeset
   863
    static byte[] generateData(int length) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 32138
diff changeset
   864
        byte data[] = new byte[length];
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 32138
diff changeset
   865
        for (int i=0; i<data.length; i++) {
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 32138
diff changeset
   866
            data[i] = (byte) (i % 256);
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 32138
diff changeset
   867
        }
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 32138
diff changeset
   868
        return data;
d7e4ba3c2e0d 8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents: 32138
diff changeset
   869
    }
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   870
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   871
    private static String fetchNssLib(String osId) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   872
        switch (osId) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   873
        case "Windows-x86-32":
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   874
            return fetchNssLib(WINDOWS_X86.class);
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   875
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   876
        case "Windows-amd64-64":
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   877
            return fetchNssLib(WINDOWS_X64.class);
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   878
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   879
        case "MacOSX-x86_64-64":
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   880
            return fetchNssLib(MACOSX_X64.class);
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   881
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   882
        default:
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   883
            return null;
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   884
        }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   885
    }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   886
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   887
    private static String fetchNssLib(Class<?> clazz) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   888
        String path = null;
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   889
        try {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   890
            path = ArtifactResolver.resolve(clazz).entrySet().stream()
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   891
                    .findAny().get().getValue() + File.separator + "nsslib"
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   892
                    + File.separator;
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   893
        } catch (ArtifactResolverException e) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   894
            Throwable cause = e.getCause();
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   895
            if (cause == null) {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   896
                System.out.println("Cannot resolve artifact, "
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   897
                        + "please check if JIB jar is present in classpath.");
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   898
            } else {
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   899
                throw new RuntimeException("Fetch artifact failed: " + clazz
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   900
                        + "\nPlease make sure the artifact is available.");
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   901
            }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   902
        }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   903
        return path;
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   904
    }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   905
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   906
    @Artifact(
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   907
            organization = "jpg.tests.jdk.nsslib",
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   908
            name = "nsslib-windows_x64",
54697
251090f84412 8204203: Many pkcs11 tests failed in Provider initialization, after compiler on Windows changed
jjiang
parents: 51944
diff changeset
   909
            revision = "3.41-VS2017",
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   910
            extension = "zip")
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   911
    private static class WINDOWS_X64 { }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   912
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   913
    @Artifact(
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   914
            organization = "jpg.tests.jdk.nsslib",
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   915
            name = "nsslib-windows_x86",
54697
251090f84412 8204203: Many pkcs11 tests failed in Provider initialization, after compiler on Windows changed
jjiang
parents: 51944
diff changeset
   916
            revision = "3.41-VS2017",
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   917
            extension = "zip")
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   918
    private static class WINDOWS_X86 { }
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   919
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   920
    @Artifact(
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   921
            organization = "jpg.tests.jdk.nsslib",
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   922
            name = "nsslib-macosx_x64",
54697
251090f84412 8204203: Many pkcs11 tests failed in Provider initialization, after compiler on Windows changed
jjiang
parents: 51944
diff changeset
   923
            revision = "3.41",
51460
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   924
            extension = "zip")
97e361fe3433 8164639: Configure PKCS11 tests to use user-supplied NSS libraries
jjiang
parents: 51361
diff changeset
   925
    private static class MACOSX_X64 { }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
}