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

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