我正在開發一個需要FIPS 140-2驗證加密的項目,並且我試圖使用NSS和SunPKCS11令牌接口,並且我已經完成了它的工作直到打開NSS中的FIPS模式。我收到一個錯誤,CKR_USER_NOT_LOGGED_IN,我根本不知道該怎麼辦。關於我應該做什麼的任何建議?試圖在Java中使用SunPKCS11和NSS來啓用FIPS模式
我是新來的安全領域,所以這個代碼是從Oracle Java教程中的例子,SunPKCS11參考頁以及在Web上以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
當我運行這段代碼時,我得到下面的堆棧跟蹤。
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 .