使用智能卡的SSL客户端身份验证在Java 6中工作,但在Java 7中失败

时间:2013-03-19 15:22:51

标签: ssl java-7 smartcard pkcs#11 authentication

以下代码使用PKCS#11设备(智能卡)创建客户端验证的SSL上下文。这一切都适用于Java 6:

// Configure the SunPkcs11 provider
String pkcs11config;
pkcs11config = "name = Cryptoki";
pkcs11config += "\nlibrary = /SCDriver/libbit4ipki.dylib";
InputStream confStream = new ByteArrayInputStream(pkcs11config.getBytes());
SunPKCS11 sunpkcs11 = new SunPKCS11(confStream);
Security.addProvider(sunpkcs11);

// Specify keystore builder parameters for PKCS#11 keystores
Builder scBuilder = Builder.newInstance("PKCS11", sunpkcs11, new KeyStore.CallbackHandlerProtection(new PasswordRetriever()));

// Create and init KeyManagerFactory
KeyManagerFactory factory = KeyManagerFactory.getInstance("NewSunX509");
factory.init(new KeyStoreBuilderParameters(scBuilder));

// create and init ssl context
m_ssl_context = SSLContext.getInstance("TLS");
m_ssl_context.init(factory.getKeyManagers(), new TrustManager[] {new PkTrustManager()}, null);      
SSLContext.setDefault(m_ssl_context);

PkTrustManager 是简单的“空”类,获取任何服务器/客户端证书, PasswordRetriever 只需通过对话框请求密码 (根据要求我发布了这些的源代码)。 在Java 7上,我在ssl上下文的SSL握手期间得到以下异常:

java.security.InvalidKeyException: Class does not represent an RSA key: sun.security.pkcs11.P11Key$P11PrivateKey
    at iaik.pkcs.pkcs1.RSACipher.engineInit(Unknown Source)
    at iaik.pkcs.pkcs1.RSACipher.engineInit(Unknown Source)
    at iaik.security.rsa.RSA.init(Unknown Source)
    at iaik.security.rsa.RawRSASignature.engineInitSign(Unknown Source)
    at java.security.SignatureSpi.engineInitSign(SignatureSpi.java:103)
    at java.security.Signature.initSign(Signature.java:529)
    at sun.security.ssl.RSASignature.engineInitSign(RSASignature.java:125)
    at java.security.Signature$Delegate.engineInitSign(Signature.java:1136)
    at java.security.Signature.initSign(Signature.java:529)
    at sun.security.ssl.HandshakeMessage$CertificateVerify.<init>(HandshakeMessage.java:1556)
    at sun.security.ssl.ClientHandshaker.serverHelloDone(ClientHandshaker.java:982)
    ... 14 more

在最好的情况下,我会说Java内部已经发生了一些变化,但是检查了Oracle文档,我没有在NewSunX509密钥管理器或其他组件上发现任何假设的变化。我仔细检查了代码,它似乎符合规范(通过那肯定是我错过的东西!)。

我尝试添加配置标志:

System.setProperty("javax.net.ssl.keyStoreType", "pkcs11");
System.setProperty("javax.net.ssl.keyStore", "NONE");
System.setProperty("javax.net.ssl.trustStoreType", "pkcs11");
System.setProperty("javax.net.ssl.trustStore", "NONE");
System.setProperty("javax.net.ssl.keyStoreProvider", sunpkcs11.getName() );
JCEMapper.setProviderId(sunpkcs11.getName());

但是没有变化,同样的错误......并且在Java 6中它们都不是必需的。 在此先感谢任何人都可以提供帮助或有任何想法!

PS:根据@owlstead的请求添加了 -Djava.security.debug = sunpkcs11 并获得了以下输出:

SunPKCS11 loading ---DummyConfig-1---
sunpkcs11: Initializing PKCS#11 library /SCDriver/libbit4ipki.dylib
Information for provider SunPKCS11-Cryptoki
Library info:
  cryptokiVersion: 2.20
  manufacturerID: bit4id srl                      
  flags: 0
  libraryDescription: bit4id PKCS#11                  
  libraryVersion: 1.02
