-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
executable file
·103 lines (89 loc) · 2.81 KB
/
server.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
package main
import (
"github.com/miekg/pkcs11"
"github.com/jschintag/notary-yubikey-adapter/yubikey"
"github.com/jschintag/notary/trustmanager/pkcs11/externalstore"
)
type ESServer struct {
}
var (
ks *yubikey.KeyStore = yubikey.NewKeyStore()
)
func NewServer() externalstore.ESServer {
return new(ESServer)
}
func (s *ESServer) Name(req externalstore.ESNameReq, res *externalstore.ESNameRes) error {
res.Name = ks.Name()
return nil
}
func (s *ESServer) AddECDSAKey(req externalstore.ESAddECDSAKeyReq, res *externalstore.ESAddECDSAKeyRes) error {
session := pkcs11.SessionHandle(req.Session)
privKey, err := externalstore.ESPrivateKeyToPrivateKey(req.PrivateKey)
if err != nil {
return err
}
return ks.AddECDSAKey(session, privKey, req.Slot, req.Pass, req.Role)
}
func (s *ESServer) GetECDSAKey(req externalstore.ESGetECDSAKeyReq, res *externalstore.ESGetECDSAKeyRes) error {
session := pkcs11.SessionHandle(req.Session)
pubKey, role, err := ks.GetECDSAKey(session, req.Slot, req.Pass)
if err != nil {
return err
}
res.PublicKey = externalstore.NewESPublicKey(pubKey)
res.Role = role
return nil
}
func (s *ESServer) Sign(req externalstore.ESSignReq, res *externalstore.ESSignRes) error {
session := pkcs11.SessionHandle(req.Session)
result, err := ks.Sign(session, req.Slot, req.Pass, req.Payload)
if err != nil {
return err
}
res.Result = result
return nil
}
func (s *ESServer) HardwareRemoveKey(req externalstore.ESHardwareRemoveKeyReq, res *externalstore.ESHardwareRemoveKeyRes) error {
session := pkcs11.SessionHandle(req.Session)
return ks.HardwareRemoveKey(session, req.Slot, req.Pass, req.KeyID)
}
func (s *ESServer) HardwareListKeys(req externalstore.ESHardwareListKeysReq, res *externalstore.ESHardwareListKeysRes) error {
session := pkcs11.SessionHandle(req.Session)
keys, err := ks.HardwareListKeys(session)
if err != nil {
return err
}
res.Keys = keys
return nil
}
func (s *ESServer) GetNextEmptySlot(req externalstore.ESGetNextEmptySlotReq, res *externalstore.ESGetNextEmptySlotRes) error {
session := pkcs11.SessionHandle(req.Session)
slot, err := ks.GetNextEmptySlot(session)
if err != nil {
return err
}
res.Slot = slot
return nil
}
func (s *ESServer) SetupHSMEnv(req externalstore.ESSetupHSMEnvReq, res *externalstore.ESSetupHSMEnvRes) error {
session, err := ks.SetupHSMEnv()
if err != nil {
return err
}
res.Session = uint(session)
return nil
}
func (s *ESServer) Cleanup(req externalstore.ESCleanupReq, _ *externalstore.ESCleanupReq) error {
session := pkcs11.SessionHandle(req.Session)
ks.CloseSession(session)
return nil
}
func (s *ESServer) NeedLogin(req externalstore.ESNeedLoginReq, res *externalstore.ESNeedLoginRes) error {
needed, userFlag, err := ks.NeedLogin(req.Function_ID)
if err != nil {
return err
}
res.NeedLogin = needed
res.UserFlag = userFlag
return nil
}