尝试在Java中使用带有NSS的SunPKCS11来启用FIPS模式



我正在处理一个需要FIPS 140-2验证的加密的项目,我正在尝试将NSS与SunPKCS11令牌接口一起使用,我已经使其工作,直到在NSS中打开FIPS模式。我收到一个错误,CKR_USER_NOT_LOGGED_IN,我不知道该怎么办。关于我该怎么做有什么建议吗?

我是安全领域的新手,所以这段代码是根据Oracle Java教程、SunPKCS11参考页中的示例以及在网络上以FIPS模式使用NSS的建议拼凑而成的。

这是我正在使用的代码:

String ksName = "my.pfx";
char[] spass = {'m', 'y', 'p', 'w' };
String alias = "testalias";
try {
    KeyStore ks = KeyStore.getInstance("PKCS12");
    FileInputStream ksfis = new FileInputStream(ksName); 
    BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
    ks.load(ksbufin, spass);
    PrivateKey priv = (PrivateKey) ks.getKey(alias, spass);
    System.out.println(" Initialize the signing.");
    Signature sig = Signature.getInstance("SHA1withRSA", "SunPKCS11-NSS-FIPS");
    sig.initSign(priv);
    System.out.println(" Open the digital object to sign.");
    FileInputStream fis = new FileInputStream( "digitalRecipes2.txt" );
    BufferedInputStream bufin = new BufferedInputStream(fis);
    byte[] buffer = new byte[1024];
    int len;
    while ((len = bufin.read(buffer)) >= 0) {
        sig.update(buffer, 0, len);
    }
    bufin.close();
    byte[] realSig = sig.sign();
    FileOutputStream sigfos = new FileOutputStream("digitalRecipes2.txt.sig");
    sigfos.write(realSig);
    sigfos.close();
    java.security.cert.Certificate cert = ks.getCertificate(alias);
    byte[] encodedCert = cert.getEncoded();
    FileOutputStream certfos = new FileOutputStream("mykey.cert");
    certfos.write(encodedCert);
    certfos.close();    
} catch (Exception e) {
    System.err.println( "Caught exception " + e.toString() );
    e.printStackTrace();
}

这是我为nss使用的配置。

name = NSS-FIPS
nssLibraryDirectory = /opt/local/lib/nss
nssSecmodDirectory = /Users/xxxx/work/workspace/learnin/XXXX
nssDbMode = readWrite 
nssModule = fips

当我运行这段代码时,我得到了以下stacktrace。

Initialize the signing.
Caught exception java.security.InvalidKeyException: Could not create RSA private key
java.security.InvalidKeyException: Could not create RSA private key
    at     sun.security.pkcs11.P11RSAKeyFactory.implTranslatePrivateKey(P11RSAKeyFactory.java:88)
    at sun.security.pkcs11.P11KeyFactory.engineTranslateKey(P11KeyFactory.java:115)
    at sun.security.pkcs11.P11KeyFactory.convertKey(P11KeyFactory.java:48)
    at sun.security.pkcs11.P11Signature.engineInitSign(P11Signature.java:374)
    at java.security.Signature$Delegate.engineInitSign(Signature.java:1095)
    at java.security.Signature.initSign(Signature.java:480)
    at     com.xxxxxxxx.digitalSigning.SignMeUpSunPKCS11NSS.main(SignMeUpSunPKCS11NSS.java:43)
Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
    at sun.security.pkcs11.wrapper.PKCS11.C_CreateObject(Native Method)
    at sun.security.pkcs11.P11RSAKeyFactory.generatePrivate(P11RSAKeyFactory.java:238)
    at     sun.security.pkcs11.P11RSAKeyFactory.implTranslatePrivateKey(P11RSAKeyFactory.java:62)
    ... 6 more

我不知道该怎么处理CKR_USER_NOT_LOGGED_IN错误。

如果我将NSS配置更改为不使用FIPS模式,那么程序运行良好,并对文件进行签名、提供签名和提供公钥。

我在NSS配置文件中列出的相应目录中使用以下命令创建了NSS数据库。

modutil -create -dbdir .
modutil -fips true -dbdir .
modutil -changepw "NSS FIPS 140-2 Certificate DB" -dbdir .

您应该首先登录安全令牌。您可以使用AuthProvider:

AuthProvider aprov = Security.getProvider("SunPKCS11-NSS-FIPS");
aprov.login(subject, new MyCallbackHandler());

根据:

http://docs.oracle.com/javase/6/docs/technotes/guides/security/p11guide.html#Login

相关内容

  • 没有找到相关文章

最新更新