All slots: 0
Slots with tokens: 0
Slot info for slot 0:
  slotDescription: bit4id miniLector-U38 00 00                                     
  manufacturerID: unknown                         
  flags: CKF_TOKEN_PRESENT | CKF_REMOVABLE_DEVICE | CKF_HW_SLOT
  hardwareVersion: 0.00
  firmwareVersion: 0.00
Token info for token in slot 0:
  label: CNS                             
  manufacturerID: ST Incard                       
  model: CNS (LB)        
  serialNumber: 7420057800291590
  flags: CKF_RNG | CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED | CKF_TOKEN_INITIALIZED
  ulMaxSessionCount: CK_EFFECTIVELY_INFINITE
  ulSessionCount: 0
  ulMaxRwSessionCount: CK_EFFECTIVELY_INFINITE
  ulRwSessionCount: CK_UNAVAILABLE_INFORMATION
  ulMaxPinLen: 8
  ulMinPinLen: 5
  ulTotalPublicMemory: 31988
  ulFreePublicMemory: CK_UNAVAILABLE_INFORMATION
  ulTotalPrivateMemory: 780
  ulFreePrivateMemory: CK_UNAVAILABLE_INFORMATION
  hardwareVersion: 0.00
  firmwareVersion: 0.00
  utcTime: 0000000000000000
Mechanism CKM_RSA_PKCS:
  ulMinKeySize: 1024
  ulMaxKeySize: 1024
  flags: 2561 = CKF_HW | CKF_DECRYPT | CKF_SIGN
Mechanism CKM_RSA_PKCS_KEY_PAIR_GEN:
  ulMinKeySize: 1024
  ulMaxKeySize: 1024
  flags: 65537 = CKF_HW | CKF_GENERATE_KEY_PAIR
Mechanism CKM_SHA1_RSA_PKCS:
  ulMinKeySize: 1024
  ulMaxKeySize: 1024
  flags: 2049 = CKF_HW | CKF_SIGN
Mechanism CKM_SHA_1:
  ulMinKeySize: 0
  ulMaxKeySize: 0
  flags: 1024 = CKF_DIGEST
Mechanism CKM_SHA256:
  ulMinKeySize: 0
  ulMaxKeySize: 0
  flags: 1024 = CKF_DIGEST
Mechanism CKM_SHA256_RSA_PKCS:
  ulMinKeySize: 1024
  ulMaxKeySize: 1024
  flags: 2049 = CKF_HW | CKF_SIGN
Mechanism CKM_SHA384:
  ulMinKeySize: 0
  ulMaxKeySize: 0
  flags: 1024 = CKF_DIGEST
Mechanism CKM_SHA384_RSA_PKCS:
  ulMinKeySize: 1024
  ulMaxKeySize: 1024
  flags: 2049 = CKF_HW | CKF_SIGN
Mechanism CKM_SHA512:
  ulMinKeySize: 0
  ulMaxKeySize: 0
  flags: 1024 = CKF_DIGEST
Mechanism CKM_SHA512_RSA_PKCS:
  ulMinKeySize: 1024
  ulMaxKeySize: 1024
  flags: 2049 = CKF_HW | CKF_SIGN
Mechanism CKM_RSA_X_509:
  ulMinKeySize: 1024
  ulMaxKeySize: 1024
  flags: 2561 = CKF_HW | CKF_DECRYPT | CKF_SIGN
Password per token PKCS11 [SunPKCS11-Cryptoki]: sunpkcs11: login succeeded
sunpkcs11: user already logged in
javax.net.ssl.SSLHandshakeException: Error signing certificate verify
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1886)
    *....... (continues with the exception as described above)*

注意:随着Java 6的上次更新,我的代码也停止在Java 6上运行:(

1 个答案:

答案 0 :(得分:7)

通过添加代码行

解决

Security.removeProvider("IAIK");

行之前:

Security.addProvider(sunpkcs11);

这适用于所有版本的java6和java7(希望它们不会在java8中再次使用某些东西......)。

看起来IAIK提​​供商将自己作为PKCS11提供商插入,但随后使用它作为软件密钥调用私钥......因为它实际上是一个软件提供商。