src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/SunMSCAPI.java

Print this page
7191662: JCE providers should be located via ServiceLoader
   1 /*
   2  * Copyright (c) 2005, 2014, 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
  23  * questions.
  24  */
  25 
  26 package sun.security.mscapi;
  27 
  28 import java.security.AccessController;
  29 import java.security.PrivilegedAction;
  30 import java.security.Provider;



  31 import java.util.HashMap;
  32 import java.util.Map;
  33 
  34 /**
  35  * A Cryptographic Service Provider for the Microsoft Crypto API.
  36  *
  37  * @since 1.6
  38  */
  39 
  40 public final class SunMSCAPI extends Provider {
  41 
  42     private static final long serialVersionUID = 8622598936488630849L; //TODO
  43 
  44     private static final String INFO = "Sun's Microsoft Crypto API provider";
  45 
  46     static {
  47         AccessController.doPrivileged(new PrivilegedAction<Void>() {
  48             public Void run() {
  49                 System.loadLibrary("sunmscapi");
  50                 return null;
  51             }
  52         });
  53     }
  54 




































































  55     public SunMSCAPI() {
  56         super("SunMSCAPI", 1.9d, INFO);
  57 
  58         // if there is no security manager installed, put directly into
  59         // the provider. Otherwise, create a temporary map and use a
  60         // doPrivileged() call at the end to transfer the contents
  61         final Map<Object, Object> map =
  62                 (System.getSecurityManager() == null)
  63                 ? this : new HashMap<Object, Object>();
  64 
  65         /*
  66          * Secure random
  67          */
  68         map.put("SecureRandom.Windows-PRNG", "sun.security.mscapi.PRNG");

  69 
  70         /*
  71          * Key store
  72          */
  73         map.put("KeyStore.Windows-MY", "sun.security.mscapi.KeyStore$MY");
  74         map.put("KeyStore.Windows-ROOT", "sun.security.mscapi.KeyStore$ROOT");


  75 
  76         /*
  77          * Signature engines
  78          */



  79         // NONEwithRSA must be supplied with a pre-computed message digest.
  80         // Only the following digest algorithms are supported: MD5, SHA-1,
  81         // SHA-256, SHA-384, SHA-512 and a special-purpose digest
  82         // algorithm which is a concatenation of SHA-1 and MD5 digests.
  83         map.put("Signature.NONEwithRSA",
  84             "sun.security.mscapi.RSASignature$Raw");
  85         map.put("Signature.SHA1withRSA",
  86             "sun.security.mscapi.RSASignature$SHA1");
  87         map.put("Signature.SHA256withRSA",
  88             "sun.security.mscapi.RSASignature$SHA256");
  89         map.put("Alg.Alias.Signature.1.2.840.113549.1.1.11",     "SHA256withRSA");
  90         map.put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.11", "SHA256withRSA");
  91         map.put("Signature.SHA384withRSA",
  92             "sun.security.mscapi.RSASignature$SHA384");
  93         map.put("Alg.Alias.Signature.1.2.840.113549.1.1.12",     "SHA384withRSA");
  94         map.put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.12", "SHA384withRSA");












  95 
  96         map.put("Signature.SHA512withRSA",
  97             "sun.security.mscapi.RSASignature$SHA512");
  98         map.put("Alg.Alias.Signature.1.2.840.113549.1.1.13",     "SHA512withRSA");
  99         map.put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.13", "SHA512withRSA");
 100 
 101         map.put("Signature.MD5withRSA",
 102             "sun.security.mscapi.RSASignature$MD5");
 103         map.put("Signature.MD2withRSA",
 104             "sun.security.mscapi.RSASignature$MD2");
 105 
 106         // supported key classes
 107         map.put("Signature.NONEwithRSA SupportedKeyClasses",
 108             "sun.security.mscapi.Key");
 109         map.put("Signature.SHA1withRSA SupportedKeyClasses",
 110             "sun.security.mscapi.Key");
 111         map.put("Signature.SHA256withRSA SupportedKeyClasses",
 112             "sun.security.mscapi.Key");
 113         map.put("Signature.SHA384withRSA SupportedKeyClasses",
 114             "sun.security.mscapi.Key");
 115         map.put("Signature.SHA512withRSA SupportedKeyClasses",
 116             "sun.security.mscapi.Key");
 117         map.put("Signature.MD5withRSA SupportedKeyClasses",
 118             "sun.security.mscapi.Key");
 119         map.put("Signature.MD2withRSA SupportedKeyClasses",
 120             "sun.security.mscapi.Key");
 121 
 122         /*
 123          * Key Pair Generator engines
 124          */
 125         map.put("KeyPairGenerator.RSA",
 126             "sun.security.mscapi.RSAKeyPairGenerator");
 127         map.put("KeyPairGenerator.RSA KeySize", "1024");


 128 
 129         /*
 130          * Cipher engines
 131          */
 132         map.put("Cipher.RSA", "sun.security.mscapi.RSACipher");
 133         map.put("Cipher.RSA/ECB/PKCS1Padding",
 134             "sun.security.mscapi.RSACipher");
 135         map.put("Cipher.RSA SupportedModes", "ECB");
 136         map.put("Cipher.RSA SupportedPaddings", "PKCS1PADDING");
 137         map.put("Cipher.RSA SupportedKeyClasses", "sun.security.mscapi.Key");
 138 
 139         if (map != this) {
 140             final Provider provider = this;
 141             PrivilegedAction<Void> putAllAction = () -> {
 142                 provider.putAll(map);
 143                 return null;
 144             };
 145             AccessController.doPrivileged(putAllAction);
 146         }

 147     }
 148 }
   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
  23  * questions.
  24  */
  25 
  26 package sun.security.mscapi;
  27 
  28 import java.security.AccessController;
  29 import java.security.PrivilegedAction;
  30 import java.security.Provider;
  31 import java.security.NoSuchAlgorithmException;
  32 import java.security.InvalidParameterException;
  33 import java.security.ProviderException;
  34 import java.util.HashMap;
  35 import java.util.Arrays;
  36 
  37 /**
  38  * A Cryptographic Service Provider for the Microsoft Crypto API.
  39  *
  40  * @since 1.6
  41  */
  42 
  43 public final class SunMSCAPI extends Provider {
  44 
  45     private static final long serialVersionUID = 8622598936488630849L; //TODO
  46 
  47     private static final String INFO = "Sun's Microsoft Crypto API provider";
  48 
  49     static {
  50         AccessController.doPrivileged(new PrivilegedAction<Void>() {
  51             public Void run() {
  52                 System.loadLibrary("sunmscapi");
  53                 return null;
  54             }
  55         });
  56     }
  57 
  58     private static final class ProviderService extends Provider.Service {
  59         ProviderService(Provider p, String type, String algo, String cn) {
  60             super(p, type, algo, cn, null, null);
  61         }
  62 
  63         ProviderService(Provider p, String type, String algo, String cn,
  64             String[] aliases, HashMap<String, String> attrs) {
  65             super(p, type, algo, cn,
  66                   (aliases == null? null : Arrays.asList(aliases)), attrs);
  67         }
  68 
  69         @Override
  70         public Object newInstance(Object ctrParamObj)
  71             throws NoSuchAlgorithmException {
  72             String type = getType();
  73             if (ctrParamObj != null) {
  74                 throw new InvalidParameterException
  75                     ("constructorParameter not used with " + type +
  76                      " engines");
  77             }
  78             String algo = getAlgorithm();
  79             try {
  80                 if (type.equals("SecureRandom")) {
  81                     if (algo.equals("Windows-PRNG")) {
  82                         return new PRNG();
  83                     }
  84                 } else if (type.equals("KeyStore")) {
  85                     if (algo.equals("Windows-MY")) {
  86                         return new KeyStore.MY();
  87                     } else if (algo.equals("Windows-ROOT")) {
  88                         return new KeyStore.ROOT();
  89                     }
  90                 } else if (type.equals("Signature")) {
  91                     if (algo.equals("NONEwithRSA")) {
  92                         return new RSASignature.Raw();
  93                     } else if (algo.equals("SHA1withRSA")) {
  94                         return new RSASignature.SHA1();
  95                     } else if (algo.equals("SHA256withRSA")) {
  96                         return new RSASignature.SHA256();
  97                     } else if (algo.equals("SHA384withRSA")) {
  98                         return new RSASignature.SHA384();
  99                     } else if (algo.equals("SHA512withRSA")) {
 100                         return new RSASignature.SHA512();
 101                     } else if (algo.equals("MD5withRSA")) {
 102                         return new RSASignature.MD5();
 103                     } else if (algo.equals("MD2withRSA")) {
 104                         return new RSASignature.MD2();
 105                     }
 106                 } else if (type.equals("KeyPairGenerator")) {
 107                     if (algo.equals("RSA")) {
 108                         return new RSAKeyPairGenerator();
 109                     }
 110                 } else if (type.equals("Cipher")) {
 111                     if (algo.equals("RSA") ||
 112                         algo.equals("RSA/ECB/PKCS1Padding")) {
 113                         return new RSACipher();
 114                     }
 115                 }
 116             } catch (Exception ex) {
 117                 throw new NoSuchAlgorithmException
 118                     ("Error constructing " + type + " for " +
 119                     algo + " using SunMSCAPI", ex);
 120             }
 121             throw new ProviderException("No impl for " + algo +
 122                 " " + type);
 123         }
 124     }
 125 
 126     public SunMSCAPI() {
 127         super("SunMSCAPI", 1.9d, INFO);
 128 
 129         final Provider p = this;
 130         AccessController.doPrivileged(new PrivilegedAction<Void>() {
 131             public Void run() {




 132                 /*
 133                  * Secure random
 134                  */
 135                 putService(new ProviderService(p, "SecureRandom",
 136                            "Windows-PRNG", "sun.security.mscapi.PRNG"));
 137 
 138                 /*
 139                  * Key store
 140                  */
 141                 putService(new ProviderService(p, "KeyStore",
 142                            "Windows-MY", "sun.security.mscapi.KeyStore$MY"));
 143                 putService(new ProviderService(p, "KeyStore",
 144                            "Windows-ROOT", "sun.security.mscapi.KeyStore$ROOT"));
 145 
 146                 /*
 147                  * Signature engines
 148                  */
 149                 HashMap<String, String> attrs = new HashMap<>(1);
 150                 attrs.put("SupportedKeyClasses", "sun.security.mscapi.Key");
 151 
 152                 // NONEwithRSA must be supplied with a pre-computed message digest.
 153                 // Only the following digest algorithms are supported: MD5, SHA-1,
 154                 // SHA-256, SHA-384, SHA-512 and a special-purpose digest
 155                 // algorithm which is a concatenation of SHA-1 and MD5 digests.
 156                 putService(new ProviderService(p, "Signature",
 157                            "NONEwithRSA", "sun.security.mscapi.RSASignature$Raw",
 158                            null, attrs));
 159                 putService(new ProviderService(p, "Signature",
 160                            "SHA1withRSA", "sun.security.mscapi.RSASignature$SHA1",
 161                            null, attrs));
 162                 putService(new ProviderService(p, "Signature",
 163                            "SHA256withRSA", "sun.security.mscapi.RSASignature$SHA256",
 164                            new String[] { "1.2.840.113549.1.1.11", "OID.1.2.840.113549.1.1.11" },
 165                            attrs));
 166                 putService(new ProviderService(p, "Signature",
 167                            "SHA384withRSA", "sun.security.mscapi.RSASignature$SHA384",
 168                            new String[] { "1.2.840.113549.1.1.12", "OID.1.2.840.113549.1.1.12" },
 169                            attrs));
 170                 putService(new ProviderService(p, "Signature",
 171                            "SHA512withRSA", "sun.security.mscapi.RSASignature$SHA512",
 172                            new String[] { "1.2.840.113549.1.1.13", "OID.1.2.840.113549.1.1.13" },
 173                            attrs));
 174                 putService(new ProviderService(p, "Signature",
 175                            "MD5withRSA", "sun.security.mscapi.RSASignature$MD5",
 176                            null, attrs));
 177                 putService(new ProviderService(p, "Signature",
 178                            "MD2withRSA", "sun.security.mscapi.RSASignature$MD2",
 179                            null, attrs));
 180 


























 181                 /*
 182                  * Key Pair Generator engines
 183                  */
 184                 attrs.clear();
 185                 attrs.put("KeySize", "1024");
 186                 putService(new ProviderService(p, "KeyPairGenerator",
 187                            "RSA", "sun.security.mscapi.RSAKeyPairGenerator",
 188                            null, attrs));
 189 
 190                 /*
 191                  * Cipher engines
 192                  */
 193                 attrs.clear();
 194                 attrs.put("SupportedModes", "ECB");
 195                 attrs.put("SupportedPaddings", "PKCS1PADDING");
 196                 attrs.put("SupportedKeyClasses", "sun.security.mscapi.Key");
 197                 putService(new ProviderService(p, "Cipher",
 198                            "RSA", "sun.security.mscapi.RSACipher",
 199                            null, attrs));
 200                 putService(new ProviderService(p, "Cipher",
 201                            "RSA/ECB/PKCS1Padding", "sun.security.mscapi.RSACipher",
 202                            null, attrs));

 203                 return null;


 204             }
 205         });
 206     }
 207 }