Skip to content

Commit

Permalink
pkcs11mod: Trace FindObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRand committed Mar 9, 2022
1 parent a95e13d commit 0822e8a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ Option B: Using Go build commands with Go modules (works on any platform with Ba

See the `pkcs11proxy` subdirectory for an example of how to use pkcs11mod. Also consider using the higher-level p11mod library (see subdirectory) instead of using pkcs11mod directly (see next section).

## Tracing

Set the environment variable `PKCS11MOD_TRACE=1` to enable debug tracing. The trace will be outputted to the log file.

## Should I use pkcs11mod or p11mod?

p11mod is much easier to use and more idiomatic to Go. However, p11mod implements less of the PKCS#11 specification than pkcs11mod. If you only need functionality that p11mod has, you will probably find p11mod more pleasant to work with. On the other hand, p11mod is much newer and less battle-tested, so you may find pkcs11mod more reliable.
Expand Down
18 changes: 18 additions & 0 deletions pkcs11mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"github.com/miekg/pkcs11"
)

var trace bool

var logfile io.Closer
var backend Backend

Expand All @@ -53,6 +55,10 @@ func init() {
logfile = f
}
log.Println("Namecoin PKCS#11 module loading")

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

func SetBackend(b Backend) {
Expand Down Expand Up @@ -642,14 +648,26 @@ func Go_FindObjects(sessionHandle C.CK_SESSION_HANDLE, phObject C.CK_OBJECT_HAND
goMax := int(ulMaxObjectCount)

if (phObject == nil && goMax > 0) || pulObjectCount == nil {
if trace {
log.Println("pkcs11mod FindObjects: CKR_ARGUMENTS_BAD")
}

return C.CKR_ARGUMENTS_BAD
}

objectHandles, _, err := backend.FindObjects(goSessionHandle, goMax)
if err != nil {
if trace {
log.Printf("pkcs11mod FindObjects: %v", err)
}

return fromError(err)
}

if trace {
log.Printf("pkcs11mod FindObjects: %d objects returned", len(objectHandles))
}

goCount := uint(len(objectHandles))
*pulObjectCount = C.CK_ULONG(goCount)
fromObjectHandleList(objectHandles, C.CK_ULONG_PTR(phObject), goCount)
Expand Down

0 comments on commit 0822e8a

Please sign in to comment.