-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
387 lines (347 loc) · 10.7 KB
/
main.c
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* Copyright (C) 2019 Ripjar Limited
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* or see see <https://www.gnu.org/licenses/>.
*/
#include <node_api.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/bio.h>
#include <openssl/x509.h>
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
struct rsa_st {
/*
* #legacy
* The first field is used to pickup errors where this is passed
* instead of an EVP_PKEY. It is always zero.
* THIS MUST REMAIN THE FIRST FIELD.
*/
int dummy_zero;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
OSSL_LIB_CTX *libctx;
#endif
int32_t version;
const RSA_METHOD *meth;
/* functional reference if 'meth' is ENGINE-provided */
ENGINE *engine;
BIGNUM *n;
BIGNUM *e;
BIGNUM *d;
BIGNUM *p;
BIGNUM *q;
BIGNUM *dmp1;
BIGNUM *dmq1;
BIGNUM *iqmp;
};
#endif
#include <string.h>
static napi_value
x509_cert_pub_key (napi_env env, napi_callback_info info)
{
size_t argc = 1, size;
napi_value argv[argc];
char *buf;
napi_status status = napi_ok;
BIO *bio;
X509 *x509;
EVP_PKEY *evp_pubkey;
RSA *public_key;
char *n_hex, *e_hex;
napi_value n_val, e_val;
napi_value obj;
if (napi_get_cb_info (env, info, &argc, argv, NULL, NULL) != napi_ok)
{
napi_throw_error (env, NULL, "Failed to parse arguments");
return NULL;
}
if (argc < 1)
{
napi_throw_error (env, NULL, "This function requires one argument");
return NULL;
}
if (napi_get_value_string_utf8 (env, argv[0], NULL, 0, &size) != napi_ok)
{
napi_throw_error (env, NULL, "Failed to parse string");
return NULL;
}
if ((buf = malloc (++size)) == NULL)
{
napi_throw_error (env, NULL, "Failed to allocate memory");
return NULL;
}
if (napi_get_value_string_utf8 (env, argv[0], buf, size, &size) != napi_ok)
{
napi_throw_error (env, NULL, "Failed to parse string");
free (buf);
return NULL;
}
if ((bio = BIO_new_mem_buf (buf, size)) == NULL)
{
napi_throw_error (env, NULL, "Failed to copy cert into buffer");
free (buf);
return NULL;
}
if ((x509 = PEM_read_bio_X509 (bio, 0, 0, 0)) == NULL)
{
napi_throw_error (env, NULL, "Failed to read x509 from BIO");
BIO_free (bio);
free (buf);
return NULL;
}
if ((evp_pubkey = X509_get_pubkey (x509)) == NULL)
{
napi_throw_error (env, NULL, "Failed to extract public key");
X509_free (x509);
BIO_free (bio);
free (buf);
return NULL;
}
if ((public_key = EVP_PKEY_get1_RSA (evp_pubkey)) == NULL)
{
napi_throw_error (env, NULL, "Faild to extract rsa key");
EVP_PKEY_free (evp_pubkey);
X509_free (x509);
BIO_free (bio);
free (buf);
return NULL;
}
if (public_key->n == NULL || public_key->e == NULL)
{
napi_throw_error (env, NULL, "One or more required values in the "
"public key is null");
RSA_free (public_key);
EVP_PKEY_free (evp_pubkey);
X509_free (x509);
BIO_free (bio);
free (buf);
return NULL;
}
n_hex = BN_bn2hex (public_key->n);
e_hex = BN_bn2hex (public_key->e);
RSA_free (public_key);
EVP_PKEY_free (evp_pubkey);
X509_free (x509);
BIO_free (bio);
free (buf);
status |= napi_create_string_utf8 (env, n_hex, strlen (n_hex), &n_val);
status |= napi_create_string_utf8 (env, e_hex, strlen (e_hex), &e_val);
status |= napi_create_object (env, &obj);
free (n_hex);
free (e_hex);
if (status != napi_ok)
{
napi_throw_error (env, NULL, "Failed to create return object");
return NULL;
}
status |= napi_set_named_property (env, obj, "n", n_val);
status |= napi_set_named_property (env, obj, "e", e_val);
if (status != napi_ok)
{
napi_throw_error (env, NULL, "Failed to assign properties to object");
return NULL;
}
return obj;
}
static int
pass_cb (char *buf, int size, int rwflag, void *u)
{
if (u == NULL)
return -1;
size_t len = strlen (u);
memcpy (buf, u, len > (size_t) size ? (size_t) size : len);
return len;
}
static napi_value
rsa_priv_key (napi_env env, napi_callback_info info)
{
size_t argc = 2, size, pass_size;
napi_value argv[argc];
char *buf, *passphrase = NULL;
napi_status status = napi_ok;
napi_valuetype valuetype;
RSA *private_key;
BIO *bio;
char *n_hex, *e_hex, *d_hex, *p_hex, *q_hex, *dmp1_hex, *dmq1_hex,
*iqmp_hex;
napi_value n_val, e_val, d_val, p_val, q_val, dmp1_val, dmq1_val, iqmp_val;
napi_value obj;
if (napi_get_cb_info (env, info, &argc, argv, NULL, NULL) != napi_ok)
{
napi_throw_error (env, NULL, "Failed to parse arguments");
return NULL;
}
if (argc < 1)
{
napi_throw_error (env, NULL, "This function requires one argument");
return NULL;
}
if (napi_get_value_string_utf8 (env, argv[0], NULL, 0, &size) != napi_ok)
{
napi_throw_error (env, NULL, "Failed to parse string");
return NULL;
}
if ((buf = malloc (++size)) == NULL)
{
napi_throw_error (env, NULL, "Failed to allocate memory");
return NULL;
}
if (napi_get_value_string_utf8 (env, argv[0], buf, size, &size) != napi_ok)
{
napi_throw_error (env, NULL, "Failed to parse string");
free (buf);
return NULL;
}
if (argc > 1)
{
if (napi_typeof (env, argv[1], &valuetype) != napi_ok)
{
napi_throw_error (env, NULL, "cannot get type of second argument");
free (buf);
return NULL;
}
}
if (argc > 1 && valuetype != napi_undefined && valuetype != napi_null)
{
if (napi_get_value_string_utf8 (env, argv[1], NULL, 0, &pass_size) !=
napi_ok)
{
napi_throw_error (env, NULL, "Failed to read passphrase: "
"make sure passphrase is a string");
free (buf);
return NULL;
}
if ((passphrase = malloc (++pass_size)) == NULL)
{
napi_throw_error (env, NULL, "Failed to allocate memory");
free (buf);
return NULL;
}
if (napi_get_value_string_utf8
(env, argv[1], passphrase, pass_size, &pass_size) != napi_ok)
{
napi_throw_error (env, NULL, "Failed to read passphrase: "
"make sure passphrase is a string");
free (buf);
if (passphrase)
free (passphrase);
return NULL;
}
}
if ((bio = BIO_new_mem_buf (buf, size)) == NULL)
{
napi_throw_error (env, NULL, "Failed to copy key into buffer");
free (buf);
if (passphrase)
free (passphrase);
return NULL;
}
if ((private_key =
PEM_read_bio_RSAPrivateKey (bio, NULL, pass_cb, passphrase)) == NULL)
{
napi_throw_error (env, NULL, "Failed to read private key from BIO");
BIO_free (bio);
free (buf);
if (passphrase)
free (passphrase);
return NULL;
}
if (private_key->n == NULL || private_key->e == NULL ||
private_key->d == NULL || private_key->p == NULL ||
private_key->q == NULL || private_key->dmp1 == NULL ||
private_key->dmq1 == NULL || private_key->iqmp == NULL)
{
napi_throw_error (env, NULL, "One or more required values in the "
"private key is null");
RSA_free (private_key);
BIO_free (bio);
free (buf);
if (passphrase)
free (passphrase);
return NULL;
}
n_hex = BN_bn2hex (private_key->n);
e_hex = BN_bn2hex (private_key->e);
d_hex = BN_bn2hex (private_key->d);
p_hex = BN_bn2hex (private_key->p);
q_hex = BN_bn2hex (private_key->q);
dmp1_hex = BN_bn2hex (private_key->dmp1);
dmq1_hex = BN_bn2hex (private_key->dmq1);
iqmp_hex = BN_bn2hex (private_key->iqmp);
BIO_free (bio);
RSA_free (private_key);
free (buf);
if (passphrase)
free (passphrase);
status |= napi_create_string_utf8 (env, n_hex, strlen (n_hex), &n_val);
status |= napi_create_string_utf8 (env, e_hex, strlen (e_hex), &e_val);
status |= napi_create_string_utf8 (env, d_hex, strlen (d_hex), &d_val);
status |= napi_create_string_utf8 (env, p_hex, strlen (p_hex), &p_val);
status |= napi_create_string_utf8 (env, q_hex, strlen (q_hex), &q_val);
status |=
napi_create_string_utf8 (env, dmp1_hex, strlen (dmp1_hex), &dmp1_val);
status |=
napi_create_string_utf8 (env, dmq1_hex, strlen (dmq1_hex), &dmq1_val);
status |=
napi_create_string_utf8 (env, iqmp_hex, strlen (iqmp_hex), &iqmp_val);
status |= napi_create_object (env, &obj);
free (n_hex);
free (e_hex);
free (d_hex);
free (p_hex);
free (q_hex);
free (dmp1_hex);
free (dmq1_hex);
free (iqmp_hex);
if (status != napi_ok)
{
napi_throw_error (env, NULL, "Failed to create return object");
return NULL;
}
status |= napi_set_named_property (env, obj, "n", n_val);
status |= napi_set_named_property (env, obj, "e", e_val);
status |= napi_set_named_property (env, obj, "d", d_val);
status |= napi_set_named_property (env, obj, "p", p_val);
status |= napi_set_named_property (env, obj, "q", q_val);
status |= napi_set_named_property (env, obj, "dmp1", dmp1_val);
status |= napi_set_named_property (env, obj, "dmq1", dmq1_val);
status |= napi_set_named_property (env, obj, "iqmp", iqmp_val);
if (status != napi_ok)
{
napi_throw_error (env, NULL, "Failed to assign properties to object");
return NULL;
}
return obj;
}
static napi_value
init (napi_env env, napi_value exports)
{
napi_value rsa_fn, x509_fn;
OpenSSL_add_all_algorithms ();
OpenSSL_add_all_ciphers ();
if (napi_create_function (env, NULL, 0, rsa_priv_key,
NULL, &rsa_fn) != napi_ok)
napi_throw_error (env, NULL, "Unable to wrap native rsa function");
if (napi_create_function (env, NULL, 0, x509_cert_pub_key,
NULL, &x509_fn) != napi_ok)
napi_throw_error (env, NULL, "Unable to wrap native x509 function");
if (napi_set_named_property (env, exports, "RSAPrivateKey",
rsa_fn) != napi_ok)
napi_throw_error (env, NULL, "Unable to populate exports with rsa");
if (napi_set_named_property (env, exports, "X509PublicKey",
x509_fn) != napi_ok)
napi_throw_error (env, NULL, "Unable to populate exports with x509");
return exports;
}
NAPI_MODULE (NODE_GYP_MODULE_NAME, init)