1 17 18 package org.apache.geronimo.util.jce.provider; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.math.BigInteger ; 23 import java.security.interfaces.RSAPrivateCrtKey ; 24 import java.security.spec.RSAPrivateCrtKeySpec ; 25 26 import org.apache.geronimo.util.asn1.ASN1Sequence; 27 import org.apache.geronimo.util.asn1.DERNull; 28 import org.apache.geronimo.util.asn1.DEROutputStream; 29 import org.apache.geronimo.util.asn1.pkcs.PKCSObjectIdentifiers; 30 import org.apache.geronimo.util.asn1.pkcs.PrivateKeyInfo; 31 import org.apache.geronimo.util.asn1.pkcs.RSAPrivateKeyStructure; 32 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier; 33 import org.apache.geronimo.util.crypto.params.RSAPrivateCrtKeyParameters; 34 35 38 public class JCERSAPrivateCrtKey 39 extends JCERSAPrivateKey 40 implements RSAPrivateCrtKey 41 { 42 private BigInteger publicExponent; 43 private BigInteger primeP; 44 private BigInteger primeQ; 45 private BigInteger primeExponentP; 46 private BigInteger primeExponentQ; 47 private BigInteger crtCoefficient; 48 49 54 JCERSAPrivateCrtKey( 55 RSAPrivateCrtKeyParameters key) 56 { 57 super(key); 58 59 this.publicExponent = key.getPublicExponent(); 60 this.primeP = key.getP(); 61 this.primeQ = key.getQ(); 62 this.primeExponentP = key.getDP(); 63 this.primeExponentQ = key.getDQ(); 64 this.crtCoefficient = key.getQInv(); 65 } 66 67 72 JCERSAPrivateCrtKey( 73 RSAPrivateCrtKeySpec spec) 74 { 75 this.modulus = spec.getModulus(); 76 this.publicExponent = spec.getPublicExponent(); 77 this.privateExponent = spec.getPrivateExponent(); 78 this.primeP = spec.getPrimeP(); 79 this.primeQ = spec.getPrimeQ(); 80 this.primeExponentP = spec.getPrimeExponentP(); 81 this.primeExponentQ = spec.getPrimeExponentQ(); 82 this.crtCoefficient = spec.getCrtCoefficient(); 83 } 84 85 90 JCERSAPrivateCrtKey( 91 RSAPrivateCrtKey key) 92 { 93 this.modulus = key.getModulus(); 94 this.publicExponent = key.getPublicExponent(); 95 this.privateExponent = key.getPrivateExponent(); 96 this.primeP = key.getPrimeP(); 97 this.primeQ = key.getPrimeQ(); 98 this.primeExponentP = key.getPrimeExponentP(); 99 this.primeExponentQ = key.getPrimeExponentQ(); 100 this.crtCoefficient = key.getCrtCoefficient(); 101 } 102 103 106 JCERSAPrivateCrtKey( 107 PrivateKeyInfo info) 108 { 109 this(new RSAPrivateKeyStructure((ASN1Sequence)info.getPrivateKey())); 110 } 111 112 115 JCERSAPrivateCrtKey( 116 RSAPrivateKeyStructure key) 117 { 118 this.modulus = key.getModulus(); 119 this.publicExponent = key.getPublicExponent(); 120 this.privateExponent = key.getPrivateExponent(); 121 this.primeP = key.getPrime1(); 122 this.primeQ = key.getPrime2(); 123 this.primeExponentP = key.getExponent1(); 124 this.primeExponentQ = key.getExponent2(); 125 this.crtCoefficient = key.getCoefficient(); 126 } 127 128 133 public String getFormat() 134 { 135 return "PKCS#8"; 136 } 137 138 144 public byte[] getEncoded() 145 { 146 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 147 DEROutputStream dOut = new DEROutputStream(bOut); 148 PrivateKeyInfo info = new PrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, new DERNull()), new RSAPrivateKeyStructure(getModulus(), getPublicExponent(), getPrivateExponent(), getPrimeP(), getPrimeQ(), getPrimeExponentP(), getPrimeExponentQ(), getCrtCoefficient()).getDERObject()); 149 150 try 151 { 152 dOut.writeObject(info); 153 dOut.close(); 154 } 155 catch (IOException e) 156 { 157 throw new RuntimeException ("Error encoding RSA public key"); 158 } 159 160 return bOut.toByteArray(); 161 } 162 163 168 public BigInteger getPublicExponent() 169 { 170 return publicExponent; 171 } 172 173 178 public BigInteger getPrimeP() 179 { 180 return primeP; 181 } 182 183 188 public BigInteger getPrimeQ() 189 { 190 return primeQ; 191 } 192 193 198 public BigInteger getPrimeExponentP() 199 { 200 return primeExponentP; 201 } 202 203 208 public BigInteger getPrimeExponentQ() 209 { 210 return primeExponentQ; 211 } 212 213 218 public BigInteger getCrtCoefficient() 219 { 220 return crtCoefficient; 221 } 222 223 public boolean equals(Object o) 224 { 225 if ( !(o instanceof RSAPrivateCrtKey ) ) 226 { 227 return false; 228 } 229 230 if ( o == this ) 231 { 232 return true; 233 } 234 235 RSAPrivateCrtKey key = (RSAPrivateCrtKey )o; 236 237 return this.getModulus().equals(key.getModulus()) 238 && this.getPublicExponent().equals(key.getPublicExponent()) 239 && this.getPrivateExponent().equals(key.getPrivateExponent()) 240 && this.getPrimeP().equals(key.getPrimeP()) 241 && this.getPrimeQ().equals(key.getPrimeQ()) 242 && this.getPrimeExponentP().equals(key.getPrimeExponentP()) 243 && this.getPrimeExponentQ().equals(key.getPrimeExponentQ()) 244 && this.getCrtCoefficient().equals(key.getCrtCoefficient()); 245 } 246 247 public String toString() 248 { 249 StringBuffer buf = new StringBuffer (); 250 String nl = System.getProperty("line.separator"); 251 252 buf.append("RSA Private CRT Key" + nl); 253 buf.append(" modulus: " + this.getModulus().toString(16) + nl); 254 buf.append(" public exponent: " + this.getPublicExponent().toString(16) + nl); 255 buf.append(" private exponent: " + this.getPrivateExponent().toString(16) + nl); 256 buf.append(" primeP: " + this.getPrimeP().toString(16) + nl); 257 buf.append(" primeQ: " + this.getPrimeQ().toString(16) + nl); 258 buf.append(" primeExponentP: " + this.getPrimeExponentP().toString(16) + nl); 259 buf.append(" primeExponentQ: " + this.getPrimeExponentQ().toString(16) + nl); 260 buf.append(" crtCoefficient: " + this.getCrtCoefficient().toString(16) + nl); 261 262 return buf.toString(); 263 } 264 } 265 | Popular Tags |