Source for gnu.java.security.key.dss.DSSPrivateKey

   1: /* DSSPrivateKey.java -- 
   2:    Copyright 2001, 2002, 2003, 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.key.dss;
  40: 
  41: import gnu.java.security.Configuration;
  42: import gnu.java.security.Registry;
  43: import gnu.java.security.action.GetPropertyAction;
  44: import gnu.java.security.key.IKeyPairCodec;
  45: 
  46: import java.math.BigInteger;
  47: import java.security.AccessController;
  48: import java.security.PrivateKey;
  49: import java.security.interfaces.DSAPrivateKey;
  50: 
  51: /**
  52:  * An object that embodies a DSS (Digital Signature Standard) private key.
  53:  * 
  54:  * @see #getEncoded
  55:  */
  56: public class DSSPrivateKey
  57:     extends DSSKey
  58:     implements PrivateKey, DSAPrivateKey
  59: {
  60:   /**
  61:    * A randomly or pseudorandomly generated integer with <code>0 &lt; x &lt;
  62:    * q</code>.
  63:    */
  64:   private final BigInteger x;
  65: 
  66:   /** String representation of this key. Cached for speed. */
  67:   private transient String str;
  68: 
  69:   /**
  70:    * Convenience constructor. Calls the constructor with 5 arguments passing
  71:    * {@link Registry#RAW_ENCODING_ID} as the identifier of the preferred
  72:    * encoding format.
  73:    * 
  74:    * @param p the public modulus.
  75:    * @param q the public prime divisor of <code>p-1</code>.
  76:    * @param g a generator of the unique cyclic group <code>Z<sup>*</sup>
  77:    *          <sub>p</sub></code>.
  78:    * @param x the private key part.
  79:    */
  80:   public DSSPrivateKey(BigInteger p, BigInteger q, BigInteger g, BigInteger x)
  81:   {
  82:     this(Registry.RAW_ENCODING_ID, p, q, g, x);
  83:   }
  84: 
  85:   /**
  86:    * Constructs a new instance of a <code>DSSPrivateKey</code> given the
  87:    * designated arguments.
  88:    * 
  89:    * @param preferredFormat the indetifier of the preferred encoding format to
  90:    *          use when externalizing this key.
  91:    * @param p the public modulus.
  92:    * @param q the public prime divisor of <code>p-1</code>.
  93:    * @param g a generator of the unique cyclic group <code>Z<sup>*</sup>
  94:    *          <sub>p</sub></code>.
  95:    * @param x the private key part.
  96:    */
  97:   public DSSPrivateKey(int preferredFormat, BigInteger p, BigInteger q,
  98:                        BigInteger g, BigInteger x)
  99:   {
 100:     super(preferredFormat == Registry.ASN1_ENCODING_ID ? Registry.PKCS8_ENCODING_ID
 101:                                                        : preferredFormat,
 102:           p, q, g);
 103:     this.x = x;
 104:   }
 105: 
 106:   /**
 107:    * A class method that takes the output of the <code>encodePrivateKey()</code>
 108:    * method of a DSS keypair codec object (an instance implementing
 109:    * {@link gnu.java.security.key.IKeyPairCodec} for DSS keys, and re-constructs
 110:    * an instance of this object.
 111:    * 
 112:    * @param k the contents of a previously encoded instance of this object.
 113:    * @exception ArrayIndexOutOfBoundsException if there is not enough bytes, in
 114:    *              <code>k</code>, to represent a valid encoding of an
 115:    *              instance of this object.
 116:    * @exception IllegalArgumentException if the byte sequence does not represent
 117:    *              a valid encoding of an instance of this object.
 118:    */
 119:   public static DSSPrivateKey valueOf(byte[] k)
 120:   {
 121:     // try RAW codec
 122:     if (k[0] == Registry.MAGIC_RAW_DSS_PRIVATE_KEY[0])
 123:       try
 124:         {
 125:           return (DSSPrivateKey) new DSSKeyPairRawCodec().decodePrivateKey(k);
 126:         }
 127:       catch (IllegalArgumentException ignored)
 128:         {
 129:         }
 130:     // try PKCS#8 codec
 131:     return (DSSPrivateKey) new DSSKeyPairPKCS8Codec().decodePrivateKey(k);
 132:   }
 133: 
 134:   public BigInteger getX()
 135:   {
 136:     return x;
 137:   }
 138: 
 139:   /**
 140:    * Returns the encoded form of this private key according to the designated
 141:    * format.
 142:    * 
 143:    * @param format the desired format identifier of the resulting encoding.
 144:    * @return the byte sequence encoding this key according to the designated
 145:    *         format.
 146:    * @exception IllegalArgumentException if the format is not supported.
 147:    * @see DSSKeyPairRawCodec
 148:    */
 149:   public byte[] getEncoded(int format)
 150:   {
 151:     byte[] result;
 152:     switch (format)
 153:       {
 154:       case IKeyPairCodec.RAW_FORMAT:
 155:         result = new DSSKeyPairRawCodec().encodePrivateKey(this);
 156:         break;
 157:       case IKeyPairCodec.PKCS8_FORMAT:
 158:         result = new DSSKeyPairPKCS8Codec().encodePrivateKey(this);
 159:         break;
 160:       default:
 161:         throw new IllegalArgumentException("Unsupported encoding format: "
 162:                                            + format);
 163:       }
 164:     return result;
 165:   }
 166: 
 167:   /**
 168:    * Returns <code>true</code> if the designated object is an instance of
 169:    * {@link DSAPrivateKey} and has the same DSS (Digital Signature Standard)
 170:    * parameter values as this one.
 171:    * 
 172:    * @param obj the other non-null DSS key to compare to.
 173:    * @return <code>true</code> if the designated object is of the same type
 174:    *         and value as this one.
 175:    */
 176:   public boolean equals(Object obj)
 177:   {
 178:     if (obj == null)
 179:       return false;
 180: 
 181:     if (! (obj instanceof DSAPrivateKey))
 182:       return false;
 183: 
 184:     DSAPrivateKey that = (DSAPrivateKey) obj;
 185:     return super.equals(that) && x.equals(that.getX());
 186:   }
 187: 
 188:   public String toString()
 189:   {
 190:     if (str == null)
 191:       {
 192:         String ls = (String) AccessController.doPrivileged
 193:             (new GetPropertyAction("line.separator"));
 194:         str = new StringBuilder(this.getClass().getName()).append("(")
 195:             .append(super.toString()).append(",").append(ls)
 196:             .append("x=0x").append(Configuration.DEBUG ? x.toString(16)
 197:                                                        : "**...*").append(ls)
 198:             .append(")")
 199:             .toString();
 200:       }
 201:     return str;
 202:   }
 203: }