|
@ -42,6 +42,11 @@ |
|
|
return ThrowException(Exception::TypeError(String::New("Not a string or buffer"))); \ |
|
|
return ThrowException(Exception::TypeError(String::New("Not a string or buffer"))); \ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static const char *RSA_PUB_KEY_PFX = "-----BEGIN RSA PUBLIC KEY-----"; |
|
|
|
|
|
static const char *DSA_PUB_KEY_PFX = "-----BEGIN PUBLIC KEY-----"; |
|
|
|
|
|
static const int RSA_PUB_KEY_PFX_LEN = strlen(RSA_PUB_KEY_PFX); |
|
|
|
|
|
static const int DSA_PUB_KEY_PFX_LEN = strlen(DSA_PUB_KEY_PFX); |
|
|
|
|
|
|
|
|
namespace node { |
|
|
namespace node { |
|
|
namespace crypto { |
|
|
namespace crypto { |
|
|
|
|
|
|
|
@ -1497,7 +1502,7 @@ class Cipher : public ObjectWrap { |
|
|
|
|
|
|
|
|
static Handle<Value> CipherInitIv(const Arguments& args) { |
|
|
static Handle<Value> CipherInitIv(const Arguments& args) { |
|
|
Cipher *cipher = ObjectWrap::Unwrap<Cipher>(args.This()); |
|
|
Cipher *cipher = ObjectWrap::Unwrap<Cipher>(args.This()); |
|
|
|
|
|
|
|
|
HandleScope scope; |
|
|
HandleScope scope; |
|
|
|
|
|
|
|
|
cipher->incomplete_base64=NULL; |
|
|
cipher->incomplete_base64=NULL; |
|
@ -1532,7 +1537,7 @@ class Cipher : public ObjectWrap { |
|
|
assert(iv_written == iv_len); |
|
|
assert(iv_written == iv_len); |
|
|
|
|
|
|
|
|
String::Utf8Value cipherType(args[0]->ToString()); |
|
|
String::Utf8Value cipherType(args[0]->ToString()); |
|
|
|
|
|
|
|
|
bool r = cipher->CipherInitIv(*cipherType, key_buf,key_len,iv_buf,iv_len); |
|
|
bool r = cipher->CipherInitIv(*cipherType, key_buf,key_len,iv_buf,iv_len); |
|
|
|
|
|
|
|
|
delete [] key_buf; |
|
|
delete [] key_buf; |
|
@ -1826,7 +1831,7 @@ class Decipher : public ObjectWrap { |
|
|
|
|
|
|
|
|
static Handle<Value> DecipherInit(const Arguments& args) { |
|
|
static Handle<Value> DecipherInit(const Arguments& args) { |
|
|
Decipher *cipher = ObjectWrap::Unwrap<Decipher>(args.This()); |
|
|
Decipher *cipher = ObjectWrap::Unwrap<Decipher>(args.This()); |
|
|
|
|
|
|
|
|
HandleScope scope; |
|
|
HandleScope scope; |
|
|
|
|
|
|
|
|
cipher->incomplete_utf8=NULL; |
|
|
cipher->incomplete_utf8=NULL; |
|
@ -1850,7 +1855,7 @@ class Decipher : public ObjectWrap { |
|
|
assert(key_written == key_len); |
|
|
assert(key_written == key_len); |
|
|
|
|
|
|
|
|
String::Utf8Value cipherType(args[0]->ToString()); |
|
|
String::Utf8Value cipherType(args[0]->ToString()); |
|
|
|
|
|
|
|
|
bool r = cipher->DecipherInit(*cipherType, key_buf,key_len); |
|
|
bool r = cipher->DecipherInit(*cipherType, key_buf,key_len); |
|
|
|
|
|
|
|
|
delete [] key_buf; |
|
|
delete [] key_buf; |
|
@ -1864,7 +1869,7 @@ class Decipher : public ObjectWrap { |
|
|
|
|
|
|
|
|
static Handle<Value> DecipherInitIv(const Arguments& args) { |
|
|
static Handle<Value> DecipherInitIv(const Arguments& args) { |
|
|
Decipher *cipher = ObjectWrap::Unwrap<Decipher>(args.This()); |
|
|
Decipher *cipher = ObjectWrap::Unwrap<Decipher>(args.This()); |
|
|
|
|
|
|
|
|
HandleScope scope; |
|
|
HandleScope scope; |
|
|
|
|
|
|
|
|
cipher->incomplete_utf8=NULL; |
|
|
cipher->incomplete_utf8=NULL; |
|
@ -1900,7 +1905,7 @@ class Decipher : public ObjectWrap { |
|
|
assert(iv_written == iv_len); |
|
|
assert(iv_written == iv_len); |
|
|
|
|
|
|
|
|
String::Utf8Value cipherType(args[0]->ToString()); |
|
|
String::Utf8Value cipherType(args[0]->ToString()); |
|
|
|
|
|
|
|
|
bool r = cipher->DecipherInitIv(*cipherType, key_buf,key_len,iv_buf,iv_len); |
|
|
bool r = cipher->DecipherInitIv(*cipherType, key_buf,key_len,iv_buf,iv_len); |
|
|
|
|
|
|
|
|
delete [] key_buf; |
|
|
delete [] key_buf; |
|
@ -2265,7 +2270,7 @@ class Hmac : public ObjectWrap { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int r; |
|
|
int r; |
|
|
|
|
|
|
|
|
if( Buffer::HasInstance(args[0])) { |
|
|
if( Buffer::HasInstance(args[0])) { |
|
|
Local<Object> buffer_obj = args[0]->ToObject(); |
|
|
Local<Object> buffer_obj = args[0]->ToObject(); |
|
|
char *buffer_data = Buffer::Data(buffer_obj); |
|
|
char *buffer_data = Buffer::Data(buffer_obj); |
|
@ -2756,29 +2761,58 @@ class Verify : public ObjectWrap { |
|
|
int VerifyFinal(char* key_pem, int key_pemLen, unsigned char* sig, int siglen) { |
|
|
int VerifyFinal(char* key_pem, int key_pemLen, unsigned char* sig, int siglen) { |
|
|
if (!initialised_) return 0; |
|
|
if (!initialised_) return 0; |
|
|
|
|
|
|
|
|
|
|
|
EVP_PKEY* pkey = NULL; |
|
|
BIO *bp = NULL; |
|
|
BIO *bp = NULL; |
|
|
EVP_PKEY* pkey; |
|
|
X509 *x509 = NULL; |
|
|
X509 *x509; |
|
|
int r = 0; |
|
|
|
|
|
|
|
|
bp = BIO_new(BIO_s_mem()); |
|
|
bp = BIO_new(BIO_s_mem()); |
|
|
if(!BIO_write(bp, key_pem, key_pemLen)) return 0; |
|
|
if (bp == NULL) { |
|
|
|
|
|
ERR_print_errors_fp(stderr); |
|
|
x509 = PEM_read_bio_X509(bp, NULL, NULL, NULL ); |
|
|
return 0; |
|
|
if (x509==NULL) return 0; |
|
|
} |
|
|
|
|
|
if(!BIO_write(bp, key_pem, key_pemLen)) { |
|
|
|
|
|
ERR_print_errors_fp(stderr); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
pkey=X509_get_pubkey(x509); |
|
|
// Check if this is an RSA or DSA "raw" public key before trying
|
|
|
if (pkey==NULL) return 0; |
|
|
// X.509
|
|
|
|
|
|
if (strncmp(key_pem, RSA_PUB_KEY_PFX, RSA_PUB_KEY_PFX_LEN) == 0 || |
|
|
|
|
|
strncmp(key_pem, DSA_PUB_KEY_PFX, DSA_PUB_KEY_PFX_LEN) == 0) { |
|
|
|
|
|
pkey = PEM_read_bio_PUBKEY(bp, NULL, NULL, NULL); |
|
|
|
|
|
if (pkey == NULL) { |
|
|
|
|
|
ERR_print_errors_fp(stderr); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
// X.509 fallback
|
|
|
|
|
|
x509 = PEM_read_bio_X509(bp, NULL, NULL, NULL); |
|
|
|
|
|
if (x509 == NULL) { |
|
|
|
|
|
ERR_print_errors_fp(stderr); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
int r = EVP_VerifyFinal(&mdctx, sig, siglen, pkey); |
|
|
pkey = X509_get_pubkey(x509); |
|
|
EVP_PKEY_free (pkey); |
|
|
if (pkey == NULL) { |
|
|
|
|
|
ERR_print_errors_fp(stderr); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (r != 1) { |
|
|
r = EVP_VerifyFinal(&mdctx, sig, siglen, pkey); |
|
|
|
|
|
if (r != 1) |
|
|
ERR_print_errors_fp (stderr); |
|
|
ERR_print_errors_fp (stderr); |
|
|
} |
|
|
|
|
|
X509_free(x509); |
|
|
if(pkey != NULL) |
|
|
BIO_free(bp); |
|
|
EVP_PKEY_free (pkey); |
|
|
|
|
|
if (x509 != NULL) |
|
|
|
|
|
X509_free(x509); |
|
|
|
|
|
if (bp != NULL) |
|
|
|
|
|
BIO_free(bp); |
|
|
EVP_MD_CTX_cleanup(&mdctx); |
|
|
EVP_MD_CTX_cleanup(&mdctx); |
|
|
initialised_ = false; |
|
|
initialised_ = false; |
|
|
|
|
|
|
|
|
return r; |
|
|
return r; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|