KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > protocol > X509ResponseMessage


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.core.protocol;
15
16
17 import java.io.IOException JavaDoc;
18 import java.security.InvalidKeyException JavaDoc;
19 import java.security.NoSuchAlgorithmException JavaDoc;
20 import java.security.NoSuchProviderException JavaDoc;
21 import java.security.PrivateKey JavaDoc;
22 import java.security.cert.CRL JavaDoc;
23 import java.security.cert.Certificate JavaDoc;
24 import java.security.cert.CertificateEncodingException JavaDoc;
25 import java.security.cert.CertificateException JavaDoc;
26 import java.security.cert.X509Certificate JavaDoc;
27
28 import org.apache.log4j.Logger;
29 import org.ejbca.core.model.ca.SignRequestException;
30 import org.ejbca.core.model.ra.NotFoundException;
31 import org.ejbca.util.CertTools;
32
33
34 /**
35  * A response message consisting of a single X509 Certificate.
36  *
37  * @version $Id: X509ResponseMessage.java,v 1.5 2006/10/22 09:05:05 anatom Exp $
38  */

39 public class X509ResponseMessage implements IResponseMessage {
40     /**
41      * Determines if a de-serialized file is compatible with this class.
42      *
43      * Maintainers must change this value if and only if the new version
44      * of this class is not compatible with old versions. See Sun docs
45      * for <a HREF=http://java.sun.com/products/jdk/1.1/docs/guide
46      * /serialization/spec/version.doc.html> details. </a>
47      *
48      */

49     static final long serialVersionUID = -2157072605987735912L;
50
51     private static Logger log = Logger.getLogger(X509ResponseMessage.class);
52
53     /** Certificate to be in response message, */
54     private Certificate JavaDoc cert = null;
55
56     /** status for the response */
57     private ResponseStatus status = ResponseStatus.SUCCESS;
58
59     /** Possible fail information in the response. Defaults to null. */
60     private FailInfo failInfo = null;
61
62     /** Possible clear text error information in the response. Defaults to null. */
63     private String JavaDoc failText = null;
64
65     /**
66      * Sets the complete certificate in the response message.
67      *
68      * @param cert certificate in the response message.
69      */

70     public void setCertificate(Certificate JavaDoc cert) {
71         this.cert = cert;
72     }
73
74     /**
75      * Sets the CRL (if present) in the response message.
76      *
77      * @param crl crl in the response message.
78      */

79     public void setCrl(CRL JavaDoc crl) {
80         // This message type does not contain a CRL
81
}
82
83     /** @see org.ejbca.core.protocol.IResponseMessage#setIncludeCACert
84      *
85      */

86     public void setIncludeCACert(boolean incCACert) {
87         // Do nothing, not applicable
88
}
89
90     /**
91      * Gets the complete certificate in the response message.
92      *
93      * @return certificate in the response message.
94      */

95     public Certificate JavaDoc getCertificate() throws CertificateEncodingException JavaDoc, CertificateException JavaDoc, IOException JavaDoc {
96         return CertTools.getCertfromByteArray(getResponseMessage());
97     }
98
99     /**
100      * Gets the response message in the default encoding format.
101      *
102      * @return the response message in the default encoding format.
103      */

104     public byte[] getResponseMessage() throws IOException JavaDoc, CertificateEncodingException JavaDoc {
105         return cert.getEncoded();
106     }
107
108     /**
109      * Sets the status of the response message.
110      *
111      * @param status status of the response.
112      */

113     public void setStatus(ResponseStatus status) {
114         this.status = status;
115     }
116
117     /**
118      * Gets the status of the response message.
119      *
120      * @return status status of the response.
121      */

122     public ResponseStatus getStatus() {
123         return status;
124     }
125
126     /**
127      * Sets info about reason for failure.
128      *
129      * @param failInfo reason for failure.
130      */

131     public void setFailInfo(FailInfo failInfo) {
132         this.failInfo = failInfo;
133     }
134
135     /**
136      * Gets info about reason for failure.
137      *
138      * @return failInfo reason for failure.
139      */

140     public FailInfo getFailInfo() {
141         return failInfo;
142     }
143
144     public void setFailText(String JavaDoc failText) {
145         this.failText = failText;
146     }
147
148     public String JavaDoc getFailText() {
149         return this.failText;
150     }
151
152     /**
153      * Create encrypts and creates signatures as needed to produce a complete response message. If
154      * needed setSignKeyInfo and setEncKeyInfo must be called before this method. After this is
155      * called the response message can be retrieved with getResponseMessage();
156      *
157      * @return True if signature/encryption was successful, false if it failed, request should not
158      * be sent back i failed.
159      *
160      * @throws IOException If input/output or encoding failed.
161      * @throws InvalidKeyException If the key used for signing/encryption is invalid.
162      * @throws NoSuchProviderException if there is an error with the Provider.
163      * @throws NoSuchAlgorithmException if the signature on the request is done with an unhandled
164      * algorithm.
165      *
166      * @see #setSignKeyInfo()
167      * @see #setEncKeyInfo()
168      */

169     public boolean create()
170             throws IOException JavaDoc, InvalidKeyException JavaDoc, NoSuchAlgorithmException JavaDoc, NoSuchProviderException JavaDoc, SignRequestException, NotFoundException {
171
172         if (status.equals(ResponseStatus.SUCCESS)) {
173             log.debug("Creating a STATUS_OK message.");
174         } else {
175             if (status.equals(ResponseStatus.FAILURE)) {
176                 log.debug("Creating a STATUS_FAILED message (or throwing an exception).");
177                 if (failInfo.equals(FailInfo.WRONG_AUTHORITY)) {
178                     throw new SignRequestException(failText);
179                 }
180                 if (failInfo.equals(FailInfo.INCORRECT_DATA)) {
181                     throw new NotFoundException(failText);
182                 }
183
184             } else {
185                 log.debug("Creating a STATUS_PENDING message.");
186             }
187         }
188         return true;
189     }
190
191     /**
192      * indicates if this message needs recipients public and private key to sign. If this returns
193      * true, setSignKeyInfo() should be called.
194      *
195      * @return True if public and private key is needed.
196      */

197     public boolean requireSignKeyInfo() {
198         return false;
199     }
200
201     /**
202      * indicates if this message needs recipients public and private key to encrypt. If this
203      * returns true, setEncKeyInfo() should be called.
204      *
205      * @return True if public and private key is needed.
206      */

207     public boolean requireEncKeyInfo() {
208         return false;
209     }
210
211     /**
212      * Sets the public and private key needed to sign the message. Must be set if
213      * requireSignKeyInfo() returns true.
214      *
215      * @param cert certificate containing the public key.
216      * @param key private key.
217      * @param provider the provider to use, if the private key is on a HSM you must use a special provider. If null is given, the default BC provider is used.
218      *
219      * @see #requireSignKeyInfo()
220      */

221     public void setSignKeyInfo(X509Certificate JavaDoc cert, PrivateKey JavaDoc key, String JavaDoc provider) {
222     }
223
224     /**
225      * Sets the public and private key needed to encrypt the message. Must be set if
226      * requireEncKeyInfo() returns true.
227      *
228      * @param cert certificate containing the public key.
229      * @param key private key.
230      * @param provider the provider to use, if the private key is on a HSM you must use a special provider. If null is given, the default BC provider is used.
231      *
232      * @see #requireEncKeyInfo()
233      */

234     public void setEncKeyInfo(X509Certificate JavaDoc cert, PrivateKey JavaDoc key, String JavaDoc provider) {
235     }
236
237     /**
238      * Sets a senderNonce if it should be present in the response
239      *
240      * @param senderNonce a string of base64 encoded bytes
241      */

242     public void setSenderNonce(String JavaDoc senderNonce) {
243     }
244
245     /**
246      * Sets a recipient if it should be present in the response
247      *
248      * @param recipientNonce a string of base64 encoded bytes
249      */

250     public void setRecipientNonce(String JavaDoc recipientNonce) {
251     }
252
253     /**
254      * Sets a transaction identifier if it should be present in the response
255      *
256      * @param transactionId transaction id
257      */

258     public void setTransactionId(String JavaDoc transactionId) {
259     }
260
261     /**
262      * Sets recipient key info, key id or similar. This is usually the request key info from the
263      * request message.
264      *
265      * @param recipientKeyInfo key info
266      */

267     public void setRecipientKeyInfo(byte[] recipientKeyInfo) {
268     }
269     
270     /** @see org.ejca.core.protocol.IResponseMessage
271      */

272     public void setPreferredDigestAlg(String JavaDoc digest) {
273     }
274
275     /** @see org.ejca.core.protocol.IResponseMessage
276      */

277     public void setRequestType(int reqtype) {
278     }
279
280     /** @see org.ejca.core.protocol.IResponseMessage
281      */

282     public void setRequestId(int reqid) {
283     }
284
285     /** @see org.ejca.core.protocol.IResponseMessage
286      */

287     public void setProtectionParamsFromRequest(IRequestMessage reqMsg) {
288     }
289 }
290
Popular Tags