pkcs11uri

package module
v0.0.0-...-7828495 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2023 License: Apache-2.0 Imports: 8 Imported by: 6

README

go-pkcs11uri

Welcome to the go-pkcs11uri library. The implementation follows RFC 7512 and this errata.

Exampe usage:

The following example builds on this library here and are using softhsm2 on Fedora.

Example

This example program extending the one found here:

package main

import (
	"fmt"
	"os"
	"strconv"

	"github.com/miekg/pkcs11"
	pkcs11uri "github.com/stefanberger/go-pkcs11uri"
)

func main() {
	if len(os.Args) < 2 {
		panic("Missing pkcs11 URI argument")
	}
	uristr := os.Args[1]

	uri, err := pkcs11uri.New()
	if err != nil {
		panic(err)
	}
	err = uri.Parse(uristr)
	if err != nil {
		panic(err)
	}

	module, err := uri.GetModule()
	if err != nil {
		panic(err)
	}

	slot, ok := uri.GetPathAttribute("slot-id", false)
	if !ok {
		panic("No slot-id in pkcs11 URI")
	}
	slotid, err := strconv.Atoi(slot)
	if err != nil {
		panic(err)
	}

	pin, err := uri.GetPIN()
	if err != nil {
		panic(err)
	}

	p := pkcs11.New(module)
	err = p.Initialize()
	if err != nil {
		panic(err)
	}

	defer p.Destroy()
	defer p.Finalize()

	session, err := p.OpenSession(uint(slotid), pkcs11.CKF_SERIAL_SESSION|pkcs11.CKF_RW_SESSION)
	if err != nil {
		panic(err)
	}
	defer p.CloseSession(session)

	err = p.Login(session, pkcs11.CKU_USER, pin)
	if err != nil {
		panic(err)
	}
	defer p.Logout(session)

	p.DigestInit(session, []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_SHA_1, nil)})
	hash, err := p.Digest(session, []byte("this is a string"))
	if err != nil {
		panic(err)
	}

	for _, d := range hash {
		fmt.Printf("%x", d)
	}
	fmt.Println()
}

Exampe Usage

$ sudo softhsm2-util --init-token --slot 1 --label test --pin 1234 --so-pin 1234
The token has been initialized and is reassigned to slot 2053753261
$ go build ./...
$ sudo ./pkcs11-example 'pkcs11:slot-id=2053753261?module-path=/usr/lib64/pkcs11/libsofthsm2.so&pin-value=1234'
517592df8fec3ad146a79a9af153db2a4d784ec5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pkcs11URI

type Pkcs11URI struct {
	// contains filtered or unexported fields
}

Pkcs11URI holds a pkcs11 URI object

func New

func New() *Pkcs11URI

New creates a new Pkcs11URI object

func (*Pkcs11URI) AddEnv

func (uri *Pkcs11URI) AddEnv(name, value string)

AddEnv adds an environment variable for the pkcs11 module

func (*Pkcs11URI) AddPathAttribute

func (uri *Pkcs11URI) AddPathAttribute(name, value string) error

AddPathAttribute adds a path attribute; it returns an error if an attribute with the same name already existed or if the given value cannot be pct-unescaped

func (*Pkcs11URI) AddPathAttributeUnencoded

func (uri *Pkcs11URI) AddPathAttributeUnencoded(name string, value []byte) error

AddPathAttributeUnencoded adds a path attribute given as byte[] which must not already be pct-encoded; it returns an error if an attribute with the same name already existed

func (*Pkcs11URI) AddQueryAttribute

func (uri *Pkcs11URI) AddQueryAttribute(name, value string) error

AddQueryAttribute adds a query attribute; it returns an error if an attribute with the same name already existed or if the given value cannot be pct-unescaped

func (*Pkcs11URI) AddQueryAttributeUnencoded

func (uri *Pkcs11URI) AddQueryAttributeUnencoded(name string, value []byte) error

AddQueryAttributeUnencoded adds a query attribute given as byte[] which must not already be pct-encoded; it returns an error if an attribute with the same name already existed

func (*Pkcs11URI) Format

func (uri *Pkcs11URI) Format() (string, error)

Format formats a Pkcs11URI to it string representaion

func (*Pkcs11URI) GetEnvMap

func (uri *Pkcs11URI) GetEnvMap() map[string]string

