J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2007 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | package sun.security.ssl; |
| 28 | |
| 29 | import java.security.*; |
| 30 | |
| 31 | /** |
| 32 | * The JSSE provider. |
| 33 | * |
| 34 | * The RSA implementation has been removed from JSSE, but we still need to |
| 35 | * register the same algorithms for compatibility. We just point to the RSA |
| 36 | * implementation in the SunRsaSign provider. This works because all classes |
| 37 | * are in the bootclasspath and therefore loaded by the same classloader. |
| 38 | * |
| 39 | * SunJSSE now supports an experimental FIPS compliant mode when used with an |
| 40 | * appropriate FIPS certified crypto provider. In FIPS mode, we: |
| 41 | * . allow only TLS 1.0 |
| 42 | * . allow only FIPS approved ciphersuites |
| 43 | * . perform all crypto in the FIPS crypto provider |
| 44 | * |
| 45 | * It is currently not possible to use both FIPS compliant SunJSSE and |
| 46 | * standard JSSE at the same time because of the various static data structures |
| 47 | * we use. |
| 48 | * |
| 49 | * However, we do want to allow FIPS mode to be enabled at runtime and without |
| 50 | * editing the java.security file. That means we need to allow |
| 51 | * Security.removeProvider("SunJSSE") to work, which creates an instance of |
| 52 | * this class in non-FIPS mode. That is why we delay the selection of the mode |
| 53 | * as long as possible. This is until we open an SSL/TLS connection and the |
| 54 | * data structures need to be initialized or until SunJSSE is initialized in |
| 55 | * FIPS mode. |
| 56 | * |
| 57 | */ |
| 58 | public abstract class SunJSSE extends java.security.Provider { |
| 59 | |
| 60 | private static final long serialVersionUID = 3231825739635378733L; |
| 61 | |
| 62 | private static String info = "Sun JSSE provider" + |
| 63 | "(PKCS12, SunX509 key/trust factories, SSLv3, TLSv1)"; |
| 64 | |
| 65 | private static String fipsInfo = |
| 66 | "Sun JSSE provider (FIPS mode, crypto provider "; |
| 67 | |
| 68 | // tri-valued flag: |
| 69 | // null := no final decision made |
| 70 | // false := data structures initialized in non-FIPS mode |
| 71 | // true := data structures initialized in FIPS mode |
| 72 | private static Boolean fips; |
| 73 | |
| 74 | // the FIPS certificate crypto provider that we use to perform all crypto |
| 75 | // operations. null in non-FIPS mode |
| 76 | static java.security.Provider cryptoProvider; |
| 77 | |
| 78 | protected static synchronized boolean isFIPS() { |
| 79 | if (fips == null) { |
| 80 | fips = false; |
| 81 | } |
| 82 | return fips; |
| 83 | } |
| 84 | |
| 85 | // ensure we can use FIPS mode using the specified crypto provider. |
| 86 | // enable FIPS mode if not already enabled. |
| 87 | private static synchronized void ensureFIPS(java.security.Provider p) { |
| 88 | if (fips == null) { |
| 89 | fips = true; |
| 90 | cryptoProvider = p; |
| 91 | } else { |
| 92 | if (fips == false) { |
| 93 | throw new ProviderException |
| 94 | ("SunJSSE already initialized in non-FIPS mode"); |
| 95 | } |
| 96 | if (cryptoProvider != p) { |
| 97 | throw new ProviderException |
| 98 | ("SunJSSE already initialized with FIPS crypto provider " |
| 99 | + cryptoProvider); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // standard constructor |
| 105 | protected SunJSSE() { |
| 106 | super("SunJSSE", 1.6d, info); |
| 107 | subclassCheck(); |
| 108 | if (Boolean.TRUE.equals(fips)) { |
| 109 | throw new ProviderException |
| 110 | ("SunJSSE is already initialized in FIPS mode"); |
| 111 | } |
| 112 | registerAlgorithms(false); |
| 113 | } |
| 114 | |
| 115 | // prefered constructor to enable FIPS mode at runtime |
| 116 | protected SunJSSE(java.security.Provider cryptoProvider){ |
| 117 | this(checkNull(cryptoProvider), cryptoProvider.getName()); |
| 118 | } |
| 119 | |
| 120 | // constructor to enable FIPS mode from java.security file |
| 121 | protected SunJSSE(String cryptoProvider){ |
| 122 | this(null, checkNull(cryptoProvider)); |
| 123 | } |
| 124 | |
| 125 | private static <T> T checkNull(T t) { |
| 126 | if (t == null) { |
| 127 | throw new ProviderException("cryptoProvider must not be null"); |
| 128 | } |
| 129 | return t; |
| 130 | } |
| 131 | |
| 132 | private SunJSSE(java.security.Provider cryptoProvider, String providerName) { |
| 133 | super("SunJSSE", 1.6d, fipsInfo + providerName + ")"); |
| 134 | subclassCheck(); |
| 135 | if (cryptoProvider == null) { |
| 136 | // Calling Security.getProvider() will cause other providers to be |
| 137 | // loaded. That is not good but unavoidable here. |
| 138 | cryptoProvider = Security.getProvider(providerName); |
| 139 | if (cryptoProvider == null) { |
| 140 | throw new ProviderException |
| 141 | ("Crypto provider not installed: " + providerName); |
| 142 | } |
| 143 | } |
| 144 | ensureFIPS(cryptoProvider); |
| 145 | registerAlgorithms(true); |
| 146 | } |
| 147 | |
| 148 | private void registerAlgorithms(final boolean isfips) { |
| 149 | AccessController.doPrivileged(new PrivilegedAction<Object>() { |
| 150 | public Object run() { |
| 151 | doRegister(isfips); |
| 152 | return null; |
| 153 | } |
| 154 | }); |
| 155 | } |
| 156 | |
| 157 | private void doRegister(boolean isfips) { |
| 158 | if (isfips == false) { |
| 159 | put("KeyFactory.RSA", |
| 160 | "sun.security.rsa.RSAKeyFactory"); |
| 161 | put("Alg.Alias.KeyFactory.1.2.840.113549.1.1", "RSA"); |
| 162 | put("Alg.Alias.KeyFactory.OID.1.2.840.113549.1.1", "RSA"); |
| 163 | |
| 164 | put("KeyPairGenerator.RSA", |
| 165 | "sun.security.rsa.RSAKeyPairGenerator"); |
| 166 | put("Alg.Alias.KeyPairGenerator.1.2.840.113549.1.1", "RSA"); |
| 167 | put("Alg.Alias.KeyPairGenerator.OID.1.2.840.113549.1.1", "RSA"); |
| 168 | |
| 169 | put("Signature.MD2withRSA", |
| 170 | "sun.security.rsa.RSASignature$MD2withRSA"); |
| 171 | put("Alg.Alias.Signature.1.2.840.113549.1.1.2", "MD2withRSA"); |
| 172 | put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.2", |
| 173 | "MD2withRSA"); |
| 174 | |
| 175 | put("Signature.MD5withRSA", |
| 176 | "sun.security.rsa.RSASignature$MD5withRSA"); |
| 177 | put("Alg.Alias.Signature.1.2.840.113549.1.1.4", "MD5withRSA"); |
| 178 | put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.4", |
| 179 | "MD5withRSA"); |
| 180 | |
| 181 | put("Signature.SHA1withRSA", |
| 182 | "sun.security.rsa.RSASignature$SHA1withRSA"); |
| 183 | put("Alg.Alias.Signature.1.2.840.113549.1.1.5", "SHA1withRSA"); |
| 184 | put("Alg.Alias.Signature.OID.1.2.840.113549.1.1.5", |
| 185 | "SHA1withRSA"); |
| 186 | put("Alg.Alias.Signature.1.3.14.3.2.29", "SHA1withRSA"); |
| 187 | put("Alg.Alias.Signature.OID.1.3.14.3.2.29", "SHA1withRSA"); |
| 188 | |
| 189 | } |
| 190 | put("Signature.MD5andSHA1withRSA", |
| 191 | "sun.security.ssl.RSASignature"); |
| 192 | |
| 193 | put("KeyManagerFactory.SunX509", |
| 194 | "sun.security.ssl.KeyManagerFactoryImpl$SunX509"); |
| 195 | put("KeyManagerFactory.NewSunX509", |
| 196 | "sun.security.ssl.KeyManagerFactoryImpl$X509"); |
| 197 | put("TrustManagerFactory.SunX509", |
| 198 | "sun.security.ssl.TrustManagerFactoryImpl$SimpleFactory"); |
| 199 | put("TrustManagerFactory.PKIX", |
| 200 | "sun.security.ssl.TrustManagerFactoryImpl$PKIXFactory"); |
| 201 | put("Alg.Alias.TrustManagerFactory.SunPKIX", "PKIX"); |
| 202 | put("Alg.Alias.TrustManagerFactory.X509", "PKIX"); |
| 203 | put("Alg.Alias.TrustManagerFactory.X.509", "PKIX"); |
| 204 | if (isfips == false) { |
| 205 | put("SSLContext.SSL", |
| 206 | "sun.security.ssl.SSLContextImpl"); |
| 207 | put("SSLContext.SSLv3", |
| 208 | "sun.security.ssl.SSLContextImpl"); |
| 209 | } |
| 210 | put("SSLContext.TLS", |
| 211 | "sun.security.ssl.SSLContextImpl"); |
| 212 | put("SSLContext.TLSv1", |
| 213 | "sun.security.ssl.SSLContextImpl"); |
| 214 | put("SSLContext.Default", |
| 215 | "sun.security.ssl.DefaultSSLContextImpl"); |
| 216 | |
| 217 | /* |
| 218 | * KeyStore |
| 219 | */ |
| 220 | put("KeyStore.PKCS12", |
| 221 | "sun.security.pkcs12.PKCS12KeyStore"); |
| 222 | } |
| 223 | |
| 224 | private void subclassCheck() { |
| 225 | if (getClass() != com.sun.net.ssl.internal.ssl.Provider.class) { |
| 226 | throw new AssertionError("Illegal subclass: " + getClass()); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | protected final void finalize() throws Throwable { |
| 232 | // empty |
| 233 | super.finalize(); |
| 234 | } |
| 235 | |
| 236 | } |