Source for gnu.java.security.jce.sig.DSSParameters

   1: /* DSSParameters.java -- DSS 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.java.security.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.DSAParameterSpec;
  54: import java.security.spec.InvalidParameterSpecException;
  55: import java.util.ArrayList;
  56: 
  57: /**
  58:  * A JCE-specific Data Access Object (DAO) for DSS parameters.
  59:  */
  60: public class DSSParameters
  61:     extends AlgorithmParametersSpi
  62: {
  63:   /**
  64:    * A prime modulus, where <code>2<sup>L-1</sup> &lt; p &lt; 2<sup>L</sup></code>
  65:    * for <code>512 &lt;= L &lt;= 1024</code> and <code>L</code> a multiple of
  66:    * <code>64</code>.
  67:    */
  68:   private BigInteger p;
  69: 
  70:   /**
  71:    * A prime divisor of <code>p - 1</code>, where <code>2<sup>159</sup> &lt; q
  72:    * &lt; 2<sup>160</sup></code>.
  73:    */
  74:   private BigInteger q;
  75: 
  76:   /**
  77:    * <code>g = h<sup>(p-1)</sup>/q mod p</code>, where <code>h</code> is any
  78:    * integer with <code>1 &lt; h &lt; p - 1</code> such that <code>h<sup>
  79:    * (p-1)</sup>/q mod p > 1</code> (<code>g</code> has order <code>q mod p
  80:    * </code>).
  81:    */
  82:   private BigInteger g;
  83: 
  84:   // default 0-arguments constructor
  85: 
  86:   protected void engineInit(AlgorithmParameterSpec spec)
  87:       throws InvalidParameterSpecException
  88:   {
  89:     if (! (spec instanceof DSAParameterSpec))
  90:       throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
  91:                                               + spec.getClass().getName());
  92:     DSAParameterSpec dsaSpec = (DSAParameterSpec) spec;
  93:     p = dsaSpec.getP();
  94:     q = dsaSpec.getQ();
  95:     g = dsaSpec.getG();
  96:   }
  97: 
  98:   /**
  99:    * Decodes the set of DSS parameters as per RFC-2459; i.e. the DER-encoded
 100:    * form of the following ASN.1 construct: 
 101:    * 
 102:    * <pre>
 103:    *   DssParams ::= SEQUENCE {
 104:    *     p   INTEGER,
 105:    *     q   INTEGER,
 106:    *     g   INTEGER
 107:    *   }
 108:    * </pre>
 109:    */
 110:   protected void engineInit(byte[] params) throws IOException
 111:   {
 112:     DERReader der = new DERReader(params);
 113: 
 114:     DERValue derParams = der.read();
 115:     DerUtil.checkIsConstructed(derParams, "Wrong DSS Parameters field");
 116: 
 117:     DERValue val = der.read();
 118:     DerUtil.checkIsBigInteger(val, "Wrong P field");
 119:     p = (BigInteger) val.getValue();
 120:     val = der.read();
 121:     DerUtil.checkIsBigInteger(val, "Wrong Q field");
 122:     q = (BigInteger) val.getValue();
 123:     val = der.read();
 124:     DerUtil.checkIsBigInteger(val, "Wrong G field");
 125:     g = (BigInteger) val.getValue();
 126:   }
 127: 
 128:   protected void engineInit(byte[] params, String format) throws IOException
 129:   {
 130:     if (format != null)
 131:       {
 132:         format = format.trim();
 133:         if (format.length() == 0)
 134:           throw new IOException("Format MUST NOT be an empty string");
 135: 
 136:         if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
 137:           throw new IOException("Unknown or unsupported format: " + format);
 138:       }
 139:     engineInit(params);
 140:   }
 141: 
 142:   protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
 143:       throws InvalidParameterSpecException
 144:   {
 145:     if (! paramSpec.isAssignableFrom(DSAParameterSpec.class))
 146:       throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
 147:                                               + paramSpec.getName());
 148:     return new DSAParameterSpec(p, q, g);
 149:   }
 150: 
 151:   /**
 152:    * Encodes the set of DSS parameters as per RFC-2459; i.e. as the DER-encoded
 153:    * form of the following ASN.1 construct: 
 154:    * 
 155:    * <pre>
 156:    *   DssParams ::= SEQUENCE {
 157:    *     p   INTEGER,
 158:    *     q   INTEGER,
 159:    *     g   INTEGER
 160:    *   }
 161:    * </pre>
 162:    */
 163:   protected byte[] engineGetEncoded() throws IOException
 164:   {
 165:     DERValue derP = new DERValue(DER.INTEGER, p);
 166:     DERValue derQ = new DERValue(DER.INTEGER, q);
 167:     DERValue derG = new DERValue(DER.INTEGER, g);
 168: 
 169:     ArrayList params = new ArrayList(3);
 170:     params.add(derP);
 171:     params.add(derQ);
 172:     params.add(derG);
 173:     DERValue derParams = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, params);
 174: 
 175:     ByteArrayOutputStream baos = new ByteArrayOutputStream();
 176:     DERWriter.write(baos, derParams);
 177:     byte[] result = baos.toByteArray();
 178: 
 179:     return result;
 180:   }
 181: 
 182:   protected byte[] engineGetEncoded(String format) throws IOException
 183:   {
 184:     if (format != null)
 185:       {
 186:         format = format.trim();
 187:         if (format.length() == 0)
 188:           throw new IOException("Format MUST NOT be an empty string");
 189: 
 190:         if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
 191:           throw new IOException("Unknown or unsupported format: " + format);
 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(", q=");
 205:     if (q == null)
 206:       sb.append("???");
 207:     else
 208:       sb.append("0x").append(q.toString(16));
 209: 
 210:     sb.append(", g=");
 211:     if (g == null)
 212:       sb.append("???");
 213:     else
 214:       sb.append("0x").append(g.toString(16));
 215: 
 216:     return sb.toString();
 217:   }
 218: }