-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
184 lines (157 loc) · 4.55 KB
/
plugin.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"context"
"crypto"
"crypto/rsa"
"errors"
"fmt"
"github.com/ThalesIgnite/crypto11"
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/hcl"
"github.com/miekg/pkcs11"
spi "github.com/spiffe/spire/proto/common/plugin"
"github.com/spiffe/spire/proto/server/keymanager"
)
type configuration struct {
HSMPath string `hcl:"hsm_path"`
TokenLabel string `hcl:"token_label"`
UserPin string `hcl:"user_pin"`
}
type KeyManager struct {
config *configuration
ctx *pkcs11.Ctx
session pkcs11.SessionHandle
slotID uint
}
func New() *KeyManager {
return &KeyManager{}
}
func (m *KeyManager) Configure(ctx context.Context, req *spi.ConfigureRequest) (*spi.ConfigureResponse, error) {
resp := &spi.ConfigureResponse{}
config := &configuration{}
hclTree, err := hcl.Parse(req.Configuration)
if err != nil {
err := fmt.Errorf("Error parsing custom keymanager configuration: %s", err)
return resp, err
}
err = hcl.DecodeObject(&config, hclTree)
if err != nil {
err := fmt.Errorf("Error decoding custom keymanager configuration: %v", err)
return resp, err
}
if err := m.configure(config); err != nil {
return nil, err
}
return &spi.ConfigureResponse{}, nil
}
func (m *KeyManager) configure(config *configuration) error {
cfg := crypto11.PKCS11Config{
Path: config.HSMPath,
TokenLabel: config.TokenLabel,
Pin: config.UserPin,
}
ctx, err := crypto11.Configure(&cfg)
if err != nil {
return err
}
slots, err := ctx.GetSlotList(true)
if err != nil {
return err
}
if len(slots) == 1 {
return errors.New("slots aren't presented on module")
}
session, err := ctx.OpenSession(slots[0], pkcs11.CKF_SERIAL_SESSION|pkcs11.CKF_RW_SESSION)
if err != nil {
return err
}
m.ctx = ctx
m.config = config
m.session = session
m.slotID = slots[0]
return nil
}
func (m *KeyManager) GetPluginInfo(ctx context.Context, req *spi.GetPluginInfoRequest) (*spi.GetPluginInfoResponse, error) {
return &spi.GetPluginInfoResponse{}, nil
}
func (m *KeyManager) GenerateKey(ctx context.Context, req *keymanager.GenerateKeyRequest) (*keymanager.GenerateKeyResponse, error) {
if req.KeyId == "" {
return nil, errors.New("key id is required")
}
if req.KeyType == keymanager.KeyType_UNSPECIFIED_KEY_TYPE {
return nil, errors.New("key type is required")
}
if m.IsKeyOnHsm(req.KeyId) {
err := m.deleteKey(req.KeyId, req.KeyType)
if err != nil {
return nil, err
}
}
pubKey, err := m.generateKey(req.KeyId, req.KeyType)
if err != nil {
return nil, err
}
return &keymanager.GenerateKeyResponse{
PublicKey: pubKey,
}, nil
}
func (m *KeyManager) GetPublicKey(ctx context.Context, req *keymanager.GetPublicKeyRequest) (*keymanager.GetPublicKeyResponse, error) {
if req.KeyId == "" {
return nil, errors.New("key id is required")
}
pubKey, err := m.findKeyOnHSM(req.KeyId)
if err != nil {
return nil, err
}
return &keymanager.GetPublicKeyResponse{
PublicKey: pubKey,
}, nil
}
func (m *KeyManager) GetPublicKeys(ctx context.Context, req *keymanager.GetPublicKeysRequest) (*keymanager.GetPublicKeysResponse, error) {
pubKeys, err := m.getPublicKeys()
if err != nil {
return nil, err
}
return &keymanager.GetPublicKeysResponse{
PublicKeys: pubKeys,
}, nil
}
func (m *KeyManager) SignData(ctx context.Context, req *keymanager.SignDataRequest) (*keymanager.SignDataResponse, error) {
if req.KeyId == "" {
return nil, errors.New("key id is required")
}
if req.SignerOpts == nil {
return nil, errors.New("signer opts is required")
}
var signerOpts crypto.SignerOpts
switch opts := req.SignerOpts.(type) {
case *keymanager.SignDataRequest_HashAlgorithm:
if opts.HashAlgorithm == keymanager.HashAlgorithm_UNSPECIFIED_HASH_ALGORITHM {
return nil, errors.New("hash algorithm is required")
}
signerOpts = crypto.Hash(opts.HashAlgorithm)
case *keymanager.SignDataRequest_PssOptions:
if opts.PssOptions == nil {
return nil, errors.New("PSS options are nil")
}
if opts.PssOptions.HashAlgorithm == keymanager.HashAlgorithm_UNSPECIFIED_HASH_ALGORITHM {
return nil, errors.New("hash algorithm is required")
}
signerOpts = &rsa.PSSOptions{
SaltLength: int(opts.PssOptions.SaltLength),
Hash: crypto.Hash(opts.PssOptions.HashAlgorithm),
}
default:
return nil, errors.New("unsupported signer opts type")
}
return m.signByHsmKey(req, signerOpts)
}
func main() {
plugin.Serve(&plugin.ServeConfig{
Plugins: map[string]plugin.Plugin{
"hsmkeymanager": &keymanager.GRPCPlugin{ServerImpl: New()},
},
HandshakeConfig: keymanager.Handshake,
GRPCServer: plugin.DefaultGRPCServer,
})
}