CertificateRequest.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* CertificateRequest.java -- SSL CertificateRequest message.
  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.nio.ByteBuffer;
  35. import java.nio.ByteOrder;
  36. /**
  37. * A request by the server for a client certificate.
  38. *
  39. * <pre>
  40. struct
  41. {
  42. ClientCertificateType certificate_types&lt;1..2^8-1&gt;;
  43. DistinguishedName certificate_authorities&lt;3..2^16-1&gt;;
  44. } CertificateRequest;
  45. </pre>
  46. */
  47. public class CertificateRequest implements Handshake.Body
  48. {
  49. // Fields.
  50. // -------------------------------------------------------------------------
  51. protected ByteBuffer buffer;
  52. // Constructor.
  53. // -------------------------------------------------------------------------
  54. public CertificateRequest(final ByteBuffer buffer)
  55. {
  56. this.buffer = buffer.duplicate().order(ByteOrder.BIG_ENDIAN);
  57. }
  58. // Instance methods.
  59. // -------------------------------------------------------------------------
  60. public int length ()
  61. {
  62. int o1 = (buffer.get (0) & 0xFF) + 1;
  63. return o1 + (buffer.getShort (o1) & 0xFFFF) + 2;
  64. }
  65. public ClientCertificateTypeList types ()
  66. {
  67. return new ClientCertificateTypeList(buffer.duplicate());
  68. }
  69. public X500PrincipalList authorities ()
  70. {
  71. int offset = (buffer.get (0) & 0xFF) + 1;
  72. return new X500PrincipalList (((ByteBuffer) buffer.position(offset)).slice());
  73. }
  74. public String toString()
  75. {
  76. return toString (null);
  77. }
  78. public String toString (final String prefix)
  79. {
  80. StringWriter str = new StringWriter();
  81. PrintWriter out = new PrintWriter(str);
  82. String subprefix = " ";
  83. if (prefix != null) subprefix = prefix + " ";
  84. if (prefix != null) out.print (prefix);
  85. out.println("struct {");
  86. if (prefix != null) out.print (prefix);
  87. out.println (" types =");
  88. out.println (types ().toString (subprefix));
  89. if (prefix != null) out.print (prefix);
  90. out.println(" authorities =");
  91. out.println (authorities ().toString (subprefix));
  92. if (prefix != null) out.print (prefix);
  93. out.print ("} CertificateRequest;");
  94. return str.toString();
  95. }
  96. public static enum ClientCertificateType
  97. {
  98. RSA_SIGN (1),
  99. DSS_SIGN (2),
  100. RSA_FIXED_DH (3),
  101. DSS_FIXED_DH (4);
  102. private final int value;
  103. // Constructor.
  104. // -----------------------------------------------------------------------
  105. private ClientCertificateType (final int value)
  106. {
  107. this.value = value;
  108. }
  109. // Class method.
  110. // -----------------------------------------------------------------------
  111. static ClientCertificateType forValue (final int value)
  112. {
  113. switch (value)
  114. {
  115. case 1: return RSA_SIGN;
  116. case 2: return DSS_SIGN;
  117. case 3: return RSA_FIXED_DH;
  118. case 4: return DSS_FIXED_DH;
  119. default: throw new IllegalArgumentException("unknown client certificate type: " + value);
  120. }
  121. }
  122. public int getValue()
  123. {
  124. return value;
  125. }
  126. }
  127. }