KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.KeyStore JavaDoc;
17 import java.security.KeyStoreException JavaDoc;
18 import java.security.NoSuchAlgorithmException JavaDoc;
19 import java.security.Provider JavaDoc;
20 import java.security.PublicKey JavaDoc;
21 import java.security.Security JavaDoc;
22 import java.security.UnrecoverableKeyException JavaDoc;
23
24 import org.apache.log4j.Logger;
25
26 /** This class implements support for the Eracom HSM for storing CA keys.
27  * The implementation was done by AdNovum Informatik AG and contributed by Philipp Faerber, philipp.faerber(at)adnovum.ch
28  * The Eracom HSM is special in such way as the provider is ERACOM.<slot id>.
29  *
30  * @author AdNovum Informatik AG
31  * @version $Id: EracomCAToken.java,v 1.4.6.1 2007/06/15 09:07:50 jeklund Exp $
32  */

33 public class EracomCAToken extends BaseCAToken implements IHardCAToken {
34
35     /** Log4j instance */
36     private static final Logger log = Logger.getLogger(EracomCAToken.class);
37
38     static final private String JavaDoc SLOT_LABEL_KEY = "slot";
39     static final private String JavaDoc PROVIDER_CLASS = "au.com.eracom.crypto.provider.slot0.ERACOMProvider";
40
41     /**
42      * @throws InstantiationException
43      * @throws IllegalAccessException
44      */

45     public EracomCAToken() throws InstantiationException JavaDoc, IllegalAccessException JavaDoc {
46         super(PROVIDER_CLASS, null, SLOT_LABEL_KEY);
47     }
48
49     /* (non-Javadoc)
50      * @see org.ejbca.core.model.ca.catoken.BaseCAToken#activate(java.lang.String)
51      */

52     public void activate(String JavaDoc authCode) throws CATokenOfflineException,
53                                          CATokenAuthenticationFailedException {
54         try {
55             if ( Security.getProvider(getProvider())==null ) {
56                 /* initialize provider for the correct slot */
57                 Class JavaDoc cl = Class.forName("au.com.eracom.crypto.provider.slot"+sSlotLabel+".ERACOMProvider");
58                 Provider JavaDoc prov = (Provider JavaDoc)cl.newInstance();
59                 Security.addProvider(prov);
60             }
61             if ( Security.getProvider(getProvider())==null )
62                 throw new CATokenOfflineException("not possible to install eracaom provider");
63             KeyStore JavaDoc keyStore = KeyStore.getInstance("CRYPTOKI", "ERACOM."+sSlotLabel);
64             log.debug("Loading key from slot"+sSlotLabel+" using pin.");
65             keyStore.load(null, (authCode!=null && authCode.length()>0)? authCode.toCharArray():null);
66             setKeys(keyStore, authCode);
67         }
68         catch (Throwable JavaDoc t) {
69             log.error("Failed to initialize Eracom provider slot '"+sSlotLabel+"'.", t);
70             throw new CATokenAuthenticationFailedException("Failed to initialize Eracom provider keystore '"+sSlotLabel+"'.");
71         }
72
73     }
74     /* (non-Javadoc)
75      * @see org.ejbca.core.model.ca.catoken.BaseCAToken#readPublicKey(java.security.KeyStore, java.lang.String)
76      */

77     protected PublicKey JavaDoc readPublicKey(KeyStore JavaDoc keyStore, String JavaDoc alias) throws KeyStoreException JavaDoc, NoSuchAlgorithmException JavaDoc, UnrecoverableKeyException JavaDoc {
78         return (PublicKey JavaDoc)keyStore.getKey(alias+"_pub", null);
79     }
80     /* (non-Javadoc)
81      * @see org.ejbca.core.model.ca.catoken.IHardCAToken#getProvider()
82      */

83     public String JavaDoc getProvider() {
84         log.debug("getProvider()");
85         return "ERACOM."+sSlotLabel;
86     }
87
88 }
89
Popular Tags