forked from chrisa/node-rsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_rsa.cc
336 lines (270 loc) · 9.41 KB
/
node_rsa.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "node_rsa.h"
#include <v8.h>
#include <node.h>
#include <node_buffer.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
# define OPENSSL_CONST const
#else
# define OPENSSL_CONST
#endif
namespace node {
using namespace v8;
// hex_encode, hex_decode, base64, unbase64 nicked from node_crypto.cc
void hex_encode(unsigned char *md_value, int md_len, char** md_hexdigest,
int* md_hex_len) {
*md_hex_len = (2*(md_len));
*md_hexdigest = (char *) malloc(*md_hex_len + 1);
for (int i = 0; i < md_len; i++) {
sprintf((char *)(*md_hexdigest + (i*2)), "%02x", md_value[i]);
}
}
#define hex2i(c) ((c) <= '9' ? ((c) - '0') : (c) <= 'Z' ? ((c) - 'A' + 10) \
: ((c) - 'a' + 10))
void hex_decode(unsigned char *input, int length, char** buf64,
int* buf64_len) {
*buf64_len = (length/2);
*buf64 = (char*) malloc(length/2 + 1);
char *b = *buf64;
for(int i = 0; i < length-1; i+=2) {
b[i/2] = (hex2i(input[i])<<4) | (hex2i(input[i+1]));
}
}
void base64(unsigned char *input, int length, char** buf64, int* buf64_len)
{
BIO *bmem, *b64;
BUF_MEM *bptr;
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
BIO_write(b64, input, length);
(void)BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
*buf64_len = bptr->length;
*buf64 = (char *)malloc(*buf64_len+1);
memcpy(*buf64, bptr->data, bptr->length);
char* b = *buf64;
b[bptr->length] = 0;
BIO_free_all(b64);
}
void unbase64(unsigned char *input, int length, char** buffer, int* buffer_len)
{
BIO *b64, *bmem;
*buffer = (char *)malloc(length);
memset(*buffer, 0, length);
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bmem = BIO_new_mem_buf(input, length);
bmem = BIO_push(b64, bmem);
*buffer_len = BIO_read(bmem, *buffer, length);
BIO_free_all(bmem);
}
void RsaKeypair::Initialize(Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(RsaKeypair::New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(String::NewSymbol("RsaKeypair"));
NODE_SET_PROTOTYPE_METHOD(t, "setPublicKey",
RsaKeypair::SetPublicKey);
NODE_SET_PROTOTYPE_METHOD(t, "setPrivateKey",
RsaKeypair::SetPrivateKey);
NODE_SET_PROTOTYPE_METHOD(t, "encrypt",
RsaKeypair::Encrypt);
NODE_SET_PROTOTYPE_METHOD(t, "decrypt",
RsaKeypair::Decrypt);
target->Set(String::NewSymbol("RsaKeypair"), t->GetFunction());
}
Handle<Value> RsaKeypair::New(const Arguments& args) {
HandleScope scope;
RsaKeypair *p = new RsaKeypair();
p->Wrap(args.Holder());
p->privateKey = NULL;
p->publicKey = NULL;
return args.This();
}
Handle<Value> RsaKeypair::SetPublicKey(const Arguments& args) {
HandleScope scope;
RsaKeypair *kp = ObjectWrap::Unwrap<RsaKeypair>(args.Holder());
if (args.Length() != 1 ||
!args[0]->IsString()) {
return ThrowException(Exception::TypeError(
String::New("Bad parameter")));
}
String::Utf8Value pubKey(args[0]->ToString());
BIO *bp = NULL;
RSA *key = NULL;
bp = BIO_new(BIO_s_mem());
if (!BIO_write(bp, *pubKey, strlen(*pubKey)))
return False();
key = PEM_read_bio_RSA_PUBKEY(bp, NULL, NULL, NULL);
if (key == NULL)
return False();
kp->publicKey = key;
BIO_free(bp);
return True();
}
Handle<Value> RsaKeypair::SetPrivateKey(const Arguments& args) {
HandleScope scope;
RsaKeypair *kp = ObjectWrap::Unwrap<RsaKeypair>(args.Holder());
if (args.Length() == 2 &&
(!args[0]->IsString() || !args[1]->IsString())) {
return ThrowException(Exception::TypeError(
String::New("Bad parameter")));
}
if (args.Length() == 1 &&
(!args[0]->IsString())) {
return ThrowException(Exception::TypeError(
String::New("Bad parameter")));
}
BIO *bp = NULL;
String::Utf8Value privKey(args[0]->ToString());
bp = BIO_new(BIO_s_mem());
if (!BIO_write(bp, *privKey, strlen(*privKey)))
return False();
RSA *key;
if (args.Length() == 2) {
String::Utf8Value passphrase(args[1]->ToString());
key = PEM_read_bio_RSAPrivateKey(bp, NULL, 0, *passphrase);
}
else {
key = PEM_read_bio_RSAPrivateKey(bp, NULL, 0, NULL);
}
if (key == NULL) {
return False();
}
kp->privateKey = key;
BIO_free(bp);
return True();
}
Handle<Value> RsaKeypair::Encrypt(const Arguments& args) {
HandleScope scope;
RsaKeypair *kp = ObjectWrap::Unwrap<RsaKeypair>(args.Holder());
if (kp->publicKey == NULL) {
Local<Value> exception = Exception::Error(String::New("Can't encrypt, no public key"));
return ThrowException(exception);
}
enum encoding enc = ParseEncoding(args[1]);
ssize_t len = DecodeBytes(args[0], enc);
if (len < 0) {
Local<Value> exception = Exception::TypeError(String::New("Bad argument"));
return ThrowException(exception);
}
// check per RSA_public_encrypt(3) when using OAEP
if (len >= RSA_size(kp->publicKey) - 41) {
Local<Value> exception = Exception::TypeError(String::New("Bad argument (too long for key size)"));
return ThrowException(exception);
}
unsigned char* buf = new unsigned char[len];
ssize_t written = DecodeWrite((char *)buf, len, args[0], enc);
assert(written == len);
int out_len = RSA_size(kp->publicKey);
unsigned char *out = (unsigned char*)malloc(out_len);
int r = RSA_public_encrypt(len, buf, out, kp->publicKey, RSA_PKCS1_OAEP_PADDING);
if (r < 0) {
char *err = ERR_error_string(ERR_get_error(), NULL);
Local<String> full_err = String::Concat(String::New("RSA encrypt: "), String::New(err));
Local<Value> exception = Exception::Error(full_err);
return ThrowException(exception);
}
Local<Value> outString;
if (out_len == 0) {
outString = String::New("");
}
else {
if (args.Length() <= 2 || !args[2]->IsString()) {
outString = Encode(out, out_len, BINARY);
} else {
char* out_hexdigest;
int out_hex_len;
String::Utf8Value encoding(args[2]->ToString());
if (strcasecmp(*encoding, "hex") == 0) {
hex_encode(out, out_len, &out_hexdigest, &out_hex_len);
outString = Encode(out_hexdigest, out_hex_len, BINARY);
free(out_hexdigest);
} else if (strcasecmp(*encoding, "base64") == 0) {
base64(out, out_len, &out_hexdigest, &out_hex_len);
outString = Encode(out_hexdigest, out_hex_len, BINARY);
free(out_hexdigest);
} else if (strcasecmp(*encoding, "binary") == 0) {
outString = Encode(out, out_len, BINARY);
} else {
outString = String::New("");
Local<Value> exception = Exception::Error(String::New("RsaKeypair.encrypt encoding "
"can be binary, base64 or hex"));
return ThrowException(exception);
}
}
}
if (out) free(out);
return scope.Close(outString);
}
Handle<Value> RsaKeypair::Decrypt(const Arguments& args) {
HandleScope scope;
RsaKeypair *kp = ObjectWrap::Unwrap<RsaKeypair>(args.Holder());
if (kp->privateKey == NULL) {
Local<Value> exception = Exception::Error(String::New("Can't decrypt, no private key"));
return ThrowException(exception);
}
ssize_t len = DecodeBytes(args[0], BINARY);
unsigned char* buf = new unsigned char[len];
(void)DecodeWrite((char *)buf, len, args[0], BINARY);
unsigned char* ciphertext;
int ciphertext_len;
if (args.Length() <= 1 || !args[1]->IsString()) {
// Binary - do nothing
} else {
String::Utf8Value encoding(args[1]->ToString());
if (strcasecmp(*encoding, "hex") == 0) {
hex_decode((unsigned char*)buf, len, (char **)&ciphertext, &ciphertext_len);
free(buf);
buf = ciphertext;
len = ciphertext_len;
} else if (strcasecmp(*encoding, "base64") == 0) {
unbase64((unsigned char*)buf, len, (char **)&ciphertext, &ciphertext_len);
free(buf);
buf = ciphertext;
len = ciphertext_len;
} else if (strcasecmp(*encoding, "binary") == 0) {
// Binary - do nothing
} else {
Local<Value> exception = Exception::Error(String::New("RsaKeypair.decrypt encoding "
"can be binary, base64 or hex"));
return ThrowException(exception);
}
}
// XXX is this check unnecessary? is it just len <= keysize?
// check per RSA_public_encrypt(3) when using OAEP
//if (len > RSA_size(kp->privateKey) - 41) {
// Local<Value> exception = Exception::Error(String::New("Bad argument (too long for key size)"));
// return ThrowException(exception);
//}
int out_len = RSA_size(kp->privateKey);
unsigned char *out = (unsigned char*)malloc(out_len);
out_len = RSA_private_decrypt(len, buf, out, kp->privateKey, RSA_PKCS1_OAEP_PADDING);
if (out_len < 0) {
char *err = ERR_error_string(ERR_get_error(), NULL);
Local<String> full_err = String::Concat(String::New("RSA decrypt: "), String::New(err));
Local<Value> exception = Exception::Error(full_err);
return ThrowException(exception);
}
Local<Value> outString;
if (out_len == 0) {
outString = String::New("");
} else if (args.Length() <= 2 || !args[2]->IsString()) {
outString = Encode(out, out_len, BINARY);
} else {
enum encoding enc = ParseEncoding(args[2]);
outString = Encode(out, out_len, enc);
}
if (out) free(out);
free(buf);
return scope.Close(outString);
}
extern "C" void
init(Handle<Object> target) {
RsaKeypair::Initialize(target);
}
} // namespace node