jdk/src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/SunPKCS11.java
changeset 31270 e6470b24700d
parent 29923 e3ee0996bedb
child 33991 619bfc4d582d
child 32931 2ba4f06f8684
equal deleted inserted replaced
31269:14968253ce7e 31270:e6470b24700d
    61 
    61 
    62     private static final long serialVersionUID = -1354835039035306505L;
    62     private static final long serialVersionUID = -1354835039035306505L;
    63 
    63 
    64     static final Debug debug = Debug.getInstance("sunpkcs11");
    64     static final Debug debug = Debug.getInstance("sunpkcs11");
    65 
    65 
    66     private static int dummyConfigId;
       
    67 
       
    68     // the PKCS11 object through which we make the native calls
    66     // the PKCS11 object through which we make the native calls
    69     final PKCS11 p11;
    67     final PKCS11 p11;
    70 
    68 
    71     // name of the configuration file
       
    72     private final String configName;
       
    73 
       
    74     // configuration information
    69     // configuration information
    75     final Config config;
    70     final Config config;
    76 
    71 
    77     // id of the PKCS#11 slot we are using
    72     // id of the PKCS#11 slot we are using
    78     final long slotID;
    73     final long slotID;
    93     Token getToken() {
    88     Token getToken() {
    94         return token;
    89         return token;
    95     }
    90     }
    96 
    91 
    97     public SunPKCS11() {
    92     public SunPKCS11() {
    98         super("SunPKCS11-Dummy", 1.9d, "SunPKCS11-Dummy");
    93         super("SunPKCS11", 1.9d, "Unconfigured and unusable PKCS11 provider");
    99         throw new ProviderException
    94         p11 = null;
   100             ("SunPKCS11 requires configuration file argument");
    95         config = null;
   101     }
    96         slotID = 0;
   102 
    97         pHandler = null;
   103     public SunPKCS11(String configName) {
    98         removable = false;
   104         this(checkNull(configName), null);
    99         nssModule = null;
   105     }
   100         nssUseSecmodTrust = false;
   106 
   101         token = null;
   107     public SunPKCS11(InputStream configStream) {
   102         poller = null;
   108         this(getDummyConfigName(), checkNull(configStream));
   103     }
       
   104 
       
   105     @Override
       
   106     public Provider configure(String configArg) throws InvalidParameterException {
       
   107         final String newConfigName = checkNull(configArg);
       
   108         try {
       
   109             return AccessController.doPrivileged(new PrivilegedExceptionAction<Provider>() {
       
   110                 @Override
       
   111                 public Provider run() throws Exception {
       
   112                     return new SunPKCS11(new Config(newConfigName));
       
   113                 }
       
   114             });
       
   115         } catch (PrivilegedActionException pae) {
       
   116             InvalidParameterException ipe =
       
   117                 new InvalidParameterException("Error configuring SunPKCS11 provider");
       
   118             throw (InvalidParameterException) ipe.initCause(pae.getException());
       
   119         }
   109     }
   120     }
   110 
   121 
   111     private static <T> T checkNull(T obj) {
   122     private static <T> T checkNull(T obj) {
   112         if (obj == null) {
   123         if (obj == null) {
   113             throw new NullPointerException();
   124             throw new NullPointerException();
   114         }
   125         }
   115         return obj;
   126         return obj;
   116     }
   127     }
   117 
   128 
   118     private static synchronized String getDummyConfigName() {
   129     // Used by Secmod
   119         int id = ++dummyConfigId;
   130     SunPKCS11(Config c) {
   120         return "---DummyConfig-" + id + "---";
   131         super("SunPKCS11-" + c.getName(), 1.9d, c.getDescription());
   121     }
   132         this.config = c;
   122 
       
   123     /**
       
   124      * @deprecated use new SunPKCS11(String) or new SunPKCS11(InputStream)
       
   125      *         instead
       
   126      */
       
   127     @Deprecated
       
   128     public SunPKCS11(String configName, InputStream configStream) {
       
   129         super("SunPKCS11-" +
       
   130             Config.getConfig(configName, configStream).getName(),
       
   131             1.9d, Config.getConfig(configName, configStream).getDescription());
       
   132         this.configName = configName;
       
   133         this.config = Config.removeConfig(configName);
       
   134 
   133 
   135         if (debug != null) {
   134         if (debug != null) {
   136             System.out.println("SunPKCS11 loading " + configName);
   135             System.out.println("SunPKCS11 loading " + config.getFileName());
   137         }
   136         }
   138 
   137 
   139         String library = config.getLibrary();
   138         String library = config.getLibrary();
   140         String functionList = config.getFunctionList();
   139         String functionList = config.getFunctionList();
   141         long slotID = config.getSlotID();
   140         long slotID = config.getSlotID();
   809     // create the poller thread, if not already active
   808     // create the poller thread, if not already active
   810     private void createPoller() {
   809     private void createPoller() {
   811         if (poller != null) {
   810         if (poller != null) {
   812             return;
   811             return;
   813         }
   812         }
   814         TokenPoller poller = new TokenPoller(this);
   813         final TokenPoller poller = new TokenPoller(this);
   815         Thread t = new ManagedLocalsThread(poller, "Poller " + getName());
   814         Thread t = new ManagedLocalsThread(poller, "Poller " + getName());
   816         t.setDaemon(true);
   815         t.setDaemon(true);
   817         t.setPriority(Thread.MIN_PRIORITY);
   816         t.setPriority(Thread.MIN_PRIORITY);
   818         t.start();
   817         t.start();
   819         this.poller = poller;
   818         this.poller = poller;
  1454 
  1453 
  1455         private final String configName;
  1454         private final String configName;
  1456 
  1455 
  1457         SunPKCS11Rep(SunPKCS11 provider) throws NotSerializableException {
  1456         SunPKCS11Rep(SunPKCS11 provider) throws NotSerializableException {
  1458             providerName = provider.getName();
  1457             providerName = provider.getName();
  1459             configName = provider.configName;
  1458             configName = provider.config.getFileName();
  1460             if (Security.getProvider(providerName) != provider) {
  1459             if (Security.getProvider(providerName) != provider) {
  1461                 throw new NotSerializableException("Only SunPKCS11 providers "
  1460                 throw new NotSerializableException("Only SunPKCS11 providers "
  1462                     + "installed in java.security.Security can be serialized");
  1461                     + "installed in java.security.Security can be serialized");
  1463             }
  1462             }
  1464         }
  1463         }
  1465 
  1464 
  1466         private Object readResolve() throws ObjectStreamException {
  1465         private Object readResolve() throws ObjectStreamException {
  1467             SunPKCS11 p = (SunPKCS11)Security.getProvider(providerName);
  1466             SunPKCS11 p = (SunPKCS11)Security.getProvider(providerName);
  1468             if ((p == null) || (p.configName.equals(configName) == false)) {
  1467             if ((p == null) || (p.config.getFileName().equals(configName) == false)) {
  1469                 throw new NotSerializableException("Could not find "
  1468                 throw new NotSerializableException("Could not find "
  1470                         + providerName + " in installed providers");
  1469                         + providerName + " in installed providers");
  1471             }
  1470             }
  1472             return p;
  1471             return p;
  1473         }
  1472         }