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

Export Base64 signatures in Envelope #565

Merged
merged 4 commits into from
Apr 14, 2023

Conversation

PradyumnaKrishna
Copy link
Contributor

Description of the changes being introduced by the pull request:

As per DSSE spec, Signatures in Envelope should be base64 encoded. While securesystemslib implementation uses hex signatures for signing and verifying them. Best way to achieve this by using base64 encoded signatures externally and not changing any internal representation of signature.

This PR adds two helper method that encodes and converts signature into base64 dict and vice versa. Testcases are added and updated as well.

Reference: in-toto/in-toto#563 (comment)

Please verify and check that the pull request fulfils the following requirements:

  • The code follows the Code Style Guidelines
  • Tests have been added for the bug fix or new feature
  • Docs have been added for the bug fix or new feature

As per DSSE spec, Signatures in Envelope should be base64 encoded.
While securesystemslib implementation uses hex signatures for
signing and verifying them. Best way to achieve this by using
base64 encoded signatures externally and not changing any internal
representation of signature.

This commit adds two helper method that encodes and converts
signature into base64 dict and vice versa. Also, testcases are
added and updated as well.

Signed-off-by: Pradyumna Krishna <[email protected]>
Copy link
Member

@lukpueh lukpueh left a comment

Choose a reason for hiding this comment

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

Thanks for the great patch, @PradyumnaKrishna!

What do you think about adding the conversion logic as internal helpers to Envelope? We don't need it elsewhere, so why not keep the API smaller.

Also, apart from the docstring, which says "hex", the Signature class itself has been agnostic to the encoding of the sig field so far. I suggest we keep it that way.

@jku
Copy link
Collaborator

jku commented Apr 13, 2023

Best way to achieve this by using base64 encoded signatures externally and not changing any internal representation of signature.

Just bike shedding here but I suppose you could have a DSSESignature that is just a Signature with overridden to_dict() and from_dict() ? I didn't really think this completely through so I'm not sure if that actually gives you real value over what lukas suggested -- feel free to disregard.

@lukpueh
Copy link
Member

lukpueh commented Apr 13, 2023

Just bike shedding here but I suppose you could have a DSSESignature that is just a Signature with overridden to_dict() and from_dict()

I considered that too, or even to just allow to also write base64 into a regular Signature. Technically, nothing stops you from doing that already.

The problem is that the signature objects are returned from the signers, so you'd need dsse-flavored signers to produce dsse-flavored signature objects, right?

@PradyumnaKrishna
Copy link
Contributor Author

The problem is that the signature objects are returned from the signers, so you'd need dsse-flavored signers to produce dsse-flavored signature objects, right?

Yes, simplest would be to encode signature into base64 on dict conversion.

Moves signature base64 encoding from helper functions to Envelope,
as it will make the API smaller and base64 encoded signature
aren't rqeuired anywhere else.

Now, DSSE Envelope create base64 encoded signature on dict
conversion.

Signed-off-by: Pradyumna Krishna <[email protected]>
signatures = []
for signature in self.signatures:
sig_dict = signature.to_dict()
sig_dict["sig"] = b64enc(sig_dict["sig"].encode("utf-8"))
Copy link
Member

Choose a reason for hiding this comment

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

I think this is incompatible with other DSSE envelope signing libraries. If I'm reading this right, we're writing the b64 encoding of the hex representation of the signatures. I think we should first decode the hex value before passing the decoded bytes to b64 encoding.

Copy link
Member

Choose a reason for hiding this comment

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

Good point, Aditya! DSSE spec doesn't go into details about the format, but I would also expect other implementations to base64-encode the "raw" signature bytes.

@adityasaky found that we are encoding hex representation of
signatures into base64, that would be incompatible with other
DSSE envelope signing libraries.

This commit changes the direct base64 encoding in Envelope to
decoding hex signature and then encode the bytes into base64.

Signed-off-by: Pradyumna Krishna <[email protected]>
@@ -71,7 +74,7 @@ def to_dict(self) -> dict:
signatures = []
for signature in self.signatures:
sig_dict = signature.to_dict()
sig_dict["sig"] = b64enc(sig_dict["sig"].encode("utf-8"))
sig_dict["sig"] = b64enc(binascii.unhexlify(sig_dict["sig"]))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
sig_dict["sig"] = b64enc(binascii.unhexlify(sig_dict["sig"]))
sig_dict["sig"] = b64enc(bytes.fromhex(sig_dict["sig"]))

@@ -1,6 +1,7 @@
"""Dead Simple Signing Envelope
"""

import binascii
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import binascii

Comment on lines 64 to 66
signature["sig"] = binascii.hexlify(
b64dec(signature["sig"])
).decode("utf-8")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
signature["sig"] = binascii.hexlify(
b64dec(signature["sig"])
).decode("utf-8")
signature["sig"] = b64dec(signature["sig"]).hex()

Copy link
Member

@lukpueh lukpueh left a comment

Choose a reason for hiding this comment

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

Since Python 3.5 this is built into the bytes type.

Since Python3.5 binascii functions are built into bytes type.
Hence, removing binascii and using bytes for hex encoding/decoding.

Signed-off-by: Pradyumna Krishna <[email protected]>
@lukpueh lukpueh merged commit c28c6f0 into secure-systems-lab:main Apr 14, 2023
@adityasaky
Copy link
Member

Thanks @PradyumnaKrishna!

@lukpueh, can we cut a release with this PR included? I'd like to release in-toto-py with --use-dsse enabled.

@lukpueh
Copy link
Member

lukpueh commented Apr 14, 2023

@lukpueh, can we cut a release with this PR included? I'd like to release in-toto-py with --use-dsse enabled.

Sure, let's release next Monday!

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