Hello,
I have a certificate/key in the local MS keystore and want to use this for client authentication (2-way SSL). However, the private key is marked as
non-exportable.
But during the SSL handshake i get the following exception:
java.security.InvalidKeyException: Modulus is missing
(full stack trace below).
When using an exportable private key AND setting
JCAPIProperties.getInstance().setPrivateKeyExportable(true);
to true, the ssl handshake succeeds.
Is it possible to use JCAPI for this purpose (2-way SSL with non-exportable key)?
Unfortunately, using exportable private keys is not an option.
Thanks in advance,
Michael
Sample code:
import java.security.*;
import java.security.cert.CertificateException;
import javax.net.ssl.*;
import com.pheox.jcapi.*;
public class JcapiSSL {
private static final String HOST = "localhost";
private static final int PORT = 443;
public static void main(String args[]) throws Exception {
System.out.println(JCAPIUtil.getEnvironmentInfo());
JCAPIProperties.setLogging(true);
//JCAPIProperties.getInstance().setPrivateKeyExportable(true);
SSLSocketFactory sslFactroy = null;
SSLContext context;
KeyManagerFactory kmf;
KeyStore keystore;
context = SSLContext.getInstance("TLS");
kmf = KeyManagerFactory.getInstance("SunX509");
keystore = KeyStore.getInstance("msks", "JCAPI");
keystore.load(null, null);
KeyStore truststore = KeyStore.getInstance("msks", "JCAPI");
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(truststore);
kmf.init(keystore, new char[]{});
context.init(kmf.getKeyManagers(), new TrustManager[]{new MyTrust()}, null);
sslFactroy = context.getSocketFactory();
SSLSocket socket = (SSLSocket)sslFactroy.createSocket(HOST, PORT);
socket.startHandshake();
socket.close();
}
// accept all server certs
static class MyTrust implements X509TrustManager {
public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws CertificateException { }
public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws CertificateException { }
public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; }
}
}
Full stack trace:
Exception in thread "main" javax.net.ssl.SSLHandshakeException: Error signing certificate verify
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:150)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1518)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:174)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverHelloDone(ClientHandshaker.java:609)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:160)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:495)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:433)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:818)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1030)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1057)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1041)
at JcapiSSL.main(JcapiSSL.java:38)
Caused by: java.security.InvalidKeyException: Modulus is missing
at sun.security.rsa.RSAKeyFactory.checkKey(RSAKeyFactory.java:112)
at sun.security.rsa.RSAKeyFactory.toRSAKey(RSAKeyFactory.java:76)
at com.sun.crypto.provider.RSACipher.engineGetKeySize(DashoA12275)
at javax.crypto.Cipher.b(DashoA12275)
at javax.crypto.Cipher.a(DashoA12275)
at javax.crypto.Cipher.init(DashoA12275)
at java.security.Signature$CipherAdapter.engineInitSign(Signature.java:1205)
at java.security.Signature$Delegate.init(Signature.java:1079)
at java.security.Signature$Delegate.chooseProvider(Signature.java:1036)
at java.security.Signature$Delegate.engineInitSign(Signature.java:1109)
at java.security.Signature.initSign(Signature.java:503)
at com.sun.net.ssl.internal.ssl.RSASignature.engineInitSign(RSASignature.java:108)
at java.security.Signature$Delegate.engineInitSign(Signature.java:1107)
at java.security.Signature.initSign(Signature.java:503)
at com.sun.net.ssl.internal.ssl.HandshakeMessage$CertificateVerify.<init>(HandshakeMessage.java:1002)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverHelloDone(ClientHandshaker.java:605)
... 8 more
Environment info:
-------------- Environment info start --------------
JCAPI version: 1.1.2.0
JCAPI DLL version: 1.1.2.0
Certificate/system stores:
My
Root
Trust
CA
UserDS
TrustedPublisher
Disallowed
AuthRoot
TrustedPeople
ACRS
ADDRESSBOOK
REQUEST
Using cert entry store: ADDRESSBOOK
Using key entry store: My
Using intermediate cert store: CA
Using root cert store: Root
Using untrusted cert store: null
Available CSPs:
Gemplus GemSAFE Card CSP v1.0
Infineon SICRYPT Base Smart Card CSP
Microsoft Base Cryptographic Provider v1.0
Microsoft Base DSS and Diffie-Hellman Cryptographic Provider
Microsoft Base DSS Cryptographic Provider
Microsoft DH SChannel Cryptographic Provider
Microsoft Enhanced Cryptographic Provider v1.0
Microsoft Enhanced DSS and Diffie-Hellman Cryptographic Provider
Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)
Microsoft Exchange Cryptographic Provider v1.0
Microsoft RSA SChannel Cryptographic Provider
Microsoft Strong Cryptographic Provider
Schlumberger Cryptographic Service Provider
Using CSP: Microsoft Enhanced Cryptographic Provider v1.0
Supported PKCS#11 CSPs:
FTSafe ePass2000 RSA Cryptographic Service Provider
eToken Base Cryptographic Provider
SmartTrust Cryptographic Service Provider
SI_CSP
SafeSign CSP Version 1.0
AR Base Cryptographic Provider
Athena ASECard Crypto CSP
User added PKCS#11 CSPs:
Loaded JCAPI plugins: No plugins loaded!
Registered JCE providers: SUN, version 1.5
SunJSSE, version 1.5
SunJCE, version 1.5
SunJGSS, version 1.0
SunSASL, version 1.5
JCAPI, version 1.12
java.version: 1.5.0_09
java.vendor: Sun Microsystems Inc.
java.vm.version: 1.5.0_09-b01
java.vm.vendor: Sun Microsystems Inc.
os.name: Windows XP
os.arch: x86
os.version: 5.1
java.library.path: C:\Program Files\Java\jdk1.5.0_09\bin;.;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.5.0
_09\bin;C:\Program Files\Support Tools\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Company\Tool;C:\Progr
am Files\Java\j2re1.4.2_02\bin\;C:\PROGRA~1\IBM\SQLLIB\BIN;C:\PROGRA~1\IBM\SQLLIB\FUNCTION;C:\Program Files\Java\j2re1.4
.2_02\bin\;C:\Program Files\IBM\WebSphere MQ\bin;C:\Program Files\IBM\WebSphere MQ\tools\c\samples\bin;C:\Program Files\
Java\j2re1.4.2_02\bin\;
-------------- Environment info end --------------