1 17 18 package org.apache.geronimo.util.asn1.pkcs; 19 20 import java.math.BigInteger ; 21 import java.util.Enumeration ; 22 23 import org.apache.geronimo.util.asn1.ASN1Encodable; 24 import org.apache.geronimo.util.asn1.ASN1EncodableVector; 25 import org.apache.geronimo.util.asn1.ASN1Sequence; 26 import org.apache.geronimo.util.asn1.ASN1TaggedObject; 27 import org.apache.geronimo.util.asn1.DERInteger; 28 import org.apache.geronimo.util.asn1.DERObject; 29 import org.apache.geronimo.util.asn1.DERSequence; 30 31 public class RSAPrivateKeyStructure 32 extends ASN1Encodable 33 { 34 private int version; 35 private BigInteger modulus; 36 private BigInteger publicExponent; 37 private BigInteger privateExponent; 38 private BigInteger prime1; 39 private BigInteger prime2; 40 private BigInteger exponent1; 41 private BigInteger exponent2; 42 private BigInteger coefficient; 43 private ASN1Sequence otherPrimeInfos = null; 44 45 public static RSAPrivateKeyStructure getInstance( 46 ASN1TaggedObject obj, 47 boolean explicit) 48 { 49 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 50 } 51 52 public static RSAPrivateKeyStructure getInstance( 53 Object obj) 54 { 55 if (obj instanceof RSAPrivateKeyStructure) 56 { 57 return (RSAPrivateKeyStructure)obj; 58 } 59 else if (obj instanceof ASN1Sequence) 60 { 61 return new RSAPrivateKeyStructure((ASN1Sequence)obj); 62 } 63 64 throw new IllegalArgumentException ("unknown object in factory"); 65 } 66 67 public RSAPrivateKeyStructure( 68 BigInteger modulus, 69 BigInteger publicExponent, 70 BigInteger privateExponent, 71 BigInteger prime1, 72 BigInteger prime2, 73 BigInteger exponent1, 74 BigInteger exponent2, 75 BigInteger coefficient) 76 { 77 this.version = 0; 78 this.modulus = modulus; 79 this.publicExponent = publicExponent; 80 this.privateExponent = privateExponent; 81 this.prime1 = prime1; 82 this.prime2 = prime2; 83 this.exponent1 = exponent1; 84 this.exponent2 = exponent2; 85 this.coefficient = coefficient; 86 } 87 88 public RSAPrivateKeyStructure( 89 ASN1Sequence seq) 90 { 91 Enumeration e = seq.getObjects(); 92 93 BigInteger v = ((DERInteger)e.nextElement()).getValue(); 94 if (v.intValue() != 0 && v.intValue() != 1) 95 { 96 throw new IllegalArgumentException ("wrong version for RSA private key"); 97 } 98 99 version = v.intValue(); 100 modulus = ((DERInteger)e.nextElement()).getValue(); 101 publicExponent = ((DERInteger)e.nextElement()).getValue(); 102 privateExponent = ((DERInteger)e.nextElement()).getValue(); 103 prime1 = ((DERInteger)e.nextElement()).getValue(); 104 prime2 = ((DERInteger)e.nextElement()).getValue(); 105 exponent1 = ((DERInteger)e.nextElement()).getValue(); 106 exponent2 = ((DERInteger)e.nextElement()).getValue(); 107 coefficient = ((DERInteger)e.nextElement()).getValue(); 108 109 if (e.hasMoreElements()) 110 { 111 otherPrimeInfos = (ASN1Sequence)e.nextElement(); 112 } 113 } 114 115 public int getVersion() 116 { 117 return version; 118 } 119 120 public BigInteger getModulus() 121 { 122 return modulus; 123 } 124 125 public BigInteger getPublicExponent() 126 { 127 return publicExponent; 128 } 129 130 public BigInteger getPrivateExponent() 131 { 132 return privateExponent; 133 } 134 135 public BigInteger getPrime1() 136 { 137 return prime1; 138 } 139 140 public BigInteger getPrime2() 141 { 142 return prime2; 143 } 144 145 public BigInteger getExponent1() 146 { 147 return exponent1; 148 } 149 150 public BigInteger getExponent2() 151 { 152 return exponent2; 153 } 154 155 public BigInteger getCoefficient() 156 { 157 return coefficient; 158 } 159 160 182 public DERObject toASN1Object() 183 { 184 ASN1EncodableVector v = new ASN1EncodableVector(); 185 186 v.add(new DERInteger(version)); v.add(new DERInteger(getModulus())); 188 v.add(new DERInteger(getPublicExponent())); 189 v.add(new DERInteger(getPrivateExponent())); 190 v.add(new DERInteger(getPrime1())); 191 v.add(new DERInteger(getPrime2())); 192 v.add(new DERInteger(getExponent1())); 193 v.add(new DERInteger(getExponent2())); 194 v.add(new DERInteger(getCoefficient())); 195 196 if (otherPrimeInfos != null) 197 { 198 v.add(otherPrimeInfos); 199 } 200 201 return new DERSequence(v); 202 } 203 } 204 | Popular Tags |