Skip to content

Commit

Permalink
Add automatic support for p11-kit
Browse files Browse the repository at this point in the history
This commit adds support to automatically use the p11-kit-proxy module
on the PKCS#11 KMS if no other module has been specified.

Fixes #259
  • Loading branch information
maraino committed Jun 8, 2023
1 parent 541f830 commit 2723a8f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
41 changes: 40 additions & 1 deletion kms/pkcs11/pkcs11.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ import (
"encoding/hex"
"fmt"
"math/big"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"sync"
"time"

"github.com/ThalesIgnite/crypto11"
"github.com/pkg/errors"

"go.step.sm/crypto/kms/apiv1"
"go.step.sm/crypto/kms/uri"
)
Expand Down Expand Up @@ -60,7 +66,6 @@ func New(ctx context.Context, opts apiv1.Options) (*PKCS11, error) {
}

config.Pin = u.Pin()
config.Path = u.Get("module-path")
config.TokenLabel = u.Get("token")
config.TokenSerial = u.Get("serial")
if v := u.Get("slot-id"); v != "" {
Expand All @@ -70,6 +75,10 @@ func New(ctx context.Context, opts apiv1.Options) (*PKCS11, error) {
}
config.SlotNumber = &n
}
// Get module or default to use p11-kit-proxy.so
if config.Path = u.Get("module-path"); config.Path == "" {
config.Path = findP11KitProxy(ctx)
}
}
if config.Pin == "" && opts.Pin != "" {
config.Pin = opts.Pin
Expand Down Expand Up @@ -402,4 +411,34 @@ func findCertificate(ctx P11, rawuri string) (*x509.Certificate, error) {
return cert, nil
}

// findP11KitProxy uses pkg-config to locate p11-kit-proxy.so
func findP11KitProxy(ctx context.Context) string {
var out strings.Builder

// It should be more than enough even in constraint VMs
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

cmd := exec.CommandContext(ctx, "pkg-config", "--variable=proxy_module", "p11-kit-1")
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return ""
}

path := strings.TrimSpace(out.String())
if _, err := os.Stat(path); err != nil {
if runtime.GOOS != "darwin" {
return ""
}

// pkg-config might return an .so file instead of a .dylib on macOs.
path = strings.Replace(path, ".so", ".dylib", 1)
if _, err := os.Stat(path); err != nil {
return ""
}
}

return path
}

var _ apiv1.CertificateManager = (*PKCS11)(nil)
18 changes: 17 additions & 1 deletion kms/pkcs11/pkcs11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ func TestNew(t *testing.T) {
return k.p11, nil
}

var (
wantMissingModule *PKCS11
wantErrMissingModule = true
)
if findP11KitProxy(context.Background()) != "" {
wantMissingModule = k
wantErrMissingModule = false
}

canceledContext, cancel := context.WithCancel(context.Background())
cancel()

type args struct {
ctx context.Context
opts apiv1.Options
Expand Down Expand Up @@ -68,10 +80,14 @@ func TestNew(t *testing.T) {
URI: "pkcs11:module-path=/usr/local/lib/softhsm/libsofthsm2.so;token=pkcs11-test",
Pin: "passowrd",
}}, k, false},
{"fail missing module", args{context.Background(), apiv1.Options{
{"perhaps with missing module", args{context.Background(), apiv1.Options{
Type: "pkcs11",
URI: "pkcs11:token=pkcs11-test",
Pin: "passowrd",
}}, wantMissingModule, wantErrMissingModule},
{"fail findP11KitProxy", args{canceledContext, apiv1.Options{
Type: "pkcs11",
URI: "pkcs11:token=pkcs11-test?pin-value=password",
}}, nil, true},
{"fail missing pin", args{context.Background(), apiv1.Options{
Type: "pkcs11",
Expand Down

0 comments on commit 2723a8f

Please sign in to comment.