-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
aesbackend.go
239 lines (195 loc) · 5.1 KB
/
aesbackend.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
/*
* Copyright (C) 2020 Sergio Rubio
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Sergio Rubio <sergio@rubio.im>
*/
package cfg
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/json"
"errors"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"golang.org/x/crypto/scrypt"
)
// PasswordEnvVar defines the environment variable name that should
// contain the configuration password.
const PasswordEnvVar = "BEEHIVE_CONFIG_PASSWORD"
// EncryptedHeaderPrefix is added to the encrypted configuration
// to make it possible to detect it's an encrypted configuration file
const EncryptedHeaderPrefix = "beehiveconf+"
// AESBackend symmetrically encrypts the configuration file using AES-GCM
type AESBackend struct{}
// NewAESBackend creates the backend.
//
// Given the password is required to encrypt/decrypt the configuration, if the
// URL passed doesn't have a password or PasswordEnvVar is not defined,
// it'll return an error.
func NewAESBackend(u *url.URL) (*AESBackend, error) {
if _, err := getPassword(u); err != nil {
return nil, err
}
return &AESBackend{}, nil
}
// IsEncrypted returns true and no error if the configuration is encrypted
//
// If the error returned is not nil, an error was returned while opening or
// reading the file.
func IsEncrypted(u *url.URL) (bool, error) {
f, err := os.Open(u.Path)
if err != nil {
return false, err
}
defer f.Close()
b := make([]byte, 12)
_, err = f.Read(b)
if err != nil {
return false, err
}
if string(b) != EncryptedHeaderPrefix {
return false, nil
}
return true, nil
}
// Load configuration file from the given URL and decrypt it
func (b *AESBackend) Load(u *url.URL) (*Config, error) {
config := &Config{url: u}
if !exist(u.Path) {
return config, nil
}
ciphertext, err := ioutil.ReadFile(u.Path)
if err != nil {
return nil, err
}
ftype := ciphertext[0:12]
if string(ftype) != EncryptedHeaderPrefix {
return nil, errors.New("encrypted configuration header not valid")
}
p, err := getPassword(u)
if err != nil {
return nil, err
}
plaintext, err := decrypt(ciphertext[12:], []byte(p))
if err != nil {
return nil, err
}
err = json.Unmarshal(plaintext, config)
if err != nil {
return nil, err
}
config.backend = b
config.url = u
return config, nil
}
// Save encrypts then saves the configuration
func (b *AESBackend) Save(config *Config) error {
u := config.URL()
cfgDir := filepath.Dir(u.Path)
if !exist(cfgDir) {
err := os.MkdirAll(cfgDir, 0755)
if err != nil {
return err
}
}
j, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
p, err := getPassword(config.URL())
if err != nil {
return err
}
ciphertext, err := encrypt(j, []byte(p))
if err != nil {
return err
}
marked := []byte(EncryptedHeaderPrefix)
err = ioutil.WriteFile(u.Path, append(marked, ciphertext...), 0644)
return err
}
func encrypt(data, key []byte) ([]byte, error) {
key, salt, err := deriveKey(key, nil)
if err != nil {
return nil, err
}
blockCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(blockCipher)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = rand.Read(nonce); err != nil {
return nil, err
}
ciphertext := gcm.Seal(nonce, nonce, data, nil)
ciphertext = append(ciphertext, salt...)
return ciphertext, nil
}
func decrypt(data, key []byte) ([]byte, error) {
salt, data := data[len(data)-32:], data[:len(data)-32]
key, _, err := deriveKey(key, salt)
if err != nil {
return nil, err
}
blockCipher, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(blockCipher)
if err != nil {
return nil, err
}
nonce, ciphertext := data[:gcm.NonceSize()], data[gcm.NonceSize():]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}
func deriveKey(password, salt []byte) ([]byte, []byte, error) {
if salt == nil {
salt = make([]byte, 32)
if _, err := rand.Read(salt); err != nil {
return nil, nil, err
}
}
key, err := scrypt.Key(password, salt, 32768, 8, 1, 32)
if err != nil {
return nil, nil, err
}
return key, salt, nil
}
func getPassword(u *url.URL) (string, error) {
p := os.Getenv(PasswordEnvVar)
if p != "" {
return p, nil
}
if u != nil && u.User != nil {
p = u.User.Username()
if p != "" {
return p, nil
}
}
return "", errors.New("password to encrypt or decrypt the config file not available")
}