1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 package com.android.org.bouncycastle.jcajce.provider.asymmetric.x509; 3 4 import java.security.InvalidKeyException; 5 import java.security.Key; 6 import java.security.KeyFactorySpi; 7 import java.security.PrivateKey; 8 import java.security.PublicKey; 9 import java.security.spec.InvalidKeySpecException; 10 import java.security.spec.KeySpec; 11 import java.security.spec.PKCS8EncodedKeySpec; 12 import java.security.spec.X509EncodedKeySpec; 13 14 import com.android.org.bouncycastle.asn1.pkcs.PrivateKeyInfo; 15 import com.android.org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; 16 import com.android.org.bouncycastle.jce.provider.BouncyCastleProvider; 17 18 /** 19 * @hide This class is not part of the Android public SDK API 20 */ 21 public class KeyFactory 22 extends KeyFactorySpi 23 { 24 25 protected PrivateKey engineGeneratePrivate( 26 KeySpec keySpec) 27 throws InvalidKeySpecException 28 { 29 if (keySpec instanceof PKCS8EncodedKeySpec) 30 { 31 try 32 { 33 PrivateKeyInfo info = PrivateKeyInfo.getInstance(((PKCS8EncodedKeySpec)keySpec).getEncoded()); 34 PrivateKey key = BouncyCastleProvider.getPrivateKey(info); 35 36 if (key != null) 37 { 38 return key; 39 } 40 41 throw new InvalidKeySpecException("no factory found for OID: " + info.getPrivateKeyAlgorithm().getAlgorithm()); 42 } 43 catch (Exception e) 44 { 45 throw new InvalidKeySpecException(e.toString()); 46 } 47 } 48 49 throw new InvalidKeySpecException("Unknown KeySpec type: " + keySpec.getClass().getName()); 50 } 51 52 protected PublicKey engineGeneratePublic( 53 KeySpec keySpec) 54 throws InvalidKeySpecException 55 { 56 if (keySpec instanceof X509EncodedKeySpec) 57 { 58 try 59 { 60 SubjectPublicKeyInfo info = SubjectPublicKeyInfo.getInstance(((X509EncodedKeySpec)keySpec).getEncoded()); 61 PublicKey key = BouncyCastleProvider.getPublicKey(info); 62 63 if (key != null) 64 { 65 return key; 66 } 67 68 throw new InvalidKeySpecException("no factory found for OID: " + info.getAlgorithm().getAlgorithm()); 69 } 70 catch (Exception e) 71 { 72 throw new InvalidKeySpecException(e.toString()); 73 } 74 } 75 76 throw new InvalidKeySpecException("Unknown KeySpec type: " + keySpec.getClass().getName()); 77 } 78 79 protected KeySpec engineGetKeySpec(Key key, Class keySpec) 80 throws InvalidKeySpecException 81 { 82 if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class) && key.getFormat().equals("PKCS#8")) 83 { 84 return new PKCS8EncodedKeySpec(key.getEncoded()); 85 } 86 else if (keySpec.isAssignableFrom(X509EncodedKeySpec.class) && key.getFormat().equals("X.509")) 87 { 88 return new X509EncodedKeySpec(key.getEncoded()); 89 } 90 91 throw new InvalidKeySpecException("not implemented yet " + key + " " + keySpec); 92 } 93 94 protected Key engineTranslateKey(Key key) 95 throws InvalidKeyException 96 { 97 throw new InvalidKeyException("not implemented yet " + key); 98 } 99 }