Source for gnu.javax.crypto.jce.sig.DHKeyFactory

   1: /* DHKeyFactory.java -- DH key-factory JCE Adapter
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.javax.crypto.jce.sig;
  40: 
  41: import gnu.java.security.Registry;
  42: import gnu.javax.crypto.key.dh.DHKeyPairPKCS8Codec;
  43: import gnu.javax.crypto.key.dh.DHKeyPairX509Codec;
  44: import gnu.javax.crypto.key.dh.GnuDHPrivateKey;
  45: import gnu.javax.crypto.key.dh.GnuDHPublicKey;
  46: 
  47: import java.math.BigInteger;
  48: import java.security.InvalidKeyException;
  49: import java.security.Key;
  50: import java.security.KeyFactorySpi;
  51: import java.security.PrivateKey;
  52: import java.security.PublicKey;
  53: import java.security.spec.InvalidKeySpecException;
  54: import java.security.spec.KeySpec;
  55: import java.security.spec.PKCS8EncodedKeySpec;
  56: import java.security.spec.X509EncodedKeySpec;
  57: 
  58: import javax.crypto.interfaces.DHPrivateKey;
  59: import javax.crypto.interfaces.DHPublicKey;
  60: import javax.crypto.spec.DHPrivateKeySpec;
  61: import javax.crypto.spec.DHPublicKeySpec;
  62: 
  63: /**
  64:  * Implementation of a JCE Adapter for DH a key-factory.
  65:  */
  66: public class DHKeyFactory
  67:     extends KeyFactorySpi
  68: {
  69:   // implicit 0-arguments constructor
  70: 
  71:   protected PublicKey engineGeneratePublic(KeySpec keySpec)
  72:       throws InvalidKeySpecException
  73:   {
  74:     if (keySpec instanceof DHPublicKeySpec)
  75:       {
  76:         DHPublicKeySpec spec = (DHPublicKeySpec) keySpec;
  77:         BigInteger p = spec.getP();
  78:         BigInteger g = spec.getG();
  79:         BigInteger y = spec.getY();
  80:         return new GnuDHPublicKey(Registry.X509_ENCODING_ID, null, p, g, y);
  81:       }
  82:     if (keySpec instanceof X509EncodedKeySpec)
  83:       {
  84:         X509EncodedKeySpec spec = (X509EncodedKeySpec) keySpec;
  85:         byte[] encoded = spec.getEncoded();
  86:         PublicKey result;
  87:         try
  88:           {
  89:             result = new DHKeyPairX509Codec().decodePublicKey(encoded);
  90:             return result;
  91:           }
  92:         catch (RuntimeException x)
  93:           {
  94:             InvalidKeySpecException y = new InvalidKeySpecException();
  95:             y.initCause(x);
  96:             throw y;
  97:           }
  98:       }
  99:     throw new InvalidKeySpecException("Unsupported (public) key specification");
 100:   }
 101: 
 102:   protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
 103:       throws InvalidKeySpecException
 104:   {
 105:     if (keySpec instanceof DHPrivateKeySpec)
 106:       {
 107:         DHPrivateKeySpec spec = (DHPrivateKeySpec) keySpec;
 108:         BigInteger p = spec.getP();
 109:         BigInteger g = spec.getG();
 110:         BigInteger x = spec.getX();
 111:         return new GnuDHPrivateKey(Registry.PKCS8_ENCODING_ID, null, p, g, x);
 112:       }
 113:     if (keySpec instanceof PKCS8EncodedKeySpec)
 114:       {
 115:         PKCS8EncodedKeySpec spec = (PKCS8EncodedKeySpec) keySpec;
 116:         byte[] encoded = spec.getEncoded();
 117:         PrivateKey result;
 118:         try
 119:           {
 120:             result = new DHKeyPairPKCS8Codec().decodePrivateKey(encoded);
 121:             return result;
 122:           }
 123:         catch (RuntimeException x)
 124:           {
 125:             InvalidKeySpecException y = new InvalidKeySpecException();
 126:             y.initCause(x);
 127:             throw y;
 128:           }
 129:       }
 130:     throw new InvalidKeySpecException("Unsupported (private) key specification");
 131:   }
 132: 
 133:   protected KeySpec engineGetKeySpec(Key key, Class keySpec)
 134:       throws InvalidKeySpecException
 135:   {
 136:     if (key instanceof DHPublicKey)
 137:       {
 138:         if (keySpec.isAssignableFrom(DHPublicKeySpec.class))
 139:           {
 140:             DHPublicKey dssKey = (DHPublicKey) key;
 141:             BigInteger p = dssKey.getParams().getP();
 142:             BigInteger g = dssKey.getParams().getG();
 143:             BigInteger y = dssKey.getY();
 144:             return new DHPublicKeySpec(y, p, g);
 145:           }
 146:         if (keySpec.isAssignableFrom(X509EncodedKeySpec.class))
 147:           {
 148:             if (key instanceof GnuDHPublicKey)
 149:               {
 150:                 GnuDHPublicKey dhKey = (GnuDHPublicKey) key;
 151:                 byte[] encoded = dhKey.getEncoded(Registry.X509_ENCODING_ID);
 152:                 return new X509EncodedKeySpec(encoded);
 153:               }
 154:             if (Registry.X509_ENCODING_SORT_NAME.equalsIgnoreCase(key.getFormat()))
 155:               {
 156:                 byte[] encoded = key.getEncoded();
 157:                 return new X509EncodedKeySpec(encoded);
 158:               }
 159:             throw new InvalidKeySpecException(
 160:                 "Wrong key type or unsupported (public) key specification");
 161:           }
 162:         throw new InvalidKeySpecException("Unsupported (public) key specification");
 163:       }
 164:     if (key instanceof DHPrivateKey)
 165:       {
 166:         if (keySpec.isAssignableFrom(DHPrivateKeySpec.class))
 167:           {
 168:             DHPrivateKey dhKey = (DHPrivateKey) key;
 169:             BigInteger p = dhKey.getParams().getP();
 170:             BigInteger g = dhKey.getParams().getG();
 171:             BigInteger x = dhKey.getX();
 172:             return new DHPrivateKeySpec(x, p, g);
 173:           }
 174:         if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class))
 175:           {
 176:             if (key instanceof GnuDHPrivateKey)
 177:               {
 178:                 GnuDHPrivateKey dhKey = (GnuDHPrivateKey) key;
 179:                 byte[] encoded = dhKey.getEncoded(Registry.PKCS8_ENCODING_ID);
 180:                 return new PKCS8EncodedKeySpec(encoded);
 181:               }
 182:             if (Registry.PKCS8_ENCODING_SHORT_NAME.equalsIgnoreCase(key.getFormat()))
 183:               {
 184:                 byte[] encoded = key.getEncoded();
 185:                 return new PKCS8EncodedKeySpec(encoded);
 186:               }
 187:             throw new InvalidKeySpecException(
 188:                 "Wrong key type or unsupported (private) key specification");
 189:           }
 190:         throw new InvalidKeySpecException(
 191:             "Unsupported (private) key specification");
 192:       }
 193:     throw new InvalidKeySpecException(
 194:         "Wrong key type or unsupported key specification");
 195:   }
 196: 
 197:   protected Key engineTranslateKey(Key key) throws InvalidKeyException
 198:   {
 199:     if ((key instanceof GnuDHPublicKey) || (key instanceof GnuDHPrivateKey))
 200:       return key;
 201:     if (key instanceof DHPublicKey)
 202:       {
 203:         DHPublicKey dsaKey = (DHPublicKey) key;
 204:         BigInteger p = dsaKey.getParams().getP();
 205:         BigInteger g = dsaKey.getParams().getG();
 206:         BigInteger y = dsaKey.getY();
 207:         return new GnuDHPublicKey(Registry.X509_ENCODING_ID, null, p, g, y);
 208:       }
 209:     if (key instanceof DHPrivateKey)
 210:       {
 211:         DHPrivateKey dsaKey = (DHPrivateKey) key;
 212:         BigInteger p = dsaKey.getParams().getP();
 213:         BigInteger g = dsaKey.getParams().getG();
 214:         BigInteger x = dsaKey.getX();
 215:         return new GnuDHPrivateKey(Registry.PKCS8_ENCODING_ID, null, p, g, x);
 216:       }
 217:     throw new InvalidKeyException("Wrong key type");
 218:   }
 219: }