@@ -39,8 +39,10 @@ func (*KeyService) Name() string {
39
39
// Keygrips returns a single slice of concatenated keygrip byteslices - one for
40
40
// each cryptographic key available on the keyservice.
41
41
func (p * KeyService ) Keygrips () ([][]byte , error ) {
42
+ p .mu .Lock ()
43
+ defer p .mu .Unlock ()
42
44
var grips [][]byte
43
- securityKeys , err := p .SecurityKeys ()
45
+ securityKeys , err := p .getSecurityKeys ()
44
46
if err != nil {
45
47
return nil , fmt .Errorf ("couldn't get security keys: %w" , err )
46
48
}
@@ -64,7 +66,9 @@ func (p *KeyService) Keygrips() ([][]byte, error) {
64
66
// HaveKey takes a list of keygrips, and returns a boolean indicating if any of
65
67
// the given keygrips were found, the found keygrip, and an error, if any.
66
68
func (p * KeyService ) HaveKey (keygrips [][]byte ) (bool , []byte , error ) {
67
- securityKeys , err := p .SecurityKeys ()
69
+ p .mu .Lock ()
70
+ defer p .mu .Unlock ()
71
+ securityKeys , err := p .getSecurityKeys ()
68
72
if err != nil {
69
73
return false , nil , fmt .Errorf ("couldn't get security keys: %w" , err )
70
74
}
@@ -90,7 +94,7 @@ func (p *KeyService) HaveKey(keygrips [][]byte) (bool, []byte, error) {
90
94
}
91
95
92
96
func (p * KeyService ) getPrivateKey (keygrip []byte ) (crypto.PrivateKey , error ) {
93
- securityKeys , err := p .SecurityKeys ()
97
+ securityKeys , err := p .getSecurityKeys ()
94
98
if err != nil {
95
99
return nil , fmt .Errorf ("couldn't get security keys: %w" , err )
96
100
}
@@ -110,7 +114,11 @@ func (p *KeyService) getPrivateKey(keygrip []byte) (crypto.PrivateKey, error) {
110
114
if err != nil {
111
115
return nil , fmt .Errorf ("couldn't get private key from slot" )
112
116
}
113
- return privKey , nil
117
+ pivGoPrivKey , ok := privKey .(* pivgo.ECDSAPrivateKey )
118
+ if ! ok {
119
+ return nil , fmt .Errorf ("unexpected private key type: %T" , privKey )
120
+ }
121
+ return & ECDHKey {mu : & p .mu , ECDSAPrivateKey : pivGoPrivKey }, nil
114
122
}
115
123
}
116
124
}
@@ -119,33 +127,39 @@ func (p *KeyService) getPrivateKey(keygrip []byte) (crypto.PrivateKey, error) {
119
127
120
128
// GetSigner returns a crypto.Signer associated with the given keygrip.
121
129
func (p * KeyService ) GetSigner (keygrip []byte ) (crypto.Signer , error ) {
130
+ p .mu .Lock ()
131
+ defer p .mu .Unlock ()
122
132
privKey , err := p .getPrivateKey (keygrip )
123
133
if err != nil {
124
134
return nil , fmt .Errorf ("couldn't get private key: %v" , err )
125
135
}
126
136
signingPrivKey , ok := privKey .(crypto.Signer )
127
137
if ! ok {
128
- return nil , fmt .Errorf ("private key is invalid type " )
138
+ return nil , fmt .Errorf ("private key is not a signer " )
129
139
}
130
140
return signingPrivKey , nil
131
141
}
132
142
133
143
// GetDecrypter returns a crypto.Decrypter associated with the given keygrip.
134
144
func (p * KeyService ) GetDecrypter (keygrip []byte ) (crypto.Decrypter , error ) {
145
+ p .mu .Lock ()
146
+ defer p .mu .Unlock ()
135
147
privKey , err := p .getPrivateKey (keygrip )
136
148
if err != nil {
137
149
return nil , fmt .Errorf ("couldn't get private key: %v" , err )
138
150
}
139
- ecdsaPrivKey , ok := privKey .(* pivgo. ECDSAPrivateKey )
151
+ decryptingPrivKey , ok := privKey .(crypto. Decrypter )
140
152
if ! ok {
141
- return nil , fmt .Errorf ("private key is invalid type " )
153
+ return nil , fmt .Errorf ("private key is not a decrypter " )
142
154
}
143
- return & ECDHKey { ecdsa : ecdsaPrivKey } , nil
155
+ return decryptingPrivKey , nil
144
156
}
145
157
146
158
// CloseAll closes all security keys without checking for errors.
147
159
// This should be called to clean up connections to `pcscd`.
148
160
func (p * KeyService ) CloseAll () {
161
+ p .mu .Lock ()
162
+ defer p .mu .Unlock ()
149
163
p .log .Debug ("closing security keys" , zap .Int ("count" , len (p .securityKeys )))
150
164
for _ , k := range p .securityKeys {
151
165
if err := k .Close (); err != nil {
0 commit comments