src/java.base/share/classes/sun/security/ssl/Alerts.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 34380 jdk/src/java.base/share/classes/sun/security/ssl/Alerts.java@2b2609379881
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2015, 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
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 25859
diff changeset
    86
    // from RFC 7301 (TLS ALPN Extension)
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 25859
diff changeset
    87
    static final byte           alert_no_application_protocol = 120;
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 25859
diff changeset
    88
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static String alertDescription(byte code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        switch (code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        case alert_close_notify:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return "close_notify";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        case alert_unexpected_message:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return "unexpected_message";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        case alert_bad_record_mac:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return "bad_record_mac";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        case alert_decryption_failed:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return "decryption_failed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        case alert_record_overflow:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return "record_overflow";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        case alert_decompression_failure:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return "decompression_failure";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        case alert_handshake_failure:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return "handshake_failure";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        case alert_no_certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return "no_certificate";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        case alert_bad_certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return "bad_certificate";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        case alert_unsupported_certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return "unsupported_certificate";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        case alert_certificate_revoked:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return "certificate_revoked";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        case alert_certificate_expired:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return "certificate_expired";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        case alert_certificate_unknown:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return "certificate_unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        case alert_illegal_parameter:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return "illegal_parameter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        case alert_unknown_ca:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return "unknown_ca";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        case alert_access_denied:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return "access_denied";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        case alert_decode_error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return "decode_error";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        case alert_decrypt_error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return "decrypt_error";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        case alert_export_restriction:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            return "export_restriction";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        case alert_protocol_version:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return "protocol_version";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        case alert_insufficient_security:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return "insufficient_security";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        case alert_internal_error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return "internal_error";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        case alert_user_canceled:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return "user_canceled";
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   138
        case alert_no_renegotiation:
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   139
            return "no_renegotiation";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        case alert_unsupported_extension:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return "unsupported_extension";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        case alert_certificate_unobtainable:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return "certificate_unobtainable";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        case alert_unrecognized_name:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return "unrecognized_name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        case alert_bad_certificate_status_response:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return "bad_certificate_status_response";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        case alert_bad_certificate_hash_value:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return "bad_certificate_hash_value";
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 25859
diff changeset
   150
        case alert_no_application_protocol:
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 25859
diff changeset
   151
            return "no_application_protocol";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return "<UNKNOWN ALERT: " + (code & 0x0ff) + ">";
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
    static SSLException getSSLException(byte description, String reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return getSSLException(description, null, reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Try to be a little more specific in our choice of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * exceptions to throw.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    static SSLException getSSLException(byte description, Throwable cause,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            String reason) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        SSLException e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // the SSLException classes do not have a no-args constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // make up a message if there is none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (reason == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (cause != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                reason = cause.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                reason = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        switch (description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        case alert_handshake_failure:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        case alert_no_certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        case alert_bad_certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        case alert_unsupported_certificate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        case alert_certificate_revoked:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        case alert_certificate_expired:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        case alert_certificate_unknown:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        case alert_unknown_ca:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        case alert_access_denied:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        case alert_decrypt_error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        case alert_export_restriction:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        case alert_insufficient_security:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        case alert_unsupported_extension:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        case alert_certificate_unobtainable:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        case alert_unrecognized_name:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        case alert_bad_certificate_status_response:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        case alert_bad_certificate_hash_value:
34380
2b2609379881 8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents: 25859
diff changeset
   197
        case alert_no_application_protocol:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            e = new SSLHandshakeException(reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        case alert_close_notify:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        case alert_unexpected_message:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        case alert_bad_record_mac:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        case alert_decryption_failed:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        case alert_record_overflow:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        case alert_decompression_failure:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        case alert_illegal_parameter:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        case alert_decode_error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        case alert_protocol_version:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        case alert_internal_error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        case alert_user_canceled:
6856
533f4ad71f88 6914943: Implement final TLS renegotiation fix
xuelei
parents: 5506
diff changeset
   212
        case alert_no_renegotiation:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            e = new SSLException(reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (cause != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            e.initCause(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
}