forked from signalapp/libsignal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
swift: PreKeyRecords lazily validate their keys
Convert the *Key and keyPair properties on *PreKeyRecord to throwing methods. Since the usual constructor is strongly typed, these should only throw when the serialized data is corrupted. But that is a possibility.
- Loading branch information
1 parent
5e81e01
commit 2f7f83d
Showing
4 changed files
with
79 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// Copyright 2024 Signal Messenger, LLC. | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
// | ||
|
||
import LibSignalClient | ||
import XCTest | ||
|
||
class SessionRecordTests: TestCaseBase { | ||
func testBadPreKeyRecords() { | ||
XCTAssertThrowsError(try PreKeyRecord(bytes: [0])) | ||
XCTAssertThrowsError(try SignedPreKeyRecord(bytes: [0])) | ||
XCTAssertThrowsError(try KyberPreKeyRecord(bytes: [0])) | ||
|
||
// The keys in records are lazily parsed, which means malformed keys aren't caught right away. | ||
// The following payloads were generated via protoscope: | ||
// % protoscope -s | base64 | ||
// The fields are described in storage.proto in the libsignal-protocol crate. | ||
do { | ||
// 1: 42 | ||
// 2: {} | ||
// 3: {} | ||
let record = try! PreKeyRecord(bytes: Data(base64Encoded: "CCoSABoA")!) | ||
XCTAssertThrowsError(try record.publicKey()) | ||
XCTAssertThrowsError(try record.privateKey()) | ||
} | ||
|
||
do { | ||
// 1: 42 | ||
// 2: {} | ||
// 3: {} | ||
// 4: {} | ||
// 5: 0i64 | ||
let record = try! SignedPreKeyRecord(bytes: Data(base64Encoded: "CCoSABoAIgApAAAAAAAAAAA=")!) | ||
XCTAssertThrowsError(try record.publicKey()) | ||
XCTAssertThrowsError(try record.privateKey()) | ||
} | ||
|
||
do { | ||
// 1: 42 | ||
// 2: {} | ||
// 3: {} | ||
// 4: {} | ||
// 5: 0i64 | ||
let record = try! KyberPreKeyRecord(bytes: Data(base64Encoded: "CCoSABoAIgApAAAAAAAAAAA=")!) | ||
XCTAssertThrowsError(try record.publicKey()) | ||
XCTAssertThrowsError(try record.secretKey()) | ||
XCTAssertThrowsError(try record.keyPair()) | ||
} | ||
} | ||
} |