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

Correctly generate signature. #48

Merged
merged 1 commit into from
Sep 23, 2019
Merged

Correctly generate signature. #48

merged 1 commit into from
Sep 23, 2019

Conversation

kylebrowning
Copy link
Member

fixes #39

@kylebrowning kylebrowning merged commit 0699bb1 into master Sep 23, 2019
Comment on lines +73 to +75
let allZeroes = Array(repeating: UInt8(0), count: 32)
signatureBytes.writeBytes([allZeroes, rb].joined().suffix(32))
signatureBytes.writeBytes([allZeroes, sb].joined().suffix(32))
Copy link

Choose a reason for hiding this comment

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

Can you explain to me why this change is necessary? Do I understand this correctly that you prepend 32 zero bytes to the actual bytes only to cut off the last 32 bytes again?!

Copy link
Member Author

Choose a reason for hiding this comment

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

Did something break for you? This was needed to fix a flaky test.

Copy link

Choose a reason for hiding this comment

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

No, nothing broke. It is just our of curiosity, I don't see how sb[0..<lens] could have let to a flaky test. And how your changes are supposed to fix this flaky test?

Copy link
Member Author

@kylebrowning kylebrowning Oct 2, 2019

Choose a reason for hiding this comment

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

lens is sometimes only 31 so this makes sure its always 32 when written in.

Copy link

Choose a reason for hiding this comment

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

Ah, ok, I see, but why is that? Is the signature still correct when the number is only 31 bytes wide?

Copy link
Member Author

Choose a reason for hiding this comment

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

I never saw it fail in the wild, nor was I able to make it fail(become 31) when sending push notifications, but I would imagine the signature would be wrong when its 31.

Our test, specifically checks for the length of 32.

Copy link

Choose a reason for hiding this comment

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

Also note that rb and sb are already always 32 bytes wide. So I wonder what would happen if you instead just use signatureBytes.writeBytes(rb)

Copy link

Choose a reason for hiding this comment

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

Also note that rb and sb are already always 32 bytes wide. So I wonder what would happen if you instead just use signatureBytes.writeBytes(rb)

Ah, wrong. I think I get it now: the return value of BN_bn2bin is the number of bytes actually written into the buffer. For small numbers this can be less than 32. The size of rb is calculated by BN_num_bits(r)+7)/8. This might also be wrong already for the same reason. So your fix is indeed correct.

There is a method BN_bn2binpad which adds the padding automatically. Maybe we should just use this instead? And also init the rb and sb arrays with a length of 32.

Copy link

@t089 t089 Oct 2, 2019

Choose a reason for hiding this comment

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

On second thought: for big-endian, should the padding be in the front, or the end of the buffer?

Copy link
Member Author

@kylebrowning kylebrowning Oct 2, 2019

Choose a reason for hiding this comment

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

Im not entirely sure, im new to this particular signature creation. I can say that if you do

while swift test --sanitize=address -c release -Xswiftc -enable-testing --disable-index-store; do date; done

The old way will eventually fail tests, but with this new way we always pass, and always have the correct length.

@t089
Copy link

t089 commented Oct 2, 2019

I think your patch is correct, but I think even better would be a slightly different approach using the BN_bn2binpad API and thus avoiding the need to create so many arrays. I’ll write up a PR.

@t089
Copy link

t089 commented Oct 2, 2019

I think your patch is correct, but I think even better would be a slightly different approach using the BN_bn2binpad API and thus avoiding the need to create so many arrays. I’ll write up a PR.

Unfortunately, this function is not available on all the different versions of SSL. So, I don't think it is helpful to rework this. Thanks for the explanation, and sorry that I missed this padding issue when proposing this code.

@kylebrowning
Copy link
Member Author

@t089 one day we will be able to drop support for openssl 1.0 :)

As an aside, and out of curiosity are you running this project on production?

@kylebrowning kylebrowning deleted the flaky-test branch June 9, 2022 14:13
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.

flaky test
2 participants