1 13 14 package org.ejbca.core.model.ca.catoken; 15 16 import java.io.ByteArrayInputStream ; 17 import java.io.ObjectInputStream ; 18 import java.security.KeyPair ; 19 import java.security.PrivateKey ; 20 import java.security.PublicKey ; 21 import java.util.Iterator ; 22 import java.util.Properties ; 23 24 import org.apache.log4j.Logger; 25 import org.ejbca.core.model.SecConst; 26 import org.ejbca.util.Base64; 27 28 29 37 public class HardCATokenSample implements IHardCAToken { 38 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 privatesignkey = null; 85 86 87 private static PublicKey publicsignkey = null; 88 89 private static byte[] enckeypairenc = Base64.decode( 90 ("rO0ABXNyABVqYXZhLnNlY3VyaXR5LktleVBhaXKXAww60s0SkwIAAkwACnByaXZh" 91 +"dGVLZXl0ABpMamF2YS9zZWN1cml0eS9Qcml2YXRlS2V5O0wACXB1YmxpY0tleXQA" 92 +"GUxqYXZhL3NlY3VyaXR5L1B1YmxpY0tleTt4cHNyADF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AApsb2FkRmFjdG9ySQAJdGhyZXNob2xkeHA/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 privateenckey = null; 133 134 135 private static PublicKey publicenckey = null; 136 137 138 private boolean authenticated = false; 139 140 private boolean offline = false; 141 142 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 161 public void init(Properties properties, String signaturealgorithm) { 162 log.debug("Init()"); 163 165 log.info("TestHardCAToken : init : Found the following properties :"); 166 Iterator iter = properties.keySet().iterator(); 167 while(iter.hasNext()){ 168 Object 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 ois = new ObjectInputStream (new ByteArrayInputStream (signkeypairenc)); 179 KeyPair signkeys = (KeyPair ) ois.readObject(); 180 privatesignkey = signkeys.getPrivate(); 181 publicsignkey = signkeys.getPublic(); 182 183 ois = new ObjectInputStream (new ByteArrayInputStream (enckeypairenc)); 184 KeyPair enckeys = (KeyPair ) ois.readObject(); 185 privateenckey = enckeys.getPrivate(); 186 publicenckey = enckeys.getPublic(); 187 188 }catch(Exception e){ 189 log.error("Error in init: ", e); 190 } 191 } 192 193 194 197 public PrivateKey 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 213 public PublicKey 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 229 public String getProvider() { 230 log.debug("getProvider()"); 231 return "BC"; 232 } 233 234 239 public void activate(String 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 256 public boolean deactivate() { 257 authenticated = false; 258 259 return true; 260 } 261 262 265 public int getCATokenStatus() { 266 if(authenticated) 267 return IHardCAToken.STATUS_ACTIVE; 268 return IHardCAToken.STATUS_OFFLINE; 269 } 270 271 } 272 | Popular Tags |