Skip to content

Commit

Permalink
Make Ctx an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Rand committed Oct 13, 2022
1 parent a4c4380 commit 690d7f0
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 90 deletions.
2 changes: 1 addition & 1 deletion p11/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func OpenModule(path string) (Module, error) {

// Module represents a PKCS#11 module, and can be used to create Sessions.
type Module struct {
ctx *pkcs11.Ctx
ctx pkcs11.Ctx
}

// Info returns general information about the module.
Expand Down
2 changes: 1 addition & 1 deletion p11/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type Session interface {

type sessionImpl struct {
sync.Mutex
ctx *pkcs11.Ctx
ctx pkcs11.Ctx
handle pkcs11.SessionHandle
}

Expand Down
2 changes: 1 addition & 1 deletion p11/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/miekg/pkcs11"

// Slot represents a slot that may hold a token.
type Slot struct {
ctx *pkcs11.Ctx
ctx pkcs11.Ctx
id uint
}

Expand Down
10 changes: 5 additions & 5 deletions parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func init() {
os.Setenv("SOFTHSM2_CONF", wd+"/softhsm2.conf")
}

func initPKCS11Context(modulePath string) (*Ctx, error) {
func initPKCS11Context(modulePath string) (Ctx, error) {
context := New(modulePath)

if context == nil {
Expand All @@ -59,7 +59,7 @@ func initPKCS11Context(modulePath string) (*Ctx, error) {
return context, err
}

func getSlot(p *Ctx, label string) (uint, error) {
func getSlot(p Ctx, label string) (uint, error) {
slots, err := p.GetSlotList(true)
if err != nil {
return 0, err
Expand All @@ -80,7 +80,7 @@ func getSlot(p *Ctx, label string) (uint, error) {
return 0, fmt.Errorf("Slot not found: %s", label)
}

func getPrivateKey(context *Ctx, session SessionHandle, label string) (ObjectHandle, error) {
func getPrivateKey(context Ctx, session SessionHandle, label string) (ObjectHandle, error) {
var noKey ObjectHandle
template := []*Attribute{
NewAttribute(CKA_CLASS, CKO_PRIVATE_KEY),
Expand All @@ -105,12 +105,12 @@ func getPrivateKey(context *Ctx, session SessionHandle, label string) (ObjectHan
}

type signer struct {
context *Ctx
context Ctx
session SessionHandle
privateKey ObjectHandle
}

func makeSigner(context *Ctx) (*signer, error) {
func makeSigner(context Ctx) (*signer, error) {
slot, err := getSlot(context, tokenLabel)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
const notFound = 0xffffffff

// test whether mech is available; skip the test if it isn't
func needMech(t *testing.T, p *Ctx, sh SessionHandle, mech uint) {
func needMech(t *testing.T, p Ctx, sh SessionHandle, mech uint) {
slots, err := p.GetSlotList(true)
if err != nil {
t.Fatal("GetSlotList:", err)
Expand All @@ -27,7 +27,7 @@ func needMech(t *testing.T, p *Ctx, sh SessionHandle, mech uint) {
t.Skipf("skipping test; mech 0x%X not supported by softhsm", mech)
}

func findObject(t *testing.T, p *Ctx, sh SessionHandle, class uint, label string) ObjectHandle {
func findObject(t *testing.T, p Ctx, sh SessionHandle, class uint, label string) ObjectHandle {
template := []*Attribute{
NewAttribute(CKA_CLASS, class),
NewAttribute(CKA_LABEL, label),
Expand All @@ -49,7 +49,7 @@ func findObject(t *testing.T, p *Ctx, sh SessionHandle, class uint, label string
}

// generate a rsa key if it doesn't exist
func getRSA(t *testing.T, p *Ctx, sh SessionHandle) (pub, priv ObjectHandle) {
func getRSA(t *testing.T, p Ctx, sh SessionHandle) (pub, priv ObjectHandle) {
pub = findObject(t, p, sh, CKO_PUBLIC_KEY, "paramstest")
priv = findObject(t, p, sh, CKO_PUBLIC_KEY, "paramstest")
if pub == notFound || priv == notFound {
Expand Down
Loading

0 comments on commit 690d7f0

Please sign in to comment.