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