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

Difference in RSA Signature on macOS vs Ubuntu #254

Closed
3 tasks done
dezinezync opened this issue Sep 11, 2024 · 8 comments
Closed
3 tasks done

Difference in RSA Signature on macOS vs Ubuntu #254

dezinezync opened this issue Sep 11, 2024 · 8 comments

Comments

@dezinezync
Copy link

New Issue Checklist

Expected behavior

When generating a signature using _RSA.Signing.PrivateKey(pemRepresentation:).signature(for:padding:), the method should generate identical signatures on macOS 14.6.1 (development machine) versus Ubuntu 22.04.4 LTS.

Actual behavior

When generating a signature using _RSA.Signing.PrivateKey(pemRepresentation:).signature(for:padding:), the method generates different outputs on macOS 14.6.1 (development machine) versus Ubuntu 22.04.4 LTS.

Steps to reproduce

Test case code for reproducing the behaviour:

final class SignatureValidatorTests: XCTestCase {
  
  func testGeneratingSignatureHeader() throws {
    let digest = "dJZMgTHg+E8GHSUfoZmlcNHmemgyb73xYXuSu6kuGS4="
    let date = "Wed, 11 Sep 2024 07:20:22 GMT"
    // what should be generated + is generated on macOS
    let expected = "Jk+0f4MPcFHewm7x+nHxYjCXNRoEwGVUz9SpKNFh9DWbQRQwy/e/SG5T1QKDnigvVI8tWDGFPTSS2AvLPud5KKzWFrps29AkRooc2bz1ONchCZRr4HC6bHi9LwALoZgnhzy04P0Esn4AfNRCi6KlFcJNi2jUDxNzgGpF5a1LHPmLmN5VldySzFmXMQe5M5MK7c9nImfhfgivr9CuxZxLnuHuotHhawn0Tv20bXvOWK1LJMLIw6UI2Ofq8Gfr3K2AYpoP/sJfETfw0kMorbj38tSjXgu4PVkL5FER3OZqNzU7i5HJEkjTdjusD7txkTP4/eO7oCoyD3pzwZ+xtknrYg=="
    // what gets generated on Ubuntu
    let actual = "dCPLJ7FOn9hkRuNOpCbM2RXrD/YOrbD4yhX7Ify3B9XrGZ/amrw/xnK6HS+W6/817iTbpi61G1NQAovi2wOVs7gR4DtP+fNxQv/3oaQmyN4PWnUBsOFnPRpISiy9b6SuEEba+se19GocZOnAtkCVHdAzvPLvX7xrPMBBmTaHfaiIDFapB8dW7zDX+7IrbleGRUwitwLH8bG8T9+SGmD48p7bFbqHiZ9B2VpV91HhhR7TJ5SzEDhHcrGRJxrXOZ1Tfftn3dJKYZIpQ8Cq61Ox65D99z6UpReav2nZK80+32c02Av95Nk22uzdiidPI0mNE/WIgOn3bkjSFLtBH4ws3A=="
    
    let stringToSign = """
(request-target): post /inbox
host: mastodon.social
date: \(date)
digest: SHA-256=\(digest)
"""
    
    let stsData = stringToSign.data(using: .utf8)!
    let privateKey = try _RSA.Signing.PrivateKey(pemRepresentation: privateKeyPEM)
    let signatureData = try privateKey.signature(for: stsData, padding: .insecurePKCS1v1_5)
    let signature = Data(signatureData.rawRepresentation).base64EncodedString()
    
    XCTAssertEqual(signature, actual)       // passes on macOS, fails on Ubuntu        
    XCTAssertEqual(signature, expected)  // passes on Ubuntu, fails on macOS  
  }
  
