1 13 14 package org.ejbca.core.model.ca.catoken; 15 16 import java.io.ByteArrayInputStream ; 17 import java.io.IOException ; 18 import java.security.KeyStore ; 19 import java.security.KeyStoreException ; 20 import java.security.NoSuchAlgorithmException ; 21 import java.security.cert.CertificateException ; 22 23 import org.apache.log4j.Logger; 24 25 26 33 public class NFastCAToken extends BaseCAToken implements IHardCAToken { 34 35 36 private static final Logger log = Logger.getLogger(NFastCAToken.class); 37 38 static final public String SLOT_LABEL_KEY = "keyStore"; 39 static final private String PROVIDER_NAME = "nCipherKM"; 40 static final private String PROVIDER_CLASS = "com.ncipher.provider.km.nCipherKM"; 41 42 private KeyStore keyStore; 45 50 public NFastCAToken() throws InstantiationException , IllegalAccessException { 51 super(PROVIDER_CLASS, PROVIDER_NAME, SLOT_LABEL_KEY); 52 log.debug("Creating NFastCAToken"); 53 } 54 55 private KeyStore getKeyStore(String authCode) throws KeyStoreException , NoSuchAlgorithmException , CertificateException , IOException { 56 final KeyStore ks = KeyStore.getInstance("nCipher.sworld"); 57 try { 58 ks.load(new ByteArrayInputStream (sSlotLabel.getBytes()), 59 null); 60 } catch( Exception e) { 61 log.debug("Preload maybe not called. Assuming 1/N. Exception was:",e); 62 ks.load(new ByteArrayInputStream (sSlotLabel.getBytes()), 63 (authCode!=null && authCode.length()>0)? authCode.toCharArray():null ); 64 } 65 return ks; 66 } 67 70 public void activate(String authCode) throws CATokenOfflineException, CATokenAuthenticationFailedException { 71 try { 72 if ( keyStore==null ) 73 keyStore = getKeyStore(authCode); 74 setKeys(keyStore, authCode); 75 log.debug("Keys from "+sSlotLabel+ " activated."); 76 } catch( Throwable t ) { 77 log.debug("Authentication failed for keystore "+sSlotLabel+':', t ); 78 CATokenAuthenticationFailedException e = new CATokenAuthenticationFailedException(t.toString()); 79 e.initCause(t); 80 deactivate(); 81 throw e; 82 } 83 } 84 } 85 | Popular Tags |