jdk/src/share/classes/com/sun/crypto/provider/DHKeyFactory.java
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 18809 97f5713a0f1a
equal deleted inserted replaced
10335:3c7eda3ab2f5 10336:0bb1999251f8
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2011, 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
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package com.sun.crypto.provider;
    26 package com.sun.crypto.provider;
    27 
    27 
    28 import java.util.*;
       
    29 import java.lang.*;
       
    30 import java.security.Key;
    28 import java.security.Key;
    31 import java.security.PublicKey;
    29 import java.security.PublicKey;
    32 import java.security.PrivateKey;
    30 import java.security.PrivateKey;
    33 import java.security.KeyFactorySpi;
    31 import java.security.KeyFactorySpi;
    34 import java.security.InvalidKeyException;
    32 import java.security.InvalidKeyException;
   138      *
   136      *
   139      * @exception InvalidKeySpecException if the requested key specification is
   137      * @exception InvalidKeySpecException if the requested key specification is
   140      * inappropriate for the given key, or the given key cannot be processed
   138      * inappropriate for the given key, or the given key cannot be processed
   141      * (e.g., the given key has an unrecognized algorithm or format).
   139      * (e.g., the given key has an unrecognized algorithm or format).
   142      */
   140      */
   143     protected KeySpec engineGetKeySpec(Key key, Class keySpec)
   141     protected <T extends KeySpec>
       
   142         T engineGetKeySpec(Key key, Class<T> keySpec)
   144         throws InvalidKeySpecException {
   143         throws InvalidKeySpecException {
   145         DHParameterSpec params;
   144         DHParameterSpec params;
   146 
   145 
   147         if (key instanceof javax.crypto.interfaces.DHPublicKey) {
   146         if (key instanceof javax.crypto.interfaces.DHPublicKey) {
   148 
   147 
   149             if (DHPublicKeySpec.class.isAssignableFrom(keySpec)) {
   148             if (DHPublicKeySpec.class.isAssignableFrom(keySpec)) {
   150                 javax.crypto.interfaces.DHPublicKey dhPubKey
   149                 javax.crypto.interfaces.DHPublicKey dhPubKey
   151                     = (javax.crypto.interfaces.DHPublicKey) key;
   150                     = (javax.crypto.interfaces.DHPublicKey) key;
   152                 params = dhPubKey.getParams();
   151                 params = dhPubKey.getParams();
   153                 return new DHPublicKeySpec(dhPubKey.getY(),
   152                 return keySpec.cast(new DHPublicKeySpec(dhPubKey.getY(),
   154                                            params.getP(),
   153                                                         params.getP(),
   155                                            params.getG());
   154                                                         params.getG()));
   156 
   155 
   157             } else if (X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
   156             } else if (X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
   158                 return new X509EncodedKeySpec(key.getEncoded());
   157                 return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
   159 
   158 
   160             } else {
   159             } else {
   161                 throw new InvalidKeySpecException
   160                 throw new InvalidKeySpecException
   162                     ("Inappropriate key specification");
   161                     ("Inappropriate key specification");
   163             }
   162             }
   166 
   165 
   167             if (DHPrivateKeySpec.class.isAssignableFrom(keySpec)) {
   166             if (DHPrivateKeySpec.class.isAssignableFrom(keySpec)) {
   168                 javax.crypto.interfaces.DHPrivateKey dhPrivKey
   167                 javax.crypto.interfaces.DHPrivateKey dhPrivKey
   169                     = (javax.crypto.interfaces.DHPrivateKey)key;
   168                     = (javax.crypto.interfaces.DHPrivateKey)key;
   170                 params = dhPrivKey.getParams();
   169                 params = dhPrivKey.getParams();
   171                 return new DHPrivateKeySpec(dhPrivKey.getX(),
   170                 return keySpec.cast(new DHPrivateKeySpec(dhPrivKey.getX(),
   172                                             params.getP(),
   171                                                          params.getP(),
   173                                             params.getG());
   172                                                          params.getG()));
   174 
   173 
   175             } else if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
   174             } else if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
   176                 return new PKCS8EncodedKeySpec(key.getEncoded());
   175                 return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
   177 
   176 
   178             } else {
   177             } else {
   179                 throw new InvalidKeySpecException
   178                 throw new InvalidKeySpecException
   180                     ("Inappropriate key specification");
   179                     ("Inappropriate key specification");
   181             }
   180             }
   206                 if (key instanceof com.sun.crypto.provider.DHPublicKey) {
   205                 if (key instanceof com.sun.crypto.provider.DHPublicKey) {
   207                     return key;
   206                     return key;
   208                 }
   207                 }
   209                 // Convert key to spec
   208                 // Convert key to spec
   210                 DHPublicKeySpec dhPubKeySpec
   209                 DHPublicKeySpec dhPubKeySpec
   211                     = (DHPublicKeySpec)engineGetKeySpec
   210                     = engineGetKeySpec(key, DHPublicKeySpec.class);
   212                     (key, DHPublicKeySpec.class);
       
   213                 // Create key from spec, and return it
   211                 // Create key from spec, and return it
   214                 return engineGeneratePublic(dhPubKeySpec);
   212                 return engineGeneratePublic(dhPubKeySpec);
   215 
   213 
   216             } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
   214             } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
   217                 // Check if key originates from this factory
   215                 // Check if key originates from this factory
   218                 if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
   216                 if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
   219                     return key;
   217                     return key;
   220                 }
   218                 }
   221                 // Convert key to spec
   219                 // Convert key to spec
   222                 DHPrivateKeySpec dhPrivKeySpec
   220                 DHPrivateKeySpec dhPrivKeySpec
   223                     = (DHPrivateKeySpec)engineGetKeySpec
   221                     = engineGetKeySpec(key, DHPrivateKeySpec.class);
   224                     (key, DHPrivateKeySpec.class);
       
   225                 // Create key from spec, and return it
   222                 // Create key from spec, and return it
   226                 return engineGeneratePrivate(dhPrivKeySpec);
   223                 return engineGeneratePrivate(dhPrivKeySpec);
   227 
   224 
   228             } else {
   225             } else {
   229                 throw new InvalidKeyException("Wrong algorithm type");
   226                 throw new InvalidKeyException("Wrong algorithm type");