-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add chef-vault implementation #minor #101
Open
lanej
wants to merge
7
commits into
go-chef:master
Choose a base branch
from
lanej:vault
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
12ee3e0
fix: build warnings relating to missing formatting directives
lanej 1049521
feature(vault): initial commit
lanej cd77489
refactor(vault): remove redundant methods. add create and read tests
lanej a773f27
test(vault): verify deletes
lanej 16cab19
refactor(vault): update and decrypt with a test
lanej a45c966
test(crypto): verify encrypt/decrypt vault data
lanej 99bf053
test: remove extraneous print statement
lanej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package chef | ||
|
||
// https://github.com/golang/go/issues/23514 | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"encoding/base64" | ||
"io" | ||
) | ||
|
||
// NewCrypter en/decrypts data parsing using aes | ||
func newCrypter(key []byte) (cipher.AEAD, error) { | ||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cipher.NewGCM(block) | ||
} | ||
|
||
func iv() ([]byte, error) { | ||
nonce := make([]byte, 12) | ||
if _, err := io.ReadFull(rand.Reader, nonce); err != nil { | ||
return nil, err | ||
} | ||
return nonce[:], nil | ||
} | ||
|
||
func generateSecret() *[]byte { | ||
secret := make([]byte, 32) | ||
rand.Read(secret) | ||
return &secret | ||
} | ||
|
||
// EncryptValue data | ||
func EncryptValue(key, iv, plaintext []byte) (tag []byte, ciphertext []byte, encryptError error) { | ||
cipher, err := newCrypter(key) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
// https://sourcegraph.com/github.com/golang/go@52cc9e3762171bd45368b3280554bf12a63f23b2/-/blob/src/crypto/aes/aes_gcm.go#L120-131 | ||
out := cipher.Seal(make([]byte, 0), iv, plaintext, nil) | ||
|
||
return out[len(out)-cipher.Overhead():], out[:len(out)-cipher.Overhead()], nil | ||
} | ||
|
||
// DecryptValue data | ||
func DecryptValue(key, iv, tag, ciphertext []byte) ([]byte, error) { | ||
// https://sourcegraph.com/github.com/golang/go@52cc9e3762171bd45368b3280554bf12a63f23b2/-/blob/src/crypto/aes/aes_gcm.go#L155 | ||
cipher, err := newCrypter(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
macData := append(ciphertext, tag...) | ||
|
||
return cipher.Open(nil, iv, macData, nil) | ||
} | ||
|
||
// EncodeSharedSecret encrypts secret with the the specified and key returns a base64 encoded string | ||
func EncodeSharedSecret(key *rsa.PrivateKey, secret []byte) (string, error) { | ||
encryptedSecret, err := rsa.EncryptPKCS1v15(rand.Reader, &key.PublicKey, secret) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(encryptedSecret), nil | ||
} | ||
|
||
// DecodeSharedSecret returns the plaintext shared secret encrypted by the key | ||
func DecodeSharedSecret(key *rsa.PrivateKey, encodedSecret string) ([]byte, error) { | ||
// Decode encrypted shared secret | ||
encryptedSecret, err := base64.StdEncoding.DecodeString(encodedSecret) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Decrypt shared secret | ||
return rsa.DecryptPKCS1v15(rand.Reader, key, encryptedSecret) | ||
} |
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,81 @@ | ||
package chef | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"testing" | ||
) | ||
|
||
var aesDecryptionTests = []struct { | ||
cipher string | ||
data string | ||
key string | ||
iv string | ||
tag string | ||
expected string | ||
}{ | ||
{ | ||
"aes-256-gcm", | ||
"tg3UG+t43RfVf2hRaPEqU5Vwllu8Uw==\n", | ||
"3f5eccf62026458b8038a06daecff4a69640ce3c5e4585aace7243d5a7f01b62", | ||
"MQ2DVUphtxAwdTfo", | ||
"nsH3unmSGw9hcOG3KpYXpQ==\n", | ||
`{"json_wrapper":"bar"}`, | ||
}, | ||
} | ||
|
||
func TestDecrypt(t *testing.T) { | ||
for _, dt := range aesDecryptionTests { | ||
// auth_tag = Base64.decode64(tag) | ||
authTag, _ := base64.StdEncoding.DecodeString(dt.tag) | ||
// data = Base64.decode64(data) | ||
data, _ := base64.StdEncoding.DecodeString(dt.data) | ||
// iv = Base64.decode64(iv) | ||
iv, _ := base64.StdEncoding.DecodeString(dt.iv) | ||
|
||
// key = [key].pack('H*') | ||
key, _ := hex.DecodeString(dt.key) | ||
// key = OpenSSL::Digest::SHA256.digest(key) | ||
cryptKey := sha256.Sum256([]byte(key)) | ||
|
||
plaintext, err := DecryptValue(cryptKey[:], iv, authTag, data) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if string(plaintext) != dt.expected { | ||
t.Fatalf("Expected: %s, Actual: %s\n", dt.expected, plaintext) | ||
} | ||
} | ||
} | ||
|
||
func TestEncrypt(t *testing.T) { | ||
for _, dt := range aesDecryptionTests { | ||
// auth_tag = Base64.decode64(tag) | ||
authTag, _ := base64.StdEncoding.DecodeString(dt.tag) | ||
// data = Base64.decode64(data) | ||
data, _ := base64.StdEncoding.DecodeString(dt.data) | ||
// iv = Base64.decode64(iv) | ||
iv, _ := base64.StdEncoding.DecodeString(dt.iv) | ||
|
||
// key = [key].pack('H*') | ||
key, _ := hex.DecodeString(dt.key) | ||
// key = OpenSSL::Digest::SHA256.digest(key) | ||
cryptKey := sha256.Sum256([]byte(key)) | ||
|
||
tag, ciphertext, err := EncryptValue(cryptKey[:], iv, []byte(dt.expected)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !bytes.Equal(ciphertext, data) { | ||
t.Fatalf("ciphertext: [expected: %q, actual: %q]\n", hex.EncodeToString(data), hex.EncodeToString(ciphertext)) | ||
} | ||
|
||
if !bytes.Equal(tag, authTag) { | ||
t.Fatalf("tag: [expected: %q, actual: %q]\n", hex.EncodeToString(authTag), hex.EncodeToString(tag)) | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self. Figure out if the deleted error message is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok