blob: 19ed26819b04996dda55af3e09f88a0f31a9d198 [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/*
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;
26import javax.security.cert.CertificateEncodingException;
27import javax.security.cert.CertificateException;
28
29/**
30 * Abstract class to represent identity certificates. It represents a way to
31 * verify the binding of a Principal and its public key. Examples are X.509,
Elliott Hughes2f9e4682009-10-09 17:21:46 -070032 * PGP, and SDSI.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080033 * <p>
34 * Note: This package is provided only for compatibility reasons.
35 * It contains a simplified version of the java.security.cert package that was
36 * previously used by JSSE (Java SSL package). All applications that do not have
37 * to be compatible with older versions of JSSE (that is before Java SDK 1.5)
38 * should only use java.security.cert.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080039 */
40public abstract class Certificate {
41
42 /**
43 * Creates a new {@code Certificate}.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080044 */
45 public Certificate() {}
46
47 /**
48 * Compares the argument to this Certificate. If both have the same bytes
49 * they are assumed to be equal.
Elliott Hughes2f9e4682009-10-09 17:21:46 -070050 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080051 * @param obj
52 * the {@code Certificate} to compare with this object
53 * @return <code>true</code> if {@code obj} is the same as this
54 * {@code Certificate}, <code>false</code> otherwise
55 * @see #hashCode
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080056 */
57 public boolean equals(Object obj) {
58 if (obj == this) {
59 return true;
60 }
61 if (!(obj instanceof Certificate)) {
62 return false;
63 }
64 Certificate object = (Certificate) obj;
65 try {
66 return Arrays.equals(getEncoded(), object.getEncoded());
67 } catch (CertificateEncodingException e) {
68 return false;
69 }
70 }
71
72 /**
73 * Returns an integer hash code for the receiver. Any two objects which
74 * return <code>true</code> when passed to <code>equals</code> must answer
75 * the same value for this method.
Elliott Hughes2f9e4682009-10-09 17:21:46 -070076 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080077 * @return the receiver's hash
78 * @see #equals
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080079 */
80 public int hashCode() {
81 int res = 0;
82 try {
83 byte[] array = getEncoded();
84 for (int i=0; i<array.length; i++) {
85 res += array[i];
86 }
87 } catch (CertificateEncodingException e) {
88 }
89 return res;
90 }
91
92 /**
93 * Returns the encoded representation for this certificate.
Elliott Hughes2f9e4682009-10-09 17:21:46 -070094 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080095 * @return the encoded representation for this certificate.
96 * @throws CertificateEncodingException
97 * if encoding fails.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080098 */
99 public abstract byte[] getEncoded()
100 throws CertificateEncodingException;
101
102 /**
103 * Verifies that this certificate was signed with the given public key.
Elliott Hughes2f9e4682009-10-09 17:21:46 -0700104 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800105 * @param key
106 * public key for which verification should be performed.
107 * @throws CertificateException
108 * if encoding errors are detected
109 * @throws NoSuchAlgorithmException
110 * if an unsupported algorithm is detected
111 * @throws InvalidKeyException
112 * if an invalid key is detected
113 * @throws NoSuchProviderException
114 * if there is no default provider
115 * @throws SignatureException
116 * if signature errors are detected
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800117 */
118 public abstract void verify(PublicKey key)
119 throws CertificateException, NoSuchAlgorithmException,
120 InvalidKeyException, NoSuchProviderException,
121 SignatureException;
122
123 /**
124 * Verifies that this certificate was signed with the given public key. Uses
125 * the signature algorithm given by the provider.
Elliott Hughes2f9e4682009-10-09 17:21:46 -0700126 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800127 * @param key
128 * public key for which verification should be performed.
129 * @param sigProvider
130 * the name of the signature provider.
131 * @exception CertificateException
132 * if encoding errors are detected
133 * @exception NoSuchAlgorithmException
134 * if an unsupported algorithm is detected
135 * @exception InvalidKeyException
136 * if an invalid key is detected
137 * @exception NoSuchProviderException
138 * if the specified provider does not exists.
139 * @exception SignatureException
140 * if signature errors are detected
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800141 */
142 public abstract void verify(PublicKey key, String sigProvider)
Elliott Hughesf33eae72010-05-13 12:36:25 -0700143 throws CertificateException, NoSuchAlgorithmException,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800144 InvalidKeyException, NoSuchProviderException,
145 SignatureException;
146
147 /**
148 * Returns a string containing a concise, human-readable description of the
149 * receiver.
Elliott Hughes2f9e4682009-10-09 17:21:46 -0700150 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800151 * @return a printable representation for the receiver.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800152 */
153 public abstract String toString();
154
155 /**
156 * Returns the public key corresponding to this certificate.
Elliott Hughes2f9e4682009-10-09 17:21:46 -0700157 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800158 * @return the public key corresponding to this certificate.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800159 */
160 public abstract PublicKey getPublicKey();
161}
162