Source for gnu.javax.crypto.jce.sig.DHParameters

   1: /* DHParameters.java -- DH parameters DAO
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.javax.crypto.jce.sig;
  40: 
  41: import gnu.java.security.Registry;
  42: import gnu.java.security.der.DER;
  43: import gnu.java.security.der.DERReader;
  44: import gnu.java.security.der.DERValue;
  45: import gnu.java.security.der.DERWriter;
  46: import gnu.java.security.util.DerUtil;
  47: 
  48: import java.io.ByteArrayOutputStream;
  49: import java.io.IOException;
  50: import java.math.BigInteger;
  51: import java.security.AlgorithmParametersSpi;
  52: import java.security.spec.AlgorithmParameterSpec;
  53: import java.security.spec.InvalidParameterSpecException;
  54: import java.util.ArrayList;
  55: 
  56: import javax.crypto.spec.DHGenParameterSpec;
  57: import javax.crypto.spec.DHParameterSpec;
  58: 
  59: /**
  60:  * A JCE-specific Data Access Object (DAO) for DH parameters.
  61:  */
  62: public class DHParameters
  63:     extends AlgorithmParametersSpi
  64: {
  65:   /** The prime public modulus. */
  66:   private BigInteger p;
  67: 
  68:   /** The generator. */
  69:   private BigInteger g;
  70: 
  71:   /** A prime factor of p-1. */
  72:   private BigInteger q;
  73: 
  74:   /** The (private) random exponent's size (in bits). */
  75:   private int l;
  76: 
  77:   // default 0-arguments constructor
  78: 
  79:   protected void engineInit(AlgorithmParameterSpec spec)
  80:       throws InvalidParameterSpecException
  81:   {
  82:     if (! (spec instanceof DHParameterSpec))
  83:       throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
  84:                                               + spec.getClass().getName());
  85:     DHParameterSpec dhSpec = (DHParameterSpec) spec;
  86:     p = dhSpec.getP();
  87:     g = dhSpec.getG();
  88:     l = dhSpec.getL();
  89:   }
  90: 
  91:   /**
  92:    * Decodes the set of DH parameters as per RFC-2459; i.e. the DER-encoded
  93:    * form of the following ASN.1 construct: 
  94:    * 
  95:    * <pre>
  96:    *   DhParams ::= SEQUENCE {
  97:    *     p  INTEGER, -- odd prime, p=jq +1
  98:    *     g  INTEGER, -- generator, g
  99:    *     q  INTEGER  -- factor of p-1
 100:    *   }
 101:    * </pre>
 102:    */
 103:   protected void engineInit(byte[] params) throws IOException
 104:   {
 105:     DERReader der = new DERReader(params);
 106: 
 107:     DERValue derParams = der.read();
 108:     DerUtil.checkIsConstructed(derParams, "Wrong DH Parameters field");
 109: 
 110:     DERValue val = der.read();
 111:     DerUtil.checkIsBigInteger(val, "Wrong P field");
 112:     p = (BigInteger) val.getValue();
 113:     val = der.read();
 114:     DerUtil.checkIsBigInteger(val, "Wrong G field");
 115:     g = (BigInteger) val.getValue();
 116:     val = der.read();
 117:     DerUtil.checkIsBigInteger(val, "Wrong Q field");
 118:     q = (BigInteger) val.getValue();
 119:     l = q.bitLength();
 120:   }
 121: 
 122:   protected void engineInit(byte[] params, String format) throws IOException
 123:   {
 124:     if (format != null)
 125:       {
 126:         format = format.trim();
 127:         if (format.length() == 0)
 128:           throw new IOException("Format MUST NOT be an empty string");
 129: 
 130:         if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
 131:           throw new IOException("Unknown or unsupported format: " + format);
 132:       }
 133: 
 134:     engineInit(params);
 135:   }
 136: 
 137:   protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
 138:       throws InvalidParameterSpecException
 139:   {
 140:     if (paramSpec.isAssignableFrom(DHParameterSpec.class))
 141:       return new DHParameterSpec(p, g, l);
 142: 
 143:     if (paramSpec.isAssignableFrom(DHGenParameterSpec.class))
 144:       return new DHGenParameterSpec(p.bitLength(), l);
 145: 
 146:     throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
 147:                                             + paramSpec.getName());
 148:   }
 149: 
 150:   /**
 151:    * Encodes the set of DH parameters as per RFC-2459; i.e. as the DER-encoded
 152:    * form of the following ASN.1 construct: 
 153:    * 
 154:    * <pre>
 155:    *   DhParams ::= SEQUENCE {
 156:    *     p  INTEGER, -- odd prime, p=jq +1
 157:    *     g  INTEGER, -- generator, g
 158:    *     q  INTEGER  -- factor of p-1
 159:    *   }
 160:    * </pre>
 161:    */
 162:   protected byte[] engineGetEncoded() throws IOException
 163:   {
 164:     DERValue derP = new DERValue(DER.INTEGER, p);
 165:     DERValue derG = new DERValue(DER.INTEGER, g);
 166:     DERValue derQ = new DERValue(DER.INTEGER, q);
 167: 
 168:     ArrayList params = new ArrayList(3);
 169:     params.add(derP);
 170:     params.add(derG);
 171:     params.add(derQ);
 172:     DERValue derParams = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, params);
 173: 
 174:     ByteArrayOutputStream baos = new ByteArrayOutputStream();
 175:     DERWriter.write(baos, derParams);
 176:     byte[] result = baos.toByteArray();
 177: 
 178:     return result;
 179:   }
 180: 
 181:   protected byte[] engineGetEncoded(String format) throws IOException
 182:   {
 183:     if (format != null)
 184:       {
 185:         format = format.trim();
 186:         if (format.length() == 0)
 187:           throw new IOException("Format MUST NOT be an empty string");
 188: 
 189:         if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
 190:           throw new IOException("Unknown or unsupported format: " + format);
 191:       }
 192: 
 193:     return engineGetEncoded();
 194:   }
 195: 
 196:   protected String engineToString()
 197:   {
 198:     StringBuffer sb = new StringBuffer("p=");
 199:     if (p == null)
 200:       sb.append("???");
 201:     else
 202:       sb.append("0x").append(p.toString(16));
 203: 
 204:     sb.append(", g=");
 205:     if (g == null)
 206:       sb.append("???");
 207:     else
 208:       sb.append("0x").append(g.toString(16));
 209: 
 210:     sb.append(", q=");
 211:     if (q == null)
 212:       sb.append("???");
 213:     else
 214:       sb.append("0x").append(q.toString(16));
 215: 
 216:     sb.append(", l=").append(l);
 217: 
 218:     return sb.toString();
 219:   }
 220: }