Authenticating Using a PKCS#11 Provider

The following sample code illustrates the authentication process using a specific provider:

Copy
private void connect(String host, String port, String client, String pwd, String CA)
throws Throwable {
String configurationFile = "...";
SunPKCS11 provider = new sun.security.pkcs11.
SunPKCS11(configurationFile); Security.addProvider(provider); System.setProperty("javax.net.ssl.keyStoreType",
"PKCS11");
KeyStore keyStore = KeyStore.getInstance("PKCS11", provider);
keyStore.load(null, pwd.toCharArray());
KeyStore trustStore = KeyStore.getInstance("jks", "SUN");
trustStore.load(null, null);
FileInputStream in = new FileInputStream(CA);
 
Certificate caCert = CertificateFactory. getInstance("X.509").generateCertificate(in);
trustStore.setCertificateEntry("cert-0", caCert);
in.close();
Map<String, Object> config = new HashMap<String, Object>();
 
config.put(CCMConstants.CONFIG_KEYSTORE, keyStore); config.put(CCMConstants.CONFIG_KEYSTORE_PWD, pwd); config.put(CCMConstants.CONFIG_TRUSTSTORE,
trustStore); config.put(CCMConstants.CONFIG_HOST_NAME, host); config.put(CCMConstants.CONFIG_HOST_PORT, port);
 
smMgr = SecurityModuleManagerFactory. newInstance(config);
smMgr.openSession(null);
smMgr.getVersion();
 
syncMgr = SyncManagerFactory.newInstance(config);
syncMgr.openSession(null);
syncMgr.getVersion();
 
userMgr = UserManagerFactory.newInstance(config);
userMgr.openSession(null);
userMgr.getVersion();
 
walletMgr = WalletManagerFactory. newInstance(config);
walletMgr.openSession(null);
walletMgr.getVersion();
}