001: /*
002: * Copyright 2005-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.security.mscapi;
027:
028: import java.security.AccessController;
029: import java.security.PrivilegedAction;
030: import java.security.Provider;
031: import java.security.ProviderException;
032: import java.util.HashMap;
033: import java.util.Map;
034:
035: import sun.security.action.PutAllAction;
036:
037: /**
038: * A Cryptographic Service Provider for the Microsoft Crypto API.
039: *
040: * @since 1.6
041: */
042:
043: public final class SunMSCAPI extends Provider {
044:
045: private static final long serialVersionUID = 8622598936488630849L; //TODO
046:
047: private static final String INFO = "Sun's Microsoft Crypto API provider";
048:
049: // Quick check to see if we are signed.
050: // The definitive check happens via verifySelfIntegrity() when
051: // we actually try to use encryption.
052: private static final boolean registerEncryption = (SunMSCAPI.class
053: .getSigners() != null);
054:
055: static {
056: AccessController.doPrivileged(new PrivilegedAction() {
057: public Object run() {
058: System.loadLibrary("sunmscapi");
059: return null;
060: }
061: });
062: }
063:
064: public SunMSCAPI() {
065: super ("SunMSCAPI", 1.7d, INFO);
066:
067: // if there is no security manager installed, put directly into
068: // the provider. Otherwise, create a temporary map and use a
069: // doPrivileged() call at the end to transfer the contents
070: final Map map = (System.getSecurityManager() == null) ? (Map) this
071: : new HashMap();
072:
073: /*
074: * Secure random
075: */
076: map
077: .put("SecureRandom.Windows-PRNG",
078: "sun.security.mscapi.PRNG");
079:
080: /*
081: * Key store
082: */
083: map.put("KeyStore.Windows-MY",
084: "sun.security.mscapi.KeyStore$MY");
085: map.put("KeyStore.Windows-ROOT",
086: "sun.security.mscapi.KeyStore$ROOT");
087:
088: /*
089: * Signature engines
090: */
091: map.put("Signature.SHA1withRSA",
092: "sun.security.mscapi.RSASignature$SHA1");
093: map.put("Signature.MD5withRSA",
094: "sun.security.mscapi.RSASignature$MD5");
095: map.put("Signature.MD2withRSA",
096: "sun.security.mscapi.RSASignature$MD2");
097:
098: // supported key classes
099: map.put("Signature.SHA1withRSA SupportedKeyClasses",
100: "sun.security.mscapi.Key");
101: map.put("Signature.MD5withRSA SupportedKeyClasses",
102: "sun.security.mscapi.Key");
103: map.put("Signature.MD2withRSA SupportedKeyClasses",
104: "sun.security.mscapi.Key");
105: map.put("Signature.NONEwithRSA SupportedKeyClasses",
106: "sun.security.mscapi.Key");
107:
108: /*
109: * Key Pair Generator engines
110: */
111: map.put("KeyPairGenerator.RSA",
112: "sun.security.mscapi.RSAKeyPairGenerator");
113: map.put("KeyPairGenerator.RSA KeySize", "1024");
114:
115: /*
116: * Cipher engines
117: * (omit registration of crypto engine when provider is unsigned)
118: */
119: if (registerEncryption) {
120: map.put("Cipher.RSA", "sun.security.mscapi.RSACipher");
121: map.put("Cipher.RSA/ECB/PKCS1Padding",
122: "sun.security.mscapi.RSACipher");
123: map.put("Cipher.RSA SupportedModes", "ECB");
124: map.put("Cipher.RSA SupportedPaddings", "PKCS1PADDING");
125: map.put("Cipher.RSA SupportedKeyClasses",
126: "sun.security.mscapi.Key");
127: }
128:
129: if (map != this ) {
130: AccessController.doPrivileged(new PutAllAction(this , map));
131: }
132: }
133:
134: // set to true once self verification is complete
135: private static volatile boolean integrityVerified;
136:
137: static void verifySelfIntegrity(Class c) {
138: if (integrityVerified) {
139: return;
140: }
141: doVerifySelfIntegrity(c);
142: }
143:
144: private static synchronized void doVerifySelfIntegrity(Class c) {
145: /* RSA CERTIFICATE USED TO SIGN SUNMSCAPI.JAR
146: Owner: CN=Sun Microsystems Inc, OU=Java Software Code Signing,
147: O=Sun Microsystems Inc
148: Issuer: CN=JCE Code Signing CA, OU=Java Software Code Signing,
149: O=Sun Microsystems Inc, L=Palo Alto, ST=CA, C=US
150: Serial number: 21f
151: Valid from: Wed Nov 23 14:49:41 PST 2005 until: Sat Nov 27 14:49:41 PST 2010
152: Certificate fingerprints:
153: MD5: 8E:42:68:F7:22:2F:57:F3:F0:F0:19:CE:AE:F3:8F:60
154: SHA1: CD:3E:0C:8A:32:E3:EF:40:21:C8:5B:34:98:9A:66:CF:E1:60:25:48
155: */
156: final String CERT = "-----BEGIN CERTIFICATE-----\n"
157: + "MIICnTCCAlugAwIBAgICAh8wCwYHKoZIzjgEAwUAMIGQMQswCQYDVQQGEwJVUzEL"
158: + "MAkGA1UECBMCQ0ExEjAQBgNVBAcTCVBhbG8gQWx0bzEdMBsGA1UEChMUU3VuIE1p"
159: + "Y3Jvc3lzdGVtcyBJbmMxIzAhBgNVBAsTGkphdmEgU29mdHdhcmUgQ29kZSBTaWdu"
160: + "aW5nMRwwGgYDVQQDExNKQ0UgQ29kZSBTaWduaW5nIENBMB4XDTA1MTEyMzIyNDk0"
161: + "MVoXDTEwMTEyNzIyNDk0MVowYzEdMBsGA1UEChMUU3VuIE1pY3Jvc3lzdGVtcyBJ"
162: + "bmMxIzAhBgNVBAsTGkphdmEgU29mdHdhcmUgQ29kZSBTaWduaW5nMR0wGwYDVQQD"
163: + "ExRTdW4gTWljcm9zeXN0ZW1zIEluYzCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkC"
164: + "gYEA16bKo6tC3OHFDNfPXLKXMCMtIyeubNnsEtlvrH34HhfF+ZmpSliLCvQ15ms7"
165: + "05vy4XgZUbZ3mgSOlLRMAGRo6596ePhc+0Z6yeKhbb3LZ8iz97ZIptkHGOshj9cf"
166: + "cSRPYmorUug9OsybMdIfQXazxT9mZJ9Yx5IDw6xak7kVbpUCAwEAAaOBiDCBhTAR"
167: + "BglghkgBhvhCAQEEBAMCBBAwDgYDVR0PAQH/BAQDAgXgMB0GA1UdDgQWBBRI319j"
168: + "Cbhc9DWJVltXgfrMybHNjzAfBgNVHSMEGDAWgBRl4vSGydNO8JFOWKJq9dh4WprB"
169: + "pjAgBgNVHREEGTAXgRV5dS1jaGluZy5wZW5nQHN1bi5jb20wCwYHKoZIzjgEAwUA"
170: + "Ay8AMCwCFFBFmED9s3OoN9rbXfQV3+brJPW/AhQr+Wq1MlubAvnfjrlqeksh0QaD"
171: + "AQ==" + "\n-----END CERTIFICATE-----";
172: integrityVerified = JarVerifierImpl.doVerification(c, CERT);
173: if (integrityVerified == false) {
174: throw new ProviderException(
175: "The SunMSCAPI provider may have been tampered with.");
176: }
177: }
178: }
|