KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > ca > caadmin > CA


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.model.ca.caadmin;
15
16 import java.io.Serializable JavaDoc;
17 import java.security.KeyPair JavaDoc;
18 import java.security.PublicKey JavaDoc;
19 import java.security.cert.CRL JavaDoc;
20 import java.security.cert.Certificate JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import javax.ejb.EJBException JavaDoc;
29
30 import org.apache.log4j.Logger;
31 import org.bouncycastle.cms.CMSException;
32 import org.ejbca.core.model.UpgradeableDataHashMap;
33 import org.ejbca.core.model.ca.SignRequestSignatureException;
34 import org.ejbca.core.model.ca.caadmin.extendedcaservices.CmsCAService;
35 import org.ejbca.core.model.ca.caadmin.extendedcaservices.CmsCAServiceInfo;
36 import org.ejbca.core.model.ca.caadmin.extendedcaservices.CmsCAServiceRequest;
37 import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAService;
38 import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceInfo;
39 import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceNotActiveException;
40 import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceRequest;
41 import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceRequestException;
42 import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceResponse;
43 import org.ejbca.core.model.ca.caadmin.extendedcaservices.IllegalExtendedCAServiceRequestException;
44 import org.ejbca.core.model.ca.caadmin.extendedcaservices.KeyRecoveryCAServiceRequest;
45 import org.ejbca.core.model.ca.caadmin.extendedcaservices.KeyRecoveryCAServiceResponse;
46 import org.ejbca.core.model.ca.caadmin.extendedcaservices.OCSPCAService;
47 import org.ejbca.core.model.ca.caadmin.extendedcaservices.OCSPCAServiceInfo;
48 import org.ejbca.core.model.ca.caadmin.extendedcaservices.OCSPCAServiceRequest;
49 import org.ejbca.core.model.ca.caadmin.extendedcaservices.XKMSCAService;
50 import org.ejbca.core.model.ca.caadmin.extendedcaservices.XKMSCAServiceInfo;
51 import org.ejbca.core.model.ca.caadmin.extendedcaservices.XKMSCAServiceRequest;
52 import org.ejbca.core.model.ca.catoken.CAToken;
53 import org.ejbca.core.model.ca.catoken.CATokenInfo;
54 import org.ejbca.core.model.ca.catoken.HardCATokenContainer;
55 import org.ejbca.core.model.ca.catoken.HardCATokenManager;
56 import org.ejbca.core.model.ca.catoken.NullCAToken;
57 import org.ejbca.core.model.ca.catoken.SoftCAToken;
58 import org.ejbca.core.model.ca.certificateprofiles.CertificateProfile;
59 import org.ejbca.core.model.ra.UserDataVO;
60 import org.ejbca.util.Base64;
61 import org.ejbca.util.CertTools;
62
63
64
65
66 /**
67  * CA is a base class that should be inherited by all CA types
68  *
69  * @version $Id: CA.java,v 1.17 2007/01/16 11:43:26 anatom Exp $
70  */

