src/java.base/share/classes/sun/security/x509/CertException.java
author darcy
Thu, 29 Aug 2019 10:52:21 -0700
changeset 57950 4612a3cfb927
parent 47216 71c04702a3d5
permissions -rw-r--r--
8229999: Apply java.io.Serial annotations to security types in java.base Reviewed-by: rriggs, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 2019, 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.x509;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * CertException indicates one of a variety of certificate problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @deprecated use one of Exceptions defined in the java.security.cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @see java.security.Certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
@Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class CertException extends SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47216
diff changeset
    42
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final long serialVersionUID = 6930793039696446142L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // Zero is reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /** Indicates that the signature in the certificate is not valid. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static final int verf_INVALID_SIG = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /** Indicates that the certificate was revoked, and so is invalid. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static final int verf_INVALID_REVOKED = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /** Indicates that the certificate is not yet valid. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static final int verf_INVALID_NOTBEFORE = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** Indicates that the certificate has expired and so is not valid. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static final int verf_INVALID_EXPIRED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** Indicates that a certificate authority in the certification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * chain is not trusted. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static final int verf_CA_UNTRUSTED = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /** Indicates that the certification chain is too long. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static final int verf_CHAIN_LENGTH = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /** Indicates an error parsing the ASN.1/DER encoding of the certificate. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final int verf_PARSE_ERROR = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /** Indicates an error constructing a certificate or certificate chain. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static final int err_CONSTRUCTION = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /** Indicates a problem with the public key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static final int err_INVALID_PUBLIC_KEY = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /** Indicates a problem with the certificate version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static final int err_INVALID_VERSION = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** Indicates a problem with the certificate format */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static final int err_INVALID_FORMAT = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /** Indicates a problem with the certificate encoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static final int err_ENCODING = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Private data members
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private int         verfCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private String      moreData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Constructs a certificate exception using an error code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * (<code>verf_*</code>) and a string describing the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * of the error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public CertException(int code, String moredata)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        verfCode = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        moreData = moredata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Constructs a certificate exception using just an error code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * without a string describing the context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public CertException(int code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        verfCode = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Returns the error code with which the exception was created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public int getVerfCode() { return verfCode; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Returns a string describing the context in which the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * was reported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public String getMoreData() { return moreData; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Return a string corresponding to the error code used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * this exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public String getVerfDescription()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        switch (verfCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        case verf_INVALID_SIG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return "The signature in the certificate is not valid.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        case verf_INVALID_REVOKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return "The certificate has been revoked.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        case verf_INVALID_NOTBEFORE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return "The certificate is not yet valid.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        case verf_INVALID_EXPIRED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return "The certificate has expired.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        case verf_CA_UNTRUSTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return "The Authority which issued the certificate is not trusted.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        case verf_CHAIN_LENGTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return "The certificate path to a trusted authority is too long.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        case verf_PARSE_ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return "The certificate could not be parsed.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        case err_CONSTRUCTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return "There was an error when constructing the certificate.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        case err_INVALID_PUBLIC_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return "The public key was not in the correct format.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        case err_INVALID_VERSION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return "The certificate has an invalid version number.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        case err_INVALID_FORMAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return "The certificate has an invalid format.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        case err_ENCODING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return "Problem encountered while encoding the data.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return "Unknown code:  " + verfCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
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
     * Returns a string describing the certificate exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public String toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    {
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   162
        return "[Certificate Exception: " + getMessage() + ']';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Returns a string describing the certificate exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public String getMessage()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return getVerfDescription()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                + ( (moreData != null)
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 25859
diff changeset
   172
                    ? ( "\n  (" + moreData + ')' ) : "" );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}