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

[tests] Fix signature length calculation in sgx_sign tests #1864

Merged
merged 1 commit into from
May 2, 2024

Conversation

mkow
Copy link
Member

@mkow mkow commented Apr 30, 2024

Description of the changes

Incorrect signature length calculation could cause an InvalidSignature exception if the signature happened to start with a 0x00 byte, resulting in random CI failures.

Fixes #1689.

How to test this PR?

Run while true; python3 -m pytest -v -k 'test_sign_from_pem_path' tests/; or break; end in Gramine root and wait. Without this PR it fails after ~64 loop iterations with:

tests/test_sgx_sign.py:64: in test_sign_from_pem_path
    verify_signature(data, exponent, modulus, signature, key_file)
tests/test_sgx_sign.py:50: in verify_signature
    public_key.verify(signature_bytes, data, padding.PKCS1v15(), hashes.SHA256())
/usr/local/lib/python3.8/dist-packages/cryptography/hazmat/backends/openssl/rsa.py:550: in verify
    _rsa_sig_verify(
/usr/local/lib/python3.8/dist-packages/cryptography/hazmat/backends/openssl/rsa.py:325: in _rsa_sig_verify
    raise InvalidSignature
E   cryptography.exceptions.InvalidSignature

This change is Reviewable

Copy link
Contributor

@kailun-qin kailun-qin left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r1, all commit messages.
Reviewable status: all files reviewed, all discussions resolved, not enough approvals from maintainers (1 more required)

Copy link
Contributor

@dimakuv dimakuv left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r1, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions, not enough approvals from maintainers (1 more required) (waiting on @mkow)


-- commits line 5 at r1:
What's the problem of a signature starting with a zero byte?


tests/test_sgx_sign.py line 47 at r1 (raw file):

    assert numbers.e == exponent
    assert numbers.n == modulus
    signature_bytes = signature.to_bytes((modulus.bit_length() + 7) // 8, byteorder='big')

I don't understand this change. Could you explain a bit? Why signature length is wrong here?

Copy link
Member Author

@mkow mkow left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 2 unresolved discussions, not enough approvals from maintainers (1 more required) (waiting on @dimakuv)


-- commits line 5 at r1:

Previously, dimakuv (Dmitrii Kuvaiskii) wrote…

What's the problem of a signature starting with a zero byte?

See below.


tests/test_sgx_sign.py line 47 at r1 (raw file):

Previously, dimakuv (Dmitrii Kuvaiskii) wrote…

I don't understand this change. Could you explain a bit? Why signature length is wrong here?

public_key.verify() expects a specific byte sequence as a correct signature, not a number (and that's also what the original signing function returns, but our wrapper converts it to a number, because that's needed for SGX stuff: https://github.com/gramineproject/gramine/blob/master/python/graminelibos/sgx_sign.py#L666). Here we're trying to convert it back, but incorrectly.

The problem here is that this "serialization" here produces different lengths depending on the signature. If it happens to start with 0x00, then the resulting bytes array is shorter than expected and the verification fails. Example: with the SGX signing parameters the key signature should always have 384 bytes (3072/8), regardless of it's specific value.

Copy link
Contributor

@dimakuv dimakuv left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

Incorrect signature length calculation could cause InvalidSignature
exception if the signature happened to start with a 0x00 byte,
resulting in random CI failures.

Signed-off-by: Michał Kowalczyk <mkow@invisiblethingslab.com>
@mkow mkow force-pushed the mkow/fix-failing-sgx-sign-test branch from dcf60c5 to bf46ba3 Compare May 2, 2024 11:22
Copy link
Contributor

@dimakuv dimakuv left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r1.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

Copy link
Contributor

@kailun-qin kailun-qin left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r1, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

@mkow mkow merged commit bf46ba3 into master May 2, 2024
18 checks passed
@mkow mkow deleted the mkow/fix-failing-sgx-sign-test branch May 2, 2024 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants