KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > hardtoken > profiles > EIDProfile


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.hardtoken.profiles;
15
16 import java.io.ObjectInputStream JavaDoc;
17 import java.io.ObjectOutputStream JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.ejb.EJBException JavaDoc;
25
26 import org.ejbca.core.model.SecConst;
27 import org.ejbca.core.model.ca.caadmin.CAInfo;
28
29
30
31
32 /**
33  * EIDProfile is a basic class that should be inherited by all types
34  * of eidprofiles in the system.
35  *
36  *
37  * @version $Id: EIDProfile.java,v 1.2 2006/07/11 13:03:04 herrvendil Exp $
38  */

39 public abstract class EIDProfile extends HardTokenProfileWithAdressLabel {
40     
41     public static final String JavaDoc KEYTYPE_RSA = "RSA";
42     
43     public static final int CAID_USEUSERDEFINED = SecConst.CAID_USEUSERDEFINED;
44     
45     // Protected Constants
46
protected static final String JavaDoc CERTIFICATEPROFILEID = "certificateprofileid";
47     protected static final String JavaDoc CAID = "caid";
48     protected static final String JavaDoc ISKEYRECOVERABLE = "iskeyrecoverable";
49     protected static final String JavaDoc REUSEOLDCERTIFICATE = "reuseoldcertificate";
50     protected static final String JavaDoc MINIMUMKEYLENGTH = "minimunkeylength";
51     protected static final String JavaDoc KEYTYPES = "keytypes";
52     protected static final String JavaDoc CERTWRITABLE = "certwritable";
53
54   
55             
56     // Default Values
57
public EIDProfile() {
58       super();
59             
60     }
61
62     // Public Methods mostly used by PrimeCard
63
/**
64      * Indicates which certificate profileid that should be requested when
65      * generating certificate of given certusage.
66      *
67      * @param certusage should be one of the CERTUSAGE_ constants.
68      * @return The certificate profile id that should be used.
69      */

70     public int getCertificateProfileId(int certusage){return ((Integer JavaDoc) ((List JavaDoc) data.get(CERTIFICATEPROFILEID)).get(certusage)).intValue();}
71
72
73     /**
74      * Field indicating if a certificate should be set to rewritable on the token.
75      *
76      *
77      * @param certusage should be one of the CERTUSAGE_ constants.
78      * @return boolean indicating if the certificate should be rewritabes, if false
79      * the certificate is written as read-only and cannot be renewed.
80      */

81     public boolean getCertWritable(int certusage){ return ((Boolean JavaDoc) ((List JavaDoc) data.get(CERTWRITABLE)).get(certusage)).booleanValue();}
82     
83     /**
84      * Indicates which ca id that should be requested when
85      * generating certificate of given certusage.
86      *
87      * RESERVED FOR FUTURE USE
88      *
89      * @param certusage should be one of the CERTUSAGE_ constants.
90      * @return The caid that should be used.
91      */

92     public int getCAId (int certusage){return ((Integer JavaDoc) ((List JavaDoc) data.get(CAID)).get(certusage)).intValue();}
93
94
95     /**
96      * Indicates if a keys used for the given certusage should be saved to database.
97      *
98      * @param certusage should be one of the CERTUSAGE_ constants.
99      * @return true if the keys used for the certusage should be saved to database.
100      */

101     public boolean getIsKeyRecoverable (int certusage){return ((Boolean JavaDoc) ((List JavaDoc) data.get(ISKEYRECOVERABLE)).get(certusage)).booleanValue();}
102
103     /**
104      * Indicates if the certificate should be reused when recovering a token. This
105      * since some application requires the same certificate when decrypting data.
106      *
107      * @param certusage should be one of the CERTUSAGE_ constants.
108      * @return true if the certificate should bereused
109      */

110     public boolean getReuseOldCertificate (int certusage){return ((Boolean JavaDoc) ((List JavaDoc) data.get(REUSEOLDCERTIFICATE)).get(certusage)).booleanValue();}
111     
112     /**
113      * Gives the minimum key length allowed.
114      * Generally will the tokens maximum kapacity of key be generated
115      *
116      * @param certusage should be one of the CERTUSAGE_ constants.
117      * @return The caid that should be used.
118      */

119     public int getMinimumKeyLength (int certusage){return ((Integer JavaDoc) ((List JavaDoc) data.get(MINIMUMKEYLENGTH)).get(certusage)).intValue(); }
120
121     /**
122      * Indicates which type of key that should be generated for the certusage.
123      *
124      * RESERVED FOR FUTURE USE
125      *
126      * @param certusage should be one of the CERTUSAGE_ constants.
127      * @return The keytype that should be generated, one of the KEYTYPE_ Constants
128      */

129     public String JavaDoc getKeyType (int certusage){return ((String JavaDoc) ((List JavaDoc) data.get(KEYTYPES)).get(certusage)); }
130
131     // Public Methods used By EJBCA
132

133     /**
134      * See above
135      */

136     public void setCertificateProfileId(int certusage, int certprofileid){
137       List JavaDoc list = (List JavaDoc) data.get(CERTIFICATEPROFILEID);
138       list.set(certusage, new Integer JavaDoc(certprofileid));
139       data.put(CERTIFICATEPROFILEID, list);
140     }
141
142     
143     /**
144      * See above
145      */

146     public void setCertWritable(int certusage, boolean certWritable){
147         List JavaDoc list = (List JavaDoc) data.get(CERTWRITABLE);
148         list.set(certusage, Boolean.valueOf(certWritable));
149         data.put(CERTWRITABLE, list);
150     }
151     
152     /**
153      * See above
154      */

155     public void setCAId (int certusage, int caid){
156       List JavaDoc list = (List JavaDoc) data.get(CAID);
157       list.set(certusage, new Integer JavaDoc(caid));
158       data.put(CAID, list);
159     }
160
161     /**
162      * See above
163      */

164     public void setIsKeyRecoverable (int certusage, boolean iskeyrecoverable){
165         List JavaDoc list = (List JavaDoc) data.get(ISKEYRECOVERABLE);
166         list.set(certusage, Boolean.valueOf(iskeyrecoverable));
167         data.put(ISKEYRECOVERABLE, list);
168     }
169     
170     /**
171      * See above
172      */

173     public void setReuseOldCertificate (int certusage, boolean reuseoldcertificate){
174         List JavaDoc list = (List JavaDoc) data.get(REUSEOLDCERTIFICATE);
175         list.set(certusage, Boolean.valueOf(reuseoldcertificate));
176         data.put(REUSEOLDCERTIFICATE, list);
177     }
178
179     /**
180      * See above
181      */

182     public void setMinimumKeyLength (int certusage, int minimumkeylength){
183         List JavaDoc list = (List JavaDoc) data.get(MINIMUMKEYLENGTH);
184         list.set(certusage, new Integer JavaDoc(minimumkeylength));
185         data.put(MINIMUMKEYLENGTH, list);
186     }
187
188     /**
189      * See above
190      */

191     public void setKeyType (int certusage, String JavaDoc keytype){
192       List JavaDoc list = (List JavaDoc) data.get(KEYTYPES);
193       list.set(certusage, keytype);
194       data.put(KEYTYPES, list);
195     }
196     
197     /**
198      * Returns a collection of all defined certificate profiles.
199      *
200      */

201     public Collection JavaDoc getAllCertificateProfileIds(){
202       return (Collection JavaDoc) data.get(CERTIFICATEPROFILEID);
203     }
204     
205     /**
206      * Returns all valid CAids, if a certusage have CAID_USEUSERDEFINED defined then
207      * it will not be among available valus in returned collection.
208      *
209      * @return
210      */

211     public Collection JavaDoc getAllCAIds(){
212       Collection JavaDoc caids = (Collection JavaDoc) data.get(CAID);
213       ArrayList JavaDoc retval = new ArrayList JavaDoc();
214       Iterator JavaDoc iter = caids.iterator();
215       while(iter.hasNext()){
216         Integer JavaDoc value = (Integer JavaDoc) iter.next();
217         if(value.intValue() > CAInfo.SPECIALCAIDBORDER || value.intValue() < 0){
218           retval.add(value);
219         }
220       }
221       
222       return retval;
223     }
224     
225     public abstract int[] getAvailableMinimumKeyLengths();
226                   
227     public void upgrade(){
228       // Perform upgrade functionality
229
super.upgrade();
230     }
231     
232     // Protected methods
233
public boolean isTokenSupported(String JavaDoc[][] supportedcards, String JavaDoc tokenidentificationstring){
234       boolean returnval = true;
235       Iterator JavaDoc iter = ((List JavaDoc) data.get(MINIMUMKEYLENGTH)).iterator();
236       int[] availablekeylengths = getAvailableMinimumKeyLengths();
237       
238       while(iter.hasNext()){
239         int index = -1;
240         int keylength = ((Integer JavaDoc) iter.next()).intValue();
241         for(int i=0;i<availablekeylengths.length;i++){
242           if(availablekeylengths[i] == keylength){
243             index=i;
244             break;
245           }
246         }
247         returnval = returnval && super.isTokenSupported(supportedcards[index], tokenidentificationstring);
248       }
249                 
250       return returnval;
251     }
252     
253     /**
254      * Help Method that should be used
255      * @param emptyclone
256      */

257     
258     protected void clone(EIDProfile emptyclone){
259         java.io.ByteArrayOutputStream JavaDoc baos = new java.io.ByteArrayOutputStream JavaDoc();
260
261         try{
262             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
263             oos.writeObject(this.saveData());
264             oos.close();
265             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(new java.io.ByteArrayInputStream JavaDoc(baos.toByteArray()));
266             HashMap JavaDoc cloneddata = (HashMap JavaDoc) ois.readObject();
267             ois.close();
268             emptyclone.loadData(cloneddata);
269         }catch(Exception JavaDoc e){
270             throw new EJBException JavaDoc(e);
271         }
272     }
273
274
275 }
276
Popular Tags