SRPPrivateKey.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* SRPPrivateKey.java --
  2. Copyright (C) 2003, 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.key.srp6;
  32. import gnu.java.security.Registry;
  33. import gnu.java.security.key.IKeyPairCodec;
  34. import java.math.BigInteger;
  35. import java.security.PrivateKey;
  36. /**
  37. * A representation of an SRP ephemeral private key.
  38. * <p>
  39. * Reference:
  40. * <ol>
  41. * <li><a href="http://srp.stanford.edu/design.html">SRP Protocol Design</a><br>
  42. * Thomas J. Wu.</li>
  43. * </ol>
  44. */
  45. public class SRPPrivateKey
  46. extends SRPKey
  47. implements PrivateKey
  48. {
  49. /**
  50. * The private exponent for either the server or the client engaged in the SRP
  51. * protocol exchange.
  52. */
  53. private final BigInteger X;
  54. /**
  55. * The user's verifier (v) --for the server-- also computed at the client side
  56. * as g.modPow(x, N), where x is the hashed output of the user name and
  57. * password .
  58. */
  59. private final BigInteger v;
  60. /**
  61. * Public constructor for use from outside this package.
  62. *
  63. * @param N the public shared modulus.
  64. * @param g the generator.
  65. * @param x the private exponent of the ephemeral key.
  66. */
  67. public SRPPrivateKey(BigInteger N, BigInteger g, BigInteger x)
  68. {
  69. this(N, g, x, null);
  70. }
  71. /**
  72. * Public constructor for use from outside this package.
  73. *
  74. * @param N the public shared modulus.
  75. * @param g the generator.
  76. * @param x the private exponent of the ephemeral key.
  77. * @param v the user's verifier value (for the server side only).
  78. */
  79. public SRPPrivateKey(BigInteger N, BigInteger g, BigInteger x, BigInteger v)
  80. {
  81. super(N, g);
  82. SRPAlgorithm.checkParams(N, g);
  83. this.X = x;
  84. this.v = v;
  85. }
  86. /**
  87. * Default constructor. Assumes N and g are already validated.
  88. *
  89. * @param params an array of either 3 or 4 values representing N, g, and
  90. * either v and X for the server, or just X for the client. Those
  91. * values represent the following:
  92. * <ol>
  93. * <li>v (server side): the user's verifier.</li>
  94. * <li>X (both sides): the server's or client's ephemeral private
  95. * exponent.</li>
  96. * </ol>
  97. */
  98. SRPPrivateKey(BigInteger[] params)
  99. {
  100. super(params[0], params[1]);
  101. if (params.length == 3)
  102. {
  103. X = params[2];
  104. v = null;
  105. }
  106. else if (params.length == 4)
  107. {
  108. X = params[2];
  109. v = params[3];
  110. }
  111. else
  112. throw new IllegalArgumentException("invalid number of SRP parameters");
  113. }
  114. /**
  115. * A class method that takes the output of the <code>encodePrivateKey()</code>
  116. * method of an SRP keypair codec object (an instance implementing
  117. * {@link IKeyPairCodec} for DSS keys, and re-constructs an instance of this
  118. * object.
  119. *
  120. * @param k the contents of a previously encoded instance of this object.
  121. * @throws ArrayIndexOutOfBoundsException if there is not enough bytes, in
  122. * <code>k</code>, to represent a valid encoding of an instance
  123. * of this object.
  124. * @throws IllegalArgumentException if the byte sequence does not represent a
  125. * valid encoding of an instance of this object.
  126. */
  127. public static SRPPrivateKey valueOf(byte[] k)
  128. {
  129. // check magic...
  130. // we should parse here enough bytes to know which codec to use, and
  131. // direct the byte array to the appropriate codec. since we only have one
  132. // codec, we could have immediately tried it; nevertheless since testing
  133. // one byte is cheaper than instatiating a codec that will fail we test
  134. // the first byte before we carry on.
  135. if (k[0] == Registry.MAGIC_RAW_SRP_PRIVATE_KEY[0])
  136. {
  137. // it's likely to be in raw format. get a raw codec and hand it over
  138. IKeyPairCodec codec = new SRPKeyPairRawCodec();
  139. return (SRPPrivateKey) codec.decodePrivateKey(k);
  140. }
  141. throw new IllegalArgumentException("magic");
  142. }
  143. /**
  144. * Returns the private exponent of the key as a {@link BigInteger}.
  145. *
  146. * @return the private exponent of the key as a {@link BigInteger}.
  147. */
  148. public BigInteger getX()
  149. {
  150. return X;
  151. }
  152. /**
  153. * Returns the user's verifier as a {@link BigInteger}.
  154. *
  155. * @return the user's verifier as a {@link BigInteger} if this is an SRP
  156. * private key of a Host, or <code>null</code> if this is a private
  157. * SRP key for a User.
  158. */
  159. public BigInteger getV()
  160. {
  161. return v;
  162. }
  163. /**
  164. * Returns the encoded form of this private key according to the designated
  165. * format.
  166. *
  167. * @param format the desired format identifier of the resulting encoding.
  168. * @return the byte sequence encoding this key according to the designated
  169. * format.
  170. * @throws IllegalArgumentException if the format is not supported.
  171. */
  172. public byte[] getEncoded(int format)
  173. {
  174. byte[] result;
  175. switch (format)
  176. {
  177. case IKeyPairCodec.RAW_FORMAT:
  178. result = new SRPKeyPairRawCodec().encodePrivateKey(this);
  179. break;
  180. default:
  181. throw new IllegalArgumentException("format");
  182. }
  183. return result;
  184. }
  185. /**
  186. * Returns <code>true</code> if the designated object is an instance of
  187. * <code>SRPPrivateKey</code> and has the same SRP parameter values as this
  188. * one.
  189. *
  190. * @param obj the other non-null SRP key to compare to.
  191. * @return <code>true</code> if the designated object is of the same type
  192. * and value as this one.
  193. */
  194. public boolean equals(Object obj)
  195. {
  196. if (obj == null)
  197. return false;
  198. if (! (obj instanceof SRPPrivateKey))
  199. return false;
  200. SRPPrivateKey that = (SRPPrivateKey) obj;
  201. boolean result = super.equals(that) && X.equals(that.getX());
  202. if (v != null)
  203. result = result && v.equals(that.getV());
  204. return result;
  205. }
  206. }