001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.asn1.DEREncodable;
004: import org.bouncycastle.asn1.DERObjectIdentifier;
005: import org.bouncycastle.asn1.DERNull;
006: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
007: import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
008: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
009: import org.bouncycastle.asn1.pkcs.RSAPrivateKeyStructure;
010: import org.bouncycastle.crypto.params.RSAKeyParameters;
011: import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
012:
013: import java.io.IOException;
014: import java.io.ObjectInputStream;
015: import java.io.ObjectOutputStream;
016: import java.math.BigInteger;
017: import java.security.interfaces.RSAPrivateKey;
018: import java.security.spec.RSAPrivateKeySpec;
019: import java.util.Enumeration;
020:
021: public class JCERSAPrivateKey implements RSAPrivateKey,
022: PKCS12BagAttributeCarrier {
023: static final long serialVersionUID = 5110188922551353628L;
024:
025: private static BigInteger ZERO = BigInteger.valueOf(0);
026:
027: protected BigInteger modulus;
028: protected BigInteger privateExponent;
029:
030: private PKCS12BagAttributeCarrierImpl attrCarrier = new PKCS12BagAttributeCarrierImpl();
031:
032: protected JCERSAPrivateKey() {
033: }
034:
035: JCERSAPrivateKey(RSAKeyParameters key) {
036: this .modulus = key.getModulus();
037: this .privateExponent = key.getExponent();
038: }
039:
040: JCERSAPrivateKey(RSAPrivateKeySpec spec) {
041: this .modulus = spec.getModulus();
042: this .privateExponent = spec.getPrivateExponent();
043: }
044:
045: JCERSAPrivateKey(RSAPrivateKey key) {
046: this .modulus = key.getModulus();
047: this .privateExponent = key.getPrivateExponent();
048: }
049:
050: public BigInteger getModulus() {
051: return modulus;
052: }
053:
054: public BigInteger getPrivateExponent() {
055: return privateExponent;
056: }
057:
058: public String getAlgorithm() {
059: return "RSA";
060: }
061:
062: public String getFormat() {
063: return "PKCS#8";
064: }
065:
066: public byte[] getEncoded() {
067: PrivateKeyInfo info = new PrivateKeyInfo(
068: new AlgorithmIdentifier(
069: PKCSObjectIdentifiers.rsaEncryption,
070: new DERNull()), new RSAPrivateKeyStructure(
071: getModulus(), ZERO, getPrivateExponent(), ZERO,
072: ZERO, ZERO, ZERO, ZERO).getDERObject());
073:
074: return info.getDEREncoded();
075: }
076:
077: public boolean equals(Object o) {
078: if (!(o instanceof RSAPrivateKey)) {
079: return false;
080: }
081:
082: if (o == this ) {
083: return true;
084: }
085:
086: RSAPrivateKey key = (RSAPrivateKey) o;
087:
088: return getModulus().equals(key.getModulus())
089: && getPrivateExponent()
090: .equals(key.getPrivateExponent());
091: }
092:
093: public int hashCode() {
094: return getModulus().hashCode()
095: ^ getPrivateExponent().hashCode();
096: }
097:
098: public void setBagAttribute(DERObjectIdentifier oid,
099: DEREncodable attribute) {
100: attrCarrier.setBagAttribute(oid, attribute);
101: }
102:
103: public DEREncodable getBagAttribute(DERObjectIdentifier oid) {
104: return attrCarrier.getBagAttribute(oid);
105: }
106:
107: public Enumeration getBagAttributeKeys() {
108: return attrCarrier.getBagAttributeKeys();
109: }
110:
111: private void readObject(ObjectInputStream in) throws IOException,
112: ClassNotFoundException {
113: this .modulus = (BigInteger) in.readObject();
114: this .attrCarrier = new PKCS12BagAttributeCarrierImpl();
115:
116: attrCarrier.readObject(in);
117:
118: this .privateExponent = (BigInteger) in.readObject();
119: }
120:
121: private void writeObject(ObjectOutputStream out) throws IOException {
122: out.writeObject(modulus);
123:
124: attrCarrier.writeObject(out);
125:
126: out.writeObject(privateExponent);
127: }
128: }
|