Path: blob/master/src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/SunMSCAPI.java
41159 views
/*1* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.mscapi;2627import java.security.AccessController;28import java.security.PrivilegedAction;29import java.security.Provider;30import java.security.NoSuchAlgorithmException;31import java.security.InvalidParameterException;32import java.security.ProviderException;33import java.util.HashMap;34import java.util.List;3536import static sun.security.util.SecurityConstants.PROVIDER_VER;37import static sun.security.util.SecurityProviderConstants.getAliases;3839/**40* A Cryptographic Service Provider for the Microsoft Crypto API.41*42* @since 1.643*/4445public final class SunMSCAPI extends Provider {4647private static final long serialVersionUID = 8622598936488630849L; //TODO4849private static final String INFO = "Sun's Microsoft Crypto API provider";5051static {52@SuppressWarnings("removal")53var dummy = AccessController.doPrivileged(new PrivilegedAction<Void>() {54public Void run() {55System.loadLibrary("sunmscapi");56return null;57}58});59}60private static class ProviderServiceA extends ProviderService {61ProviderServiceA(Provider p, String type, String algo, String cn,62HashMap<String, String> attrs) {63super(p, type, algo, cn, getAliases(algo), attrs);64}65}6667private static class ProviderService extends Provider.Service {68ProviderService(Provider p, String type, String algo, String cn) {69super(p, type, algo, cn, null, null);70}7172ProviderService(Provider p, String type, String algo, String cn,73List<String> aliases, HashMap<String, String> attrs) {74super(p, type, algo, cn, aliases, attrs);75}7677@Override78public Object newInstance(Object ctrParamObj)79throws NoSuchAlgorithmException {80String type = getType();81if (ctrParamObj != null) {82throw new InvalidParameterException83("constructorParameter not used with " + type +84" engines");85}86String algo = getAlgorithm();87try {88if (type.equals("SecureRandom")) {89if (algo.equals("Windows-PRNG")) {90return new PRNG();91}92} else if (type.equals("KeyStore")) {93if (algo.equals("Windows-MY")) {94return new CKeyStore.MY();95} else if (algo.equals("Windows-ROOT")) {96return new CKeyStore.ROOT();97}98} else if (type.equals("Signature")) {99if (algo.equals("NONEwithRSA")) {100return new CSignature.NONEwithRSA();101} else if (algo.equals("SHA1withRSA")) {102return new CSignature.SHA1withRSA();103} else if (algo.equals("SHA256withRSA")) {104return new CSignature.SHA256withRSA();105} else if (algo.equals("SHA384withRSA")) {106return new CSignature.SHA384withRSA();107} else if (algo.equals("SHA512withRSA")) {108return new CSignature.SHA512withRSA();109} else if (algo.equals("MD5withRSA")) {110return new CSignature.MD5withRSA();111} else if (algo.equals("MD2withRSA")) {112return new CSignature.MD2withRSA();113} else if (algo.equals("RSASSA-PSS")) {114return new CSignature.PSS();115} else if (algo.equals("SHA1withECDSA")) {116return new CSignature.SHA1withECDSA();117} else if (algo.equals("SHA224withECDSA")) {118return new CSignature.SHA224withECDSA();119} else if (algo.equals("SHA256withECDSA")) {120return new CSignature.SHA256withECDSA();121} else if (algo.equals("SHA384withECDSA")) {122return new CSignature.SHA384withECDSA();123} else if (algo.equals("SHA512withECDSA")) {124return new CSignature.SHA512withECDSA();125}126} else if (type.equals("KeyPairGenerator")) {127if (algo.equals("RSA")) {128return new CKeyPairGenerator.RSA();129}130} else if (type.equals("Cipher")) {131if (algo.equals("RSA") ||132algo.equals("RSA/ECB/PKCS1Padding")) {133return new CRSACipher();134}135}136} catch (Exception ex) {137throw new NoSuchAlgorithmException138("Error constructing " + type + " for " +139algo + " using SunMSCAPI", ex);140}141throw new ProviderException("No impl for " + algo +142" " + type);143}144}145146@SuppressWarnings("removal")147public SunMSCAPI() {148super("SunMSCAPI", PROVIDER_VER, INFO);149150final Provider p = this;151AccessController.doPrivileged(new PrivilegedAction<Void>() {152public Void run() {153/*154* Secure random155*/156HashMap<String, String> srattrs = new HashMap<>(1);157srattrs.put("ThreadSafe", "true");158putService(new ProviderService(p, "SecureRandom",159"Windows-PRNG", "sun.security.mscapi.PRNG",160null, srattrs));161162/*163* Key store164*/165putService(new ProviderService(p, "KeyStore",166"Windows-MY", "sun.security.mscapi.CKeyStore$MY"));167putService(new ProviderService(p, "KeyStore",168"Windows-ROOT", "sun.security.mscapi.CKeyStore$ROOT"));169170/*171* Signature engines172*/173HashMap<String, String> attrs = new HashMap<>(1);174attrs.put("SupportedKeyClasses", "sun.security.mscapi.CKey");175176// NONEwithRSA must be supplied with a pre-computed message digest.177// Only the following digest algorithms are supported: MD5, SHA-1,178// SHA-256, SHA-384, SHA-512 and a special-purpose digest179// algorithm which is a concatenation of SHA-1 and MD5 digests.180putService(new ProviderService(p, "Signature",181"NONEwithRSA", "sun.security.mscapi.CSignature$NONEwithRSA",182null, attrs));183putService(new ProviderService(p, "Signature",184"SHA1withRSA", "sun.security.mscapi.CSignature$SHA1withRSA",185null, attrs));186putService(new ProviderServiceA(p, "Signature",187"SHA256withRSA",188"sun.security.mscapi.CSignature$SHA256withRSA",189attrs));190putService(new ProviderServiceA(p, "Signature",191"SHA384withRSA",192"sun.security.mscapi.CSignature$SHA384withRSA",193attrs));194putService(new ProviderServiceA(p, "Signature",195"SHA512withRSA",196"sun.security.mscapi.CSignature$SHA512withRSA",197attrs));198putService(new ProviderServiceA(p, "Signature",199"RSASSA-PSS", "sun.security.mscapi.CSignature$PSS",200attrs));201putService(new ProviderService(p, "Signature",202"MD5withRSA", "sun.security.mscapi.CSignature$MD5withRSA",203null, attrs));204putService(new ProviderService(p, "Signature",205"MD2withRSA", "sun.security.mscapi.CSignature$MD2withRSA",206null, attrs));207putService(new ProviderServiceA(p, "Signature",208"SHA1withECDSA",209"sun.security.mscapi.CSignature$SHA1withECDSA",210attrs));211putService(new ProviderServiceA(p, "Signature",212"SHA224withECDSA",213"sun.security.mscapi.CSignature$SHA224withECDSA",214attrs));215putService(new ProviderServiceA(p, "Signature",216"SHA256withECDSA",217"sun.security.mscapi.CSignature$SHA256withECDSA",218attrs));219putService(new ProviderServiceA(p, "Signature",220"SHA384withECDSA",221"sun.security.mscapi.CSignature$SHA384withECDSA",222attrs));223putService(new ProviderServiceA(p, "Signature",224"SHA512withECDSA",225"sun.security.mscapi.CSignature$SHA512withECDSA",226attrs));227/*228* Key Pair Generator engines229*/230attrs.clear();231attrs.put("KeySize", "16384");232putService(new ProviderService(p, "KeyPairGenerator",233"RSA", "sun.security.mscapi.CKeyPairGenerator$RSA",234null, attrs));235236/*237* Cipher engines238*/239attrs.clear();240attrs.put("SupportedModes", "ECB");241attrs.put("SupportedPaddings", "PKCS1PADDING");242attrs.put("SupportedKeyClasses", "sun.security.mscapi.CKey");243putService(new ProviderService(p, "Cipher",244"RSA", "sun.security.mscapi.CRSACipher",245null, attrs));246putService(new ProviderService(p, "Cipher",247"RSA/ECB/PKCS1Padding", "sun.security.mscapi.CRSACipher",248null, attrs));249return null;250}251});252}253}254255256