-
Notifications
You must be signed in to change notification settings - Fork 18k
crypto/x509: IsEncryptedPEMBlock returns false on valid encrypted keys. ParseRawPrivateKeyWithPassphrase fails on PKCS8 format encrypted key. #41949
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
Comments
cc @FiloSottile |
Related issue #8860 I wrote a small example showing how we can get the decryption algorithm info from the encrypted PEM: We can set the Update: nvm, the go/src/crypto/x509/pem_decrypt.go Lines 79 to 96 in 8cd75f3
It also can't accept a separate salt. It just takes the first 8 bytes of the IV as the salt. |
@FiloSottile @toothrot I just created a quick and dirty version of how it could detect and decrypt the private key: One major issue is that Since Another issue is how to pass the information about which KDF to use to the
Note: Even if
|
We can address this confusion with better docs in the deprecation message. |
Change https://golang.org/cl/263181 mentions this issue: |
@FiloSottile I submitted a PR to add a warning in the docs for each of those 3 functions. Update: Actually reading this https://golang.org/doc/contribute.html#ref_issues it seems the issues on extensions are also tracked on this repo. I have changed the PR comment to |
The existing documentation does not mention the exact meaning of "PEM encryption". So add a note to clarify that it is referring to RFC 1423 and that the functions are not meant to support any newer standard like PKCS golang#8. Updates golang#41949
Change https://golang.org/cl/264159 mentions this issue: |
@FiloSottile but what about |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
YES
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Example with the command to use to generate the keys and the keys themselves:
https://play.golang.org/p/F2nUIO_S6hT
Called
x509.IsEncryptedPEMBlock
onpem.Block
s created usingpem.Decode
.pem.Decode
is called on valid encrypted RSA private keys generated using the following commands:ssh-keygen -m PEM -t rsa -b 4096 -C 'foobar@example.com'
ssh-keygen -m PKCS8 -t rsa -b 4096 -C 'foobar@example.com'
ssh-keygen -m RFC4716 -t rsa -b 4096 -C 'foobar@example.com'
Also called
ssh.ParseRawPrivateKeyWithPassphrase
on each of those keys.What did you expect to see?
The
x509.IsEncryptedPEMBlock
function should report true in all the cases given in the example.The
ssh.ParseRawPrivateKeyWithPassphrase
should succeed on thePKCS8
key instead of failing as it does in the example.Note that
ssh-keygen -yf mykey
is able to detect that the file is a valid encrypted key and decrypt it given the correct password in all the 3 cases. SoIsEncryptedPEMBlock
andParseRawPrivateKeyWithPassphrase
should be able to handle them as well.What did you see instead?
x509.IsEncryptedPEMBlock
incorrectly returns false when given thepem.Block
s of thePKCS8
andRFC4716
keys.ssh-keygen
lets you specify the format for the key file using the-m
flag:https://www.man7.org/linux/man-pages/man1/ssh-keygen.1.html
There are 3 supported formats:
PEM
,PKCS8
andRFC4716
.x509.IsEncryptedPEMBlock
only reports correctly on keys generated usingPEM
. This is because keys generated usingPKCS8
andRFC4716
no longer have headers that indicate that the data is encrypted and the decryption algorithm to use.x509.IsEncryptedPEMBlock
checks for those headers in order to determine whether the data is encrypted:go/src/crypto/x509/pem_decrypt.go
Lines 99 to 102 in 5b509d9
Interestingly the
ssh.ParseRawPrivateKeyWithPassphrase
function fails onPKCS8
but is able to handleRFC4716
because of this special case: https://github.com/golang/crypto/blob/master/ssh/keys.go#L1156-L1158I have tried the example with
go version go1.15.2 darwin/amd64
and the latestgolang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee
on my Macbook PromacOS Catalina 10.15.6
The text was updated successfully, but these errors were encountered: