[CodeHealth] Use span for input buffer with challange bypass (#21271)

The FFI for `challenge_bypass_ristretto_ffi` is doing some banned
C-style casts for pointer conversion. This change corrects that by using
`base::span` in the interface as a better read buffer type than
`std::string`.
This commit is contained in:
cdesouza-chromium
2023-12-07 10:05:07 +00:00
committed by GitHub
parent 926552083d
commit 872466df9b
14 changed files with 84 additions and 70 deletions
@@ -7,6 +7,7 @@
#include <utility>
#include "base/containers/span.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/blinded_token.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/blinded_token_util.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/challenge_bypass_ristretto_util.h"
@@ -43,7 +44,7 @@ std::optional<challenge_bypass_ristretto::BatchDLEQProof> Create(
return ValueOrLogError(
challenge_bypass_ristretto::BatchDLEQProof::decode_base64(
batch_dleq_proof_base64));
base::as_bytes(base::make_span(batch_dleq_proof_base64))));
}
std::vector<UnblindedToken> ToUnblindedTokens(
@@ -5,6 +5,7 @@
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/blinded_token.h"
#include "base/containers/span.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/challenge_bypass_ristretto_util.h"
namespace brave_ads::cbr {
@@ -19,7 +20,7 @@ std::optional<challenge_bypass_ristretto::BlindedToken> Create(
return ValueOrLogError(
challenge_bypass_ristretto::BlindedToken::decode_base64(
blinded_token_base64));
base::as_bytes(base::make_span(blinded_token_base64))));
}
} // namespace
@@ -5,6 +5,7 @@
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/dleq_proof.h"
#include "base/containers/span.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/blinded_token.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/challenge_bypass_ristretto_util.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/public_key.h"
@@ -34,8 +35,8 @@ std::optional<challenge_bypass_ristretto::DLEQProof> Create(
return std::nullopt;
}
return ValueOrLogError(
challenge_bypass_ristretto::DLEQProof::decode_base64(dleq_proof_base64));
return ValueOrLogError(challenge_bypass_ristretto::DLEQProof::decode_base64(
base::as_bytes(base::make_span(dleq_proof_base64))));
}
} // namespace
@@ -5,6 +5,7 @@
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/public_key.h"
#include "base/containers/span.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/challenge_bypass_ristretto_util.h"
namespace brave_ads::cbr {
@@ -17,8 +18,8 @@ std::optional<challenge_bypass_ristretto::PublicKey> Create(
return std::nullopt;
}
return ValueOrLogError(
challenge_bypass_ristretto::PublicKey::decode_base64(public_key_base64));
return ValueOrLogError(challenge_bypass_ristretto::PublicKey::decode_base64(
base::as_bytes(base::make_span(public_key_base64))));
}
} // namespace
@@ -5,6 +5,7 @@
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/signed_token.h"
#include "base/containers/span.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/challenge_bypass_ristretto_util.h"
namespace brave_ads::cbr {
@@ -18,7 +19,7 @@ std::optional<challenge_bypass_ristretto::SignedToken> Create(
}
return ValueOrLogError(challenge_bypass_ristretto::SignedToken::decode_base64(
signed_token_base64));
base::as_bytes(base::make_span(signed_token_base64))));
}
} // namespace
@@ -5,6 +5,7 @@
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/signing_key.h"
#include "base/containers/span.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/blinded_token.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/challenge_bypass_ristretto_util.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/public_key.h"
@@ -23,7 +24,7 @@ std::optional<challenge_bypass_ristretto::SigningKey> Create(
}
return ValueOrLogError(challenge_bypass_ristretto::SigningKey::decode_base64(
signing_key_base64));
base::as_bytes(base::make_span(signing_key_base64))));
}
} // namespace
@@ -5,6 +5,7 @@
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/token.h"
#include "base/containers/span.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/blinded_token.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/challenge_bypass_ristretto_util.h"
@@ -18,8 +19,8 @@ std::optional<challenge_bypass_ristretto::Token> Create(
return std::nullopt;
}
return ValueOrLogError(
challenge_bypass_ristretto::Token::decode_base64(token_base64));
return ValueOrLogError(challenge_bypass_ristretto::Token::decode_base64(
base::as_bytes(base::make_span(token_base64))));
}
} // namespace
@@ -5,6 +5,7 @@
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/token_preimage.h"
#include "base/containers/span.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/challenge_bypass_ristretto_util.h"
namespace brave_ads::cbr {
@@ -19,7 +20,7 @@ std::optional<challenge_bypass_ristretto::TokenPreimage> Create(
return ValueOrLogError(
challenge_bypass_ristretto::TokenPreimage::decode_base64(
token_preimage_base64));
base::as_bytes(base::make_span(token_preimage_base64))));
}
} // namespace
@@ -5,6 +5,7 @@
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/unblinded_token.h"
#include "base/containers/span.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/challenge_bypass_ristretto_util.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/token_preimage.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/verification_key.h"
@@ -21,7 +22,7 @@ std::optional<challenge_bypass_ristretto::UnblindedToken> Create(
return ValueOrLogError(
challenge_bypass_ristretto::UnblindedToken::decode_base64(
unblinded_token_base64));
base::as_bytes(base::make_span(unblinded_token_base64))));
}
} // namespace
@@ -5,6 +5,7 @@
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/verification_key.h"
#include "base/containers/span.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/challenge_bypass_ristretto_util.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/verification_signature.h"
@@ -18,7 +19,7 @@ std::optional<VerificationSignature> VerificationKey::Sign(
const std::string& message) {
return ValueOrLogError<challenge_bypass_ristretto::VerificationSignature,
VerificationSignature>(
verification_key_.sign(message));
verification_key_.sign(base::as_bytes(base::make_span(message))));
}
bool VerificationKey::Verify(
@@ -28,7 +29,9 @@ bool VerificationKey::Verify(
return false;
}
return verification_key_.verify(verification_signature.get(), message)
return verification_key_
.verify(verification_signature.get(),
base::as_bytes(base::make_span(message)))
.value_or(false);
}
@@ -5,6 +5,7 @@
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/verification_signature.h"
#include "base/containers/span.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/challenge_bypass_ristretto_util.h"
namespace brave_ads::cbr {
@@ -19,7 +20,7 @@ std::optional<challenge_bypass_ristretto::VerificationSignature> Create(
return ValueOrLogError(
challenge_bypass_ristretto::VerificationSignature::decode_base64(
verification_signature_base64));
base::as_bytes(base::make_span(verification_signature_base64))));
}
} // namespace
@@ -9,6 +9,7 @@
#include <utility>
#include "base/base64.h"
#include "base/containers/span.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "brave/components/brave_rewards/core/rewards_engine_impl.h"
@@ -92,7 +93,8 @@ base::expected<std::vector<std::string>, std::string> UnBlindCreds(
const mojom::CredsBatch& creds_batch) {
std::vector<std::string> unblinded_encoded_creds;
auto batch_proof = BatchDLEQProof::decode_base64(creds_batch.batch_proof);
auto batch_proof = BatchDLEQProof::decode_base64(
base::as_bytes(base::make_span(creds_batch.batch_proof)));
if (!batch_proof.has_value()) {
return base::unexpected(std::move(batch_proof).error());
}
@@ -101,7 +103,9 @@ base::expected<std::vector<std::string>, std::string> UnBlindCreds(
DCHECK(creds_base64.has_value());
std::vector<Token> creds;
for (auto& item : creds_base64.value()) {
if (auto cred = Token::decode_base64(item.GetString()); cred.has_value()) {
if (auto cred = Token::decode_base64(
base::as_bytes(base::make_span(item.GetString())));
cred.has_value()) {
creds.push_back(std::move(cred).value());
} else {
return base::unexpected(std::move(cred).error());
@@ -112,7 +116,8 @@ base::expected<std::vector<std::string>, std::string> UnBlindCreds(
DCHECK(blinded_creds_base64.has_value());
std::vector<BlindedToken> blinded_creds;
for (auto& item : blinded_creds_base64.value()) {
if (auto blinded_cred = BlindedToken::decode_base64(item.GetString());
if (auto blinded_cred = BlindedToken::decode_base64(
base::as_bytes(base::make_span(item.GetString())));
blinded_cred.has_value()) {
blinded_creds.push_back(std::move(blinded_cred).value());
} else {
@@ -124,7 +129,8 @@ base::expected<std::vector<std::string>, std::string> UnBlindCreds(
DCHECK(signed_creds_base64.has_value());
std::vector<SignedToken> signed_creds;
for (auto& item : signed_creds_base64.value()) {
if (auto signed_cred = SignedToken::decode_base64(item.GetString());
if (auto signed_cred = SignedToken::decode_base64(
base::as_bytes(base::make_span(item.GetString())));
signed_cred.has_value()) {
signed_creds.push_back(std::move(signed_cred).value());
} else {
@@ -132,7 +138,8 @@ base::expected<std::vector<std::string>, std::string> UnBlindCreds(
}
}
auto public_key = PublicKey::decode_base64(creds_batch.public_key);
auto public_key = PublicKey::decode_base64(
base::as_bytes(base::make_span(creds_batch.public_key)));
if (!public_key.has_value()) {
return base::unexpected(std::move(public_key).error());
}
@@ -222,13 +229,14 @@ std::optional<base::Value::Dict> GenerateSuggestion(
return std::nullopt;
}
auto unblinded = UnblindedToken::decode_base64(token_value);
auto unblinded = UnblindedToken::decode_base64(
base::as_bytes(base::make_span(token_value)));
if (!unblinded.has_value()) {
return std::nullopt;
}
VerificationKey verification_key = unblinded->derive_verification_key();
auto signature = verification_key.sign(body);
auto signature = verification_key.sign(base::as_bytes(base::make_span(body)));
if (!signature.has_value()) {
return std::nullopt;
}
+26 -36
View File
@@ -5,6 +5,8 @@
#include "brave/third_party/challenge_bypass_ristretto_ffi/src/wrapper.h"
#include "base/containers/span.h"
extern "C" {
#include "brave/third_party/challenge_bypass_ristretto_ffi/src/lib.h"
}
@@ -16,10 +18,9 @@ TokenPreimage::TokenPreimage(const TokenPreimage& other) = default;
TokenPreimage::~TokenPreimage() {}
base::expected<TokenPreimage, std::string> TokenPreimage::decode_base64(
const std::string encoded) {
base::span<const uint8_t> encoded) {
std::shared_ptr<C_TokenPreimage> raw_preimage(
token_preimage_decode_base64((const uint8_t*)encoded.data(),
encoded.length()),
token_preimage_decode_base64(encoded.data(), encoded.size()),
token_preimage_destroy);
if (raw_preimage == nullptr) {
return base::unexpected("Failed to decode token preimage");
@@ -68,10 +69,9 @@ base::expected<BlindedToken, std::string> Token::blind() {
}
base::expected<Token, std::string> Token::decode_base64(
const std::string encoded) {
base::span<const uint8_t> encoded) {
std::shared_ptr<C_Token> raw_tok(
token_decode_base64((const uint8_t*)encoded.data(), encoded.length()),
token_destroy);
token_decode_base64(encoded.data(), encoded.size()), token_destroy);
if (raw_tok == nullptr) {
return base::unexpected("Failed to decode token");
}
@@ -104,10 +104,9 @@ BlindedToken& BlindedToken::operator=(const BlindedToken& other) = default;
BlindedToken::~BlindedToken() {}
base::expected<BlindedToken, std::string> BlindedToken::decode_base64(
const std::string encoded) {
base::span<const uint8_t> encoded) {
std::shared_ptr<C_BlindedToken> raw_blinded(
blinded_token_decode_base64((const uint8_t*)encoded.data(),
encoded.length()),
blinded_token_decode_base64(encoded.data(), encoded.size()),
blinded_token_destroy);
if (raw_blinded == nullptr) {
return base::unexpected("Failed to decode blinded token");
@@ -138,10 +137,9 @@ SignedToken::SignedToken(const SignedToken& other) = default;
SignedToken::~SignedToken() {}
base::expected<SignedToken, std::string> SignedToken::decode_base64(
const std::string encoded) {
base::span<const uint8_t> encoded) {
std::shared_ptr<C_SignedToken> raw_signed(
signed_token_decode_base64((const uint8_t*)encoded.data(),
encoded.length()),
signed_token_decode_base64(encoded.data(), encoded.size()),
signed_token_destroy);
if (raw_signed == nullptr) {
return base::unexpected("Failed to decode signed token");
@@ -175,10 +173,9 @@ VerificationSignature::VerificationSignature(
VerificationSignature::~VerificationSignature() {}
base::expected<VerificationSignature, std::string>
VerificationSignature::decode_base64(const std::string encoded) {
VerificationSignature::decode_base64(base::span<const uint8_t> encoded) {
std::shared_ptr<C_VerificationSignature> raw_sig(
verification_signature_decode_base64((const uint8_t*)encoded.data(),
encoded.length()),
verification_signature_decode_base64(encoded.data(), encoded.size()),
verification_signature_destroy);
if (raw_sig == nullptr) {
return base::unexpected("Failed to decode verification signature");
@@ -214,10 +211,9 @@ TokenPreimage UnblindedToken::preimage() const {
}
base::expected<UnblindedToken, std::string> UnblindedToken::decode_base64(
const std::string encoded) {
base::span<const uint8_t> encoded) {
std::shared_ptr<C_UnblindedToken> raw_unblinded(
unblinded_token_decode_base64((const uint8_t*)encoded.data(),
encoded.length()),
unblinded_token_decode_base64(encoded.data(), encoded.size()),
unblinded_token_destroy);
if (raw_unblinded == nullptr) {
return base::unexpected("Failed to decode unblinded token");
@@ -249,10 +245,9 @@ VerificationKey::VerificationKey(const VerificationKey& other) = default;
VerificationKey::~VerificationKey() {}
base::expected<VerificationSignature, std::string> VerificationKey::sign(
const std::string message) {
base::span<const uint8_t> message) {
std::shared_ptr<C_VerificationSignature> raw_verification_signature(
verification_key_sign_sha512(raw.get(), (const uint8_t*)message.data(),
message.length()),
verification_key_sign_sha512(raw.get(), message.data(), message.size()),
verification_signature_destroy);
if (raw_verification_signature == nullptr) {
base::unexpected("Failed to sign message");
@@ -262,10 +257,9 @@ base::expected<VerificationSignature, std::string> VerificationKey::sign(
base::expected<bool, std::string> VerificationKey::verify(
VerificationSignature sig,
const std::string message) {
base::span<const uint8_t> message) {
int result = verification_key_invalid_sha512(raw.get(), sig.raw.get(),
(const uint8_t*)message.data(),
message.length());
message.data(), message.size());
if (result < 0) {
base::unexpected("Failed to verify message signature");
}
@@ -308,10 +302,9 @@ PublicKey SigningKey::public_key() {
}
base::expected<SigningKey, std::string> SigningKey::decode_base64(
const std::string encoded) {
base::span<const uint8_t> encoded) {
std::shared_ptr<C_SigningKey> raw_key(
signing_key_decode_base64((const uint8_t*)encoded.data(),
encoded.length()),
signing_key_decode_base64(encoded.data(), encoded.size()),
signing_key_destroy);
if (raw_key == nullptr) {
return base::unexpected("Failed to decode signing key");
@@ -342,10 +335,9 @@ PublicKey::PublicKey(const PublicKey& other) = default;
PublicKey::~PublicKey() {}
base::expected<PublicKey, std::string> PublicKey::decode_base64(
const std::string encoded) {
base::span<const uint8_t> encoded) {
std::shared_ptr<C_PublicKey> raw_key(
public_key_decode_base64((const uint8_t*)encoded.data(),
encoded.length()),
public_key_decode_base64(encoded.data(), encoded.size()),
public_key_destroy);
if (raw_key == nullptr) {
return base::unexpected("Failed to decode public key");
@@ -409,10 +401,9 @@ base::expected<bool, std::string> DLEQProof::verify(BlindedToken blinded_token,
}
base::expected<DLEQProof, std::string> DLEQProof::decode_base64(
const std::string encoded) {
base::span<const uint8_t> encoded) {
std::shared_ptr<C_DLEQProof> raw_proof(
dleq_proof_decode_base64((const uint8_t*)encoded.data(),
encoded.length()),
dleq_proof_decode_base64(encoded.data(), encoded.size()),
dleq_proof_destroy);
if (raw_proof == nullptr) {
return base::unexpected("Failed to decode DLEQ proof");
@@ -570,10 +561,9 @@ BatchDLEQProof::verify_and_unblind(std::vector<Token> tokens,
}
base::expected<BatchDLEQProof, std::string> BatchDLEQProof::decode_base64(
const std::string encoded) {
base::span<const uint8_t> encoded) {
std::shared_ptr<C_BatchDLEQProof> raw_proof(
batch_dleq_proof_decode_base64((const uint8_t*)encoded.data(),
encoded.length()),
batch_dleq_proof_decode_base64(encoded.data(), encoded.size()),
batch_dleq_proof_destroy);
if (raw_proof == nullptr) {
return base::unexpected("Failed to decode batch DLEQ proof");
+15 -12
View File
@@ -10,6 +10,7 @@
#include <string>
#include <vector>
#include "base/containers/span.h"
#include "base/types/expected.h"
extern "C" {
@@ -44,7 +45,7 @@ class CHALLENGE_BYPASS_RISTRETTO_EXPORT TokenPreimage {
TokenPreimage(const TokenPreimage&);
~TokenPreimage();
static base::expected<TokenPreimage, std::string> decode_base64(
const std::string);
base::span<const uint8_t>);
base::expected<std::string, std::string> encode_base64() const;
bool operator==(const TokenPreimage& rhs) const;
@@ -67,7 +68,7 @@ class CHALLENGE_BYPASS_RISTRETTO_EXPORT BlindedToken {
BlindedToken& operator=(BlindedToken&& other);
~BlindedToken();
static base::expected<BlindedToken, std::string> decode_base64(
const std::string);
base::span<const uint8_t>);
base::expected<std::string, std::string> encode_base64() const;
bool operator==(const BlindedToken& rhs) const;
@@ -87,7 +88,7 @@ class CHALLENGE_BYPASS_RISTRETTO_EXPORT SignedToken {
SignedToken(const SignedToken&);
~SignedToken();
static base::expected<SignedToken, std::string> decode_base64(
const std::string);
base::span<const uint8_t>);
base::expected<std::string, std::string> encode_base64() const;
bool operator==(const SignedToken& rhs) const;
@@ -105,7 +106,7 @@ class CHALLENGE_BYPASS_RISTRETTO_EXPORT VerificationSignature {
VerificationSignature(const VerificationSignature&);
~VerificationSignature();
static base::expected<VerificationSignature, std::string> decode_base64(
const std::string);
base::span<const uint8_t>);
base::expected<std::string, std::string> encode_base64() const;
private:
@@ -117,9 +118,10 @@ class CHALLENGE_BYPASS_RISTRETTO_EXPORT VerificationKey {
explicit VerificationKey(std::shared_ptr<C_VerificationKey>);
VerificationKey(const VerificationKey&);
~VerificationKey();
base::expected<VerificationSignature, std::string> sign(const std::string);
base::expected<VerificationSignature, std::string> sign(
base::span<const uint8_t>);
base::expected<bool, std::string> verify(VerificationSignature,
const std::string);
base::span<const uint8_t>);
private:
std::shared_ptr<C_VerificationKey> raw;
@@ -133,7 +135,7 @@ class CHALLENGE_BYPASS_RISTRETTO_EXPORT UnblindedToken {
VerificationKey derive_verification_key() const;
TokenPreimage preimage() const;
static base::expected<UnblindedToken, std::string> decode_base64(
const std::string);
base::span<const uint8_t>);
base::expected<std::string, std::string> encode_base64() const;
bool operator==(const UnblindedToken& rhs) const;
@@ -152,7 +154,8 @@ class CHALLENGE_BYPASS_RISTRETTO_EXPORT Token {
~Token();
static base::expected<Token, std::string> random();
base::expected<BlindedToken, std::string> blind();
static base::expected<Token, std::string> decode_base64(const std::string);
static base::expected<Token, std::string> decode_base64(
base::span<const uint8_t>);
base::expected<std::string, std::string> encode_base64() const;
bool operator==(const Token& rhs) const;
@@ -171,7 +174,7 @@ class CHALLENGE_BYPASS_RISTRETTO_EXPORT PublicKey {
PublicKey(const PublicKey&);
~PublicKey();
static base::expected<PublicKey, std::string> decode_base64(
const std::string);
base::span<const uint8_t>);
base::expected<std::string, std::string> encode_base64() const;
bool operator==(const PublicKey& rhs) const;
@@ -194,7 +197,7 @@ class CHALLENGE_BYPASS_RISTRETTO_EXPORT SigningKey {
UnblindedToken rederive_unblinded_token(TokenPreimage);
PublicKey public_key();
static base::expected<SigningKey, std::string> decode_base64(
const std::string);
base::span<const uint8_t>);
base::expected<std::string, std::string> encode_base64() const;
bool operator==(const SigningKey& rhs) const;
@@ -213,7 +216,7 @@ class CHALLENGE_BYPASS_RISTRETTO_EXPORT DLEQProof {
SignedToken,
PublicKey);
static base::expected<DLEQProof, std::string> decode_base64(
const std::string);
base::span<const uint8_t>);
base::expected<std::string, std::string> encode_base64() const;
bool operator==(const DLEQProof& rhs) const;
@@ -241,7 +244,7 @@ class CHALLENGE_BYPASS_RISTRETTO_EXPORT BatchDLEQProof {
std::vector<SignedToken>,
PublicKey);
static base::expected<BatchDLEQProof, std::string> decode_base64(
const std::string);
base::span<const uint8_t>);
base::expected<std::string, std::string> encode_base64() const;
static base::expected<BatchDLEQProof, std::string> Create(
std::vector<BlindedToken> blinded_tokens,