-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
key.go
executable file
·107 lines (90 loc) · 3.04 KB
/
key.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package pkcs11
import (
"encoding/asn1"
"math/big"
"fmt"
"github.com/miekg/pkcs11"
)
type Key struct {
Type string
Label string
CKAID string
Public PublicKeyTemplate
Private PrivateKeyTemplate
}
type PublicKeyTemplate struct {
Token bool
Encrypt bool
Verify bool
Wrap bool
ModulesBits int
Exponent *big.Int
Curve string
}
type PrivateKeyTemplate struct {
Token bool
Private bool
Subject string
Sensitive bool
Extractable bool
Decrypt bool
Sign bool
Unwrap bool
}
func CreateKey(p *pkcs11.Ctx, s pkcs11.SessionHandle, k Key) (pub pkcs11.ObjectHandle, priv pkcs11.ObjectHandle, err error) {
// Key Templates, GenerateKeyPair
pubTemplate := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_ENCRYPT, k.Public.Encrypt),
pkcs11.NewAttribute(pkcs11.CKA_VERIFY, k.Public.Verify),
pkcs11.NewAttribute(pkcs11.CKA_WRAP, k.Public.Wrap),
pkcs11.NewAttribute(pkcs11.CKA_TOKEN, k.Public.Token),
pkcs11.NewAttribute(pkcs11.CKA_LABEL, k.Label),
pkcs11.NewAttribute(pkcs11.CKA_ID, []byte(k.CKAID)),
}
privTemplate := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_TOKEN, k.Private.Token),
pkcs11.NewAttribute(pkcs11.CKA_PRIVATE, k.Private.Private),
pkcs11.NewAttribute(pkcs11.CKA_SENSITIVE, k.Private.Sensitive),
pkcs11.NewAttribute(pkcs11.CKA_EXTRACTABLE, k.Private.Extractable),
pkcs11.NewAttribute(pkcs11.CKA_DECRYPT, k.Private.Decrypt),
pkcs11.NewAttribute(pkcs11.CKA_SIGN, k.Private.Sign),
pkcs11.NewAttribute(pkcs11.CKA_UNWRAP, k.Private.Unwrap),
pkcs11.NewAttribute(pkcs11.CKA_LABEL, k.Label),
pkcs11.NewAttribute(pkcs11.CKA_ID, []byte(k.CKAID)),
}
// Encode ASN.1 key parameters to DER
var derCurve []byte
if k.Type == "EC" || k.Type == "ECDSA" {
derCurve, err = asn1.Marshal(ellipticCurve[k.Public.Curve])
if err != nil {
err = fmt.Errorf("error marshalling curve (%s)", err.Error())
return
}
}
var mechanism *pkcs11.Mechanism
switch k.Type {
case "EC":
mechanism = pkcs11.NewMechanism(pkcs11.CKM_EC_KEY_PAIR_GEN, nil)
pubTemplate = append(pubTemplate, pkcs11.NewAttribute(pkcs11.CKA_EC_PARAMS, derCurve))
case "ECDSA":
mechanism = pkcs11.NewMechanism(pkcs11.CKM_ECDSA_KEY_PAIR_GEN, nil)
pubTemplate = append(pubTemplate, pkcs11.NewAttribute(pkcs11.CKA_ECDSA_PARAMS, derCurve))
case "DH":
mechanism = pkcs11.NewMechanism(pkcs11.CKM_DH_PKCS_KEY_PAIR_GEN, nil)
case "DSA":
mechanism = pkcs11.NewMechanism(pkcs11.CKM_DSA_KEY_PAIR_GEN, nil)
case "RSA":
mechanism = pkcs11.NewMechanism(pkcs11.CKM_RSA_PKCS_KEY_PAIR_GEN, nil)
pubTemplate = append(pubTemplate, pkcs11.NewAttribute(pkcs11.CKA_MODULUS_BITS, k.Public.ModulesBits))
pubTemplate = append(pubTemplate, pkcs11.NewAttribute(pkcs11.CKA_PUBLIC_EXPONENT, k.Public.Exponent.Bytes()))
default:
err = fmt.Errorf("configured key type '%s' is supported", k.Type)
return
}
pub, priv, err = p.GenerateKeyPair(s, []*pkcs11.Mechanism{mechanism}, pubTemplate, privTemplate)
if err != nil {
err = fmt.Errorf("generateKeyPair failed (%s)", err.Error())
return
}
return
}