Skip to content

Commit d45bc0e

Browse files
committed
feat: add MSVC support and implement ECDSA key validatio
1 parent 8e7284c commit d45bc0e

2 files changed

Lines changed: 49 additions & 7 deletions

File tree

src/crypto/key_validator/key_validator_impl.cpp

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,57 @@ namespace libp2p::crypto::validator {
170170

171171
outcome::result<void> KeyValidatorImpl::validateEcdsa(
172172
const PrivateKey &key) const {
173-
// TODO(xDimon): Check if it possible to validate ECDSA key by some way.
174-
// issue: https://github.com/libp2p/cpp-libp2p/issues/103
173+
// Basic ECDSA private key validation
174+
// ECDSA private keys are typically 32 bytes for P-256, 48 bytes for P-384, 66 bytes for P-521
175+
if (key.data.empty()) {
176+
return KeyValidatorError::INVALID_PRIVATE_KEY;
177+
}
178+
179+
// Check for reasonable key sizes (32-66 bytes covers most common curves)
180+
if (key.data.size() < 32 || key.data.size() > 66) {
181+
return KeyValidatorError::WRONG_PRIVATE_KEY_SIZE;
182+
}
183+
184+
// Check that the key is not all zeros (invalid private key)
185+
bool all_zeros = true;
186+
for (const auto& byte : key.data) {
187+
if (byte != 0) {
188+
all_zeros = false;
189+
break;
190+
}
191+
}
192+
if (all_zeros) {
193+
return KeyValidatorError::INVALID_PRIVATE_KEY;
194+
}
195+
175196
return outcome::success();
176197
}
177198

178199
outcome::result<void> KeyValidatorImpl::validateEcdsa(
179200
const PublicKey &key) const {
180-
// TODO(xDimon): Check if it possible to validate ECDSA key by some way.
181-
// issue: https://github.com/libp2p/cpp-libp2p/issues/103
201+
// Basic ECDSA public key validation
202+
if (key.data.empty()) {
203+
return KeyValidatorError::INVALID_PUBLIC_KEY;
204+
}
205+
206+
// ECDSA public keys are typically 64 bytes (uncompressed) or 33/49/67 bytes (compressed)
207+
// for P-256/P-384/P-521 respectively
208+
if (key.data.size() < 33 || key.data.size() > 133) {
209+
return KeyValidatorError::WRONG_PUBLIC_KEY_SIZE;
210+
}
211+
212+
// Check that the key is not all zeros (invalid public key)
213+
bool all_zeros = true;
214+
for (const auto& byte : key.data) {
215+
if (byte != 0) {
216+
all_zeros = false;
217+
break;
218+
}
219+
}
220+
if (all_zeros) {
221+
return KeyValidatorError::INVALID_PUBLIC_KEY;
222+
}
223+
182224
return outcome::success();
183225
}
184226

src/storage/sqlite.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ namespace libp2p::storage {
2222
}
2323
}
2424

25-
int SQLite::getErrorCode() {
25+
int SQLite::getErrorCode() const {
2626
return sqlite3_extended_errcode(db_.connection().get());
2727
}
2828

29-
std::string SQLite::getErrorMessage() {
30-
int ec{getErrorCode()};
29+
std::string SQLite::getErrorMessage() const {
30+
const int ec{getErrorCode()};
3131
return (0 == ec) ? std::string()
3232
: std::string(sqlite3_errstr(ec)) + ": "
3333
+ sqlite3_errmsg(db_.connection().get());

0 commit comments

Comments
 (0)