src/java.base/share/classes/javax/security/cert/Certificate.java
author xuelei
Wed, 13 Mar 2019 07:14:50 -0700
changeset 54099 fd332722014c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8160247: Mark deprecated javax.security.cert APIs with forRemoval=true Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54099
fd332722014c 8160247: Mark deprecated javax.security.cert APIs with forRemoval=true
xuelei
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package javax.security.cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.NoSuchProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>Abstract class for managing a variety of identity certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * An identity certificate is a guarantee by a principal that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * a public key is that of another principal.  (A principal represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * an entity such as an individual user, a group, or a corporation.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class is an abstraction for certificates that have different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * formats but important common uses.  For example, different types of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * certificates, such as X.509 and PGP, share general certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * functionality (like encoding and verifying) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * some types of information (like a public key).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * X.509, PGP, and SDSI certificates can all be implemented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * subclassing the Certificate class, even though they contain different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * sets of information, and they store and retrieve the information in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * different ways.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    52
 * <p><em>Note: The classes in the package {@code javax.security.cert}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * exist for compatibility with earlier versions of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Java Secure Sockets Extension (JSSE). New applications should instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * use the standard Java SE certificate classes located in
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    56
 * {@code java.security.cert}.</em></p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see X509Certificate
29377
d3f457ce9c48 8073430: Deprecate security APIs that have been superseded
juh
parents: 25859
diff changeset
    60
 * @deprecated Use the classes in {@code java.security.cert} instead.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
54099
fd332722014c 8160247: Mark deprecated javax.security.cert APIs with forRemoval=true
xuelei
parents: 47216
diff changeset
    64
@SuppressWarnings("removal")
fd332722014c 8160247: Mark deprecated javax.security.cert APIs with forRemoval=true
xuelei
parents: 47216
diff changeset
    65
@Deprecated(since="9", forRemoval=true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public abstract class Certificate {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Compares this certificate for equality with the specified
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    70
     * object. If the {@code other} object is an
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 5506
diff changeset
    71
     * {@code instanceof} {@code Certificate}, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * its encoded form is retrieved and compared with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * encoded form of this certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param other the object to test for equality with this certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @return true if the encoded forms of the two certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *         match, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (this == other)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (!(other instanceof Certificate))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            byte[] thisCert = this.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            byte[] otherCert = ((Certificate)other).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (thisCert.length != otherCert.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            for (int i = 0; i < thisCert.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                 if (thisCert[i] != otherCert[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                     return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Returns a hashcode value for this certificate from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * encoded form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @return the hashcode value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int     retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            byte[] certData = this.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            for (int i = 1; i < certData.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                 retval += certData[i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return (retval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } catch (CertificateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return (retval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Returns the encoded form of this certificate. It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * assumed that each certificate type would have only a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * form of encoding; for example, X.509 certificates would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * be encoded as ASN.1 DER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @return encoded form of this certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @exception CertificateEncodingException on internal certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *            encoding failure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public abstract byte[] getEncoded() throws CertificateEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Verifies that this certificate was signed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * private key that corresponds to the specified public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param key the PublicKey used to carry out the verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @exception NoSuchAlgorithmException on unsupported signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @exception InvalidKeyException on incorrect key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @exception NoSuchProviderException if there's no default provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @exception SignatureException on signature errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @exception CertificateException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public abstract void verify(PublicKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        throws CertificateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        InvalidKeyException, NoSuchProviderException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Verifies that this certificate was signed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * private key that corresponds to the specified public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * This method uses the signature verification engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * supplied by the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param key the PublicKey used to carry out the verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param sigProvider the name of the signature provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @exception NoSuchAlgorithmException on unsupported signature algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @exception InvalidKeyException on incorrect key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @exception NoSuchProviderException on incorrect provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @exception SignatureException on signature errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @exception CertificateException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public abstract void verify(PublicKey key, String sigProvider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        throws CertificateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        InvalidKeyException, NoSuchProviderException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Returns a string representation of this certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return a string representation of this certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public abstract String toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Gets the public key from this certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return the public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public abstract PublicKey getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
}