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

Secure key storage #481

Merged
merged 3 commits into from
Sep 2, 2024
Merged

Conversation

MatheusFranco99
Copy link
Contributor

Overview

This PR adjusts the PemToPrivateKey and PemToPublicKey functions to use a secure storage method, removing the deprecated functions that were being used.

@@ -92,13 +88,17 @@ func PemToPublicKey(pkPem []byte) (*rsa.PublicKey, error) {
}

// PrivateKeyToPem converts privateKey to pem encoded
func PrivateKeyToPem(sk *rsa.PrivateKey) []byte {
func PrivateKeyToPem(sk *rsa.PrivateKey) ([]byte, error) {
pemBytes, err := x509.MarshalPKCS8PrivateKey(sk)
Copy link
Contributor

Choose a reason for hiding this comment

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

I see in SSV they use x509.MarshalPKCS1PrivateKey(sk)
Why the difference?
This function isn't deprectated?

Copy link
Contributor Author

@MatheusFranco99 MatheusFranco99 Aug 15, 2024

Choose a reason for hiding this comment

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

From a discussion with Alan and as far as I understood, PKCS8 will just add a key-type tag. Follow this link for more

So, it's more or less the same but it seems that PKCS8 is preferable if we can use it (the audit also suggests it). Therefore, it would be good if SSV also updates it. If not, I can restore to PKCS1

Copy link
Contributor

Choose a reason for hiding this comment

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

The only thing is maybe node stores private keys and if we switch formats it may cause issues...
I can give an approve for now... but we should wait for @moshe-blox or @y0sher to read this comment and give their opinion

Copy link
Contributor

Choose a reason for hiding this comment

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

Btw, to be honest... this is an example of where we have too much implementation details in spec...
A spec probably shouldn't care about whether the key is PKCS1 or PKCS8 encoded

Copy link
Contributor Author

@MatheusFranco99 MatheusFranco99 Aug 15, 2024

Choose a reason for hiding this comment

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

Yup, I agree
The best would be either to have another repo to deal with this stuff (and spec could call its functions if necessary) or to define a function type and leave the node to implement it

Copy link
Contributor

Choose a reason for hiding this comment

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

as commented above AFAIK, PKCS8 isn't backward compatible with PKCS1, so if we decide to go forward with this we'll need to do some migration in the node. The main issue is that private keys are created and stored outside of the node by operators. and they'll have to change the way their keys are encoded if we stop supporting PKCS1.

parsedSk, err := x509.ParsePKCS1PrivateKey(b)

// Parse key as PKCS8
parsedSk, err := x509.ParsePKCS8PrivateKey(block.Bytes)
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIK PKCS8 isn't backward compatible with PKCS1.. wouldn't this break stuff?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct
It depends on how these functions are being used. From your above comment, it seems that it would break indeed. So, I'll switch back to PKCS1

Copy link
Contributor

@y0sher y0sher left a comment

Choose a reason for hiding this comment

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

@GalRogozinski @MatheusFranco99
(@moshe-blox, @lior-blox ).

I think I can approve this for the spec as I understand why you want this change. But I can't tell if and when we'll be able to adapt in the node. So maybe its best to wait with merging this.

@MatheusFranco99
Copy link
Contributor Author

@y0sher
Sure
Adding back the PKCS1 due to the break issues

@y0sher
Copy link
Contributor

y0sher commented Aug 19, 2024

@MatheusFranco99 I would create an issue about switching to PKCS8 in the future if its better in any terms, we just need to to this carefully and think about it from a product perspective.

@MatheusFranco99
Copy link
Contributor Author

@y0sher Agree 100%. Thanks

Copy link

@alan-ssvlabs alan-ssvlabs left a comment

Choose a reason for hiding this comment

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

Agree with the discussions above

@GalRogozinski
Copy link
Contributor

@y0sher
there is no hinderance now that we switched back to PKCS1 and this can be merged from your perspective?

@y0sher
Copy link
Contributor

y0sher commented Sep 2, 2024

Other than that looks good to me.

@GalRogozinski GalRogozinski merged commit 6342e32 into ssvlabs:dev Sep 2, 2024
2 checks passed
@GalRogozinski
Copy link
Contributor

Btw @MatheusFranco99 and @alan-ssvlabs @y0sher, I don't see a reason to open an issue with PKCS8 unless someone can tell me a clear advantage of it that is relevant to us.
I understood the tags will be helpful if we ever change our encryption algo, when we do that the encoding can change.

GalRogozinski pushed a commit that referenced this pull request Sep 2, 2024
* Implement secure key storage using PKCS8

* Fix lint issue

* Switch back PKCS8 to PKCS1
GalRogozinski pushed a commit that referenced this pull request Sep 12, 2024
* Implement secure key storage using PKCS8

* Fix lint issue

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

Successfully merging this pull request may close these issues.

4 participants