Source for gnu.javax.crypto.key.srp6.SRPPublicKey

   1: /* SRPPublicKey.java --
   2:    Copyright (C) 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.javax.crypto.key.srp6;
  40: 
  41: import gnu.java.security.Registry;
  42: import gnu.java.security.key.IKeyPairCodec;
  43: 
  44: import java.math.BigInteger;
  45: import java.security.PublicKey;
  46: 
  47: /**
  48:  * A representation of an SRP ephemeral public key.
  49:  * <p>
  50:  * Reference:
  51:  * <ol>
  52:  * <li><a href="http://srp.stanford.edu/design.html">SRP Protocol Design</a><br>
  53:  * Thomas J. Wu.</li>
  54:  * </ol>
  55:  */
  56: public class SRPPublicKey
  57:     extends SRPKey
  58:     implements PublicKey
  59: {
  60:   /**
  61:    * The public exponent for either the server or the client engaged in the SRP
  62:    * protocol exchange.
  63:    */
  64:   private final BigInteger Y;
  65: 
  66:   /**
  67:    * Public constructor for use from outside this package.
  68:    *
  69:    * @param N the public shared modulus.
  70:    * @param g the generator.
  71:    * @param Y the public exponent of the ephemeral key.
  72:    */
  73:   public SRPPublicKey(BigInteger N, BigInteger g, BigInteger Y)
  74:   {
  75:     super(N, g);
  76: 
  77:     SRPAlgorithm.checkParams(N, g);
  78:     this.Y = Y;
  79:   }
  80: 
  81:   /**
  82:    * Default constructor. Assumes that N and g are already validated.
  83:    *
  84:    * @param params an array of 3 values representing N, g and Y; the latter
  85:    *          being the client's or server's public exponent.
  86:    */
  87:   SRPPublicKey(BigInteger[] params)
  88:   {
  89:     super(params[0], params[1]);
  90: 
  91:     this.Y = params[2];
  92:   }
  93: 
  94:   /**
  95:    * A class method that takes the output of the <code>encodePublicKey()</code>
  96:    * method of an SRP keypair codec object (an instance implementing
  97:    * {@link IKeyPairCodec} for SRP keys, and re-constructs an instance of this
  98:    * object.
  99:    *
 100:    * @param k the contents of a previously encoded instance of this object.
 101:    * @throws ArrayIndexOutOfBoundsException if there is not enough bytes, in
 102:    *           <code>k</code>, to represent a valid encoding of an instance
 103:    *           of this object.
 104:    * @throws IllegalArgumentException if the byte sequence does not represent a
 105:    *           valid encoding of an instance of this object.
 106:    */
 107:   public static SRPPublicKey valueOf(byte[] k)
 108:   {
 109:     // check magic...
 110:     // we should parse here enough bytes to know which codec to use, and
 111:     // direct the byte array to the appropriate codec. since we only have one
 112:     // codec, we could have immediately tried it; nevertheless since testing
 113:     // one byte is cheaper than instatiating a codec that will fail we test
 114:     // the first byte before we carry on.
 115:     if (k[0] == Registry.MAGIC_RAW_SRP_PUBLIC_KEY[0])
 116:       {
 117:         // it's likely to be in raw format. get a raw codec and hand it over
 118:         IKeyPairCodec codec = new SRPKeyPairRawCodec();
 119:         return (SRPPublicKey) codec.decodePublicKey(k);
 120:       }
 121:     throw new IllegalArgumentException("magic");
 122:   }
 123: 
 124:   /**
 125:    * Returns the public exponent of the key as a {@link BigInteger}.
 126:    *
 127:    * @return the public exponent of the key as a {@link BigInteger}.
 128:    */
 129:   public BigInteger getY()
 130:   {
 131:     return Y;
 132:   }
 133: 
 134:   /**
 135:    * Returns the encoded form of this public key according to the designated
 136:    * format.
 137:    *
 138:    * @param format the desired format identifier of the resulting encoding.
 139:    * @return the byte sequence encoding this key according to the designated
 140:    *         format.
 141:    * @throws IllegalArgumentException if the format is not supported.
 142:    */
 143:   public byte[] getEncoded(int format)
 144:   {
 145:     byte[] result;
 146:     switch (format)
 147:       {
 148:       case IKeyPairCodec.RAW_FORMAT:
 149:         result = new SRPKeyPairRawCodec().encodePublicKey(this);
 150:         break;
 151:       default:
 152:         throw new IllegalArgumentException("format");
 153:       }
 154:     return result;
 155:   }
 156: 
 157:   /**
 158:    * Returns <code>true</code> if the designated object is an instance of
 159:    * <code>SRPPublicKey</code>and has the same SRP parameter values as this
 160:    * one.
 161:    *
 162:    * @param obj the other non-null SRP key to compare to.
 163:    * @return <code>true</code> if the designated object is of the same type
 164:    *         and value as this one.
 165:    */
 166:   public boolean equals(Object obj)
 167:   {
 168:     if (obj == null)
 169:       return false;
 170:     if (! (obj instanceof SRPPublicKey))
 171:       return false;
 172:     SRPPublicKey that = (SRPPublicKey) obj;
 173:     return super.equals(that) && Y.equals(that.getY());
 174:   }
 175: }