1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package javax.security.cert;
19
20import java.security.InvalidKeyException;
21import java.security.NoSuchAlgorithmException;
22import java.security.NoSuchProviderException;
23import java.security.PublicKey;
24import java.security.SignatureException;
25import java.util.Arrays;
26
27/**
28 * Abstract class to represent identity certificates. It represents a way to
29 * verify the binding of a Principal and its public key. Examples are X.509,
30 * PGP, and SDSI.
31 * <p>
32 * Note: This package is provided only for compatibility reasons.
33 * It contains a simplified version of the java.security.cert package that was
34 * previously used by JSSE (Java SSL package). All applications that do not have
35 * to be compatible with older versions of JSSE (that is before Java SDK 1.5)
36 * should only use java.security.cert.
37 */
38public abstract class Certificate {
39
40    /**
41     * Creates a new {@code Certificate}.
42     */
43    public Certificate() {}
44
45    /**
46     * Compares the argument to this Certificate. If both have the same bytes
47     * they are assumed to be equal.
48     *
49     * @param obj
50     *            the {@code Certificate} to compare with this object
51     * @return <code>true</code> if {@code obj} is the same as this
52     *         {@code Certificate}, <code>false</code> otherwise
53     * @see #hashCode
54     */
55    public boolean equals(Object obj) {
56        if (obj == this) {
57            return true;
58        }
59        if (!(obj instanceof Certificate)) {
60            return false;
61        }
62        Certificate object = (Certificate) obj;
63        try {
64            return Arrays.equals(getEncoded(), object.getEncoded());
65        } catch (CertificateEncodingException e) {
66            return false;
67        }
68    }
69
70    /**
71     * Returns an integer hash code for the receiver. Any two objects which
72     * return <code>true</code> when passed to <code>equals</code> must answer
73     * the same value for this method.
74     *
75     * @return the receiver's hash
76     * @see #equals
77     */
78    public int hashCode() {
79        int res = 0;
80        try {
81            byte[] array = getEncoded();
82            for (int i=0; i<array.length; i++) {
83                res += array[i];
84            }
85        } catch (CertificateEncodingException e) {
86        }
87        return res;
88    }
89
90    /**
91     * Returns the encoded representation for this certificate.
92     *
93     * @return the encoded representation for this certificate.
94     * @throws CertificateEncodingException
95     *             if encoding fails.
96     */
97    public abstract byte[] getEncoded()
98            throws CertificateEncodingException;
99
100    /**
101     * Verifies that this certificate was signed with the given public key.
102     *
103     * @param key
104     *            public key for which verification should be performed.
105     * @throws CertificateException
106     *             if encoding errors are detected
107     * @throws NoSuchAlgorithmException
108     *             if an unsupported algorithm is detected
109     * @throws InvalidKeyException
110     *             if an invalid key is detected
111     * @throws NoSuchProviderException
112     *             if there is no default provider
113     * @throws SignatureException
114     *             if signature errors are detected
115     */
116    public abstract void verify(PublicKey key)
117            throws CertificateException, NoSuchAlgorithmException,
118                   InvalidKeyException, NoSuchProviderException,
119                   SignatureException;
120
121    /**
122     * Verifies that this certificate was signed with the given public key. Uses
123     * the signature algorithm given by the provider.
124     *
125     * @param key
126     *            public key for which verification should be performed.
127     * @param sigProvider
128     *            the name of the signature provider.
129     * @throws CertificateException
130     *                if encoding errors are detected
131     * @throws NoSuchAlgorithmException
132     *                if an unsupported algorithm is detected
133     * @throws InvalidKeyException
134     *                if an invalid key is detected
135     * @throws NoSuchProviderException
136     *                if the specified provider does not exists.
137     * @throws SignatureException
138     *                if signature errors are detected
139     */
140    public abstract void verify(PublicKey key, String sigProvider)
141            throws CertificateException, NoSuchAlgorithmException,
142                   InvalidKeyException, NoSuchProviderException,
143                   SignatureException;
144
145    /**
146     * Returns a string containing a concise, human-readable description of the
147     * receiver.
148     *
149     * @return a printable representation for the receiver.
150     */
151    public abstract String toString();
152
153    /**
154     * Returns the public key corresponding to this certificate.
155     *
156     * @return the public key corresponding to this certificate.
157     */
158    public abstract PublicKey getPublicKey();
159}
160