ClientDiffieHellmanPublic.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* ClientDiffieHellmanPublic.java -- Client Diffie-Hellman value.
  2. Copyright (C) 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.net.ssl.provider;
  32. import java.io.PrintWriter;
  33. import java.io.StringWriter;
  34. import java.math.BigInteger;
  35. import java.nio.ByteBuffer;
  36. /**
  37. * The client's explicit Diffie Hellman value.
  38. *
  39. * <pre>
  40. struct {
  41. select (PublicValueEncoding) {
  42. case implicit: struct { };
  43. case explicit: opaque dh_Yc&lt;1..2^16-1&gt;;
  44. } dh_public;
  45. } ClientDiffieHellmanPublic;</pre>
  46. */
  47. public class ClientDiffieHellmanPublic extends ExchangeKeys implements Builder
  48. {
  49. public ClientDiffieHellmanPublic(final ByteBuffer buffer)
  50. {
  51. super(buffer);
  52. }
  53. public ClientDiffieHellmanPublic(final BigInteger Yc)
  54. {
  55. super(wrap(Yc));
  56. }
  57. private static ByteBuffer wrap(BigInteger Yc)
  58. {
  59. byte[] b = Util.trim(Yc);
  60. ByteBuffer ret = ByteBuffer.allocate(b.length + 2);
  61. ret.putShort((short) b.length);
  62. ret.put(b);
  63. return (ByteBuffer) ret.rewind();
  64. }
  65. public ByteBuffer buffer()
  66. {
  67. return (ByteBuffer) buffer.duplicate().rewind().limit(length());
  68. }
  69. public BigInteger publicValue()
  70. {
  71. int len = length() - 2;
  72. byte[] b = new byte[len];
  73. buffer.position(2);
  74. buffer.get(b);
  75. buffer.rewind();
  76. return new BigInteger(1, b);
  77. }
  78. public void setPublicValue(final BigInteger Yc)
  79. {
  80. byte[] buf = Util.trim(Yc);
  81. if (buffer.capacity() < buf.length + 2)
  82. buffer = ByteBuffer.allocate(buf.length + 2);
  83. buffer.putShort((short) buf.length);
  84. buffer.put(buf);
  85. buffer.rewind();
  86. }
  87. public int length ()
  88. {
  89. return (buffer.getShort(0) & 0xFFFF) + 2;
  90. }
  91. public String toString ()
  92. {
  93. return toString (null);
  94. }
  95. public String toString (final String prefix)
  96. {
  97. StringWriter str = new StringWriter ();
  98. PrintWriter out = new PrintWriter (str);
  99. if (prefix != null) out.print (prefix);
  100. out.println ("struct {");
  101. if (prefix != null) out.print (prefix);
  102. out.print (" dh_Yc = ");
  103. out.print (publicValue ().toString (16));
  104. out.println (';');
  105. if (prefix != null) out.print (prefix);
  106. out.print ("} ClientDiffieHellmanPublic;");
  107. return str.toString ();
  108. }
  109. }