BCRSAPrivateKey.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.jcajce.provider.asymmetric.rsa; 2 3import java.io.IOException; 4import java.io.ObjectInputStream; 5import java.io.ObjectOutputStream; 6import java.math.BigInteger; 7import java.security.interfaces.RSAPrivateKey; 8import java.security.spec.RSAPrivateKeySpec; 9import java.util.Enumeration; 10 11import org.bouncycastle.asn1.ASN1Encodable; 12import org.bouncycastle.asn1.ASN1ObjectIdentifier; 13import org.bouncycastle.asn1.DERNull; 14import org.bouncycastle.asn1.DERObjectIdentifier; 15import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; 16import org.bouncycastle.asn1.x509.AlgorithmIdentifier; 17import org.bouncycastle.crypto.params.RSAKeyParameters; 18import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil; 19import org.bouncycastle.jcajce.provider.asymmetric.util.PKCS12BagAttributeCarrierImpl; 20import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier; 21 22public class BCRSAPrivateKey 23 implements RSAPrivateKey, PKCS12BagAttributeCarrier 24{ 25 static final long serialVersionUID = 5110188922551353628L; 26 27 private static BigInteger ZERO = BigInteger.valueOf(0); 28 29 protected BigInteger modulus; 30 protected BigInteger privateExponent; 31 32 private transient PKCS12BagAttributeCarrierImpl attrCarrier = new PKCS12BagAttributeCarrierImpl(); 33 34 protected BCRSAPrivateKey() 35 { 36 } 37 38 BCRSAPrivateKey( 39 RSAKeyParameters key) 40 { 41 this.modulus = key.getModulus(); 42 this.privateExponent = key.getExponent(); 43 } 44 45 BCRSAPrivateKey( 46 RSAPrivateKeySpec spec) 47 { 48 this.modulus = spec.getModulus(); 49 this.privateExponent = spec.getPrivateExponent(); 50 } 51 52 BCRSAPrivateKey( 53 RSAPrivateKey key) 54 { 55 this.modulus = key.getModulus(); 56 this.privateExponent = key.getPrivateExponent(); 57 } 58 59 public BigInteger getModulus() 60 { 61 return modulus; 62 } 63 64 public BigInteger getPrivateExponent() 65 { 66 return privateExponent; 67 } 68 69 public String getAlgorithm() 70 { 71 return "RSA"; 72 } 73 74 public String getFormat() 75 { 76 return "PKCS#8"; 77 } 78 79 public byte[] getEncoded() 80 { 81 // BEGIN android-changed 82 return KeyUtil.getEncodedPrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new org.bouncycastle.asn1.pkcs.RSAPrivateKey(getModulus(), ZERO, getPrivateExponent(), ZERO, ZERO, ZERO, ZERO, ZERO)); 83 // END android-changed 84 } 85 86 public boolean equals(Object o) 87 { 88 if (!(o instanceof RSAPrivateKey)) 89 { 90 return false; 91 } 92 93 if (o == this) 94 { 95 return true; 96 } 97 98 RSAPrivateKey key = (RSAPrivateKey)o; 99 100 return getModulus().equals(key.getModulus()) 101 && getPrivateExponent().equals(key.getPrivateExponent()); 102 } 103 104 public int hashCode() 105 { 106 return getModulus().hashCode() ^ getPrivateExponent().hashCode(); 107 } 108 109 public void setBagAttribute( 110 ASN1ObjectIdentifier oid, 111 ASN1Encodable attribute) 112 { 113 attrCarrier.setBagAttribute(oid, attribute); 114 } 115 116 public ASN1Encodable getBagAttribute( 117 DERObjectIdentifier oid) 118 { 119 return attrCarrier.getBagAttribute(oid); 120 } 121 122 public Enumeration getBagAttributeKeys() 123 { 124 return attrCarrier.getBagAttributeKeys(); 125 } 126 127 private void readObject( 128 ObjectInputStream in) 129 throws IOException, ClassNotFoundException 130 { 131 in.defaultReadObject(); 132 133 this.attrCarrier = new PKCS12BagAttributeCarrierImpl(); 134 } 135 136 private void writeObject( 137 ObjectOutputStream out) 138 throws IOException 139 { 140 out.defaultWriteObject(); 141 } 142} 143