Is it possible to store secret keys on SoftHSM?
<p>I've found this thread: <a href="https://stackoverflow.com/questions/7009931/connecting-to-softhsm-java">Connecting to SoftHSM java</a> and it works when storing private keys, just like the example.</p>

<p>But I need to store secret keys, such as AES.</p>

<p>Here's my code:</p>

<pre><code>import java.security.*;
import sun.security.pkcs11.*;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    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 = "mypin".toCharArray();
        KeyStore keyStore = KeyStore.getInstance("PKCS11", p);
        keyStore.load(null, pin);

        // AES key
        SecretKeySpec secretKeySpec = new SecretKeySpec("0123456789ABCDEF".getBytes(), "AES");
        Key key = new SecretKeySpec(secretKeySpec.getEncoded(), "AES");

        keyStore.setKeyEntry("AA", key, "1234".toCharArray(), null);
        keyStore.store(null); //this gives me the exception.
    }
}
</code></pre>

<p>And this is the softhsm.cfg file:</p>

<pre><code>name = SoftHSM
library = /usr/local/lib/softhsm/libsofthsm.so
slot = 0
attributes(generate, *, *) = {
   CKA_TOKEN = true
}
attributes(generate, CKO_CERTIFICATE, *) = {
   CKA_PRIVATE = false
}
attributes(generate, CKO_PUBLIC_KEY, *) = {
   CKA_PRIVATE = false
}
</code></pre>

<p>When executing keyStore.store(null) I'm getting <code>java.security.KeyStoreException Cannot convert to PKCS11 keys</code></p>