KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > ca > catoken > HardCATokenSample


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.catoken;
15
16 import java.io.ByteArrayInputStream JavaDoc;
17 import java.io.ObjectInputStream JavaDoc;
18 import java.security.KeyPair JavaDoc;
19 import java.security.PrivateKey JavaDoc;
20 import java.security.PublicKey JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import org.apache.log4j.Logger;
25 import org.ejbca.core.model.SecConst;
26 import org.ejbca.util.Base64;
27
28
29 /**
30  * Class used as test and demonstrationclass when writing HardCAToken plug-ins as HSMs.
31  *
32  * Observe: Remember to add a line in teh static section of HardCATokenManager adding the token as available token.
33  *
34  * @author herrvendil
35  * @version $Id: HardCATokenSample.java,v 1.1 2006/01/17 20:31:51 anatom Exp $
36  */

37 public class HardCATokenSample implements IHardCAToken {
38     /** Log4j instance for Base */
39     private static final Logger log = Logger.getLogger(HardCATokenSample.class);
40     
41     private static byte[] signkeypairenc = Base64.decode(
42             ("rO0ABXNyABVqYXZhLnNlY3VyaXR5LktleVBhaXKXAww60s0SkwIAAkwACnByaXZh"
43             +"dGVLZXl0ABpMamF2YS9zZWN1cml0eS9Qcml2YXRlS2V5O0wACXB1YmxpY0tleXQA"
44             +"GUxqYXZhL3NlY3VyaXR5L1B1YmxpY0tleTt4cHNyADFvcmcuYm91bmN5Y2FzdGxl"
45             +"LmpjZS5wcm92aWRlci5KQ0VSU0FQcml2YXRlQ3J0S2V5bLqHzgJzVS4CAAZMAA5j"
46             +"cnRDb2VmZmljaWVudHQAFkxqYXZhL21hdGgvQmlnSW50ZWdlcjtMAA5wcmltZUV4"
47             +"cG9uZW50UHEAfgAFTAAOcHJpbWVFeHBvbmVudFFxAH4ABUwABnByaW1lUHEAfgAF"
48             +"TAAGcHJpbWVRcQB+AAVMAA5wdWJsaWNFeHBvbmVudHEAfgAFeHIALm9yZy5ib3Vu"
49             +"Y3ljYXN0bGUuamNlLnByb3ZpZGVyLkpDRVJTQVByaXZhdGVLZXmyNYtAHTGFVgMA"
50             +"BEwAB21vZHVsdXNxAH4ABUwAEHBrY3MxMkF0dHJpYnV0ZXN0ABVMamF2YS91dGls"
51             +"L0hhc2h0YWJsZTtMAA5wa2NzMTJPcmRlcmluZ3QAEkxqYXZhL3V0aWwvVmVjdG9y"
52             +"O0wAD3ByaXZhdGVFeHBvbmVudHEAfgAFeHBzcgAUamF2YS5tYXRoLkJpZ0ludGVn"
53             +"ZXKM/J8fqTv7HQMABkkACGJpdENvdW50SQAJYml0TGVuZ3RoSQATZmlyc3ROb256"
54             +"ZXJvQnl0ZU51bUkADGxvd2VzdFNldEJpdEkABnNpZ251bVsACW1hZ25pdHVkZXQA"
55             +"AltCeHIAEGphdmEubGFuZy5OdW1iZXKGrJUdC5TgiwIAAHhw///////////////+"
56             +"/////gAAAAF1cgACW0Ks8xf4BghU4AIAAHhwAAAAgI/DALgijbOgTrfu92VQ4Eax"
57             +"KsFSxOESv1vCxLVXvoRxED/LYfIv4ylbyhNdheuYUtQsTlqNzxUrd3AvbovI9TKl"
58             +"kNYs7ICrEJ5Ir2EJrlVTuXnHjLRXwlWYw2J5WGPU15B9tUjcv0HLSJXgax52xEac"
59             +"2VuwVvozPlbKXBXghPeReHNyABNqYXZhLnV0aWwuSGFzaHRhYmxlE7sPJSFK5LgD"
60             +"AAJGAApsb2FkRmFjdG9ySQAJdGhyZXNob2xkeHA/QAAAAAAACHcIAAAACwAAAAB4"
61             +"c3IAEGphdmEudXRpbC5WZWN0b3LZl31bgDuvAQMAA0kAEWNhcGFjaXR5SW5jcmVt"
62             +"ZW50SQAMZWxlbWVudENvdW50WwALZWxlbWVudERhdGF0ABNbTGphdmEvbGFuZy9P"
63             +"YmplY3Q7eHAAAAAAAAAAAHVyABNbTGphdmEubGFuZy5PYmplY3Q7kM5YnxBzKWwC"
64             +"AAB4cAAAAApwcHBwcHBwcHBweHNxAH4ACv///////////////v////4AAAABdXEA"
65             +"fgAOAAAAgA88mbzqXJ5nJTC1cR8Z3Utgx6lJvngkZoexMuwNarHa0eARDEaA8NCX"
66             +"W+mFhgYcVNsL/xn14bbaroQDYvIJ0ILu/4fciaFrngqLYbZTrYAQCOtC+akP4cru"
67             +"uCJwJJ7duocUl4fGktHhq/knmujeOePBjpeevWme7VD4yZz97qABeHhzcQB+AAr/"
68             +"//////////////7////+AAAAAXVxAH4ADgAAAEA7cNGRFsoYtTo8DVK9VZnao9IA"
69             +"1H3cYAc/hYehyey+uNRh6Xh27OcjeFemBmGrvy8NONMizV3nbk/Cerl8TPQ5eHNx"
70             +"AH4ACv///////////////v////4AAAABdXEAfgAOAAAAQAUitZLEEBHZp9pDInbI"
71             +"fzOhht9LWetuIr5Npi9vE/6PYnoz5AWtDzp/1XJbzUGrrR2ybpEweEG6q4V0IJe/"
72             +"pgF4c3EAfgAK///////////////+/////gAAAAF1cQB+AA4AAABAtUkn0jjn/2qV"
73             +"7brM7c/eOBmNg4uhEZn+xM6tq6WAnconFeX4EPO1ap5WYT0hbdzRFD2wz8OxuHz9"
74             +"I3hX/4IFQXhzcQB+AAr///////////////7////+AAAAAXVxAH4ADgAAAEDE0zGf"
75             +"1U8IV68lRnim6wnmW/7s3vJYCU7P2ljJ/rd8w1/AR5gJvkOaDvZykEKwy2uyFZjx"
76             +"yIZ3mvwgSMPMiWUBeHNxAH4ACv///////////////v////4AAAABdXEAfgAOAAAA"
77             +"QLr7v/gjbnmvWH5JXrcYXgUG9/JOX69Fo1RCaD8fVIpyN2gMTeZLWd8KyRd2ci02"
78             +"xgDdhDAbmKZf6XCPFyAowpF4c3EAfgAK///////////////+/////gAAAAF1cQB+"
79             +"AA4AAAADAQABeHNyAC1vcmcuYm91bmN5Y2FzdGxlLmpjZS5wcm92aWRlci5KQ0VS"
80             +"U0FQdWJsaWNLZXklImoOW/pshAIAAkwAB21vZHVsdXNxAH4ABUwADnB1YmxpY0V4"
81             +"cG9uZW50cQB+AAV4cHEAfgANcQB+ACM=").getBytes());
82                                                                                                                         
83     
84     private static PrivateKey JavaDoc privatesignkey = null;
85     
86     
87     private static PublicKey JavaDoc publicsignkey = null;
88     
89     private static byte[] enckeypairenc = Base64.decode(
90             ("rO0ABXNyABVqYXZhLnNlY3VyaXR5LktleVBhaXKXAww60s0SkwIAAkwACnByaXZh"
91             +"dGVLZXl0ABpMamF2YS9zZWN1cml0eS9Qcml2YXRlS2V5O0wACXB1YmxpY0tleXQA"
92             +"GUxqYXZhL3NlY3VyaXR5L1B1YmxpY0tleTt4cHNyADFvcmcuYm91bmN5Y2FzdGxl"
93             +"LmpjZS5wcm92aWRlci5KQ0VSU0FQcml2YXRlQ3J0S2V5bLqHzgJzVS4CAAZMAA5j"
94             +"cnRDb2VmZmljaWVudHQAFkxqYXZhL21hdGgvQmlnSW50ZWdlcjtMAA5wcmltZUV4"
95             +"cG9uZW50UHEAfgAFTAAOcHJpbWVFeHBvbmVudFFxAH4ABUwABnByaW1lUHEAfgAF"
96             +"TAAGcHJpbWVRcQB+AAVMAA5wdWJsaWNFeHBvbmVudHEAfgAFeHIALm9yZy5ib3Vu"
97             +"Y3ljYXN0bGUuamNlLnByb3ZpZGVyLkpDRVJTQVByaXZhdGVLZXmyNYtAHTGFVgMA"
98             +"BEwAB21vZHVsdXNxAH4ABUwAEHBrY3MxMkF0dHJpYnV0ZXN0ABVMamF2YS91dGls"
99             +"L0hhc2h0YWJsZTtMAA5wa2NzMTJPcmRlcmluZ3QAEkxqYXZhL3V0aWwvVmVjdG9y"
100             +"O0wAD3ByaXZhdGVFeHBvbmVudHEAfgAFeHBzcgAUamF2YS5tYXRoLkJpZ0ludGVn"
101             +"ZXKM/J8fqTv7HQMABkkACGJpdENvdW50SQAJYml0TGVuZ3RoSQATZmlyc3ROb256"
102             +"ZXJvQnl0ZU51bUkADGxvd2VzdFNldEJpdEkABnNpZ251bVsACW1hZ25pdHVkZXQA"
103             +"AltCeHIAEGphdmEubGFuZy5OdW1iZXKGrJUdC5TgiwIAAHhw///////////////+"
104             +"/////gAAAAF1cgACW0Ks8xf4BghU4AIAAHhwAAAAgJiNTc1TY9naOkLIbRGcBW7E"
105             +"h+AiT3sn7QaTXgleT7EP68wmxPYIXeodi8M4iv+8koLW3NT/XG6mwTy5GFWQqJ8k"
106             +"FVgM+KFsP40PCXZq02fB0dBnivo1k6ccAhTWngMpd4qSYVgK42Klqku2PK+9vEUw"
107             +"OeLim3FkKMYnphxpfXe3eHNyABNqYXZhLnV0aWwuSGFzaHRhYmxlE7sPJSFK5LgD"
108             +"AAJGAApsb2FkRmFjdG9ySQAJdGhyZXNob2xkeHA/QAAAAAAACHcIAAAACwAAAAB4"
109             +"c3IAEGphdmEudXRpbC5WZWN0b3LZl31bgDuvAQMAA0kAEWNhcGFjaXR5SW5jcmVt"
110             +"ZW50SQAMZWxlbWVudENvdW50WwALZWxlbWVudERhdGF0ABNbTGphdmEvbGFuZy9P"
111             +"YmplY3Q7eHAAAAAAAAAAAHVyABNbTGphdmEubGFuZy5PYmplY3Q7kM5YnxBzKWwC"
112             +"AAB4cAAAAApwcHBwcHBwcHBweHNxAH4ACv///////////////v////4AAAABdXEA"
113             +"fgAOAAAAgBPEW4x7fbjA2kPFDLz3ZozP0ntsdrgSmFf9yHWWTuU2lutBKsxmkSTA"
114             +"51dIgFpa6PEjPkIrRKLh4LApm8makP66l+Qt73CLxzEmqRl7ZxEbXrPV8bALo2K4"
115             +"bDHG/83+jCGoeYAHLbC4tQmqjMba3Wt0lYNqpHeZlwtR3uqiWoyheHhzcQB+AAr/"
116             +"//////////////7////+AAAAAXVxAH4ADgAAAEDMH9eiaoYotp7mu/bmXffgd1P9"
117             +"epIRq1wK1e85U/NViVYdgfdxeQf0U/mK+yfq9VAGmCn2c46gxq0hNykCmSc1eHNx"
118             +"AH4ACv///////////////v////4AAAABdXEAfgAOAAAAQIKMrS3zZ2QwfcJcUHql"
119             +"bsKET8O7PsLyxgYQp7ucZHDktP4CnUgxXwcM9WgN/ciZe+r5gokcZTDWz1Z8q/3s"
120             +"Yml4c3EAfgAK///////////////+/////gAAAAF1cQB+AA4AAABAohOic8dgmTfa"
121             +"7vniO1dPmzA+AdSMILeM3Av/UVhIS6dIh9SJlHtFzSS0Lfx7VRxfrUmbFK0gwvwy"
122             +"vFlUpU8G2XhzcQB+AAr///////////////7////+AAAAAXVxAH4ADgAAAEDX76op"
123             +"lk5Hj9lAiJRJl0MuhsraA/hj3zxdVz+x36tRF/uShXR8Mts9Q37CcrVkO2tSNPBR"
124             +"CmQuIceQiK4nuZo9eHNxAH4ACv///////////////v////4AAAABdXEAfgAOAAAA"
125             +"QLTbEx+wBtZQiJTqwHjDLoKof5B+/ROPkVZlEZSZDX6YOhcJVX0nL8qf4spa4K0P"
126             +"T7zzOL5taWVWT7c+Vb1y3QN4c3EAfgAK///////////////+/////gAAAAF1cQB+"
127             +"AA4AAAADAQABeHNyAC1vcmcuYm91bmN5Y2FzdGxlLmpjZS5wcm92aWRlci5KQ0VS"
128             +"U0FQdWJsaWNLZXklImoOW/pshAIAAkwAB21vZHVsdXNxAH4ABUwADnB1YmxpY0V4"
129             +"cG9uZW50cQB+AAV4cHEAfgANcQB+ACM=").getBytes());
130             
131             
132     private static PrivateKey JavaDoc privateenckey = null;
133     
134        
135     private static PublicKey JavaDoc publicenckey = null;
136    
137    
138     private boolean authenticated = false;
139     
140     private boolean offline = false;
141    
142     /** The constructor of HardCAToken should throw an InstantiationException is the token can not
143      * be created, if for example depending jar files for the particular HSM is not available.
144      */

145     public HardCATokenSample() {
146         log.debug("Creating HardCATokenSample");
147         AvailableHardCAToken token = HardCATokenManager.instance().getAvailableHardCAToken("org.ejbca.core.ejb.ca.catoken.HardCATokenSample");
148         if (token != null) {
149             log.debug("Registered HardCATokenSample succesfully.");
150         }
151     }
152     
153     /**
154      * This method should initalize this plug-in with the properties configured in the adminweb-GUI.
155      *
156      * The following properties is available:
157      * OFFLINE = TRUE | FALSE (Default)
158      *
159      *
160      */

161     public void init(Properties JavaDoc properties, String JavaDoc signaturealgorithm) {
162         log.debug("Init()");
163           // Implement this.
164

165         log.info("TestHardCAToken : init : Found the following properties :");
166         Iterator JavaDoc iter = properties.keySet().iterator();
167         while(iter.hasNext()){
168           Object JavaDoc key = iter.next();
169           log.info(key + " : " + properties.get(key));
170         }
171         
172         if(properties.getProperty("OFFLINE", "FALSE").equals("TRUE"))
173           offline = true;
174         
175         log.info("TestHardCAToken : init : End of properties");
176         
177         try{
178           ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(signkeypairenc));
179           KeyPair JavaDoc signkeys = (KeyPair JavaDoc) ois.readObject();
180           privatesignkey = signkeys.getPrivate();
181           publicsignkey = signkeys.getPublic();
182           
183           ois = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(enckeypairenc));
184           KeyPair JavaDoc enckeys = (KeyPair JavaDoc) ois.readObject();
185           privateenckey = enckeys.getPrivate();
186           publicenckey = enckeys.getPublic();
187                                  
188         }catch(Exception JavaDoc e){
189             log.error("Error in init: ", e);
190         }
191     }
192     
193
194     /**
195      * Should return a reference to the private key.
196      */

