src/java.base/share/classes/sun/security/provider/DSAKeyFactory.java
author coffeys
Fri, 17 Aug 2018 22:20:47 +0100
changeset 51438 6ca468ea3564
parent 47216 71c04702a3d5
permissions -rw-r--r--
8208675: Remove legacy sun.security.key.serial.interop property Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51438
6ca468ea3564 8208675: Remove legacy sun.security.key.serial.interop property
coffeys
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, 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.interfaces.DSAParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.spec.DSAPublicKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.spec.DSAPrivateKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.spec.KeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.spec.InvalidKeySpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.spec.X509EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.spec.PKCS8EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class implements the DSA key factory of the Sun provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class DSAKeyFactory extends KeyFactorySpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Generates a public key object from the provided key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param keySpec the specification (key material) of the public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @return the public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * is inappropriate for this key factory to produce a public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected PublicKey engineGeneratePublic(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (keySpec instanceof DSAPublicKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec)keySpec;
51438
6ca468ea3564 8208675: Remove legacy sun.security.key.serial.interop property
coffeys
parents: 47216
diff changeset
    67
                return new DSAPublicKeyImpl(dsaPubKeySpec.getY(),
6ca468ea3564 8208675: Remove legacy sun.security.key.serial.interop property
coffeys
parents: 47216
diff changeset
    68
                                    dsaPubKeySpec.getP(),
6ca468ea3564 8208675: Remove legacy sun.security.key.serial.interop property
coffeys
parents: 47216
diff changeset
    69
                                    dsaPubKeySpec.getQ(),
6ca468ea3564 8208675: Remove legacy sun.security.key.serial.interop property
coffeys
parents: 47216
diff changeset
    70
                                    dsaPubKeySpec.getG());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            } else if (keySpec instanceof X509EncodedKeySpec) {
51438
6ca468ea3564 8208675: Remove legacy sun.security.key.serial.interop property
coffeys
parents: 47216
diff changeset
    72
                return new DSAPublicKeyImpl
6ca468ea3564 8208675: Remove legacy sun.security.key.serial.interop property
coffeys
parents: 47216
diff changeset
    73
                    (((X509EncodedKeySpec)keySpec).getEncoded());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    ("Inappropriate key specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                ("Inappropriate key specification: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Generates a private key object from the provided key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * (key material).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param keySpec the specification (key material) of the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @return the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @exception InvalidKeySpecException if the given key specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * is inappropriate for this key factory to produce a private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (keySpec instanceof DSAPrivateKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                DSAPrivateKeySpec dsaPrivKeySpec = (DSAPrivateKeySpec)keySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return new DSAPrivateKey(dsaPrivKeySpec.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                         dsaPrivKeySpec.getP(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                         dsaPrivKeySpec.getQ(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                         dsaPrivKeySpec.getG());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            } else if (keySpec instanceof PKCS8EncodedKeySpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                return new DSAPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    (((PKCS8EncodedKeySpec)keySpec).getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    ("Inappropriate key specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                ("Inappropriate key specification: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Returns a specification (key material) of the given key object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * in the requested format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param keySpec the requested format in which the key material shall be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @return the underlying key specification (key material) in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * requested format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @exception InvalidKeySpecException if the requested key specification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * inappropriate for the given key, or the given key cannot be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * (e.g., the given key has an unrecognized algorithm or format).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    protected <T extends KeySpec>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        T engineGetKeySpec(Key key, Class<T> keySpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    throws InvalidKeySpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        DSAParams params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (key instanceof java.security.interfaces.DSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // Determine valid key specs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                Class<?> dsaPubKeySpec = Class.forName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    ("java.security.spec.DSAPublicKeySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                Class<?> x509KeySpec = Class.forName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    ("java.security.spec.X509EncodedKeySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (dsaPubKeySpec.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    java.security.interfaces.DSAPublicKey dsaPubKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        = (java.security.interfaces.DSAPublicKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    params = dsaPubKey.getParams();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   155
                    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
   156
                                                             params.getP(),
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   157
                                                             params.getQ(),
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   158
                                                             params.getG()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                } else if (x509KeySpec.isAssignableFrom(keySpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   161
                    return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        ("Inappropriate key specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                // Determine valid key specs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                Class<?> dsaPrivKeySpec = Class.forName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    ("java.security.spec.DSAPrivateKeySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                Class<?> pkcs8KeySpec = Class.forName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    ("java.security.spec.PKCS8EncodedKeySpec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                if (dsaPrivKeySpec.isAssignableFrom(keySpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    java.security.interfaces.DSAPrivateKey dsaPrivKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        = (java.security.interfaces.DSAPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    params = dsaPrivKey.getParams();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   180
                    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
   181
                                                              params.getP(),
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   182
                                                              params.getQ(),
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   183
                                                              params.getG()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                } else if (pkcs8KeySpec.isAssignableFrom(keySpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   186
                    return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        ("Inappropriate key specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                throw new InvalidKeySpecException("Inappropriate key type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new InvalidKeySpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                ("Unsupported key specification: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Translates a key object, whose provider may be unknown or potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * untrusted, into a corresponding key object of this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param key the key whose provider is unknown or untrusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return the translated key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @exception InvalidKeyException if the given key cannot be processed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * this key factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    protected Key engineTranslateKey(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (key instanceof java.security.interfaces.DSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                // Check if key originates from this factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                if (key instanceof sun.security.provider.DSAPublicKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                // Convert key to spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                DSAPublicKeySpec dsaPubKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    = engineGetKeySpec(key, DSAPublicKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                // Create key from spec, and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                return engineGeneratePublic(dsaPubKeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                // Check if key originates from this factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                if (key instanceof sun.security.provider.DSAPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                // Convert key to spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                DSAPrivateKeySpec dsaPrivKeySpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    = engineGetKeySpec(key, DSAPrivateKeySpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                // Create key from spec, and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                return engineGeneratePrivate(dsaPrivKeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                throw new InvalidKeyException("Wrong algorithm type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        } catch (InvalidKeySpecException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new InvalidKeyException("Cannot translate key: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                          + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
}