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

   1: /* DSSKeyPairGeneratorSpi.java --
   2:    Copyright 2001, 2002, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a 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 of the License, or (at
   9: your option) 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; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: 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.key.dss.DSSKeyPairGenerator;
  43: 
  44: import java.security.InvalidAlgorithmParameterException;
  45: import java.security.InvalidParameterException;
  46: import java.security.SecureRandom;
  47: import java.security.interfaces.DSAKeyPairGenerator;
  48: import java.security.interfaces.DSAParams;
  49: import java.security.spec.AlgorithmParameterSpec;
  50: import java.security.spec.DSAParameterSpec;
  51: import java.util.HashMap;
  52: 
  53: /**
  54:  * The implementation of a {@link java.security.KeyPairGenerator} adapter class
  55:  * to wrap GNU DSS keypair generator instances.
  56:  * <p>
  57:  * In case the client does not explicitly initialize the KeyPairGenerator (via a
  58:  * call to an <code>initialize()</code> method), the GNU provider uses a
  59:  * default <i>modulus</i> size (keysize) of 1024 bits.
  60:  */
  61: public class DSSKeyPairGeneratorSpi
  62:     extends KeyPairGeneratorAdapter
  63:     implements DSAKeyPairGenerator
  64: {
  65:   public DSSKeyPairGeneratorSpi()
  66:   {
  67:     super(Registry.DSS_KPG);
  68:   }
  69: 
  70:   public void initialize(int keysize, SecureRandom random)
  71:   {
  72:     this.initialize(keysize, false, random);
  73:   }
  74: 
  75:   public void initialize(AlgorithmParameterSpec params, SecureRandom random)
  76:       throws InvalidAlgorithmParameterException
  77:   {
  78:     HashMap attributes = new HashMap();
  79:     if (params != null)
  80:       {
  81:         if (! (params instanceof DSAParameterSpec))
  82:           throw new InvalidAlgorithmParameterException(
  83:               "Parameters argument is not a non-null instance, or "
  84:               + "sub-instance, of java.security.spec.DSAParameterSpec");
  85:         attributes.put(DSSKeyPairGenerator.DSS_PARAMETERS, params);
  86:       }
  87:     if (random != null)
  88:       attributes.put(DSSKeyPairGenerator.SOURCE_OF_RANDOMNESS, random);
  89: 
  90:     attributes.put(DSSKeyPairGenerator.PREFERRED_ENCODING_FORMAT,
  91:                    Integer.valueOf(Registry.ASN1_ENCODING_ID));
  92:     try
  93:       {
  94:         adaptee.setup(attributes);
  95:       }
  96:     catch (IllegalArgumentException x)
  97:       {
  98:         throw new InvalidAlgorithmParameterException(x.getMessage(), x);
  99:       }
 100:   }
 101: 
 102:   public void initialize(DSAParams params, SecureRandom random)
 103:       throws InvalidParameterException
 104:   {
 105:     if (params == null || !(params instanceof DSAParameterSpec))
 106:       throw new InvalidParameterException(
 107:           "Parameters argument is either null or is not an instance, or "
 108:           + "sub-instance, of java.security.spec.DSAParameterSpec");
 109:     DSAParameterSpec spec = (DSAParameterSpec) params;
 110:     try
 111:       {
 112:         this.initialize((AlgorithmParameterSpec) spec, random);
 113:       }
 114:     catch (InvalidAlgorithmParameterException x)
 115:       {
 116:         InvalidParameterException y = new InvalidParameterException(x.getMessage());
 117:         y.initCause(x);
 118:         throw y;
 119:       }
 120:   }
 121: 
 122:   public void initialize(int modlen, boolean genParams, SecureRandom random)
 123:       throws InvalidParameterException
 124:   {
 125:     HashMap attributes = new HashMap();
 126:     attributes.put(DSSKeyPairGenerator.MODULUS_LENGTH, Integer.valueOf(modlen));
 127:     if (random != null)
 128:       attributes.put(DSSKeyPairGenerator.SOURCE_OF_RANDOMNESS, random);
 129: 
 130:     attributes.put(DSSKeyPairGenerator.USE_DEFAULTS,
 131:                    Boolean.valueOf(! genParams));
 132:     attributes.put(DSSKeyPairGenerator.STRICT_DEFAULTS, Boolean.TRUE);
 133:     attributes.put(DSSKeyPairGenerator.PREFERRED_ENCODING_FORMAT,
 134:                    Integer.valueOf(Registry.ASN1_ENCODING_ID));
 135:     try
 136:       {
 137:         adaptee.setup(attributes);
 138:       }
 139:     catch (IllegalArgumentException x)
 140:       {
 141:         InvalidParameterException y = new InvalidParameterException(x.getMessage());
 142:         y.initCause(x);
 143:         throw y;
 144:       }
 145:   }
 146: }