author | asmotrak |
Mon, 14 Sep 2015 19:54:58 +0300 | |
changeset 32635 | d7e4ba3c2e0d |
parent 30046 | cf2c86e1819e |
child 35379 | 1e8e336ef66b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
32635
d7e4ba3c2e0d
8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
30046
diff
changeset
|
2 |
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 6273877 6322208 6275523 |
|
32635
d7e4ba3c2e0d
8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
30046
diff
changeset
|
27 |
* @summary make sure we can access the NSS softtoken KeyStore |
d7e4ba3c2e0d
8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
30046
diff
changeset
|
28 |
* and use a private key |
2 | 29 |
* @author Andreas Sterbenz |
30 |
* @library .. |
|
10328 | 31 |
* @run main/othervm GetPrivateKey |
2 | 32 |
*/ |
33 |
||
34 |
import java.util.*; |
|
35 |
||
36 |
import java.security.*; |
|
37 |
import java.security.KeyStore.*; |
|
38 |
import java.security.cert.*; |
|
39 |
||
40 |
public class GetPrivateKey extends SecmodTest { |
|
41 |
||
42 |
public static void main(String[] args) throws Exception { |
|
43 |
if (initSecmod() == false) { |
|
44 |
return; |
|
45 |
} |
|
46 |
||
47 |
String configName = BASE + SEP + "nss.cfg"; |
|
48 |
Provider p = getSunPKCS11(configName); |
|
49 |
||
50 |
System.out.println(p); |
|
51 |
Security.addProvider(p); |
|
32635
d7e4ba3c2e0d
8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
30046
diff
changeset
|
52 |
KeyStore ks = KeyStore.getInstance(PKCS11, p); |
2 | 53 |
ks.load(null, password); |
32635
d7e4ba3c2e0d
8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
30046
diff
changeset
|
54 |
Collection<String> aliases = new TreeSet<>( |
d7e4ba3c2e0d
8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
30046
diff
changeset
|
55 |
Collections.list(ks.aliases())); |
2 | 56 |
System.out.println("entries: " + aliases.size()); |
57 |
System.out.println(aliases); |
|
58 |
||
59 |
PrivateKey privateKey = (PrivateKey)ks.getKey(keyAlias, password); |
|
60 |
System.out.println(privateKey); |
|
61 |
||
32635
d7e4ba3c2e0d
8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
30046
diff
changeset
|
62 |
byte[] data = generateData(1024); |
2 | 63 |
|
64 |
System.out.println("Signing..."); |
|
65 |
Signature signature = Signature.getInstance("MD5withRSA"); |
|
66 |
signature.initSign(privateKey); |
|
67 |
signature.update(data); |
|
68 |
byte[] sig = signature.sign(); |
|
69 |
||
32635
d7e4ba3c2e0d
8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
30046
diff
changeset
|
70 |
X509Certificate[] chain = |
d7e4ba3c2e0d
8048622: Enhance tests for PKCS11 keystores with NSS
asmotrak
parents:
30046
diff
changeset
|
71 |
(X509Certificate[]) ks.getCertificateChain(keyAlias); |
2 | 72 |
signature.initVerify(chain[0].getPublicKey()); |
73 |
signature.update(data); |
|
74 |
boolean ok = signature.verify(sig); |
|
75 |
if (ok == false) { |
|
76 |
throw new Exception("Signature verification error"); |
|
77 |
} |
|
78 |
||
79 |
System.out.println("OK"); |
|
80 |
||
81 |
} |
|
82 |
||
83 |
} |