Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/provider/DSAKeyFactory.java
38830 views
/*1* Copyright (c) 1997, 2011, 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.provider;2627import java.security.Key;28import java.security.PublicKey;29import java.security.PrivateKey;30import java.security.KeyFactorySpi;31import java.security.InvalidKeyException;32import java.security.AccessController;33import java.security.interfaces.DSAParams;34import java.security.spec.DSAPublicKeySpec;35import java.security.spec.DSAPrivateKeySpec;36import java.security.spec.KeySpec;37import java.security.spec.InvalidKeySpecException;38import java.security.spec.X509EncodedKeySpec;39import java.security.spec.PKCS8EncodedKeySpec;4041import sun.security.action.GetPropertyAction;4243/**44* This class implements the DSA key factory of the Sun provider.45*46* @author Jan Luehe47*48*49* @since 1.250*/5152public class DSAKeyFactory extends KeyFactorySpi {5354// package private for DSAKeyPairGenerator55static final boolean SERIAL_INTEROP;56private static final String SERIAL_PROP = "sun.security.key.serial.interop";5758static {5960/**61* Check to see if we need to maintain interoperability for serialized62* keys between JDK 5.0 -> JDK 1.4. In other words, determine whether63* a key object serialized in JDK 5.0 must be deserializable in64* JDK 1.4.65*66* If true, then we generate sun.security.provider.DSAPublicKey.67* If false, then we generate sun.security.provider.DSAPublicKeyImpl.68*69* By default this is false.70* This incompatibility was introduced by 4532506.71*/72String prop = AccessController.doPrivileged73(new GetPropertyAction(SERIAL_PROP, null));74SERIAL_INTEROP = "true".equalsIgnoreCase(prop);75}7677/**78* Generates a public key object from the provided key specification79* (key material).80*81* @param keySpec the specification (key material) of the public key82*83* @return the public key84*85* @exception InvalidKeySpecException if the given key specification86* is inappropriate for this key factory to produce a public key.87*/88protected PublicKey engineGeneratePublic(KeySpec keySpec)89throws InvalidKeySpecException {90try {91if (keySpec instanceof DSAPublicKeySpec) {92DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec)keySpec;93if (SERIAL_INTEROP) {94return new DSAPublicKey(dsaPubKeySpec.getY(),95dsaPubKeySpec.getP(),96dsaPubKeySpec.getQ(),97dsaPubKeySpec.getG());98} else {99return new DSAPublicKeyImpl(dsaPubKeySpec.getY(),100dsaPubKeySpec.getP(),101dsaPubKeySpec.getQ(),102dsaPubKeySpec.getG());103}104} else if (keySpec instanceof X509EncodedKeySpec) {105if (SERIAL_INTEROP) {106return new DSAPublicKey107(((X509EncodedKeySpec)keySpec).getEncoded());108} else {109return new DSAPublicKeyImpl110(((X509EncodedKeySpec)keySpec).getEncoded());111}112} else {113throw new InvalidKeySpecException114("Inappropriate key specification");115}116} catch (InvalidKeyException e) {117throw new InvalidKeySpecException118("Inappropriate key specification: " + e.getMessage());119}120}121122/**123* Generates a private key object from the provided key specification124* (key material).125*126* @param keySpec the specification (key material) of the private key127*128* @return the private key129*130* @exception InvalidKeySpecException if the given key specification131* is inappropriate for this key factory to produce a private key.132*/133protected PrivateKey engineGeneratePrivate(KeySpec keySpec)134throws InvalidKeySpecException {135try {136if (keySpec instanceof DSAPrivateKeySpec) {137DSAPrivateKeySpec dsaPrivKeySpec = (DSAPrivateKeySpec)keySpec;138return new DSAPrivateKey(dsaPrivKeySpec.getX(),139dsaPrivKeySpec.getP(),140dsaPrivKeySpec.getQ(),141dsaPrivKeySpec.getG());142143} else if (keySpec instanceof PKCS8EncodedKeySpec) {144return new DSAPrivateKey145(((PKCS8EncodedKeySpec)keySpec).getEncoded());146147} else {148throw new InvalidKeySpecException149("Inappropriate key specification");150}151} catch (InvalidKeyException e) {152throw new InvalidKeySpecException153("Inappropriate key specification: " + e.getMessage());154}155}156157/**158* Returns a specification (key material) of the given key object159* in the requested format.160*161* @param key the key162*163* @param keySpec the requested format in which the key material shall be164* returned165*166* @return the underlying key specification (key material) in the167* requested format168*169* @exception InvalidKeySpecException if the requested key specification is170* inappropriate for the given key, or the given key cannot be processed171* (e.g., the given key has an unrecognized algorithm or format).172*/173protected <T extends KeySpec>174T engineGetKeySpec(Key key, Class<T> keySpec)175throws InvalidKeySpecException {176177DSAParams params;178179try {180181if (key instanceof java.security.interfaces.DSAPublicKey) {182183// Determine valid key specs184Class<?> dsaPubKeySpec = Class.forName185("java.security.spec.DSAPublicKeySpec");186Class<?> x509KeySpec = Class.forName187("java.security.spec.X509EncodedKeySpec");188189if (dsaPubKeySpec.isAssignableFrom(keySpec)) {190java.security.interfaces.DSAPublicKey dsaPubKey191= (java.security.interfaces.DSAPublicKey)key;192params = dsaPubKey.getParams();193return keySpec.cast(new DSAPublicKeySpec(dsaPubKey.getY(),194params.getP(),195params.getQ(),196params.getG()));197198} else if (x509KeySpec.isAssignableFrom(keySpec)) {199return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));200201} else {202throw new InvalidKeySpecException203("Inappropriate key specification");204}205206} else if (key instanceof java.security.interfaces.DSAPrivateKey) {207208// Determine valid key specs209Class<?> dsaPrivKeySpec = Class.forName210("java.security.spec.DSAPrivateKeySpec");211Class<?> pkcs8KeySpec = Class.forName212("java.security.spec.PKCS8EncodedKeySpec");213214if (dsaPrivKeySpec.isAssignableFrom(keySpec)) {215java.security.interfaces.DSAPrivateKey dsaPrivKey216= (java.security.interfaces.DSAPrivateKey)key;217params = dsaPrivKey.getParams();218return keySpec.cast(new DSAPrivateKeySpec(dsaPrivKey.getX(),219params.getP(),220params.getQ(),221params.getG()));222223} else if (pkcs8KeySpec.isAssignableFrom(keySpec)) {224return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));225226} else {227throw new InvalidKeySpecException228("Inappropriate key specification");229}230231} else {232throw new InvalidKeySpecException("Inappropriate key type");233}234235} catch (ClassNotFoundException e) {236throw new InvalidKeySpecException237("Unsupported key specification: " + e.getMessage());238}239}240241/**242* Translates a key object, whose provider may be unknown or potentially243* untrusted, into a corresponding key object of this key factory.244*245* @param key the key whose provider is unknown or untrusted246*247* @return the translated key248*249* @exception InvalidKeyException if the given key cannot be processed by250* this key factory.251*/252protected Key engineTranslateKey(Key key) throws InvalidKeyException {253254try {255256if (key instanceof java.security.interfaces.DSAPublicKey) {257// Check if key originates from this factory258if (key instanceof sun.security.provider.DSAPublicKey) {259return key;260}261// Convert key to spec262DSAPublicKeySpec dsaPubKeySpec263= engineGetKeySpec(key, DSAPublicKeySpec.class);264// Create key from spec, and return it265return engineGeneratePublic(dsaPubKeySpec);266267} else if (key instanceof java.security.interfaces.DSAPrivateKey) {268// Check if key originates from this factory269if (key instanceof sun.security.provider.DSAPrivateKey) {270return key;271}272// Convert key to spec273DSAPrivateKeySpec dsaPrivKeySpec274= engineGetKeySpec(key, DSAPrivateKeySpec.class);275// Create key from spec, and return it276return engineGeneratePrivate(dsaPrivKeySpec);277278} else {279throw new InvalidKeyException("Wrong algorithm type");280}281282} catch (InvalidKeySpecException e) {283throw new InvalidKeyException("Cannot translate key: "284+ e.getMessage());285}286}287}288289290