| 1 | /* Certificate.java --- Certificate class
|
|---|
| 2 | Copyright (C) 1999 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.security.cert;
|
|---|
| 40 | import java.security.PublicKey;
|
|---|
| 41 | import java.security.NoSuchAlgorithmException;
|
|---|
| 42 | import java.security.InvalidKeyException;
|
|---|
| 43 | import java.security.NoSuchProviderException;
|
|---|
| 44 | import java.security.SignatureException;
|
|---|
| 45 | import java.io.ObjectInputStream;
|
|---|
| 46 | import java.io.ByteArrayInputStream;
|
|---|
| 47 | import java.io.ObjectStreamException;
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | The Certificate class is an abstract class used to manage
|
|---|
| 51 | identity certificates. An identity certificate is a
|
|---|
| 52 | combination of a principal and a public key which is
|
|---|
| 53 | certified by another principal. This is the puprose of
|
|---|
| 54 | Certificate Authorities (CA).
|
|---|
| 55 |
|
|---|
| 56 | This class is used to manage different types of certificates
|
|---|
| 57 | but have important common puposes. Different types of
|
|---|
| 58 | certificates like X.509 and OpenPGP share general certificate
|
|---|
| 59 | functions (like encoding and verifying) and information like
|
|---|
| 60 | public keys.
|
|---|
| 61 |
|
|---|
| 62 | X.509, OpenPGP, and SDSI can be implemented by subclassing this
|
|---|
| 63 | class even though they differ in storage methods and information
|
|---|
| 64 | stored.
|
|---|
| 65 |
|
|---|
| 66 | @since JDK 1.2
|
|---|
| 67 |
|
|---|
| 68 | @author Mark Benvenuto
|
|---|
| 69 | */
|
|---|
| 70 | public abstract class Certificate
|
|---|
| 71 | {
|
|---|
| 72 |
|
|---|
| 73 | private String type;
|
|---|
| 74 | /**
|
|---|
| 75 | Constructs a new certificate of the specified type. An example
|
|---|
| 76 | is "X.509".
|
|---|
| 77 |
|
|---|
| 78 | @param type a valid standard name for a certificate.
|
|---|
| 79 | */
|
|---|
| 80 | protected Certificate(String type)
|
|---|
| 81 | {
|
|---|
| 82 | this.type = type;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | Returns the Certificate type.
|
|---|
| 87 |
|
|---|
| 88 | @return a string representing the Certificate type
|
|---|
| 89 | */
|
|---|
| 90 | public final String getType()
|
|---|
| 91 | {
|
|---|
| 92 | return type;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | Compares this Certificate to other. It checks if the
|
|---|
| 97 | object if instanceOf Certificate and then checks if
|
|---|
| 98 | the encoded form matches.
|
|---|
| 99 |
|
|---|
| 100 | @param other An Object to test for equality
|
|---|
| 101 |
|
|---|
| 102 | @return true if equal, false otherwise
|
|---|
| 103 | */
|
|---|
| 104 | public boolean equals(Object other)
|
|---|
| 105 | {
|
|---|
| 106 | if( other instanceof Certificate ) {
|
|---|
| 107 | try {
|
|---|
| 108 | Certificate x = (Certificate) other;
|
|---|
| 109 | if( getEncoded().length != x.getEncoded().length )
|
|---|
| 110 | return false;
|
|---|
| 111 |
|
|---|
| 112 | byte b1[] = getEncoded();
|
|---|
| 113 | byte b2[] = x.getEncoded();
|
|---|
| 114 |
|
|---|
| 115 | for( int i = 0; i < b1.length; i++ )
|
|---|
| 116 | if( b1[i] != b2[i] )
|
|---|
| 117 | return false;
|
|---|
| 118 |
|
|---|
| 119 | } catch( CertificateEncodingException cee ) {
|
|---|
| 120 | return false;
|
|---|
| 121 | }
|
|---|
| 122 | return true;
|
|---|
| 123 | }
|
|---|
| 124 | return false;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /**
|
|---|
| 128 | Returns a hash code for this Certificate in its encoded
|
|---|
| 129 | form.
|
|---|
| 130 |
|
|---|
| 131 | @return A hash code of this class
|
|---|
| 132 | */
|
|---|
| 133 | public int hashCode()
|
|---|
| 134 | {
|
|---|
| 135 | return super.hashCode();
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /**
|
|---|
| 139 | Gets the DER ASN.1 encoded format for this Certificate.
|
|---|
| 140 | It assumes each certificate has only one encoding format.
|
|---|
| 141 | Ex: X.509 is encoded as ASN.1 DER
|
|---|
| 142 |
|
|---|
| 143 | @return byte array containg encoded form
|
|---|
| 144 |
|
|---|
| 145 | @throws CertificateEncodingException if an error occurs
|
|---|
| 146 | */
|
|---|
| 147 | public abstract byte[] getEncoded() throws CertificateEncodingException;
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | Verifies that this Certificate was properly signed with the
|
|---|
| 151 | PublicKey that corresponds to its private key.
|
|---|
| 152 |
|
|---|
| 153 | @param key PublicKey to verify with
|
|---|
| 154 |
|
|---|
| 155 | @throws CertificateException encoding error
|
|---|
| 156 | @throws NoSuchAlgorithmException unsupported algorithm
|
|---|
| 157 | @throws InvalidKeyException incorrect key
|
|---|
| 158 | @throws NoSuchProviderException no provider
|
|---|
| 159 | @throws SignatureException signature error
|
|---|
| 160 | */
|
|---|
| 161 | public abstract void verify(PublicKey key)
|
|---|
| 162 | throws CertificateException,
|
|---|
| 163 | NoSuchAlgorithmException,
|
|---|
| 164 | InvalidKeyException,
|
|---|
| 165 | NoSuchProviderException,
|
|---|
| 166 | SignatureException;
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| 169 | Verifies that this Certificate was properly signed with the
|
|---|
| 170 | PublicKey that corresponds to its private key and uses
|
|---|
| 171 | the signature engine provided by the provider.
|
|---|
| 172 |
|
|---|
| 173 | @param key PublicKey to verify with
|
|---|
| 174 | @param sigProvider Provider to use for signature algorithm
|
|---|
| 175 |
|
|---|
| 176 | @throws CertificateException encoding error
|
|---|
| 177 | @throws NoSuchAlgorithmException unsupported algorithm
|
|---|
| 178 | @throws InvalidKeyException incorrect key
|
|---|
| 179 | @throws NoSuchProviderException incorrect provider
|
|---|
| 180 | @throws SignatureException signature error
|
|---|
| 181 | */
|
|---|
| 182 | public abstract void verify(PublicKey key,
|
|---|
| 183 | String sigProvider)
|
|---|
| 184 | throws CertificateException,
|
|---|
| 185 | NoSuchAlgorithmException,
|
|---|
| 186 | InvalidKeyException,
|
|---|
| 187 | NoSuchProviderException,
|
|---|
| 188 | SignatureException;
|
|---|
| 189 |
|
|---|
| 190 | /**
|
|---|
| 191 | Returns a string representing the Certificate.
|
|---|
| 192 |
|
|---|
| 193 | @return a string representing the Certificate.
|
|---|
| 194 | */
|
|---|
| 195 | public abstract String toString();
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 | /**
|
|---|
| 199 | Returns the public key stored in the Certificate.
|
|---|
| 200 |
|
|---|
| 201 | @return The public key
|
|---|
| 202 | */
|
|---|
| 203 | public abstract PublicKey getPublicKey();
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 | /* INNER CLASS */
|
|---|
| 207 | /**
|
|---|
| 208 | Certificate.CertificateRep is an inner class used to provide an alternate
|
|---|
| 209 | storage mechanism for serialized Certificates.
|
|---|
| 210 | */
|
|---|
| 211 | protected static class CertificateRep implements java.io.Serializable
|
|---|
| 212 | {
|
|---|
| 213 | private String type;
|
|---|
| 214 | private byte[] data;
|
|---|
| 215 |
|
|---|
| 216 | /**
|
|---|
| 217 | Create an alternate Certificate class to store a serialized Certificate
|
|---|
| 218 |
|
|---|
| 219 | @param type the name of certificate type
|
|---|
| 220 | @param data the certificate data
|
|---|
| 221 | */
|
|---|
| 222 | protected CertificateRep(String type,
|
|---|
| 223 | byte[] data)
|
|---|
| 224 | {
|
|---|
| 225 | this.type = type;
|
|---|
| 226 | this.data = data;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | /**
|
|---|
| 230 | Return the stored Certificate
|
|---|
| 231 |
|
|---|
| 232 | @return the stored certificate
|
|---|
| 233 |
|
|---|
| 234 | @throws ObjectStreamException if certificate cannot be resolved
|
|---|
| 235 | */
|
|---|
| 236 | protected Object readResolve()
|
|---|
| 237 | throws ObjectStreamException
|
|---|
| 238 | {
|
|---|
| 239 | try {
|
|---|
| 240 | return new ObjectInputStream( new ByteArrayInputStream( data ) ).readObject();
|
|---|
| 241 | } catch ( Exception e ) {
|
|---|
| 242 | e.printStackTrace();
|
|---|
| 243 | throw new RuntimeException ( e.toString() );
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | }
|
|---|