mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
src: clean up some obsolete crypto methods
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: https://github.com/nodejs/node/pull/56792 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
1 parent
abca97f7e2
commit
94a0237b18
27 files changed
+678
-665
No files matched your search
Vendored
+130
-2
@@ -915,6 +915,18 @@ BIOPointer X509View::toDER() const {
|
||||
return bio;
|
||||
}
|
||||
|
||||
const X509Name X509View::getSubjectName() const {
|
||||
ClearErrorOnReturn clearErrorOnReturn;
|
||||
if (cert_ == nullptr) return {};
|
||||
return X509Name(X509_get_subject_name(cert_));
|
||||
}
|
||||
|
||||
const X509Name X509View::getIssuerName() const {
|
||||
ClearErrorOnReturn clearErrorOnReturn;
|
||||
if (cert_ == nullptr) return {};
|
||||
return X509Name(X509_get_issuer_name(cert_));
|
||||
}
|
||||
|
||||
BIOPointer X509View::getSubject() const {
|
||||
ClearErrorOnReturn clearErrorOnReturn;
|
||||
if (cert_ == nullptr) return {};
|
||||
@@ -2390,6 +2402,15 @@ EVPKeyPointer::operator Rsa() const {
|
||||
return Rsa(rsa);
|
||||
}
|
||||
|
||||
EVPKeyPointer::operator Dsa() const {
|
||||
int type = id();
|
||||
if (type != EVP_PKEY_DSA) return {};
|
||||
|
||||
OSSL3_CONST DSA* dsa = EVP_PKEY_get0_DSA(get());
|
||||
if (dsa == nullptr) return {};
|
||||
return Dsa(dsa);
|
||||
}
|
||||
|
||||
bool EVPKeyPointer::validateDsaParameters() const {
|
||||
if (!pkey_) return false;
|
||||
/* Validate DSA2 parameters from FIPS 186-4 */
|
||||
@@ -2585,6 +2606,24 @@ EVPKeyPointer SSLPointer::getPeerTempKey() const {
|
||||
return EVPKeyPointer(raw_key);
|
||||
}
|
||||
|
||||
std::optional<std::string_view> SSLPointer::getCipherName() const {
|
||||
auto cipher = getCipher();
|
||||
if (cipher == nullptr) return std::nullopt;
|
||||
return SSL_CIPHER_get_name(cipher);
|
||||
}
|
||||
|
||||
std::optional<std::string_view> SSLPointer::getCipherStandardName() const {
|
||||
auto cipher = getCipher();
|
||||
if (cipher == nullptr) return std::nullopt;
|
||||
return SSL_CIPHER_standard_name(cipher);
|
||||
}
|
||||
|
||||
std::optional<std::string_view> SSLPointer::getCipherVersion() const {
|
||||
auto cipher = getCipher();
|
||||
if (cipher == nullptr) return std::nullopt;
|
||||
return SSL_CIPHER_get_version(cipher);
|
||||
}
|
||||
|
||||
SSLCtxPointer::SSLCtxPointer(SSL_CTX* ctx) : ctx_(ctx) {}
|
||||
|
||||
SSLCtxPointer::SSLCtxPointer(SSLCtxPointer&& other) noexcept
|
||||
@@ -2630,8 +2669,8 @@ bool SSLCtxPointer::setGroups(const char* groups) {
|
||||
|
||||
// ============================================================================
|
||||
|
||||
const Cipher Cipher::FromName(const char* name) {
|
||||
return Cipher(EVP_get_cipherbyname(name));
|
||||
const Cipher Cipher::FromName(std::string_view name) {
|
||||
return Cipher(EVP_get_cipherbyname(name.data()));
|
||||
}
|
||||
|
||||
const Cipher Cipher::FromNid(int nid) {
|
||||
@@ -3813,4 +3852,93 @@ DataPointer hashDigest(const Buffer<const unsigned char>& buf,
|
||||
return data.resize(result_size);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
||||
X509Name::X509Name() : name_(nullptr), total_(0) {}
|
||||
|
||||
X509Name::X509Name(const X509_NAME* name)
|
||||
: name_(name), total_(X509_NAME_entry_count(name)) {}
|
||||
|
||||
X509Name::Iterator::Iterator(const X509Name& name, int pos)
|
||||
: name_(name), loc_(pos) {}
|
||||
|
||||
X509Name::Iterator& X509Name::Iterator::operator++() {
|
||||
++loc_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
X509Name::Iterator::operator bool() const {
|
||||
return loc_ < name_.total_;
|
||||
}
|
||||
|
||||
bool X509Name::Iterator::operator==(const Iterator& other) const {
|
||||
return loc_ == other.loc_;
|
||||
}
|
||||
|
||||
bool X509Name::Iterator::operator!=(const Iterator& other) const {
|
||||
return loc_ != other.loc_;
|
||||
}
|
||||
|
||||
std::pair<std::string, std::string> X509Name::Iterator::operator*() const {
|
||||
if (loc_ == name_.total_) return {{}, {}};
|
||||
|
||||
X509_NAME_ENTRY* entry = X509_NAME_get_entry(name_, loc_);
|
||||
if (entry == nullptr) [[unlikely]]
|
||||
return {{}, {}};
|
||||
|
||||
ASN1_OBJECT* name = X509_NAME_ENTRY_get_object(entry);
|
||||
ASN1_STRING* value = X509_NAME_ENTRY_get_data(entry);
|
||||
|
||||
if (name == nullptr || value == nullptr) [[unlikely]] {
|
||||
return {{}, {}};
|
||||
}
|
||||
|
||||
int nid = OBJ_obj2nid(name);
|
||||
std::string name_str;
|
||||
if (nid != NID_undef) {
|
||||
name_str = std::string(OBJ_nid2sn(nid));
|
||||
} else {
|
||||
char buf[80];
|
||||
OBJ_obj2txt(buf, sizeof(buf), name, 0);
|
||||
name_str = std::string(buf);
|
||||
}
|
||||
|
||||
unsigned char* value_str;
|
||||
int value_str_size = ASN1_STRING_to_UTF8(&value_str, value);
|
||||
|
||||
return {
|
||||
std::move(name_str),
|
||||
std::string(reinterpret_cast<const char*>(value_str), value_str_size)};
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
||||
Dsa::Dsa() : dsa_(nullptr) {}
|
||||
|
||||
Dsa::Dsa(OSSL3_CONST DSA* dsa) : dsa_(dsa) {}
|
||||
|
||||
const BIGNUM* Dsa::getP() const {
|
||||
if (dsa_ == nullptr) return nullptr;
|
||||
const BIGNUM* p;
|
||||
DSA_get0_pqg(dsa_, &p, nullptr, nullptr);
|
||||
return p;
|
||||
}
|
||||
|
||||
const BIGNUM* Dsa::getQ() const {
|
||||
if (dsa_ == nullptr) return nullptr;
|
||||
const BIGNUM* q;
|
||||
DSA_get0_pqg(dsa_, nullptr, &q, nullptr);
|
||||
return q;
|
||||
}
|
||||
|
||||
size_t Dsa::getModulusLength() const {
|
||||
if (dsa_ == nullptr) return 0;
|
||||
return BignumPointer::GetBitCount(getP());
|
||||
}
|
||||
|
||||
size_t Dsa::getDivisorLength() const {
|
||||
if (dsa_ == nullptr) return 0;
|
||||
return BignumPointer::GetBitCount(getQ());
|
||||
}
|
||||
|
||||
} // namespace ncrypto
|
||||
Vendored
+77
-2
@@ -221,6 +221,7 @@ class ECDSASigPointer;
|
||||
class ECGroupPointer;
|
||||
class ECPointPointer;
|
||||
class ECKeyPointer;
|
||||
class Dsa;
|
||||
class Rsa;
|
||||
class Ec;
|
||||
|
||||
@@ -267,7 +268,7 @@ class Cipher final {
|
||||
|
||||
bool isSupportedAuthenticatedMode() const;
|
||||
|
||||
static const Cipher FromName(const char* name);
|
||||
static const Cipher FromName(std::string_view name);
|
||||
static const Cipher FromNid(int nid);
|
||||
static const Cipher FromCtx(const CipherCtxPointer& ctx);
|
||||
|
||||
@@ -292,10 +293,35 @@ class Cipher final {
|
||||
const CipherParams& params,
|
||||
const Buffer<const void> in);
|
||||
|
||||
static constexpr bool IsValidGCMTagLength(unsigned int tag_len) {
|
||||
return tag_len == 4 || tag_len == 8 || (tag_len >= 12 && tag_len <= 16);
|
||||
}
|
||||
|
||||
private:
|
||||
const EVP_CIPHER* cipher_ = nullptr;
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// DSA
|
||||
|
||||
class Dsa final {
|
||||
public:
|
||||
Dsa();
|
||||
Dsa(OSSL3_CONST DSA* dsa);
|
||||
NCRYPTO_DISALLOW_COPY_AND_MOVE(Dsa)
|
||||
|
||||
inline operator bool() const { return dsa_ != nullptr; }
|
||||
inline operator OSSL3_CONST DSA*() const { return dsa_; }
|
||||
|
||||
const BIGNUM* getP() const;
|
||||
const BIGNUM* getQ() const;
|
||||
size_t getModulusLength() const;
|
||||
size_t getDivisorLength() const;
|
||||
|
||||
private:
|
||||
OSSL3_CONST DSA* dsa_;
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// RSA
|
||||
|
||||
@@ -384,7 +410,12 @@ class DataPointer final {
|
||||
|
||||
inline bool operator==(std::nullptr_t) noexcept { return data_ == nullptr; }
|
||||
inline operator bool() const { return data_ != nullptr; }
|
||||
inline void* get() const noexcept { return data_; }
|
||||
|
||||
template <typename T = void>
|
||||
inline T* get() const noexcept {
|
||||
return static_cast<T*>(data_);
|
||||
}
|
||||
|
||||
inline size_t size() const noexcept { return len_; }
|
||||
void reset(void* data = nullptr, size_t len = 0);
|
||||
void reset(const Buffer<void>& buffer);
|
||||
@@ -762,6 +793,7 @@ class EVPKeyPointer final {
|
||||
std::optional<uint32_t> getBytesOfRS() const;
|
||||
int getDefaultSignPadding() const;
|
||||
operator Rsa() const;
|
||||
operator Dsa() const;
|
||||
|
||||
bool isRsaVariant() const;
|
||||
bool isOneShotVariant() const;
|
||||
@@ -914,6 +946,10 @@ class SSLPointer final {
|
||||
const SSL_CIPHER* getCipher() const;
|
||||
bool isServer() const;
|
||||
|
||||
std::optional<std::string_view> getCipherName() const;
|
||||
std::optional<std::string_view> getCipherStandardName() const;
|
||||
std::optional<std::string_view> getCipherVersion() const;
|
||||
|
||||
std::optional<uint32_t> verifyPeerCertificate() const;
|
||||
|
||||
void getCiphers(std::function<void(const std::string_view)> cb) const;
|
||||
@@ -925,6 +961,43 @@ class SSLPointer final {
|
||||
DeleteFnPtr<SSL, SSL_free> ssl_;
|
||||
};
|
||||
|
||||
class X509Name final {
|
||||
public:
|
||||
X509Name();
|
||||
explicit X509Name(const X509_NAME* name);
|
||||
NCRYPTO_DISALLOW_COPY_AND_MOVE(X509Name)
|
||||
|
||||
inline operator const X509_NAME*() const { return name_; }
|
||||
inline operator bool() const { return name_ != nullptr; }
|
||||
inline const X509_NAME* get() const { return name_; }
|
||||
inline size_t size() const { return total_; }
|
||||
|
||||
class Iterator final {
|
||||
public:
|
||||
Iterator(const X509Name& name, int pos);
|
||||
Iterator(const Iterator& other) = default;
|
||||
Iterator(Iterator&& other) = default;
|
||||
Iterator& operator=(const Iterator& other) = delete;
|
||||
Iterator& operator=(Iterator&& other) = delete;
|
||||
Iterator& operator++();
|
||||
operator bool() const;
|
||||
bool operator==(const Iterator& other) const;
|
||||
bool operator!=(const Iterator& other) const;
|
||||
std::pair<std::string, std::string> operator*() const;
|
||||
|
||||
private:
|
||||
const X509Name& name_;
|
||||
int loc_;
|
||||
};
|
||||
|
||||
inline Iterator begin() const { return Iterator(*this, 0); }
|
||||
inline Iterator end() const { return Iterator(*this, total_); }
|
||||
|
||||
private:
|
||||
const X509_NAME* name_;
|
||||
int total_;
|
||||
};
|
||||
|
||||
class X509View final {
|
||||
public:
|
||||
static X509View From(const SSLPointer& ssl);
|
||||
@@ -946,6 +1019,8 @@ class X509View final {
|
||||
BIOPointer toPEM() const;
|
||||
BIOPointer toDER() const;
|
||||
|
||||
const X509Name getSubjectName() const;
|
||||
const X509Name getIssuerName() const;
|
||||
BIOPointer getSubject() const;
|
||||
BIOPointer getSubjectAltName() const;
|
||||
BIOPointer getIssuer() const;
|
||||
|
||||
@@ -106,10 +106,6 @@ an `ArrayBuffer` (`v8::BackingStore`), or allocated data.
|
||||
* If allocated data is used, then it must have been allocated using OpenSSL's
|
||||
allocator. It will be freed automatically when the `ByteSource` is destroyed.
|
||||
|
||||
The `ByteSource::Builder` class can be used to allocate writable memory that can
|
||||
then be released as a `ByteSource`, making it read-only, or freed by destroying
|
||||
the `ByteSource::Builder` without releasing it as a `ByteSource`.
|
||||
|
||||
### `ArrayBufferOrViewContents`
|
||||
|
||||
The `ArrayBufferOrViewContents` class is a helper utility that abstracts
|
||||
|
||||
+48
-30
@@ -19,6 +19,7 @@ namespace node {
|
||||
using ncrypto::BignumPointer;
|
||||
using ncrypto::Cipher;
|
||||
using ncrypto::CipherCtxPointer;
|
||||
using ncrypto::DataPointer;
|
||||
using v8::FunctionCallbackInfo;
|
||||
using v8::Just;
|
||||
using v8::JustVoid;
|
||||
@@ -31,6 +32,9 @@ using v8::Value;
|
||||
|
||||
namespace crypto {
|
||||
namespace {
|
||||
constexpr size_t kAesBlockSize = 16;
|
||||
constexpr const char* kDefaultWrapIV = "\xa6\xa6\xa6\xa6\xa6\xa6\xa6\xa6";
|
||||
|
||||
// Implements general AES encryption and decryption for CBC
|
||||
// The key_data must be a secret key.
|
||||
// On success, this function sets out to a new ByteSource
|
||||
@@ -113,15 +117,17 @@ WebCryptoCipherStatus AES_Cipher(Environment* env,
|
||||
return WebCryptoCipherStatus::FAILED;
|
||||
}
|
||||
|
||||
ByteSource::Builder buf(buf_len);
|
||||
auto buf = DataPointer::Alloc(buf_len);
|
||||
auto ptr = static_cast<unsigned char*>(buf.get());
|
||||
|
||||
// In some outdated version of OpenSSL (e.g.
|
||||
// ubi81_sharedlibs_openssl111fips_x64) may be used in sharedlib mode, the
|
||||
// logic will be failed when input size is zero. The newly OpenSSL has fixed
|
||||
// logic will be failed when input size is zero. The newer OpenSSL has fixed
|
||||
// it up. But we still have to regard zero as special in Node.js code to
|
||||
// prevent old OpenSSL failure.
|
||||
//
|
||||
// Refs: https://github.com/openssl/openssl/commit/420cb707b880e4fb649094241371701013eeb15f
|
||||
// Refs:
|
||||
// https://github.com/openssl/openssl/commit/420cb707b880e4fb649094241371701013eeb15f
|
||||
// Refs: https://github.com/nodejs/node/pull/38913#issuecomment-866505244
|
||||
buffer = {
|
||||
.data = in.data<unsigned char>(),
|
||||
@@ -129,14 +135,14 @@ WebCryptoCipherStatus AES_Cipher(Environment* env,
|
||||
};
|
||||
if (in.empty()) {
|
||||
out_len = 0;
|
||||
} else if (!ctx.update(buffer, buf.data<unsigned char>(), &out_len)) {
|
||||
} else if (!ctx.update(buffer, ptr, &out_len)) {
|
||||
return WebCryptoCipherStatus::FAILED;
|
||||
}
|
||||
|
||||
total += out_len;
|
||||
CHECK_LE(out_len, buf_len);
|
||||
out_len = ctx.getBlockSize();
|
||||
if (!ctx.update({}, buf.data<unsigned char>() + total, &out_len, true)) {
|
||||
if (!ctx.update({}, ptr + total, &out_len, true)) {
|
||||
return WebCryptoCipherStatus::FAILED;
|
||||
}
|
||||
total += out_len;
|
||||
@@ -144,14 +150,20 @@ WebCryptoCipherStatus AES_Cipher(Environment* env,
|
||||
// If using AES_GCM, grab the generated auth tag and append
|
||||
// it to the end of the ciphertext.
|
||||
if (cipher_mode == kWebCryptoCipherEncrypt && mode == EVP_CIPH_GCM_MODE) {
|
||||
if (!ctx.getAeadTag(tag_len, buf.data<unsigned char>() + total)) {
|
||||
if (!ctx.getAeadTag(tag_len, ptr + total)) {
|
||||
return WebCryptoCipherStatus::FAILED;
|
||||
}
|
||||
total += tag_len;
|
||||
}
|
||||
|
||||
if (total == 0) {
|
||||
*out = ByteSource::Allocated(nullptr, 0);
|
||||
return WebCryptoCipherStatus::OK;
|
||||
}
|
||||
|
||||
// It's possible that we haven't used the full allocated space. Size down.
|
||||
*out = std::move(buf).release(total);
|
||||
buf = buf.resize(total);
|
||||
*out = ByteSource::Allocated(buf.release());
|
||||
|
||||
return WebCryptoCipherStatus::OK;
|
||||
}
|
||||
@@ -213,11 +225,10 @@ WebCryptoCipherStatus AES_CTR_Cipher2(const KeyObjectData& key_data,
|
||||
if (!ctx) {
|
||||
return WebCryptoCipherStatus::FAILED;
|
||||
}
|
||||
const bool encrypt = cipher_mode == kWebCryptoCipherEncrypt;
|
||||
|
||||
if (!ctx.init(
|
||||
params.cipher,
|
||||
encrypt,
|
||||
cipher_mode == kWebCryptoCipherEncrypt,
|
||||
reinterpret_cast<const unsigned char*>(key_data.GetSymmetricKey()),
|
||||
counter)) {
|
||||
// Cipher init failed
|
||||
@@ -230,19 +241,14 @@ WebCryptoCipherStatus AES_CTR_Cipher2(const KeyObjectData& key_data,
|
||||
.data = in.data<unsigned char>(),
|
||||
.len = in.size(),
|
||||
};
|
||||
if (!ctx.update(buffer, out, &out_len)) {
|
||||
if (!ctx.update(buffer, out, &out_len) ||
|
||||
!ctx.update({}, out + out_len, &final_len, true)) {
|
||||
return WebCryptoCipherStatus::FAILED;
|
||||
}
|
||||
|
||||
if (!ctx.update({}, out + out_len, &final_len, true)) {
|
||||
return WebCryptoCipherStatus::FAILED;
|
||||
}
|
||||
|
||||
out_len += final_len;
|
||||
if (static_cast<unsigned>(out_len) != in.size())
|
||||
return WebCryptoCipherStatus::FAILED;
|
||||
|
||||
return WebCryptoCipherStatus::OK;
|
||||
return static_cast<unsigned>(out_len + final_len) != in.size()
|
||||
? WebCryptoCipherStatus::FAILED
|
||||
: WebCryptoCipherStatus::OK;
|
||||
}
|
||||
|
||||
WebCryptoCipherStatus AES_CTR_Cipher(Environment* env,
|
||||
@@ -258,8 +264,9 @@ WebCryptoCipherStatus AES_CTR_Cipher(Environment* env,
|
||||
|
||||
auto num_output = BignumPointer::New();
|
||||
|
||||
if (!num_output.setWord(CeilDiv(in.size(), kAesBlockSize)))
|
||||
if (!num_output.setWord(CeilDiv(in.size(), kAesBlockSize))) {
|
||||
return WebCryptoCipherStatus::FAILED;
|
||||
}
|
||||
|
||||
// Just like in chromium's implementation, if the counter will
|
||||
// be incremented more than there are counter values, we fail.
|
||||
@@ -272,7 +279,7 @@ WebCryptoCipherStatus AES_CTR_Cipher(Environment* env,
|
||||
}
|
||||
|
||||
// Output size is identical to the input size.
|
||||
ByteSource::Builder buf(in.size());
|
||||
auto buf = DataPointer::Alloc(in.size());
|
||||
|
||||
// Also just like in chromium's implementation, if we can process
|
||||
// the input without wrapping the counter, we'll do it as a single
|
||||
@@ -283,8 +290,10 @@ WebCryptoCipherStatus AES_CTR_Cipher(Environment* env,
|
||||
params,
|
||||
in,
|
||||
params.iv.data<unsigned char>(),
|
||||
buf.data<unsigned char>());
|
||||
if (status == WebCryptoCipherStatus::OK) *out = std::move(buf).release();
|
||||
static_cast<unsigned char*>(buf.get()));
|
||||
if (status == WebCryptoCipherStatus::OK) {
|
||||
*out = ByteSource::Allocated(buf.release());
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -297,14 +306,16 @@ WebCryptoCipherStatus AES_CTR_Cipher(Environment* env,
|
||||
params,
|
||||
ByteSource::Foreign(in.data<char>(), input_size_part1),
|
||||
params.iv.data<unsigned char>(),
|
||||
buf.data<unsigned char>());
|
||||
static_cast<unsigned char*>(buf.get()));
|
||||
|
||||
if (status != WebCryptoCipherStatus::OK)
|
||||
if (status != WebCryptoCipherStatus::OK) {
|
||||
return status;
|
||||
}
|
||||
|
||||
// Wrap the counter around to zero
|
||||
std::vector<unsigned char> new_counter_block = BlockWithZeroedCounter(params);
|
||||
|
||||
auto ptr = static_cast<unsigned char*>(buf.get()) + input_size_part1;
|
||||
// Encrypt the second part...
|
||||
status =
|
||||
AES_CTR_Cipher2(key_data,
|
||||
@@ -313,9 +324,11 @@ WebCryptoCipherStatus AES_CTR_Cipher(Environment* env,
|
||||
ByteSource::Foreign(in.data<char>() + input_size_part1,
|
||||
in.size() - input_size_part1),
|
||||
new_counter_block.data(),
|
||||
buf.data<unsigned char>() + input_size_part1);
|
||||
ptr);
|
||||
|
||||
if (status == WebCryptoCipherStatus::OK) *out = std::move(buf).release();
|
||||
if (status == WebCryptoCipherStatus::OK) {
|
||||
*out = ByteSource::Allocated(buf.release());
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
@@ -456,7 +469,7 @@ Maybe<void> AESCipherTraits::AdditionalConfig(
|
||||
|
||||
int cipher_nid;
|
||||
#define V(name, _, nid) \
|
||||
case kKeyVariantAES_##name: { \
|
||||
case AESKeyVariant::name: { \
|
||||
cipher_nid = nid; \
|
||||
break; \
|
||||
}
|
||||
@@ -507,7 +520,7 @@ WebCryptoCipherStatus AESCipherTraits::DoCipher(Environment* env,
|
||||
const ByteSource& in,
|
||||
ByteSource* out) {
|
||||
#define V(name, fn, _) \
|
||||
case kKeyVariantAES_##name: \
|
||||
case AESKeyVariant::name: \
|
||||
return fn(env, key_data, cipher_mode, params, in, out);
|
||||
switch (params.variant) {
|
||||
VARIANTS(V)
|
||||
@@ -520,8 +533,13 @@ WebCryptoCipherStatus AESCipherTraits::DoCipher(Environment* env,
|
||||
void AES::Initialize(Environment* env, Local<Object> target) {
|
||||
AESCryptoJob::Initialize(env, target);
|
||||
|
||||
#define V(name, _, __) NODE_DEFINE_CONSTANT(target, kKeyVariantAES_##name);
|
||||
#define V(name, _, __) \
|
||||
constexpr static auto kKeyVariantAES_##name = \
|
||||
static_cast<int>(AESKeyVariant::name); \
|
||||
NODE_DEFINE_CONSTANT(target, kKeyVariantAES_##name);
|
||||
|
||||
VARIANTS(V)
|
||||
|
||||
#undef V
|
||||
}
|
||||
|
||||
|
||||
@@ -9,11 +9,8 @@
|
||||
#include "env.h"
|
||||
#include "v8.h"
|
||||
|
||||
namespace node {
|
||||
namespace crypto {
|
||||
constexpr size_t kAesBlockSize = 16;
|
||||
namespace node::crypto {
|
||||
constexpr unsigned kNoAuthTagLength = static_cast<unsigned>(-1);
|
||||
constexpr const char* kDefaultWrapIV = "\xa6\xa6\xa6\xa6\xa6\xa6\xa6\xa6";
|
||||
|
||||
#define VARIANTS(V) \
|
||||
V(CTR_128, AES_CTR_Cipher, NID_aes_128_ctr) \
|
||||
@@ -29,8 +26,8 @@ constexpr const char* kDefaultWrapIV = "\xa6\xa6\xa6\xa6\xa6\xa6\xa6\xa6";
|
||||
V(KW_192, AES_Cipher, NID_id_aes192_wrap) \
|
||||
V(KW_256, AES_Cipher, NID_id_aes256_wrap)
|
||||
|
||||
enum AESKeyVariant {
|
||||
#define V(name, _, __) kKeyVariantAES_##name,
|
||||
enum class AESKeyVariant {
|
||||
#define V(name, _, __) name,
|
||||
VARIANTS(V)
|
||||
#undef V
|
||||
};
|
||||
@@ -81,8 +78,7 @@ namespace AES {
|
||||
void Initialize(Environment* env, v8::Local<v8::Object> target);
|
||||
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
|
||||
} // namespace AES
|
||||
} // namespace crypto
|
||||
} // namespace node
|
||||
} // namespace node::crypto
|
||||
|
||||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
||||
#endif // SRC_CRYPTO_CRYPTO_AES_H_
|
||||
+66
-54
@@ -35,10 +35,6 @@ using v8::Value;
|
||||
|
||||
namespace crypto {
|
||||
namespace {
|
||||
bool IsValidGCMTagLength(unsigned int tag_len) {
|
||||
return tag_len == 4 || tag_len == 8 || (tag_len >= 12 && tag_len <= 16);
|
||||
}
|
||||
|
||||
// Collects and returns information on the given cipher
|
||||
void GetCipherInfo(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
@@ -50,7 +46,7 @@ void GetCipherInfo(const FunctionCallbackInfo<Value>& args) {
|
||||
const auto cipher = ([&] {
|
||||
if (args[1]->IsString()) {
|
||||
Utf8Value name(env->isolate(), args[1]);
|
||||
return Cipher::FromName(*name);
|
||||
return Cipher::FromName(name.ToStringView());
|
||||
} else {
|
||||
int nid = args[1].As<Int32>()->Value();
|
||||
return Cipher::FromNid(nid);
|
||||
@@ -166,16 +162,19 @@ void GetCipherInfo(const FunctionCallbackInfo<Value>& args) {
|
||||
} // namespace
|
||||
|
||||
void CipherBase::GetSSLCiphers(const FunctionCallbackInfo<Value>& args) {
|
||||
MarkPopErrorOnReturn mark_pop_error_on_return;
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
auto ctx = SSLCtxPointer::New();
|
||||
if (!ctx) {
|
||||
return ThrowCryptoError(env, ERR_get_error(), "SSL_CTX_new");
|
||||
return ThrowCryptoError(
|
||||
env, mark_pop_error_on_return.peekError(), "SSL_CTX_new");
|
||||
}
|
||||
|
||||
auto ssl = SSLPointer::New(ctx);
|
||||
if (!ssl) {
|
||||
return ThrowCryptoError(env, ERR_get_error(), "SSL_new");
|
||||
return ThrowCryptoError(
|
||||
env, mark_pop_error_on_return.peekError(), "SSL_new");
|
||||
}
|
||||
|
||||
LocalVector<Value> arr(env->isolate());
|
||||
@@ -309,6 +308,7 @@ void CipherBase::CommonInit(const char* cipher_type,
|
||||
const unsigned char* iv,
|
||||
int iv_len,
|
||||
unsigned int auth_tag_len) {
|
||||
MarkPopErrorOnReturn mark_pop_error_on_return;
|
||||
CHECK(!ctx_);
|
||||
ctx_ = CipherCtxPointer::New();
|
||||
CHECK(ctx_);
|
||||
@@ -319,14 +319,16 @@ void CipherBase::CommonInit(const char* cipher_type,
|
||||
|
||||
const bool encrypt = (kind_ == kCipher);
|
||||
if (!ctx_.init(cipher, encrypt)) {
|
||||
return ThrowCryptoError(env(), ERR_get_error(),
|
||||
return ThrowCryptoError(env(),
|
||||
mark_pop_error_on_return.peekError(),
|
||||
"Failed to initialize cipher");
|
||||
}
|
||||
|
||||
if (cipher.isSupportedAuthenticatedMode()) {
|
||||
CHECK_GE(iv_len, 0);
|
||||
if (!InitAuthenticated(cipher_type, iv_len, auth_tag_len))
|
||||
if (!InitAuthenticated(cipher_type, iv_len, auth_tag_len)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ctx_.setKeyLength(key_len)) {
|
||||
@@ -335,7 +337,8 @@ void CipherBase::CommonInit(const char* cipher_type,
|
||||
}
|
||||
|
||||
if (!ctx_.init(Cipher(), encrypt, key, iv)) {
|
||||
return ThrowCryptoError(env(), ERR_get_error(),
|
||||
return ThrowCryptoError(env(),
|
||||
mark_pop_error_on_return.peekError(),
|
||||
"Failed to initialize cipher");
|
||||
}
|
||||
}
|
||||
@@ -422,8 +425,9 @@ void CipherBase::InitIv(const char* cipher_type,
|
||||
const bool has_iv = iv_buf.size() > 0;
|
||||
|
||||
// Throw if no IV was passed and the cipher requires an IV
|
||||
if (!has_iv && expected_iv_len != 0)
|
||||
if (!has_iv && expected_iv_len != 0) {
|
||||
return THROW_ERR_CRYPTO_INVALID_IV(env());
|
||||
}
|
||||
|
||||
// Throw if an IV was passed which does not match the cipher's fixed IV length
|
||||
// static_cast<int> for the iv_buf.size() is safe because we've verified
|
||||
@@ -438,8 +442,9 @@ void CipherBase::InitIv(const char* cipher_type,
|
||||
// Check for invalid IV lengths, since OpenSSL does not under some
|
||||
// conditions:
|
||||
// https://www.openssl.org/news/secadv/20190306.txt.
|
||||
if (iv_buf.size() > 12)
|
||||
if (iv_buf.size() > 12) {
|
||||
return THROW_ERR_CRYPTO_INVALID_IV(env());
|
||||
}
|
||||
}
|
||||
|
||||
CommonInit(
|
||||
@@ -504,7 +509,7 @@ bool CipherBase::InitAuthenticated(
|
||||
const int mode = ctx_.getMode();
|
||||
if (mode == EVP_CIPH_GCM_MODE) {
|
||||
if (auth_tag_len != kNoAuthTagLength) {
|
||||
if (!IsValidGCMTagLength(auth_tag_len)) {
|
||||
if (!Cipher::IsValidGCMTagLength(auth_tag_len)) {
|
||||
THROW_ERR_CRYPTO_INVALID_AUTH_TAG(
|
||||
env(),
|
||||
"Invalid authentication tag length: %u",
|
||||
@@ -595,9 +600,11 @@ void CipherBase::GetAuthTag(const FunctionCallbackInfo<Value>& args) {
|
||||
return;
|
||||
}
|
||||
|
||||
args.GetReturnValue().Set(
|
||||
Buffer::Copy(env, cipher->auth_tag_, cipher->auth_tag_len_)
|
||||
.FromMaybe(Local<Value>()));
|
||||
Local<Value> ret;
|
||||
if (Buffer::Copy(env, cipher->auth_tag_, cipher->auth_tag_len_)
|
||||
.ToLocal(&ret)) {
|
||||
args.GetReturnValue().Set(ret);
|
||||
}
|
||||
}
|
||||
|
||||
void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
|
||||
@@ -624,7 +631,7 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
|
||||
// Restrict GCM tag lengths according to NIST 800-38d, page 9.
|
||||
is_valid = (cipher->auth_tag_len_ == kNoAuthTagLength ||
|
||||
cipher->auth_tag_len_ == tag_len) &&
|
||||
IsValidGCMTagLength(tag_len);
|
||||
Cipher::IsValidGCMTagLength(tag_len);
|
||||
} else {
|
||||
// At this point, the tag length is already known and must match the
|
||||
// length of the given authentication tag.
|
||||
@@ -693,12 +700,12 @@ bool CipherBase::SetAAD(
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!CheckCCMMessageLength(plaintext_len))
|
||||
if (!CheckCCMMessageLength(plaintext_len)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (kind_ == kDecipher) {
|
||||
if (!MaybePassAuthTagToOpenSSL())
|
||||
return false;
|
||||
if (kind_ == kDecipher && !MaybePassAuthTagToOpenSSL()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ncrypto::Buffer<const unsigned char> buffer{
|
||||
@@ -738,19 +745,20 @@ CipherBase::UpdateResult CipherBase::Update(
|
||||
const char* data,
|
||||
size_t len,
|
||||
std::unique_ptr<BackingStore>* out) {
|
||||
if (!ctx_ || len > INT_MAX)
|
||||
return kErrorState;
|
||||
if (!ctx_ || len > INT_MAX) return kErrorState;
|
||||
MarkPopErrorOnReturn mark_pop_error_on_return;
|
||||
|
||||
const int mode = ctx_.getMode();
|
||||
|
||||
if (mode == EVP_CIPH_CCM_MODE && !CheckCCMMessageLength(len))
|
||||
if (mode == EVP_CIPH_CCM_MODE && !CheckCCMMessageLength(len)) {
|
||||
return kErrorMessageSize;
|
||||
}
|
||||
|
||||
// Pass the authentication tag to OpenSSL if possible. This will only happen
|
||||
// once, usually on the first update.
|
||||
if (kind_ == kDecipher && IsAuthenticatedMode())
|
||||
if (kind_ == kDecipher && IsAuthenticatedMode()) {
|
||||
CHECK(MaybePassAuthTagToOpenSSL());
|
||||
}
|
||||
|
||||
const int block_size = ctx_.getBlockSize();
|
||||
CHECK_GT(block_size, 0);
|
||||
@@ -800,34 +808,38 @@ CipherBase::UpdateResult CipherBase::Update(
|
||||
}
|
||||
|
||||
void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
|
||||
Decode<CipherBase>(args, [](CipherBase* cipher,
|
||||
const FunctionCallbackInfo<Value>& args,
|
||||
const char* data, size_t size) {
|
||||
std::unique_ptr<BackingStore> out;
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
Decode<CipherBase>(
|
||||
args,
|
||||
[](CipherBase* cipher,
|
||||
const FunctionCallbackInfo<Value>& args,
|
||||
const char* data,
|
||||
size_t size) {
|
||||
MarkPopErrorOnReturn mark_pop_error_on_return;
|
||||
std::unique_ptr<BackingStore> out;
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
|
||||
if (size > INT_MAX) [[unlikely]] {
|
||||
return THROW_ERR_OUT_OF_RANGE(env, "data is too long");
|
||||
}
|
||||
UpdateResult r = cipher->Update(data, size, &out);
|
||||
if (size > INT_MAX) [[unlikely]] {
|
||||
return THROW_ERR_OUT_OF_RANGE(env, "data is too long");
|
||||
}
|
||||
UpdateResult r = cipher->Update(data, size, &out);
|
||||
|
||||
if (r != kSuccess) {
|
||||
if (r == kErrorState) {
|
||||
ThrowCryptoError(env, ERR_get_error(),
|
||||
"Trying to add data in unsupported state");
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (r != kSuccess) {
|
||||
if (r == kErrorState) {
|
||||
ThrowCryptoError(env,
|
||||
mark_pop_error_on_return.peekError(),
|
||||
"Trying to add data in unsupported state");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(out));
|
||||
args.GetReturnValue().Set(
|
||||
Buffer::New(env, ab, 0, ab->ByteLength()).FromMaybe(Local<Value>()));
|
||||
});
|
||||
auto ab = ArrayBuffer::New(env->isolate(), std::move(out));
|
||||
args.GetReturnValue().Set(Buffer::New(env, ab, 0, ab->ByteLength())
|
||||
.FromMaybe(Local<Value>()));
|
||||
});
|
||||
}
|
||||
|
||||
bool CipherBase::SetAutoPadding(bool auto_padding) {
|
||||
if (!ctx_)
|
||||
return false;
|
||||
if (!ctx_) return false;
|
||||
MarkPopErrorOnReturn mark_pop_error_on_return;
|
||||
return ctx_.setPadding(auto_padding);
|
||||
}
|
||||
@@ -841,8 +853,7 @@ void CipherBase::SetAutoPadding(const FunctionCallbackInfo<Value>& args) {
|
||||
}
|
||||
|
||||
bool CipherBase::Final(std::unique_ptr<BackingStore>* out) {
|
||||
if (!ctx_)
|
||||
return false;
|
||||
if (!ctx_) return false;
|
||||
|
||||
const int mode = ctx_.getMode();
|
||||
|
||||
@@ -906,11 +917,13 @@ bool CipherBase::Final(std::unique_ptr<BackingStore>* out) {
|
||||
|
||||
void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
MarkPopErrorOnReturn mark_pop_error_on_return;
|
||||
|
||||
CipherBase* cipher;
|
||||
ASSIGN_OR_RETURN_UNWRAP(&cipher, args.This());
|
||||
if (cipher->ctx_ == nullptr)
|
||||
if (cipher->ctx_ == nullptr) {
|
||||
return THROW_ERR_CRYPTO_INVALID_STATE(env);
|
||||
}
|
||||
|
||||
std::unique_ptr<BackingStore> out;
|
||||
|
||||
@@ -923,10 +936,10 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
|
||||
? "Unsupported state or unable to authenticate data"
|
||||
: "Unsupported state";
|
||||
|
||||
return ThrowCryptoError(env, ERR_get_error(), msg);
|
||||
return ThrowCryptoError(env, mark_pop_error_on_return.peekError(), msg);
|
||||
}
|
||||
|
||||
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), std::move(out));
|
||||
auto ab = ArrayBuffer::New(env->isolate(), std::move(out));
|
||||
args.GetReturnValue().Set(
|
||||
Buffer::New(env, ab, 0, ab->ByteLength()).FromMaybe(Local<Value>()));
|
||||
}
|
||||
@@ -974,8 +987,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
|
||||
auto data = KeyObjectData::GetPublicOrPrivateKeyFromJs(args, &offset);
|
||||
if (!data) return;
|
||||
const auto& pkey = data.GetAsymmetricKey();
|
||||
if (!pkey)
|
||||
return;
|
||||
if (!pkey) return;
|
||||
|
||||
ArrayBufferOrViewContents<unsigned char> buf(args[offset]);
|
||||
if (!buf.CheckSizeInt32()) [[unlikely]] {
|
||||
|
||||
+47
-105
@@ -43,7 +43,6 @@ using v8::Integer;
|
||||
using v8::Local;
|
||||
using v8::MaybeLocal;
|
||||
using v8::Object;
|
||||
using v8::String;
|
||||
using v8::Undefined;
|
||||
using v8::Value;
|
||||
|
||||
@@ -53,32 +52,16 @@ SSLSessionPointer GetTLSSession(const unsigned char* buf, size_t length) {
|
||||
return SSLSessionPointer(d2i_SSL_SESSION(nullptr, &buf, length));
|
||||
}
|
||||
|
||||
long VerifyPeerCertificate( // NOLINT(runtime/int)
|
||||
const SSLPointer& ssl,
|
||||
long def) { // NOLINT(runtime/int)
|
||||
return ssl.verifyPeerCertificate().value_or(def);
|
||||
}
|
||||
|
||||
bool UseSNIContext(
|
||||
const SSLPointer& ssl, BaseObjectPtr<SecureContext> context) {
|
||||
return ssl.setSniContext(context->ctx());
|
||||
}
|
||||
|
||||
bool SetGroups(SecureContext* sc, const char* groups) {
|
||||
return sc->ctx().setGroups(groups);
|
||||
}
|
||||
|
||||
MaybeLocal<Value> GetValidationErrorReason(Environment* env, int err) {
|
||||
auto reason = X509Pointer::ErrorReason(err).value_or("");
|
||||
if (reason == "") return Undefined(env->isolate());
|
||||
return OneByteString(env->isolate(), reason.data(), reason.length());
|
||||
return OneByteString(env->isolate(), reason);
|
||||
}
|
||||
|
||||
MaybeLocal<Value> GetValidationErrorCode(Environment* env, int err) {
|
||||
if (err == 0)
|
||||
return Undefined(env->isolate());
|
||||
if (err == 0) return Undefined(env->isolate());
|
||||
auto error = X509Pointer::ErrorCode(err);
|
||||
return OneByteString(env->isolate(), error.data(), error.length());
|
||||
return OneByteString(env->isolate(), error);
|
||||
}
|
||||
|
||||
MaybeLocal<Value> GetCert(Environment* env, const SSLPointer& ssl) {
|
||||
@@ -89,35 +72,6 @@ MaybeLocal<Value> GetCert(Environment* env, const SSLPointer& ssl) {
|
||||
}
|
||||
|
||||
namespace {
|
||||
template <typename T>
|
||||
bool Set(
|
||||
Local<Context> context,
|
||||
Local<Object> target,
|
||||
Local<Value> name,
|
||||
MaybeLocal<T> maybe_value) {
|
||||
Local<Value> value;
|
||||
if (!maybe_value.ToLocal(&value))
|
||||
return false;
|
||||
|
||||
// Undefined is ignored, but still considered successful
|
||||
if (value->IsUndefined())
|
||||
return true;
|
||||
|
||||
return !target->Set(context, name, value).IsNothing();
|
||||
}
|
||||
|
||||
template <const char* (*getstr)(const SSL_CIPHER* cipher)>
|
||||
MaybeLocal<Value> GetCipherValue(Environment* env, const SSL_CIPHER* cipher) {
|
||||
if (cipher == nullptr)
|
||||
return Undefined(env->isolate());
|
||||
|
||||
return OneByteString(env->isolate(), getstr(cipher));
|
||||
}
|
||||
|
||||
constexpr auto GetCipherName = GetCipherValue<SSL_CIPHER_get_name>;
|
||||
constexpr auto GetCipherStandardName = GetCipherValue<SSL_CIPHER_standard_name>;
|
||||
constexpr auto GetCipherVersion = GetCipherValue<SSL_CIPHER_get_version>;
|
||||
|
||||
StackOfX509 CloneSSLCerts(X509Pointer&& cert,
|
||||
const STACK_OF(X509)* const ssl_certs) {
|
||||
StackOfX509 peer_certs(sk_X509_new(nullptr));
|
||||
@@ -148,11 +102,11 @@ MaybeLocal<Object> AddIssuerChainToObject(X509Pointer* cert,
|
||||
Local<Value> ca_info;
|
||||
if (!X509Certificate::toObject(env, ca).ToLocal(&ca_info)) return {};
|
||||
CHECK(ca_info->IsObject());
|
||||
|
||||
if (!Set<Object>(env->context(),
|
||||
object,
|
||||
env->issuercert_string(),
|
||||
ca_info.As<Object>())) {
|
||||
if (object
|
||||
->Set(env->context(),
|
||||
env->issuercert_string(),
|
||||
ca_info.As<Object>())
|
||||
.IsNothing()) {
|
||||
return {};
|
||||
}
|
||||
object = ca_info.As<Object>();
|
||||
@@ -184,10 +138,10 @@ MaybeLocal<Object> GetLastIssuedCert(
|
||||
|
||||
CHECK(ca_info->IsObject());
|
||||
|
||||
if (!Set<Object>(env->context(),
|
||||
issuer_chain,
|
||||
env->issuercert_string(),
|
||||
ca_info.As<Object>())) {
|
||||
if (issuer_chain
|
||||
->Set(
|
||||
env->context(), env->issuercert_string(), ca_info.As<Object>())
|
||||
.IsNothing()) {
|
||||
return {};
|
||||
}
|
||||
issuer_chain = ca_info.As<Object>();
|
||||
@@ -204,41 +158,30 @@ MaybeLocal<Object> GetLastIssuedCert(
|
||||
return MaybeLocal<Object>(issuer_chain);
|
||||
}
|
||||
|
||||
Local<Value> maybeString(Environment* env,
|
||||
std::optional<std::string_view> value) {
|
||||
if (!value.has_value()) return Undefined(env->isolate());
|
||||
return OneByteString(env->isolate(), value.value());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
MaybeLocal<Value> GetCurrentCipherName(Environment* env,
|
||||
const SSLPointer& ssl) {
|
||||
return GetCipherName(env, ssl.getCipher());
|
||||
}
|
||||
|
||||
MaybeLocal<Value> GetCurrentCipherVersion(Environment* env,
|
||||
const SSLPointer& ssl) {
|
||||
return GetCipherVersion(env, ssl.getCipher());
|
||||
}
|
||||
|
||||
template <MaybeLocal<Value> (*Get)(Environment* env, const SSL_CIPHER* cipher)>
|
||||
MaybeLocal<Value> GetCurrentCipherValue(Environment* env,
|
||||
const SSLPointer& ssl) {
|
||||
return Get(env, ssl.getCipher());
|
||||
}
|
||||
|
||||
MaybeLocal<Object> GetCipherInfo(Environment* env, const SSLPointer& ssl) {
|
||||
if (ssl.getCipher() == nullptr) return MaybeLocal<Object>();
|
||||
EscapableHandleScope scope(env->isolate());
|
||||
Local<Object> info = Object::New(env->isolate());
|
||||
|
||||
if (!Set<Value>(env->context(),
|
||||
info,
|
||||
env->name_string(),
|
||||
GetCurrentCipherValue<GetCipherName>(env, ssl)) ||
|
||||
!Set<Value>(env->context(),
|
||||
info,
|
||||
env->standard_name_string(),
|
||||
GetCurrentCipherValue<GetCipherStandardName>(env, ssl)) ||
|
||||
!Set<Value>(env->context(),
|
||||
info,
|
||||
env->version_string(),
|
||||
GetCurrentCipherValue<GetCipherVersion>(env, ssl))) {
|
||||
if (info->Set(env->context(),
|
||||
env->name_string(),
|
||||
maybeString(env, ssl.getCipherName()))
|
||||
.IsNothing() ||
|
||||
info->Set(env->context(),
|
||||
env->standard_name_string(),
|
||||
maybeString(env, ssl.getCipherStandardName()))
|
||||
.IsNothing() ||
|
||||
info->Set(env->context(),
|
||||
env->version_string(),
|
||||
maybeString(env, ssl.getCipherVersion()))
|
||||
.IsNothing()) {
|
||||
return MaybeLocal<Object>();
|
||||
}
|
||||
|
||||
@@ -258,11 +201,12 @@ MaybeLocal<Object> GetEphemeralKey(Environment* env, const SSLPointer& ssl) {
|
||||
int kid = key.id();
|
||||
switch (kid) {
|
||||
case EVP_PKEY_DH:
|
||||
if (!Set<String>(context, info, env->type_string(), env->dh_string()) ||
|
||||
!Set<Integer>(context,
|
||||
info,
|
||||
env->size_string(),
|
||||
Integer::New(env->isolate(), key.bits()))) {
|
||||
if (info->Set(context, env->type_string(), env->dh_string())
|
||||
.IsNothing() ||
|
||||
info->Set(context,
|
||||
env->size_string(),
|
||||
Integer::New(env->isolate(), key.bits()))
|
||||
.IsNothing()) {
|
||||
return MaybeLocal<Object>();
|
||||
}
|
||||
break;
|
||||
@@ -277,16 +221,16 @@ MaybeLocal<Object> GetEphemeralKey(Environment* env, const SSLPointer& ssl) {
|
||||
} else {
|
||||
curve_name = OBJ_nid2sn(kid);
|
||||
}
|
||||
if (!Set<String>(
|
||||
context, info, env->type_string(), env->ecdh_string()) ||
|
||||
!Set<String>(context,
|
||||
info,
|
||||
env->name_string(),
|
||||
OneByteString(env->isolate(), curve_name)) ||
|
||||
!Set<Integer>(context,
|
||||
info,
|
||||
env->size_string(),
|
||||
Integer::New(env->isolate(), key.bits()))) {
|
||||
if (info->Set(context, env->type_string(), env->ecdh_string())
|
||||
.IsNothing() ||
|
||||
info->Set(context,
|
||||
env->name_string(),
|
||||
OneByteString(env->isolate(), curve_name))
|
||||
.IsNothing() ||
|
||||
info->Set(context,
|
||||
env->size_string(),
|
||||
Integer::New(env->isolate(), key.bits()))
|
||||
.IsNothing()) {
|
||||
return MaybeLocal<Object>();
|
||||
}
|
||||
}
|
||||
@@ -378,10 +322,8 @@ MaybeLocal<Value> GetPeerCert(
|
||||
|
||||
// Last certificate should be self-signed.
|
||||
if (cert.view().isIssuedBy(cert.view()) &&
|
||||
!Set<Object>(env->context(),
|
||||
issuer_chain,
|
||||
env->issuercert_string(),
|
||||
issuer_chain)) {
|
||||
issuer_chain->Set(env->context(), env->issuercert_string(), issuer_chain)
|
||||
.IsNothing()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
@@ -17,15 +17,6 @@ namespace crypto {
|
||||
ncrypto::SSLSessionPointer GetTLSSession(const unsigned char* buf,
|
||||
size_t length);
|
||||
|
||||
long VerifyPeerCertificate( // NOLINT(runtime/int)
|
||||
const ncrypto::SSLPointer& ssl,
|
||||
long def = X509_V_ERR_UNSPECIFIED); // NOLINT(runtime/int)
|
||||
|
||||
bool UseSNIContext(const ncrypto::SSLPointer& ssl,
|
||||
BaseObjectPtr<SecureContext> context);
|
||||
|
||||
bool SetGroups(SecureContext* sc, const char* groups);
|
||||
|
||||
v8::MaybeLocal<v8::Value> GetValidationErrorReason(Environment* env, int err);
|
||||
|
||||
v8::MaybeLocal<v8::Value> GetValidationErrorCode(Environment* env, int err);
|
||||
@@ -51,11 +42,6 @@ v8::MaybeLocal<v8::Object> ECPointToBuffer(
|
||||
point_conversion_form_t form,
|
||||
const char** error);
|
||||
|
||||
v8::MaybeLocal<v8::Value> GetCurrentCipherName(Environment* env,
|
||||
const ncrypto::SSLPointer& ssl);
|
||||
v8::MaybeLocal<v8::Value> GetCurrentCipherVersion(
|
||||
Environment* env, const ncrypto::SSLPointer& ssl);
|
||||
|
||||
} // namespace crypto
|
||||
} // namespace node
|
||||
|
||||
|
||||
@@ -524,11 +524,11 @@ bool DHBitsTraits::DeriveBits(
|
||||
return true;
|
||||
}
|
||||
|
||||
Maybe<void> GetDhKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
bool GetDhKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
CHECK_EQ(key.GetAsymmetricKey().id(), EVP_PKEY_DH);
|
||||
return JustVoid();
|
||||
return true;
|
||||
}
|
||||
|
||||
void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
|
||||
|
||||
@@ -115,9 +115,9 @@ struct DHBitsTraits final {
|
||||
|
||||
using DHBitsJob = DeriveBitsJob<DHBitsTraits>;
|
||||
|
||||
v8::Maybe<void> GetDhKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
bool GetDhKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
|
||||
} // namespace crypto
|
||||
} // namespace node
|
||||
|
||||
+21
-35
@@ -14,14 +14,13 @@
|
||||
|
||||
namespace node {
|
||||
|
||||
using ncrypto::BignumPointer;
|
||||
using ncrypto::Dsa;
|
||||
using ncrypto::EVPKeyCtxPointer;
|
||||
using v8::FunctionCallbackInfo;
|
||||
using v8::Int32;
|
||||
using v8::JustVoid;
|
||||
using v8::Local;
|
||||
using v8::Maybe;
|
||||
using v8::Nothing;
|
||||
using v8::Number;
|
||||
using v8::Object;
|
||||
using v8::Uint32;
|
||||
@@ -106,41 +105,28 @@ WebCryptoKeyExportStatus DSAKeyExportTraits::DoExport(
|
||||
}
|
||||
}
|
||||
|
||||
Maybe<void> GetDsaKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
const BIGNUM* p; // Modulus length
|
||||
const BIGNUM* q; // Divisor length
|
||||
bool GetDsaKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
if (!key) return false;
|
||||
Dsa dsa = key.GetAsymmetricKey();
|
||||
if (!dsa) return false;
|
||||
|
||||
Mutex::ScopedLock lock(key.mutex());
|
||||
const auto& m_pkey = key.GetAsymmetricKey();
|
||||
int type = m_pkey.id();
|
||||
CHECK(type == EVP_PKEY_DSA);
|
||||
size_t modulus_length = dsa.getModulusLength();
|
||||
size_t divisor_length = dsa.getDivisorLength();
|
||||
|
||||
const DSA* dsa = EVP_PKEY_get0_DSA(m_pkey.get());
|
||||
CHECK_NOT_NULL(dsa);
|
||||
|
||||
DSA_get0_pqg(dsa, &p, &q, nullptr);
|
||||
|
||||
size_t modulus_length = BignumPointer::GetBitCount(p);
|
||||
size_t divisor_length = BignumPointer::GetBitCount(q);
|
||||
|
||||
if (target
|
||||
->Set(
|
||||
env->context(),
|
||||
env->modulus_length_string(),
|
||||
Number::New(env->isolate(), static_cast<double>(modulus_length)))
|
||||
.IsNothing() ||
|
||||
target
|
||||
->Set(
|
||||
env->context(),
|
||||
env->divisor_length_string(),
|
||||
Number::New(env->isolate(), static_cast<double>(divisor_length)))
|
||||
.IsNothing()) {
|
||||
return Nothing<void>();
|
||||
}
|
||||
|
||||
return JustVoid();
|
||||
return target
|
||||
->Set(env->context(),
|
||||
env->modulus_length_string(),
|
||||
Number::New(env->isolate(),
|
||||
static_cast<double>(modulus_length)))
|
||||
.IsJust() &&
|
||||
target
|
||||
->Set(env->context(),
|
||||
env->divisor_length_string(),
|
||||
Number::New(env->isolate(),
|
||||
static_cast<double>(divisor_length)))
|
||||
.IsJust();
|
||||
}
|
||||
|
||||
namespace DSAAlg {
|
||||
|
||||
@@ -10,8 +10,7 @@
|
||||
#include "memory_tracker.h"
|
||||
#include "v8.h"
|
||||
|
||||
namespace node {
|
||||
namespace crypto {
|
||||
namespace node::crypto {
|
||||
struct DsaKeyPairParams final : public MemoryRetainer {
|
||||
unsigned int modulus_bits;
|
||||
int divisor_bits;
|
||||
@@ -60,16 +59,15 @@ struct DSAKeyExportTraits final {
|
||||
|
||||
using DSAKeyExportJob = KeyExportJob<DSAKeyExportTraits>;
|
||||
|
||||
v8::Maybe<void> GetDsaKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
bool GetDsaKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
|
||||
namespace DSAAlg {
|
||||
void Initialize(Environment* env, v8::Local<v8::Object> target);
|
||||
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
|
||||
} // namespace DSAAlg
|
||||
} // namespace crypto
|
||||
} // namespace node
|
||||
} // namespace node::crypto
|
||||
|
||||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
||||
#endif // SRC_CRYPTO_CRYPTO_DSA_H_
|
||||
+47
-41
@@ -483,15 +483,16 @@ bool ECDHBitsTraits::DeriveBits(Environment* env,
|
||||
const auto pub = ECKeyPointer::GetPublicKey(public_key);
|
||||
int field_size = EC_GROUP_get_degree(group);
|
||||
len = (field_size + 7) / 8;
|
||||
ByteSource::Builder buf(len);
|
||||
auto buf = DataPointer::Alloc(len);
|
||||
CHECK_NOT_NULL(pub);
|
||||
CHECK_NOT_NULL(private_key);
|
||||
if (ECDH_compute_key(buf.data<char>(), len, pub, private_key, nullptr) <=
|
||||
if (ECDH_compute_key(
|
||||
static_cast<char*>(buf.get()), len, pub, private_key, nullptr) <=
|
||||
0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*out = std::move(buf).release();
|
||||
*out = ByteSource::Allocated(buf.release());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -605,14 +606,19 @@ WebCryptoKeyExportStatus EC_Raw_Export(const KeyObjectData& key_data,
|
||||
size_t len = EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr);
|
||||
if (len == 0)
|
||||
return WebCryptoKeyExportStatus::FAILED;
|
||||
ByteSource::Builder data(len);
|
||||
size_t check_len = EC_POINT_point2oct(
|
||||
group, point, form, data.data<unsigned char>(), len, nullptr);
|
||||
auto data = DataPointer::Alloc(len);
|
||||
size_t check_len =
|
||||
EC_POINT_point2oct(group,
|
||||
point,
|
||||
form,
|
||||
static_cast<unsigned char*>(data.get()),
|
||||
len,
|
||||
nullptr);
|
||||
if (check_len == 0)
|
||||
return WebCryptoKeyExportStatus::FAILED;
|
||||
|
||||
CHECK_EQ(len, check_len);
|
||||
*out = std::move(data).release();
|
||||
*out = ByteSource::Allocated(data.release());
|
||||
}
|
||||
|
||||
return WebCryptoKeyExportStatus::OK;
|
||||
@@ -660,15 +666,20 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport(
|
||||
const size_t need =
|
||||
EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr);
|
||||
if (need == 0) return WebCryptoKeyExportStatus::FAILED;
|
||||
ByteSource::Builder data(need);
|
||||
const size_t have = EC_POINT_point2oct(
|
||||
group, point, form, data.data<unsigned char>(), need, nullptr);
|
||||
auto data = DataPointer::Alloc(need);
|
||||
const size_t have =
|
||||
EC_POINT_point2oct(group,
|
||||
point,
|
||||
form,
|
||||
static_cast<unsigned char*>(data.get()),
|
||||
need,
|
||||
nullptr);
|
||||
if (have == 0) return WebCryptoKeyExportStatus::FAILED;
|
||||
auto ec = ECKeyPointer::New(group);
|
||||
CHECK(ec);
|
||||
auto uncompressed = ECPointPointer::New(group);
|
||||
ncrypto::Buffer<const unsigned char> buffer{
|
||||
.data = data.data<unsigned char>(),
|
||||
.data = static_cast<unsigned char*>(data.get()),
|
||||
.len = data.size(),
|
||||
};
|
||||
CHECK(uncompressed.setFromBuffer(buffer, group));
|
||||
@@ -686,9 +697,9 @@ WebCryptoKeyExportStatus ECKeyExportTraits::DoExport(
|
||||
}
|
||||
}
|
||||
|
||||
Maybe<void> ExportJWKEcKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
bool ExportJWKEcKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
Mutex::ScopedLock lock(key.mutex());
|
||||
const auto& m_pkey = key.GetAsymmetricKey();
|
||||
CHECK_EQ(m_pkey.id(), EVP_PKEY_EC);
|
||||
@@ -709,14 +720,14 @@ Maybe<void> ExportJWKEcKey(Environment* env,
|
||||
if (!EC_POINT_get_affine_coordinates(group, pub, x.get(), y.get(), nullptr)) {
|
||||
ThrowCryptoError(env, ERR_get_error(),
|
||||
"Failed to get elliptic-curve point coordinates");
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (target->Set(
|
||||
env->context(),
|
||||
env->jwk_kty_string(),
|
||||
env->jwk_ec_string()).IsNothing()) {
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (SetEncodedValue(
|
||||
@@ -731,7 +742,7 @@ Maybe<void> ExportJWKEcKey(Environment* env,
|
||||
env->jwk_y_string(),
|
||||
y.get(),
|
||||
degree_bytes).IsNothing()) {
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
|
||||
Local<String> crv_name;
|
||||
@@ -752,27 +763,28 @@ Maybe<void> ExportJWKEcKey(Environment* env,
|
||||
default: {
|
||||
THROW_ERR_CRYPTO_JWK_UNSUPPORTED_CURVE(
|
||||
env, "Unsupported JWK EC curve: %s.", OBJ_nid2sn(nid));
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (target->Set(
|
||||
env->context(),
|
||||
env->jwk_crv_string(),
|
||||
crv_name).IsNothing()) {
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (key.GetKeyType() == kKeyTypePrivate) {
|
||||
auto pvt = ECKeyPointer::GetPrivateKey(ec);
|
||||
return SetEncodedValue(env, target, env->jwk_d_string(), pvt, degree_bytes);
|
||||
return SetEncodedValue(env, target, env->jwk_d_string(), pvt, degree_bytes)
|
||||
.IsJust();
|
||||
}
|
||||
|
||||
return JustVoid();
|
||||
return true;
|
||||
}
|
||||
|
||||
Maybe<void> ExportJWKEdKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
bool ExportJWKEdKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
Mutex::ScopedLock lock(key.mutex());
|
||||
const auto& pkey = key.GetAsymmetricKey();
|
||||
|
||||
@@ -809,7 +821,8 @@ Maybe<void> ExportJWKEdKey(Environment* env,
|
||||
return true;
|
||||
};
|
||||
|
||||
if (target
|
||||
return !(
|
||||
target
|
||||
->Set(env->context(),
|
||||
env->jwk_crv_string(),
|
||||
OneByteString(env->isolate(), curve))
|
||||
@@ -818,11 +831,7 @@ Maybe<void> ExportJWKEdKey(Environment* env,
|
||||
!trySetKey(env, pkey.rawPrivateKey(), target, env->jwk_d_string())) ||
|
||||
!trySetKey(env, pkey.rawPublicKey(), target, env->jwk_x_string()) ||
|
||||
target->Set(env->context(), env->jwk_kty_string(), env->jwk_okp_string())
|
||||
.IsNothing()) {
|
||||
return Nothing<void>();
|
||||
}
|
||||
|
||||
return JustVoid();
|
||||
.IsNothing());
|
||||
}
|
||||
|
||||
KeyObjectData ImportJWKEcKey(Environment* env,
|
||||
@@ -886,9 +895,9 @@ KeyObjectData ImportJWKEcKey(Environment* env,
|
||||
return KeyObjectData::CreateAsymmetric(type, std::move(pkey));
|
||||
}
|
||||
|
||||
Maybe<void> GetEcKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
bool GetEcKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
Mutex::ScopedLock lock(key.mutex());
|
||||
const auto& m_pkey = key.GetAsymmetricKey();
|
||||
CHECK_EQ(m_pkey.id(), EVP_PKEY_EC);
|
||||
@@ -899,14 +908,11 @@ Maybe<void> GetEcKeyDetail(Environment* env,
|
||||
const auto group = ECKeyPointer::GetGroup(ec);
|
||||
int nid = EC_GROUP_get_curve_name(group);
|
||||
|
||||
if (target
|
||||
->Set(env->context(),
|
||||
env->named_curve_string(),
|
||||
OneByteString(env->isolate(), OBJ_nid2sn(nid)))
|
||||
.IsNothing()) {
|
||||
return Nothing<void>();
|
||||
}
|
||||
return JustVoid();
|
||||
return target
|
||||
->Set(env->context(),
|
||||
env->named_curve_string(),
|
||||
OneByteString(env->isolate(), OBJ_nid2sn(nid)))
|
||||
.IsJust();
|
||||
}
|
||||
|
||||
// WebCrypto requires a different format for ECDSA signatures than
|
||||
|
||||
@@ -141,22 +141,22 @@ struct ECKeyExportTraits final {
|
||||
|
||||
using ECKeyExportJob = KeyExportJob<ECKeyExportTraits>;
|
||||
|
||||
v8::Maybe<void> ExportJWKEcKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
bool ExportJWKEcKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
|
||||
v8::Maybe<void> ExportJWKEdKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
bool ExportJWKEdKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
|
||||
KeyObjectData ImportJWKEcKey(Environment* env,
|
||||
v8::Local<v8::Object> jwk,
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args,
|
||||
unsigned int offset);
|
||||
|
||||
v8::Maybe<void> GetEcKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
bool GetEcKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
} // namespace crypto
|
||||
} // namespace node
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
namespace node {
|
||||
|
||||
using ncrypto::DataPointer;
|
||||
using ncrypto::EVPKeyCtxPointer;
|
||||
using v8::FunctionCallbackInfo;
|
||||
using v8::Int32;
|
||||
@@ -70,21 +71,19 @@ Maybe<void> SecretKeyGenTraits::AdditionalConfig(
|
||||
|
||||
KeyGenJobStatus SecretKeyGenTraits::DoKeyGen(Environment* env,
|
||||
SecretKeyGenConfig* params) {
|
||||
ByteSource::Builder bytes(params->length);
|
||||
if (!ncrypto::CSPRNG(bytes.data<unsigned char>(), params->length))
|
||||
auto bytes = DataPointer::Alloc(params->length);
|
||||
if (!ncrypto::CSPRNG(static_cast<unsigned char*>(bytes.get()),
|
||||
params->length)) {
|
||||
return KeyGenJobStatus::FAILED;
|
||||
params->out = std::move(bytes).release();
|
||||
}
|
||||
params->out = ByteSource::Allocated(bytes.release());
|
||||
return KeyGenJobStatus::OK;
|
||||
}
|
||||
|
||||
MaybeLocal<Value> SecretKeyGenTraits::EncodeKey(Environment* env,
|
||||
SecretKeyGenConfig* params) {
|
||||
auto data = KeyObjectData::CreateSecret(std::move(params->out));
|
||||
Local<Value> ret;
|
||||
if (!KeyObjectHandle::Create(env, data).ToLocal(&ret)) {
|
||||
return MaybeLocal<Value>();
|
||||
}
|
||||
return ret;
|
||||
return KeyObjectHandle::Create(env, data).FromMaybe(Local<Value>());
|
||||
}
|
||||
|
||||
namespace Keygen {
|
||||
@@ -97,7 +96,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
|
||||
NidKeyPairGenJob::RegisterExternalReferences(registry);
|
||||
SecretKeyGenJob::RegisterExternalReferences(registry);
|
||||
}
|
||||
|
||||
} // namespace Keygen
|
||||
} // namespace crypto
|
||||
} // namespace node
|
||||
@@ -11,8 +11,7 @@
|
||||
#include "memory_tracker.h"
|
||||
#include "v8.h"
|
||||
|
||||
namespace node {
|
||||
namespace crypto {
|
||||
namespace node::crypto {
|
||||
namespace Keygen {
|
||||
void Initialize(Environment* env, v8::Local<v8::Object> target);
|
||||
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
|
||||
@@ -184,13 +183,11 @@ struct KeyPairGenTraits final {
|
||||
static v8::MaybeLocal<v8::Value> EncodeKey(Environment* env,
|
||||
AdditionalParameters* params) {
|
||||
v8::Local<v8::Value> keys[2];
|
||||
if (params->key
|
||||
.ToEncodedPublicKey(env, params->public_key_encoding, &keys[0])
|
||||
.IsNothing() ||
|
||||
params->key
|
||||
.ToEncodedPrivateKey(env, params->private_key_encoding, &keys[1])
|
||||
.IsNothing()) {
|
||||
return v8::MaybeLocal<v8::Value>();
|
||||
if (!params->key.ToEncodedPublicKey(
|
||||
env, params->public_key_encoding, &keys[0]) ||
|
||||
!params->key.ToEncodedPrivateKey(
|
||||
env, params->private_key_encoding, &keys[1])) {
|
||||
return {};
|
||||
}
|
||||
return v8::Array::New(env->isolate(), keys, arraysize(keys));
|
||||
}
|
||||
@@ -233,11 +230,6 @@ struct KeyPairGenConfig final : public MemoryRetainer {
|
||||
AlgorithmParams params;
|
||||
|
||||
KeyPairGenConfig() = default;
|
||||
~KeyPairGenConfig() {
|
||||
if (key) {
|
||||
Mutex::ScopedLock priv_lock(key.mutex());
|
||||
}
|
||||
}
|
||||
|
||||
explicit KeyPairGenConfig(KeyPairGenConfig&& other) noexcept
|
||||
: public_key_encoding(other.public_key_encoding),
|
||||
@@ -291,8 +283,7 @@ struct NidKeyPairGenTraits final {
|
||||
|
||||
using NidKeyPairGenJob = KeyGenJob<KeyPairGenTraits<NidKeyPairGenTraits>>;
|
||||
using SecretKeyGenJob = KeyGenJob<SecretKeyGenTraits>;
|
||||
} // namespace crypto
|
||||
} // namespace node
|
||||
} // namespace node::crypto
|
||||
|
||||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
||||
#endif // SRC_CRYPTO_CRYPTO_KEYGEN_H_
|
||||
|
||||
+123
-146
@@ -32,7 +32,6 @@ using v8::FunctionTemplate;
|
||||
using v8::Int32;
|
||||
using v8::Isolate;
|
||||
using v8::Just;
|
||||
using v8::JustVoid;
|
||||
using v8::Local;
|
||||
using v8::Maybe;
|
||||
using v8::MaybeLocal;
|
||||
@@ -82,10 +81,11 @@ Maybe<EVPKeyPointer::AsymmetricKeyEncodingConfig> GetKeyFormatAndTypeFromJs(
|
||||
return Just(config);
|
||||
}
|
||||
|
||||
MaybeLocal<Value> BIOToStringOrBuffer(
|
||||
MaybeLocal<Value> ToV8Value(
|
||||
Environment* env,
|
||||
const BIOPointer& bio,
|
||||
const EVPKeyPointer::AsymmetricKeyEncodingConfig& config) {
|
||||
if (!bio) return {};
|
||||
BUF_MEM* bptr = bio;
|
||||
if (config.format == EVPKeyPointer::PKFormatType::PEM) {
|
||||
// PEM is an ASCII format, so we will return it as a string.
|
||||
@@ -103,11 +103,9 @@ MaybeLocal<Value> WritePrivateKey(
|
||||
Environment* env,
|
||||
const EVPKeyPointer& pkey,
|
||||
const EVPKeyPointer::PrivateKeyEncodingConfig& config) {
|
||||
CHECK(pkey);
|
||||
if (!pkey) return {};
|
||||
auto res = pkey.writePrivateKey(config);
|
||||
if (res) {
|
||||
return BIOToStringOrBuffer(env, std::move(res.value), config);
|
||||
}
|
||||
if (res) return ToV8Value(env, std::move(res.value), config);
|
||||
|
||||
ThrowCryptoError(
|
||||
env, res.openssl_error.value_or(0), "Failed to encode private key");
|
||||
@@ -118,47 +116,37 @@ MaybeLocal<Value> WritePublicKey(
|
||||
Environment* env,
|
||||
const EVPKeyPointer& pkey,
|
||||
const EVPKeyPointer::PublicKeyEncodingConfig& config) {
|
||||
CHECK(pkey);
|
||||
if (!pkey) return {};
|
||||
auto res = pkey.writePublicKey(config);
|
||||
if (res) {
|
||||
return BIOToStringOrBuffer(env, res.value, config);
|
||||
}
|
||||
if (res) return ToV8Value(env, res.value, config);
|
||||
|
||||
ThrowCryptoError(
|
||||
env, res.openssl_error.value_or(0), "Failed to encode public key");
|
||||
return MaybeLocal<Value>();
|
||||
}
|
||||
|
||||
Maybe<void> ExportJWKSecretKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
bool ExportJWKSecretKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
CHECK_EQ(key.GetKeyType(), kKeyTypeSecret);
|
||||
|
||||
Local<Value> error;
|
||||
Local<Value> raw;
|
||||
MaybeLocal<Value> key_data = StringBytes::Encode(env->isolate(),
|
||||
key.GetSymmetricKey(),
|
||||
key.GetSymmetricKeySize(),
|
||||
BASE64URL,
|
||||
&error);
|
||||
if (!key_data.ToLocal(&raw)) {
|
||||
CHECK(!error.IsEmpty());
|
||||
if (!StringBytes::Encode(env->isolate(),
|
||||
key.GetSymmetricKey(),
|
||||
key.GetSymmetricKeySize(),
|
||||
BASE64URL,
|
||||
&error)
|
||||
.ToLocal(&raw)) {
|
||||
DCHECK(!error.IsEmpty());
|
||||
env->isolate()->ThrowException(error);
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (target->Set(
|
||||
env->context(),
|
||||
env->jwk_kty_string(),
|
||||
env->jwk_oct_string()).IsNothing() ||
|
||||
target->Set(
|
||||
env->context(),
|
||||
env->jwk_k_string(),
|
||||
raw).IsNothing()) {
|
||||
return Nothing<void>();
|
||||
}
|
||||
|
||||
return JustVoid();
|
||||
return target
|
||||
->Set(env->context(), env->jwk_kty_string(), env->jwk_oct_string())
|
||||
.IsJust() &&
|
||||
target->Set(env->context(), env->jwk_k_string(), raw).IsJust();
|
||||
}
|
||||
|
||||
KeyObjectData ImportJWKSecretKey(Environment* env, Local<Object> jwk) {
|
||||
@@ -174,10 +162,10 @@ KeyObjectData ImportJWKSecretKey(Environment* env, Local<Object> jwk) {
|
||||
ByteSource::FromEncodedString(env, key.As<String>()));
|
||||
}
|
||||
|
||||
Maybe<void> ExportJWKAsymmetricKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target,
|
||||
bool handleRsaPss) {
|
||||
bool ExportJWKAsymmetricKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target,
|
||||
bool handleRsaPss) {
|
||||
switch (key.GetAsymmetricKey().id()) {
|
||||
case EVP_PKEY_RSA_PSS: {
|
||||
if (handleRsaPss) return ExportJWKRsaKey(env, key, target);
|
||||
@@ -197,7 +185,7 @@ Maybe<void> ExportJWKAsymmetricKey(Environment* env,
|
||||
return ExportJWKEdKey(env, key, target);
|
||||
}
|
||||
THROW_ERR_CRYPTO_JWK_UNSUPPORTED_KEY_TYPE(env);
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
|
||||
KeyObjectData ImportJWKAsymmetricKey(Environment* env,
|
||||
@@ -216,35 +204,39 @@ KeyObjectData ImportJWKAsymmetricKey(Environment* env,
|
||||
return {};
|
||||
}
|
||||
|
||||
Maybe<void> GetSecretKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
bool GetSecretKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
// For the secret key detail, all we care about is the length,
|
||||
// converted to bits.
|
||||
size_t length = key.GetSymmetricKeySize() * CHAR_BIT;
|
||||
if (target
|
||||
->Set(env->context(),
|
||||
env->length_string(),
|
||||
Number::New(env->isolate(), static_cast<double>(length)))
|
||||
.IsNothing()) {
|
||||
return Nothing<void>();
|
||||
}
|
||||
return JustVoid();
|
||||
return target
|
||||
->Set(env->context(),
|
||||
env->length_string(),
|
||||
Number::New(
|
||||
env->isolate(),
|
||||
static_cast<double>(key.GetSymmetricKeySize() * CHAR_BIT)))
|
||||
.IsJust();
|
||||
}
|
||||
|
||||
Maybe<void> GetAsymmetricKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
bool GetAsymmetricKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
if (!key) {
|
||||
THROW_ERR_CRYPTO_OPERATION_FAILED(env);
|
||||
return false;
|
||||
}
|
||||
switch (key.GetAsymmetricKey().id()) {
|
||||
case EVP_PKEY_RSA:
|
||||
// Fall through
|
||||
case EVP_PKEY_RSA2:
|
||||
// Fall through
|
||||
case EVP_PKEY_RSA_PSS: return GetRsaKeyDetail(env, key, target);
|
||||
case EVP_PKEY_DSA: return GetDsaKeyDetail(env, key, target);
|
||||
case EVP_PKEY_EC: return GetEcKeyDetail(env, key, target);
|
||||
case EVP_PKEY_DH: return GetDhKeyDetail(env, key, target);
|
||||
}
|
||||
THROW_ERR_CRYPTO_INVALID_KEYTYPE(env);
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
|
||||
KeyObjectData TryParsePrivateKey(
|
||||
@@ -266,30 +258,34 @@ KeyObjectData TryParsePrivateKey(
|
||||
return {};
|
||||
}
|
||||
|
||||
// This maps true to JustVoid and false to Nothing<void>().
|
||||
static inline Maybe<void> NothingIfFalse(bool b) {
|
||||
return b ? JustVoid() : Nothing<void>();
|
||||
bool ExportJWKInner(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Value> result,
|
||||
bool handleRsaPss) {
|
||||
return key.GetKeyType() == kKeyTypeSecret
|
||||
? ExportJWKSecretKey(env, key, result.As<Object>())
|
||||
: ExportJWKAsymmetricKey(
|
||||
env, key, result.As<Object>(), handleRsaPss);
|
||||
}
|
||||
|
||||
Maybe<void> ExportJWKInner(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Value> result,
|
||||
bool handleRsaPss) {
|
||||
switch (key.GetKeyType()) {
|
||||
case kKeyTypeSecret:
|
||||
return ExportJWKSecretKey(env, key, result.As<Object>());
|
||||
case kKeyTypePublic:
|
||||
// Fall through
|
||||
case kKeyTypePrivate:
|
||||
return ExportJWKAsymmetricKey(
|
||||
env, key, result.As<Object>(), handleRsaPss);
|
||||
default:
|
||||
UNREACHABLE();
|
||||
int GetOKPCurveFromName(const char* name) {
|
||||
int nid;
|
||||
if (strcmp(name, "Ed25519") == 0) {
|
||||
nid = EVP_PKEY_ED25519;
|
||||
} else if (strcmp(name, "Ed448") == 0) {
|
||||
nid = EVP_PKEY_ED448;
|
||||
} else if (strcmp(name, "X25519") == 0) {
|
||||
nid = EVP_PKEY_X25519;
|
||||
} else if (strcmp(name, "X448") == 0) {
|
||||
nid = EVP_PKEY_X448;
|
||||
} else {
|
||||
nid = NID_undef;
|
||||
}
|
||||
return nid;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Maybe<void> KeyObjectData::ToEncodedPublicKey(
|
||||
bool KeyObjectData::ToEncodedPublicKey(
|
||||
Environment* env,
|
||||
const EVPKeyPointer::PublicKeyEncodingConfig& config,
|
||||
Local<Value>* out) {
|
||||
@@ -297,36 +293,33 @@ Maybe<void> KeyObjectData::ToEncodedPublicKey(
|
||||
if (config.output_key_object) {
|
||||
// Note that this has the downside of containing sensitive data of the
|
||||
// private key.
|
||||
return NothingIfFalse(
|
||||
KeyObjectHandle::Create(env, addRefWithType(KeyType::kKeyTypePublic))
|
||||
.ToLocal(out));
|
||||
return KeyObjectHandle::Create(env, addRefWithType(KeyType::kKeyTypePublic))
|
||||
.ToLocal(out);
|
||||
} else if (config.format == EVPKeyPointer::PKFormatType::JWK) {
|
||||
*out = Object::New(env->isolate());
|
||||
return ExportJWKInner(
|
||||
env, addRefWithType(KeyType::kKeyTypePublic), *out, false);
|
||||
}
|
||||
|
||||
return NothingIfFalse(
|
||||
WritePublicKey(env, GetAsymmetricKey(), config).ToLocal(out));
|
||||
return WritePublicKey(env, GetAsymmetricKey(), config).ToLocal(out);
|
||||
}
|
||||
|
||||
Maybe<void> KeyObjectData::ToEncodedPrivateKey(
|
||||
bool KeyObjectData::ToEncodedPrivateKey(
|
||||
Environment* env,
|
||||
const EVPKeyPointer::PrivateKeyEncodingConfig& config,
|
||||
Local<Value>* out) {
|
||||
CHECK(key_type_ != KeyType::kKeyTypeSecret);
|
||||
if (config.output_key_object) {
|
||||
return NothingIfFalse(
|
||||
KeyObjectHandle::Create(env, addRefWithType(KeyType::kKeyTypePrivate))
|
||||
.ToLocal(out));
|
||||
return KeyObjectHandle::Create(env,
|
||||
addRefWithType(KeyType::kKeyTypePrivate))
|
||||
.ToLocal(out);
|
||||
} else if (config.format == EVPKeyPointer::PKFormatType::JWK) {
|
||||
*out = Object::New(env->isolate());
|
||||
return ExportJWKInner(
|
||||
env, addRefWithType(KeyType::kKeyTypePrivate), *out, false);
|
||||
}
|
||||
|
||||
return NothingIfFalse(
|
||||
WritePrivateKey(env, GetAsymmetricKey(), config).ToLocal(out));
|
||||
return WritePrivateKey(env, GetAsymmetricKey(), config).ToLocal(out);
|
||||
}
|
||||
|
||||
Maybe<EVPKeyPointer::PrivateKeyEncodingConfig>
|
||||
@@ -502,6 +495,7 @@ KeyObjectData KeyObjectData::GetParsedKey(KeyType type,
|
||||
EVPKeyPointer&& pkey,
|
||||
ParseKeyResult ret,
|
||||
const char* default_msg) {
|
||||
MarkPopErrorOnReturn mark_pop_error_on_return;
|
||||
switch (ret) {
|
||||
case ParseKeyResult::kParseKeyOk: {
|
||||
return CreateAsymmetric(type, std::move(pkey));
|
||||
@@ -512,7 +506,7 @@ KeyObjectData KeyObjectData::GetParsedKey(KeyType type,
|
||||
return {};
|
||||
}
|
||||
default: {
|
||||
ThrowCryptoError(env, ERR_get_error(), default_msg);
|
||||
ThrowCryptoError(env, mark_pop_error_on_return.peekError(), default_msg);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
@@ -593,12 +587,12 @@ size_t KeyObjectData::GetSymmetricKeySize() const {
|
||||
}
|
||||
|
||||
bool KeyObjectHandle::HasInstance(Environment* env, Local<Value> value) {
|
||||
Local<FunctionTemplate> t = env->crypto_key_object_handle_constructor();
|
||||
auto t = env->crypto_key_object_handle_constructor();
|
||||
return !t.IsEmpty() && t->HasInstance(value);
|
||||
}
|
||||
|
||||
Local<Function> KeyObjectHandle::Initialize(Environment* env) {
|
||||
Local<FunctionTemplate> templ = env->crypto_key_object_handle_constructor();
|
||||
auto templ = env->crypto_key_object_handle_constructor();
|
||||
if (templ.IsEmpty()) {
|
||||
Isolate* isolate = env->isolate();
|
||||
templ = NewFunctionTemplate(isolate, New);
|
||||
@@ -646,8 +640,9 @@ MaybeLocal<Object> KeyObjectHandle::Create(Environment* env,
|
||||
Local<Object> obj;
|
||||
Local<Function> ctor = KeyObjectHandle::Initialize(env);
|
||||
CHECK(!env->crypto_key_object_handle_constructor().IsEmpty());
|
||||
if (!ctor->NewInstance(env->context(), 0, nullptr).ToLocal(&obj))
|
||||
return MaybeLocal<Object>();
|
||||
if (!ctor->NewInstance(env->context(), 0, nullptr).ToLocal(&obj)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
KeyObjectHandle* key = Unwrap<KeyObjectHandle>(obj);
|
||||
CHECK_NOT_NULL(key);
|
||||
@@ -783,22 +778,6 @@ void KeyObjectHandle::InitECRaw(const FunctionCallbackInfo<Value>& args) {
|
||||
args.GetReturnValue().Set(true);
|
||||
}
|
||||
|
||||
int GetOKPCurveFromName(const char* name) {
|
||||
int nid;
|
||||
if (strcmp(name, "Ed25519") == 0) {
|
||||
nid = EVP_PKEY_ED25519;
|
||||
} else if (strcmp(name, "Ed448") == 0) {
|
||||
nid = EVP_PKEY_ED448;
|
||||
} else if (strcmp(name, "X25519") == 0) {
|
||||
nid = EVP_PKEY_X25519;
|
||||
} else if (strcmp(name, "X448") == 0) {
|
||||
nid = EVP_PKEY_X448;
|
||||
} else {
|
||||
nid = NID_undef;
|
||||
}
|
||||
return nid;
|
||||
}
|
||||
|
||||
void KeyObjectHandle::InitEDRaw(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
KeyObjectHandle* key;
|
||||
@@ -867,6 +846,7 @@ void KeyObjectHandle::Equals(const FunctionCallbackInfo<Value>& args) {
|
||||
break;
|
||||
}
|
||||
case kKeyTypePublic:
|
||||
// Fall through
|
||||
case kKeyTypePrivate: {
|
||||
EVP_PKEY* pkey = key.GetAsymmetricKey().get();
|
||||
EVP_PKEY* pkey2 = key2.GetAsymmetricKey().get();
|
||||
@@ -898,22 +878,16 @@ void KeyObjectHandle::GetKeyDetail(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
const auto& data = key->Data();
|
||||
|
||||
switch (data.GetKeyType()) {
|
||||
case kKeyTypeSecret:
|
||||
if (GetSecretKeyDetail(env, data, args[0].As<Object>()).IsNothing())
|
||||
return;
|
||||
break;
|
||||
case kKeyTypePublic:
|
||||
// Fall through
|
||||
case kKeyTypePrivate:
|
||||
if (GetAsymmetricKeyDetail(env, data, args[0].As<Object>()).IsNothing())
|
||||
return;
|
||||
break;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
if (data.GetKeyType() == kKeyTypeSecret) {
|
||||
if (GetSecretKeyDetail(env, data, args[0].As<Object>())) [[likely]] {
|
||||
args.GetReturnValue().Set(args[0]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
args.GetReturnValue().Set(args[0]);
|
||||
if (GetAsymmetricKeyDetail(env, data, args[0].As<Object>())) [[likely]] {
|
||||
args.GetReturnValue().Set(args[0]);
|
||||
}
|
||||
}
|
||||
|
||||
Local<Value> KeyObjectHandle::GetAsymmetricKeyType() const {
|
||||
@@ -957,11 +931,8 @@ bool KeyObjectHandle::CheckEcKeyData() const {
|
||||
CHECK(ctx);
|
||||
CHECK_EQ(key.id(), EVP_PKEY_EC);
|
||||
|
||||
if (data_.GetKeyType() == kKeyTypePrivate) {
|
||||
return ctx.privateCheck();
|
||||
}
|
||||
|
||||
return ctx.publicCheck();
|
||||
return data_.GetKeyType() == kKeyTypePrivate ? ctx.privateCheck()
|
||||
: ctx.publicCheck();
|
||||
}
|
||||
|
||||
void KeyObjectHandle::CheckEcKeyData(const FunctionCallbackInfo<Value>& args) {
|
||||
@@ -984,12 +955,17 @@ void KeyObjectHandle::Export(const FunctionCallbackInfo<Value>& args) {
|
||||
ASSIGN_OR_RETURN_UNWRAP(&key, args.This());
|
||||
|
||||
KeyType type = key->Data().GetKeyType();
|
||||
unsigned int offset = 0;
|
||||
|
||||
MaybeLocal<Value> result;
|
||||
Local<Value> result;
|
||||
if (type == kKeyTypeSecret) {
|
||||
result = key->ExportSecretKey();
|
||||
} else if (type == kKeyTypePublic) {
|
||||
unsigned int offset = 0;
|
||||
if (key->ExportSecretKey().ToLocal(&result)) [[likely]] {
|
||||
args.GetReturnValue().Set(result);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == kKeyTypePublic) {
|
||||
EVPKeyPointer::PublicKeyEncodingConfig config;
|
||||
if (!KeyObjectData::GetPublicKeyEncodingFromJs(
|
||||
args, &offset, kKeyContextExport)
|
||||
@@ -997,28 +973,29 @@ void KeyObjectHandle::Export(const FunctionCallbackInfo<Value>& args) {
|
||||
return;
|
||||
}
|
||||
CHECK_EQ(offset, static_cast<unsigned int>(args.Length()));
|
||||
result = key->ExportPublicKey(config);
|
||||
} else {
|
||||
CHECK_EQ(type, kKeyTypePrivate);
|
||||
unsigned int offset = 0;
|
||||
EVPKeyPointer::PrivateKeyEncodingConfig config;
|
||||
if (!KeyObjectData::GetPrivateKeyEncodingFromJs(
|
||||
args, &offset, kKeyContextExport)
|
||||
.To(&config)) {
|
||||
return;
|
||||
if (key->ExportPublicKey(config).ToLocal(&result)) [[likely]] {
|
||||
args.GetReturnValue().Set(result);
|
||||
}
|
||||
CHECK_EQ(offset, static_cast<unsigned int>(args.Length()));
|
||||
result = key->ExportPrivateKey(config);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!result.IsEmpty())
|
||||
args.GetReturnValue().Set(result.FromMaybe(Local<Value>()));
|
||||
CHECK_EQ(type, kKeyTypePrivate);
|
||||
EVPKeyPointer::PrivateKeyEncodingConfig config;
|
||||
if (!KeyObjectData::GetPrivateKeyEncodingFromJs(
|
||||
args, &offset, kKeyContextExport)
|
||||
.To(&config)) {
|
||||
return;
|
||||
}
|
||||
CHECK_EQ(offset, static_cast<unsigned int>(args.Length()));
|
||||
if (key->ExportPrivateKey(config).ToLocal(&result)) [[likely]] {
|
||||
args.GetReturnValue().Set(result);
|
||||
}
|
||||
}
|
||||
|
||||
MaybeLocal<Value> KeyObjectHandle::ExportSecretKey() const {
|
||||
const char* buf = data_.GetSymmetricKey();
|
||||
unsigned int len = data_.GetSymmetricKeySize();
|
||||
return Buffer::Copy(env(), buf, len).FromMaybe(Local<Value>());
|
||||
return Buffer::Copy(
|
||||
env(), data_.GetSymmetricKey(), data_.GetSymmetricKeySize())
|
||||
.FromMaybe(Local<Value>());
|
||||
}
|
||||
|
||||
MaybeLocal<Value> KeyObjectHandle::ExportPublicKey(
|
||||
@@ -1040,9 +1017,9 @@ void KeyObjectHandle::ExportJWK(
|
||||
CHECK(args[0]->IsObject());
|
||||
CHECK(args[1]->IsBoolean());
|
||||
|
||||
ExportJWKInner(env, key->Data(), args[0], args[1]->IsTrue());
|
||||
|
||||
args.GetReturnValue().Set(args[0]);
|
||||
if (ExportJWKInner(env, key->Data(), args[0], args[1]->IsTrue())) {
|
||||
args.GetReturnValue().Set(args[0]);
|
||||
}
|
||||
}
|
||||
|
||||
void NativeKeyObject::Initialize(Environment* env, Local<Object> target) {
|
||||
|
||||
@@ -16,9 +16,7 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace node {
|
||||
namespace crypto {
|
||||
|
||||
namespace node::crypto {
|
||||
enum KeyType {
|
||||
kKeyTypeSecret,
|
||||
kKeyTypePublic,
|
||||
@@ -85,12 +83,12 @@ class KeyObjectData final : public MemoryRetainer {
|
||||
unsigned int* offset,
|
||||
KeyEncodingContext context);
|
||||
|
||||
v8::Maybe<void> ToEncodedPublicKey(
|
||||
bool ToEncodedPublicKey(
|
||||
Environment* env,
|
||||
const ncrypto::EVPKeyPointer::PublicKeyEncodingConfig& config,
|
||||
v8::Local<v8::Value>* out);
|
||||
|
||||
v8::Maybe<void> ToEncodedPrivateKey(
|
||||
bool ToEncodedPrivateKey(
|
||||
Environment* env,
|
||||
const ncrypto::EVPKeyPointer::PrivateKeyEncodingConfig& config,
|
||||
v8::Local<v8::Value>* out);
|
||||
@@ -378,15 +376,11 @@ WebCryptoKeyExportStatus PKEY_SPKI_Export(const KeyObjectData& key_data,
|
||||
WebCryptoKeyExportStatus PKEY_PKCS8_Export(const KeyObjectData& key_data,
|
||||
ByteSource* out);
|
||||
|
||||
int GetOKPCurveFromName(const char* name);
|
||||
|
||||
namespace Keys {
|
||||
void Initialize(Environment* env, v8::Local<v8::Object> target);
|
||||
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
|
||||
} // namespace Keys
|
||||
|
||||
} // namespace crypto
|
||||
} // namespace node
|
||||
} // namespace node::crypto
|
||||
|
||||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
||||
#endif // SRC_CRYPTO_CRYPTO_KEYS_H_
|
||||
@@ -13,6 +13,7 @@ namespace node {
|
||||
|
||||
using ncrypto::BignumPointer;
|
||||
using ncrypto::ClearErrorOnReturn;
|
||||
using ncrypto::DataPointer;
|
||||
using v8::ArrayBuffer;
|
||||
using v8::Boolean;
|
||||
using v8::FunctionCallbackInfo;
|
||||
@@ -196,9 +197,9 @@ bool CheckPrimeTraits::DeriveBits(
|
||||
int ret = params.candidate.isPrime(params.checks, getPrimeCheckCallback(env));
|
||||
if (ret < 0) [[unlikely]]
|
||||
return false;
|
||||
ByteSource::Builder buf(1);
|
||||
buf.data<char>()[0] = ret;
|
||||
*out = std::move(buf).release();
|
||||
auto buf = DataPointer::Alloc(1);
|
||||
static_cast<char*>(buf.get())[0] = ret;
|
||||
*out = ByteSource::Allocated(buf.release());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+17
-17
@@ -318,9 +318,9 @@ WebCryptoCipherStatus RSACipherTraits::DoCipher(Environment* env,
|
||||
return WebCryptoCipherStatus::FAILED;
|
||||
}
|
||||
|
||||
Maybe<void> ExportJWKRsaKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
bool ExportJWKRsaKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
Mutex::ScopedLock lock(key.mutex());
|
||||
const auto& m_pkey = key.GetAsymmetricKey();
|
||||
|
||||
@@ -328,7 +328,7 @@ Maybe<void> ExportJWKRsaKey(Environment* env,
|
||||
if (!rsa ||
|
||||
target->Set(env->context(), env->jwk_kty_string(), env->jwk_rsa_string())
|
||||
.IsNothing()) {
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
|
||||
auto pub_key = rsa.getPublicKey();
|
||||
@@ -337,7 +337,7 @@ Maybe<void> ExportJWKRsaKey(Environment* env,
|
||||
.IsNothing() ||
|
||||
SetEncodedValue(env, target, env->jwk_e_string(), pub_key.e)
|
||||
.IsNothing()) {
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (key.GetKeyType() == kKeyTypePrivate) {
|
||||
@@ -354,11 +354,11 @@ Maybe<void> ExportJWKRsaKey(Environment* env,
|
||||
.IsNothing() ||
|
||||
SetEncodedValue(env, target, env->jwk_qi_string(), pvt_key.qi)
|
||||
.IsNothing()) {
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return JustVoid();
|
||||
return true;
|
||||
}
|
||||
|
||||
KeyObjectData ImportJWKRsaKey(Environment* env,
|
||||
@@ -441,16 +441,16 @@ KeyObjectData ImportJWKRsaKey(Environment* env,
|
||||
return KeyObjectData::CreateAsymmetric(type, std::move(pkey));
|
||||
}
|
||||
|
||||
Maybe<void> GetRsaKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
bool GetRsaKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
Local<Object> target) {
|
||||
Mutex::ScopedLock lock(key.mutex());
|
||||
const auto& m_pkey = key.GetAsymmetricKey();
|
||||
|
||||
// TODO(tniessen): Remove the "else" branch once we drop support for OpenSSL
|
||||
// versions older than 1.1.1e via FIPS / dynamic linking.
|
||||
const ncrypto::Rsa rsa = m_pkey;
|
||||
if (!rsa) return Nothing<void>();
|
||||
if (!rsa) return false;
|
||||
|
||||
auto pub_key = rsa.getPublicKey();
|
||||
|
||||
@@ -461,7 +461,7 @@ Maybe<void> GetRsaKeyDetail(Environment* env,
|
||||
env->isolate(),
|
||||
static_cast<double>(BignumPointer::GetBitCount(pub_key.n))))
|
||||
.IsNothing()) {
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
|
||||
auto public_exponent = ArrayBuffer::NewBackingStore(
|
||||
@@ -479,7 +479,7 @@ Maybe<void> GetRsaKeyDetail(Environment* env,
|
||||
env->public_exponent_string(),
|
||||
ArrayBuffer::New(env->isolate(), std::move(public_exponent)))
|
||||
.IsNothing()) {
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_pkey.id() == EVP_PKEY_RSA_PSS) {
|
||||
@@ -500,7 +500,7 @@ Maybe<void> GetRsaKeyDetail(Environment* env,
|
||||
env->hash_algorithm_string(),
|
||||
OneByteString(env->isolate(), params.digest))
|
||||
.IsNothing()) {
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
|
||||
// If, for some reason, the MGF is not MGF1, then the MGF1 hash function
|
||||
@@ -512,7 +512,7 @@ Maybe<void> GetRsaKeyDetail(Environment* env,
|
||||
env->mgf1_hash_algorithm_string(),
|
||||
OneByteString(env->isolate(), digest))
|
||||
.IsNothing()) {
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -521,12 +521,12 @@ Maybe<void> GetRsaKeyDetail(Environment* env,
|
||||
env->salt_length_string(),
|
||||
Integer::New(env->isolate(), params.salt_length))
|
||||
.IsNothing()) {
|
||||
return Nothing<void>();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return JustVoid();
|
||||
return true;
|
||||
}
|
||||
|
||||
namespace RSAAlg {
|
||||
|
||||
@@ -112,18 +112,18 @@ struct RSACipherTraits final {
|
||||
|
||||
using RSACipherJob = CipherJob<RSACipherTraits>;
|
||||
|
||||
v8::Maybe<void> ExportJWKRsaKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
bool ExportJWKRsaKey(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
|
||||
KeyObjectData ImportJWKRsaKey(Environment* env,
|
||||
v8::Local<v8::Object> jwk,
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args,
|
||||
unsigned int offset);
|
||||
|
||||
v8::Maybe<void> GetRsaKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
bool GetRsaKeyDetail(Environment* env,
|
||||
const KeyObjectData& key,
|
||||
v8::Local<v8::Object> target);
|
||||
|
||||
namespace RSAAlg {
|
||||
void Initialize(Environment* env, v8::Local<v8::Object> target);
|
||||
|
||||
@@ -700,12 +700,12 @@ bool SignTraits::DeriveBits(
|
||||
break;
|
||||
}
|
||||
case SignConfiguration::Mode::Verify: {
|
||||
ByteSource::Builder buf(1);
|
||||
buf.data<char>()[0] = 0;
|
||||
auto buf = DataPointer::Alloc(1);
|
||||
static_cast<char*>(buf.get())[0] = 0;
|
||||
if (context.verify(params.data, params.signature)) {
|
||||
buf.data<char>()[0] = 1;
|
||||
static_cast<char*>(buf.get())[0] = 1;
|
||||
}
|
||||
*out = std::move(buf).release();
|
||||
*out = ByteSource::Allocated(buf.release());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1612,7 +1612,7 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) {
|
||||
// Store the SNI context for later use.
|
||||
w->sni_context_ = BaseObjectPtr<SecureContext>(sc);
|
||||
|
||||
if (UseSNIContext(w->ssl_, w->sni_context_) && !w->SetCACerts(sc)) {
|
||||
if (w->ssl_.setSniContext(w->sni_context_->ctx()) && !w->SetCACerts(sc)) {
|
||||
// Not clear why sometimes we throw error, and sometimes we call
|
||||
// onerror(). Both cause .destroy(), but onerror does a bit more.
|
||||
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
|
||||
@@ -1675,8 +1675,7 @@ void TLSWrap::SetKeyCert(const FunctionCallbackInfo<Value>& args) {
|
||||
if (cons->HasInstance(ctx)) {
|
||||
SecureContext* sc = Unwrap<SecureContext>(ctx.As<Object>());
|
||||
CHECK_NOT_NULL(sc);
|
||||
if (!UseSNIContext(w->ssl_, BaseObjectPtr<SecureContext>(sc)) ||
|
||||
!w->SetCACerts(sc)) {
|
||||
if (!w->ssl_.setSniContext(sc->ctx()) || !w->SetCACerts(sc)) {
|
||||
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
|
||||
return ThrowCryptoError(env, err, "SetKeyCert");
|
||||
}
|
||||
@@ -1851,8 +1850,7 @@ void TLSWrap::VerifyError(const FunctionCallbackInfo<Value>& args) {
|
||||
// peer certificate is questionable but it's compatible with what was
|
||||
// here before.
|
||||
long x509_verify_error = // NOLINT(runtime/int)
|
||||
VerifyPeerCertificate(
|
||||
w->ssl_,
|
||||
w->ssl_.verifyPeerCertificate().value_or(
|
||||
X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT);
|
||||
|
||||
if (x509_verify_error == X509_V_OK)
|
||||
|
||||
+12
-10
@@ -29,6 +29,7 @@ namespace node {
|
||||
using ncrypto::BignumPointer;
|
||||
using ncrypto::BIOPointer;
|
||||
using ncrypto::CryptoErrorList;
|
||||
using ncrypto::DataPointer;
|
||||
using ncrypto::EnginePointer;
|
||||
using ncrypto::EVPKeyCtxPointer;
|
||||
using ncrypto::SSLCtxPointer;
|
||||
@@ -386,9 +387,9 @@ MaybeLocal<Uint8Array> ByteSource::ToBuffer(Environment* env) {
|
||||
ByteSource ByteSource::FromBIO(const BIOPointer& bio) {
|
||||
CHECK(bio);
|
||||
BUF_MEM* bptr = bio;
|
||||
ByteSource::Builder out(bptr->length);
|
||||
memcpy(out.data<void>(), bptr->data, bptr->length);
|
||||
return std::move(out).release();
|
||||
auto out = DataPointer::Alloc(bptr->length);
|
||||
memcpy(out.get(), bptr->data, bptr->length);
|
||||
return ByteSource::Allocated(out.release());
|
||||
}
|
||||
|
||||
ByteSource ByteSource::FromEncodedString(Environment* env,
|
||||
@@ -398,10 +399,10 @@ ByteSource ByteSource::FromEncodedString(Environment* env,
|
||||
ByteSource out;
|
||||
|
||||
if (StringBytes::Size(env->isolate(), key, enc).To(&length) && length > 0) {
|
||||
ByteSource::Builder buf(length);
|
||||
size_t actual =
|
||||
StringBytes::Write(env->isolate(), buf.data<char>(), length, key, enc);
|
||||
out = std::move(buf).release(actual);
|
||||
auto buf = DataPointer::Alloc(length);
|
||||
size_t actual = StringBytes::Write(
|
||||
env->isolate(), static_cast<char*>(buf.get()), length, key, enc);
|
||||
out = ByteSource::Allocated(buf.resize(actual).release());
|
||||
}
|
||||
|
||||
return out;
|
||||
@@ -418,11 +419,12 @@ ByteSource ByteSource::FromString(Environment* env, Local<String> str,
|
||||
CHECK(str->IsString());
|
||||
size_t size = str->Utf8Length(env->isolate());
|
||||
size_t alloc_size = ntc ? size + 1 : size;
|
||||
ByteSource::Builder out(alloc_size);
|
||||
auto out = DataPointer::Alloc(alloc_size);
|
||||
int opts = String::NO_OPTIONS;
|
||||
if (!ntc) opts |= String::NO_NULL_TERMINATION;
|
||||
str->WriteUtf8(env->isolate(), out.data<char>(), alloc_size, nullptr, opts);
|
||||
return std::move(out).release();
|
||||
str->WriteUtf8(
|
||||
env->isolate(), static_cast<char*>(out.get()), alloc_size, nullptr, opts);
|
||||
return ByteSource::Allocated(out.release());
|
||||
}
|
||||
|
||||
ByteSource ByteSource::FromBuffer(Local<Value> buffer, bool ntc) {
|
||||
|
||||
@@ -166,53 +166,6 @@ T* MallocOpenSSL(size_t count) {
|
||||
// contents are zeroed.
|
||||
class ByteSource final {
|
||||
public:
|
||||
class Builder {
|
||||
public:
|
||||
// Allocates memory using OpenSSL's memory allocator.
|
||||
explicit Builder(size_t size)
|
||||
: data_(MallocOpenSSL<char>(size)), size_(size) {}
|
||||
|
||||
Builder(Builder&& other) = delete;
|
||||
Builder& operator=(Builder&& other) = delete;
|
||||
Builder(const Builder&) = delete;
|
||||
Builder& operator=(const Builder&) = delete;
|
||||
|
||||
~Builder() { OPENSSL_clear_free(data_, size_); }
|
||||
|
||||
// Returns the underlying non-const pointer.
|
||||
template <typename T>
|
||||
T* data() {
|
||||
return reinterpret_cast<T*>(data_);
|
||||
}
|
||||
|
||||
// Returns the (allocated) size in bytes.
|
||||
size_t size() const { return size_; }
|
||||
|
||||
// Returns if (allocated) size is zero.
|
||||
bool empty() const { return size_ == 0; }
|
||||
|
||||
// Finalizes the Builder and returns a read-only view that is optionally
|
||||
// truncated.
|
||||
ByteSource release(std::optional<size_t> resize = std::nullopt) && {
|
||||
if (resize) {
|
||||
CHECK_LE(*resize, size_);
|
||||
if (*resize == 0) {
|
||||
OPENSSL_clear_free(data_, size_);
|
||||
data_ = nullptr;
|
||||
}
|
||||
size_ = *resize;
|
||||
}
|
||||
ByteSource out = ByteSource::Allocated(data_, size_);
|
||||
data_ = nullptr;
|
||||
size_ = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
private:
|
||||
void* data_;
|
||||
size_t size_;
|
||||
};
|
||||
|
||||
ByteSource() = default;
|
||||
ByteSource(ByteSource&& other) noexcept;
|
||||
~ByteSource();
|
||||
@@ -651,18 +604,18 @@ class ArrayBufferOrViewContents final {
|
||||
}
|
||||
|
||||
inline ByteSource ToCopy() const {
|
||||
if (empty()) return ByteSource();
|
||||
ByteSource::Builder buf(size());
|
||||
memcpy(buf.data<void>(), data(), size());
|
||||
return std::move(buf).release();
|
||||
if (empty()) return {};
|
||||
auto buf = ncrypto::DataPointer::Alloc(size());
|
||||
memcpy(buf.get(), data(), size());
|
||||
return ByteSource::Allocated(buf.release());
|
||||
}
|
||||
|
||||
inline ByteSource ToNullTerminatedCopy() const {
|
||||
if (empty()) return ByteSource();
|
||||
ByteSource::Builder buf(size() + 1);
|
||||
memcpy(buf.data<void>(), data(), size());
|
||||
buf.data<char>()[size()] = 0;
|
||||
return std::move(buf).release(size());
|
||||
if (empty()) return {};
|
||||
auto buf = ncrypto::DataPointer::Alloc(size() + 1);
|
||||
memcpy(buf.get(), data(), size());
|
||||
static_cast<char*>(buf.get())[size()] = 0;
|
||||
return ByteSource::Allocated(buf.release());
|
||||
}
|
||||
|
||||
inline ncrypto::DataPointer ToDataPointer() const {
|
||||
|
||||
+13
-62
@@ -21,6 +21,7 @@ using ncrypto::ClearErrorOnReturn;
|
||||
using ncrypto::DataPointer;
|
||||
using ncrypto::ECKeyPointer;
|
||||
using ncrypto::SSLPointer;
|
||||
using ncrypto::X509Name;
|
||||
using ncrypto::X509Pointer;
|
||||
using ncrypto::X509View;
|
||||
using v8::Array;
|
||||
@@ -92,6 +93,11 @@ void Fingerprint(const FunctionCallbackInfo<Value>& args) {
|
||||
}
|
||||
}
|
||||
|
||||
MaybeLocal<String> ToV8Value(Environment* env, std::string_view val) {
|
||||
return String::NewFromUtf8(
|
||||
env->isolate(), val.data(), NewStringType::kNormal, val.size());
|
||||
}
|
||||
|
||||
MaybeLocal<Value> ToV8Value(Local<Context> context, BIOPointer&& bio) {
|
||||
if (!bio) [[unlikely]]
|
||||
return {};
|
||||
@@ -106,51 +112,6 @@ MaybeLocal<Value> ToV8Value(Local<Context> context, BIOPointer&& bio) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
MaybeLocal<Value> ToV8Value(Local<Context> context, const ASN1_OBJECT* obj) {
|
||||
// If OpenSSL knows the type, use the short name of the type as the key, and
|
||||
// the numeric representation of the type's OID otherwise.
|
||||
int nid = OBJ_obj2nid(obj);
|
||||
char buf[80];
|
||||
const char* str;
|
||||
if (nid != NID_undef) {
|
||||
str = OBJ_nid2sn(nid);
|
||||
CHECK_NOT_NULL(str);
|
||||
} else {
|
||||
OBJ_obj2txt(buf, sizeof(buf), obj, true);
|
||||
str = buf;
|
||||
}
|
||||
|
||||
Local<Value> result;
|
||||
if (!String::NewFromUtf8(context->GetIsolate(), str).ToLocal(&result)) {
|
||||
return {};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
MaybeLocal<Value> ToV8Value(Local<Context> context, const ASN1_STRING* str) {
|
||||
// The previous implementation used X509_NAME_print_ex, which escapes some
|
||||
// characters in the value. The old implementation did not decode/unescape
|
||||
// values correctly though, leading to ambiguous and incorrect
|
||||
// representations. The new implementation only converts to Unicode and does
|
||||
// not escape anything.
|
||||
unsigned char* value_str;
|
||||
int value_str_size = ASN1_STRING_to_UTF8(&value_str, str);
|
||||
if (value_str_size < 0) [[unlikely]] {
|
||||
return Undefined(context->GetIsolate());
|
||||
}
|
||||
DataPointer free_value_str(value_str, value_str_size);
|
||||
|
||||
Local<Value> result;
|
||||
if (!String::NewFromUtf8(context->GetIsolate(),
|
||||
reinterpret_cast<const char*>(value_str),
|
||||
NewStringType::kNormal,
|
||||
value_str_size)
|
||||
.ToLocal(&result)) {
|
||||
return {};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
MaybeLocal<Value> ToV8Value(Local<Context> context, const BIOPointer& bio) {
|
||||
if (!bio) [[unlikely]]
|
||||
return {};
|
||||
@@ -594,14 +555,9 @@ bool Set(Environment* env,
|
||||
// Convert an X509_NAME* into a JavaScript object.
|
||||
// Each entry of the name is converted into a property of the object.
|
||||
// The property value may be a single string or an array of strings.
|
||||
template <X509_NAME* get_name(const X509*)>
|
||||
static MaybeLocal<Value> GetX509NameObject(Environment* env,
|
||||
const X509View& cert) {
|
||||
X509_NAME* name = get_name(cert.get());
|
||||
CHECK_NOT_NULL(name);
|
||||
|
||||
int cnt = X509_NAME_entry_count(name);
|
||||
CHECK_GE(cnt, 0);
|
||||
const X509Name& name) {
|
||||
if (!name) return {};
|
||||
|
||||
Local<Value> v8_name;
|
||||
Local<Value> v8_value;
|
||||
@@ -610,14 +566,9 @@ static MaybeLocal<Value> GetX509NameObject(Environment* env,
|
||||
Object::New(env->isolate(), Null(env->isolate()), nullptr, nullptr, 0);
|
||||
if (result.IsEmpty()) return {};
|
||||
|
||||
for (int i = 0; i < cnt; i++) {
|
||||
X509_NAME_ENTRY* entry = X509_NAME_get_entry(name, i);
|
||||
CHECK_NOT_NULL(entry);
|
||||
|
||||
if (!ToV8Value(env->context(), X509_NAME_ENTRY_get_object(entry))
|
||||
.ToLocal(&v8_name) ||
|
||||
!ToV8Value(env->context(), X509_NAME_ENTRY_get_data(entry))
|
||||
.ToLocal(&v8_value)) {
|
||||
for (auto i : name) {
|
||||
if (!ToV8Value(env, i.first).ToLocal(&v8_name) ||
|
||||
!ToV8Value(env, i.second).ToLocal(&v8_value)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -727,11 +678,11 @@ MaybeLocal<Object> X509ToObject(Environment* env, const X509View& cert) {
|
||||
if (!Set<Value>(env,
|
||||
info,
|
||||
env->subject_string(),
|
||||
GetX509NameObject<X509_get_subject_name>(env, cert)) ||
|
||||
GetX509NameObject(env, cert.getSubjectName())) ||
|
||||
!Set<Value>(env,
|
||||
info,
|
||||
env->issuer_string(),
|
||||
GetX509NameObject<X509_get_issuer_name>(env, cert)) ||
|
||||
GetX509NameObject(env, cert.getIssuerName())) ||
|
||||
!Set<Value>(env,
|
||||
info,
|
||||
env->subjectaltname_string(),
|
||||
|
||||
@@ -34,6 +34,7 @@ using v8::Maybe;
|
||||
using v8::MaybeLocal;
|
||||
using v8::Nothing;
|
||||
using v8::Object;
|
||||
using v8::Undefined;
|
||||
using v8::Value;
|
||||
|
||||
namespace quic {
|
||||
@@ -589,7 +590,7 @@ SSLPointer TLSSession::Initialize(
|
||||
|
||||
std::optional<TLSSession::PeerIdentityValidationError>
|
||||
TLSSession::VerifyPeerIdentity(Environment* env) {
|
||||
int err = crypto::VerifyPeerCertificate(ssl_);
|
||||
int err = ssl_.verifyPeerCertificate().value_or(X509_V_ERR_UNSPECIFIED);
|
||||
if (err == X509_V_OK) return std::nullopt;
|
||||
Local<Value> reason;
|
||||
Local<Value> code;
|
||||
@@ -619,11 +620,15 @@ MaybeLocal<Object> TLSSession::ephemeral_key(Environment* env) const {
|
||||
}
|
||||
|
||||
MaybeLocal<Value> TLSSession::cipher_name(Environment* env) const {
|
||||
return crypto::GetCurrentCipherName(env, ssl_);
|
||||
auto name = ssl_.getCipherName();
|
||||
if (!name.has_value()) return Undefined(env->isolate());
|
||||
return OneByteString(env->isolate(), name.value());
|
||||
}
|
||||
|
||||
MaybeLocal<Value> TLSSession::cipher_version(Environment* env) const {
|
||||
return crypto::GetCurrentCipherVersion(env, ssl_);
|
||||
auto version = ssl_.getCipherVersion();
|
||||
if (!version.has_value()) return Undefined(env->isolate());
|
||||
return OneByteString(env->isolate(), version.value());
|
||||
}
|
||||
|
||||
const std::string_view TLSSession::servername() const {
|
||||
|
||||
Reference in new issue
Block a user