KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.security.PrivateKey JavaDoc;
17 import java.security.cert.Certificate JavaDoc;
18 import java.security.cert.X509Certificate JavaDoc;
19 import java.util.Random JavaDoc;
20
21 import org.apache.log4j.Logger;
22 import org.ejbca.util.Base64;
23
24
25
26 /**
27  * Utility class to gather a few functions
28  *
29  * @version $Id: RequestMessageUtils.java,v 1.1.2.2 2007/03/28 12:26:54 anatom Exp $
30  */

31 public class RequestMessageUtils {
32     /**
33      * Determines if a de-serialized file is compatible with this class.
34      *
35      * Maintainers must change this value if and only if the new version
36      * of this class is not compatible with old versions. See Sun docs
37      * for <a HREF=http://java.sun.com/products/jdk/1.1/docs/guide
38      * /serialization/spec/version.doc.html> details. </a>
39      *
40      */

41     static final long serialVersionUID = 3597275157018205138L;
42
43     private static final Logger log = Logger.getLogger(RequestMessageUtils.class);
44
45     public static IResponseMessage createResponseMessage(Class JavaDoc responseClass, IRequestMessage req, Certificate JavaDoc cert, PrivateKey JavaDoc signPriv, PrivateKey JavaDoc encPriv, String JavaDoc provider){
46         IResponseMessage ret = null;
47         // Create the response message and set all required fields
48
try {
49             ret = (IResponseMessage) responseClass.newInstance();
50         } catch (InstantiationException JavaDoc e) {
51             //TODO : do something with these exceptions
52
log.error("Error creating response message", e);
53             return null;
54         } catch (IllegalAccessException JavaDoc e) {
55             log.error("Error creating response message", e);
56             return null;
57         }
58         if (ret.requireSignKeyInfo()) {
59             ret.setSignKeyInfo((X509Certificate JavaDoc) cert, signPriv, provider);
60         }
61         if (ret.requireEncKeyInfo()) {
62             ret.setEncKeyInfo((X509Certificate JavaDoc) cert, encPriv, provider);
63         }
64         if (req.getSenderNonce() != null) {
65             ret.setRecipientNonce(req.getSenderNonce());
66         }
67         if (req.getTransactionId() != null) {
68             ret.setTransactionId(req.getTransactionId());
69         }
70         // Sendernonce is a random number
71
byte[] senderNonce = new byte[16];
72         Random JavaDoc randomSource = new Random JavaDoc();
73         randomSource.nextBytes(senderNonce);
74         ret.setSenderNonce(new String JavaDoc(Base64.encode(senderNonce)));
75         // If we have a specified request key info, use it in the reply
76
if (req.getRequestKeyInfo() != null) {
77             ret.setRecipientKeyInfo(req.getRequestKeyInfo());
78         }
79         // Which digest algorithm to use to create the response, if applicable
80
ret.setPreferredDigestAlg(req.getPreferredDigestAlg());
81         // Include the CA cert or not in the response, if applicable for the response type
82
ret.setIncludeCACert(req.includeCACert());
83         // Hint to the response which request type it is in response to
84
ret.setRequestType(req.getRequestType());
85         ret.setRequestId(req.getRequestId());
86         // If there is some protection parameters we need to lift over from the request message, the request and response knows about it
87
ret.setProtectionParamsFromRequest(req);
88         return ret;
89     }
90
91
92 }
93
Popular Tags