diff --git a/src/node_crypto.cc b/src/node_crypto.cc
index 36102e53f9c045..84f006b00f67eb 100644
--- a/src/node_crypto.cc
+++ b/src/node_crypto.cc
@@ -1804,11 +1804,10 @@ void SSLWrap::GetSession(const FunctionCallbackInfo& args) {
int slen = i2d_SSL_SESSION(sess, nullptr);
CHECK_GT(slen, 0);
- char* sbuf = new char[slen];
+ char* sbuf = Malloc(slen);
unsigned char* p = reinterpret_cast(sbuf);
i2d_SSL_SESSION(sess, &p);
- args.GetReturnValue().Set(Encode(env->isolate(), sbuf, slen, BUFFER));
- delete[] sbuf;
+ args.GetReturnValue().Set(Buffer::New(env, sbuf, slen).ToLocalChecked());
}
@@ -2373,10 +2372,9 @@ int SSLWrap::TLSExtStatusCallback(SSL* s, void* arg) {
if (resp == nullptr) {
arg = Null(env->isolate());
} else {
- arg = Buffer::Copy(
- env,
- reinterpret_cast(const_cast(resp)),
- len).ToLocalChecked();
+ arg =
+ Buffer::Copy(env, reinterpret_cast(resp), len)
+ .ToLocalChecked();
}
w->MakeCallback(env->onocspresponse_string(), 1, &arg);
@@ -3333,16 +3331,16 @@ void CipherBase::Init(const char* cipher_type,
}
#endif // NODE_FIPS_MODE
- CHECK_EQ(cipher_, nullptr);
- cipher_ = EVP_get_cipherbyname(cipher_type);
- if (cipher_ == nullptr) {
+ CHECK_EQ(initialised_, false);
+ const EVP_CIPHER* const cipher = EVP_get_cipherbyname(cipher_type);
+ if (cipher == nullptr) {
return env()->ThrowError("Unknown cipher");
}
unsigned char key[EVP_MAX_KEY_LENGTH];
unsigned char iv[EVP_MAX_IV_LENGTH];
- int key_len = EVP_BytesToKey(cipher_,
+ int key_len = EVP_BytesToKey(cipher,
EVP_md5(),
nullptr,
reinterpret_cast(key_buf),
@@ -3353,7 +3351,7 @@ void CipherBase::Init(const char* cipher_type,
EVP_CIPHER_CTX_init(&ctx_);
const bool encrypt = (kind_ == kCipher);
- EVP_CipherInit_ex(&ctx_, cipher_, nullptr, nullptr, nullptr, encrypt);
+ EVP_CipherInit_ex(&ctx_, cipher, nullptr, nullptr, nullptr, encrypt);
if (!EVP_CIPHER_CTX_set_key_length(&ctx_, key_len)) {
EVP_CIPHER_CTX_cleanup(&ctx_);
return env()->ThrowError("Invalid key length");
@@ -3395,13 +3393,13 @@ void CipherBase::InitIv(const char* cipher_type,
int iv_len) {
HandleScope scope(env()->isolate());
- cipher_ = EVP_get_cipherbyname(cipher_type);
- if (cipher_ == nullptr) {
+ const EVP_CIPHER* const cipher = EVP_get_cipherbyname(cipher_type);
+ if (cipher == nullptr) {
return env()->ThrowError("Unknown cipher");
}
- const int expected_iv_len = EVP_CIPHER_iv_length(cipher_);
- const bool is_gcm_mode = (EVP_CIPH_GCM_MODE == EVP_CIPHER_mode(cipher_));
+ const int expected_iv_len = EVP_CIPHER_iv_length(cipher);
+ const bool is_gcm_mode = (EVP_CIPH_GCM_MODE == EVP_CIPHER_mode(cipher));
if (is_gcm_mode == false && iv_len != expected_iv_len) {
return env()->ThrowError("Invalid IV length");
@@ -3409,7 +3407,7 @@ void CipherBase::InitIv(const char* cipher_type,
EVP_CIPHER_CTX_init(&ctx_);
const bool encrypt = (kind_ == kCipher);
- EVP_CipherInit_ex(&ctx_, cipher_, nullptr, nullptr, nullptr, encrypt);
+ EVP_CipherInit_ex(&ctx_, cipher, nullptr, nullptr, nullptr, encrypt);
if (is_gcm_mode &&
!EVP_CIPHER_CTX_ctrl(&ctx_, EVP_CTRL_GCM_SET_IVLEN, iv_len, nullptr)) {
@@ -3455,50 +3453,30 @@ void CipherBase::InitIv(const FunctionCallbackInfo& args) {
bool CipherBase::IsAuthenticatedMode() const {
- // check if this cipher operates in an AEAD mode that we support.
- if (!cipher_)
- return false;
- int mode = EVP_CIPHER_mode(cipher_);
+ // Check if this cipher operates in an AEAD mode that we support.
+ CHECK_EQ(initialised_, true);
+ const EVP_CIPHER* const cipher = EVP_CIPHER_CTX_cipher(&ctx_);
+ int mode = EVP_CIPHER_mode(cipher);
return mode == EVP_CIPH_GCM_MODE;
}
-bool CipherBase::GetAuthTag(char** out, unsigned int* out_len) const {
- // only callable after Final and if encrypting.
- if (initialised_ || kind_ != kCipher || !auth_tag_)
- return false;
- *out_len = auth_tag_len_;
- *out = node::Malloc(auth_tag_len_);
- memcpy(*out, auth_tag_, auth_tag_len_);
- return true;
-}
-
-
void CipherBase::GetAuthTag(const FunctionCallbackInfo& args) {
Environment* env = Environment::GetCurrent(args);
CipherBase* cipher;
ASSIGN_OR_RETURN_UNWRAP(&cipher, args.Holder());
- char* out = nullptr;
- unsigned int out_len = 0;
-
- if (cipher->GetAuthTag(&out, &out_len)) {
- Local