带有SunMSCapi的applet在linux中不起作用



我们为我们的网站创建了一个新网站,让用户使用我们设计的小程序签署pdf文档。问题是,这个小程序只能在Windows操作系统中正常工作,我们希望将其扩展到linux操作系统。

当我们在linux中运行小程序时,我们会收到以下错误消息:

[opensc-pkcs11]读卡器pcsc.c:896:pcsc_detect_readers:SCardListReaders失败:0x8010002e[opensc-pkcs11]读卡器pcsc.c:1015:pcsc_detect_readers:返回时没有读卡器已找到[opensc-pkcs11]读取器pcsc.c:896:pcsc_detect_readers:SCardListReaders失败:0x8010002e[opensc-pkcs11]读卡器pcsc.c:1015:pcsc_detect_readers:返回时没有读卡器找到java.security.NoSuchProviderException:没有这样的提供程序:sun.security.jca.GetInstance.getService上的SunMSCAPI(未知源(在sun.security.jca.GetInstance.GetInstance(未知源(

我认为当我们试图读取存储在Windows操作系统中的证书时,问题就来了,我们的代码中有这样的调用:

 KeyStore keystore = KeyStore.getInstance("Windows-MY", "SunMSCAPI"); 
            keystore.load(null, null); 
            return keystore;

这是我们用来获取证书列表的函数。

public KeyStore  obtenerCertificados() throws Exception {
            String osNombre = System.getProperty("os.name");
            String osArquitectura = System.getProperty("os.arch");
            String providerConfig = null;
            String configuracionPKCS11 = null;
            // LINUX
            if(osNombre.contains(new StringBuffer("Linux")))
                providerConfig = "name = OpenSCnlibrary = /usr/lib/opensc-pkcs11.son";
            // WINDOWS
            else if(osNombre.contains(new StringBuffer("Windows")))
                if(!osArquitectura.toLowerCase().contains("x86")){
                    System.out.println("Estamos en toLowerCase().contains x86");
                    providerConfig = "name = NSS"+"n"+
                    "nssLibraryDirectory = "+"C:/Archivos de programa/Mozilla Firefox"+"n"+
                    "nssSecmodDirectory = "+"C:/Users/SM/AppData/Local/Mozilla/Firefox/Profiles/plmk3eh9.default"+"n"+
                    "nssDbMode = readOnly" + "n" +
                    "nssModule = keystore" + "n" + 
                    "r";
                }
                else{
                    System.out.println("Estamos en NO toLowerCase().contains x86");
                    providerConfig = "name = NSS"+"n"+
                    "nssLibraryDirectory = "+"C:/Program Files (x86)/Mozilla Firefox"+"n"+
                    "nssLibrary = "+"C:/Program Files (x86)/Mozilla Firefox/softokn3.dll"+"n"+
                    "nssSecmodDirectory = "+"C:/Users/SM/AppData/Roaming/Mozilla/Firefox/Profiles/plmk3eh9.default"+"n"+
                    "nssDbMode = readOnly" + "n" +
                    "nssModule = keystore" + "n" + 
                    "r";
                }
            // MAC OS
            else {providerConfig = "name = OpenSCnlibrary = /Library/OpenSC/lib/opensc-pkcs11.son";}
            ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(providerConfig.getBytes());
            SunPKCS11 _pk11provider = null;
            try {
            _pk11provider = new SunPKCS11(localByteArrayInputStream);
            Security.addProvider(_pk11provider); 
//          _pk11provider.login(new Subject(), new DialogCallbackHandler());
            }catch(Throwable e){
            System.out.println(e.getMessage());
            }
            KeyStore keystore = KeyStore.getInstance("Windows-MY", "SunMSCAPI"); 
            keystore.load(null, null); 
            return keystore;

            }

关于如何将这种使用扩展到linux和MAC,有什么想法吗???

非常感谢你的帮助!!

您不能在Linux或MAC OS X中使用SunMSCAPI提供程序,该提供程序是特定于Windows的,用于处理Windows密钥库。如果你想通过SunPKCS11提供程序在Linux或MAC OS X中使用智能卡或Firefox密钥库,你必须获得一个java.security.KeyStore的实例,将SunPKCS11作为提供程序,就像你对SunMSCAPI所做的那样,即:

        ByteArrayInputStream confStream = ...// your configuration
        SunPKCS11 pkcs11 = new SunPKCS11(confStream);
        Security.addProvider(pkcs11);
        KeyStore ks = KeyStore.getInstance("PKCS11", pkcs11);
        ks.load(null, "your_pin".toCharArray());

使用此代码,您可以在Keystore ks上加载已配置的PKCS11中的密钥。

如果您希望您的PKCS11 pin稍后由第三方引入,还有另一种方法可以做到这一点。为此,您可以使用java.security.KeyStore.CallbackHandlerProtection参数初始化密钥库,如下所示:

        ByteArrayInputStream confStream = ...// your configuration
        SunPKCS11 pkcs11 = new SunPKCS11(confStream);
        Security.addProvider(pkcs11);
        KeyStore.CallbackHandlerProtection cbhp = new KeyStore.CallbackHandlerProtection(new PinInputHandler(msg));
        KeyStore.Builder builder = KeyStore.Builder.newInstance("PKCS11", pkcs11, cbhp);
        builder.getKeyStore();

请注意,第二个示例中的PinInputHandler必须实现:javax.security.auth.callback.CallbackHandler

此外,在您的代码中,您似乎从未通过SunPKCS11提供程序加载PKCS11密钥(即使在Windows情况下也是如此(,因为您没有用SunPKCS11实例化密钥库,您只是用Security.addProvider方法将其添加为提供程序,并且始终只用SunMSCAPI实例化密钥库,然而,在Windows中,你可能会获得一些智能卡密钥,因为如果你为智能卡安装Windows驱动程序,你可以通过Windows密钥库获得它们的密钥。

希望这有帮助,

最新更新