Alert.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* Alert.java -- SSL Alert 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. /**
  36. * An alert message in the SSL protocol. Alerts are sent both as warnings
  37. * which may allow execution to continue, or they may be fatal, which will
  38. * halt this session. An alert object is composed of two enums -- the level,
  39. * which indicates the seriousness of the alert, and the description, which
  40. * indicates the reason for the alert.
  41. *
  42. * <pre>
  43. * struct {
  44. * AlertLevel level;
  45. * AlertDescription description;
  46. * }
  47. * </pre>
  48. */
  49. public final class Alert implements Constructed
  50. {
  51. // Fields.
  52. // -------------------------------------------------------------------------
  53. /** The underlying byte buffer. */
  54. private final ByteBuffer buffer;
  55. // Constructor.
  56. // -------------------------------------------------------------------------
  57. public Alert (final ByteBuffer buffer)
  58. {
  59. this.buffer = buffer;
  60. }
  61. public Alert (final Level level, final Description description)
  62. {
  63. level.getClass ();
  64. description.getClass ();
  65. ByteBuffer b = ByteBuffer.allocate (2);
  66. b.put (0, (byte) level.getValue ());
  67. b.put (1, (byte) description.getValue ());
  68. this.buffer = b.asReadOnlyBuffer ();
  69. }
  70. // Instance methods.
  71. // -------------------------------------------------------------------------
  72. public int length ()
  73. {
  74. return 2;
  75. }
  76. byte[] getEncoded()
  77. {
  78. byte[] buf = new byte[2];
  79. buffer.position (0);
  80. buffer.get (buf);
  81. return buf;
  82. }
  83. public Level level()
  84. {
  85. return Level.forInteger (buffer.get (0) & 0xFF);
  86. }
  87. public Description description()
  88. {
  89. return Description.forInteger (buffer.get (1) & 0xFF);
  90. }
  91. public void setLevel (final Level level)
  92. {
  93. buffer.put (0, (byte) level.getValue ());
  94. }
  95. public void setDescription (final Description description)
  96. {
  97. buffer.put (1, (byte) description.getValue ());
  98. }
  99. public boolean equals (Object o)
  100. {
  101. if (!(o instanceof Alert))
  102. return false;
  103. Alert that = (Alert) o;
  104. return that.buffer.position (0).equals (buffer.position (0));
  105. }
  106. public int hashCode ()
  107. {
  108. return buffer.getShort (0) & 0xFFFF;
  109. }
  110. public String toString()
  111. {
  112. return toString (null);
  113. }
  114. public String toString (final String prefix)
  115. {
  116. StringWriter str = new StringWriter ();
  117. PrintWriter out = new PrintWriter (str);
  118. if (prefix != null) out.print (prefix);
  119. out.println ("struct {");
  120. if (prefix != null) out.print (prefix);
  121. out.print (" level: ");
  122. out.print (level ());
  123. out.println (";");
  124. if (prefix != null) out.print (prefix);
  125. out.print (" description: ");
  126. out.print (description ());
  127. out.println (";");
  128. if (prefix != null) out.print (prefix);
  129. out.print ("} Alert;");
  130. return str.toString ();
  131. }
  132. // Enumerations.
  133. // -------------------------------------------------------------------------
  134. /**
  135. * The level enumeration.
  136. *
  137. * <pre>
  138. * enum { warning(1), fatal(2), (255) } AlertLevel;
  139. * </pre>
  140. */
  141. public static enum Level
  142. {
  143. WARNING (1), FATAL (2);
  144. private final int value;
  145. private Level(int value)
  146. {
  147. this.value = value;
  148. }
  149. public static Level forInteger (final int value)
  150. {
  151. switch (value & 0xFF)
  152. {
  153. case 1: return WARNING;
  154. case 2: return FATAL;
  155. default: throw new IllegalArgumentException ("invalid alert level: " + value);
  156. }
  157. }
  158. public int getValue()
  159. {
  160. return value;
  161. }
  162. }
  163. /**
  164. * The description enumeration.
  165. */
  166. public static enum Description
  167. {
  168. CLOSE_NOTIFY ( 0),
  169. UNEXPECTED_MESSAGE ( 10),
  170. BAD_RECORD_MAC ( 20),
  171. DECRYPTION_FAILED ( 21),
  172. RECORD_OVERFLOW ( 22),
  173. DECOMPRESSION_FAILURE ( 30),
  174. HANDSHAKE_FAILURE ( 40),
  175. NO_CERTIFICATE ( 41),
  176. BAD_CERTIFICATE ( 42),
  177. UNSUPPORTED_CERTIFICATE ( 43),
  178. CERTIFICATE_REVOKED ( 44),
  179. CERTIFICATE_EXPIRED ( 45),
  180. CERTIFICATE_UNKNOWN ( 46),
  181. ILLEGAL_PARAMETER ( 47),
  182. UNKNOWN_CA ( 48),
  183. ACCESS_DENIED ( 49),
  184. DECODE_ERROR ( 50),
  185. DECRYPT_ERROR ( 51),
  186. EXPORT_RESTRICTION ( 60),
  187. PROTOCOL_VERSION ( 70),
  188. INSUFFICIENT_SECURITY ( 71),
  189. INTERNAL_ERROR ( 80),
  190. USER_CANCELED ( 90),
  191. NO_RENEGOTIATION (100),
  192. UNSUPPORTED_EXTENSION (110),
  193. CERTIFICATE_UNOBTAINABLE (111),
  194. UNRECOGNIZED_NAME (112),
  195. BAD_CERTIFICATE_STATUS_RESPONSE (113),
  196. BAD_CERTIFICATE_HASH_VALUE (114),
  197. UNKNOWN_SRP_USERNAME (120),
  198. MISSING_SRP_USERNAME (121);
  199. private final int value;
  200. private Description(int value)
  201. {
  202. this.value = value;
  203. }
  204. /**
  205. * Return an alert description object based on the specified integer
  206. * value.
  207. *
  208. * @param value The raw description value.
  209. * @return The appropriate description object.
  210. */
  211. public static Description forInteger (final int value)
  212. {
  213. switch (value & 0xFF)
  214. {
  215. case 0: return CLOSE_NOTIFY;
  216. case 10: return UNEXPECTED_MESSAGE;
  217. case 20: return BAD_RECORD_MAC;
  218. case 21: return DECRYPTION_FAILED;
  219. case 22: return RECORD_OVERFLOW;
  220. case 30: return DECOMPRESSION_FAILURE;
  221. case 40: return HANDSHAKE_FAILURE;
  222. case 41: return NO_CERTIFICATE;
  223. case 42: return BAD_CERTIFICATE;
  224. case 43: return UNSUPPORTED_CERTIFICATE;
  225. case 44: return CERTIFICATE_REVOKED;
  226. case 45: return CERTIFICATE_EXPIRED;
  227. case 46: return CERTIFICATE_UNKNOWN;
  228. case 47: return ILLEGAL_PARAMETER;
  229. case 48: return UNKNOWN_CA;
  230. case 49: return ACCESS_DENIED;
  231. case 50: return DECODE_ERROR;
  232. case 51: return DECRYPT_ERROR;
  233. case 60: return EXPORT_RESTRICTION;
  234. case 70: return PROTOCOL_VERSION;
  235. case 71: return INSUFFICIENT_SECURITY;
  236. case 80: return INTERNAL_ERROR;
  237. case 90: return USER_CANCELED;
  238. case 100: return NO_RENEGOTIATION;
  239. case 120: return UNKNOWN_SRP_USERNAME;
  240. case 121: return MISSING_SRP_USERNAME;
  241. default: throw new IllegalArgumentException("unknown alert description: " + value);
  242. }
  243. }
  244. public int getValue()
  245. {
  246. return value;
  247. }
  248. }
  249. }