-
Notifications
You must be signed in to change notification settings - Fork 1
/
trusted.go
270 lines (230 loc) · 7.38 KB
/
trusted.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package trusted
import (
errors "errors"
log "log"
rand "crypto/rand"
rsa "crypto/rsa"
x509 "crypto/x509"
base64 "encoding/base64"
hex "encoding/hex"
pem "encoding/pem"
bigmath "math/big"
jwt "github.com/pascaldekloe/jwt"
ssh "golang.org/x/crypto/ssh"
grpc_example "github.com/jeroenvanmaanen/archetype-go-axon/src/pkg/grpc/example"
axon_utils "github.com/jeroenvanmaanen/dendrite/src/pkg/axon_utils"
)
var keyManagers map[string]string
var trustedKeys map[string]string
var privateKey rsa.PrivateKey
var privateKeyName string
func SetPrivateKey(name string, pemString string) error {
var e error
encodedPublicKey := trustedKeys[name]
log.Printf("Trusted: Set private key: public key: %v: %v", name, encodedPublicKey)
publicKey, e := getTrustedKey(name)
if e != nil {
log.Printf("Trusted: Set private key: Unable to parse public key: %v", e)
return errors.New("invalid public key")
}
givenPrivateKey, e := ParsePrivateKey(pemString)
if e != nil {
return e
}
signer, e := ssh.NewSignerFromKey(givenPrivateKey)
if e != nil {
log.Printf("Trusted: Set private key: Unable to create signer from private key: %v", e)
return errors.New("invalid private key")
}
nonce := make([]byte, 64)
_, _ = rand.Reader.Read(nonce)
hexNonce := hex.EncodeToString(nonce)
log.Printf("Nonce: %v", hexNonce)
signature, e := signer.Sign(rand.Reader, nonce)
if e != nil {
log.Printf("Trusted: Set private key: Unable to sign nonce: %v", e)
return errors.New("invalid private key")
}
e = publicKey.Verify(nonce, signature)
if e != nil {
log.Printf("Trusted: Set private key: Unable to verify signature of nonce: %v", e)
return errors.New("invalid private key")
}
privateKey = *givenPrivateKey
privateKeyName = name
log.Printf("Trusted: Set private key: private key: %v", name)
return nil
}
func AddTrustedKey(request *grpc_example.TrustedKeyRequest, nonce []byte, clientConnection *axon_utils.ClientConnection) error {
name := request.PublicKey.Name
publicKey := request.PublicKey.PublicKey
protoSignature := request.Signature
signatureName := protoSignature.SignatureName
isKeyManager := request.IsKeyManager
_, e := parsePublicKey(publicKey)
if e != nil {
log.Printf("Trusted: Add trusted key: Unable to parse new trusted key: %v", e)
return errors.New("invalid trusted key")
}
signatureKey, e := GetKeyManagerKey(signatureName)
if e != nil {
log.Printf("Trusted: Add trusted key: Unable to parse signature key: %v", e)
return errors.New("invalid trusted key")
}
signature := ssh.Signature{
Format: protoSignature.Format,
Blob: protoSignature.Blob,
Rest: protoSignature.Rest,
}
e = signatureKey.Verify(nonce, &signature)
if e != nil {
log.Printf("Trusted: Add trusted key: Unable to verify signature of nonce: %v", e)
return errors.New("invalid trusted key")
}
trustedKeys[name] = publicKey
if isKeyManager {
command := grpc_example.RegisterKeyManagerCommand{
PublicKey: request.PublicKey,
}
e = axon_utils.SendCommand("RegisterKeyManagerCommand", &command, clientConnection)
if e != nil {
log.Printf("Trusted: Error when sending RegisterKeyManagerCommand: %v", e)
}
} else {
command := grpc_example.RegisterTrustedKeyCommand{
PublicKey: request.PublicKey,
}
e = axon_utils.SendCommand("RegisterTrustedKeyCommand", &command, clientConnection)
if e != nil {
log.Printf("Trusted: Error when sending RegisterTrustedKeyCommand: %v", e)
}
}
log.Printf("Added public key: %v: %v", name, publicKey)
return nil
}
func UnsafeSetTrustedKey(publicKey *grpc_example.PublicKey) {
trustedKeys[publicKey.Name] = publicKey.PublicKey
}
func UnsafeSetKeyManager(publicKey *grpc_example.PublicKey) {
keyManagers[publicKey.Name] = publicKey.PublicKey
}
func GetKeyManagerKey(name string) (ssh.PublicKey, error) {
var e error
encodedPublicKey := keyManagers[name]
log.Printf("Trusted: Get key manager key: %v: %v", name, encodedPublicKey)
publicKey, e := parsePublicKey(encodedPublicKey)
return publicKey, e
}
func EncryptString(plainText string) (string, error) {
rsaPublicKey, e := GetRsaPublicKey()
if e != nil {
return "", e
}
log.Printf("Trusted: Encrypt string: RSA public key: %v", rsaPublicKey)
encryptedBytes, e := rsa.EncryptPKCS1v15(rand.Reader, rsaPublicKey, []byte(plainText))
if e != nil {
return "", e
}
return base64.RawStdEncoding.EncodeToString(encryptedBytes), nil
}
func DecryptString(cryptText string) (string, error) {
encryptedBytes, e := base64.RawStdEncoding.DecodeString(cryptText)
if e != nil {
return "", e
}
decryptedBytes, e := rsa.DecryptPKCS1v15(rand.Reader, &privateKey, encryptedBytes)
if e != nil {
return "", e
}
return string(decryptedBytes), nil
}
func getTrustedKey(name string) (ssh.PublicKey, error) {
var e error
encodedPublicKey := trustedKeys[name]
log.Printf("Trusted: Get trusted key: %v: %v", name, encodedPublicKey)
publicKey, e := parsePublicKey(encodedPublicKey)
return publicKey, e
}
func parsePublicKey(encodedPublicKey string) (ssh.PublicKey, error) {
publicKey, _, _, _, e := ssh.ParseAuthorizedKey([]byte("ssh-rsa " + encodedPublicKey))
if e != nil {
log.Printf("Trusted: Unable to parse public key: %v", e)
return nil, e
}
return publicKey, nil
}
func ParsePrivateKey(pemString string) (givenPrivateKey *rsa.PrivateKey, e error) {
privatePem, _ := pem.Decode([]byte(pemString))
if privatePem.Type != "PRIVATE KEY" && privatePem.Type != "RSA PRIVATE KEY" {
log.Printf("RSA private key is of the wrong type: %v", privatePem.Type)
return nil, errors.New("wrong PEM type")
}
privatePemBytes := privatePem.Bytes
if privatePemBytes == nil {
log.Printf("Trusted: Set private key: empty PEM")
return nil, errors.New("empty PEM")
}
var parsedKey interface{}
if parsedKey, e = x509.ParsePKCS1PrivateKey(privatePemBytes); e != nil {
if parsedKey, e = x509.ParsePKCS8PrivateKey(privatePemBytes); e != nil { // note this returns type `interface{}`
log.Printf("Trusted: Set private key: Unable to parse RSA private key: %v", e)
return nil, errors.New("invalid private key")
}
}
givenPrivateKey, ok := parsedKey.(*rsa.PrivateKey)
if !ok {
log.Printf("Type assertion for RSA private key failed")
return nil, errors.New("invalid private key")
}
return
}
func CreateJWT(claims jwt.Claims) (token string, e error) {
tokenBuffer, e := claims.RSASign("RS256", &privateKey)
token = string(tokenBuffer)
return
}
func GetRsaPublicKey() (*rsa.PublicKey, error) {
publicKey, e := parsePublicKey(trustedKeys[privateKeyName])
if e != nil {
return nil, e
}
buffer := publicKey.Marshal()
w := getInt(buffer)
keyType := string(buffer[4 : w+4])
if keyType != "ssh-rsa" {
return nil, errors.New("Not an ssh-rsa key: " + keyType)
}
buffer = buffer[w+4:]
w = getInt(buffer)
exponent := int(getBigInt(buffer[4 : w+4]).Int64())
buffer = buffer[w+4:]
w = getInt(buffer)
modulus := getBigInt(buffer[4 : w+4])
rsaPublicKey := rsa.PublicKey{
N: modulus,
E: exponent,
}
return &rsaPublicKey, e
}
func getInt(buffer []byte) (result uint32) {
result = 0
for i := 0; i < 4; i++ {
result = result*256 + uint32(buffer[i])
}
return
}
func getBigInt(buffer []byte) *bigmath.Int {
result := bigmath.NewInt(0)
for _, v := range buffer {
result.Mul(result, bigmath.NewInt(256))
result.Add(result, bigmath.NewInt(int64(uint32(v))))
}
return result
}
func GetTrustedKeys() map[string]string {
var result = make(map[string]string)
for name, key := range trustedKeys {
result[name] = key
}
return result
}