Two possibilities come to my mind:
- Per PKCS#11 2.40,
Only session objects can be created during a read-only session
Therefore the session of C_CreateObject
needs to have been opened with the flags
argument set to CKF_SERIAL_SESSION | CKF_RW_SESSION
.
- The
pTemplate
argment to C_CreateObject
needs to include the CKA_TOKEN
attribute so that the newly created key would be a "token object" rather than a "session object".
In PKCS#11, a token object is persistent across sessions whereas a session object is ephemeral and would get dropped once the session is closed.
Hope this helps.