001package org.apache.commons.ssl.org.bouncycastle.asn1.x9; 002 003import java.math.BigInteger; 004import java.util.Enumeration; 005 006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable; 007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector; 008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Integer; 009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object; 010import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive; 011import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence; 012import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject; 013import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence; 014 015public class DHDomainParameters 016 extends ASN1Object 017{ 018 private ASN1Integer p, g, q, j; 019 private DHValidationParms validationParms; 020 021 public static DHDomainParameters getInstance(ASN1TaggedObject obj, boolean explicit) 022 { 023 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 024 } 025 026 public static DHDomainParameters getInstance(Object obj) 027 { 028 if (obj == null || obj instanceof DHDomainParameters) 029 { 030 return (DHDomainParameters)obj; 031 } 032 033 if (obj instanceof ASN1Sequence) 034 { 035 return new DHDomainParameters((ASN1Sequence)obj); 036 } 037 038 throw new IllegalArgumentException("Invalid DHDomainParameters: " 039 + obj.getClass().getName()); 040 } 041 042 public DHDomainParameters(BigInteger p, BigInteger g, BigInteger q, BigInteger j, 043 DHValidationParms validationParms) 044 { 045 if (p == null) 046 { 047 throw new IllegalArgumentException("'p' cannot be null"); 048 } 049 if (g == null) 050 { 051 throw new IllegalArgumentException("'g' cannot be null"); 052 } 053 if (q == null) 054 { 055 throw new IllegalArgumentException("'q' cannot be null"); 056 } 057 058 this.p = new ASN1Integer(p); 059 this.g = new ASN1Integer(g); 060 this.q = new ASN1Integer(q); 061 this.j = new ASN1Integer(j); 062 this.validationParms = validationParms; 063 } 064 065 public DHDomainParameters(ASN1Integer p, ASN1Integer g, ASN1Integer q, ASN1Integer j, 066 DHValidationParms validationParms) 067 { 068 if (p == null) 069 { 070 throw new IllegalArgumentException("'p' cannot be null"); 071 } 072 if (g == null) 073 { 074 throw new IllegalArgumentException("'g' cannot be null"); 075 } 076 if (q == null) 077 { 078 throw new IllegalArgumentException("'q' cannot be null"); 079 } 080 081 this.p = p; 082 this.g = g; 083 this.q = q; 084 this.j = j; 085 this.validationParms = validationParms; 086 } 087 088 private DHDomainParameters(ASN1Sequence seq) 089 { 090 if (seq.size() < 3 || seq.size() > 5) 091 { 092 throw new IllegalArgumentException("Bad sequence size: " + seq.size()); 093 } 094 095 Enumeration e = seq.getObjects(); 096 this.p = ASN1Integer.getInstance(e.nextElement()); 097 this.g = ASN1Integer.getInstance(e.nextElement()); 098 this.q = ASN1Integer.getInstance(e.nextElement()); 099 100 ASN1Encodable next = getNext(e); 101 102 if (next != null && next instanceof ASN1Integer) 103 { 104 this.j = ASN1Integer.getInstance(next); 105 next = getNext(e); 106 } 107 108 if (next != null) 109 { 110 this.validationParms = DHValidationParms.getInstance(next.toASN1Primitive()); 111 } 112 } 113 114 private static ASN1Encodable getNext(Enumeration e) 115 { 116 return e.hasMoreElements() ? (ASN1Encodable)e.nextElement() : null; 117 } 118 119 public ASN1Integer getP() 120 { 121 return this.p; 122 } 123 124 public ASN1Integer getG() 125 { 126 return this.g; 127 } 128 129 public ASN1Integer getQ() 130 { 131 return this.q; 132 } 133 134 public ASN1Integer getJ() 135 { 136 return this.j; 137 } 138 139 public DHValidationParms getValidationParms() 140 { 141 return this.validationParms; 142 } 143 144 public ASN1Primitive toASN1Primitive() 145 { 146 ASN1EncodableVector v = new ASN1EncodableVector(); 147 v.add(this.p); 148 v.add(this.g); 149 v.add(this.q); 150 151 if (this.j != null) 152 { 153 v.add(this.j); 154 } 155 156 if (this.validationParms != null) 157 { 158 v.add(this.validationParms); 159 } 160 161 return new DERSequence(v); 162 } 163}