1 package org.bouncycastle.jcajce.provider.asymmetric.dsa; 2 3 import java.io.IOException; 4 import java.security.InvalidKeyException; 5 import java.security.Key; 6 import java.security.PrivateKey; 7 import java.security.PublicKey; 8 import java.security.interfaces.DSAPrivateKey; 9 import java.security.interfaces.DSAPublicKey; 10 import java.security.spec.DSAPrivateKeySpec; 11 import java.security.spec.DSAPublicKeySpec; 12 import java.security.spec.InvalidKeySpecException; 13 import java.security.spec.KeySpec; 14 15 import org.bouncycastle.asn1.ASN1ObjectIdentifier; 16 import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; 17 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; 18 import org.bouncycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi; 19 20 public class KeyFactorySpi 21 extends BaseKeyFactorySpi 22 { 23 public KeyFactorySpi() 24 { 25 } 26 27 protected KeySpec engineGetKeySpec( 28 Key key, 29 Class spec) 30 throws InvalidKeySpecException 31 { 32 if (spec.isAssignableFrom(DSAPublicKeySpec.class) && key instanceof DSAPublicKey) 33 { 34 DSAPublicKey k = (DSAPublicKey)key; 35 36 return new DSAPublicKeySpec(k.getY(), k.getParams().getP(), k.getParams().getQ(), k.getParams().getG()); 37 } 38 else if (spec.isAssignableFrom(DSAPrivateKeySpec.class) && key instanceof java.security.interfaces.DSAPrivateKey) 39 { 40 java.security.interfaces.DSAPrivateKey k = (java.security.interfaces.DSAPrivateKey)key; 41 42 return new DSAPrivateKeySpec(k.getX(), k.getParams().getP(), k.getParams().getQ(), k.getParams().getG()); 43 } 44 45 return super.engineGetKeySpec(key, spec); 46 } 47 48 protected Key engineTranslateKey( 49 Key key) 50 throws InvalidKeyException 51 { 52 if (key instanceof DSAPublicKey) 53 { 54 return new BCDSAPublicKey((DSAPublicKey)key); 55 } 56 else if (key instanceof DSAPrivateKey) 57 { 58 return new BCDSAPrivateKey((DSAPrivateKey)key); 59 } 60 61 throw new InvalidKeyException("key type unknown"); 62 } 63 64 public PrivateKey generatePrivate(PrivateKeyInfo keyInfo) 65 throws IOException 66 { 67 ASN1ObjectIdentifier algOid = keyInfo.getPrivateKeyAlgorithm().getAlgorithm(); 68 69 if (DSAUtil.isDsaOid(algOid)) 70 { 71 return new BCDSAPrivateKey(keyInfo); 72 } 73 else 74 { 75 throw new IOException("algorithm identifier " + algOid + " in key not recognised"); 76 } 77 } 78 79 public PublicKey generatePublic(SubjectPublicKeyInfo keyInfo) 80 throws IOException 81 { 82 ASN1ObjectIdentifier algOid = keyInfo.getAlgorithm().getAlgorithm(); 83 84 if (DSAUtil.isDsaOid(algOid)) 85 { 86 return new BCDSAPublicKey(keyInfo); 87 } 88 else 89 { 90 throw new IOException("algorithm identifier " + algOid + " in key not recognised"); 91 } 92 } 93 94 protected PrivateKey engineGeneratePrivate( 95 KeySpec keySpec) 96 throws InvalidKeySpecException 97 { 98 if (keySpec instanceof DSAPrivateKeySpec) 99 { 100 return new BCDSAPrivateKey((DSAPrivateKeySpec)keySpec); 101 } 102 103 return super.engineGeneratePrivate(keySpec); 104 } 105 106 protected PublicKey engineGeneratePublic( 107 KeySpec keySpec) 108 throws InvalidKeySpecException 109 { 110 if (keySpec instanceof DSAPublicKeySpec) 111 { 112 try 113 { 114 return new BCDSAPublicKey((DSAPublicKeySpec)keySpec); 115 } 116 catch (final Exception e) 117 { 118 throw new InvalidKeySpecException("invalid KeySpec: " + e.getMessage()) 119 { 120 public Throwable getCause() 121 { 122 return e; 123 } 124 }; 125 } 126 } 127 128 return super.engineGeneratePublic(keySpec); 129 } 130 } 131