1 /* Alert.java -- SSL Alert message.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.net.ssl.provider;
41 import java.io.PrintWriter;
42 import java.io.StringWriter;
44 import java.nio.ByteBuffer;
47 * An alert message in the SSL protocol. Alerts are sent both as warnings
48 * which may allow execution to continue, or they may be fatal, which will
49 * halt this session. An alert object is composed of two enums -- the level,
50 * which indicates the seriousness of the alert, and the description, which
51 * indicates the reason for the alert.
56 * AlertDescription description;
60 public final class Alert implements Constructed
64 // -------------------------------------------------------------------------
66 /** The underlying byte buffer. */
67 private final ByteBuffer buffer;
70 // -------------------------------------------------------------------------
72 public Alert (final ByteBuffer buffer)
77 public Alert (final Level level, final Description description)
80 description.getClass ();
81 ByteBuffer b = ByteBuffer.allocate (2);
82 b.put (0, (byte) level.getValue ());
83 b.put (1, (byte) description.getValue ());
84 this.buffer = b.asReadOnlyBuffer ();
88 // -------------------------------------------------------------------------
97 byte[] buf = new byte[2];
105 return Level.forInteger (buffer.get (0) & 0xFF);
108 public Description description()
110 return Description.forInteger (buffer.get (1) & 0xFF);
113 public void setLevel (final Level level)
115 buffer.put (0, (byte) level.getValue ());
118 public void setDescription (final Description description)
120 buffer.put (1, (byte) description.getValue ());
123 public boolean equals (Object o)
125 if (!(o instanceof Alert))
127 Alert that = (Alert) o;
128 return that.buffer.position (0).equals (buffer.position (0));
131 public int hashCode ()
133 return buffer.getShort (0) & 0xFFFF;
136 public String toString()
138 return toString (null);
141 public String toString (final String prefix)
143 StringWriter str = new StringWriter ();
144 PrintWriter out = new PrintWriter (str);
145 if (prefix != null) out.print (prefix);
146 out.println ("struct {");
147 if (prefix != null) out.print (prefix);
148 out.print (" level: ");
149 out.print (level ());
151 if (prefix != null) out.print (prefix);
152 out.print (" description: ");
153 out.print (description ());
155 if (prefix != null) out.print (prefix);
156 out.print ("} Alert;");
157 return str.toString ();
161 // -------------------------------------------------------------------------
164 * The level enumeration.
167 * enum { warning(1), fatal(2), (255) } AlertLevel;
170 public static enum Level
173 WARNING (1), FATAL (2);
175 private final int value;
177 private Level(int value)
182 public static Level forInteger (final int value)
184 switch (value & 0xFF)
186 case 1: return WARNING;
187 case 2: return FATAL;
188 default: throw new IllegalArgumentException ("invalid alert level: " + value);
192 public int getValue()
199 * The description enumeration.
201 public static enum Description
204 UNEXPECTED_MESSAGE ( 10),
205 BAD_RECORD_MAC ( 20),
206 DECRYPTION_FAILED ( 21),
207 RECORD_OVERFLOW ( 22),
208 DECOMPRESSION_FAILURE ( 30),
209 HANDSHAKE_FAILURE ( 40),
210 NO_CERTIFICATE ( 41),
211 BAD_CERTIFICATE ( 42),
212 UNSUPPORTED_CERTIFICATE ( 43),
213 CERTIFICATE_REVOKED ( 44),
214 CERTIFICATE_EXPIRED ( 45),
215 CERTIFICATE_UNKNOWN ( 46),
216 ILLEGAL_PARAMETER ( 47),
221 EXPORT_RESTRICTION ( 60),
222 PROTOCOL_VERSION ( 70),
223 INSUFFICIENT_SECURITY ( 71),
224 INTERNAL_ERROR ( 80),
226 NO_RENEGOTIATION (100),
227 UNSUPPORTED_EXTENSION (110),
228 CERTIFICATE_UNOBTAINABLE (111),
229 UNRECOGNIZED_NAME (112),
230 BAD_CERTIFICATE_STATUS_RESPONSE (113),
231 BAD_CERTIFICATE_HASH_VALUE (114),
232 UNKNOWN_SRP_USERNAME (120),
233 MISSING_SRP_USERNAME (121);
235 private final int value;
237 private Description(int value)
243 * Return an alert description object based on the specified integer
246 * @param value The raw description value.
247 * @return The appropriate description object.
249 public static Description forInteger (final int value)
251 switch (value & 0xFF)
253 case 0: return CLOSE_NOTIFY;
254 case 10: return UNEXPECTED_MESSAGE;
255 case 20: return BAD_RECORD_MAC;
256 case 21: return DECRYPTION_FAILED;
257 case 22: return RECORD_OVERFLOW;
258 case 30: return DECOMPRESSION_FAILURE;
259 case 40: return HANDSHAKE_FAILURE;
260 case 41: return NO_CERTIFICATE;
261 case 42: return BAD_CERTIFICATE;
262 case 43: return UNSUPPORTED_CERTIFICATE;
263 case 44: return CERTIFICATE_REVOKED;
264 case 45: return CERTIFICATE_EXPIRED;
265 case 46: return CERTIFICATE_UNKNOWN;
266 case 47: return ILLEGAL_PARAMETER;
267 case 48: return UNKNOWN_CA;
268 case 49: return ACCESS_DENIED;
269 case 50: return DECODE_ERROR;
270 case 51: return DECRYPT_ERROR;
271 case 60: return EXPORT_RESTRICTION;
272 case 70: return PROTOCOL_VERSION;
273 case 71: return INSUFFICIENT_SECURITY;
274 case 80: return INTERNAL_ERROR;
275 case 90: return USER_CANCELED;
276 case 100: return NO_RENEGOTIATION;
277 case 120: return UNKNOWN_SRP_USERNAME;
278 case 121: return MISSING_SRP_USERNAME;
279 default: throw new IllegalArgumentException("unknown alert description: " + value);
283 public int getValue()