src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/Secmod.java

Print this page
7191662: JCE providers should be located via ServiceLoader
   1 /*
   2  * Copyright (c) 2005, 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 497         public synchronized Provider getProvider() {
 498             if (provider == null) {
 499                 provider = newProvider();
 500             }
 501             return provider;
 502         }
 503 
 504         synchronized boolean hasInitializedProvider() {
 505             return provider != null;
 506         }
 507 
 508         void setProvider(SunPKCS11 p) {
 509             if (provider != null) {
 510                 throw new ProviderException("Secmod provider already initialized");
 511             }
 512             provider = p;
 513         }
 514 
 515         private SunPKCS11 newProvider() {
 516             try {
 517                 InputStream in = new ByteArrayInputStream(config.getBytes("UTF8"));
 518                 return new SunPKCS11(in);
 519             } catch (Exception e) {
 520                 // XXX
 521                 throw new ProviderException(e);
 522             }
 523         }
 524 
 525         synchronized void setTrust(Token token, X509Certificate cert) {
 526             Bytes bytes = new Bytes(getDigest(cert, "SHA-1"));
 527             TrustAttributes attr = getTrust(bytes);
 528             if (attr == null) {
 529                 attr = new TrustAttributes(token, cert, bytes, CKT_NETSCAPE_TRUSTED_DELEGATOR);
 530                 trust.put(bytes, attr);
 531             } else {
 532                 // does it already have the correct trust settings?
 533                 if (attr.isTrusted(TrustType.ALL) == false) {
 534                     // XXX not yet implemented
 535                     throw new ProviderException("Cannot change existing trust attributes");
 536                 }
 537             }
 538         }


   1 /*
   2  * Copyright (c) 2005, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 497         public synchronized Provider getProvider() {
 498             if (provider == null) {
 499                 provider = newProvider();
 500             }
 501             return provider;
 502         }
 503 
 504         synchronized boolean hasInitializedProvider() {
 505             return provider != null;
 506         }
 507 
 508         void setProvider(SunPKCS11 p) {
 509             if (provider != null) {
 510                 throw new ProviderException("Secmod provider already initialized");
 511             }
 512             provider = p;
 513         }
 514 
 515         private SunPKCS11 newProvider() {
 516             try {
 517                 return new SunPKCS11(new Config("--" + config));

 518             } catch (Exception e) {
 519                 // XXX
 520                 throw new ProviderException(e);
 521             }
 522         }
 523 
 524         synchronized void setTrust(Token token, X509Certificate cert) {
 525             Bytes bytes = new Bytes(getDigest(cert, "SHA-1"));
 526             TrustAttributes attr = getTrust(bytes);
 527             if (attr == null) {
 528                 attr = new TrustAttributes(token, cert, bytes, CKT_NETSCAPE_TRUSTED_DELEGATOR);
 529                 trust.put(bytes, attr);
 530             } else {
 531                 // does it already have the correct trust settings?
 532                 if (attr.isTrusted(TrustType.ALL) == false) {
 533                     // XXX not yet implemented
 534                     throw new ProviderException("Cannot change existing trust attributes");
 535                 }
 536             }
 537         }