SRP6TLSClient.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* SRP6TLSClient.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.util.Util;
  33. import gnu.javax.crypto.key.KeyAgreementException;
  34. import gnu.javax.crypto.key.IncomingMessage;
  35. import gnu.javax.crypto.key.OutgoingMessage;
  36. import gnu.javax.crypto.sasl.srp.SRP;
  37. import java.math.BigInteger;
  38. import java.security.KeyPair;
  39. import java.security.SecureRandom;
  40. import java.util.HashMap;
  41. import java.util.Map;
  42. /**
  43. * A variation of the SRP6 key agreement protocol, for the client-side as
  44. * proposed in <a
  45. * href="http://www.ietf.org/internet-drafts/draft-ietf-tls-srp-05.txt">Using
  46. * SRP for TLS Authentication</a>. The only difference between it and the SASL
  47. * variant is that the shared secret is the entity <code>S</code> and not
  48. * <code>H(S)</code>.
  49. */
  50. public class SRP6TLSClient
  51. extends SRP6KeyAgreement
  52. {
  53. /** The user's identity. */
  54. private String I;
  55. /** The user's cleartext password. */
  56. private byte[] p;
  57. /** The user's ephemeral key pair. */
  58. private KeyPair userKeyPair;
  59. // default 0-arguments constructor
  60. protected void engineInit(final Map attributes) throws KeyAgreementException
  61. {
  62. rnd = (SecureRandom) attributes.get(SOURCE_OF_RANDOMNESS);
  63. final String md = (String) attributes.get(HASH_FUNCTION);
  64. if (md == null || md.trim().length() == 0)
  65. throw new KeyAgreementException("missing hash function");
  66. srp = SRP.instance(md);
  67. I = (String) attributes.get(USER_IDENTITY);
  68. if (I == null)
  69. throw new KeyAgreementException("missing user identity");
  70. p = (byte[]) attributes.get(USER_PASSWORD);
  71. if (p == null)
  72. throw new KeyAgreementException("missing user password");
  73. }
  74. protected OutgoingMessage engineProcessMessage(final IncomingMessage in)
  75. throws KeyAgreementException
  76. {
  77. switch (step)
  78. {
  79. case 0:
  80. return sendIdentity(in);
  81. case 1:
  82. return computeSharedSecret(in);
  83. default:
  84. throw new IllegalStateException("unexpected state");
  85. }
  86. }
  87. protected void engineReset()
  88. {
  89. I = null;
  90. p = null;
  91. userKeyPair = null;
  92. super.engineReset();
  93. }
  94. private OutgoingMessage sendIdentity(final IncomingMessage in)
  95. throws KeyAgreementException
  96. {
  97. final OutgoingMessage result = new OutgoingMessage();
  98. result.writeString(I);
  99. return result;
  100. }
  101. protected OutgoingMessage computeSharedSecret(final IncomingMessage in)
  102. throws KeyAgreementException
  103. {
  104. N = in.readMPI();
  105. g = in.readMPI();
  106. final BigInteger s = in.readMPI();
  107. final BigInteger B = in.readMPI();
  108. // generate an ephemeral keypair
  109. final SRPKeyPairGenerator kpg = new SRPKeyPairGenerator();
  110. final Map attributes = new HashMap();
  111. if (rnd != null)
  112. attributes.put(SRPKeyPairGenerator.SOURCE_OF_RANDOMNESS, rnd);
  113. attributes.put(SRPKeyPairGenerator.SHARED_MODULUS, N);
  114. attributes.put(SRPKeyPairGenerator.GENERATOR, g);
  115. kpg.setup(attributes);
  116. userKeyPair = kpg.generate();
  117. final BigInteger A = ((SRPPublicKey) userKeyPair.getPublic()).getY();
  118. final BigInteger u = uValue(A, B); // u = H(A | B)
  119. final BigInteger x;
  120. try
  121. {
  122. x = new BigInteger(1, srp.computeX(Util.trim(s), I, p));
  123. }
  124. catch (Exception e)
  125. {
  126. throw new KeyAgreementException("computeSharedSecret()", e);
  127. }
  128. // compute S = (B - 3g^x) ^ (a + ux)
  129. final BigInteger a = ((SRPPrivateKey) userKeyPair.getPrivate()).getX();
  130. final BigInteger S = B.subtract(THREE.multiply(g.modPow(x, N)))
  131. .modPow(a.add(u.multiply(x)), N);
  132. K = S;
  133. final OutgoingMessage result = new OutgoingMessage();
  134. result.writeMPI(A);
  135. complete = true;
  136. return result;
  137. }
  138. }