0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.provider;
0N/A
0N/Aimport java.util.*;
0N/Aimport java.lang.*;
0N/Aimport java.security.Key;
0N/Aimport java.security.PublicKey;
0N/Aimport java.security.PrivateKey;
0N/Aimport java.security.KeyFactorySpi;
0N/Aimport java.security.InvalidKeyException;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.security.interfaces.DSAParams;
0N/Aimport java.security.spec.DSAPublicKeySpec;
0N/Aimport java.security.spec.DSAPrivateKeySpec;
0N/Aimport java.security.spec.KeySpec;
0N/Aimport java.security.spec.InvalidKeySpecException;
0N/Aimport java.security.spec.X509EncodedKeySpec;
0N/Aimport java.security.spec.PKCS8EncodedKeySpec;
0N/A
0N/Aimport sun.security.action.GetPropertyAction;
0N/A
0N/A/**
0N/A * This class implements the DSA key factory of the Sun provider.
0N/A *
0N/A * @author Jan Luehe
0N/A *
0N/A *
0N/A * @since 1.2
0N/A */
0N/A
0N/Apublic class DSAKeyFactory extends KeyFactorySpi {
0N/A
0N/A // package private for DSAKeyPairGenerator
0N/A static final boolean SERIAL_INTEROP;
0N/A private static final String SERIAL_PROP = "sun.security.key.serial.interop";
0N/A
0N/A static {
0N/A
0N/A /**
0N/A * Check to see if we need to maintain interoperability for serialized
0N/A * keys between JDK 5.0 -> JDK 1.4. In other words, determine whether
0N/A * a key object serialized in JDK 5.0 must be deserializable in
0N/A * JDK 1.4.
0N/A *
0N/A * If true, then we generate sun.security.provider.DSAPublicKey.
0N/A * If false, then we generate sun.security.provider.DSAPublicKeyImpl.
0N/A *
0N/A * By default this is false.
0N/A * This incompatibility was introduced by 4532506.
0N/A */
0N/A String prop = AccessController.doPrivileged
0N/A (new GetPropertyAction(SERIAL_PROP, null));
0N/A SERIAL_INTEROP = "true".equalsIgnoreCase(prop);
0N/A }
0N/A
0N/A /**
0N/A * Generates a public key object from the provided key specification
0N/A * (key material).
0N/A *
0N/A * @param keySpec the specification (key material) of the public key
0N/A *
0N/A * @return the public key
0N/A *
0N/A * @exception InvalidKeySpecException if the given key specification
0N/A * is inappropriate for this key factory to produce a public key.
0N/A */
0N/A protected PublicKey engineGeneratePublic(KeySpec keySpec)
0N/A throws InvalidKeySpecException {
0N/A try {
0N/A if (keySpec instanceof DSAPublicKeySpec) {
0N/A DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec)keySpec;
0N/A if (SERIAL_INTEROP) {
0N/A return new DSAPublicKey(dsaPubKeySpec.getY(),
0N/A dsaPubKeySpec.getP(),
0N/A dsaPubKeySpec.getQ(),
0N/A dsaPubKeySpec.getG());
0N/A } else {
0N/A return new DSAPublicKeyImpl(dsaPubKeySpec.getY(),
0N/A dsaPubKeySpec.getP(),
0N/A dsaPubKeySpec.getQ(),
0N/A dsaPubKeySpec.getG());
0N/A }
0N/A } else if (keySpec instanceof X509EncodedKeySpec) {
0N/A if (SERIAL_INTEROP) {
0N/A return new DSAPublicKey
0N/A (((X509EncodedKeySpec)keySpec).getEncoded());
0N/A } else {
0N/A return new DSAPublicKeyImpl
0N/A (((X509EncodedKeySpec)keySpec).getEncoded());
0N/A }
0N/A } else {
0N/A throw new InvalidKeySpecException
0N/A ("Inappropriate key specification");
0N/A }
0N/A } catch (InvalidKeyException e) {
0N/A throw new InvalidKeySpecException
0N/A ("Inappropriate key specification: " + e.getMessage());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Generates a private key object from the provided key specification
0N/A * (key material).
0N/A *
0N/A * @param keySpec the specification (key material) of the private key
0N/A *
0N/A * @return the private key
0N/A *
0N/A * @exception InvalidKeySpecException if the given key specification
0N/A * is inappropriate for this key factory to produce a private key.
0N/A */
0N/A protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
0N/A throws InvalidKeySpecException {
0N/A try {
0N/A if (keySpec instanceof DSAPrivateKeySpec) {
0N/A DSAPrivateKeySpec dsaPrivKeySpec = (DSAPrivateKeySpec)keySpec;
0N/A return new DSAPrivateKey(dsaPrivKeySpec.getX(),
0N/A dsaPrivKeySpec.getP(),
0N/A dsaPrivKeySpec.getQ(),
0N/A dsaPrivKeySpec.getG());
0N/A
0N/A } else if (keySpec instanceof PKCS8EncodedKeySpec) {
0N/A return new DSAPrivateKey
0N/A (((PKCS8EncodedKeySpec)keySpec).getEncoded());
0N/A
0N/A } else {
0N/A throw new InvalidKeySpecException
0N/A ("Inappropriate key specification");
0N/A }
0N/A } catch (InvalidKeyException e) {
0N/A throw new InvalidKeySpecException
0N/A ("Inappropriate key specification: " + e.getMessage());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a specification (key material) of the given key object
0N/A * in the requested format.
0N/A *
0N/A * @param key the key
0N/A *
0N/A * @param keySpec the requested format in which the key material shall be
0N/A * returned
0N/A *
0N/A * @return the underlying key specification (key material) in the
0N/A * requested format
0N/A *
0N/A * @exception InvalidKeySpecException if the requested key specification is
0N/A * inappropriate for the given key, or the given key cannot be processed
0N/A * (e.g., the given key has an unrecognized algorithm or format).
0N/A */
0N/A protected <T extends KeySpec>
0N/A T engineGetKeySpec(Key key, Class<T> keySpec)
0N/A throws InvalidKeySpecException {
0N/A
0N/A DSAParams params;
0N/A
0N/A try {
0N/A
0N/A if (key instanceof java.security.interfaces.DSAPublicKey) {
0N/A
0N/A // Determine valid key specs
0N/A Class<?> dsaPubKeySpec = Class.forName
0N/A ("java.security.spec.DSAPublicKeySpec");
0N/A Class<?> x509KeySpec = Class.forName
0N/A ("java.security.spec.X509EncodedKeySpec");
0N/A
0N/A if (dsaPubKeySpec.isAssignableFrom(keySpec)) {
0N/A java.security.interfaces.DSAPublicKey dsaPubKey
0N/A = (java.security.interfaces.DSAPublicKey)key;
0N/A params = dsaPubKey.getParams();
0N/A return (T) new DSAPublicKeySpec(dsaPubKey.getY(),
0N/A params.getP(),
0N/A params.getQ(),
0N/A params.getG());
0N/A
0N/A } else if (x509KeySpec.isAssignableFrom(keySpec)) {
0N/A return (T) new X509EncodedKeySpec(key.getEncoded());
0N/A
0N/A } else {
0N/A throw new InvalidKeySpecException
0N/A ("Inappropriate key specification");
0N/A }
0N/A
0N/A } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
0N/A
0N/A // Determine valid key specs
0N/A Class<?> dsaPrivKeySpec = Class.forName
0N/A ("java.security.spec.DSAPrivateKeySpec");
0N/A Class<?> pkcs8KeySpec = Class.forName
0N/A ("java.security.spec.PKCS8EncodedKeySpec");
0N/A
0N/A if (dsaPrivKeySpec.isAssignableFrom(keySpec)) {
0N/A java.security.interfaces.DSAPrivateKey dsaPrivKey
0N/A = (java.security.interfaces.DSAPrivateKey)key;
0N/A params = dsaPrivKey.getParams();
0N/A return (T) new DSAPrivateKeySpec(dsaPrivKey.getX(),
0N/A params.getP(),
0N/A params.getQ(),
0N/A params.getG());
0N/A
0N/A } else if (pkcs8KeySpec.isAssignableFrom(keySpec)) {
0N/A return (T) new PKCS8EncodedKeySpec(key.getEncoded());
0N/A
0N/A } else {
0N/A throw new InvalidKeySpecException
0N/A ("Inappropriate key specification");
0N/A }
0N/A
0N/A } else {
0N/A throw new InvalidKeySpecException("Inappropriate key type");
0N/A }
0N/A
0N/A } catch (ClassNotFoundException e) {
0N/A throw new InvalidKeySpecException
0N/A ("Unsupported key specification: " + e.getMessage());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Translates a key object, whose provider may be unknown or potentially
0N/A * untrusted, into a corresponding key object of this key factory.
0N/A *
0N/A * @param key the key whose provider is unknown or untrusted
0N/A *
0N/A * @return the translated key
0N/A *
0N/A * @exception InvalidKeyException if the given key cannot be processed by
0N/A * this key factory.
0N/A */
0N/A protected Key engineTranslateKey(Key key) throws InvalidKeyException {
0N/A
0N/A try {
0N/A
0N/A if (key instanceof java.security.interfaces.DSAPublicKey) {
0N/A // Check if key originates from this factory
0N/A if (key instanceof sun.security.provider.DSAPublicKey) {
0N/A return key;
0N/A }
0N/A // Convert key to spec
0N/A DSAPublicKeySpec dsaPubKeySpec
0N/A = engineGetKeySpec(key, DSAPublicKeySpec.class);
0N/A // Create key from spec, and return it
0N/A return engineGeneratePublic(dsaPubKeySpec);
0N/A
0N/A } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
0N/A // Check if key originates from this factory
0N/A if (key instanceof sun.security.provider.DSAPrivateKey) {
0N/A return key;
0N/A }
0N/A // Convert key to spec
0N/A DSAPrivateKeySpec dsaPrivKeySpec
0N/A = engineGetKeySpec(key, DSAPrivateKeySpec.class);
0N/A // Create key from spec, and return it
0N/A return engineGeneratePrivate(dsaPrivKeySpec);
0N/A
0N/A } else {
0N/A throw new InvalidKeyException("Wrong algorithm type");
0N/A }
0N/A
0N/A } catch (InvalidKeySpecException e) {
0N/A throw new InvalidKeyException("Cannot translate key: "
0N/A + e.getMessage());
0N/A }
0N/A }
0N/A}