Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to open serial only sessions #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion crypto11.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ type Config struct {
GCMIVLength int

GCMIVFromHSMControl GCMIVFromHSMConfig

SerialSessionOnly bool
}

type GCMIVFromHSMConfig struct {
Expand Down Expand Up @@ -362,7 +364,12 @@ func Configure(config *Config) (*Context, error) {

// Create a long-term session and log it in (if supported). This session won't be used by callers, instead it is
// used to keep a connection alive to the token to ensure object handles and the log in status remain accessible.
instance.persistentSession, err = instance.ctx.OpenSession(instance.slot, pkcs11.CKF_SERIAL_SESSION|pkcs11.CKF_RW_SESSION)
flags := uint(pkcs11.CKF_SERIAL_SESSION)
if !config.SerialSessionOnly {
flags = flags | pkcs11.CKF_RW_SESSION
}

instance.persistentSession, err = instance.ctx.OpenSession(instance.slot, flags)
if err != nil {
_ = instance.ctx.Finalize()
instance.ctx.Destroy()
Expand Down
8 changes: 7 additions & 1 deletion sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ func (c *Context) getSession() (*pkcs11Session, error) {

// resourcePoolFactoryFunc is called by the resource pool when a new session is needed.
func (c *Context) resourcePoolFactoryFunc() (pool.Resource, error) {
session, err := c.ctx.OpenSession(c.slot, pkcs11.CKF_SERIAL_SESSION|pkcs11.CKF_RW_SESSION)
flags := uint(pkcs11.CKF_SERIAL_SESSION)

if !c.cfg.SerialSessionOnly {
flags = flags | pkcs11.CKF_RW_SESSION
}

session, err := c.ctx.OpenSession(c.slot, flags)
if err != nil {
return nil, err
}
Expand Down