1 package org.bouncycastle.asn1.x9; 2 3 import java.util.Enumeration; 4 5 import org.bouncycastle.asn1.ASN1Encodable; 6 import org.bouncycastle.asn1.ASN1EncodableVector; 7 import org.bouncycastle.asn1.ASN1Sequence; 8 import org.bouncycastle.asn1.ASN1TaggedObject; 9 import org.bouncycastle.asn1.DEREncodable; 10 import org.bouncycastle.asn1.DERInteger; 11 import org.bouncycastle.asn1.DERObject; 12 import org.bouncycastle.asn1.DERSequence; 13 14 public class DHDomainParameters 15 extends ASN1Encodable 16 { 17 private DERInteger p, g, q, j; 18 private DHValidationParms validationParms; 19 20 public static DHDomainParameters getInstance(ASN1TaggedObject obj, boolean explicit) 21 { 22 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 23 } 24 25 public static DHDomainParameters getInstance(Object obj) 26 { 27 if (obj == null || obj instanceof DHDomainParameters) 28 { 29 return (DHDomainParameters)obj; 30 } 31 32 if (obj instanceof ASN1Sequence) 33 { 34 return new DHDomainParameters((ASN1Sequence)obj); 35 } 36 37 throw new IllegalArgumentException("Invalid DHDomainParameters: " 38 + obj.getClass().getName()); 39 } 40 41 public DHDomainParameters(DERInteger p, DERInteger g, DERInteger q, DERInteger j, 42 DHValidationParms validationParms) 43 { 44 if (p == null) 45 { 46 throw new IllegalArgumentException("'p' cannot be null"); 47 } 48 if (g == null) 49 { 50 throw new IllegalArgumentException("'g' cannot be null"); 51 } 52 if (q == null) 53 { 54 throw new IllegalArgumentException("'q' cannot be null"); 55 } 56 57 this.p = p; 58 this.g = g; 59 this.q = q; 60 this.j = j; 61 this.validationParms = validationParms; 62 } 63 64 private DHDomainParameters(ASN1Sequence seq) 65 { 66 if (seq.size() < 3 || seq.size() > 5) 67 { 68 throw new IllegalArgumentException("Bad sequence size: " + seq.size()); 69 } 70 71 Enumeration e = seq.getObjects(); 72 this.p = DERInteger.getInstance(e.nextElement()); 73 this.g = DERInteger.getInstance(e.nextElement()); 74 this.q = DERInteger.getInstance(e.nextElement()); 75 76 DEREncodable next = getNext(e); 77 78 if (next != null && next instanceof DERInteger) 79 { 80 this.j = DERInteger.getInstance(next); 81 next = getNext(e); 82 } 83 84 if (next != null) 85 { 86 this.validationParms = DHValidationParms.getInstance(next.getDERObject()); 87 } 88 } 89 90 private static DEREncodable getNext(Enumeration e) 91 { 92 return e.hasMoreElements() ? (DEREncodable)e.nextElement() : null; 93 } 94 95 public DERInteger getP() 96 { 97 return this.p; 98 } 99 100 public DERInteger getG() 101 { 102 return this.g; 103 } 104 105 public DERInteger getQ() 106 { 107 return this.q; 108 } 109 110 public DERInteger getJ() 111 { 112 return this.j; 113 } 114 115 public DHValidationParms getValidationParms() 116 { 117 return this.validationParms; 118 } 119 120 public DERObject toASN1Object() 121 { 122 ASN1EncodableVector v = new ASN1EncodableVector(); 123 v.add(this.p); 124 v.add(this.g); 125 v.add(this.q); 126 127 if (this.j != null) 128 { 129 v.add(this.j); 130 } 131 132 if (this.validationParms != null) 133 { 134 v.add(this.validationParms); 135 } 136 137 return new DERSequence(v); 138 } 139 } 140