-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpkcs11.go
75 lines (71 loc) · 1.94 KB
/
pkcs11.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"crypto/rsa"
"errors"
"log"
"github.com/ThalesIgnite/crypto11"
)
func initPkcs11(pubkey *rsa.PublicKey) (signer crypto11.Signer, err error) {
if flDebug {
log.Printf("DEBUG: init called with pubkey of type %T", pubkey)
}
// depending on the pkcs11 provider we need to pass these in differently.
// For KMS and some others , we need to find the key via the Token Label,
// but the YubiKey and others, we need to locate the token via the slot.
//
// crypto11.Config
var p11Config crypto11.Config
if flDebug {
log.Printf("Attempting to configure provider with these values: %#v, %#v, %#v, %#v", config.Path, config.Pin, config.TokenLabel, config.SlotNumber)
}
if config.TokenLabel != "" {
p11Config = crypto11.Config{
Path: config.Path,
Pin: config.Pin,
//SlotNumber: &config.SlotNumber,
TokenLabel: config.TokenLabel,
}
} else {
p11Config = crypto11.Config{
Path: config.Path,
Pin: config.Pin,
SlotNumber: &config.SlotNumber,
//TokenLabel: config.TokenLabel,
}
}
if flDebug {
log.Printf("DEBUG: P11Config is %#v", p11Config)
}
ctx, err := crypto11.Configure(&p11Config)
if err != nil {
return signer, err
}
if flDebug {
log.Printf("DEBUG: crypto11.Context is : %#v", ctx)
}
signers, err := ctx.FindAllKeyPairs()
if err != nil {
return signer, err
}
if flDebug {
log.Printf("Signers are: %#v", signers)
}
for x, y := range signers {
if flDebug {
log.Printf("Signer is a %T %#v", y.Public(), y)
}
switch y.Public().(type) {
case *rsa.PublicKey:
//var signingkey *rsa.PublicKey = y.Public().(*rsa.PublicKey)
if pubkey.Equal(y.Public()) {
//if signingkey.Equal(pubkey.(rsa.PublicKey)) {
return signers[x], nil
} else {
log.Printf("INFO: public key mismatch, checking next key")
}
default:
// do nowt.
}
}
return signer, errors.New("something weird happened. please file an issue at https://github.com/PortSwigger/certsquirt/issues")
}