1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45:
46:
60: public final class Alert implements Constructed
61: {
62:
63:
64:
65:
66:
67: private final ByteBuffer buffer;
68:
69:
70:
71:
72: public Alert (final ByteBuffer buffer)
73: {
74: this.buffer = buffer;
75: }
76:
77: public Alert (final Level level, final Description description)
78: {
79: level.getClass ();
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 ();
85: }
86:
87:
88:
89:
90: public int length ()
91: {
92: return 2;
93: }
94:
95: byte[] getEncoded()
96: {
97: byte[] buf = new byte[2];
98: buffer.position (0);
99: buffer.get (buf);
100: return buf;
101: }
102:
103: public Level level()
104: {
105: return Level.forInteger (buffer.get (0) & 0xFF);
106: }
107:
108: public Description description()
109: {
110: return Description.forInteger (buffer.get (1) & 0xFF);
111: }
112:
113: public void setLevel (final Level level)
114: {
115: buffer.put (0, (byte) level.getValue ());
116: }
117:
118: public void setDescription (final Description description)
119: {
120: buffer.put (1, (byte) description.getValue ());
121: }
122:
123: public boolean equals (Object o)
124: {
125: if (!(o instanceof Alert))
126: return false;
127: Alert that = (Alert) o;
128: return that.buffer.position (0).equals (buffer.position (0));
129: }
130:
131: public int hashCode ()
132: {
133: return buffer.getShort (0) & 0xFFFF;
134: }
135:
136: public String toString()
137: {
138: return toString (null);
139: }
140:
141: public String toString (final String prefix)
142: {
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 ());
150: out.println (";");
151: if (prefix != null) out.print (prefix);
152: out.print (" description: ");
153: out.print (description ());
154: out.println (";");
155: if (prefix != null) out.print (prefix);
156: out.print ("} Alert;");
157: return str.toString ();
158: }
159:
160:
161:
162:
163:
170: public static enum Level
171: {
172:
173: WARNING (1), FATAL (2);
174:
175: private final int value;
176:
177: private Level(int value)
178: {
179: this.value = value;
180: }
181:
182: public static Level forInteger (final int value)
183: {
184: switch (value & 0xFF)
185: {
186: case 1: return WARNING;
187: case 2: return FATAL;
188: default: throw new IllegalArgumentException ("invalid alert level: " + value);
189: }
190: }
191:
192: public int getValue()
193: {
194: return value;
195: }
196: }
197:
198:
201: public static enum Description
202: {
203: CLOSE_NOTIFY ( 0),
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),
217: UNKNOWN_CA ( 48),
218: ACCESS_DENIED ( 49),
219: DECODE_ERROR ( 50),
220: DECRYPT_ERROR ( 51),
221: EXPORT_RESTRICTION ( 60),
222: PROTOCOL_VERSION ( 70),
223: INSUFFICIENT_SECURITY ( 71),
224: INTERNAL_ERROR ( 80),
225: USER_CANCELED ( 90),
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);
234:
235: private final int value;
236:
237: private Description(int value)
238: {
239: this.value = value;
240: }
241:
242:
249: public static Description forInteger (final int value)
250: {
251: switch (value & 0xFF)
252: {
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);
280: }
281: }
282:
283: public int getValue()
284: {
285: return value;
286: }
287: }
288: }