001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package javax.security.cert;
022:
023: import java.io.ByteArrayInputStream;
024: import java.io.InputStream;
025: import java.lang.reflect.Constructor;
026: import java.math.BigInteger;
027: import java.security.AccessController;
028: import java.security.InvalidKeyException;
029: import java.security.NoSuchAlgorithmException;
030: import java.security.NoSuchProviderException;
031: import java.security.Principal;
032: import java.security.PublicKey;
033: import java.security.Security;
034: import java.security.SignatureException;
035: import java.security.cert.CertificateFactory;
036: import java.util.Date;
037: import javax.security.cert.Certificate;
038: import javax.security.cert.CertificateEncodingException;
039: import javax.security.cert.CertificateException;
040: import javax.security.cert.CertificateExpiredException;
041: import javax.security.cert.CertificateNotYetValidException;
042:
043: import org.apache.harmony.security.internal.nls.Messages;
044:
045: /**
046: * @com.intel.drl.spec_ref
047: */
048: public abstract class X509Certificate extends Certificate {
049:
050: private static Constructor constructor;
051:
052: static {
053: try {
054: String classname = (String) AccessController
055: .doPrivileged(new java.security.PrivilegedAction() {
056: public Object run() {
057: return Security
058: .getProperty("cert.provider.x509v1"); //$NON-NLS-1$
059: }
060: });
061: Class cl = Class.forName(classname);
062: constructor = cl
063: .getConstructor(new Class[] { InputStream.class });
064: } catch (Throwable e) {
065: }
066: }
067:
068: /**
069: * @com.intel.drl.spec_ref
070: */
071: public X509Certificate() {
072: super ();
073: }
074:
075: /**
076: * @com.intel.drl.spec_ref
077: */
078: public static final X509Certificate getInstance(InputStream inStream)
079: throws CertificateException {
080: if (inStream == null) {
081: throw new CertificateException(Messages
082: .getString("security.87")); //$NON-NLS-1$
083: }
084: if (constructor != null) {
085: try {
086: return (X509Certificate) constructor
087: .newInstance(new Object[] { inStream });
088: } catch (Throwable e) {
089: throw new CertificateException(e.getMessage());
090: }
091: }
092:
093: final java.security.cert.X509Certificate cert;
094: try {
095: CertificateFactory cf = CertificateFactory
096: .getInstance("X.509"); //$NON-NLS-1$
097: cert = (java.security.cert.X509Certificate) cf
098: .generateCertificate(inStream);
099: } catch (java.security.cert.CertificateException e) {
100: throw new CertificateException(e.getMessage());
101: }
102:
103: return new X509Certificate() {
104:
105: public byte[] getEncoded()
106: throws CertificateEncodingException {
107: try {
108: return cert.getEncoded();
109: } catch (java.security.cert.CertificateEncodingException e) {
110: throw new CertificateEncodingException(e
111: .getMessage());
112: }
113: }
114:
115: public void verify(PublicKey key)
116: throws CertificateException,
117: NoSuchAlgorithmException, InvalidKeyException,
118: NoSuchProviderException, SignatureException {
119: try {
120: cert.verify(key);
121: } catch (java.security.cert.CertificateException e) {
122: throw new CertificateException(e.getMessage());
123: }
124: }
125:
126: public void verify(PublicKey key, String sigProvider)
127: throws CertificateException,
128: NoSuchAlgorithmException, InvalidKeyException,
129: NoSuchProviderException, SignatureException {
130: try {
131: cert.verify(key, sigProvider);
132: } catch (java.security.cert.CertificateException e) {
133: throw new CertificateException(e.getMessage());
134: }
135: }
136:
137: public String toString() {
138: return cert.toString();
139: }
140:
141: public PublicKey getPublicKey() {
142: return cert.getPublicKey();
143: }
144:
145: public void checkValidity()
146: throws CertificateExpiredException,
147: CertificateNotYetValidException {
148: try {
149: cert.checkValidity();
150: } catch (java.security.cert.CertificateNotYetValidException e) {
151: throw new CertificateNotYetValidException(e
152: .getMessage());
153: } catch (java.security.cert.CertificateExpiredException e) {
154: throw new CertificateExpiredException(e
155: .getMessage());
156: }
157: }
158:
159: public void checkValidity(Date date)
160: throws CertificateExpiredException,
161: CertificateNotYetValidException {
162: try {
163: cert.checkValidity(date);
164: } catch (java.security.cert.CertificateNotYetValidException e) {
165: throw new CertificateNotYetValidException(e
166: .getMessage());
167: } catch (java.security.cert.CertificateExpiredException e) {
168: throw new CertificateExpiredException(e
169: .getMessage());
170: }
171: }
172:
173: public int getVersion() {
174: return 2;
175: }
176:
177: public BigInteger getSerialNumber() {
178: return cert.getSerialNumber();
179: }
180:
181: public Principal getIssuerDN() {
182: return cert.getIssuerDN();
183: }
184:
185: public Principal getSubjectDN() {
186: return cert.getSubjectDN();
187: }
188:
189: public Date getNotBefore() {
190: return cert.getNotBefore();
191: }
192:
193: public Date getNotAfter() {
194: return cert.getNotAfter();
195: }
196:
197: public String getSigAlgName() {
198: return cert.getSigAlgName();
199: }
200:
201: public String getSigAlgOID() {
202: return cert.getSigAlgOID();
203: }
204:
205: public byte[] getSigAlgParams() {
206: return cert.getSigAlgParams();
207: }
208: };
209: }
210:
211: /**
212: * @com.intel.drl.spec_ref
213: */
214: public static final X509Certificate getInstance(byte[] certData)
215: throws CertificateException {
216: if (certData == null) {
217: throw new CertificateException(Messages
218: .getString("security.88")); //$NON-NLS-1$
219: }
220: ByteArrayInputStream bais = new ByteArrayInputStream(certData);
221: return getInstance(bais);
222: }
223:
224: /**
225: * @com.intel.drl.spec_ref
226: */
227: public abstract void checkValidity()
228: throws CertificateExpiredException,
229: CertificateNotYetValidException;
230:
231: /**
232: * @com.intel.drl.spec_ref
233: */
234: public abstract void checkValidity(Date date)
235: throws CertificateExpiredException,
236: CertificateNotYetValidException;
237:
238: /**
239: * @com.intel.drl.spec_ref
240: */
241: public abstract int getVersion();
242:
243: /**
244: * @com.intel.drl.spec_ref
245: */
246: public abstract BigInteger getSerialNumber();
247:
248: /**
249: * @com.intel.drl.spec_ref
250: */
251: public abstract Principal getIssuerDN();
252:
253: /**
254: * @com.intel.drl.spec_ref
255: */
256: public abstract Principal getSubjectDN();
257:
258: /**
259: * @com.intel.drl.spec_ref
260: */
261: public abstract Date getNotBefore();
262:
263: /**
264: * @com.intel.drl.spec_ref
265: */
266: public abstract Date getNotAfter();
267:
268: /**
269: * @com.intel.drl.spec_ref
270: */
271: public abstract String getSigAlgName();
272:
273: /**
274: * @com.intel.drl.spec_ref
275: */
276: public abstract String getSigAlgOID();
277:
278: /**
279: * @com.intel.drl.spec_ref
280: */
281: public abstract byte[] getSigAlgParams();
282: }
|