1: 
  37: 
  38: 
  39: package ;
  40: 
  41: import ;
  42: 
  43: import ;
  44: import ;
  45: import ;
  46: import ;
  47: import ;
  48: import ;
  49: 
  50: import ;
  51: import ;
  52: import ;
  53: import ;
  54: import ;
  55: import ;
  56: import ;
  57: 
  58: import ;
  59: import ;
  60: 
  61: 
  64: public class DHParameters
  65:     extends AlgorithmParametersSpi
  66: {
  67:   
  68:   private BigInteger p;
  69: 
  70:   
  71:   private BigInteger g;
  72: 
  73:   
  74:   private BigInteger q;
  75: 
  76:   
  77:   private int l;
  78: 
  79:   
  80: 
  81:   protected void engineInit(AlgorithmParameterSpec spec)
  82:       throws InvalidParameterSpecException
  83:   {
  84:     if (! (spec instanceof DHParameterSpec))
  85:       throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
  86:                                               + spec.getClass().getName());
  87:     DHParameterSpec dhSpec = (DHParameterSpec) spec;
  88:     p = dhSpec.getP();
  89:     g = dhSpec.getG();
  90:     l = dhSpec.getL();
  91:   }
  92: 
  93:   
 105:   protected void engineInit(byte[] params) throws IOException
 106:   {
 107:     DERReader der = new DERReader(params);
 108: 
 109:     DERValue derParams = der.read();
 110:     DerUtil.checkIsConstructed(derParams, "Wrong DH Parameters field");
 111: 
 112:     DERValue val = der.read();
 113:     DerUtil.checkIsBigInteger(val, "Wrong P field");
 114:     p = (BigInteger) val.getValue();
 115:     val = der.read();
 116:     DerUtil.checkIsBigInteger(val, "Wrong G field");
 117:     g = (BigInteger) val.getValue();
 118:     val = der.read();
 119:     DerUtil.checkIsBigInteger(val, "Wrong Q field");
 120:     q = (BigInteger) val.getValue();
 121:     l = q.bitLength();
 122:   }
 123: 
 124:   protected void engineInit(byte[] params, String format) throws IOException
 125:   {
 126:     if (format != null)
 127:       {
 128:         format = format.trim();
 129:         if (format.length() == 0)
 130:           throw new IOException("Format MUST NOT be an empty string");
 131: 
 132:         if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
 133:           throw new IOException("Unknown or unsupported format: " + format);
 134:       }
 135: 
 136:     engineInit(params);
 137:   }
 138: 
 139:   protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
 140:       throws InvalidParameterSpecException
 141:   {
 142:     if (paramSpec.isAssignableFrom(DHParameterSpec.class))
 143:       return new DHParameterSpec(p, g, l);
 144: 
 145:     if (paramSpec.isAssignableFrom(DHGenParameterSpec.class))
 146:       return new DHGenParameterSpec(p.bitLength(), l);
 147: 
 148:     throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
 149:                                             + paramSpec.getName());
 150:   }
 151: 
 152:   
 164:   protected byte[] engineGetEncoded() throws IOException
 165:   {
 166:     DERValue derP = new DERValue(DER.INTEGER, p);
 167:     DERValue derG = new DERValue(DER.INTEGER, g);
 168:     DERValue derQ = new DERValue(DER.INTEGER, q);
 169: 
 170:     ArrayList params = new ArrayList(3);
 171:     params.add(derP);
 172:     params.add(derG);
 173:     params.add(derQ);
 174:     DERValue derParams = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, params);
 175: 
 176:     ByteArrayOutputStream baos = new ByteArrayOutputStream();
 177:     DERWriter.write(baos, derParams);
 178:     byte[] result = baos.toByteArray();
 179: 
 180:     return result;
 181:   }
 182: 
 183:   protected byte[] engineGetEncoded(String format) throws IOException
 184:   {
 185:     if (format != null)
 186:       {
 187:         format = format.trim();
 188:         if (format.length() == 0)
 189:           throw new IOException("Format MUST NOT be an empty string");
 190: 
 191:         if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
 192:           throw new IOException("Unknown or unsupported format: " + format);
 193:       }
 194: 
 195:     return engineGetEncoded();
 196:   }
 197: 
 198:   protected String engineToString()
 199:   {
 200:     CPStringBuilder sb = new CPStringBuilder("p=");
 201:     if (p == null)
 202:       sb.append("???");
 203:     else
 204:       sb.append("0x").append(p.toString(16));
 205: 
 206:     sb.append(", g=");
 207:     if (g == null)
 208:       sb.append("???");
 209:     else
 210:       sb.append("0x").append(g.toString(16));
 211: 
 212:     sb.append(", q=");
 213:     if (q == null)
 214:       sb.append("???");
 215:     else
 216:       sb.append("0x").append(q.toString(16));
 217: 
 218:     sb.append(", l=").append(l);
 219: 
 220:     return sb.toString();
 221:   }
 222: }