1: 
  37: 
  38: 
  39: package ;
  40: 
  41: import ;
  42: import ;
  43: import ;
  44: import ;
  45: import ;
  46: 
  47: import ;
  48: import ;
  49: import ;
  50: import ;
  51: import ;
  52: import ;
  53: import ;
  54: import ;
  55: import ;
  56: import ;
  57: 
  58: import ;
  59: import ;
  60: import ;
  61: import ;
  62: 
  63: 
  66: public class DHKeyFactory
  67:     extends KeyFactorySpi
  68: {
  69:   
  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: }