-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Interpolation functions rsaencrypt and rsadecrypt #16647
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,15 @@ import ( | |
"bytes" | ||
"compress/gzip" | ||
"crypto/md5" | ||
"crypto/rsa" | ||
"crypto/sha1" | ||
"crypto/sha256" | ||
"crypto/sha512" | ||
"crypto/x509" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"encoding/json" | ||
"encoding/pem" | ||
"fmt" | ||
"io/ioutil" | ||
"math" | ||
|
@@ -103,6 +106,7 @@ func Funcs() map[string]ast.Function { | |
"pow": interpolationFuncPow(), | ||
"uuid": interpolationFuncUUID(), | ||
"replace": interpolationFuncReplace(), | ||
"rsadecrypt": interpolationFuncRsaDecrypt(), | ||
"sha1": interpolationFuncSha1(), | ||
"sha256": interpolationFuncSha256(), | ||
"sha512": interpolationFuncSha512(), | ||
|
@@ -1657,3 +1661,43 @@ func interpolationFuncAbs() ast.Function { | |
}, | ||
} | ||
} | ||
|
||
// interpolationFuncRsaDecrypt implements the "rsadecrypt" function that does | ||
// RSA decryption. | ||
func interpolationFuncRsaDecrypt() ast.Function { | ||
return ast.Function{ | ||
ArgTypes: []ast.Type{ast.TypeString, ast.TypeString}, | ||
ReturnType: ast.TypeString, | ||
Callback: func(args []interface{}) (interface{}, error) { | ||
s := args[0].(string) | ||
key := args[1].(string) | ||
|
||
b, err := base64.StdEncoding.DecodeString(s) | ||
if err != nil { | ||
return "", fmt.Errorf("Failed to decode input %q: cipher text must be base64-encoded", key) | ||
} | ||
|
||
block, _ := pem.Decode([]byte(key)) | ||
if block == nil { | ||
return "", fmt.Errorf("Failed to read key %q: no key found", key) | ||
} | ||
if block.Headers["Proc-Type"] == "4,ENCRYPTED" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like you take a different approach to load the key in this function vs. the one below. In this case we check to see if the key is encrypted and, I think, implicitly require it to be RSA by using Does this work differently than I think here, or is there a reason why the two should be different? |
||
return "", fmt.Errorf( | ||
"Failed to read key %q: password protected keys are\n"+ | ||
"not supported. Please decrypt the key prior to use.", key) | ||
} | ||
|
||
x509Key, err := x509.ParsePKCS1PrivateKey(block.Bytes) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
out, err := rsa.DecryptPKCS1v15(nil, x509Key, b) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(out), nil | ||
}, | ||
} | ||
} |
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.
Assuming base64 for the ciphertext seems reasonable since it'll probably not be valid UTF-8, but in that case I'd expect
rsaencrypt
to be symmetrical and produce a base64 result as output, which it doesn't seem to. Are these asymmetrical for a reason?