|
| 1 | +package awskms |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto" |
| 6 | + "crypto/rand" |
| 7 | + "crypto/rsa" |
| 8 | + "crypto/sha1" |
| 9 | + "crypto/sha256" |
| 10 | + "encoding/pem" |
| 11 | + "fmt" |
| 12 | + "hash" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/aws/aws-sdk-go-v2/service/kms" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + |
| 18 | + "go.step.sm/crypto/kms/apiv1" |
| 19 | + "go.step.sm/crypto/pemutil" |
| 20 | +) |
| 21 | + |
| 22 | +func TestCreateDecrypter(t *testing.T) { |
| 23 | + key, err := pemutil.ParseKey([]byte(rsaPublicKey)) |
| 24 | + require.NoError(t, err) |
| 25 | + require.IsType(t, &rsa.PublicKey{}, key) |
| 26 | + rsaKey := key.(*rsa.PublicKey) |
| 27 | + |
| 28 | + k := &KMS{client: &MockClient{ |
| 29 | + getPublicKey: func(ctx context.Context, input *kms.GetPublicKeyInput, opts ...func(*kms.Options)) (*kms.GetPublicKeyOutput, error) { |
| 30 | + block, _ := pem.Decode([]byte(rsaPublicKey)) |
| 31 | + return &kms.GetPublicKeyOutput{ |
| 32 | + KeyId: input.KeyId, |
| 33 | + PublicKey: block.Bytes, |
| 34 | + }, nil |
| 35 | + }, |
| 36 | + }} |
| 37 | + |
| 38 | + // fail with empty decryption key |
| 39 | + d, err := k.CreateDecrypter(&apiv1.CreateDecrypterRequest{ |
| 40 | + DecryptionKey: "", |
| 41 | + }) |
| 42 | + require.Error(t, err) |
| 43 | + require.Nil(t, d) |
| 44 | + |
| 45 | + // expect same public key to be returned |
| 46 | + d, err = k.CreateDecrypter(&apiv1.CreateDecrypterRequest{ |
| 47 | + DecryptionKey: "test", |
| 48 | + }) |
| 49 | + require.NoError(t, err) |
| 50 | + require.NotNil(t, d) |
| 51 | + require.True(t, rsaKey.Equal(d.Public())) |
| 52 | +} |
| 53 | + |
| 54 | +func TestDecrypterDecrypts(t *testing.T) { |
| 55 | + kms, pub := createTestKMS(t, 2048) |
| 56 | + fail1024KMS, _ := createTestKMS(t, 1024) |
| 57 | + |
| 58 | + // prepare encrypted contents |
| 59 | + encSHA256, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, pub, []byte("test"), nil) |
| 60 | + require.NoError(t, err) |
| 61 | + encSHA1, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, pub, []byte("test"), nil) |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + // create a decrypter, identified by "test-sha256", and check the public key |
| 65 | + d256, err := kms.CreateDecrypter(&apiv1.CreateDecrypterRequest{ |
| 66 | + DecryptionKey: "test-sha256", |
| 67 | + }) |
| 68 | + require.NoError(t, err) |
| 69 | + require.NotNil(t, d256) |
| 70 | + require.True(t, pub.Equal(d256.Public())) |
| 71 | + |
| 72 | + // create a decrypter, identified by "test-sha1", and check the public key |
| 73 | + d1, err := kms.CreateDecrypter(&apiv1.CreateDecrypterRequest{ |
| 74 | + DecryptionKey: "test-sha1", |
| 75 | + }) |
| 76 | + require.NoError(t, err) |
| 77 | + require.NotNil(t, d1) |
| 78 | + require.True(t, pub.Equal(d1.Public())) |
| 79 | + |
| 80 | + t.Run("ok/sha256", func(t *testing.T) { |
| 81 | + // successful decryption using OAEP with SHA-256 |
| 82 | + plain, err := d256.Decrypt(nil, encSHA256, &rsa.OAEPOptions{Hash: crypto.SHA256}) |
| 83 | + require.NoError(t, err) |
| 84 | + require.Equal(t, []byte("test"), plain) |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("ok/sha1", func(t *testing.T) { |
| 88 | + // successful decryption using OAEP with SHA-1 |
| 89 | + plain, err := d1.Decrypt(nil, encSHA1, &rsa.OAEPOptions{Hash: crypto.SHA1}) |
| 90 | + require.NoError(t, err) |
| 91 | + require.Equal(t, []byte("test"), plain) |
| 92 | + }) |
| 93 | + |
| 94 | + t.Run("ok/default-options", func(t *testing.T) { |
| 95 | + // successful decryption, defaulting to OAEP with SHA-256 |
| 96 | + plain, err := d256.Decrypt(nil, encSHA256, nil) |
| 97 | + require.NoError(t, err) |
| 98 | + require.Equal(t, []byte("test"), plain) |
| 99 | + }) |
| 100 | + |
| 101 | + t.Run("fail/hash", func(t *testing.T) { |
| 102 | + plain, err := d256.Decrypt(nil, encSHA256, &rsa.OAEPOptions{Hash: crypto.SHA384}) |
| 103 | + require.EqualError(t, err, `failed determining decryption algorithm: awskms does not support hash algorithm "SHA-384" with RSA-OAEP`) |
| 104 | + require.Empty(t, plain) |
| 105 | + }) |
| 106 | + |
| 107 | + t.Run("fail/label", func(t *testing.T) { |
| 108 | + plain, err := d256.Decrypt(nil, encSHA256, &rsa.OAEPOptions{Hash: crypto.SHA256, Label: []byte{1, 2, 3, 4}}) |
| 109 | + require.EqualError(t, err, "failed determining decryption algorithm: awskms does not support RSA-OAEP label") |
| 110 | + require.Empty(t, plain) |
| 111 | + }) |
| 112 | + |
| 113 | + t.Run("fail/hash-mismatch", func(t *testing.T) { |
| 114 | + plain, err := d256.Decrypt(nil, encSHA256, &rsa.OAEPOptions{Hash: crypto.SHA256, MGFHash: crypto.SHA384}) |
| 115 | + require.EqualError(t, err, `failed determining decryption algorithm: awskms does not support using different algorithms for hashing "SHA-256" and masking "SHA-384"`) |
| 116 | + require.Empty(t, plain) |
| 117 | + }) |
| 118 | + |
| 119 | + t.Run("fail/pkcs15", func(t *testing.T) { |
| 120 | + plain, err := d256.Decrypt(nil, encSHA256, &rsa.PKCS1v15DecryptOptions{}) |
| 121 | + require.EqualError(t, err, "failed determining decryption algorithm: awskms does not support PKCS #1 v1.5 decryption") |
| 122 | + require.Empty(t, plain) |
| 123 | + }) |
| 124 | + |
| 125 | + t.Run("fail/invalid-options", func(t *testing.T) { |
| 126 | + plain, err := d256.Decrypt(nil, encSHA256, struct{}{}) |
| 127 | + require.EqualError(t, err, "failed determining decryption algorithm: invalid decrypter options type struct {}") |
| 128 | + require.Empty(t, plain) |
| 129 | + }) |
| 130 | + |
| 131 | + t.Run("fail/invalid-key", func(t *testing.T) { |
| 132 | + failingDecrypter, err := fail1024KMS.CreateDecrypter(&apiv1.CreateDecrypterRequest{ |
| 133 | + DecryptionKey: "fail", |
| 134 | + }) |
| 135 | + require.NoError(t, err) |
| 136 | + |
| 137 | + _, err = failingDecrypter.Decrypt(nil, nil, nil) |
| 138 | + require.EqualError(t, err, "failed determining decryption algorithm: awskms does not support RSA public key size 1024") |
| 139 | + }) |
| 140 | +} |
| 141 | + |
| 142 | +func createTestKMS(t *testing.T, bitSize int) (*KMS, *rsa.PublicKey) { |
| 143 | + t.Helper() |
| 144 | + |
| 145 | + key, err := rsa.GenerateKey(rand.Reader, bitSize) |
| 146 | + require.NoError(t, err) |
| 147 | + |
| 148 | + k := &KMS{client: &MockClient{ |
| 149 | + getPublicKey: func(ctx context.Context, input *kms.GetPublicKeyInput, opts ...func(*kms.Options)) (*kms.GetPublicKeyOutput, error) { |
| 150 | + block, _ := pemutil.Serialize(key.Public()) |
| 151 | + return &kms.GetPublicKeyOutput{ |
| 152 | + KeyId: input.KeyId, |
| 153 | + PublicKey: block.Bytes, |
| 154 | + }, nil |
| 155 | + }, |
| 156 | + decrypt: func(ctx context.Context, params *kms.DecryptInput, optFns ...func(*kms.Options)) (*kms.DecryptOutput, error) { |
| 157 | + var h hash.Hash |
| 158 | + switch *params.KeyId { |
| 159 | + case "test-sha256": |
| 160 | + if params.EncryptionAlgorithm != "RSAES_OAEP_SHA_256" { |
| 161 | + return nil, fmt.Errorf("invalid encryption algorithm %q", params.EncryptionAlgorithm) |
| 162 | + } |
| 163 | + h = sha256.New() |
| 164 | + case "test-sha1": |
| 165 | + if params.EncryptionAlgorithm != "RSAES_OAEP_SHA_1" { |
| 166 | + return nil, fmt.Errorf("invalid encryption algorithm %q", params.EncryptionAlgorithm) |
| 167 | + } |
| 168 | + h = sha1.New() |
| 169 | + default: |
| 170 | + return nil, fmt.Errorf("invalid key ID %q", *params.KeyId) |
| 171 | + } |
| 172 | + |
| 173 | + dec, err := rsa.DecryptOAEP(h, nil, key, params.CiphertextBlob, nil) |
| 174 | + if err != nil { |
| 175 | + return nil, err |
| 176 | + } |
| 177 | + return &kms.DecryptOutput{ |
| 178 | + KeyId: params.KeyId, |
| 179 | + Plaintext: dec, |
| 180 | + }, nil |
| 181 | + }, |
| 182 | + }} |
| 183 | + |
| 184 | + return k, &key.PublicKey |
| 185 | +} |
0 commit comments