Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6246419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:41:09+00:00 2026-05-24T12:41:09+00:00

Code: String pkcs11cfg = pkcs11.cfg; Provider p = new SunPKCS11(pkcs11cfg); Security.addProvider(p); KeyStore ks =

  • 0

Code:

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

The problem is that I have some key pairs, I added them with pkcs11-tool.
The version of softhsm is 1.2.1
Why there aren’t any aliases in the KeyStore? How to fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:41:09+00:00Added an answer on May 24, 2026 at 12:41 pm

    We will release SoftHSM 1.3.0 soon. It has support for certificates and is tested with 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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code: String[] torrentFiles = new File(/root/torrents/).list(); if(torrentFiles.length == 0 || torrentFiles == null)
Have the following code: String s= v.request(engine/?key=, P4z72NmBa91&method=load); JSONParser parser = new JSONParser(); Object
I am trying following code String s1 = ß.cfg; System.out.println (s.toUpperCase()); output I am
here is code String[] month=new String[12]{January,February,March,April,May,June,July,August,September,Octomber,November,December}; int day = DateTime.Now.Day; int mon= DateTime.Now.Month; mon
Here's my test code: String foo = new String(); System.out.println(foo); The output is blank
Code: String dir = //Path to the directory File saveDir = new File(dir); //Here
I have this code : string ABSfilePath = ; CsvFileReader reader = null; ABSfilePath
Given the following code: String tmp = new String(\\u0068\\u0065\\u006c\\u006c\\u006f\\u000a); String result = convertToEffectiveString(tmp); //
Here is my code: string[] inputs = new[] {1:2,5:90,7:12,1:70,29:60}; //Declare Dictionary var results =
My code: string result = [{lat:b, lon:d, ulica:ulica1}]; jArray = new JSONArray(result); tab =new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.