KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
18 import java.security.KeyStore JavaDoc;
19 import java.security.KeyStoreException JavaDoc;
20 import java.security.NoSuchAlgorithmException JavaDoc;
21 import java.security.cert.CertificateException JavaDoc;
22
23 import org.apache.log4j.Logger;
24
25
26 /** This class implements support for the nCipher nFast HSM for storing CA keys.
27  * This implementation was done by PrimeKey Solutions AB (www.primekey.se) in 2005
28  * and the development was sponsored by Linagora (www.linagora.com).
29  *
30  * @author Lars Silv�n
31  * @version $Id: NFastCAToken.java,v 1.11.6.2 2007/06/15 09:07:50 jeklund Exp $
32  */

33 public class NFastCAToken extends BaseCAToken implements IHardCAToken {
34
35     /** Log4j instance */
36     private static final Logger log = Logger.getLogger(NFastCAToken.class);
37
38     static final public String JavaDoc SLOT_LABEL_KEY = "keyStore";
39     static final private String JavaDoc PROVIDER_NAME = "nCipherKM";
40     static final private String JavaDoc PROVIDER_CLASS = "com.ncipher.provider.km.nCipherKM";
41
42     private KeyStore JavaDoc keyStore; // The used keystore has to be saved. Otherwise the used keys of the store are destroyed when the
43
// KeyStore destructor is called after the reference is lost. This is a workaround for a nCipher bug.
44

45     /** The constructor of HardCAToken should throw an InstantiationException if the token can not
46      * be created, if for example depending jar files for the particular HSM is not available.
47      * @throws InstantiationException
48      * @throws IllegalAccessException if the nCipher provider is not available
49      */

50     public NFastCAToken() throws InstantiationException JavaDoc, IllegalAccessException JavaDoc {
51         super(PROVIDER_CLASS, PROVIDER_NAME, SLOT_LABEL_KEY);
52         log.debug("Creating NFastCAToken");
53     }
54
55     private KeyStore JavaDoc getKeyStore(String JavaDoc authCode) throws KeyStoreException JavaDoc, NoSuchAlgorithmException JavaDoc, CertificateException JavaDoc, IOException JavaDoc {
56         final KeyStore JavaDoc ks = KeyStore.getInstance("nCipher.sworld");
57         try {
58             ks.load(new ByteArrayInputStream JavaDoc(sSlotLabel.getBytes()),
59                     null);
60         } catch( Exception JavaDoc e) {
61             log.debug("Preload maybe not called. Assuming 1/N. Exception was:",e);
62             ks.load(new ByteArrayInputStream JavaDoc(sSlotLabel.getBytes()),
63                     (authCode!=null && authCode.length()>0)? authCode.toCharArray():null );
64         }
65         return ks;
66     }
67     /* (non-Javadoc)
68      * @see org.ejbca.core.model.ca.catoken.IHardCAToken#activate(java.lang.String)
69      */

70     public void activate(String JavaDoc 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 JavaDoc 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