build: apply cpp linting and formatting to ncrypto

PR-URL: https://github.com/nodejs/node/pull/55362
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Aviv Keller 2024-11-10 12:40:16 -05:00 committed by GitHub
parent 7bcc250799
commit e7991e8da6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 438 additions and 339 deletions

View File

@ -1414,6 +1414,11 @@ LINT_CPP_EXCLUDE += $(LINT_CPP_ADDON_DOC_FILES)
# These files were copied more or less verbatim from V8.
LINT_CPP_EXCLUDE += src/tracing/trace_event.h src/tracing/trace_event_common.h
# deps/ncrypto is included in this list, as it is maintained in
# this repository, and should be linted. Eventually it should move
# to its own repo, at which point we should remove it from this list.
LINT_CPP_DEPS = deps/ncrypto/*.cc deps/ncrypto/*.h
LINT_CPP_FILES = $(filter-out $(LINT_CPP_EXCLUDE), $(wildcard \
benchmark/napi/*/*.cc \
src/*.c \
@ -1438,6 +1443,7 @@ LINT_CPP_FILES = $(filter-out $(LINT_CPP_EXCLUDE), $(wildcard \
tools/code_cache/*.h \
tools/snapshot/*.cc \
tools/snapshot/*.h \
$(LINT_CPP_DEPS) \
))
FORMAT_CPP_FILES ?=

View File

@ -50,16 +50,19 @@
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com). */
#ifndef DEPS_NCRYPTO_DH_PRIMES_H_
#define DEPS_NCRYPTO_DH_PRIMES_H_
#include <openssl/dh.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/mem.h>
extern "C" int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num);
extern "C" int bn_set_words(BIGNUM* bn, const BN_ULONG* words, size_t num);
// Backporting primes that may not be supported in earlier boringssl versions. Intentionally
// keeping the existing C-style formatting.
// Backporting primes that may not be supported in earlier boringssl versions.
// Intentionally keeping the existing C-style formatting.
#define OPENSSL_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
@ -71,25 +74,27 @@ extern "C" int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num);
#error "Must define either OPENSSL_32_BIT or OPENSSL_64_BIT"
#endif
static BIGNUM *get_params(BIGNUM *ret, const BN_ULONG *words, size_t num_words) {
BIGNUM *alloc = NULL;
if (ret == NULL) {
static BIGNUM* get_params(BIGNUM* ret,
const BN_ULONG* words,
size_t num_words) {
BIGNUM* alloc = nullptr;
if (ret == nullptr) {
alloc = BN_new();
if (alloc == NULL) {
return NULL;
if (alloc == nullptr) {
return nullptr;
}
ret = alloc;
}
if (!bn_set_words(ret, words, num_words)) {
BN_free(alloc);
return NULL;
return nullptr;
}
return ret;
}
BIGNUM *BN_get_rfc3526_prime_2048(BIGNUM *ret) {
BIGNUM* BN_get_rfc3526_prime_2048(BIGNUM* ret) {
static const BN_ULONG kWords[] = {
TOBN(0xffffffff, 0xffffffff), TOBN(0x15728e5a, 0x8aacaa68),
TOBN(0x15d22618, 0x98fa0510), TOBN(0x3995497c, 0xea956ae5),
@ -111,7 +116,7 @@ BIGNUM *BN_get_rfc3526_prime_2048(BIGNUM *ret) {
return get_params(ret, kWords, OPENSSL_ARRAY_SIZE(kWords));
}
BIGNUM *BN_get_rfc3526_prime_3072(BIGNUM *ret) {
BIGNUM* BN_get_rfc3526_prime_3072(BIGNUM* ret) {
static const BN_ULONG kWords[] = {
TOBN(0xffffffff, 0xffffffff), TOBN(0x4b82d120, 0xa93ad2ca),
TOBN(0x43db5bfc, 0xe0fd108e), TOBN(0x08e24fa0, 0x74e5ab31),
@ -141,7 +146,7 @@ BIGNUM *BN_get_rfc3526_prime_3072(BIGNUM *ret) {
return get_params(ret, kWords, OPENSSL_ARRAY_SIZE(kWords));
}
BIGNUM *BN_get_rfc3526_prime_4096(BIGNUM *ret) {
BIGNUM* BN_get_rfc3526_prime_4096(BIGNUM* ret) {
static const BN_ULONG kWords[] = {
TOBN(0xffffffff, 0xffffffff), TOBN(0x4df435c9, 0x34063199),
TOBN(0x86ffb7dc, 0x90a6c08f), TOBN(0x93b4ea98, 0x8d8fddc1),
@ -179,7 +184,7 @@ BIGNUM *BN_get_rfc3526_prime_4096(BIGNUM *ret) {
return get_params(ret, kWords, OPENSSL_ARRAY_SIZE(kWords));
}
BIGNUM *BN_get_rfc3526_prime_6144(BIGNUM *ret) {
BIGNUM* BN_get_rfc3526_prime_6144(BIGNUM* ret) {
static const BN_ULONG kWords[] = {
TOBN(0xffffffff, 0xffffffff), TOBN(0xe694f91e, 0x6dcc4024),
TOBN(0x12bf2d5b, 0x0b7474d6), TOBN(0x043e8f66, 0x3f4860ee),
@ -233,7 +238,7 @@ BIGNUM *BN_get_rfc3526_prime_6144(BIGNUM *ret) {
return get_params(ret, kWords, OPENSSL_ARRAY_SIZE(kWords));
}
BIGNUM *BN_get_rfc3526_prime_8192(BIGNUM *ret) {
BIGNUM* BN_get_rfc3526_prime_8192(BIGNUM* ret) {
static const BN_ULONG kWords[] = {
TOBN(0xffffffff, 0xffffffff), TOBN(0x60c980dd, 0x98edd3df),
TOBN(0xc81f56e8, 0x80b96e71), TOBN(0x9e3050e2, 0x765694df),
@ -302,3 +307,5 @@ BIGNUM *BN_get_rfc3526_prime_8192(BIGNUM *ret) {
};
return get_params(ret, kWords, OPENSSL_ARRAY_SIZE(kWords));
}
#endif // DEPS_NCRYPTO_DH_PRIMES_H_

View File

@ -7,16 +7,16 @@ namespace ncrypto {
#ifndef OPENSSL_NO_ENGINE
EnginePointer::EnginePointer(ENGINE* engine_, bool finish_on_exit_)
: engine(engine_),
finish_on_exit(finish_on_exit_) {}
: engine(engine_), finish_on_exit(finish_on_exit_) {}
EnginePointer::EnginePointer(EnginePointer&& other) noexcept
: engine(other.engine),
finish_on_exit(other.finish_on_exit) {
: engine(other.engine), finish_on_exit(other.finish_on_exit) {
other.release();
}
EnginePointer::~EnginePointer() { reset(); }
EnginePointer::~EnginePointer() {
reset();
}
EnginePointer& EnginePointer::operator=(EnginePointer&& other) noexcept {
if (this == &other) return *this;
@ -75,7 +75,8 @@ bool EnginePointer::init(bool finish_on_exit) {
EVPKeyPointer EnginePointer::loadPrivateKey(const std::string_view key_name) {
if (engine == nullptr) return EVPKeyPointer();
return EVPKeyPointer(ENGINE_load_private_key(engine, key_name.data(), nullptr, nullptr));
return EVPKeyPointer(
ENGINE_load_private_key(engine, key_name.data(), nullptr, nullptr));
}
void EnginePointer::initEnginesOnce() {

File diff suppressed because it is too large Load Diff

120
deps/ncrypto/ncrypto.h vendored
View File

@ -1,11 +1,5 @@
#pragma once
#include <cstddef>
#include <list>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/dh.h>
@ -18,13 +12,19 @@
#include <openssl/rsa.h>
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <cstddef>
#include <list>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#ifndef OPENSSL_NO_ENGINE
# include <openssl/engine.h>
#include <openssl/engine.h>
#endif // !OPENSSL_NO_ENGINE
// The FIPS-related functions are only available
// when the OpenSSL itself was compiled with FIPS support.
#if defined(OPENSSL_FIPS) && OPENSSL_VERSION_MAJOR < 3
# include <openssl/fips.h>
#include <openssl/fips.h>
#endif // OPENSSL_FIPS
#ifdef __GNUC__
@ -93,11 +93,8 @@ namespace ncrypto {
}
static constexpr int kX509NameFlagsMultiline =
ASN1_STRFLGS_ESC_2253 |
ASN1_STRFLGS_ESC_CTRL |
ASN1_STRFLGS_UTF8_CONVERT |
XN_FLAG_SEP_MULTILINE |
XN_FLAG_FN_SN;
ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_UTF8_CONVERT |
XN_FLAG_SEP_MULTILINE | XN_FLAG_FN_SN;
// ============================================================================
// Error handling utilities
@ -106,11 +103,8 @@ static constexpr int kX509NameFlagsMultiline =
// that the error currently at the top of the stack is at the end of the
// list and the error at the bottom of the stack is at the beginning.
class CryptoErrorList final {
public:
enum class Option {
NONE,
CAPTURE_ON_CONSTRUCT
};
public:
enum class Option { NONE, CAPTURE_ON_CONSTRUCT };
CryptoErrorList(Option option = Option::CAPTURE_ON_CONSTRUCT);
void capture();
@ -130,7 +124,7 @@ public:
std::optional<std::string> pop_back();
std::optional<std::string> pop_front();
private:
private:
std::list<std::string> errors_;
};
@ -142,7 +136,7 @@ private:
// If created with a pointer to a CryptoErrorList, the current OpenSSL error
// stack will be captured before clearing the error.
class ClearErrorOnReturn final {
public:
public:
ClearErrorOnReturn(CryptoErrorList* errors = nullptr);
~ClearErrorOnReturn();
NCRYPTO_DISALLOW_COPY_AND_MOVE(ClearErrorOnReturn)
@ -150,7 +144,7 @@ public:
int peekError();
private:
private:
CryptoErrorList* errors_;
};
@ -160,7 +154,7 @@ private:
// If created with a pointer to a CryptoErrorList, the current OpenSSL error
// stack will be captured before resetting the error to the mark.
class MarkPopErrorOnReturn final {
public:
public:
MarkPopErrorOnReturn(CryptoErrorList* errors = nullptr);
~MarkPopErrorOnReturn();
NCRYPTO_DISALLOW_COPY_AND_MOVE(MarkPopErrorOnReturn)
@ -168,7 +162,7 @@ public:
int peekError();
private:
private:
CryptoErrorList* errors_;
};
@ -220,7 +214,7 @@ using SSLPointer = DeleteFnPtr<SSL, SSL_free>;
using SSLSessionPointer = DeleteFnPtr<SSL_SESSION, SSL_SESSION_free>;
struct StackOfXASN1Deleter {
void operator()(STACK_OF(ASN1_OBJECT)* p) const {
void operator()(STACK_OF(ASN1_OBJECT) * p) const {
sk_ASN1_OBJECT_pop_free(p, ASN1_OBJECT_free);
}
};
@ -262,8 +256,8 @@ class DataPointer final {
template <typename T = void>
inline operator const Buffer<T>() const {
return {
.data = static_cast<T*>(data_),
.len = len_,
.data = static_cast<T*>(data_),
.len = len_,
};
}
@ -273,7 +267,7 @@ class DataPointer final {
};
class BIOPointer final {
public:
public:
static BIOPointer NewMem();
static BIOPointer NewSecMem();
static BIOPointer New(const BIO_METHOD* method);
@ -314,13 +308,13 @@ public:
static int Write(BIOPointer* bio, std::string_view message);
template <typename...Args>
static void Printf(BIOPointer* bio, const char* format, Args...args) {
template <typename... Args>
static void Printf(BIOPointer* bio, const char* format, Args... args) {
if (bio == nullptr || !*bio) return;
BIO_printf(bio->get(), format, std::forward<Args...>(args...));
}
private:
private:
mutable DeleteFnPtr<BIO, BIO_free_all> bio_;
};
@ -345,8 +339,8 @@ class BignumPointer final {
bool isZero() const;
bool isOne() const;
bool setWord(unsigned long w);
unsigned long getWord() const;
bool setWord(unsigned long w); // NOLINT(runtime/int)
unsigned long getWord() const; // NOLINT(runtime/int)
size_t byteLength() const;
@ -360,10 +354,12 @@ class BignumPointer final {
static BignumPointer NewSecure();
static DataPointer Encode(const BIGNUM* bn);
static DataPointer EncodePadded(const BIGNUM* bn, size_t size);
static size_t EncodePaddedInto(const BIGNUM* bn, unsigned char* out, size_t size);
static size_t EncodePaddedInto(const BIGNUM* bn,
unsigned char* out,
size_t size);
static int GetBitCount(const BIGNUM* bn);
static int GetByteCount(const BIGNUM* bn);
static unsigned long GetWord(const BIGNUM* bn);
static unsigned long GetWord(const BIGNUM* bn); // NOLINT(runtime/int)
static const BIGNUM* One();
BignumPointer clone();
@ -373,10 +369,12 @@ class BignumPointer final {
};
class EVPKeyPointer final {
public:
public:
static EVPKeyPointer New();
static EVPKeyPointer NewRawPublic(int id, const Buffer<const unsigned char>& data);
static EVPKeyPointer NewRawPrivate(int id, const Buffer<const unsigned char>& data);
static EVPKeyPointer NewRawPublic(int id,
const Buffer<const unsigned char>& data);
static EVPKeyPointer NewRawPrivate(int id,
const Buffer<const unsigned char>& data);
enum class PKEncodingType {
// RSAPublicKey / RSAPrivateKey according to PKCS#1.
@ -395,11 +393,7 @@ public:
JWK,
};
enum class PKParseError {
NOT_RECOGNIZED,
NEED_PASSPHRASE,
FAILED
};
enum class PKParseError { NOT_RECOGNIZED, NEED_PASSPHRASE, FAILED };
using ParseKeyResult = Result<EVPKeyPointer, PKParseError>;
struct AsymmetricKeyEncodingConfig {
@ -407,17 +401,22 @@ public:
PKFormatType format = PKFormatType::DER;
PKEncodingType type = PKEncodingType::PKCS8;
AsymmetricKeyEncodingConfig() = default;
AsymmetricKeyEncodingConfig(bool output_key_object, PKFormatType format, PKEncodingType type);
AsymmetricKeyEncodingConfig(bool output_key_object,
PKFormatType format,
PKEncodingType type);
AsymmetricKeyEncodingConfig(const AsymmetricKeyEncodingConfig&) = default;
AsymmetricKeyEncodingConfig& operator=(const AsymmetricKeyEncodingConfig&) = default;
AsymmetricKeyEncodingConfig& operator=(const AsymmetricKeyEncodingConfig&) =
default;
};
using PublicKeyEncodingConfig = AsymmetricKeyEncodingConfig;
struct PrivateKeyEncodingConfig: public AsymmetricKeyEncodingConfig {
struct PrivateKeyEncodingConfig : public AsymmetricKeyEncodingConfig {
const EVP_CIPHER* cipher = nullptr;
std::optional<DataPointer> passphrase = std::nullopt;
PrivateKeyEncodingConfig() = default;
PrivateKeyEncodingConfig(bool output_key_object, PKFormatType format, PKEncodingType type)
PrivateKeyEncodingConfig(bool output_key_object,
PKFormatType format,
PKEncodingType type)
: AsymmetricKeyEncodingConfig(output_key_object, format, type) {}
PrivateKeyEncodingConfig(const PrivateKeyEncodingConfig&);
PrivateKeyEncodingConfig& operator=(const PrivateKeyEncodingConfig&);
@ -441,7 +440,9 @@ public:
NCRYPTO_DISALLOW_COPY(EVPKeyPointer)
~EVPKeyPointer();
inline bool operator==(std::nullptr_t) const noexcept { return pkey_ == nullptr; }
inline bool operator==(std::nullptr_t) const noexcept {
return pkey_ == nullptr;
}
inline operator bool() const { return pkey_ != nullptr; }
inline EVP_PKEY* get() const { return pkey_.get(); }
void reset(EVP_PKEY* pkey = nullptr);
@ -461,20 +462,21 @@ public:
DataPointer rawPrivateKey() const;
BIOPointer derPublicKey() const;
Result<BIOPointer, bool> writePrivateKey(const PrivateKeyEncodingConfig& config) const;
Result<BIOPointer, bool> writePublicKey(const PublicKeyEncodingConfig& config) const;
Result<BIOPointer, bool> writePrivateKey(
const PrivateKeyEncodingConfig& config) const;
Result<BIOPointer, bool> writePublicKey(
const PublicKeyEncodingConfig& config) const;
EVPKeyCtxPointer newCtx() const;
static bool IsRSAPrivateKey(const Buffer<const unsigned char>& buffer);
private:
private:
DeleteFnPtr<EVP_PKEY, EVP_PKEY_free> pkey_;
};
class DHPointer final {
public:
public:
enum class FindGroupOption {
NONE,
// There are known and documented security issues with prime groups smaller
@ -485,8 +487,9 @@ public:
static BignumPointer GetStandardGenerator();
static BignumPointer FindGroup(const std::string_view name,
FindGroupOption option = FindGroupOption::NONE);
static BignumPointer FindGroup(
const std::string_view name,
FindGroupOption option = FindGroupOption::NONE);
static DHPointer FromGroup(const std::string_view name,
FindGroupOption option = FindGroupOption::NONE);
@ -544,7 +547,7 @@ public:
static DataPointer stateless(const EVPKeyPointer& ourKey,
const EVPKeyPointer& theirKey);
private:
private:
DeleteFnPtr<DH, DH_free> dh_;
};
@ -594,7 +597,8 @@ class X509View final {
INVALID_NAME,
OPERATION_FAILED,
};
CheckMatch checkHost(const std::string_view host, int flags,
CheckMatch checkHost(const std::string_view host,
int flags,
DataPointer* peerName = nullptr) const;
CheckMatch checkEmail(const std::string_view email, int flags) const;
CheckMatch checkIp(const std::string_view ip, int flags) const;
@ -632,7 +636,7 @@ class X509Pointer final {
#ifndef OPENSSL_NO_ENGINE
class EnginePointer final {
public:
public:
EnginePointer() = default;
explicit EnginePointer(ENGINE* engine_, bool finish_on_exit = false);
@ -663,7 +667,7 @@ public:
// Call once when initializing OpenSSL at startup for the process.
static void initEnginesOnce();
private:
private:
ENGINE* engine = nullptr;
bool finish_on_exit = false;
};