1package org.bouncycastle.asn1.x9; 2 3import java.util.Enumeration; 4 5import org.bouncycastle.asn1.ASN1Encodable; 6import org.bouncycastle.asn1.ASN1EncodableVector; 7import org.bouncycastle.asn1.ASN1Integer; 8import org.bouncycastle.asn1.ASN1Object; 9import org.bouncycastle.asn1.ASN1Primitive; 10import org.bouncycastle.asn1.ASN1Sequence; 11import org.bouncycastle.asn1.ASN1TaggedObject; 12import org.bouncycastle.asn1.DERSequence; 13 14public class DHDomainParameters 15 extends ASN1Object 16{ 17 private ASN1Integer 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(ASN1Integer p, ASN1Integer g, ASN1Integer q, ASN1Integer 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 = ASN1Integer.getInstance(e.nextElement()); 73 this.g = ASN1Integer.getInstance(e.nextElement()); 74 this.q = ASN1Integer.getInstance(e.nextElement()); 75 76 ASN1Encodable next = getNext(e); 77 78 if (next != null && next instanceof ASN1Integer) 79 { 80 this.j = ASN1Integer.getInstance(next); 81 next = getNext(e); 82 } 83 84 if (next != null) 85 { 86 this.validationParms = DHValidationParms.getInstance(next.toASN1Primitive()); 87 } 88 } 89 90 private static ASN1Encodable getNext(Enumeration e) 91 { 92 return e.hasMoreElements() ? (ASN1Encodable)e.nextElement() : null; 93 } 94 95 public ASN1Integer getP() 96 { 97 return this.p; 98 } 99 100 public ASN1Integer getG() 101 { 102 return this.g; 103 } 104 105 public ASN1Integer getQ() 106 { 107 return this.q; 108 } 109 110 public ASN1Integer getJ() 111 { 112 return this.j; 113 } 114 115 public DHValidationParms getValidationParms() 116 { 117 return this.validationParms; 118 } 119 120 public ASN1Primitive toASN1Primitive() 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