aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2011-06-15 14:43:37 +0200
committerRyan Dahl <ry@tinyclouds.org>2011-06-15 14:43:37 +0200
commit3d7d994ffce2e79d516f76d7384a411b21021142 (patch)
treebc557287ad6ea87574a339f59ea9f7c5bccfca9a /src
parent86214c9f164c152b57b140d33deefa48eff50920 (diff)
parent88552c51aead1520a0d20d42b4af39520baab5d1 (diff)
downloadandroid-node-v8-3d7d994ffce2e79d516f76d7384a411b21021142.tar.gz
android-node-v8-3d7d994ffce2e79d516f76d7384a411b21021142.tar.bz2
android-node-v8-3d7d994ffce2e79d516f76d7384a411b21021142.zip
Merge branch 'v0.4'
Conflicts: doc/api/modules.markdown test/simple/test-crypto.js
Diffstat (limited to 'src')
-rw-r--r--src/node_crypto.cc76
1 files changed, 55 insertions, 21 deletions
diff --git a/src/node_crypto.cc b/src/node_crypto.cc
index 13749a83fe..20a475e35e 100644
--- a/src/node_crypto.cc
+++ b/src/node_crypto.cc
@@ -42,6 +42,11 @@
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 crypto {
@@ -1647,7 +1652,7 @@ class Cipher : public ObjectWrap {
static Handle<Value> CipherInitIv(const Arguments& args) {
Cipher *cipher = ObjectWrap::Unwrap<Cipher>(args.This());
-
+
HandleScope scope;
cipher->incomplete_base64=NULL;
@@ -1682,7 +1687,7 @@ class Cipher : public ObjectWrap {
assert(iv_written == iv_len);
String::Utf8Value cipherType(args[0]->ToString());
-
+
bool r = cipher->CipherInitIv(*cipherType, key_buf,key_len,iv_buf,iv_len);
delete [] key_buf;
@@ -1976,7 +1981,7 @@ class Decipher : public ObjectWrap {
static Handle<Value> DecipherInit(const Arguments& args) {
Decipher *cipher = ObjectWrap::Unwrap<Decipher>(args.This());
-
+
HandleScope scope;
cipher->incomplete_utf8=NULL;
@@ -2000,7 +2005,7 @@ class Decipher : public ObjectWrap {
assert(key_written == key_len);
String::Utf8Value cipherType(args[0]->ToString());
-
+
bool r = cipher->DecipherInit(*cipherType, key_buf,key_len);
delete [] key_buf;
@@ -2014,7 +2019,7 @@ class Decipher : public ObjectWrap {
static Handle<Value> DecipherInitIv(const Arguments& args) {
Decipher *cipher = ObjectWrap::Unwrap<Decipher>(args.This());
-
+
HandleScope scope;
cipher->incomplete_utf8=NULL;
@@ -2050,7 +2055,7 @@ class Decipher : public ObjectWrap {
assert(iv_written == iv_len);
String::Utf8Value cipherType(args[0]->ToString());
-
+
bool r = cipher->DecipherInitIv(*cipherType, key_buf,key_len,iv_buf,iv_len);
delete [] key_buf;
@@ -2415,7 +2420,7 @@ class Hmac : public ObjectWrap {
}
int r;
-
+
if( Buffer::HasInstance(args[0])) {
Local<Object> buffer_obj = args[0]->ToObject();
char *buffer_data = Buffer::Data(buffer_obj);
@@ -2906,29 +2911,58 @@ class Verify : public ObjectWrap {
int VerifyFinal(char* key_pem, int key_pemLen, unsigned char* sig, int siglen) {
if (!initialised_) return 0;
+ EVP_PKEY* pkey = NULL;
BIO *bp = NULL;
- EVP_PKEY* pkey;
- X509 *x509;
+ X509 *x509 = NULL;
+ int r = 0;
bp = BIO_new(BIO_s_mem());
- if(!BIO_write(bp, key_pem, key_pemLen)) return 0;
-
- x509 = PEM_read_bio_X509(bp, NULL, NULL, NULL );
- if (x509==NULL) return 0;
+ if (bp == NULL) {
+ ERR_print_errors_fp(stderr);
+ return 0;
+ }
+ if(!BIO_write(bp, key_pem, key_pemLen)) {
+ ERR_print_errors_fp(stderr);
+ return 0;
+ }
- pkey=X509_get_pubkey(x509);
- if (pkey==NULL) return 0;
+ // Check if this is an RSA or DSA "raw" public key before trying
+ // 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);
- EVP_PKEY_free (pkey);
+ pkey = X509_get_pubkey(x509);
+ 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);
- }
- X509_free(x509);
- BIO_free(bp);
+
+ if(pkey != NULL)
+ EVP_PKEY_free (pkey);
+ if (x509 != NULL)
+ X509_free(x509);
+ if (bp != NULL)
+ BIO_free(bp);
EVP_MD_CTX_cleanup(&mdctx);
initialised_ = false;
+
return r;
}