Skip to content
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

Add support for RSA-OAEP decryption #4

Merged
merged 6 commits into from
Mar 2, 2023
Merged

Add support for RSA-OAEP decryption #4

merged 6 commits into from
Mar 2, 2023

Conversation

hslatman
Copy link
Member

@hslatman hslatman commented Mar 2, 2023

Changes from mozilla-services#48 are included with this PR. Slightly adapted to fit with how we work with crypto.Decrypter.

Changes from mozilla-services#44 for decryption are included with this PR.

Changes from mozilla-services#48 are
included in this commit. Slightly adapted to fit with how we
work with `crypto.Decrypter`.
Currently the test suite still goes back as far as Go 1.12. This
failed the test.
@hslatman hslatman requested review from azazeal and maraino March 2, 2023 13:02
Copy link

@azazeal azazeal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but I have a few suggestions

decrypt.go Outdated Show resolved Hide resolved
decrypt.go Outdated Show resolved Hide resolved
decrypt_test.go Outdated Show resolved Hide resolved
decrypt_test.go Outdated Show resolved Hide resolved
// echo -n "This is a test" > test.txt
// - Generate PKCS #7 enveloped data encrypted using AES using
// openssl smime -encrypt -in test.txt -aes256 -outform pem certificate.pem
var RSAPKCS1v15EncryptedTestFixture = `
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the embed package could help you out here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repo is still tested against Go 1.12, because it's a fork. I've added a note instead to look into that when the version is upped to at least 1.16. We're getting closer to actually maintaining the fork for others to be used, so after that we can think about upgrading Go versions, I would say.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could put these in files under testdata and load them during TestMain or init in the test file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good idea, but I propose to keep it like this for a bit, as it's the format used by the upstream and I took their tests. There's some more cases not currently covered by tests, so I want to revisit this soon, but do it with embed at once.

@hslatman hslatman requested a review from azazeal March 2, 2023 15:35
Copy link

@maraino maraino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few details, we should consider to maintaining this ourselves in go.step.sm/crypto

decrypt.go Outdated Show resolved Hide resolved
decrypt.go Outdated Show resolved Hide resolved
decrypt.go Show resolved Hide resolved
@@ -317,7 +317,7 @@ func UnmarshalTestFixture(testPEMBlock string) TestFixture {
result.Input = derBlock.Bytes
case "CERTIFICATE":
result.Certificate, _ = x509.ParseCertificate(derBlock.Bytes)
case "PRIVATE KEY":
case "PRIVATE KEY", "RSA PRIVATE KEY":
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The consensus is to use PRIVATE KEY for PKCS#8 and RSA PRIVATE KEY for PKCS#1.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding "RSA PRIVATE KEY" was required for the test cases from the existing PRs that I ported over to here.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your change is ok, but if you get PEM with PRIVATE KEY is very likely that is encoded using PKCS#8

$ openssl genrsa -out rsa.pem
$ head -1 rsa.pem
-----BEGIN RSA PRIVATE KEY-----
$ openssl pkcs8 -topk8 -in rsa.pem -nocrypt | head -1
-----BEGIN PRIVATE KEY-----

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok leaving this for backward compatibility. I don't think we are using this path.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also just in a _test.go file to extract that from the hardcoded PEMs. We could replace the tests if we want to.

decrypt.go Outdated Show resolved Hide resolved
decrypt.go Outdated Show resolved Hide resolved
Addressing most of the review comments.
@hslatman
Copy link
Member Author

hslatman commented Mar 2, 2023

A few details, we should consider to maintaining this ourselves in go.step.sm/crypto

I think keeping the core pkcs7 library in a separate repository is clearer, at least for the time being, but I can see the utility of that. It could be a way forward after making this an officially maintained fork, then update the README to point to /crypto/pkcs7?

I've also thought about changing some details of this package, as there's a couple of things I don't really like, such as how encryption algorithms need to be set globally before encrypting. Those would break the current API, so it might be possible to put that in a safe wrapper around this lib and put that in /crypto instead or in addition to the low level library.

@hslatman hslatman requested a review from maraino March 2, 2023 19:25
@maraino
Copy link

maraino commented Mar 2, 2023

I've also thought about changing some details of this package, as there's a couple of things I don't really like, such as how encryption algorithms need to be set globally before encrypting. Those would break the current API, so it might be possible to put that in a safe wrapper around this lib and put that in /crypto instead or in addition to the low level library.

Sure, we can keep the pkcs7, and if we add a wrapper into crypto we should include the PKCS#8 thing.

Copy link

@maraino maraino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, because this package is intended to have backward compatibility with the original package.

Things that can be added in a go.step.sm/crypto/pkcs8 wrapper:

  • MGFHash support
  • Proper parsing of key
  • Encrypted key support
  • Better API
  • ...

@hslatman
Copy link
Member Author

hslatman commented Mar 2, 2023

Ok, because this package is intended to have backward compatibility with the original package.

Things that can be added in a go.step.sm/crypto/pkcs8 wrapper:

* MGFHash support

* Proper parsing of key

* Encrypted key support

* Better API

* ...

Noted!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants