| 1 |
# go-pkcs11uri |
| 2 |
|
| 3 |
Welcome to the go-pkcs11uri library. The implementation follows [RFC 7512](https://tools.ietf.org/html/rfc7512) and this [errata](https://www.rfc-editor.org/errata/rfc7512). |
| 4 |
|
| 5 |
# Exampe usage: |
| 6 |
|
| 7 |
The following example builds on this library [here](https://github.com/miekg/pkcs11) and are using softhsm2 on Fedora. |
| 8 |
|
| 9 |
## Example |
| 10 |
|
| 11 |
This example program extending the one found [here](https://github.com/miekg/pkcs11/blob/master/README.md#examples): |
| 12 |
|
| 13 |
``` |
| 14 |
package main |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"os" |
| 19 |
"strconv" |
| 20 |
|
| 21 |
"github.com/miekg/pkcs11" |
| 22 |
pkcs11uri "github.com/stefanberger/go-pkcs11uri" |
| 23 |
) |
| 24 |
|
| 25 |
func main() { |
| 26 |
if len(os.Args) < 2 { |
| 27 |
panic("Missing pkcs11 URI argument") |
| 28 |
} |
| 29 |
uristr := os.Args[1] |
| 30 |
|
| 31 |
uri, err := pkcs11uri.New() |
| 32 |
if err != nil { |
| 33 |
panic(err) |
| 34 |
} |
| 35 |
err = uri.Parse(uristr) |
| 36 |
if err != nil { |
| 37 |
panic(err) |
| 38 |
} |
| 39 |
|
| 40 |
module, err := uri.GetModule() |
| 41 |
if err != nil { |
| 42 |
panic(err) |
| 43 |
} |
| 44 |
|
| 45 |
slot, ok := uri.GetPathAttribute("slot-id", false) |
| 46 |
if !ok { |
| 47 |
panic("No slot-id in pkcs11 URI") |
| 48 |
} |
| 49 |
slotid, err := strconv.Atoi(slot) |
| 50 |
if err != nil { |
| 51 |
panic(err) |
| 52 |
} |
| 53 |
|
| 54 |
pin, err := uri.GetPIN() |
| 55 |
if err != nil { |
| 56 |
panic(err) |
| 57 |
} |
| 58 |
|
| 59 |
p := pkcs11.New(module) |
| 60 |
err = p.Initialize() |
| 61 |
if err != nil { |
| 62 |
panic(err) |
| 63 |
} |
| 64 |
|
| 65 |
defer p.Destroy() |
| 66 |
defer p.Finalize() |
| 67 |
|
| 68 |
session, err := p.OpenSession(uint(slotid), pkcs11.CKF_SERIAL_SESSION|pkcs11.CKF_RW_SESSION) |
| 69 |
if err != nil { |
| 70 |
panic(err) |
| 71 |
} |
| 72 |
defer p.CloseSession(session) |
| 73 |
|
| 74 |
err = p.Login(session, pkcs11.CKU_USER, pin) |
| 75 |
if err != nil { |
| 76 |
panic(err) |
| 77 |
} |
| 78 |
defer p.Logout(session) |
| 79 |
|
| 80 |
p.DigestInit(session, []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_SHA_1, nil)}) |
| 81 |
hash, err := p.Digest(session, []byte("this is a string")) |
| 82 |
if err != nil { |
| 83 |
panic(err) |
| 84 |
} |
| 85 |
|
| 86 |
for _, d := range hash { |
| 87 |
fmt.Printf("%x", d) |
| 88 |
} |
| 89 |
fmt.Println() |
| 90 |
} |
| 91 |
``` |
| 92 |
|
| 93 |
## Exampe Usage |
| 94 |
|
| 95 |
``` |
| 96 |
$ sudo softhsm2-util --init-token --slot 1 --label test --pin 1234 --so-pin 1234 |
| 97 |
The token has been initialized and is reassigned to slot 2053753261 |
| 98 |
$ go build ./... |
| 99 |
$ sudo ./pkcs11-example 'pkcs11:slot-id=2053753261?module-path=/usr/lib64/pkcs11/libsofthsm2.so&pin-value=1234' |
| 100 |
517592df8fec3ad146a79a9af153db2a4d784ec5 |
| 101 |
``` |
| 102 |
|