Showing the changes to java/pkg/sun/security/provider/DSAKeyFactory.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,291 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1997, 2006, 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.provider; | |
| 27 | + | |
| 28 | +import java.util.*; | |
| 29 | +import java.lang.*; | |
| 30 | +import java.security.Key; | |
| 31 | +import java.security.PublicKey; | |
| 32 | +import java.security.PrivateKey; | |
| 33 | +import java.security.KeyFactorySpi; | |
| 34 | +import java.security.InvalidKeyException; | |
| 35 | +import java.security.AccessController; | |
| 36 | +import java.security.PrivilegedAction; | |
| 37 | +import java.security.interfaces.DSAParams; | |
| 38 | +import java.security.spec.DSAPublicKeySpec; | |
| 39 | +import java.security.spec.DSAPrivateKeySpec; | |
| 40 | +import java.security.spec.KeySpec; | |
| 41 | +import java.security.spec.InvalidKeySpecException; | |
| 42 | +import java.security.spec.X509EncodedKeySpec; | |
| 43 | +import java.security.spec.PKCS8EncodedKeySpec; | |
| 44 | + | |
| 45 | +import sun.security.action.GetPropertyAction; | |
| 46 | + | |
| 47 | +/** | |
| 48 | + * This class implements the DSA key factory of the Sun provider. | |
| 49 | + * | |
| 50 | + * @author Jan Luehe | |
| 51 | + * | |
| 52 | + * | |
| 53 | + * @since 1.2 | |
| 54 | + */ | |
| 55 | + | |
| 56 | +public class DSAKeyFactory extends KeyFactorySpi { | |
| 57 | + | |
| 58 | + // package private for DSAKeyPairGenerator | |
| 59 | + static final boolean SERIAL_INTEROP; | |
| 60 | + private static final String SERIAL_PROP = "sun.security.key.serial.interop"; | |
| 61 | + | |
| 62 | + static { | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Check to see if we need to maintain interoperability for serialized | |
| 66 | + * keys between JDK 5.0 -> JDK 1.4. In other words, determine whether | |
| 67 | + * a key object serialized in JDK 5.0 must be deserializable in | |
| 68 | + * JDK 1.4. | |
| 69 | + * | |
| 70 | + * If true, then we generate sun.security.provider.DSAPublicKey. | |
| 71 | + * If false, then we generate sun.security.provider.DSAPublicKeyImpl. | |
| 72 | + * | |
| 73 | + * By default this is false. | |
| 74 | + * This incompatibility was introduced by 4532506. | |
| 75 | + */ | |
| 76 | + String prop = AccessController.doPrivileged | |
| 77 | + (new GetPropertyAction(SERIAL_PROP, null)); | |
| 78 | + SERIAL_INTEROP = "true".equalsIgnoreCase(prop); | |
| 79 | + } | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * Generates a public key object from the provided key specification | |
| 83 | + * (key material). | |
| 84 | + * | |
| 85 | + * @param keySpec the specification (key material) of the public key | |
| 86 | + * | |
| 87 | + * @return the public key | |
| 88 | + * | |
| 89 | + * @exception InvalidKeySpecException if the given key specification | |
| 90 | + * is inappropriate for this key factory to produce a public key. | |
| 91 | + */ | |
| 92 | + protected PublicKey engineGeneratePublic(KeySpec keySpec) | |
| 93 | + throws InvalidKeySpecException { | |
| 94 | + try { | |
| 95 | + if (keySpec instanceof DSAPublicKeySpec) { | |
| 96 | + DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec)keySpec; | |
| 97 | + if (SERIAL_INTEROP) { | |
| 98 | + return new DSAPublicKey(dsaPubKeySpec.getY(), | |
| 99 | + dsaPubKeySpec.getP(), | |
| 100 | + dsaPubKeySpec.getQ(), | |
| 101 | + dsaPubKeySpec.getG()); | |
| 102 | + } else { | |
| 103 | + return new DSAPublicKeyImpl(dsaPubKeySpec.getY(), | |
| 104 | + dsaPubKeySpec.getP(), | |
| 105 | + dsaPubKeySpec.getQ(), | |
| 106 | + dsaPubKeySpec.getG()); | |
| 107 | + } | |
| 108 | + } else if (keySpec instanceof X509EncodedKeySpec) { | |
| 109 | + if (SERIAL_INTEROP) { | |
| 110 | + return new DSAPublicKey | |
| 111 | + (((X509EncodedKeySpec)keySpec).getEncoded()); | |
| 112 | + } else { | |
| 113 | + return new DSAPublicKeyImpl | |
| 114 | + (((X509EncodedKeySpec)keySpec).getEncoded()); | |
| 115 | + } | |
| 116 | + } else { | |
| 117 | + throw new InvalidKeySpecException | |
| 118 | + ("Inappropriate key specification"); | |
| 119 | + } | |
| 120 | + } catch (InvalidKeyException e) { | |
| 121 | + throw new InvalidKeySpecException | |
| 122 | + ("Inappropriate key specification: " + e.getMessage()); | |
| 123 | + } | |
| 124 | + } | |
| 125 | + | |
| 126 | + /** | |
| 127 | + * Generates a private key object from the provided key specification | |
| 128 | + * (key material). | |
| 129 | + * | |
| 130 | + * @param keySpec the specification (key material) of the private key | |
| 131 | + * | |
| 132 | + * @return the private key | |
| 133 | + * | |
| 134 | + * @exception InvalidKeySpecException if the given key specification | |
| 135 | + * is inappropriate for this key factory to produce a private key. | |
| 136 | + */ | |
| 137 | + protected PrivateKey engineGeneratePrivate(KeySpec keySpec) | |
| 138 | + throws InvalidKeySpecException { | |
| 139 | + try { | |
| 140 | + if (keySpec instanceof DSAPrivateKeySpec) { | |
| 141 | + DSAPrivateKeySpec dsaPrivKeySpec = (DSAPrivateKeySpec)keySpec; | |
| 142 | + return new DSAPrivateKey(dsaPrivKeySpec.getX(), | |
| 143 | + dsaPrivKeySpec.getP(), | |
| 144 | + dsaPrivKeySpec.getQ(), | |
| 145 | + dsaPrivKeySpec.getG()); | |
| 146 | + | |
| 147 | + } else if (keySpec instanceof PKCS8EncodedKeySpec) { | |
| 148 | + return new DSAPrivateKey | |
| 149 | + (((PKCS8EncodedKeySpec)keySpec).getEncoded()); | |
| 150 | + | |
| 151 | + } else { | |
| 152 | + throw new InvalidKeySpecException | |
| 153 | + ("Inappropriate key specification"); | |
| 154 | + } | |
| 155 | + } catch (InvalidKeyException e) { | |
| 156 | + throw new InvalidKeySpecException | |
| 157 | + ("Inappropriate key specification: " + e.getMessage()); | |
| 158 | + } | |
| 159 | + } | |
| 160 | + | |
| 161 | + /** | |
| 162 | + * Returns a specification (key material) of the given key object | |
| 163 | + * in the requested format. | |
| 164 | + * | |
| 165 | + * @param key the key | |
| 166 | + * | |
| 167 | + * @param keySpec the requested format in which the key material shall be | |
| 168 | + * returned | |
| 169 | + * | |
| 170 | + * @return the underlying key specification (key material) in the | |
| 171 | + * requested format | |
| 172 | + * | |
| 173 | + * @exception InvalidKeySpecException if the requested key specification is | |
| 174 | + * inappropriate for the given key, or the given key cannot be processed | |
| 175 | + * (e.g., the given key has an unrecognized algorithm or format). | |
| 176 | + */ | |
| 177 | + protected <T extends KeySpec> | |
| 178 | + T engineGetKeySpec(Key key, Class<T> keySpec) | |
| 179 | + throws InvalidKeySpecException { | |
| 180 | + | |
| 181 | + DSAParams params; | |
| 182 | + | |
| 183 | + try { | |
| 184 | + | |
| 185 | + if (key instanceof java.security.interfaces.DSAPublicKey) { | |
| 186 | + | |
| 187 | + // Determine valid key specs | |
| 188 | + Class<?> dsaPubKeySpec = Class.forName | |
| 189 | + ("java.security.spec.DSAPublicKeySpec"); | |
| 190 | + Class<?> x509KeySpec = Class.forName | |
| 191 | + ("java.security.spec.X509EncodedKeySpec"); | |
| 192 | + | |
| 193 | + if (dsaPubKeySpec.isAssignableFrom(keySpec)) { | |
| 194 | + java.security.interfaces.DSAPublicKey dsaPubKey | |
| 195 | + = (java.security.interfaces.DSAPublicKey)key; | |
| 196 | + params = dsaPubKey.getParams(); | |
| 197 | + return (T) new DSAPublicKeySpec(dsaPubKey.getY(), | |
| 198 | + params.getP(), | |
| 199 | + params.getQ(), | |
| 200 | + params.getG()); | |
| 201 | + | |
| 202 | + } else if (x509KeySpec.isAssignableFrom(keySpec)) { | |
| 203 | + return (T) new X509EncodedKeySpec(key.getEncoded()); | |
| 204 | + | |
| 205 | + } else { | |
| 206 | + throw new InvalidKeySpecException | |
| 207 | + ("Inappropriate key specification"); | |
| 208 | + } | |
| 209 | + | |
| 210 | + } else if (key instanceof java.security.interfaces.DSAPrivateKey) { | |
| 211 | + | |
| 212 | + // Determine valid key specs | |
| 213 | + Class<?> dsaPrivKeySpec = Class.forName | |
| 214 | + ("java.security.spec.DSAPrivateKeySpec"); | |
| 215 | + Class<?> pkcs8KeySpec = Class.forName | |
| 216 | + ("java.security.spec.PKCS8EncodedKeySpec"); | |
| 217 | + | |
| 218 | + if (dsaPrivKeySpec.isAssignableFrom(keySpec)) { | |
| 219 | + java.security.interfaces.DSAPrivateKey dsaPrivKey | |
| 220 | + = (java.security.interfaces.DSAPrivateKey)key; | |
| 221 | + params = dsaPrivKey.getParams(); | |
| 222 | + return (T) new DSAPrivateKeySpec(dsaPrivKey.getX(), | |
| 223 | + params.getP(), | |
| 224 | + params.getQ(), | |
| 225 | + params.getG()); | |
| 226 | + | |
| 227 | + } else if (pkcs8KeySpec.isAssignableFrom(keySpec)) { | |
| 228 | + return (T) new PKCS8EncodedKeySpec(key.getEncoded()); | |
| 229 | + | |
| 230 | + } else { | |
| 231 | + throw new InvalidKeySpecException | |
| 232 | + ("Inappropriate key specification"); | |
| 233 | + } | |
| 234 | + | |
| 235 | + } else { | |
| 236 | + throw new InvalidKeySpecException("Inappropriate key type"); | |
| 237 | + } | |
| 238 | + | |
| 239 | + } catch (ClassNotFoundException e) { | |
| 240 | + throw new InvalidKeySpecException | |
| 241 | + ("Unsupported key specification: " + e.getMessage()); | |
| 242 | + } | |
| 243 | + } | |
| 244 | + | |
| 245 | + /** | |
| 246 | + * Translates a key object, whose provider may be unknown or potentially | |
| 247 | + * untrusted, into a corresponding key object of this key factory. | |
| 248 | + * | |
| 249 | + * @param key the key whose provider is unknown or untrusted | |
| 250 | + * | |
| 251 | + * @return the translated key | |
| 252 | + * | |
| 253 | + * @exception InvalidKeyException if the given key cannot be processed by | |
| 254 | + * this key factory. | |
| 255 | + */ | |
| 256 | + protected Key engineTranslateKey(Key key) throws InvalidKeyException { | |
| 257 | + | |
| 258 | + try { | |
| 259 | + | |
| 260 | + if (key instanceof java.security.interfaces.DSAPublicKey) { | |
| 261 | + // Check if key originates from this factory | |
| 262 | + if (key instanceof sun.security.provider.DSAPublicKey) { | |
| 263 | + return key; | |
| 264 | + } | |
| 265 | + // Convert key to spec | |
| 266 | + DSAPublicKeySpec dsaPubKeySpec | |
| 267 | + = engineGetKeySpec(key, DSAPublicKeySpec.class); | |
| 268 | + // Create key from spec, and return it | |
| 269 | + return engineGeneratePublic(dsaPubKeySpec); | |
| 270 | + | |
| 271 | + } else if (key instanceof java.security.interfaces.DSAPrivateKey) { | |
| 272 | + // Check if key originates from this factory | |
| 273 | + if (key instanceof sun.security.provider.DSAPrivateKey) { | |
| 274 | + return key; | |
| 275 | + } | |
| 276 | + // Convert key to spec | |
| 277 | + DSAPrivateKeySpec dsaPrivKeySpec | |
| 278 | + = engineGetKeySpec(key, DSAPrivateKeySpec.class); | |
| 279 | + // Create key from spec, and return it | |
| 280 | + return engineGeneratePrivate(dsaPrivKeySpec); | |
| 281 | + | |
| 282 | + } else { | |
| 283 | + throw new InvalidKeyException("Wrong algorithm type"); | |
| 284 | + } | |
| 285 | + | |
| 286 | + } catch (InvalidKeySpecException e) { | |
| 287 | + throw new InvalidKeyException("Cannot translate key: " | |
| 288 | + + e.getMessage()); | |
| 289 | + } | |
| 290 | + } | |
| 291 | +} | |