forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecret.go
130 lines (105 loc) · 3.15 KB
/
secret.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
/*
Copyright 2019 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package secret implements a authenticated encryption with associated data
// (AEAD) cipher to be used when symmetric is required in Teleport. The
// underlying cipher is AES-GCM.
package secret
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"encoding/json"
"io"
"github.com/gravitational/trace"
)
type sealedData struct {
Ciphertext []byte `json:"ciphertext"`
Nonce []byte `json:"nonce"`
}
// Key for the symmetric cipher.
type Key []byte
// NewKey generates a new key from a cryptographically secure pseudo-random
// number generator (CSPRNG).
func NewKey() (Key, error) {
k := make([]byte, 32)
_, err := io.ReadFull(rand.Reader, k)
if err != nil {
return nil, trace.Wrap(err)
}
return Key(k), nil
}
// ParseKey reads in an existing hex encoded key.
func ParseKey(k []byte) (Key, error) {
key, err := hex.DecodeString(string(k))
if err != nil {
return nil, trace.Wrap(err)
}
return Key(key), nil
}
// Seal will encrypt then authenticate the ciphertext.
func (k Key) Seal(plaintext []byte) ([]byte, error) {
block, err := aes.NewCipher([]byte(k))
if err != nil {
return nil, trace.Wrap(err)
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, trace.Wrap(err)
}
nonce := make([]byte, aesgcm.NonceSize())
_, err = io.ReadFull(rand.Reader, nonce)
if err != nil {
return nil, trace.Wrap(err)
}
ciphertext, err := json.Marshal(&sealedData{
Ciphertext: aesgcm.Seal(nil, nonce, plaintext, nil),
Nonce: nonce,
})
if err != nil {
return nil, trace.Wrap(err)
}
return ciphertext, nil
}
// Open will authenticate then decrypt the ciphertext.
func (k Key) Open(ciphertext []byte) ([]byte, error) {
var data sealedData
err := json.Unmarshal(ciphertext, &data)
if err != nil {
return nil, trace.Wrap(err)
}
block, err := aes.NewCipher(k)
if err != nil {
return nil, trace.Wrap(err)
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, trace.Wrap(err)
}
// Attempting to call aesgcm.Open with a invalid nonce will cause it to panic.
// To make sure that doesn't happen even handling invalid ciphertext
// (for example, for legacy secret package or attacker controlled data),
// reject invalid sized nonces.
if len(data.Nonce) != aesgcm.NonceSize() {
return nil, trace.BadParameter("invalid nonce sice, only %v supported", aesgcm.NonceSize())
}
plaintext, err := aesgcm.Open(nil, data.Nonce, data.Ciphertext, nil)
if err != nil {
return nil, trace.Wrap(err)
}
return plaintext, nil
}
// String returns the human-readable representation of the key.
func (k Key) String() string {
return hex.EncodeToString(k)
}