-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathextcrypto.cc
167 lines (118 loc) · 3.72 KB
/
extcrypto.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
// extcrypto.cc - some openssl wrappers to extend RSA support
#include <node.h>
#include <nan.h>
#include <stdint.h>
#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/bio.h>
#include <openssl/bn.h>
namespace extcrypto {
using v8::Exception;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Null;
using v8::Object;
using v8::String;
using v8::Value;
void async_ret(Isolate* isolate, Local<Function> cb, Local<String> rval) {
const unsigned argc = 2;
Local<Value> argv[argc] = { Null(isolate), rval };
Nan::Call(Nan::Callback(cb), argc, argv);
}
void async_eret(Isolate* isolate, Local<Function> cb, Local<String> rval) {
const uint64_t argc = 1;
Local<Value> argv[argc] = { Exception::Error(rval) };
Nan::Call(Nan::Callback(cb), argc, argv);
}
void keygen(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Function> cb;
Local<String> rval;
bool async = args.Length() > 0;
if (async) { cb = Local<Function>::Cast(args[0]); }
// begin keygen
BIGNUM* exp = BN_new();
BN_set_word(exp, RSA_F4); // 65537
RSA* rsa = RSA_new();
int64_t kg = RSA_generate_key_ex(rsa, 2048, exp, NULL);
if (!kg) {
rval = Nan::New("Unable to generate key").ToLocalChecked();
if (!async) {
args.GetReturnValue().Set(rval);
return;
}
return async_eret(isolate, cb, rval);
}
BIO* bio = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, NULL);
uint64_t kl = BIO_pending(bio);
char* key = (char *) calloc(kl + 1, 1);
if (!key || (kl == UINT64_MAX)) {
free(key); // in case compiler dependent behavior for calloc after overflow returns a non-null pointer
rval = Nan::New("Unable to generate key").ToLocalChecked();
if (!async) {
args.GetReturnValue().Set(rval);
return;
}
return async_eret(isolate, cb, rval);
}
BIO_read(bio, key, kl);
BIO_vfree(bio);
RSA_free(rsa);
BN_free(exp);
// return
rval = Nan::New(key).ToLocalChecked();
free(key);
if (!async) {
args.GetReturnValue().Set(rval);
return;
}
async_ret(isolate, cb, rval);
}
void extract(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<String> pem = Local<String>::Cast(args[0]);
Local<Function> cb;
Local<String> rval;
// begin extraction
bool async = args.Length() > 1;
if (async) { cb = Local<Function>::Cast(args[1]); }
Nan::Utf8String ipem(pem);
char* skey(*ipem);
BIO* bio = BIO_new(BIO_s_mem());
RSA* rsa = RSA_new();
BIO_write(bio, skey, strlen(skey));
PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL);
PEM_write_bio_RSAPublicKey(bio, rsa);
uint64_t kl = BIO_pending(bio);
char* pkey = (char *) calloc(kl + 1, 1);
if (!pkey || (kl == UINT64_MAX)) {
free(pkey); // in case compiler dependent behavior for calloc after overflow returns a non-null pointer
rval = Nan::New("Unable to extract key").ToLocalChecked();
if (!async) {
args.GetReturnValue().Set(rval);
return;
}
return async_eret(isolate, cb, rval);
}
BIO_read(bio, pkey, kl);
BIO_vfree(bio);
RSA_free(rsa);
// return
rval = Nan::New(pkey).ToLocalChecked();
free(pkey);
if (!async) {
args.GetReturnValue().Set(rval);
return;
}
async_ret(isolate, cb, rval);
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "keygen", keygen);
NODE_SET_METHOD(exports, "extract", extract);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, init)
}