Skip to content

Conversation

fuhry
Copy link

@fuhry fuhry commented Oct 20, 2022

Make smallstep's pkcs7 fork support RSA key handles returned from any private key type that returns an RSA public key.

Specifically, this addresses the need to use key handles returned by piv-go that are of the piv.keyRSA type.

Signed-Off-By: Dan Fuhry dan@fuhry.com

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.

Hi @fuhry, I've added an improvement, I think we can merge it here, once it's fixed, and then you can send a PR to mozilla/pkcs7.

@fuhry
Copy link
Author

fuhry commented Oct 21, 2022

Thanks for the suggestions! I'll update in the morning.

@hslatman
Copy link
Member

hslatman commented Oct 21, 2022

@maraino about the PR to Mozilla: yes, that's a good idea, but I haven't seen any movement there for over a year, so I doubt it'll be merged anytime soon. Also check this: mozilla-services#73.

Also, merging this into master won't be enough. In step-ca we currently import the patches branch from this PR: #1 via a replace: replace go.mozilla.org/pkcs7 => github.com/smallstep/pkcs7 v0.0.0-20211016004704-52592125d6f6. I intended that to be a temporary solution until changes were included in the Mozilla repo, but currently to no avail 😅

I think we're at a point where we may want to go forward with our own fork and tag our own versions now. What do you think? This subject was also briefly discussed during our meeting with MicroMDM. They've also been patching the PKCS7 library for their use cases and it may be an option to work on this as co-maintainers somehow.

@fuhry fuhry force-pushed the piv-rsakey-support branch 2 times, most recently from 983c0ac to bd4cd75 Compare October 21, 2022 16:14
@fuhry fuhry changed the title decrypt: support for piv-go RSA keys decrypt: support for RSA keys from piv-go and other generic RSA signers/decrypters Oct 21, 2022
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.

This is ok, but let's clean the code a bit, we can combine rsa and Decrypter cases in one.

decrypt.go Outdated
Comment on lines 41 to 39

case crypto.Decrypter:
// Generic case to handle anything that provides the crypto.Decrypter interface.
var contentKey []byte
contentKey, err := pkey.Decrypt(rand.Reader, recipient.EncryptedKey, nil)
if err != nil {
return nil, err
}
return data.EncryptedContentInfo.decrypt(contentKey)
Copy link

Choose a reason for hiding this comment

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

*rsa.PrivateKey also implements the crypto.Decrypter interface, and when you pass nil to the options on an rsa key, Go just call rsa.DecryptPKCS1v15(), we can do both of these cases like this:

Your changes look ok, but as *rsa.PrivateKey implements decrypter we can merge those two cases with:

	if dec, ok := pkey.(crypto.Decrypter); ok {
		contentKey, err := pkey.Decrypt(rand.Reader, recipient.EncryptedKey, nil)
		if err != nil {
			return nil, err
		}
		return data.EncryptedContentInfo.decrypt(contentKey)
	}

	return nil, ErrUnsupportedAlgorithm
}

And there is no need to do var contentKey []byte if you are creating it later with :=

@maraino
Copy link

maraino commented Oct 21, 2022

@hslatman Should we change the base branch to patches

@hslatman
Copy link
Member

hslatman commented Oct 21, 2022

Probably the best at this time

@maraino maraino changed the base branch from master to patches October 21, 2022 17:43
@maraino
Copy link

maraino commented Oct 21, 2022

@fuhry I've changed the base, from master to patches, there are some new files, do not worry about them.

@fuhry fuhry force-pushed the piv-rsakey-support branch from bd4cd75 to 3bc3305 Compare October 22, 2022 13:43
@fuhry
Copy link
Author

fuhry commented Oct 22, 2022

@maraino All changes are done, please take another look when you have a chance. Thank you!

…rs/decrypters

Make smallstep's pkcs7 fork support RSA key handles returned from any private key type that returns an RSA public key.

Specifically, this addresses the need to use key handles returned by piv-go that are of the `piv.keyRSA` type.

Signed-Off-By: Dan Fuhry <dan@fuhry.com>
@fuhry fuhry force-pushed the piv-rsakey-support branch from 3bc3305 to e6d0478 Compare October 22, 2022 13:45
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.

Thanks @fuhry, approved, I'll add it to smallstep/certificates

@maraino maraino merged commit e1aab68 into smallstep:patches Oct 24, 2022
@maraino
Copy link

maraino commented Oct 24, 2022

@fuhry here it is smallstep/certificates#1139

@hslatman
Copy link
Member

hslatman commented Mar 2, 2023

Noting for future reference: the changes from this PR seem to have the same functional results as this PR: mozilla-services#41.

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.

4 participants