Skip to content

Commit

Permalink
Merge #66: p11mod: Trace FindObjectsInit
Browse files Browse the repository at this point in the history
e60311a p11mod: Trace FindObjectsInit (Jeremy Rand)

Pull request description:

  Refs #58

Top commit has no ACKs.

Tree-SHA512: 788252ec9c163ac2bc3b9c369ad1f9a626ed142bbff2ed80eae65cc016d513e2b5580153bda91f070f74e3e160127691ad8329b923872387e337009eb589fd51
  • Loading branch information
JeremyRand committed Mar 9, 2022
2 parents ad96a12 + e60311a commit a95e13d
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 a95e13d

Please sign in to comment.