X509V1CertImpl.java revision 11581:7a4b6292286b
1/*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.security.cert.internal.x509;
27
28import java.io.ByteArrayInputStream;
29import java.io.IOException;
30import java.io.Serializable;
31import java.io.InputStream;
32import java.io.ObjectInputStream;
33import java.io.OutputStream;
34import java.io.ObjectOutputStream;
35import java.math.BigInteger;
36import java.security.Signature;
37import javax.security.cert.*;
38import java.security.*;
39import java.util.Date;
40import java.util.BitSet;
41import java.util.Enumeration;
42import java.util.Vector;
43
44/**
45 * The X509V1CertImpl class is used as a conversion wrapper around
46 * sun.security.x509.X509Cert certificates when running under JDK1.1.x.
47 *
48 * @deprecated This is the implementation class for the deprecated
49 *  {@code javax.security.cert.X509Certificate} class. The classes in the
50 *  {@code java.security.cert} package should be used instead.
51 *
52 * @author Jeff Nisewanger
53 */
54@Deprecated
55public class X509V1CertImpl extends X509Certificate implements Serializable {
56    static final long serialVersionUID = -2048442350420423405L;
57    private java.security.cert.X509Certificate wrappedCert;
58
59    synchronized private static java.security.cert.CertificateFactory
60    getFactory()
61    throws java.security.cert.CertificateException
62    {
63        return java.security.cert.CertificateFactory.getInstance("X.509");
64    }
65
66    /**
67     * Default constructor.
68     */
69    public X509V1CertImpl() { }
70
71    /**
72     * Unmarshals a certificate from its encoded form, parsing the
73     * encoded bytes.  This form of constructor is used by agents which
74     * need to examine and use certificate contents.  That is, this is
75     * one of the more commonly used constructors.  Note that the buffer
76     * must include only a certificate, and no "garbage" may be left at
77     * the end.  If you need to ignore data at the end of a certificate,
78     * use another constructor.
79     *
80     * @param certData the encoded bytes, with no trailing padding.
81     * @exception CertificateException on parsing errors.
82     */
83    public X509V1CertImpl(byte[] certData)
84    throws CertificateException {
85        try {
86            ByteArrayInputStream bs;
87
88            bs = new ByteArrayInputStream(certData);
89            wrappedCert = (java.security.cert.X509Certificate)
90                getFactory().generateCertificate(bs);
91        } catch (java.security.cert.CertificateException e) {
92            throw new CertificateException(e.getMessage());
93        }
94    }
95
96    /**
97     * unmarshals an X.509 certificate from an input stream.
98     *
99     * @param in an input stream holding at least one certificate
100     * @exception CertificateException on parsing errors.
101     */
102    public X509V1CertImpl(InputStream in)
103    throws CertificateException {
104        try {
105            wrappedCert = (java.security.cert.X509Certificate)
106                getFactory().generateCertificate(in);
107        } catch (java.security.cert.CertificateException e) {
108            throw new CertificateException(e.getMessage());
109        }
110    }
111
112    /**
113     * Returns the encoded form of this certificate. It is
114     * assumed that each certificate type would have only a single
115     * form of encoding; for example, X.509 certificates would
116     * be encoded as ASN.1 DER.
117     */
118    public byte[] getEncoded() throws CertificateEncodingException {
119        try {
120            return wrappedCert.getEncoded();
121        } catch (java.security.cert.CertificateEncodingException e) {
122            throw new CertificateEncodingException(e.getMessage());
123        }
124    }
125
126    /**
127     * Throws an exception if the certificate was not signed using the
128     * verification key provided.  Successfully verifying a certificate
129     * does <em>not</em> indicate that one should trust the entity which
130     * it represents.
131     *
132     * @param key the public key used for verification.
133     */
134    public void verify(PublicKey key)
135        throws CertificateException, NoSuchAlgorithmException,
136        InvalidKeyException, NoSuchProviderException,
137        SignatureException
138    {
139        try {
140            wrappedCert.verify(key);
141        } catch (java.security.cert.CertificateException e) {
142            throw new CertificateException(e.getMessage());
143        }
144    }
145
146    /**
147     * Throws an exception if the certificate was not signed using the
148     * verification key provided.  Successfully verifying a certificate
149     * does <em>not</em> indicate that one should trust the entity which
150     * it represents.
151     *
152     * @param key the public key used for verification.
153     * @param sigProvider the name of the provider.
154     */
155    public void verify(PublicKey key, String sigProvider)
156        throws CertificateException, NoSuchAlgorithmException,
157        InvalidKeyException, NoSuchProviderException,
158        SignatureException
159    {
160        try {
161            wrappedCert.verify(key, sigProvider);
162        } catch (java.security.cert.CertificateException e) {
163            throw new CertificateException(e.getMessage());
164        }
165    }
166
167    /**
168     * Checks that the certificate is currently valid, i.e. the current
169     * time is within the specified validity period.
170     */
171    public void checkValidity() throws
172      CertificateExpiredException, CertificateNotYetValidException {
173        checkValidity(new Date());
174    }
175
176    /**
177     * Checks that the specified date is within the certificate's
178     * validity period, or basically if the certificate would be
179     * valid at the specified date/time.
180     *
181     * @param date the Date to check against to see if this certificate
182     *        is valid at that date/time.
183     */
184    public void checkValidity(Date date) throws
185      CertificateExpiredException, CertificateNotYetValidException {
186        try {
187            wrappedCert.checkValidity(date);
188        } catch (java.security.cert.CertificateNotYetValidException e) {
189            throw new CertificateNotYetValidException(e.getMessage());
190        } catch (java.security.cert.CertificateExpiredException e) {
191            throw new CertificateExpiredException(e.getMessage());
192        }
193    }
194
195
196    /**
197     * Returns a printable representation of the certificate.  This does not
198     * contain all the information available to distinguish this from any
199     * other certificate.  The certificate must be fully constructed
200     * before this function may be called.
201     */
202    public String toString() {
203        return wrappedCert.toString();
204    }
205
206    /**
207     * Gets the publickey from this certificate.
208     *
209     * @return the publickey.
210     */
211    public PublicKey getPublicKey() {
212        PublicKey key = wrappedCert.getPublicKey();
213        return key;
214    }
215
216    /*
217     * Gets the version number from the certificate.
218     *
219     * @return the version number.
220     */
221    public int getVersion() {
222        return wrappedCert.getVersion() - 1;
223    }
224
225    /**
226     * Gets the serial number from the certificate.
227     *
228     * @return the serial number.
229     */
230    public BigInteger getSerialNumber() {
231        return wrappedCert.getSerialNumber();
232    }
233
234    /**
235     * Gets the subject distinguished name from the certificate.
236     *
237     * @return the subject name.
238     * @exception CertificateException if a parsing error occurs.
239     */
240    public Principal getSubjectDN() {
241        return wrappedCert.getSubjectDN();
242    }
243
244    /**
245     * Gets the issuer distinguished name from the certificate.
246     *
247     * @return the issuer name.
248     * @exception CertificateException if a parsing error occurs.
249     */
250    public Principal getIssuerDN() {
251        return wrappedCert.getIssuerDN();
252    }
253
254    /**
255     * Gets the notBefore date from the validity period of the certificate.
256     *
257     * @return the start date of the validity period.
258     * @exception CertificateException if a parsing error occurs.
259     */
260    public Date getNotBefore() {
261        return wrappedCert.getNotBefore();
262    }
263
264    /**
265     * Gets the notAfter date from the validity period of the certificate.
266     *
267     * @return the end date of the validity period.
268     * @exception CertificateException if a parsing error occurs.
269     */
270    public Date getNotAfter() {
271        return wrappedCert.getNotAfter();
272    }
273
274    /**
275     * Gets the signature algorithm name for the certificate
276     * signature algorithm.
277     * For example, the string "SHA1/DSA".
278     *
279     * @return the signature algorithm name.
280     * @exception CertificateException if a parsing error occurs.
281     */
282    public String getSigAlgName() {
283        return wrappedCert.getSigAlgName();
284    }
285
286    /**
287     * Gets the signature algorithm OID string from the certificate.
288     * For example, the string "1.2.840.10040.4.3"
289     *
290     * @return the signature algorithm oid string.
291     * @exception CertificateException if a parsing error occurs.
292     */
293    public String getSigAlgOID() {
294        return wrappedCert.getSigAlgOID();
295    }
296
297    /**
298     * Gets the DER encoded signature algorithm parameters from this
299     * certificate's signature algorithm.
300     *
301     * @return the DER encoded signature algorithm parameters, or
302     *         null if no parameters are present.
303     * @exception CertificateException if a parsing error occurs.
304     */
305    public byte[] getSigAlgParams() {
306        return wrappedCert.getSigAlgParams();
307    }
308
309    private synchronized void writeObject(ObjectOutputStream stream)
310        throws IOException {
311        try {
312            stream.write(getEncoded());
313        } catch (CertificateEncodingException e) {
314            throw new IOException("getEncoded failed: " + e.getMessage());
315        }
316    }
317
318    private synchronized void readObject(ObjectInputStream stream)
319        throws IOException {
320        try {
321            wrappedCert = (java.security.cert.X509Certificate)
322                getFactory().generateCertificate(stream);
323        } catch (java.security.cert.CertificateException e) {
324            throw new IOException("generateCertificate failed: " + e.getMessage());
325        }
326    }
327
328    public java.security.cert.X509Certificate getX509Certificate() {
329        return wrappedCert;
330    }
331}
332