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

Added * pkcs11.ctx to Crypto11.Config, and changed it so that existing ctx can be reused #93

Open
wants to merge 2 commits 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
20 changes: 16 additions & 4 deletions crypto11.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ type Config struct {
// Full path to PKCS#11 library.
Path string

// pkcs11.ctx
PKCS11Ctx *pkcs11.Ctx

// Token serial number.
TokenSerial string

Expand Down Expand Up @@ -315,9 +318,16 @@ func Configure(config *Config) (*Context, error) {
config.GCMIVLength = DefaultGCMIVLength
}

var ctx *pkcs11.Ctx
if config.PKCS11Ctx != nil {
ctx = config.PKCS11Ctx
} else {
ctx = pkcs11.New(config.Path)
}

instance := &Context{
cfg: config,
ctx: pkcs11.New(config.Path),
ctx: ctx,
}

if instance.ctx == nil {
Expand All @@ -331,9 +341,11 @@ func Configure(config *Config) (*Context, error) {

// Only Initialize if we are the first Context using the library
if numExistingContexts == 0 {
if err := instance.ctx.Initialize(); err != nil {
instance.ctx.Destroy()
return nil, errors.WithMessage(err, "failed to initialize PKCS#11 library")
if config.PKCS11Ctx == nil {
if err := instance.ctx.Initialize(); err != nil {
instance.ctx.Destroy()
return nil, errors.WithMessage(err, "failed to initialize PKCS#11 library")
}
}
}
slots, err := instance.ctx.GetSlotList(true)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ThalesIgnite/crypto11
go 1.13

require (
github.com/miekg/pkcs11 v1.0.3-0.20190429190417-a667d056470f
github.com/miekg/pkcs11 v1.1.1
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0
github.com/thales-e-security/pool v0.0.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/miekg/pkcs11 v1.0.3-0.20190429190417-a667d056470f h1:eVB9ELsoq5ouItQBr5Tj334bhPJG/MX+m7rTchmzVUQ=
github.com/miekg/pkcs11 v1.0.3-0.20190429190417-a667d056470f/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
github.com/miekg/pkcs11 v1.1.1 h1:Ugu9pdy6vAYku5DEpVWVFPYnzV+bxB+iRdbuFSu7TvU=
github.com/miekg/pkcs11 v1.1.1/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down