1 17 18 package org.apache.geronimo.util.jce.provider; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.IOException ; 23 import java.io.ObjectInputStream ; 24 import java.io.ObjectOutputStream ; 25 import java.math.BigInteger ; 26 import java.security.interfaces.RSAPrivateKey ; 27 import java.security.spec.RSAPrivateKeySpec ; 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 import java.util.Vector ; 31 32 import org.apache.geronimo.util.asn1.ASN1InputStream; 33 import org.apache.geronimo.util.asn1.ASN1OutputStream; 34 import org.apache.geronimo.util.asn1.DEREncodable; 35 import org.apache.geronimo.util.asn1.DERObjectIdentifier; 36 import org.apache.geronimo.util.crypto.params.RSAKeyParameters; 37 import org.apache.geronimo.util.jce.interfaces.PKCS12BagAttributeCarrier; 38 39 public class JCERSAPrivateKey 40 implements RSAPrivateKey , PKCS12BagAttributeCarrier 41 { 42 protected BigInteger modulus; 43 protected BigInteger privateExponent; 44 45 private Hashtable pkcs12Attributes = new Hashtable (); 46 private Vector pkcs12Ordering = new Vector (); 47 48 protected JCERSAPrivateKey() 49 { 50 } 51 52 JCERSAPrivateKey( 53 RSAKeyParameters key) 54 { 55 this.modulus = key.getModulus(); 56 this.privateExponent = key.getExponent(); 57 } 58 59 JCERSAPrivateKey( 60 RSAPrivateKeySpec spec) 61 { 62 this.modulus = spec.getModulus(); 63 this.privateExponent = spec.getPrivateExponent(); 64 } 65 66 JCERSAPrivateKey( 67 RSAPrivateKey key) 68 { 69 this.modulus = key.getModulus(); 70 this.privateExponent = key.getPrivateExponent(); 71 } 72 73 public BigInteger getModulus() 74 { 75 return modulus; 76 } 77 78 public BigInteger getPrivateExponent() 79 { 80 return privateExponent; 81 } 82 83 public String getAlgorithm() 84 { 85 return "RSA"; 86 } 87 88 public String getFormat() 89 { 90 return "NULL"; 91 } 92 93 public byte[] getEncoded() 94 { 95 return null; 96 } 97 98 public boolean equals(Object o) 99 { 100 if ( !(o instanceof RSAPrivateKey ) ) 101 { 102 return false; 103 } 104 105 if ( o == this ) 106 { 107 return true; 108 } 109 110 RSAPrivateKey key = (RSAPrivateKey )o; 111 112 return getModulus().equals(key.getModulus()) 113 && getPrivateExponent().equals(key.getPrivateExponent()); 114 } 115 116 public void setBagAttribute( 117 DERObjectIdentifier oid, 118 DEREncodable attribute) 119 { 120 pkcs12Attributes.put(oid, attribute); 121 pkcs12Ordering.addElement(oid); 122 } 123 124 public DEREncodable getBagAttribute( 125 DERObjectIdentifier oid) 126 { 127 return (DEREncodable)pkcs12Attributes.get(oid); 128 } 129 130 public Enumeration getBagAttributeKeys() 131 { 132 return pkcs12Ordering.elements(); 133 } 134 135 private void readObject( 136 ObjectInputStream in) 137 throws IOException , ClassNotFoundException 138 { 139 this.modulus = (BigInteger )in.readObject(); 140 141 Object obj = in.readObject(); 142 143 if (obj instanceof Hashtable ) 144 { 145 this.pkcs12Attributes = (Hashtable )obj; 146 this.pkcs12Ordering = (Vector )in.readObject(); 147 } 148 else 149 { 150 this.pkcs12Attributes = new Hashtable (); 151 this.pkcs12Ordering = new Vector (); 152 153 ByteArrayInputStream bIn = new ByteArrayInputStream ((byte[])obj); 154 ASN1InputStream aIn = new ASN1InputStream(bIn); 155 156 DERObjectIdentifier oid; 157 158 while ((oid = (DERObjectIdentifier)aIn.readObject()) != null) 159 { 160 this.setBagAttribute(oid, aIn.readObject()); 161 } 162 } 163 164 this.privateExponent = (BigInteger )in.readObject(); 165 } 166 167 private void writeObject( 168 ObjectOutputStream out) 169 throws IOException 170 { 171 out.writeObject(modulus); 172 173 if (pkcs12Ordering.size() == 0) 174 { 175 out.writeObject(pkcs12Attributes); 176 out.writeObject(pkcs12Ordering); 177 } 178 else 179 { 180 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 181 ASN1OutputStream aOut = new ASN1OutputStream(bOut); 182 183 Enumeration e = this.getBagAttributeKeys(); 184 185 while (e.hasMoreElements()) 186 { 187 DEREncodable oid = (DEREncodable)e.nextElement(); 188 189 aOut.writeObject(oid); 190 aOut.writeObject(pkcs12Attributes.get(oid)); 191 } 192 193 out.writeObject(bOut.toByteArray()); 194 } 195 196 out.writeObject(privateExponent); 197 } 198 } 199 | Popular Tags |