Skip to content

Commit 8111ca2

Browse files
committed
src: const-ify variables in src/node_crypto*
No functional changes, just some code tightening. Clean up some style inconsistencies while we are here.
1 parent 71b3138 commit 8111ca2

3 files changed

Lines changed: 89 additions & 85 deletions

File tree

src/node_crypto.cc

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ void SecureContext::Init(const FunctionCallbackInfo<Value>& args) {
217217
OPENSSL_CONST SSL_METHOD *method = SSLv23_method();
218218

219219
if (args.Length() == 1 && args[0]->IsString()) {
220-
String::Utf8Value sslmethod(args[0]);
220+
const String::Utf8Value sslmethod(args[0]);
221221

222222
if (strcmp(*sslmethod, "SSLv2_method") == 0) {
223223
#ifndef OPENSSL_NO_SSL2
@@ -335,7 +335,7 @@ static BIO* LoadBIO (Handle<Value> v) {
335335
int r = -1;
336336

337337
if (v->IsString()) {
338-
String::Utf8Value s(v);
338+
const String::Utf8Value s(v);
339339
r = BIO_write(bio, *s, s.length());
340340
} else if (Buffer::HasInstance(v)) {
341341
char* buffer_data = Buffer::Data(v);
@@ -605,7 +605,7 @@ void SecureContext::SetCiphers(const FunctionCallbackInfo<Value>& args) {
605605
return ThrowTypeError("Bad parameter");
606606
}
607607

608-
String::Utf8Value ciphers(args[0]);
608+
const String::Utf8Value ciphers(args[0]);
609609
SSL_CTX_set_cipher_list(sc->ctx_, *ciphers);
610610
}
611611

@@ -633,8 +633,9 @@ void SecureContext::SetSessionIdContext(
633633
return ThrowTypeError("Bad parameter");
634634
}
635635

636-
String::Utf8Value sessionIdContext(args[0]);
637-
const unsigned char* sid_ctx = (const unsigned char*) *sessionIdContext;
636+
const String::Utf8Value sessionIdContext(args[0]);
637+
const unsigned char* sid_ctx =
638+
reinterpret_cast<const unsigned char*>(*sessionIdContext);
638639
unsigned int sid_ctx_len = sessionIdContext.length();
639640

640641
int r = SSL_CTX_set_session_id_context(sc->ctx_, sid_ctx, sid_ctx_len);
@@ -1295,7 +1296,7 @@ void Connection::New(const FunctionCallbackInfo<Value>& args) {
12951296
if (is_server) {
12961297
SSL_CTX_set_tlsext_servername_callback(sc->ctx_, SelectSNIContextCallback_);
12971298
} else {
1298-
String::Utf8Value servername(args[2]);
1299+
const String::Utf8Value servername(args[2]);
12991300
SSL_set_tlsext_host_name(p->ssl_, *servername);
13001301
}
13011302
#endif
@@ -2092,7 +2093,9 @@ void CipherBase::New(const FunctionCallbackInfo<Value>& args) {
20922093
}
20932094

20942095

2095-
void CipherBase::Init(char* cipher_type, char* key_buf, int key_buf_len) {
2096+
void CipherBase::Init(const char* cipher_type,
2097+
const char* key_buf,
2098+
int key_buf_len) {
20962099
HandleScope scope(node_isolate);
20972100

20982101
assert(cipher_ == NULL);
@@ -2107,7 +2110,7 @@ void CipherBase::Init(char* cipher_type, char* key_buf, int key_buf_len) {
21072110
int key_len = EVP_BytesToKey(cipher_,
21082111
EVP_md5(),
21092112
NULL,
2110-
reinterpret_cast<unsigned char*>(key_buf),
2113+
reinterpret_cast<const unsigned char*>(key_buf),
21112114
key_buf_len,
21122115
1,
21132116
key,
@@ -2140,17 +2143,17 @@ void CipherBase::Init(const FunctionCallbackInfo<Value>& args) {
21402143
return ThrowError("Must give cipher-type, key");
21412144
}
21422145

2143-
String::Utf8Value cipher_type(args[0]);
2144-
char* key_buf = Buffer::Data(args[1]);
2146+
const String::Utf8Value cipher_type(args[0]);
2147+
const char* key_buf = Buffer::Data(args[1]);
21452148
ssize_t key_buf_len = Buffer::Length(args[1]);
21462149
cipher->Init(*cipher_type, key_buf, key_buf_len);
21472150
}
21482151

21492152

2150-
void CipherBase::InitIv(char* cipher_type,
2151-
char* key,
2153+
void CipherBase::InitIv(const char* cipher_type,
2154+
const char* key,
21522155
int key_len,
2153-
char* iv,
2156+
const char* iv,
21542157
int iv_len) {
21552158
HandleScope scope(node_isolate);
21562159

@@ -2175,8 +2178,8 @@ void CipherBase::InitIv(char* cipher_type,
21752178
EVP_CipherInit_ex(&ctx_,
21762179
NULL,
21772180
NULL,
2178-
reinterpret_cast<unsigned char*>(key),
2179-
reinterpret_cast<unsigned char*>(iv),
2181+
reinterpret_cast<const unsigned char*>(key),
2182+
reinterpret_cast<const unsigned char*>(iv),
21802183
kind_ == kCipher);
21812184
initialised_ = true;
21822185
}
@@ -2194,16 +2197,16 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
21942197
ASSERT_IS_BUFFER(args[1]);
21952198
ASSERT_IS_BUFFER(args[2]);
21962199

2197-
String::Utf8Value cipher_type(args[0]);
2200+
const String::Utf8Value cipher_type(args[0]);
21982201
ssize_t key_len = Buffer::Length(args[1]);
2199-
char* key_buf = Buffer::Data(args[1]);
2202+
const char* key_buf = Buffer::Data(args[1]);
22002203
ssize_t iv_len = Buffer::Length(args[2]);
2201-
char* iv_buf = Buffer::Data(args[2]);
2204+
const char* iv_buf = Buffer::Data(args[2]);
22022205
cipher->InitIv(*cipher_type, key_buf, key_len, iv_buf, iv_len);
22032206
}
22042207

22052208

2206-
bool CipherBase::Update(char* data,
2209+
bool CipherBase::Update(const char* data,
22072210
int len,
22082211
unsigned char** out,
22092212
int* out_len) {
@@ -2213,7 +2216,7 @@ bool CipherBase::Update(char* data,
22132216
return EVP_CipherUpdate(&ctx_,
22142217
*out,
22152218
out_len,
2216-
reinterpret_cast<unsigned char*>(data),
2219+
reinterpret_cast<const unsigned char*>(data),
22172220
len);
22182221
}
22192222

@@ -2328,11 +2331,11 @@ void Hmac::New(const FunctionCallbackInfo<Value>& args) {
23282331
}
23292332

23302333

2331-
void Hmac::HmacInit(char* hashType, char* key, int key_len) {
2334+
void Hmac::HmacInit(const char* hash_type, const char* key, int key_len) {
23322335
HandleScope scope(node_isolate);
23332336

23342337
assert(md_ == NULL);
2335-
md_ = EVP_get_digestbyname(hashType);
2338+
md_ = EVP_get_digestbyname(hash_type);
23362339
if (md_ == NULL) {
23372340
return ThrowError("Unknown message digest");
23382341
}
@@ -2357,17 +2360,16 @@ void Hmac::HmacInit(const FunctionCallbackInfo<Value>& args) {
23572360

23582361
ASSERT_IS_BUFFER(args[1]);
23592362

2360-
String::Utf8Value hashType(args[0]);
2361-
2362-
char* buffer_data = Buffer::Data(args[1]);
2363+
const String::Utf8Value hash_type(args[0]);
2364+
const char* buffer_data = Buffer::Data(args[1]);
23632365
size_t buffer_length = Buffer::Length(args[1]);
2364-
hmac->HmacInit(*hashType, buffer_data, buffer_length);
2366+
hmac->HmacInit(*hash_type, buffer_data, buffer_length);
23652367
}
23662368

23672369

2368-
bool Hmac::HmacUpdate(char* data, int len) {
2370+
bool Hmac::HmacUpdate(const char* data, int len) {
23692371
if (!initialised_) return false;
2370-
HMAC_Update(&ctx_, reinterpret_cast<unsigned char*>(data), len);
2372+
HMAC_Update(&ctx_, reinterpret_cast<const unsigned char*>(data), len);
23712373
return true;
23722374
}
23732375

@@ -2460,10 +2462,10 @@ void Hash::New(const FunctionCallbackInfo<Value>& args) {
24602462
return ThrowError("Must give hashtype string as argument");
24612463
}
24622464

2463-
String::Utf8Value hashType(args[0]);
2465+
const String::Utf8Value hash_type(args[0]);
24642466

24652467
Hash* hash = new Hash();
2466-
if (!hash->HashInit(*hashType)) {
2468+
if (!hash->HashInit(*hash_type)) {
24672469
delete hash;
24682470
return ThrowError("Digest method not supported");
24692471
}
@@ -2472,9 +2474,9 @@ void Hash::New(const FunctionCallbackInfo<Value>& args) {
24722474
}
24732475

24742476

2475-
bool Hash::HashInit(const char* hashType) {
2477+
bool Hash::HashInit(const char* hash_type) {
24762478
assert(md_ == NULL);
2477-
md_ = EVP_get_digestbyname(hashType);
2479+
md_ = EVP_get_digestbyname(hash_type);
24782480
if (md_ == NULL) return false;
24792481
EVP_MD_CTX_init(&mdctx_);
24802482
EVP_DigestInit_ex(&mdctx_, md_, NULL);
@@ -2483,7 +2485,7 @@ bool Hash::HashInit(const char* hashType) {
24832485
}
24842486

24852487

2486-
bool Hash::HashUpdate(char* data, int len) {
2488+
bool Hash::HashUpdate(const char* data, int len) {
24872489
if (!initialised_) return false;
24882490
EVP_DigestUpdate(&mdctx_, data, len);
24892491
return true;
@@ -2593,12 +2595,12 @@ void Sign::SignInit(const FunctionCallbackInfo<Value>& args) {
25932595
return ThrowError("Must give signtype string as argument");
25942596
}
25952597

2596-
String::Utf8Value sign_type(args[0]);
2598+
const String::Utf8Value sign_type(args[0]);
25972599
sign->SignInit(*sign_type);
25982600
}
25992601

26002602

2601-
bool Sign::SignUpdate(char* data, int len) {
2603+
bool Sign::SignUpdate(const char* data, int len) {
26022604
if (!initialised_) return false;
26032605
EVP_SignUpdate(&mdctx_, data, len);
26042606
return true;
@@ -2638,7 +2640,7 @@ void Sign::SignUpdate(const FunctionCallbackInfo<Value>& args) {
26382640

26392641
bool Sign::SignFinal(unsigned char** md_value,
26402642
unsigned int *md_len,
2641-
char* key_pem,
2643+
const char* key_pem,
26422644
int key_pem_len) {
26432645
if (!initialised_) return false;
26442646

@@ -2739,12 +2741,12 @@ void Verify::VerifyInit(const FunctionCallbackInfo<Value>& args) {
27392741
return ThrowError("Must give verifytype string as argument");
27402742
}
27412743

2742-
String::Utf8Value verify_type(args[0]);
2744+
const String::Utf8Value verify_type(args[0]);
27432745
verify->VerifyInit(*verify_type);
27442746
}
27452747

27462748

2747-
bool Verify::VerifyUpdate(char* data, int len) {
2749+
bool Verify::VerifyUpdate(const char* data, int len) {
27482750
if (!initialised_) return false;
27492751
EVP_VerifyUpdate(&mdctx_, data, len);
27502752
return true;
@@ -2782,9 +2784,9 @@ void Verify::VerifyUpdate(const FunctionCallbackInfo<Value>& args) {
27822784
}
27832785

27842786

2785-
bool Verify::VerifyFinal(char* key_pem,
2787+
bool Verify::VerifyFinal(const char* key_pem,
27862788
int key_pem_len,
2787-
unsigned char* sig,
2789+
const char* sig,
27882790
int siglen) {
27892791
HandleScope scope(node_isolate);
27902792

@@ -2834,7 +2836,10 @@ bool Verify::VerifyFinal(char* key_pem,
28342836
}
28352837

28362838
fatal = false;
2837-
r = EVP_VerifyFinal(&mdctx_, sig, siglen, pkey);
2839+
r = EVP_VerifyFinal(&mdctx_,
2840+
reinterpret_cast<const unsigned char*>(sig),
2841+
siglen,
2842+
pkey);
28382843

28392844
exit:
28402845
if (pkey != NULL)
@@ -2876,14 +2881,13 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {
28762881
ssize_t hlen = StringBytes::Size(args[1], encoding);
28772882

28782883
// only copy if we need to, because it's a string.
2879-
unsigned char* hbuf;
2884+
char* hbuf;
28802885
if (args[1]->IsString()) {
2881-
hbuf = new unsigned char[hlen];
2882-
ssize_t hwritten = StringBytes::Write(
2883-
reinterpret_cast<char*>(hbuf), hlen, args[1], encoding);
2886+
hbuf = new char[hlen];
2887+
ssize_t hwritten = StringBytes::Write(hbuf, hlen, args[1], encoding);
28842888
assert(hwritten == hlen);
28852889
} else {
2886-
hbuf = reinterpret_cast<unsigned char*>(Buffer::Data(args[1]));
2890+
hbuf = Buffer::Data(args[1]);
28872891
}
28882892

28892893
bool rc = verify->VerifyFinal(kbuf, klen, hbuf, hlen);
@@ -2936,9 +2940,9 @@ bool DiffieHellman::Init(int primeLength) {
29362940
}
29372941

29382942

2939-
bool DiffieHellman::Init(unsigned char* p, int p_len) {
2943+
bool DiffieHellman::Init(const char* p, int p_len) {
29402944
dh = DH_new();
2941-
dh->p = BN_bin2bn(p, p_len, 0);
2945+
dh->p = BN_bin2bn(reinterpret_cast<const unsigned char*>(p), p_len, 0);
29422946
dh->g = BN_new();
29432947
if (!BN_set_word(dh->g, 2)) return false;
29442948
bool result = VerifyContext();
@@ -2948,13 +2952,10 @@ bool DiffieHellman::Init(unsigned char* p, int p_len) {
29482952
}
29492953

29502954

2951-
bool DiffieHellman::Init(unsigned char* p,
2952-
int p_len,
2953-
unsigned char* g,
2954-
int g_len) {
2955+
bool DiffieHellman::Init(const char* p, int p_len, const char* g, int g_len) {
29552956
dh = DH_new();
2956-
dh->p = BN_bin2bn(p, p_len, 0);
2957-
dh->g = BN_bin2bn(g, g_len, 0);
2957+
dh->p = BN_bin2bn(reinterpret_cast<const unsigned char*>(p), p_len, 0);
2958+
dh->g = BN_bin2bn(reinterpret_cast<const unsigned char*>(g), g_len, 0);
29582959
initialised_ = true;
29592960
return true;
29602961
}
@@ -2970,11 +2971,11 @@ void DiffieHellman::DiffieHellmanGroup(
29702971
return ThrowError("No group name given");
29712972
}
29722973

2973-
String::Utf8Value group_name(args[0]);
2974+
const String::Utf8Value group_name(args[0]);
29742975

29752976
modp_group* it = modp_groups;
29762977

2977-
while(it->name != NULL) {
2978+
while (it->name != NULL) {
29782979
if (!strcasecmp(*group_name, it->name))
29792980
break;
29802981
it++;
@@ -3003,9 +3004,8 @@ void DiffieHellman::New(const FunctionCallbackInfo<Value>& args) {
30033004
if (args[0]->IsInt32()) {
30043005
initialized = diffieHellman->Init(args[0]->Int32Value());
30053006
} else {
3006-
initialized = diffieHellman->Init(
3007-
reinterpret_cast<unsigned char*>(Buffer::Data(args[0])),
3008-
Buffer::Length(args[0]));
3007+
initialized = diffieHellman->Init(Buffer::Data(args[0]),
3008+
Buffer::Length(args[0]));
30093009
}
30103010
}
30113011

0 commit comments

Comments
 (0)