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

p11mod: Trace FindObjectsInit #66

Merged
merged 1 commit into from
Mar 9, 2022
Merged
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
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