Source for gnu.javax.net.ssl.provider.Alert

   1: /* Alert.java -- SSL Alert message.
   2:    Copyright (C) 2006  Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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
  19: USA
  20: 
  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
  24: combination.
  25: 
  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.  */
  37: 
  38: 
  39: package gnu.javax.net.ssl.provider;
  40: 
  41: import java.io.PrintWriter;
  42: import java.io.StringWriter;
  43: 
  44: import java.nio.ByteBuffer;
  45: 
  46: /**
  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.
  52:  *
  53:  * <pre>
  54:  * struct {
  55:  *   AlertLevel       level;
  56:  *   AlertDescription description;
  57:  * }
  58:  * </pre>
  59:  */
  60: public final class Alert implements Constructed
  61: {
  62: 
  63:   // Fields.
  64:   // -------------------------------------------------------------------------
  65: 
  66:   /** The underlying byte buffer. */
  67:   private final ByteBuffer buffer;
  68: 
  69:   // Constructor.
  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:   // Instance methods.
  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:   // Enumerations.
 161:   // -------------------------------------------------------------------------
 162: 
 163:   /**
 164:    * The level enumeration.
 165:    *
 166:    * <pre>
 167:    * enum { warning(1), fatal(2), (255) } AlertLevel;
 168:    * </pre>
 169:    */
 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:   /**
 199:    * The description enumeration.
 200:    */
 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:     /**
 243:      * Return an alert description object based on the specified integer
 244:      * value.
 245:      *
 246:      * @param value The raw description value.
 247:      * @return The appropriate description object.
 248:      */
 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: }