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

   1: /* SRPPrivateKey.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.PrivateKey;
  46: 
  47: /**
  48:  * A representation of an SRP ephemeral private 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 SRPPrivateKey
  57:     extends SRPKey
  58:     implements PrivateKey
  59: {
  60:   /**
  61:    * The private exponent for either the server or the client engaged in the SRP
  62:    * protocol exchange.
  63:    */
  64:   private final BigInteger X;
  65:   /**
  66:    * The user's verifier (v) --for the server-- also computed at the client side
  67:    * as g.modPow(x, N), where x is the hashed output of the user name and
  68:    * password .
  69:    */
  70:   private final BigInteger v;
  71: 
  72:   /**
  73:    * Public constructor for use from outside this package.
  74:    *
  75:    * @param N the public shared modulus.
  76:    * @param g the generator.
  77:    * @param x the private exponent of the ephemeral key.
  78:    */
  79:   public SRPPrivateKey(BigInteger N, BigInteger g, BigInteger x)
  80:   {
  81:     this(N, g, x, null);
  82:   }
  83: 
  84:   /**
  85:    * Public constructor for use from outside this package.
  86:    *
  87:    * @param N the public shared modulus.
  88:    * @param g the generator.
  89:    * @param x the private exponent of the ephemeral key.
  90:    * @param v the user's verifier value (for the server side only).
  91:    */
  92:   public SRPPrivateKey(BigInteger N, BigInteger g, BigInteger x, BigInteger v)
  93:   {
  94:     super(N, g);
  95: 
  96:     SRPAlgorithm.checkParams(N, g);
  97:     this.X = x;
  98:     this.v = v;
  99:   }
 100: 
 101:   /**
 102:    * Default constructor. Assumes N and g are already validated.
 103:    *
 104:    * @param params an array of either 3 or 4 values representing N, g, and
 105:    *          either v and X for the server, or just X for the client. Those
 106:    *          values represent the following:
 107:    *          <ol>
 108:    *          <li>v (server side): the user's verifier.</li>
 109:    *          <li>X (both sides): the server's or client's ephemeral private
 110:    *          exponent.</li>
 111:    *          </ol>
 112:    */
 113:   SRPPrivateKey(BigInteger[] params)
 114:   {
 115:     super(params[0], params[1]);
 116: 
 117:     if (params.length == 3)
 118:       {
 119:         X = params[2];
 120:         v = null;
 121:       }
 122:     else if (params.length == 4)
 123:       {
 124:         X = params[2];
 125:         v = params[3];
 126:       }
 127:     else
 128:       throw new IllegalArgumentException("invalid number of SRP parameters");
 129:   }
 130: 
 131:   /**
 132:    * A class method that takes the output of the <code>encodePrivateKey()</code>
 133:    * method of an SRP keypair codec object (an instance implementing
 134:    * {@link IKeyPairCodec} for DSS keys, and re-constructs an instance of this
 135:    * object.
 136:    *
 137:    * @param k the contents of a previously encoded instance of this object.
 138:    * @throws ArrayIndexOutOfBoundsException if there is not enough bytes, in
 139:    *           <code>k</code>, to represent a valid encoding of an instance
 140:    *           of this object.
 141:    * @throws IllegalArgumentException if the byte sequence does not represent a
 142:    *           valid encoding of an instance of this object.
 143:    */
 144:   public static SRPPrivateKey valueOf(byte[] k)
 145:   {
 146:     // check magic...
 147:     // we should parse here enough bytes to know which codec to use, and
 148:     // direct the byte array to the appropriate codec. since we only have one
 149:     // codec, we could have immediately tried it; nevertheless since testing
 150:     // one byte is cheaper than instatiating a codec that will fail we test
 151:     // the first byte before we carry on.
 152:     if (k[0] == Registry.MAGIC_RAW_SRP_PRIVATE_KEY[0])
 153:       {
 154:         // it's likely to be in raw format. get a raw codec and hand it over
 155:         IKeyPairCodec codec = new SRPKeyPairRawCodec();
 156:         return (SRPPrivateKey) codec.decodePrivateKey(k);
 157:       }
 158:     throw new IllegalArgumentException("magic");
 159:   }
 160: 
 161:   /**
 162:    * Returns the private exponent of the key as a {@link BigInteger}.
 163:    *
 164:    * @return the private exponent of the key as a {@link BigInteger}.
 165:    */
 166:   public BigInteger getX()
 167:   {
 168:     return X;
 169:   }
 170: 
 171:   /**
 172:    * Returns the user's verifier as a {@link BigInteger}.
 173:    *
 174:    * @return the user's verifier as a {@link BigInteger} if this is an SRP
 175:    *         private key of a Host, or <code>null</code> if this is a private
 176:    *         SRP key for a User.
 177:    */
 178:   public BigInteger getV()
 179:   {
 180:     return v;
 181:   }
 182: 
 183:   /**
 184:    * Returns the encoded form of this private key according to the designated
 185:    * format.
 186:    *
 187:    * @param format the desired format identifier of the resulting encoding.
 188:    * @return the byte sequence encoding this key according to the designated
 189:    *         format.
 190:    * @throws IllegalArgumentException if the format is not supported.
 191:    */
 192:   public byte[] getEncoded(int format)
 193:   {
 194:     byte[] result;
 195:     switch (format)
 196:       {
 197:       case IKeyPairCodec.RAW_FORMAT:
 198:         result = new SRPKeyPairRawCodec().encodePrivateKey(this);
 199:         break;
 200:       default:
 201:         throw new IllegalArgumentException("format");
 202:       }
 203:     return result;
 204:   }
 205: 
 206:   /**
 207:    * Returns <code>true</code> if the designated object is an instance of
 208:    * <code>SRPPrivateKey</code> and has the same SRP parameter values as this
 209:    * one.
 210:    *
 211:    * @param obj the other non-null SRP key to compare to.
 212:    * @return <code>true</code> if the designated object is of the same type
 213:    *         and value as this one.
 214:    */
 215:   public boolean equals(Object obj)
 216:   {
 217:     if (obj == null)
 218:       return false;
 219:     if (! (obj instanceof SRPPrivateKey))
 220:       return false;
 221:     SRPPrivateKey that = (SRPPrivateKey) obj;
 222:     boolean result = super.equals(that) && X.equals(that.getX());
 223:     if (v != null)
 224:       result = result && v.equals(that.getV());
 225:     return result;
 226:   }
 227: }