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.RSAPublicKey ; 24 import java.security.spec.RSAPublicKeySpec ; 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.x509.AlgorithmIdentifier; 31 import org.apache.geronimo.util.asn1.x509.RSAPublicKeyStructure; 32 import org.apache.geronimo.util.asn1.x509.SubjectPublicKeyInfo; 33 import org.apache.geronimo.util.crypto.params.RSAKeyParameters; 34 35 public class JCERSAPublicKey 36 implements RSAPublicKey 37 { 38 private BigInteger modulus; 39 private BigInteger publicExponent; 40 41 JCERSAPublicKey( 42 RSAKeyParameters key) 43 { 44 this.modulus = key.getModulus(); 45 this.publicExponent = key.getExponent(); 46 } 47 48 JCERSAPublicKey( 49 RSAPublicKeySpec spec) 50 { 51 this.modulus = spec.getModulus(); 52 this.publicExponent = spec.getPublicExponent(); 53 } 54 55 JCERSAPublicKey( 56 RSAPublicKey key) 57 { 58 this.modulus = key.getModulus(); 59 this.publicExponent = key.getPublicExponent(); 60 } 61 62 JCERSAPublicKey( 63 SubjectPublicKeyInfo info) 64 { 65 try 66 { 67 RSAPublicKeyStructure pubKey = new RSAPublicKeyStructure((ASN1Sequence)info.getPublicKey()); 68 69 this.modulus = pubKey.getModulus(); 70 this.publicExponent = pubKey.getPublicExponent(); 71 } 72 catch (IOException e) 73 { 74 throw new IllegalArgumentException ("invalid info structure in RSA public key"); 75 } 76 } 77 78 83 public BigInteger getModulus() 84 { 85 return modulus; 86 } 87 88 93 public BigInteger getPublicExponent() 94 { 95 return publicExponent; 96 } 97 98 public String getAlgorithm() 99 { 100 return "RSA"; 101 } 102 103 public String getFormat() 104 { 105 return "X.509"; 106 } 107 108 public byte[] getEncoded() 109 { 110 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 111 DEROutputStream dOut = new DEROutputStream(bOut); 112 SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, new DERNull()), new RSAPublicKeyStructure(getModulus(), getPublicExponent()).getDERObject()); 113 114 try 115 { 116 dOut.writeObject(info); 117 dOut.close(); 118 } 119 catch (IOException e) 120 { 121 throw new RuntimeException ("Error encoding RSA public key"); 122 } 123 124 return bOut.toByteArray(); 125 126 } 127 128 public boolean equals(Object o) 129 { 130 if ( !(o instanceof RSAPublicKey ) ) 131 { 132 return false; 133 } 134 135 if ( o == this ) 136 { 137 return true; 138 } 139 140 RSAPublicKey key = (RSAPublicKey )o; 141 142 return getModulus().equals(key.getModulus()) 143 && getPublicExponent().equals(key.getPublicExponent()); 144 } 145 146 public String toString() 147 { 148 StringBuffer buf = new StringBuffer (); 149 String nl = System.getProperty("line.separator"); 150 151 buf.append("RSA Public Key" + nl); 152 buf.append(" modulus: " + this.getModulus().toString(16) + nl); 153 buf.append(" public exponent: " + this.getPublicExponent().toString(16) + nl); 154 155 return buf.toString(); 156 } 157 } 158 | Popular Tags |