197     public PrivateKey JavaDoc getPrivateKey(int purpose) throws CATokenOfflineException {
198         log.debug("getPrivateSignKey()");
199         
200         if(offline || !authenticated)
201           throw new CATokenOfflineException();
202         
203         
204         if(purpose == SecConst.CAKEYPURPOSE_KEYENCRYPT)
205           return HardCATokenSample.privateenckey;
206           
207         return HardCATokenSample.privatesignkey;
208     }
209     
210     /**
211      * Should return a reference to the public key.
212      */

213     public PublicKey JavaDoc getPublicKey(int purpose) throws CATokenOfflineException {
214         log.debug("getPublicSignKey()");
215         if(offline || !authenticated)
216           throw new CATokenOfflineException();
217
218         if(purpose == SecConst.CAKEYPURPOSE_KEYENCRYPT)
219           return HardCATokenSample.publicenckey;
220              
221         return HardCATokenSample.publicsignkey;
222     }
223         
224     
225     /** Should return the signature Provider that should be used to sign things with
226      * the PrivateKey object returned by this signingdevice implementation.
227      * @return String the name of the Provider
228      */

229     public String JavaDoc getProvider() {
230         log.debug("getProvider()");
231         return "BC";
232     }
233
234     /**
235      * The correct authentication code is: foo123
236      *
237      * @see org.ejbca.core.model.ca.catoken.IHardCAToken#activate(java.lang.String)
238      */

239     public void activate(String JavaDoc authenticationcode) throws CATokenAuthenticationFailedException, CATokenOfflineException {
240         if(offline)
241           throw new CATokenOfflineException();
242     
243         if(authenticationcode != null && authenticationcode.equals("foo123"))
244           authenticated = true;
245         else{
246           authenticated = false;
247           throw new CATokenAuthenticationFailedException("Wrong authentication code, try 'foo123'");
248         }
249     }
250     
251     /**
252      *
253      *
254      * @see org.ejbca.core.model.ca.catoken.IHardCAToken#deactivate()
255      */

256     public boolean deactivate() {
257       authenticated = false;
258       
259       return true;
260     }
261
262     /**
263      * @see org.ejbca.core.model.ca.catoken.IHardCAToken#getCATokenStatus()
264      */

265     public int getCATokenStatus() {
266         if(authenticated)
267           return IHardCAToken.STATUS_ACTIVE;
268         return IHardCAToken.STATUS_OFFLINE;
269     }
270     
271 }
272
Popular Tags