【问题标题】:Setting pkcs11 cfg file设置 pkcs11 cfg 文件
【发布时间】:2019-11-03 09:54:41
【问题描述】:

我正在尝试通过下一个代码运行一个如何使用 pkcs11 的示例

import java.lang.reflect.Constructor;
import java.security.KeyStore;
import java.security.Provider;
import java.security.Security;
import java.util.Enumeration;

public class SignWithPKCS11
{
    public static void main (String [] args)
    {
            try
            {
                    Class<?> pkcs11Class = Class.forName("sun.security.pkcs11.SunPKCS11");
                    Constructor<?> construct = pkcs11Class.getConstructor(new Class[] {String.class});

                    String configName = "pkcs11.cfg";
                    Provider p = (Provider)construct.newInstance(new Object[] {configName});
                    Security.addProvider(p);
            }
            catch (Throwable t)
            {
                    t.printStackTrace();
            }

    }
}

上面的代码调用文件pkcs11.cfg。它的内容是

name = JSignPdf
library = /home/grados-sanchez/grive/E-CONNECTING/david/opensc-pkcs11.so

使用这个命令

java -cp . SignWithPKCS11
java -Djava.library.path=".:/home/grados-sanchez/grive/E-CONNECTING/david" -cp . SignWithPKCS11

但我明白了

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at SignWithPKCS11.main(SignWithPKCS11.java:32)
Caused by: java.security.ProviderException: Initialization failed
    at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:376)
    at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:103)
Caused by: java.io.IOException: libopensc.so.6: cannot open shared object file: No such file or directory/home/grados-sanchez/grive/E-CONNECTING/david/opensc-pkcs11.so

你能帮我解决一下吗?怎么了?

注意:当我这样做时:

 ls -l /home/grados-sanchez/grive/E-CONNECTING/david/opensc-pkcs11.so

我明白了

 -rw-rw-r-- 1 user user 220232 Jun 19 01:59 /home/grados-sanchez/grive/E-CONNECTING/david/opensc-pkcs11.so

【问题讨论】:

  • 您使用反射而不是直接使用类是否有任何具体原因?

标签: java pkcs#11


【解决方案1】:

似乎缺少的不是opensc-pkcs11.so,而是该库的依赖项。使用 ldd 检查这些依赖项。如果它们与提供者位于同一目录中,则需要在执行命令之前设置LD_LIBRARY_PATH=/home/grados-sanchez/grive/E-CONNECTING/david(例如作为前缀)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 2013-06-21
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多