jdk/src/java.base/share/classes/sun/security/provider/DSAKeyFactory.java
author redestad
Tue, 03 May 2016 15:50:54 +0200
changeset 37781 71ed5645f17c
parent 37593 824750ada3d6
permissions -rw-r--r--
8155775: Re-examine naming of privileged methods to access System properties Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.KeyFactorySpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.interfaces.DSAParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.spec.DSAPublicKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.spec.DSAPrivateKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.spec.KeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.spec.X509EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.spec.PKCS8EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This class implements the DSA key factory of the Sun provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class DSAKeyFactory extends KeyFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // package private for DSAKeyPairGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static final boolean SERIAL_INTEROP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static final String SERIAL_PROP = "sun.security.key.serial.interop";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
         * Check to see if we need to maintain interoperability for serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         * keys between JDK 5.0 -> JDK 1.4.  In other words, determine whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         * a key object serialized in JDK 5.0 must be deserializable in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
         * JDK 1.4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         * If true, then we generate sun.security.provider.DSAPublicKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         * If false, then we generate sun.security.provider.DSAPublicKeyImpl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
         * By default this is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         * This incompatibility was introduced by 4532506.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         */
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    73
        String prop = GetPropertyAction.privilegedGetProperty(SERIAL_PROP);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        SERIAL_INTEROP = "true".equalsIgnoreCase(prop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Generates a public key object from the provided key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param keySpec the specification (key material) of the public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return the public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * is inappropriate for this key factory to produce a public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected PublicKey engineGeneratePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (keySpec instanceof DSAPublicKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (SERIAL_INTEROP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    return new DSAPublicKey(dsaPubKeySpec.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                        dsaPubKeySpec.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                        dsaPubKeySpec.getQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                        dsaPubKeySpec.getG());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    return new DSAPublicKeyImpl(dsaPubKeySpec.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                        dsaPubKeySpec.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                        dsaPubKeySpec.getQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                        dsaPubKeySpec.getG());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            } else if (keySpec instanceof X509EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                if (SERIAL_INTEROP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    return new DSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        (((X509EncodedKeySpec)keySpec).getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    return new DSAPublicKeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        (((X509EncodedKeySpec)keySpec).getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    ("Inappropriate key specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                ("Inappropriate key specification: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Generates a private key object from the provided key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param keySpec the specification (key material) of the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @return the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * is inappropriate for this key factory to produce a private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (keySpec instanceof DSAPrivateKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                DSAPrivateKeySpec dsaPrivKeySpec = (DSAPrivateKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                return new DSAPrivateKey(dsaPrivKeySpec.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                         dsaPrivKeySpec.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                         dsaPrivKeySpec.getQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                         dsaPrivKeySpec.getG());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            } else if (keySpec instanceof PKCS8EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                return new DSAPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    (((PKCS8EncodedKeySpec)keySpec).getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    ("Inappropriate key specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                ("Inappropriate key specification: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns a specification (key material) of the given key object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * in the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param keySpec the requested format in which the key material shall be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @return the underlying key specification (key material) in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * requested format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @exception InvalidKeySpecException if the requested key specification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * inappropriate for the given key, or the given key cannot be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * (e.g., the given key has an unrecognized algorithm or format).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    protected <T extends KeySpec>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        T engineGetKeySpec(Key key, Class<T> keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        DSAParams params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (key instanceof java.security.interfaces.DSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                // Determine valid key specs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                Class<?> dsaPubKeySpec = Class.forName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    ("java.security.spec.DSAPublicKeySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                Class<?> x509KeySpec = Class.forName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    ("java.security.spec.X509EncodedKeySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                if (dsaPubKeySpec.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    java.security.interfaces.DSAPublicKey dsaPubKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        = (java.security.interfaces.DSAPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    params = dsaPubKey.getParams();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   193
                    return keySpec.cast(new DSAPublicKeySpec(dsaPubKey.getY(),
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   194
                                                             params.getP(),
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   195
                                                             params.getQ(),
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   196
                                                             params.getG()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                } else if (x509KeySpec.isAssignableFrom(keySpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   199
                    return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        ("Inappropriate key specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                // Determine valid key specs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                Class<?> dsaPrivKeySpec = Class.forName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    ("java.security.spec.DSAPrivateKeySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                Class<?> pkcs8KeySpec = Class.forName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    ("java.security.spec.PKCS8EncodedKeySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (dsaPrivKeySpec.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    java.security.interfaces.DSAPrivateKey dsaPrivKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        = (java.security.interfaces.DSAPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    params = dsaPrivKey.getParams();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   218
                    return keySpec.cast(new DSAPrivateKeySpec(dsaPrivKey.getX(),
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   219
                                                              params.getP(),
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   220
                                                              params.getQ(),
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   221
                                                              params.getG()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                } else if (pkcs8KeySpec.isAssignableFrom(keySpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   224
                    return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        ("Inappropriate key specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                throw new InvalidKeySpecException("Inappropriate key type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                ("Unsupported key specification: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Translates a key object, whose provider may be unknown or potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * untrusted, into a corresponding key object of this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param key the key whose provider is unknown or untrusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @return the translated key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @exception InvalidKeyException if the given key cannot be processed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    protected Key engineTranslateKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (key instanceof java.security.interfaces.DSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                // Check if key originates from this factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                if (key instanceof sun.security.provider.DSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                // Convert key to spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                DSAPublicKeySpec dsaPubKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    = engineGetKeySpec(key, DSAPublicKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                // Create key from spec, and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                return engineGeneratePublic(dsaPubKeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                // Check if key originates from this factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                if (key instanceof sun.security.provider.DSAPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                // Convert key to spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                DSAPrivateKeySpec dsaPrivKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    = engineGetKeySpec(key, DSAPrivateKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                // Create key from spec, and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                return engineGeneratePrivate(dsaPrivKeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                throw new InvalidKeyException("Wrong algorithm type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            throw new InvalidKeyException("Cannot translate key: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                          + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
}