Source for gnu.java.security.provider.DSAParameters

   1: /* DSAParameters.java --- DSA Parameters Implementation
   2:    Copyright (C) 1999, 2003, 2004  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.provider;
  40: 
  41: import gnu.java.io.ASN1ParsingException;
  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: 
  47: import java.io.ByteArrayOutputStream;
  48: import java.io.IOException;
  49: import java.math.BigInteger;
  50: import java.security.AlgorithmParametersSpi;
  51: import java.security.spec.AlgorithmParameterSpec;
  52: import java.security.spec.DSAParameterSpec;
  53: import java.security.spec.InvalidParameterSpecException;
  54: import java.util.ArrayList;
  55: 
  56: /*
  57:     ASN.1 Encoding for DSA from rfc2459 
  58: 
  59:         id-dsa ID ::= { iso(1) member-body(2) us(840) x9-57(10040)
  60:                   x9cm(4) 1 }
  61: 
  62:         Dss-Parms  ::=  SEQUENCE  {
  63:             p             INTEGER,
  64:             q             INTEGER,
  65:             g             INTEGER  }
  66: 
  67: */
  68: public class DSAParameters extends AlgorithmParametersSpi
  69: {
  70: private BigInteger q; // the small prime
  71: private BigInteger p; // the big prime
  72: private BigInteger g;
  73: 
  74: 
  75: public void engineInit(AlgorithmParameterSpec paramSpec)
  76:                             throws InvalidParameterSpecException
  77: {
  78:     if( paramSpec instanceof DSAParameterSpec ) {
  79:         DSAParameterSpec dsaParamSpec = (DSAParameterSpec)paramSpec;
  80:         p = dsaParamSpec.getP();
  81:         q = dsaParamSpec.getQ();
  82:         g = dsaParamSpec.getG();
  83:     }
  84:     else
  85:         throw new InvalidParameterSpecException("Only accepts DSAParameterSpec");
  86: }
  87: 
  88: public void engineInit(byte[] params)
  89:                             throws IOException
  90: {
  91:     DERReader in = new DERReader(params);
  92:     DERValue val = in.read();
  93:     if (val.getValue() != DER.CONSTRUCTED_VALUE)
  94:         throw new ASN1ParsingException("badly formed parameters");
  95:     try
  96:         {
  97:             p = (BigInteger) in.read().getValue();
  98:             q = (BigInteger) in.read().getValue();
  99:             g = (BigInteger) in.read().getValue();
 100:         }
 101:     catch (Exception x)
 102:         {
 103:             throw new ASN1ParsingException("badly formed parameters");
 104:         }
 105: }
 106: 
 107: public void engineInit(byte[] params, String format)
 108:                             throws IOException
 109: {
 110:     if( !format.equals("ASN.1") )
 111:         throw new IOException("Invalid Format: Only accepts ASN.1");
 112:     engineInit( params );    
 113: }
 114: 
 115: public AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
 116:                           throws InvalidParameterSpecException
 117: {
 118:     if( paramSpec.isAssignableFrom(DSAParameterSpec.class) )
 119:         return new DSAParameterSpec(p, q, g);
 120:     else
 121:         throw new InvalidParameterSpecException("Only accepts DSAParameterSpec");
 122: }
 123: 
 124: public byte[] engineGetEncoded()
 125:                                     throws IOException
 126: {
 127:     ByteArrayOutputStream bout = new ByteArrayOutputStream();
 128:     ArrayList seq = new ArrayList(3);
 129:     seq.add(new DERValue(DER.INTEGER, p));
 130:     seq.add(new DERValue(DER.INTEGER, q));
 131:     seq.add(new DERValue(DER.INTEGER, g));
 132:     DERWriter.write(bout, new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, seq));
 133:     return bout.toByteArray();
 134: }
 135: 
 136: 
 137: public byte[] engineGetEncoded(String format)
 138:                                     throws IOException
 139: {
 140:     if( !format.equals("ASN.1") )
 141:         throw new IOException("Invalid Format: Only accepts ASN.1");
 142:     return engineGetEncoded();
 143: }
 144: 
 145: public String engineToString()
 146: {
 147:     return ("q: " + q + " p: " + p + " g: " + g);
 148: }
 149: 
 150: }