diff --git a/lang/funcs/crypto.go b/lang/funcs/crypto.go index 28074fb13..03b1572c2 100644 --- a/lang/funcs/crypto.go +++ b/lang/funcs/crypto.go @@ -6,12 +6,12 @@ import ( "crypto/sha1" "crypto/sha256" "crypto/sha512" - "crypto/x509" + "encoding/asn1" "encoding/base64" "encoding/hex" - "encoding/pem" "fmt" "hash" + "strings" uuidv5 "github.com/google/uuid" uuid "github.com/hashicorp/go-uuid" @@ -19,6 +19,7 @@ import ( "github.com/zclconf/go-cty/cty/function" "github.com/zclconf/go-cty/cty/gocty" "golang.org/x/crypto/bcrypt" + "golang.org/x/crypto/ssh" ) var UUIDFunc = function.New(&function.Spec{ @@ -152,27 +153,30 @@ var RsaDecryptFunc = function.New(&function.Spec{ b, err := base64.StdEncoding.DecodeString(s) if err != nil { - return cty.UnknownVal(cty.String), fmt.Errorf("failed to decode input %q: cipher text must be base64-encoded", s) + return cty.UnknownVal(cty.String), function.NewArgErrorf(0, "failed to decode input %q: cipher text must be base64-encoded", s) } - block, _ := pem.Decode([]byte(key)) - if block == nil { - return cty.UnknownVal(cty.String), fmt.Errorf("failed to parse key: no key found") - } - if block.Headers["Proc-Type"] == "4,ENCRYPTED" { - return cty.UnknownVal(cty.String), fmt.Errorf( - "failed to parse key: password protected keys are not supported. Please decrypt the key prior to use", - ) - } - - x509Key, err := x509.ParsePKCS1PrivateKey(block.Bytes) + rawKey, err := ssh.ParseRawPrivateKey([]byte(key)) if err != nil { - return cty.UnknownVal(cty.String), err + var errStr string + switch e := err.(type) { + case asn1.SyntaxError: + errStr = strings.ReplaceAll(e.Error(), "asn1: syntax error", "invalid ASN1 data in the given private key") + case asn1.StructuralError: + errStr = strings.ReplaceAll(e.Error(), "asn1: struture error", "invalid ASN1 data in the given private key") + default: + errStr = fmt.Sprintf("invalid private key: %s", e) + } + return cty.UnknownVal(cty.String), function.NewArgErrorf(1, errStr) + } + privateKey, ok := rawKey.(*rsa.PrivateKey) + if !ok { + return cty.UnknownVal(cty.String), function.NewArgErrorf(1, "invalid private key type %t", rawKey) } - out, err := rsa.DecryptPKCS1v15(nil, x509Key, b) + out, err := rsa.DecryptPKCS1v15(nil, privateKey, b) if err != nil { - return cty.UnknownVal(cty.String), err + return cty.UnknownVal(cty.String), fmt.Errorf("failed to decrypt: %s", err) } return cty.StringVal(string(out)), nil diff --git a/lang/funcs/crypto_test.go b/lang/funcs/crypto_test.go index a79b67f17..279777743 100644 --- a/lang/funcs/crypto_test.go +++ b/lang/funcs/crypto_test.go @@ -370,58 +370,67 @@ func TestRsaDecrypt(t *testing.T) { Ciphertext cty.Value Privatekey cty.Value Want cty.Value - Err bool + Err string }{ // Base-64 encoded cipher decrypts correctly { cty.StringVal(CipherBase64), cty.StringVal(PrivateKey), cty.StringVal("message"), - false, + "", + }, + // OpenSSH key format + { + cty.StringVal(CipherBase64), + cty.StringVal(OpenSSHPrivateKey), + cty.StringVal("message"), + "", }, // Wrong key { cty.StringVal(CipherBase64), cty.StringVal(WrongPrivateKey), cty.UnknownVal(cty.String), - true, + "failed to decrypt: crypto/rsa: decryption error", }, // Bad key { cty.StringVal(CipherBase64), - cty.StringVal("bad key"), + cty.StringVal(BadPrivateKey), cty.UnknownVal(cty.String), - true, + "invalid ASN1 data in the given private key: data truncated", }, // Empty key { cty.StringVal(CipherBase64), cty.StringVal(""), cty.UnknownVal(cty.String), - true, + "invalid private key: ssh: no key found", }, - // Bad cipher + // Bad ciphertext { - cty.StringVal("bad cipher"), + cty.StringVal("bad"), cty.StringVal(PrivateKey), cty.UnknownVal(cty.String), - true, + `failed to decode input "bad": cipher text must be base64-encoded`, }, - // Empty cipher + // Empty ciphertext { cty.StringVal(""), cty.StringVal(PrivateKey), cty.UnknownVal(cty.String), - true, + "failed to decrypt: crypto/rsa: decryption error", }, } for _, test := range tests { t.Run(fmt.Sprintf("RsaDecrypt(%#v, %#v)", test.Ciphertext, test.Privatekey), func(t *testing.T) { got, err := RsaDecrypt(test.Ciphertext, test.Privatekey) - if test.Err { + if test.Err != "" { if err == nil { t.Fatal("succeeded; want error") + } else if err.Error() != test.Err { + t.Fatalf("wrong error\ngot: %s\nwant: %s", err.Error(), test.Err) } return } else if err != nil { @@ -699,6 +708,35 @@ OLlRAoGBAIZ5Uv4Z3s8O7WKXXUe/lq6j7vfiVkR1NW/Z/WLKXZpnmvJ7FgxN4e56 RXT7GwNQHIY8eDjDnsHxzrxd+raOxOZeKcMHj3XyjCX3NHfTscnsBPAGYpY/Wxzh T8UYnFu6RzkixElTf2rseEav7rkdKkI3LAeIZy7B0HulKKsmqVQ7 -----END RSA PRIVATE KEY----- +` + OpenSSHPrivateKey = ` +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn +NhAAAAAwEAAQAAAQEAgUElV5mwqkloIrM8ZNZ72gSCcnSJt7+/Usa5G+D15YQUAdf9c1zE +ekTfHgDP+04nw/uFNFaE5v1RbHaPxhZYVg5ZErNCa/hzn+x10xzcepeS3KPVXcxae4MR0B +EegvqZqJzN9loXsNL/c3H/B+2Gle3hTxjlWFb3F5qLgR+4Mf4ruhER1v6eHQa/nchi03MB +pT4UeJ7MrL92hTJYLdpSyCqmr8yjxkKJDVC2uRrr+sTSxfh7r6v24u/vp/QTmBIAlNPgad +VAZw17iNNb7vjV7Gwl/5gHXonCUKURaV++dBNLrHIZpqcAM8wHRph8mD1EfL9hsz77pHew +xolBATV+7QAAA7jbhEFk24RBZAAAAAdzc2gtcnNhAAABAQCBQSVXmbCqSWgiszxk1nvaBI +JydIm3v79Sxrkb4PXlhBQB1/1zXMR6RN8eAM/7TifD+4U0VoTm/VFsdo/GFlhWDlkSs0Jr ++HOf7HXTHNx6l5Lco9VdzFp7gxHQER6C+pmonM32Whew0v9zcf8H7YaV7eFPGOVYVvcXmo +uBH7gx/iu6ERHW/p4dBr+dyGLTcwGlPhR4nsysv3aFMlgt2lLIKqavzKPGQokNULa5Guv6 +xNLF+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