The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | |
| 18 | package javax.security.cert; |
| 19 | |
| 20 | import java.security.InvalidKeyException; |
| 21 | import java.security.NoSuchAlgorithmException; |
| 22 | import java.security.NoSuchProviderException; |
| 23 | import java.security.PublicKey; |
| 24 | import java.security.SignatureException; |
| 25 | import java.util.Arrays; |
| 26 | import javax.security.cert.CertificateEncodingException; |
| 27 | import 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 Hughes | 2f9e468 | 2009-10-09 17:21:46 -0700 | [diff] [blame] | 32 | * PGP, and SDSI. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 33 | * <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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 39 | */ |
| 40 | public abstract class Certificate { |
| 41 | |
| 42 | /** |
| 43 | * Creates a new {@code Certificate}. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 44 | */ |
| 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 Hughes | 2f9e468 | 2009-10-09 17:21:46 -0700 | [diff] [blame] | 50 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 51 | * @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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 56 | */ |
| 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 Hughes | 2f9e468 | 2009-10-09 17:21:46 -0700 | [diff] [blame] | 76 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 77 | * @return the receiver's hash |
| 78 | * @see #equals |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 79 | */ |
| 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 Hughes | 2f9e468 | 2009-10-09 17:21:46 -0700 | [diff] [blame] | 94 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 95 | * @return the encoded representation for this certificate. |
| 96 | * @throws CertificateEncodingException |
| 97 | * if encoding fails. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 98 | */ |
| 99 | public abstract byte[] getEncoded() |
| 100 | throws CertificateEncodingException; |
| 101 | |
| 102 | /** |
| 103 | * Verifies that this certificate was signed with the given public key. |
Elliott Hughes | 2f9e468 | 2009-10-09 17:21:46 -0700 | [diff] [blame] | 104 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 105 | * @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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 117 | */ |
| 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 Hughes | 2f9e468 | 2009-10-09 17:21:46 -0700 | [diff] [blame] | 126 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 127 | * @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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 141 | */ |
| 142 | public abstract void verify(PublicKey key, String sigProvider) |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame^] | 143 | throws CertificateException, NoSuchAlgorithmException, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 144 | InvalidKeyException, NoSuchProviderException, |
| 145 | SignatureException; |
| 146 | |
| 147 | /** |
| 148 | * Returns a string containing a concise, human-readable description of the |
| 149 | * receiver. |
Elliott Hughes | 2f9e468 | 2009-10-09 17:21:46 -0700 | [diff] [blame] | 150 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 151 | * @return a printable representation for the receiver. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 152 | */ |
| 153 | public abstract String toString(); |
| 154 | |
| 155 | /** |
| 156 | * Returns the public key corresponding to this certificate. |
Elliott Hughes | 2f9e468 | 2009-10-09 17:21:46 -0700 | [diff] [blame] | 157 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 158 | * @return the public key corresponding to this certificate. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 159 | */ |
| 160 | public abstract PublicKey getPublicKey(); |
| 161 | } |
| 162 | |