src/java.base/share/classes/sun/security/provider/DSAKeyFactory.java
changeset 51438 6ca468ea3564
parent 47216 71c04702a3d5
equal deleted inserted replaced
51437:6221a199ec20 51438:6ca468ea3564
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    28 import java.security.Key;
    28 import java.security.Key;
    29 import java.security.PublicKey;
    29 import java.security.PublicKey;
    30 import java.security.PrivateKey;
    30 import java.security.PrivateKey;
    31 import java.security.KeyFactorySpi;
    31 import java.security.KeyFactorySpi;
    32 import java.security.InvalidKeyException;
    32 import java.security.InvalidKeyException;
    33 import java.security.AccessController;
       
    34 import java.security.interfaces.DSAParams;
    33 import java.security.interfaces.DSAParams;
    35 import java.security.spec.DSAPublicKeySpec;
    34 import java.security.spec.DSAPublicKeySpec;
    36 import java.security.spec.DSAPrivateKeySpec;
    35 import java.security.spec.DSAPrivateKeySpec;
    37 import java.security.spec.KeySpec;
    36 import java.security.spec.KeySpec;
    38 import java.security.spec.InvalidKeySpecException;
    37 import java.security.spec.InvalidKeySpecException;
    39 import java.security.spec.X509EncodedKeySpec;
    38 import java.security.spec.X509EncodedKeySpec;
    40 import java.security.spec.PKCS8EncodedKeySpec;
    39 import java.security.spec.PKCS8EncodedKeySpec;
    41 
    40 
    42 import sun.security.action.GetPropertyAction;
       
    43 
       
    44 /**
    41 /**
    45  * This class implements the DSA key factory of the Sun provider.
    42  * This class implements the DSA key factory of the Sun provider.
    46  *
    43  *
    47  * @author Jan Luehe
    44  * @author Jan Luehe
    48  *
    45  *
    49  *
    46  *
    50  * @since 1.2
    47  * @since 1.2
    51  */
    48  */
    52 
    49 
    53 public class DSAKeyFactory extends KeyFactorySpi {
    50 public class DSAKeyFactory extends KeyFactorySpi {
    54 
       
    55     // package private for DSAKeyPairGenerator
       
    56     static final boolean SERIAL_INTEROP;
       
    57     private static final String SERIAL_PROP = "sun.security.key.serial.interop";
       
    58 
       
    59     static {
       
    60 
       
    61         /**
       
    62          * Check to see if we need to maintain interoperability for serialized
       
    63          * keys between JDK 5.0 -> JDK 1.4.  In other words, determine whether
       
    64          * a key object serialized in JDK 5.0 must be deserializable in
       
    65          * JDK 1.4.
       
    66          *
       
    67          * If true, then we generate sun.security.provider.DSAPublicKey.
       
    68          * If false, then we generate sun.security.provider.DSAPublicKeyImpl.
       
    69          *
       
    70          * By default this is false.
       
    71          * This incompatibility was introduced by 4532506.
       
    72          */
       
    73         String prop = GetPropertyAction.privilegedGetProperty(SERIAL_PROP);
       
    74         SERIAL_INTEROP = "true".equalsIgnoreCase(prop);
       
    75     }
       
    76 
       
    77     /**
    51     /**
    78      * Generates a public key object from the provided key specification
    52      * Generates a public key object from the provided key specification
    79      * (key material).
    53      * (key material).
    80      *
    54      *
    81      * @param keySpec the specification (key material) of the public key
    55      * @param keySpec the specification (key material) of the public key
    88     protected PublicKey engineGeneratePublic(KeySpec keySpec)
    62     protected PublicKey engineGeneratePublic(KeySpec keySpec)
    89     throws InvalidKeySpecException {
    63     throws InvalidKeySpecException {
    90         try {
    64         try {
    91             if (keySpec instanceof DSAPublicKeySpec) {
    65             if (keySpec instanceof DSAPublicKeySpec) {
    92                 DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec)keySpec;
    66                 DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec)keySpec;
    93                 if (SERIAL_INTEROP) {
    67                 return new DSAPublicKeyImpl(dsaPubKeySpec.getY(),
    94                     return new DSAPublicKey(dsaPubKeySpec.getY(),
    68                                     dsaPubKeySpec.getP(),
    95                                         dsaPubKeySpec.getP(),
    69                                     dsaPubKeySpec.getQ(),
    96                                         dsaPubKeySpec.getQ(),
    70                                     dsaPubKeySpec.getG());
    97                                         dsaPubKeySpec.getG());
       
    98                 } else {
       
    99                     return new DSAPublicKeyImpl(dsaPubKeySpec.getY(),
       
   100                                         dsaPubKeySpec.getP(),
       
   101                                         dsaPubKeySpec.getQ(),
       
   102                                         dsaPubKeySpec.getG());
       
   103                 }
       
   104             } else if (keySpec instanceof X509EncodedKeySpec) {
    71             } else if (keySpec instanceof X509EncodedKeySpec) {
   105                 if (SERIAL_INTEROP) {
    72                 return new DSAPublicKeyImpl
   106                     return new DSAPublicKey
    73                     (((X509EncodedKeySpec)keySpec).getEncoded());
   107                         (((X509EncodedKeySpec)keySpec).getEncoded());
       
   108                 } else {
       
   109                     return new DSAPublicKeyImpl
       
   110                         (((X509EncodedKeySpec)keySpec).getEncoded());
       
   111                 }
       
   112             } else {
    74             } else {
   113                 throw new InvalidKeySpecException
    75                 throw new InvalidKeySpecException
   114                     ("Inappropriate key specification");
    76                     ("Inappropriate key specification");
   115             }
    77             }
   116         } catch (InvalidKeyException e) {
    78         } catch (InvalidKeyException e) {