1 /*
2 * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
95 + "AgMBAAGjgYUwgYIwNAYDVR0fBC0wKzApoCegJYYjbGRhcDovL2xkYXAuaG9zdC5m\n"
96 + "b3IuY3JsZHAvbWFpbi5jcmwwSgYIKwYBBQUHAQEEPjA8MDoGCCsGAQUFBzAChi5s\n"
97 + "ZGFwOi8vbGRhcC5ob3N0LmZvci5haWEvZGM9Um9vdD9jQUNlcnRpZmljYXRlMA0G\n"
98 + "CSqGSIb3DQEBDQUAA4IBAQBWDfZHpuUx0yn5d3+BuztFqoks1MkGdk+USlH0TB1/\n"
99 + "gWWBd+4S4PCKlpSur0gj2rMW4fP5HQfNlHci8JV8/bG4KuKRAXW56dg1818Hl3pc\n"
100 + "iIrUSRn8uUjH3p9qb+Rb/u3mmVQRyJjN2t/zceNsO8/+Dd808OB9aEwGs8lMT0nn\n"
101 + "ZYaaAqYz1GIY/Ecyx1vfEZEQ1ljo6i/r70C3igbypBUShxSiGsleiVTLOGNA+MN1\n"
102 + "/a/Qh0bkaQyTGqK3bwvzzMeQVqWu2EWTBD/PmND5ExkpRICdv8LBVXfLnpoBr4lL\n"
103 + "hnxn9+e0Ah+t8dS5EKfn44w5bI5PCu2bqxs6RCTxNjcY\n"
104 + "-----END CERTIFICATE-----\n";
105
106 private static final char[] PASSWD2 = new char[] {
107 'b', 'o', 'r', 'e', 'd'
108 };
109 private static final char[] PASSWDK = "cannot be null"
110 .toCharArray();
111 private static final String[] KS_Type = {
112 "jks", "jceks", "pkcs12", "PKCS11KeyStore"
113 };
114 private static final String[] PROVIDERS = {
115 "SUN", "SunJCE", "SunJSSE", "SunPKCS11-Solaris"
116 };
117 private static final String ALIAS_HEAD = "test";
118
119 private static final String CRYPTO_ALG = "PBEWithHmacSHA256AndAES_128";
120
121 public static void main(String args[]) throws Exception {
122 TestKeyStoreBasic jstest = new TestKeyStoreBasic();
123 jstest.run();
124 }
125
126 public void run() throws Exception {
127 for (String provider : PROVIDERS) {
128 try {
129 runTest(provider);
130 System.out.println("Test with provider " + provider + " passed");
131 } catch (java.security.KeyStoreException e) {
132 if (provider.equals("SunPKCS11-Solaris")) {
133 System.out.println("KeyStoreException is expected: "
134 + "PKCS11KeyStore is invalid keystore type: " + e);
135 } else {
136 throw e;
137 }
138 } catch (NoSuchProviderException e) {
139 String osName = System.getProperty("os.name");
140 if (provider.equals("SunPKCS11-Solaris")
141 && !osName.equals("SunOS")) {
142 System.out.println("Skip SunPKCS11-Solaris provider on "
143 + osName);
144 } else {
145 throw e;
146 }
147 }
148 }
149 }
150
151 public void runTest(String provider) throws Exception {
152
153 // load private key
154 // all keystore types should support private keys
155 KeySpec spec = new PKCS8EncodedKeySpec(
156 Base64.getMimeDecoder().decode(PRIVATE_KEY_PKCS8_BASE64));
157 PrivateKey privateKey = KeyFactory.getInstance("RSA")
158 .generatePrivate(spec);
159
160 // load x509 certificate
161 Certificate cert;
162 try (InputStream is = new BufferedInputStream(
163 new ByteArrayInputStream(CERTIFICATE.getBytes()))) {
164 cert = CertificateFactory.getInstance("X.509")
165 .generateCertificate(is);
166 }
167
|
1 /*
2 * Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
95 + "AgMBAAGjgYUwgYIwNAYDVR0fBC0wKzApoCegJYYjbGRhcDovL2xkYXAuaG9zdC5m\n"
96 + "b3IuY3JsZHAvbWFpbi5jcmwwSgYIKwYBBQUHAQEEPjA8MDoGCCsGAQUFBzAChi5s\n"
97 + "ZGFwOi8vbGRhcC5ob3N0LmZvci5haWEvZGM9Um9vdD9jQUNlcnRpZmljYXRlMA0G\n"
98 + "CSqGSIb3DQEBDQUAA4IBAQBWDfZHpuUx0yn5d3+BuztFqoks1MkGdk+USlH0TB1/\n"
99 + "gWWBd+4S4PCKlpSur0gj2rMW4fP5HQfNlHci8JV8/bG4KuKRAXW56dg1818Hl3pc\n"
100 + "iIrUSRn8uUjH3p9qb+Rb/u3mmVQRyJjN2t/zceNsO8/+Dd808OB9aEwGs8lMT0nn\n"
101 + "ZYaaAqYz1GIY/Ecyx1vfEZEQ1ljo6i/r70C3igbypBUShxSiGsleiVTLOGNA+MN1\n"
102 + "/a/Qh0bkaQyTGqK3bwvzzMeQVqWu2EWTBD/PmND5ExkpRICdv8LBVXfLnpoBr4lL\n"
103 + "hnxn9+e0Ah+t8dS5EKfn44w5bI5PCu2bqxs6RCTxNjcY\n"
104 + "-----END CERTIFICATE-----\n";
105
106 private static final char[] PASSWD2 = new char[] {
107 'b', 'o', 'r', 'e', 'd'
108 };
109 private static final char[] PASSWDK = "cannot be null"
110 .toCharArray();
111 private static final String[] KS_Type = {
112 "jks", "jceks", "pkcs12", "PKCS11KeyStore"
113 };
114 private static final String[] PROVIDERS = {
115 "SUN", "SunJCE", "SunJSSE"
116 };
117 private static final String ALIAS_HEAD = "test";
118
119 private static final String CRYPTO_ALG = "PBEWithHmacSHA256AndAES_128";
120
121 public static void main(String args[]) throws Exception {
122 TestKeyStoreBasic jstest = new TestKeyStoreBasic();
123 jstest.run();
124 }
125
126 public void run() throws Exception {
127 for (String provider : PROVIDERS) {
128 runTest(provider);
129 System.out.println("Test with provider " + provider + " passed");
130 }
131 }
132
133 public void runTest(String provider) throws Exception {
134
135 // load private key
136 // all keystore types should support private keys
137 KeySpec spec = new PKCS8EncodedKeySpec(
138 Base64.getMimeDecoder().decode(PRIVATE_KEY_PKCS8_BASE64));
139 PrivateKey privateKey = KeyFactory.getInstance("RSA")
140 .generatePrivate(spec);
141
142 // load x509 certificate
143 Certificate cert;
144 try (InputStream is = new BufferedInputStream(
145 new ByteArrayInputStream(CERTIFICATE.getBytes()))) {
146 cert = CertificateFactory.getInstance("X.509")
147 .generateCertificate(is);
148 }
149
|