1
1
/*
2
- * Copyright (c) 2003, 2019 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2003, 2021 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
21
21
* questions.
22
22
*/
23
23
24
+ /* @test
25
+ * @bug 4938185
26
+ * @summary KeyStore support for NSS cert/key databases
27
+ * To run manually:
28
+ * set environment variable:
29
+ * <token> [activcard|ibutton|nss|sca1000]
30
+ * <command> [list|basic]
31
+ *
32
+ * Note:
33
+ * . 'list' lists the token aliases
34
+ * . 'basic' does not run with activcard,
35
+ * @library /test/lib ..
36
+ * @run testng/othervm Basic
37
+ */
38
+
24
39
import java .io .*;
40
+ import java .nio .file .Path ;
25
41
import java .util .*;
26
42
27
43
import java .security .KeyStore ;
28
44
import java .security .KeyStoreException ;
29
45
import java .security .KeyFactory ;
30
- import java .security .KeyPairGenerator ;
31
- import java .security .KeyPair ;
32
- import java .security .SecureRandom ;
33
- import java .security .AuthProvider ;
34
46
import java .security .PrivateKey ;
35
47
import java .security .Provider ;
36
- import java .security .ProviderException ;
37
48
import java .security .Signature ;
38
49
import java .security .Security ;
39
50
44
55
import javax .crypto .SecretKey ;
45
56
46
57
import javax .security .auth .Subject ;
47
- import javax .security .auth .login .LoginException ;
48
58
49
59
import com .sun .security .auth .module .*;
50
60
import com .sun .security .auth .callback .*;
61
+ import org .testng .annotations .BeforeClass ;
62
+ import org .testng .annotations .Test ;
51
63
52
64
53
65
public class Basic extends PKCS11Test {
54
66
55
- private static final char SEP = File . separatorChar ;
56
-
57
- private static String DIR = System . getProperty ( "DIR" );
67
+ private static final Path TEST_DATA_PATH = Path . of ( BASE )
68
+ . resolve ( "BasicData" );
69
+ private static final String DIR = TEST_DATA_PATH . toString ( );
58
70
private static char [] tokenPwd ;
59
71
private static final char [] ibuttonPwd =
60
72
new char [0 ];
@@ -88,6 +100,22 @@ public class Basic extends PKCS11Test {
88
100
private static final String KS_TYPE = "PKCS11" ;
89
101
private static Provider provider ;
90
102
103
+ @ BeforeClass
104
+ public void setUp () throws Exception {
105
+ copyNssCertKeyToClassesDir ();
106
+ setCommonSystemProps ();
107
+ System .setProperty ("CUSTOM_P11_CONFIG" ,
108
+ TEST_DATA_PATH .resolve ("p11-nss.txt" ).toString ());
109
+ System .setProperty ("TOKEN" , "nss" );
110
+ System .setProperty ("TEST" , "basic" );
111
+ }
112
+
113
+ @ Test
114
+ public void testBasic () throws Exception {
115
+ String [] args = {"sm" , "Basic.policy" };
116
+ main (new Basic (), args );
117
+ }
118
+
91
119
private static class FooEntry implements KeyStore .Entry { }
92
120
93
121
private static class P11SecretKey implements SecretKey {
@@ -102,10 +130,6 @@ public P11SecretKey(String alg, int length) {
102
130
public byte [] getEncoded () { return new byte [length /8 ]; }
103
131
}
104
132
105
- public static void main (String [] args ) throws Exception {
106
- main (new Basic (), args );
107
- }
108
-
109
133
public void main (Provider p ) throws Exception {
110
134
111
135
this .provider = p ;
@@ -136,17 +160,17 @@ public void main(Provider p) throws Exception {
136
160
137
161
// get cert chains for private keys
138
162
CertificateFactory cf = CertificateFactory .getInstance ("X.509" , "SUN" );
139
- Certificate caCert = ( X509Certificate ) cf .generateCertificate
163
+ Certificate caCert = cf .generateCertificate
140
164
(new FileInputStream (new File (DIR , "ca.cert" )));
141
- Certificate ca2Cert = ( X509Certificate ) cf .generateCertificate
165
+ Certificate ca2Cert = cf .generateCertificate
142
166
(new FileInputStream (new File (DIR , "ca2.cert" )));
143
- Certificate pk1cert = ( X509Certificate ) cf .generateCertificate
167
+ Certificate pk1cert = cf .generateCertificate
144
168
(new FileInputStream (new File (DIR , "pk1.cert" )));
145
- Certificate pk1cert2 = ( X509Certificate ) cf .generateCertificate
169
+ Certificate pk1cert2 = cf .generateCertificate
146
170
(new FileInputStream (new File (DIR , "pk1.cert2" )));
147
- Certificate pk2cert = ( X509Certificate ) cf .generateCertificate
171
+ Certificate pk2cert = cf .generateCertificate
148
172
(new FileInputStream (new File (DIR , "pk2.cert" )));
149
- Certificate pk3cert = ( X509Certificate ) cf .generateCertificate
173
+ Certificate pk3cert = cf .generateCertificate
150
174
(new FileInputStream (new File (DIR , "pk3.cert" )));
151
175
chain1 = new Certificate [] { pk1cert , caCert };
152
176
chain2 = new Certificate [] { pk2cert , caCert };
@@ -373,12 +397,12 @@ private static void module() throws Exception {
373
397
374
398
KeyStoreLoginModule m = new KeyStoreLoginModule ();
375
399
Subject s = new Subject ();
376
- Map options = new HashMap ();
400
+ Map < String , String > options = new HashMap <> ();
377
401
options .put ("keyStoreURL" , "NONE" );
378
402
options .put ("keyStoreType" , KS_TYPE );
379
403
options .put ("keyStoreProvider" , KS_PROVIDER );
380
404
options .put ("debug" , "true" );
381
- m .initialize (s , new TextCallbackHandler (), new HashMap (), options );
405
+ m .initialize (s , new TextCallbackHandler (), new HashMap <> (), options );
382
406
m .login ();
383
407
m .commit ();
384
408
System .out .println ("authenticated subject = " + s );
1 commit comments
openjdk-notifier[bot] commentedon Jun 1, 2021
Review
Issues