jdk/src/share/classes/sun/security/ssl/Alerts.java
author xuelei
Fri, 08 Apr 2011 02:00:09 -0700
changeset 9246 c459f79af46b
parent 6856 533f4ad71f88
permissions -rw-r--r--
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled Summary: Reorg the SSLContext implementation Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A simple class to congregate alerts, their definitions, and common
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * support methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
final class Alerts {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     * Alerts are always a fixed two byte format (level/description).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    // warnings and fatal errors are package private facilities/constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // Alert levels (enum AlertLevel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static final byte           alert_warning = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final byte           alert_fatal = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Alert descriptions (enum AlertDescription)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * We may not use them all in our processing, but if someone
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * sends us one, we can at least convert it to a string for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final byte           alert_close_notify = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static final byte           alert_unexpected_message = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static final byte           alert_bad_record_mac = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static final byte           alert_decryption_failed = 21;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final byte           alert_record_overflow = 22;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static final byte           alert_decompression_failure = 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static final byte           alert_handshake_failure = 40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static final byte           alert_no_certificate = 41;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static final byte           alert_bad_certificate = 42;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static final byte           alert_unsupported_certificate = 43;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static final byte           alert_certificate_revoked = 44;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static final byte           alert_certificate_expired = 45;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final byte           alert_certificate_unknown = 46;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static final byte           alert_illegal_parameter = 47;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static final byte           alert_unknown_ca = 48;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static final byte           alert_access_denied = 49;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static final byte           alert_decode_error = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static final byte           alert_decrypt_error = 51;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static final byte           alert_export_restriction = 60;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static final byte           alert_protocol_version = 70;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static final byte           alert_insufficient_security = 71;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static final byte           alert_internal_error = 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static final byte           alert_user_canceled = 90;
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
    77
    static final byte           alert_no_renegotiation = 100;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // from RFC 3546 (TLS Extensions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static final byte           alert_unsupported_extension = 110;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static final byte           alert_certificate_unobtainable = 111;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static final byte           alert_unrecognized_name = 112;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static final byte           alert_bad_certificate_status_response = 113;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static final byte           alert_bad_certificate_hash_value = 114;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static String alertDescription(byte code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        switch (code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        case alert_close_notify:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return "close_notify";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        case alert_unexpected_message:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return "unexpected_message";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        case alert_bad_record_mac:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return "bad_record_mac";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        case alert_decryption_failed:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return "decryption_failed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        case alert_record_overflow:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return "record_overflow";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        case alert_decompression_failure:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            return "decompression_failure";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        case alert_handshake_failure:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return "handshake_failure";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        case alert_no_certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return "no_certificate";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        case alert_bad_certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return "bad_certificate";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        case alert_unsupported_certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return "unsupported_certificate";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        case alert_certificate_revoked:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return "certificate_revoked";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        case alert_certificate_expired:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return "certificate_expired";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        case alert_certificate_unknown:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return "certificate_unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        case alert_illegal_parameter:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return "illegal_parameter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        case alert_unknown_ca:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return "unknown_ca";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        case alert_access_denied:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return "access_denied";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        case alert_decode_error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return "decode_error";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        case alert_decrypt_error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return "decrypt_error";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        case alert_export_restriction:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return "export_restriction";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        case alert_protocol_version:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return "protocol_version";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        case alert_insufficient_security:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return "insufficient_security";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        case alert_internal_error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return "internal_error";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        case alert_user_canceled:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return "user_canceled";
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   135
        case alert_no_renegotiation:
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   136
            return "no_renegotiation";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        case alert_unsupported_extension:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return "unsupported_extension";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        case alert_certificate_unobtainable:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return "certificate_unobtainable";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        case alert_unrecognized_name:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return "unrecognized_name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        case alert_bad_certificate_status_response:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return "bad_certificate_status_response";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        case alert_bad_certificate_hash_value:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return "bad_certificate_hash_value";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return "<UNKNOWN ALERT: " + (code & 0x0ff) + ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    static SSLException getSSLException(byte description, String reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return getSSLException(description, null, reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Try to be a little more specific in our choice of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * exceptions to throw.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    static SSLException getSSLException(byte description, Throwable cause,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            String reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        SSLException e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // the SSLException classes do not have a no-args constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // make up a message if there is none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (reason == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (cause != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                reason = cause.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                reason = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        switch (description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        case alert_handshake_failure:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        case alert_no_certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        case alert_bad_certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        case alert_unsupported_certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        case alert_certificate_revoked:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        case alert_certificate_expired:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        case alert_certificate_unknown:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        case alert_unknown_ca:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        case alert_access_denied:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        case alert_decrypt_error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        case alert_export_restriction:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        case alert_insufficient_security:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        case alert_unsupported_extension:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        case alert_certificate_unobtainable:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        case alert_unrecognized_name:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        case alert_bad_certificate_status_response:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        case alert_bad_certificate_hash_value:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            e = new SSLHandshakeException(reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        case alert_close_notify:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        case alert_unexpected_message:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        case alert_bad_record_mac:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        case alert_decryption_failed:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        case alert_record_overflow:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        case alert_decompression_failure:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        case alert_illegal_parameter:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        case alert_decode_error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        case alert_protocol_version:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        case alert_internal_error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        case alert_user_canceled:
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   206
        case alert_no_renegotiation:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            e = new SSLException(reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (cause != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            e.initCause(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
}