71 public abstract class CA extends UpgradeableDataHashMap implements Serializable JavaDoc {
72
73     /** Log4j instance */
74     private static Logger log = Logger.getLogger(CA.class);
75
76     public static final String JavaDoc TRUE = "true";
77     public static final String JavaDoc FALSE = "false";
78     
79     // protected fields.
80
public static final String JavaDoc CATYPE = "catype";
81     protected static final String JavaDoc SUBJECTDN = "subjectdn";
82     protected static final String JavaDoc CAID = "caid";
83     protected static final String JavaDoc NAME = "name";
84     protected static final String JavaDoc STATUS = "status";
85     protected static final String JavaDoc VALIDITY = "validity";
86     protected static final String JavaDoc EXPIRETIME = "expiretime";
87     protected static final String JavaDoc CERTIFICATECHAIN = "certificatechain";
88     protected static final String JavaDoc CATOKENDATA = "catoken";
89     protected static final String JavaDoc SIGNEDBY = "signedby";
90     protected static final String JavaDoc DESCRIPTION = "description";
91     protected static final String JavaDoc REVOKATIONREASON = "revokationreason";
92     protected static final String JavaDoc REVOKATIONDATE = "revokationdate";
93     protected static final String JavaDoc CERTIFICATEPROFILEID = "certificateprofileid";
94     protected static final String JavaDoc CRLPERIOD = "crlperiod";
95     protected static final String JavaDoc CRLISSUEINTERVAL = "crlIssueInterval";
96     protected static final String JavaDoc CRLOVERLAPTIME = "crlOverlapTime";
97     protected static final String JavaDoc CRLPUBLISHERS = "crlpublishers";
98     protected static final String JavaDoc FINISHUSER = "finishuser";
99     protected static final String JavaDoc REQUESTCERTCHAIN = "requestcertchain";
100     protected static final String JavaDoc EXTENDEDCASERVICES = "extendedcaservices";
101     protected static final String JavaDoc EXTENDEDCASERVICE = "extendedcaservice";
102     protected static final String JavaDoc APPROVALSETTINGS = "approvalsettings";
103     protected static final String JavaDoc NUMBEROFREQAPPROVALS = "numberofreqapprovals";
104     
105     // Public Methods
106
/** Creates a new instance of CA, this constuctor should be used when a new CA is created */
107     public CA(CAInfo cainfo){
108        data = new HashMap JavaDoc();
109        
110        this.cainfo = cainfo;
111               
112        data.put(VALIDITY, new Integer JavaDoc(cainfo.getValidity()));
113        data.put(SIGNEDBY, new Integer JavaDoc(cainfo.getSignedBy()));
114        data.put(DESCRIPTION, cainfo.getDescription());
115        data.put(REVOKATIONREASON, new Integer JavaDoc(-1));
116        data.put(CERTIFICATEPROFILEID, new Integer JavaDoc(cainfo.getCertificateProfileId()));
117        setCRLPeriod(cainfo.getCRLPeriod());
118        setCRLIssueInterval(cainfo.getCRLIssueInterval());
119        setCRLOverlapTime(cainfo.getCRLOverlapTime());
120        setCRLPublishers(cainfo.getCRLPublishers());
121        setFinishUser(cainfo.getFinishUser());
122        
123        
124        Iterator JavaDoc iter = cainfo.getExtendedCAServiceInfos().iterator();
125        ArrayList JavaDoc extendedservicetypes = new ArrayList JavaDoc();
126        while(iter.hasNext()){
127          ExtendedCAServiceInfo next = (ExtendedCAServiceInfo) iter.next();
128          if(next instanceof OCSPCAServiceInfo){
129            setExtendedCAService(new OCSPCAService(next));
130            extendedservicetypes.add(new Integer JavaDoc(ExtendedCAServiceInfo.TYPE_OCSPEXTENDEDSERVICE));
131          }
132          if(next instanceof XKMSCAServiceInfo){
133            setExtendedCAService(new XKMSCAService(next));
134            extendedservicetypes.add(new Integer JavaDoc(ExtendedCAServiceInfo.TYPE_XKMSEXTENDEDSERVICE));
135          }
136          if(next instanceof CmsCAServiceInfo){
137                setExtendedCAService(new CmsCAService(next));
138                extendedservicetypes.add(new Integer JavaDoc(ExtendedCAServiceInfo.TYPE_CMSEXTENDEDSERVICE));
139              }
140        }
141        data.put(EXTENDEDCASERVICES, extendedservicetypes);
142        setApprovalSettings(cainfo.getApprovalSettings());
143        setNumOfRequiredApprovals(cainfo.getNumOfReqApprovals());
144     }
145     
146     /** Constructor used when retrieving existing CA from database. */
147     public CA(HashMap JavaDoc data){
148       loadData(data);
149       extendedcaservicemap = new HashMap JavaDoc();
150     }
151     public void setCAInfo(CAInfo cainfo) {
152         this.cainfo = cainfo;
153     }
154     public CAInfo getCAInfo() {
155         return this.cainfo;
156     }
157
158     // Public Methods.
159
public String JavaDoc getSubjectDN(){
160         return cainfo.getSubjectDN();
161     }
162     public void setSubjectDN(String JavaDoc subjectdn){
163         cainfo.subjectdn = subjectdn;
164     }
165     
166     public int getCAId(){
167         return cainfo.getCAId();
168     }
169     public void setCAId(int caid){
170         cainfo.caid = caid;
171     }
172     
173     public String JavaDoc getName(){
174         return cainfo.getName();
175     }
176     public void setName(String JavaDoc caname){
177         cainfo.name = caname;
178     }
179     
180     public int getStatus(){
181         return cainfo.getStatus();
182     }
183     public void setStatus(int status){
184         cainfo.status = status;
185     }
186     
187     public int getCAType(){ return ((Integer JavaDoc)data.get(CATYPE)).intValue();}
188     
189     public int getValidity(){ return ((Integer JavaDoc) data.get(VALIDITY)).intValue();}
190     public void setValidity(int validity){ data.put(VALIDITY, new Integer JavaDoc(validity));}
191     
192     public Date JavaDoc getExpireTime(){return ((Date JavaDoc)data.get(EXPIRETIME));}
193     public void setExpireTime(Date JavaDoc expiretime) { data.put(EXPIRETIME,expiretime);}
194    
195     public int getSignedBy(){ return ((Integer JavaDoc) data.get(SIGNEDBY)).intValue();}
196     
197     public String JavaDoc getDescription(){return ((String JavaDoc)data.get(DESCRIPTION));}
198     public void setDescription(String JavaDoc description) { data.put(DESCRIPTION,description);}
199     
200     public int getRevokationReason(){return ((Integer JavaDoc) data.get(REVOKATIONREASON)).intValue();}
201     public void setRevokationReason(int reason){ data.put(REVOKATIONREASON,new Integer JavaDoc(reason));}
202         
203     public Date JavaDoc getRevokationDate(){return (Date JavaDoc) data.get(REVOKATIONDATE);}
204     public void setRevokationDate(Date JavaDoc date){ data.put(REVOKATIONDATE,date);}
205                 
206     public int getCRLPeriod(){return ((Integer JavaDoc)data.get(CRLPERIOD)).intValue();}
207     public void setCRLPeriod(int crlperiod) {data.put(CRLPERIOD, new Integer JavaDoc(crlperiod));}
208     
209     public int getCRLIssueInterval(){return ((Integer JavaDoc)data.get(CRLISSUEINTERVAL)).intValue();}
210     public void setCRLIssueInterval(int crlIssueInterval) {data.put(CRLISSUEINTERVAL, new Integer JavaDoc(crlIssueInterval));}
211     
212     public int getCRLOverlapTime(){return ((Integer JavaDoc)data.get(CRLOVERLAPTIME)).intValue();}
213     public void setCRLOverlapTime(int crlOverlapTime) {data.put(CRLOVERLAPTIME, new Integer JavaDoc(crlOverlapTime));}
214
215     public Collection JavaDoc getCRLPublishers(){return ((Collection JavaDoc)data.get(CRLPUBLISHERS));}
216     public void setCRLPublishers(Collection JavaDoc crlpublishers) {data.put(CRLPUBLISHERS, crlpublishers);}
217     
218     
219     public int getCertificateProfileId() {return ((Integer JavaDoc) data.get(CERTIFICATEPROFILEID)).intValue();}
220     
221     /** Returns the CAs token. The token is fetched from the token registry, or created and added to the token registry.
222      *
223      * @return The CAs token, be it soft or hard.
224      * @throws IllegalKeyStoreException If the token keystore is invalid (crypto error thrown by crypto provider), or the CA token type is undefined.
225      */

226     public CAToken getCAToken(int caid) throws IllegalKeyStoreException {
227         CAToken ret = HardCATokenManager.instance().getCAToken(caid);
228         if (ret == null) {
229             Integer JavaDoc tokentype = (Integer JavaDoc) ((HashMap JavaDoc)data.get(CATOKENDATA)).get(CAToken.CATOKENTYPE);
230             switch(tokentype.intValue()) {
231             case CATokenInfo.CATOKENTYPE_P12:
232                 ret = new SoftCAToken((HashMap JavaDoc)data.get(CATOKENDATA));
233                 break;
234             case CATokenInfo.CATOKENTYPE_HSM:
235                 ret = new HardCATokenContainer((HashMap JavaDoc)data.get(CATOKENDATA));
236                 break;
237             case CATokenInfo.CATOKENTYPE_NULL:
238                 ret = new NullCAToken();
239                 break;
240             default:
241                 throw new IllegalKeyStoreException("No CA Token type defined: "+tokentype.intValue());
242             }
243             HardCATokenManager.instance().addCAToken(caid, ret);
244         }
245       return ret;
246     }
247     /** Returns the CAs token. The token is fetched from the token registry, or created and added to the token registry.
248      *
249      * @return The CAs token, be it soft or hard.
250      * @throws IllegalKeyStoreException If the token keystore is invalid (crypto error thrown by crypto provider), or the CA token type is undefined.
251      */

252     public CAToken getCAToken() throws IllegalKeyStoreException {
253         return getCAToken(getCAId());
254     }
255         
256     /** Sets the CA token. Adds or updates the token in the token registry.
257      *
258      * @param catoken The CAs token, be it soft or hard.
259      */

260     public void setCAToken(CAToken catoken){
261        data.put(CATOKENDATA, catoken.saveData());
262        HardCATokenManager.instance().addCAToken(getCAId(), catoken);
263     }
264     
265     public Collection JavaDoc getRequestCertificateChain(){
266       if(requestcertchain == null){
267         Collection JavaDoc storechain = (Collection JavaDoc) data.get(REQUESTCERTCHAIN);
268         Iterator JavaDoc iter = storechain.iterator();
269         this.requestcertchain = new ArrayList JavaDoc();
270         while(iter.hasNext()){
271           String JavaDoc b64Cert = (String JavaDoc) iter.next();
272           try{
273             this.requestcertchain.add(CertTools.getCertfromByteArray(Base64.decode(b64Cert.getBytes())));
274           }catch(Exception JavaDoc e){
275              throw new EJBException JavaDoc(e);
276           }
277         }
278       }
279         
280       return requestcertchain;
281     }
282     
283     public void setRequestCertificateChain(Collection JavaDoc requestcertificatechain){
284       Iterator JavaDoc iter = requestcertificatechain.iterator();
285       ArrayList JavaDoc storechain = new ArrayList JavaDoc();
286       while(iter.hasNext()){
287         Certificate JavaDoc cert = (Certificate JavaDoc) iter.next();
288         try{
289           String JavaDoc b64Cert = new String JavaDoc(Base64.encode(cert.getEncoded()));
290           storechain.add(b64Cert);
291         }catch(Exception JavaDoc e){
292           throw new EJBException JavaDoc(e);
293         }
294       }
295       data.put(REQUESTCERTCHAIN,storechain);
296       
297       this.requestcertchain = new ArrayList JavaDoc();
298       this.requestcertchain.addAll(requestcertificatechain);
299     }
300
301     /* Returns a collection of CA-certificates, with this CAs cert i position 0, or null
302      * if no CA-certificates exist.
303      */

304     public Collection JavaDoc getCertificateChain(){
305       if(certificatechain == null){
306         Collection JavaDoc storechain = (Collection JavaDoc) data.get(CERTIFICATECHAIN);
307         if (storechain == null) {
308             return null;
309         }
310         Iterator JavaDoc iter = storechain.iterator();
311         this.certificatechain = new ArrayList JavaDoc();
312         while(iter.hasNext()){
313           String JavaDoc b64Cert = (String JavaDoc) iter.next();
314           try{
315             this.certificatechain.add(CertTools.getCertfromByteArray(Base64.decode(b64Cert.getBytes())));
316           }catch(Exception JavaDoc e){
317              throw new EJBException JavaDoc(e);
318           }
319         }
320       }
321       return certificatechain;
322     }
323     
324     public void setCertificateChain(Collection JavaDoc certificatechain){
325       Iterator JavaDoc iter = certificatechain.iterator();
326       ArrayList JavaDoc storechain = new ArrayList JavaDoc();
327       while(iter.hasNext()){
328         Certificate JavaDoc cert = (Certificate JavaDoc) iter.next();
329         try{
330           String JavaDoc b64Cert = new String JavaDoc(Base64.encode(cert.getEncoded()));
331           storechain.add(b64Cert);
332         }catch(Exception JavaDoc e){
333           throw new EJBException JavaDoc(e);
334         }
335       }
336       data.put(CERTIFICATECHAIN,storechain);
337       
338       this.certificatechain = new ArrayList JavaDoc();
339       this.certificatechain.addAll(certificatechain);
340     }
341
342     
343     /* Returns the CAs certificate, or null if no CA-certificates exist.
344      */

345     public Certificate JavaDoc getCACertificate(){
346        if(certificatechain == null) {
347            getCertificateChain();
348            // if it's still null, return null
349
if (certificatechain == null) {
350                return null;
351            }
352        }
353        return (Certificate JavaDoc) this.certificatechain.get(0);
354     }
355     
356     public boolean getFinishUser(){return ((Boolean JavaDoc)data.get(FINISHUSER)).booleanValue();}
357     
358     public void setFinishUser(boolean finishuser) {data.put(FINISHUSER, new Boolean JavaDoc(finishuser));}
359     
360     /**
361      * Returns a collection of Integers (CAInfo.REQ_APPROVAL_ constants) of which
362      * action that requires approvals, default none
363      *
364      * Never null
365      * @return
366      */

367     public Collection JavaDoc getApprovalSettings(){
368         if(data.get(APPROVALSETTINGS) == null){
369             return new ArrayList JavaDoc();
370         }
371         
372         return (Collection JavaDoc) data.get(APPROVALSETTINGS);
373     }
374     
375     /**
376      * Collection of Integers (CAInfo.REQ_APPROVAL_ constants) of which
377      * action that requires approvals
378      */

379     public void setApprovalSettings(Collection JavaDoc approvalSettings){
380        data.put(APPROVALSETTINGS,approvalSettings);
381     }
382     
383     /**
384      * Returns the number of different administrators that needs to approve
385      * an action, default 1.
386      */

387     public int getNumOfRequiredApprovals(){
388         if(data.get(NUMBEROFREQAPPROVALS) == null){
389             return 1;
390         }
391         return ((Integer JavaDoc) data.get(NUMBEROFREQAPPROVALS)).intValue();
392     }
393     
394     /**
395      * The number of different administrators that needs to approve
396      */

397     public void setNumOfRequiredApprovals(int numOfReqApprovals){
398         data.put(NUMBEROFREQAPPROVALS,new Integer JavaDoc(numOfReqApprovals));
399     }
400     
401     public void updateCA(CAInfo cainfo) throws Exception JavaDoc{
402         data.put(VALIDITY, new Integer JavaDoc(cainfo.getValidity()));
403         data.put(DESCRIPTION, cainfo.getDescription());
404         data.put(CRLPERIOD, new Integer JavaDoc(cainfo.getCRLPeriod()));
405         data.put(CRLISSUEINTERVAL, new Integer JavaDoc(cainfo.getCRLIssueInterval()));
406         data.put(CRLOVERLAPTIME, new Integer JavaDoc(cainfo.getCRLOverlapTime()));
407         data.put(CRLPUBLISHERS, cainfo.getCRLPublishers());
408         data.put(APPROVALSETTINGS,cainfo.getApprovalSettings());
409         data.put(NUMBEROFREQAPPROVALS,new Integer JavaDoc(cainfo.getNumOfReqApprovals()));
410         CAToken token = getCAToken();
411         if (token != null) {
412             token.updateCATokenInfo(cainfo.getCATokenInfo());
413             setCAToken(token);
414         }
415         setFinishUser(cainfo.getFinishUser());
416         
417         Iterator JavaDoc iter = cainfo.getExtendedCAServiceInfos().iterator();
418         while(iter.hasNext()){
419             ExtendedCAServiceInfo info = (ExtendedCAServiceInfo) iter.next();
420             if(info instanceof OCSPCAServiceInfo){
421                 this.getExtendedCAService(ExtendedCAServiceInfo.TYPE_OCSPEXTENDEDSERVICE).update(info, this);
422             }
423             if(info instanceof XKMSCAServiceInfo){
424                 this.getExtendedCAService(ExtendedCAServiceInfo.TYPE_XKMSEXTENDEDSERVICE).update(info, this);
425             }
426             if(info instanceof CmsCAServiceInfo){
427                 this.getExtendedCAService(ExtendedCAServiceInfo.TYPE_CMSEXTENDEDSERVICE).update(info, this);
428             }
429         }
430         this.cainfo = cainfo;
431     }
432     
433     
434     public Certificate JavaDoc generateCertificate(UserDataVO subject,
435             PublicKey JavaDoc publicKey,
436             int keyusage,
437             long validity,
438             CertificateProfile certProfile) throws Exception JavaDoc {
439         // Calculate the notAfter date
440
Date JavaDoc notAfter = null;
441         if(validity != -1) {
442             notAfter = new Date JavaDoc();
443             notAfter.setTime(notAfter.getTime() + ( validity * 24 * 60 * 60 * 1000));
444         }
445         Date JavaDoc notBefore = new Date JavaDoc();
446         return generateCertificate(subject, publicKey, keyusage, notBefore, notAfter, certProfile);
447     }
448     
449     public abstract Certificate JavaDoc generateCertificate(UserDataVO subject,
450                                                     PublicKey JavaDoc publicKey,
451                                                     int keyusage,
452                                                     Date JavaDoc notBefore,
453                                                     Date JavaDoc notAfter,
454                                                     CertificateProfile certProfile) throws Exception JavaDoc;
455     
456     public abstract CRL JavaDoc generateCRL(Vector JavaDoc certs, int crlnumber) throws Exception JavaDoc;
457     
458     public abstract byte[] createPKCS7(Certificate JavaDoc cert, boolean includeChain) throws SignRequestSignatureException;
459   
460         
461     public abstract byte[] encryptKeys(KeyPair JavaDoc keypair) throws Exception JavaDoc;
462     
463     public abstract KeyPair JavaDoc decryptKeys(byte[] data) throws Exception JavaDoc;
464
465     
466     // Methods used with extended services
467
/**
468      * Initializes the ExtendedCAService
469      *
470      * @param info contains information used to activate the service.
471      */

472     public void initExternalService(int type, CA ca) throws Exception JavaDoc{
473         ExtendedCAService service = getExtendedCAService(type);
474         if (service != null) {
475             service.init(ca);
476         }
477     }
478         
479
480     /**
481      * Method used to retrieve information about the service.
482      */

483
484     public ExtendedCAServiceInfo getExtendedCAServiceInfo(int type){
485         ExtendedCAServiceInfo ret = null;
486         ExtendedCAService service = getExtendedCAService(type);
487         if (service != null) {
488             ret = service.getExtendedCAServiceInfo();
489         }
490         return ret;
491     }
492
493     /**
494      * Method used to perform the service.
495      */

496     public ExtendedCAServiceResponse extendedService(ExtendedCAServiceRequest request)
497       throws ExtendedCAServiceRequestException, IllegalExtendedCAServiceRequestException, ExtendedCAServiceNotActiveException{
498           ExtendedCAServiceResponse returnval = null;
499           if(request instanceof OCSPCAServiceRequest) {
500               returnval = getExtendedCAService(ExtendedCAServiceInfo.TYPE_OCSPEXTENDEDSERVICE).extendedService(request);
501           }
502           if(request instanceof XKMSCAServiceRequest) {
503               returnval = getExtendedCAService(ExtendedCAServiceInfo.TYPE_XKMSEXTENDEDSERVICE).extendedService(request);
504           }
505           if(request instanceof CmsCAServiceRequest) {
506               returnval = getExtendedCAService(ExtendedCAServiceInfo.TYPE_CMSEXTENDEDSERVICE).extendedService(request);
507           }
508           
509           if(request instanceof KeyRecoveryCAServiceRequest){
510             KeyRecoveryCAServiceRequest keyrecoveryrequest = (KeyRecoveryCAServiceRequest) request;
511             if(keyrecoveryrequest.getCommand() == KeyRecoveryCAServiceRequest.COMMAND_ENCRYPTKEYS){
512                 try{
513                     returnval = new KeyRecoveryCAServiceResponse(KeyRecoveryCAServiceResponse.TYPE_ENCRYPTKEYSRESPONSE,
514                             encryptKeys(keyrecoveryrequest.getKeyPair()));
515                 }catch(CMSException e){
516                     log.error("encrypt:", e.getUnderlyingException());
517                     throw new IllegalExtendedCAServiceRequestException(e);
518                 }catch(Exception JavaDoc e){
519                     throw new IllegalExtendedCAServiceRequestException(e);
520                 }
521             }else{
522                 if(keyrecoveryrequest.getCommand() == KeyRecoveryCAServiceRequest.COMMAND_DECRYPTKEYS){
523                   try{
524                     returnval = new KeyRecoveryCAServiceResponse(KeyRecoveryCAServiceResponse.TYPE_DECRYPTKEYSRESPONSE,
525                             this.decryptKeys(keyrecoveryrequest.getKeyData()));
526                   }catch(CMSException e){
527                      log.error("decrypt:", e.getUnderlyingException());
528                      throw new IllegalExtendedCAServiceRequestException(e);
529                   }catch(Exception JavaDoc e){
530                      throw new IllegalExtendedCAServiceRequestException(e);
531                   }
532                 }else{
533                   throw new IllegalExtendedCAServiceRequestException("Illegal Command");
534                 }
535             }
536           }
537           
538           return returnval;
539     }
540     
541     protected ExtendedCAService getExtendedCAService(int type){
542       ExtendedCAService returnval = null;
543       try{
544         returnval = (ExtendedCAService) extendedcaservicemap.get(new Integer JavaDoc(type));
545         if(returnval == null) {
546             switch(((Integer JavaDoc) ((HashMap JavaDoc)data.get(EXTENDEDCASERVICE+type)).get(ExtendedCAService.EXTENDEDCASERVICETYPE)).intValue()) {
547                 case ExtendedCAServiceInfo.TYPE_OCSPEXTENDEDSERVICE:
548                     returnval = new OCSPCAService((HashMap JavaDoc)data.get(EXTENDEDCASERVICE+type));
549                     break;
550                 case ExtendedCAServiceInfo.TYPE_XKMSEXTENDEDSERVICE:
551                     returnval = new XKMSCAService((HashMap JavaDoc)data.get(EXTENDEDCASERVICE+type));
552                     break;
553                 case ExtendedCAServiceInfo.TYPE_CMSEXTENDEDSERVICE:
554                     returnval = new CmsCAService((HashMap JavaDoc)data.get(EXTENDEDCASERVICE+type));
555                     break;
556             }
557         extendedcaservicemap.put(new Integer JavaDoc(type), returnval);
558         }
559       }catch(Exception JavaDoc e){
560         throw new EJBException JavaDoc(e);
561       }
562     
563       return returnval;
564     }
565     
566     protected void setExtendedCAService(ExtendedCAService extendedcaservice) {
567         if(extendedcaservice instanceof OCSPCAService){
568             data.put(EXTENDEDCASERVICE+ExtendedCAServiceInfo.TYPE_OCSPEXTENDEDSERVICE, extendedcaservice.saveData());
569             extendedcaservicemap.put(new Integer JavaDoc(ExtendedCAServiceInfo.TYPE_OCSPEXTENDEDSERVICE), extendedcaservice);
570         }
571         if(extendedcaservice instanceof XKMSCAService){
572             data.put(EXTENDEDCASERVICE+ExtendedCAServiceInfo.TYPE_XKMSEXTENDEDSERVICE, extendedcaservice.saveData());
573             extendedcaservicemap.put(new Integer JavaDoc(ExtendedCAServiceInfo.TYPE_XKMSEXTENDEDSERVICE), extendedcaservice);
574         }
575         if(extendedcaservice instanceof CmsCAService){
576             data.put(EXTENDEDCASERVICE+ExtendedCAServiceInfo.TYPE_CMSEXTENDEDSERVICE, extendedcaservice.saveData());
577             extendedcaservicemap.put(new Integer JavaDoc(ExtendedCAServiceInfo.TYPE_CMSEXTENDEDSERVICE), extendedcaservice);
578         }
579     }
580     /**
581      * Returns a Collection of ExternalCAServices (int) added to this CA.
582      *
583      */

584         
585     public Collection JavaDoc getExternalCAServiceTypes(){
586         if(data.get(EXTENDEDCASERVICES) == null)
587           return new ArrayList JavaDoc();
588                 
589         return (Collection JavaDoc) data.get(EXTENDEDCASERVICES);
590     }
591     
592     private HashMap JavaDoc extendedcaservicemap = new HashMap JavaDoc();
593     
594     private ArrayList JavaDoc certificatechain = null;
595     private ArrayList JavaDoc requestcertchain = null;
596     
597     private CAInfo cainfo = null;
598
599     /**
600      * Method to upgrade new (or existing externacaservices)
601      * This method needs to be called outside the regular upgrade
602      * since the CA isn't instansiated in the regular upgrade.
603      *
604      */

605     public abstract boolean upgradeExtendedCAServices() ;
606 }
Popular Tags