001: /*
002: * @(#)CertificateException.java 1.22 02/07/24 @(#)
003: *
004: * Copyright (c) 2001-2002 Sun Microsystems, Inc. All rights reserved.
005: * PROPRIETARY/CONFIDENTIAL
006: * Use is subject to license terms.
007: */
008:
009: package com.sun.portal.microedition.pki;
010:
011: import com.sun.portal.microedition.pki.Certificate;
012:
013: import java.lang.String;
014:
015: /**
016: * The <CODE>CertificateException</CODE> encapsulates an error that
017: * occurred while a <CODE>Certificate</CODE> is being used. If multiple errors
018: * are found within a <CODE>Certificate</CODE> the more significant error
019: * should be reported in the exception.
020: * @since MIDP 2.0
021: */
022: public class CertificateException extends java.io.IOException {
023:
024: /** The reason code for this exception */
025: private byte reason;
026:
027: /**
028: * The certificate that caused the exception
029: */
030: private Certificate cert;
031:
032: /**
033: * Indicates a certificate has unrecognized critical extensions.
034: * The value is 1.
035: */
036: public static final byte BAD_EXTENSIONS = 1;
037:
038: /**
039: * Indicates the server certificate chain exceeds the length allowed
040: * by an issuer's policy.
041: * The value is 2.
042: */
043: public static final byte CERTIFICATE_CHAIN_TOO_LONG = 2;
044:
045: /**
046: * Indicates a certificate is expired.
047: * The value is 3.
048: */
049: public static final byte EXPIRED = 3;
050:
051: /**
052: * Indicates an intermediate certificate in the chain does not have the
053: * authority to be a intermediate CA. The value is 4.
054: */
055: public static final byte UNAUTHORIZED_INTERMEDIATE_CA = 4;
056:
057: /**
058: * Indicates a certificate object does not contain a signature.
059: * The value is 5.
060: */
061: public static final byte MISSING_SIGNATURE = 5;
062:
063: /**
064: * Indicates a certificate is not yet valid.
065: * The value is 6.
066: */
067: public static final byte NOT_YET_VALID = 6;
068:
069: /**
070: * Indicates a certificate does not contain the correct site name.
071: * The value is 7.
072: */
073: public static final byte SITENAME_MISMATCH = 7;
074:
075: /**
076: * Indicates a certificate was issued by an unrecognized entity.
077: * The value is 8.
078: */
079: public static final byte UNRECOGNIZED_ISSUER = 8;
080:
081: /**
082: * Indicates a certificate was signed using an unsupported algorithm.
083: * The value is 9.
084: */
085: public static final byte UNSUPPORTED_SIGALG = 9;
086:
087: /**
088: * Indicates a certificate public key has been used in way deemed
089: * inappropriate by the issuer. The value is 10.
090: */
091: public static final byte INAPPROPRIATE_KEY_USAGE = 10;
092:
093: /**
094: * Indicates a certificate in a chain was not issued by the next
095: * authority in the chain. The value is 11.
096: */
097: public static final byte BROKEN_CHAIN = 11;
098:
099: /**
100: * Indicates the root CA's public key is expired. The value is 12.
101: */
102: public static final byte ROOT_CA_EXPIRED = 12;
103:
104: /**
105: * Indicates that type of the public key in a certificate is not
106: * supported by the device. The value is 13.
107: */
108: public static final byte UNSUPPORTED_PUBLIC_KEY_TYPE = 13;
109:
110: /**
111: * Indicates a certificate failed verification.
112: * The value is 14.
113: */
114: public static final byte VERIFICATION_FAILED = 14;
115:
116: /**
117: * Create a new exception with a <CODE>Certificate</CODE>
118: * and specific error reason. The descriptive message for the new exception
119: * will be automatically provided, based on the reason.
120: * @param certificate the certificate that caused the exception
121: * @param status the reason for the exception;
122: * the status MUST be between BAD_EXTENSIONS and VERIFICATION_FAILED
123: * inclusive.
124: */
125: public CertificateException(Certificate certificate, byte status) {
126: super (getMessageForReason(status));
127: cert = certificate;
128: reason = status;
129: }
130:
131: /**
132: * Create a new exception with a message, <CODE>Certificate</CODE>,
133: * and specific error reason.
134: * @param message a descriptive message
135: * @param certificate the certificate that caused the exception
136: * @param status the reason for the exception;
137: * the status MUST be between BAD_EXTENSIONS and VERIFICATION_FAILED
138: * inclusive.
139: */
140: public CertificateException(String message,
141: Certificate certificate, byte status) {
142: super (message);
143: cert = certificate;
144: reason = status;
145: }
146:
147: /**
148: * Get the <CODE>Certificate</CODE> that caused the exception.
149: * @return the <CODE>Certificate</CODE> that included the failure.
150: */
151: public Certificate getCertificate() {
152: return cert;
153: }
154:
155: /**
156: * Get the reason code.
157: * @return the reason code
158: */
159: public byte getReason() {
160: return reason;
161: }
162:
163: // package private methods //
164:
165: /**
166: * Gets the exception message for a reason.
167: *
168: * @param reason reason code
169: *
170: * @return exception message
171: */
172: static String getMessageForReason(int reason) {
173: switch (reason) {
174: case BAD_EXTENSIONS:
175: return "Certificate has unrecognized critical extensions";
176:
177: case CERTIFICATE_CHAIN_TOO_LONG:
178: return "Server certificate chain exceeds the length allowed "
179: + "by an issuer's policy";
180:
181: case EXPIRED:
182: return "Certificate is expired";
183:
184: case UNAUTHORIZED_INTERMEDIATE_CA:
185: return "Intermediate certificate in the chain does not have the "
186: + "authority to be an intermediate CA";
187:
188: case MISSING_SIGNATURE:
189: return "Certificate object does not contain a signature";
190:
191: case NOT_YET_VALID:
192: return "Certificate is not yet valid";
193:
194: case SITENAME_MISMATCH:
195: return "Certificate does not contain the correct site name";
196:
197: case UNRECOGNIZED_ISSUER:
198: return "Certificate was issued by an unrecognized entity";
199:
200: case UNSUPPORTED_SIGALG:
201: return "Certificate was signed using an unsupported algorithm";
202:
203: case INAPPROPRIATE_KEY_USAGE:
204: return "Certificate's public key has been used in a way deemed "
205: + "inappropriate by the issuer";
206:
207: case BROKEN_CHAIN:
208: return "Certificate in a chain was not issued by the next "
209: + "authority in the chain";
210:
211: case ROOT_CA_EXPIRED:
212: return "Root CA's public key is expired";
213:
214: case UNSUPPORTED_PUBLIC_KEY_TYPE:
215: return "Certificate has a public key that is not a "
216: + "supported type";
217:
218: case VERIFICATION_FAILED:
219: return "Certificate failed verification";
220: }
221:
222: return "Unknown reason (" + reason + ")";
223: }
224: }
|