11/*
2- * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
3131import javax .security .auth .login .LoginException ;
3232import java .io .IOException ;
3333import java .lang .ref .WeakReference ;
34- import java .security .KeyStore ;
35- import java .security .Provider ;
36- import java .security .Security ;
34+ import java .security .*;
3735import java .util .Iterator ;
36+ import java .util .PropertyPermission ;
3837import java .util .ServiceConfigurationError ;
3938import java .util .ServiceLoader ;
4039
@@ -44,19 +43,28 @@ public class MultipleLogins {
4443 private static final String KS_TYPE = "PKCS11" ;
4544 private static final int NUM_PROVIDERS = 20 ;
4645 private static final SunPKCS11 [] providers = new SunPKCS11 [NUM_PROVIDERS ];
47-
46+ static final Policy DEFAULT_POLICY = Policy . getPolicy ();
4847
4948 public static void main (String [] args ) throws Exception {
49+ String nssConfig = PKCS11Test .getNssConfig ();
50+ if (nssConfig == null ) {
51+ // No test framework support yet. Ignore
52+ System .out .println ("No NSS config found. Skipping." );
53+ return ;
54+ }
55+
5056 for (int i =0 ; i < NUM_PROVIDERS ; i ++) {
51- String nssConfig = PKCS11Test .getNssConfig ();
52- if (nssConfig == null ) {
53- // No test framework support yet. Ignore
54- System .out .println ("No NSS config found. Skipping." );
55- return ;
56- }
57- providers [i ] =
58- (SunPKCS11 )PKCS11Test .newPKCS11Provider ()
59- .configure (nssConfig );
57+ // loop to set up test without security manger
58+ providers [i ] = (SunPKCS11 )PKCS11Test .newPKCS11Provider ();
59+ }
60+
61+ if (args .length > 0 ) {
62+ Policy .setPolicy (new SimplePolicy ());
63+ System .setSecurityManager (new SecurityManager ());
64+ }
65+
66+ for (int i =0 ; i < NUM_PROVIDERS ; i ++) {
67+ providers [i ] = (SunPKCS11 )providers [i ].configure (nssConfig );
6068 Security .addProvider (providers [i ]);
6169 test (providers [i ]);
6270 }
@@ -92,7 +100,6 @@ public static void main(String[] args) throws Exception {
92100
93101 private static void test (SunPKCS11 p ) throws Exception {
94102 KeyStore ks = KeyStore .getInstance (KS_TYPE , p );
95-
96103 p .setCallbackHandler (new PasswordCallbackHandler ());
97104 try {
98105 ks .load (null , (char []) null );
@@ -117,6 +124,23 @@ private static void test(SunPKCS11 p) throws Exception {
117124 }
118125 }
119126
127+ static final class SimplePolicy extends Policy {
128+
129+ final Permissions perms = new Permissions ();
130+ SimplePolicy () {
131+ perms .add (new PropertyPermission ("*" , "read, write" ));
132+ perms .add (new SecurityPermission ("authProvider.*" ));
133+ perms .add (new SecurityPermission ("insertProvider.*" ));
134+ perms .add (new SecurityPermission ("removeProvider.*" ));
135+ }
136+
137+ @ Override
138+ public boolean implies (ProtectionDomain domain , Permission permission ) {
139+ return perms .implies (permission ) ||
140+ DEFAULT_POLICY .implies (domain , permission );
141+ }
142+ }
143+
120144 public static class PasswordCallbackHandler implements CallbackHandler {
121145 public void handle (Callback [] callbacks )
122146 throws IOException , UnsupportedCallbackException {
0 commit comments