GetEnvMap returns the map of environment variables

func (*Pkcs11URI) GetModule

func (uri *Pkcs11URI) GetModule() (string, error)

GetModule returns the module to use or an error in case no module could be found. First the module-path is checked for whether it holds an absolute that can be read by the current user. If this is the case the module is returned. Otherwise either the module-path is used or the user-provided module path is used to match a module containing what is set in the attribute module-name.

func (*Pkcs11URI) GetModuleDirectories

func (uri *Pkcs11URI) GetModuleDirectories() []string

GetModuleDirectories gets the search directories for pkcs11 modules

func (*Pkcs11URI) GetPIN

func (uri *Pkcs11URI) GetPIN() (string, error)

GetPIN gets the PIN from either the pin-value or pin-source attribute; a user may want to call HasPIN() before calling this function to determine whether a PIN has been provided at all so that an error code returned by this function indicates that the PIN value could not be retrieved.

func (*Pkcs11URI) GetPathAttribute

func (uri *Pkcs11URI) GetPathAttribute(name string, pctencode bool) (string, bool)

GetPathAttribute returns the value of a path attribute in unescaped form or pct-encoded form

func (*Pkcs11URI) GetQueryAttribute

func (uri *Pkcs11URI) GetQueryAttribute(name string, pctencode bool) (string, bool)

GetQueryAttribute returns the value of a query attribute in unescaped or pct-encoded form

func (*Pkcs11URI) HasPIN

func (uri *Pkcs11URI) HasPIN() bool

HasPIN allows the user to check whether a PIN has been provided either by the pin-value or the pin-source attributes. It should be called before GetPIN(), which may still fail getting the PIN from a file for example.

func (*Pkcs11URI) Parse

func (uri *Pkcs11URI) Parse(uristring string) error

Parse parses a pkcs11: URI string

func (*Pkcs11URI) RemovePathAttribute

func (uri *Pkcs11URI) RemovePathAttribute(name string)

RemovePathAttribute removes a path attribute

func (*Pkcs11URI) RemoveQueryAttribute

func (uri *Pkcs11URI) RemoveQueryAttribute(name string)

RemoveQueryAttribute removes a path attribute

func (*Pkcs11URI) SetAllowAnyModule

func (uri *Pkcs11URI) SetAllowAnyModule(allowAnyModule bool)

SetAllowAnyModule allows any module to be loaded; by default this is not allowed

func (*Pkcs11URI) SetAllowedModulePaths

func (uri *Pkcs11URI) SetAllowedModulePaths(allowedModulePaths []string)

SetAllowedModulePaths sets allowed module paths to restrict access to modules. Directory entries must end with a '/', all other ones are assumed to be file entries. Allowed modules are filtered by string matching.

func (*Pkcs11URI) SetEnvMap

func (uri *Pkcs11URI) SetEnvMap(env map[string]string)

SetEnvMap sets the environment variables for the pkcs11 module

func (*Pkcs11URI) SetModuleDirectories

func (uri *Pkcs11URI) SetModuleDirectories(moduleDirectories []string)

SetModuleDirectories sets the search directories for pkcs11 modules

func (*Pkcs11URI) SetPathAttribute

func (uri *Pkcs11URI) SetPathAttribute(name, value string) error

SetPathAttribute sets the value for a path attribute; this function may return an error if the given value cannot be pct-unescaped

func (*Pkcs11URI) SetPathAttributeUnencoded

func (uri *Pkcs11URI) SetPathAttributeUnencoded(name string, value []byte)

SetPathAttributeUnencoded sets the value for a path attribute given as byte[]. The value must not have been pct-encoded already.

func (*Pkcs11URI) SetQueryAttribute

func (uri *Pkcs11URI) SetQueryAttribute(name, value string) error

SetQueryAttribute sets the value for a query attribute; this function may return an error if the given value cannot pct-unescaped

func (*Pkcs11URI) SetQueryAttributeUnencoded

func (uri *Pkcs11URI) SetQueryAttributeUnencoded(name string, value []byte)

SetQueryAttributeUnencoded sets the value for a quiery attribute given as byte[]. The value must not have been pct-encoded already.

func (*Pkcs11URI) Validate

func (uri *Pkcs11URI) Validate() error

Validate validates a Pkcs11URI object's attributes following RFC 7512 rules and proper formatting of their values

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL