当前位置: 首页 > 面试题库 >

连接到SoftHSM Java

阴迪
2023-03-14
问题内容

码:

String pkcs11cfg = "pkcs11.cfg";
Provider p = new SunPKCS11(pkcs11cfg);
Security.addProvider(p);

KeyStore ks = KeyStore.getInstance("PKCS11", p);
ks.load(null, pin);

System.out.println(ks.size()); // prints 0

CFG:

name = pkcs11Test
library = /usr/local/lib/libsofthsm.so
slot = 1

问题是我有一些密钥对,我用pkcs11-tool添加了它们。
softhsm的版本是1.2.1
为什么在密钥库中没有任何别名?如何解决这个问题?


问题答案:

我们将很快发布SoftHSM 1.3.0。它支持证书,并已通过Java测试。

SoftHSM.java:

import java.io.*;
import java.math.*;
import java.util.*;

import java.security.*;
import java.security.interfaces.*;
import java.security.cert.*;
import sun.security.pkcs11.*;
import org.bouncycastle.x509.*;

import javax.security.auth.x500.X500Principal;

import org.bouncycastle.asn1.x509.BasicConstraints;
import org.bouncycastle.asn1.x509.ExtendedKeyUsage;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.asn1.x509.GeneralNames;
import org.bouncycastle.asn1.x509.KeyPurposeId;
import org.bouncycastle.asn1.x509.KeyUsage;
import org.bouncycastle.asn1.x509.X509Extensions;
import org.bouncycastle.x509.X509V3CertificateGenerator;

class SoftHSM
{
    public static void main(String args[]) throws Exception {
        // Set up the Sun PKCS 11 provider
        String configName = "softhsm.cfg";
        Provider p = new SunPKCS11(configName);

        if (-1 == Security.addProvider(p)) {
            throw new RuntimeException("could not add security provider");
        }

        // Load the key store
        char[] pin = "1234".toCharArray();
        KeyStore ks = KeyStore.getInstance("PKCS11", p);
        ks.load(null, pin);

        // Generate the key
        SecureRandom sr = new SecureRandom();
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", p);
        keyGen.initialize(1024, sr);
        KeyPair keyPair = keyGen.generateKeyPair();
        PrivateKey pk = keyPair.getPrivate();

        // Java API requires a certificate chain
        X509Certificate[] chain = generateV3Certificate(keyPair);

        ks.setKeyEntry("ALIAS-GOES-HERE", pk, "1234".toCharArray(), chain);

        ks.store(null);

        System.out.println("OK");
    }


    public static X509Certificate[] generateV3Certificate(KeyPair pair) throws InvalidKeyException, NoSuchProviderException, SignatureException {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

        X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();

        certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
        certGen.setIssuerDN(new X500Principal("CN=Test Certificate"));
        certGen.setNotBefore(new Date(System.currentTimeMillis() - 10000));
        certGen.setNotAfter(new Date(System.currentTimeMillis() + 10000));
        certGen.setSubjectDN(new X500Principal("CN=Test Certificate"));
        certGen.setPublicKey(pair.getPublic());
        certGen.setSignatureAlgorithm("SHA256WithRSA");

        certGen.addExtension(X509Extensions.BasicConstraints, true, new BasicConstraints(false));
        certGen.addExtension(X509Extensions.KeyUsage, true, new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyEncipherment));
        certGen.addExtension(X509Extensions.ExtendedKeyUsage, true, new ExtendedKeyUsage(KeyPurposeId.id_kp_serverAuth));

        certGen.addExtension(X509Extensions.SubjectAlternativeName, false, new GeneralNames(new GeneralName(GeneralName.rfc822Name, "test@test.test")));

        X509Certificate[] chain = new X509Certificate[1];
        chain[0] = certGen.generateX509Certificate(pair.getPrivate(), "SunPKCS11-SoftHSM");

        return chain;
    }
}

softhsm.cfg:

name = SoftHSM
library = /usr/local/lib/libsofthsm.so
slot = 2
attributes(generate, *, *) = {
   CKA_TOKEN = true
}
attributes(generate, CKO_CERTIFICATE, *) = {
   CKA_PRIVATE = false
}
attributes(generate, CKO_PUBLIC_KEY, *) = {
   CKA_PRIVATE = false
}


 类似资料:
  • 我正在尝试运行卡桑德拉,但每次它在连接时都会给出相同的错误...有什么我需要在配置文件或属性文件中编辑的吗? ('无法连接到任何服务器',{'127.0.0.1:9042 ':错误(61,"尝试连接到[('127.0.0.1 ',9042)]。最后一个错误:连接被拒绝")}) 启动cassandra时出错

  • 我已经做了所有的尝试,但都不起作用,这是我的问题,我试图将一些文件从一台机器发送到另一台机器,想法是在目标机器上创建一个servlet,在服务器上运行它,并等待任何客户机连接,客户机和servlet代码在lan网络类型中完美地工作,但当涉及到wan网络类型时,我得到了这个例外 线程“main”org.apache.http.conn.HTTPhostConnectException:连接到192.

  • Selenium Webdriver(2.53)使用testNG和java(jdk 8)。Mozilla Firefox(46)在页面打开时立即崩溃,并显示错误消息: org.apache.http.conn.连接到127.0.0.1:7055[/127.0.0.1]失败:连接拒绝:连接

  • 主要内容:1. 使用SSMS连接到SQL Server,2. 执行查询在本教程中,您将学习如何使用SQL Server Management Studio连接到SQL Server并执行查询。 1. 使用SSMS连接到SQL Server 要使用Microsoft SQL Server Management Studio连接到SQL Server,请使用以下步骤: 首先,从“开始”菜单启动Microsoft SQL Server Management Studio:

  • 我正在尝试使用MongoDB指南针使用Atlas

  • 问题内容: 某些小部件将允许我执行以下操作: 但这样做: 无法说对象没有属性“连接”。 我知道子类化小部件并重新实现该方法将使我能够响应事件。但是,如何从用户上下文来看此后的键盘事件或以其他方式说呢? 问题答案: 创建一个自定义信号,并从重新实现的事件处理程序中发出它: (注意:为了保持事件的现有处理,需要调用基类实现)。