KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > protocol > cmp > CmpErrorResponseMessage


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 package org.ejbca.core.protocol.cmp;
14
15 import java.io.IOException JavaDoc;
16 import java.security.InvalidKeyException JavaDoc;
17 import java.security.NoSuchAlgorithmException JavaDoc;
18 import java.security.NoSuchProviderException JavaDoc;
19 import java.security.PrivateKey JavaDoc;
20 import java.security.cert.CRL JavaDoc;
21 import java.security.cert.Certificate JavaDoc;
22 import java.security.cert.CertificateEncodingException JavaDoc;
23 import java.security.cert.X509Certificate JavaDoc;
24
25 import org.apache.log4j.Logger;
26 import org.bouncycastle.asn1.DERInteger;
27 import org.bouncycastle.asn1.DERUTF8String;
28 import org.bouncycastle.asn1.x509.X509Name;
29 import org.ejbca.core.model.ca.SignRequestException;
30 import org.ejbca.core.model.ra.NotFoundException;
31 import org.ejbca.core.protocol.FailInfo;
32 import org.ejbca.core.protocol.IRequestMessage;
33 import org.ejbca.core.protocol.IResponseMessage;
34 import org.ejbca.core.protocol.ResponseStatus;
35
36 import com.novosec.pkix.asn1.cmp.ErrorMsgContent;
37 import com.novosec.pkix.asn1.cmp.PKIBody;
38 import com.novosec.pkix.asn1.cmp.PKIFreeText;
39 import com.novosec.pkix.asn1.cmp.PKIHeader;
40 import com.novosec.pkix.asn1.cmp.PKIMessage;
41 import com.novosec.pkix.asn1.cmp.PKIStatusInfo;
42
43
44 /**
45  * A very simple error message, no protection
46  * @author tomas
47  * @version $Id: CmpErrorResponseMessage.java,v 1.3 2006/11/09 11:03:14 anatom Exp $
48  */

49 public class CmpErrorResponseMessage extends BaseCmpMessage implements IResponseMessage {
50
51     private static Logger log = Logger.getLogger(CrmfMessageHandler.class);
52     /**
53      * Determines if a de-serialized file is compatible with this class.
54      *
55      * Maintainers must change this value if and only if the new version
56      * of this class is not compatible with old versions. See Sun docs
57      * for <a HREF=http://java.sun.com/products/jdk/1.1/docs/guide
58      * /serialization/spec/version.doc.html> details. </a>
59      *
60      */

61     static final long serialVersionUID = 10002L;
62
63     /** The encoded response message */
64     private byte[] responseMessage = null;
65     private String JavaDoc failText = null;
66     private FailInfo failInfo = null;
67     private ResponseStatus status = null;
68     private int requestId = 0;
69     private int requestType = 23; // 23 is general error message
70

71     public void setCertificate(Certificate JavaDoc cert) {
72     }
73
74     public void setCrl(CRL JavaDoc crl) {
75     }
76
77     public void setIncludeCACert(boolean incCACert) {
78     }
79
80     public byte[] getResponseMessage() throws IOException JavaDoc,
81             CertificateEncodingException JavaDoc {
82         return responseMessage;
83     }
84
85     public void setStatus(ResponseStatus status) {
86         this.status = status;
87     }
88
89     public ResponseStatus getStatus() {
90         return status;
91     }
92
93     public void setFailInfo(FailInfo failInfo) {
94         this.failInfo = failInfo;
95     }
96
97     public FailInfo getFailInfo() {
98         return failInfo;
99     }
100
101     public void setFailText(String JavaDoc failText) {
102         this.failText = failText;
103     }
104
105     public String JavaDoc getFailText() {
106         return failText;
107     }
108
109     public boolean create() throws IOException JavaDoc, InvalidKeyException JavaDoc,
110             NoSuchAlgorithmException JavaDoc, NoSuchProviderException JavaDoc,
111             SignRequestException, NotFoundException {
112         X509Name sender = X509Name.getInstance(getSender().getName());
113         X509Name recipient = X509Name.getInstance(getRecipient().getName());
114         PKIHeader myPKIHeader = CmpMessageHelper.createPKIHeader(sender, recipient, getSenderNonce(), getRecipientNonce(), getTransactionId());
115         PKIStatusInfo myPKIStatusInfo = new PKIStatusInfo(new DERInteger(2)); // 2 = rejection
116
if (failInfo != null) {
117             myPKIStatusInfo.setFailInfo(failInfo.getAsBitString());
118         }
119         if (failText != null) {
120             myPKIStatusInfo.setStatusString(new PKIFreeText(new DERUTF8String(failText)));
121         }
122         PKIBody myPKIBody = null;
123         log.debug("Create error message from requestType: "+requestType);
124         if (requestType==0 || requestType==2) {
125             myPKIBody = CmpMessageHelper.createCertRequestRejectBody(myPKIHeader, myPKIStatusInfo, requestId, requestType);
126         } else {
127             ErrorMsgContent myErrorContent = new ErrorMsgContent(myPKIStatusInfo);
128             myPKIBody = new PKIBody(myErrorContent, 23); // 23 = error
129
}
130         PKIMessage myPKIMessage = new PKIMessage(myPKIHeader, myPKIBody);
131         if ((getPbeDigestAlg() != null) && (getPbeMacAlg() != null) && (getPbeKeyId() != null) && (getPbeKey() != null) ) {
132             responseMessage = CmpMessageHelper.protectPKIMessageWithPBE(myPKIMessage, getPbeKeyId(), getPbeKey(), getPbeDigestAlg(), getPbeMacAlg(), getPbeIterationCount());
133         } else {
134             responseMessage = CmpMessageHelper.pkiMessageToByteArray(myPKIMessage);
135         }
136         return true;
137     }
138
139     public boolean requireSignKeyInfo() {
140         return false;
141     }
142
143     public boolean requireEncKeyInfo() {
144         return false;
145     }
146
147     public void setSignKeyInfo(X509Certificate JavaDoc cert, PrivateKey JavaDoc key,
148             String JavaDoc provider) {
149     }
150
151     public void setEncKeyInfo(X509Certificate JavaDoc cert, PrivateKey JavaDoc key,
152             String JavaDoc provider) {
153     }
154
155     public void setSenderNonce(String JavaDoc senderNonce) {
156         super.setSenderNonce(senderNonce);
157     }
158
159     public void setRecipientNonce(String JavaDoc recipientNonce) {
160         super.setRecipientNonce(recipientNonce);
161     }
162
163     public void setTransactionId(String JavaDoc transactionId) {
164         super.setTransactionId(transactionId);
165     }
166
167     public void setRecipientKeyInfo(byte[] recipientKeyInfo) {
168     }
169
170     public void setPreferredDigestAlg(String JavaDoc digest) {
171     }
172
173     public void setRequestType(int reqtype) {
174         this.requestType = reqtype;
175     }
176
177     public void setRequestId(int reqid) {
178         this.requestId = reqid;
179     }
180
181     /** @see org.ejca.core.protocol.IResponseMessage
182      */

183     public void setProtectionParamsFromRequest(IRequestMessage reqMsg) {
184     }
185 }
186
Popular Tags