-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encrypt all files containing secrets
Fallback files and downloaded secrets will now be encrypted before touching the filesystem. PBKDF2 is used against the (token, project, config) to generate the key. AES-GCM is used with a random salt to encrypt the file.
- Loading branch information
1 parent
13ffe71
commit 78d6d0b
Showing
5 changed files
with
186 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
Copyright © 2020 Doppler <support@doppler.com> | ||
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. | ||
*/ | ||
|
||
// From https://gist.github.com/tscholl2/dc7dc15dc132ea70a98e8542fefffa28 | ||
|
||
package crypto | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"strings" | ||
|
||
"golang.org/x/crypto/pbkdf2" | ||
) | ||
|
||
func deriveKey(passphrase string, salt []byte) ([]byte, []byte, error) { | ||
if salt == nil { | ||
salt = make([]byte, 8) | ||
// http://www.ietf.org/rfc/rfc2898.txt | ||
// Salt. | ||
_, err := rand.Read(salt) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
} | ||
|
||
return pbkdf2.Key([]byte(passphrase), salt, 50000, 32, sha256.New), salt, nil | ||
} | ||
|
||
// Encrypt plaintext with a passphrase; uses pbkdf2 for key deriv and aes-gcm for encryption | ||
func Encrypt(passphrase string, plaintext []byte) (string, error) { | ||
key, salt, err := deriveKey(passphrase, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
iv := make([]byte, 12) | ||
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf | ||
// Section 8.2 | ||
_, err = rand.Read(iv) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
b, err := aes.NewCipher(key) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
aesgcm, err := cipher.NewGCM(b) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
data := aesgcm.Seal(nil, iv, plaintext, nil) | ||
return hex.EncodeToString(salt) + "-" + hex.EncodeToString(iv) + "-" + hex.EncodeToString(data), nil | ||
} | ||
|
||
// Decrypt ciphertext with a passphrase | ||
func Decrypt(passphrase string, ciphertext []byte) (string, error) { | ||
arr := strings.Split(string(ciphertext), "-") | ||
salt, err := hex.DecodeString(arr[0]) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
iv, err := hex.DecodeString(arr[1]) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
data, err := hex.DecodeString(arr[2]) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
key, _, err := deriveKey(passphrase, salt) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
b, err := aes.NewCipher(key) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
aesgcm, err := cipher.NewGCM(b) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
data, err = aesgcm.Open(nil, iv, data, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(data), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters