2017-06-06 20:02:54 +00:00
|
|
|
package transit
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2017-06-06 20:02:54 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-06-06 20:02:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTransit_ConfigSettings(t *testing.T) {
|
2018-06-12 16:24:12 +00:00
|
|
|
b, storage := createBackendWithSysView(t)
|
2017-06-06 20:02:54 +00:00
|
|
|
|
|
|
|
doReq := func(req *logical.Request) *logical.Response {
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err := b.HandleRequest(context.Background(), req)
|
2018-10-17 16:05:05 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
2017-06-06 20:02:54 +00:00
|
|
|
t.Fatalf("got err:\n%#v\nreq:\n%#v\n", err, *req)
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
doErrReq := func(req *logical.Request) {
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err := b.HandleRequest(context.Background(), req)
|
2017-06-06 20:02:54 +00:00
|
|
|
if err == nil {
|
|
|
|
if resp == nil || !resp.IsError() {
|
|
|
|
t.Fatalf("expected error; req:\n%#v\n", *req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// First create a key
|
|
|
|
req := &logical.Request{
|
|
|
|
Storage: storage,
|
|
|
|
Operation: logical.UpdateOperation,
|
2019-10-03 20:11:43 +00:00
|
|
|
Path: "keys/aes256",
|
2017-06-06 20:02:54 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"derived": true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
doReq(req)
|
|
|
|
|
2019-10-03 20:11:43 +00:00
|
|
|
req.Path = "keys/aes128"
|
|
|
|
req.Data["type"] = "aes128-gcm96"
|
|
|
|
doReq(req)
|
|
|
|
|
2017-06-06 20:02:54 +00:00
|
|
|
req.Path = "keys/ed"
|
|
|
|
req.Data["type"] = "ed25519"
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
delete(req.Data, "derived")
|
|
|
|
|
|
|
|
req.Path = "keys/p256"
|
|
|
|
req.Data["type"] = "ecdsa-p256"
|
|
|
|
doReq(req)
|
|
|
|
|
2019-10-03 16:32:43 +00:00
|
|
|
req.Path = "keys/p384"
|
|
|
|
req.Data["type"] = "ecdsa-p384"
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
req.Path = "keys/p521"
|
|
|
|
req.Data["type"] = "ecdsa-p521"
|
|
|
|
doReq(req)
|
|
|
|
|
2017-06-06 20:02:54 +00:00
|
|
|
delete(req.Data, "type")
|
|
|
|
|
2019-10-03 20:11:43 +00:00
|
|
|
req.Path = "keys/aes128/rotate"
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
req.Path = "keys/aes256/rotate"
|
2017-06-06 20:02:54 +00:00
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
req.Path = "keys/ed/rotate"
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
req.Path = "keys/p256/rotate"
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
|
2019-10-03 16:32:43 +00:00
|
|
|
req.Path = "keys/p384/rotate"
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
req.Path = "keys/p521/rotate"
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
doReq(req)
|
|
|
|
|
2019-10-03 20:11:43 +00:00
|
|
|
req.Path = "keys/aes256/config"
|
2017-06-06 20:02:54 +00:00
|
|
|
// Too high
|
|
|
|
req.Data["min_decryption_version"] = 7
|
|
|
|
doErrReq(req)
|
|
|
|
// Too low
|
|
|
|
req.Data["min_decryption_version"] = -1
|
|
|
|
doErrReq(req)
|
|
|
|
|
|
|
|
delete(req.Data, "min_decryption_version")
|
|
|
|
// Too high
|
|
|
|
req.Data["min_encryption_version"] = 7
|
|
|
|
doErrReq(req)
|
|
|
|
// Too low
|
|
|
|
req.Data["min_encryption_version"] = 7
|
|
|
|
doErrReq(req)
|
|
|
|
|
|
|
|
// Not allowed, cannot decrypt
|
|
|
|
req.Data["min_decryption_version"] = 3
|
|
|
|
req.Data["min_encryption_version"] = 2
|
|
|
|
doErrReq(req)
|
|
|
|
|
|
|
|
// Allowed
|
|
|
|
req.Data["min_decryption_version"] = 2
|
|
|
|
req.Data["min_encryption_version"] = 3
|
|
|
|
doReq(req)
|
2019-10-03 20:11:43 +00:00
|
|
|
req.Path = "keys/aes128/config"
|
|
|
|
doReq(req)
|
2017-06-06 20:02:54 +00:00
|
|
|
req.Path = "keys/ed/config"
|
|
|
|
doReq(req)
|
|
|
|
req.Path = "keys/p256/config"
|
|
|
|
doReq(req)
|
2019-10-03 16:32:43 +00:00
|
|
|
req.Path = "keys/p384/config"
|
|
|
|
doReq(req)
|
|
|
|
|
|
|
|
req.Path = "keys/p521/config"
|
|
|
|
doReq(req)
|
2017-06-06 20:02:54 +00:00
|
|
|
|
|
|
|
req.Data = map[string]interface{}{
|
|
|
|
"plaintext": "abcd",
|
2019-03-04 20:26:20 +00:00
|
|
|
"input": "abcd",
|
2017-06-06 20:02:54 +00:00
|
|
|
"context": "abcd",
|
|
|
|
}
|
|
|
|
|
|
|
|
maxKeyVersion := 5
|
2019-10-03 20:11:43 +00:00
|
|
|
key := "aes256"
|
2017-06-06 20:02:54 +00:00
|
|
|
|
|
|
|
testHMAC := func(ver int, valid bool) {
|
|
|
|
req.Path = "hmac/" + key
|
|
|
|
delete(req.Data, "hmac")
|
|
|
|
if ver == maxKeyVersion {
|
|
|
|
delete(req.Data, "key_version")
|
|
|
|
} else {
|
|
|
|
req.Data["key_version"] = ver
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
doErrReq(req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := doReq(req)
|
|
|
|
ct := resp.Data["hmac"].(string)
|
|
|
|
if strings.Split(ct, ":")[1] != "v"+strconv.Itoa(ver) {
|
|
|
|
t.Fatal("wrong hmac version")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Path = "verify/" + key
|
|
|
|
delete(req.Data, "key_version")
|
|
|
|
req.Data["hmac"] = resp.Data["hmac"]
|
|
|
|
doReq(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
testEncryptDecrypt := func(ver int, valid bool) {
|
|
|
|
req.Path = "encrypt/" + key
|
|
|
|
delete(req.Data, "ciphertext")
|
|
|
|
if ver == maxKeyVersion {
|
|
|
|
delete(req.Data, "key_version")
|
|
|
|
} else {
|
|
|
|
req.Data["key_version"] = ver
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
doErrReq(req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := doReq(req)
|
|
|
|
ct := resp.Data["ciphertext"].(string)
|
|
|
|
if strings.Split(ct, ":")[1] != "v"+strconv.Itoa(ver) {
|
|
|
|
t.Fatal("wrong encryption version")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Path = "decrypt/" + key
|
|
|
|
delete(req.Data, "key_version")
|
|
|
|
req.Data["ciphertext"] = resp.Data["ciphertext"]
|
|
|
|
doReq(req)
|
|
|
|
}
|
|
|
|
testEncryptDecrypt(5, true)
|
|
|
|
testEncryptDecrypt(4, true)
|
|
|
|
testEncryptDecrypt(3, true)
|
|
|
|
testEncryptDecrypt(2, false)
|
|
|
|
testHMAC(5, true)
|
|
|
|
testHMAC(4, true)
|
|
|
|
testHMAC(3, true)
|
|
|
|
testHMAC(2, false)
|
|
|
|
|
2019-10-03 20:11:43 +00:00
|
|
|
key = "aes128"
|
|
|
|
testEncryptDecrypt(5, true)
|
|
|
|
testEncryptDecrypt(4, true)
|
|
|
|
testEncryptDecrypt(3, true)
|
|
|
|
testEncryptDecrypt(2, false)
|
|
|
|
testHMAC(5, true)
|
|
|
|
testHMAC(4, true)
|
|
|
|
testHMAC(3, true)
|
|
|
|
testHMAC(2, false)
|
|
|
|
|
2017-06-06 20:02:54 +00:00
|
|
|
delete(req.Data, "plaintext")
|
|
|
|
req.Data["input"] = "abcd"
|
|
|
|
key = "ed"
|
|
|
|
testSignVerify := func(ver int, valid bool) {
|
|
|
|
req.Path = "sign/" + key
|
|
|
|
delete(req.Data, "signature")
|
|
|
|
if ver == maxKeyVersion {
|
|
|
|
delete(req.Data, "key_version")
|
|
|
|
} else {
|
|
|
|
req.Data["key_version"] = ver
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
doErrReq(req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := doReq(req)
|
|
|
|
ct := resp.Data["signature"].(string)
|
|
|
|
if strings.Split(ct, ":")[1] != "v"+strconv.Itoa(ver) {
|
|
|
|
t.Fatal("wrong signature version")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Path = "verify/" + key
|
|
|
|
delete(req.Data, "key_version")
|
|
|
|
req.Data["signature"] = resp.Data["signature"]
|
|
|
|
doReq(req)
|
|
|
|
}
|
|
|
|
testSignVerify(5, true)
|
|
|
|
testSignVerify(4, true)
|
|
|
|
testSignVerify(3, true)
|
|
|
|
testSignVerify(2, false)
|
|
|
|
testHMAC(5, true)
|
|
|
|
testHMAC(4, true)
|
|
|
|
testHMAC(3, true)
|
|
|
|
testHMAC(2, false)
|
|
|
|
|
|
|
|
delete(req.Data, "context")
|
|
|
|
key = "p256"
|
|
|
|
testSignVerify(5, true)
|
|
|
|
testSignVerify(4, true)
|
|
|
|
testSignVerify(3, true)
|
|
|
|
testSignVerify(2, false)
|
|
|
|
testHMAC(5, true)
|
|
|
|
testHMAC(4, true)
|
|
|
|
testHMAC(3, true)
|
|
|
|
testHMAC(2, false)
|
2019-10-03 16:32:43 +00:00
|
|
|
|
|
|
|
key = "p384"
|
|
|
|
testSignVerify(5, true)
|
|
|
|
testSignVerify(4, true)
|
|
|
|
testSignVerify(3, true)
|
|
|
|
testSignVerify(2, false)
|
|
|
|
testHMAC(5, true)
|
|
|
|
testHMAC(4, true)
|
|
|
|
testHMAC(3, true)
|
|
|
|
testHMAC(2, false)
|
|
|
|
|
|
|
|
key = "p521"
|
|
|
|
testSignVerify(5, true)
|
|
|
|
testSignVerify(4, true)
|
|
|
|
testSignVerify(3, true)
|
|
|
|
testSignVerify(2, false)
|
|
|
|
testHMAC(5, true)
|
|
|
|
testHMAC(4, true)
|
|
|
|
testHMAC(3, true)
|
|
|
|
testHMAC(2, false)
|
2017-06-06 20:02:54 +00:00
|
|
|
}
|