Skip to content

Commit 7e848e0

Browse files
fix(webauthn): Fix buffer out of bounds (#4577)
* fix(webauthn): Fix buffer out of bounds * fix(webauthn): Fix `TWWebAuthnGetRSValues` implicit Data conversion * fix(webauthn): Extra checks for algorithm, and public key parts * fix(webauthn): Test common buffer overflows * fix(webauthn): Check `crv` and `kty` parameters * fix(private-key): Add PrivateKey Data move constructor * chore(noexcept): Remove wrong noexcept * fix(private-key): Zeroize memory on move `operator=` * fix(private-key): Avoid cleaning private key if self-assign
1 parent 1fe9c73 commit 7e848e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+391
-228
lines changed

include/TrustWalletCore/TWWebAuthn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct TWPublicKey *_Nullable TWWebAuthnGetPublicKey(TWData *_Nonnull attestatio
2424
/// \param signature ASN encoded webauthn signature: https://www.w3.org/TR/webauthn-2/#sctn-signature-attestation-types
2525
/// \return Concatenated r and s values.
2626
TW_EXPORT_STATIC_METHOD
27-
TWData *_Nonnull TWWebAuthnGetRSValues(TWData *_Nonnull signature);
27+
TWData *_Nullable TWWebAuthnGetRSValues(TWData *_Nonnull signature);
2828

2929
/// Reconstructs the original message that was signed via P256 curve. Can be used for signature validation.
3030
///

src/Aeternity/Signer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ using namespace TW;
1414

1515
namespace TW::Aeternity {
1616

17-
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
18-
auto privateKey = PrivateKey(Data(input.private_key().begin(), input.private_key().end()), TWCurveED25519);
17+
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) {
18+
auto privateKey = PrivateKey(input.private_key(), TWCurveED25519);
1919
std::string sender_id = input.from_address();
2020
std::string recipient_id = input.to_address();
2121
std::string payload = input.payload();

src/Aeternity/Signer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace TW::Aeternity {
1313
class Signer {
1414
public:
1515
/// Signs a Proto::SigningInput transaction
16-
static Proto::SigningOutput sign(const Proto::SigningInput& input) noexcept;
16+
static Proto::SigningOutput sign(const Proto::SigningInput& input);
1717

1818
/// Signs the given transaction.
1919
static Proto::SigningOutput sign(const PrivateKey& privateKey, Transaction& transaction);

src/Aion/Signer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ using namespace TW;
88

99
namespace TW::Aion {
1010

11-
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
12-
auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()), TWCurveED25519);
11+
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) {
12+
auto key = PrivateKey(input.private_key(), TWCurveED25519);
1313
auto transaction = Signer::buildTransaction(input);
1414
Signer::sign(key, transaction);
1515

@@ -20,7 +20,7 @@ Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
2020
return output;
2121
}
2222

23-
void Signer::sign(const PrivateKey& privateKey, Transaction& transaction) noexcept {
23+
void Signer::sign(const PrivateKey& privateKey, Transaction& transaction) {
2424
auto encoded = transaction.encode();
2525
auto hashData = Hash::blake2b(encoded, 32);
2626
auto hashSignature = privateKey.sign(hashData);
@@ -33,13 +33,13 @@ void Signer::sign(const PrivateKey& privateKey, Transaction& transaction) noexce
3333
transaction.signature = result;
3434
}
3535

36-
TW::Data Signer::signaturePreimage(const Proto::SigningInput& input) noexcept {
36+
TW::Data Signer::signaturePreimage(const Proto::SigningInput& input) {
3737
auto transaction = Signer::buildTransaction(input);
3838
auto encoded = transaction.encode();
3939
return transaction.encode();
4040
}
4141

42-
Proto::SigningOutput Signer::compile(const Data& signature, const PublicKey& publicKey, const Proto::SigningInput& input) noexcept {
42+
Proto::SigningOutput Signer::compile(const Data& signature, const PublicKey& publicKey, const Proto::SigningInput& input) {
4343
auto transaction = Signer::buildTransaction(input);
4444

4545
// Aion signature = pubKeyBytes + signatureBytes
@@ -56,7 +56,7 @@ Proto::SigningOutput Signer::compile(const Data& signature, const PublicKey& pub
5656
return output;
5757
}
5858

59-
Transaction Signer::buildTransaction(const Proto::SigningInput& input) noexcept {
59+
Transaction Signer::buildTransaction(const Proto::SigningInput& input) {
6060
auto transaction = Transaction(
6161
/* nonce: */ static_cast<uint128_t>(load(input.nonce())),
6262
/* gasPrice: */ static_cast<uint128_t>(load(input.gas_price())),

src/Aion/Signer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ class Signer {
2222
Signer() = delete;
2323

2424
/// Signs a Proto::SigningInput transaction
25-
static Proto::SigningOutput sign(const Proto::SigningInput& input) noexcept;
25+
static Proto::SigningOutput sign(const Proto::SigningInput& input);
2626

2727
/// Signs the given transaction.
28-
static void sign(const PrivateKey& privateKey, Transaction& transaction) noexcept;
28+
static void sign(const PrivateKey& privateKey, Transaction& transaction);
2929

3030
/// Get transaction data to be signed
31-
static TW::Data signaturePreimage(const Proto::SigningInput& input) noexcept;
32-
static Proto::SigningOutput compile(const Data& signature, const PublicKey& publicKey, const Proto::SigningInput& input) noexcept;
31+
static TW::Data signaturePreimage(const Proto::SigningInput& input);
32+
static Proto::SigningOutput compile(const Data& signature, const PublicKey& publicKey, const Proto::SigningInput& input);
3333

3434
private:
3535
/// Builds an Aion transaction from the given `Proto::SigningInput`.
36-
static Transaction buildTransaction(const Proto::SigningInput& input) noexcept;
36+
static Transaction buildTransaction(const Proto::SigningInput& input);
3737
};
3838

3939
} // namespace TW::Aion

src/Algorand/Signer.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ const Data TRANSACTION_TAG = {84, 88};
1616
const std::string TRANSACTION_PAY = "pay";
1717
const std::string ASSET_TRANSACTION = "axfer";
1818

19-
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
19+
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) {
2020
auto protoOutput = Proto::SigningOutput();
21-
auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()), TWCurveED25519);
21+
auto key = PrivateKey(input.private_key(), TWCurveED25519);
2222
auto pubkey = key.getPublicKey(TWPublicKeyTypeED25519);
2323

2424
auto preImageData = Signer::preImage(pubkey, input);
@@ -33,24 +33,24 @@ std::string Signer::signJSON(const std::string& json, const Data& key) {
3333
return hex(Signer::sign(input).encoded());
3434
}
3535

36-
Data Signer::sign(const PrivateKey& privateKey, const BaseTransaction& transaction) noexcept {
36+
Data Signer::sign(const PrivateKey& privateKey, const BaseTransaction& transaction) {
3737
Data data;
3838
append(data, TRANSACTION_TAG);
3939
append(data, transaction.serialize());
4040
auto signature = privateKey.sign(data);
4141
return {signature.begin(), signature.end()};
4242
}
4343

44-
TW::Data Signer::signaturePreimage(const Proto::SigningInput& input) noexcept {
44+
TW::Data Signer::signaturePreimage(const Proto::SigningInput& input) {
4545
auto pubKey = input.public_key();
4646
return Signer::preImage(PublicKey(Data(pubKey.begin(), pubKey.end()), TWPublicKeyTypeED25519), input);
4747
}
4848

49-
Proto::SigningOutput Signer::compile(const Data& signature, const PublicKey& publicKey, const Proto::SigningInput& input) noexcept {
49+
Proto::SigningOutput Signer::compile(const Data& signature, const PublicKey& publicKey, const Proto::SigningInput& input) {
5050
return Signer::encodeTransaction(signature, publicKey, input);
5151
}
5252

53-
TW::Data Signer::preImage(const TW::PublicKey& pubKey, const Proto::SigningInput& input) noexcept {
53+
TW::Data Signer::preImage(const TW::PublicKey& pubKey, const Proto::SigningInput& input) {
5454
auto from = Address(pubKey);
5555
auto firstRound = input.first_round();
5656
auto lastRound = input.last_round();
@@ -93,7 +93,7 @@ TW::Data Signer::preImage(const TW::PublicKey& pubKey, const Proto::SigningInput
9393
return data;
9494
}
9595

96-
Proto::SigningOutput Signer::encodeTransaction(const Data& signature, const TW::PublicKey& pubKey, const Proto::SigningInput& input) noexcept {
96+
Proto::SigningOutput Signer::encodeTransaction(const Data& signature, const TW::PublicKey& pubKey, const Proto::SigningInput& input) {
9797
auto protoOutput = Proto::SigningOutput();
9898

9999
auto from = Address(pubKey);

src/Algorand/Signer.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ class Signer {
2020
Signer() = delete;
2121

2222
/// Signs a Proto::SigningInput transaction
23-
static Proto::SigningOutput sign(const Proto::SigningInput& input) noexcept;
23+
static Proto::SigningOutput sign(const Proto::SigningInput& input);
2424

2525
/// Signs a json Proto::SigningInput with private key
2626
static std::string signJSON(const std::string& json, const Data& key);
2727

2828
/// Signs the given transaction.
29-
static Data sign(const PrivateKey& privateKey, const BaseTransaction& transaction) noexcept;
29+
static Data sign(const PrivateKey& privateKey, const BaseTransaction& transaction);
3030

3131
/// Get transaction data to be signed
32-
static TW::Data signaturePreimage(const Proto::SigningInput& input) noexcept;
33-
static Proto::SigningOutput compile(const Data& signature, const PublicKey& publicKey, const Proto::SigningInput& input) noexcept;
32+
static TW::Data signaturePreimage(const Proto::SigningInput& input);
33+
static Proto::SigningOutput compile(const Data& signature, const PublicKey& publicKey, const Proto::SigningInput& input);
3434

3535
private:
36-
static TW::Data preImage(const TW::PublicKey& pubKey, const Proto::SigningInput& input) noexcept;
37-
static Proto::SigningOutput encodeTransaction(const Data& signature, const TW::PublicKey& pubKey, const Proto::SigningInput& input) noexcept;
36+
static TW::Data preImage(const TW::PublicKey& pubKey, const Proto::SigningInput& input);
37+
static Proto::SigningOutput encodeTransaction(const Data& signature, const TW::PublicKey& pubKey, const Proto::SigningInput& input);
3838
};
3939

4040
} // namespace TW::Algorand

src/Bitcoin/Signer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
namespace TW::Bitcoin {
1616

17-
Proto::TransactionPlan Signer::plan(const Proto::SigningInput& input) noexcept {
17+
Proto::TransactionPlan Signer::plan(const Proto::SigningInput& input) {
1818
if (input.has_signing_v2()) {
1919
return planAsV2(input);
2020
}
2121
auto plan = TransactionSigner<Transaction, TransactionBuilder>::plan(input);
2222
return plan.proto();
2323
}
2424

25-
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input, std::optional<SignaturePubkeyList> optionalExternalSigs) noexcept {
25+
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input, std::optional<SignaturePubkeyList> optionalExternalSigs) {
2626
if (input.has_signing_v2()) {
2727
return signAsV2(input);
2828
}
@@ -51,7 +51,7 @@ Proto::SigningOutput Signer::sign(const Proto::SigningInput& input, std::optiona
5151
return output;
5252
}
5353

54-
Proto::PreSigningOutput Signer::preImageHashes(const Proto::SigningInput& input) noexcept {
54+
Proto::PreSigningOutput Signer::preImageHashes(const Proto::SigningInput& input) {
5555
if (input.has_signing_v2()) {
5656
return preImageHashesAsV2(input);
5757
}
@@ -76,7 +76,7 @@ Proto::PreSigningOutput Signer::preImageHashes(const Proto::SigningInput& input)
7676

7777
Proto::SigningOutput Signer::compile(const Proto::SigningInput& input,
7878
const std::vector<Data>& signatures,
79-
const std::vector<PublicKey>& publicKeys) noexcept {
79+
const std::vector<PublicKey>& publicKeys) {
8080
if (input.has_signing_v2()) {
8181
return compileAsV2(input, signatures, publicKeys);
8282
}

src/Bitcoin/Signer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ class Signer {
1818
Signer() = delete;
1919

2020
/// Returns a transaction plan (utxo selection, fee estimation)
21-
static Proto::TransactionPlan plan(const Proto::SigningInput& input) noexcept;
21+
static Proto::TransactionPlan plan(const Proto::SigningInput& input);
2222

2323
/// Signs a Proto::SigningInput transaction
24-
static Proto::SigningOutput sign(const Proto::SigningInput& input, std::optional<SignaturePubkeyList> optionalExternalSigs = {}) noexcept;
24+
static Proto::SigningOutput sign(const Proto::SigningInput& input, std::optional<SignaturePubkeyList> optionalExternalSigs = {});
2525

2626
/// Collects pre-image hashes to be signed
27-
static Proto::PreSigningOutput preImageHashes(const Proto::SigningInput& input) noexcept;
27+
static Proto::PreSigningOutput preImageHashes(const Proto::SigningInput& input);
2828

2929
/// Compiles a transaction with the given signatures and public keys.
3030
static Proto::SigningOutput compile(const Proto::SigningInput& input,
3131
const std::vector<Data>& signatures,
32-
const std::vector<PublicKey>& publicKeys) noexcept;
32+
const std::vector<PublicKey>& publicKeys);
3333

3434
/// Plans a transaction via BitcoinV2 protocol (utxo selection, fee estimation).
3535
static Proto::TransactionPlan planAsV2(const Proto::SigningInput& input) noexcept;

src/BitcoinDiamond/Signer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
using namespace TW;
1414
namespace TW::BitcoinDiamond {
1515

16-
TransactionPlan Signer::plan(const SigningInput& input) noexcept {
16+
TransactionPlan Signer::plan(const SigningInput& input) {
1717
auto plan = Bitcoin::TransactionSigner<Transaction, TransactionBuilder>::plan(input);
1818
return plan.proto();
1919
}
2020

21-
SigningOutput Signer::sign(const SigningInput& input, std::optional<SignaturePubkeyList> optionalExternalSigs) noexcept {
21+
SigningOutput Signer::sign(const SigningInput& input, std::optional<SignaturePubkeyList> optionalExternalSigs) {
2222
SigningOutput output;
2323
auto result = Bitcoin::TransactionSigner<Transaction, TransactionBuilder>::sign(input, false, optionalExternalSigs);
2424
if (!result) {
@@ -43,7 +43,7 @@ SigningOutput Signer::sign(const SigningInput& input, std::optional<SignaturePub
4343
return output;
4444
}
4545

46-
PreSigningOutput Signer::preImageHashes(const SigningInput& input) noexcept {
46+
PreSigningOutput Signer::preImageHashes(const SigningInput& input) {
4747
PreSigningOutput output;
4848
auto result = Bitcoin::TransactionSigner<Transaction, TransactionBuilder>::preImageHashes(input);
4949
if (!result) {

0 commit comments

Comments
 (0)