  // MARK: -
  let privateKeyPEM = """
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA0oZMFXQadnV7K03Dy3ZJBdYPb/8aJputrykCecEv+CRZIRyu
+v+F75nZx7aeu0Mz61onAINhIrQSwpT3HkqDB1XfNEwFE2VubgK/b8MvC07hXrVl
scYITyiDy2836bwvkr7w2T1jxCjBcrBKM+7/atFKuRRQhW2FXqfD/24UToo4nW3w
Qo/e8JlAhcq7y0Mx4KqU8zCw8f0VGl1FnaWpS0x1YRESgvet1vXtST/uAUz2SOV0
DyqpPHvhyQSr+4iwm+3WoSgKn67PQ4gxdIccqXNh+YnZAo0+nQ9rp+jlRfJEMnda
aTsWmCx2L40nWQUXd8mRnNiBMdZtqD3R+JTg9QIDAQABAoIBABobO04hwrJT6T06
fIA7n9yQagOqb9S/0274N+1fTpKH92GKzAwmKbbHrmD8vXxdXg/ecx22LRJ/oB/v
FyHzfYIoX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noyYzYB5s9QFURft8ppu2b6zj5so
HvFrfdu4DUI4b5rTXLNRxDeDsYTwAUghy9Ds5V5qqoTepuDaPSgFdg==
-----END RSA PRIVATE KEY-----
"""
  
}

Swift Crypto version/commit hash

ce204da

Environment

Swift version: Swift version 5.10 (swift-5.10-RELEASE)
Target: x86_64-unknown-linux-gnu
Unix version: Linux ip-172-30-3-16 6.5.0-1016-aws #16~22.04.1-Ubuntu SMP Wed Mar 13 18:54:49 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
@Lukasa
Copy link
Contributor

Lukasa commented Sep 11, 2024

Hrm, how confident are you of this? I can't reproduce this locally with a docker image: I see this passing in both environments.

@dezinezync
Copy link
Author

I hit this bug on a production server.

So to verify, I tunnelled my dev setup to a temporary remote host to verify, and it works as expected, no changes in code.

I forgot to mention in the OP: the local code, running on macOS, is a debug build, while the server build is a release/production build, if that is of any consequence.

@Lukasa
Copy link
Contributor

Lukasa commented Sep 11, 2024

I'm really struggling to find a way to make this reproduce at all. Does your xctest actually fail on your Linux node? Are you actually running Crypto from main?

@dezinezync
Copy link
Author

Does your xctest actually fail on your Linux node?

Yes

Are you actually running Crypto from main?

Yes, the specific commit mentioned in the OP.

I'll setup Ubuntu 22.04.4 LTS in UTM as a VM tomorrow morning to figure out if I can reproduce this locally as well, and get back to you with my findings.

@Lukasa
Copy link
Contributor

Lukasa commented Sep 11, 2024

Thank you, that would be helpful. I tried the 5.10 docker image, both arm64 and amd64, without success.

@dezinezync
Copy link
Author

I can reproduce this on a Ubuntu 22.04.4 VM running in UTM (host: macOS 14.6.1 ARM) :

➜  testPKG ./.build/release/CryptoMe
Actual true
Expected false
➜  testPKG ./.build/checkouts/swift-crypto/scripts/environment.sh
This will replace your current pasteboard. Continue? [y/n]y
Swift version: Swift version 5.10.1 (swift-5.10.1-RELEASE)
Target: aarch64-unknown-linux-gnu
Unix version: Linux ubuntuutm 6.8.0-40-generic #40~22.04.3-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 30 17:53:10 UTC 2 aarch64 aarch64 aarch64 GNU/Linux

Your pasteboard now contains debug info, paste it on Github

Creating a debug build has no impact on this:

➜  testPKG ./.build/debug/CryptoMe
Actual true
Expected false

@dezinezync
Copy link
Author

@Lukasa I've created a more condensed sample code and it validates correctly on all three platforms: macOS (local), Ubuntu (UTM VM), and the production server (UTM VM).

I apologise for the confusion, and I appreciate your patience.

I'm closing this thread, as I continue to investigate this further on my end.

@Lukasa
Copy link
Contributor

Lukasa commented Sep 12, 2024

No need to apologise, raising it here was absolutely the right thing to do. I hope you were able to get pointed in the right direction! Thanks for the issue. ✨

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

No branches or pull requests

2 participants