Skip to content

Commit

Permalink
p11mod: Trace FindObjectsInit
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRand committed Mar 9, 2022
1 parent ad96a12 commit e60311a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions p11mod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ Prerequisites:
## Example usage

See the `p11proxy` sibling directory for an example of how to use p11mod.

## Tracing

Set the environment variable `P11MOD_TRACE=1` to enable debug tracing. The trace will be outputted to the log file.
19 changes: 19 additions & 0 deletions p11mod/p11mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ package p11mod

import (
"log"
"os"
"sync"

"github.com/miekg/pkcs11"
"github.com/miekg/pkcs11/p11"
"github.com/namecoin/pkcs11mod"
)

var trace bool

var highBackend Backend
var highBackendError error

Expand All @@ -26,6 +29,10 @@ func init() {
}

pkcs11mod.SetBackend(b)

if os.Getenv("P11MOD_TRACE") == "1" {
trace = true
}
}

type llSession struct {
Expand Down Expand Up @@ -391,11 +398,19 @@ func (ll *llBackend) SetAttributeValue(sh pkcs11.SessionHandle, o pkcs11.ObjectH
func (ll *llBackend) FindObjectsInit(sh pkcs11.SessionHandle, template []*pkcs11.Attribute) error {
session, err := ll.getSessionByHandle(sh)
if err != nil {
if trace {
log.Printf("p11mod FindObjectsInit: %v", err)
}

return err
}

objects, err := session.session.FindObjects(template)
if err != nil {
if trace {
log.Printf("p11mod FindObjectsInit: %v", err)
}

if err == pkcs11.Error(pkcs11.CKR_OPERATION_NOT_INITIALIZED) {
// session.FindObjects() can relay CKR_OPERATION_NOT_INITIALIZED from
// FindObjects or FindObjectsFinal, but PKCS#11 spec says
Expand All @@ -409,6 +424,10 @@ func (ll *llBackend) FindObjectsInit(sh pkcs11.SessionHandle, template []*pkcs11
}
}

if trace {
log.Printf("p11mod FindObjectsInit: %d objects returned", len(objects))
}

session.objects = append(session.objects, objects...)
session.objectsPending = objects

Expand Down

0 comments on commit e60311a

Please sign in to comment.