-
Notifications
You must be signed in to change notification settings - Fork 0
/
x509_crl.cpp
166 lines (131 loc) · 3.99 KB
/
x509_crl.cpp
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
#include "x509_crl.hpp"
#include <assert.h>
#include "mbedtls_utils.h"
napi_ref X509Crl::sConstructor;
napi_value X509Crl::Init(napi_env env, napi_value exports)
{
napi_status status;
const uint32_t num_properties = 1;
napi_property_descriptor properties[num_properties] = {
DECLARE_NAPI_METHOD("parse", Parse),
};
napi_value cons;
status = napi_define_class(env, "mbedtls_x509_crl", NAPI_AUTO_LENGTH,
New, nullptr, num_properties, properties, &cons);
assert(status == napi_ok);
status = napi_create_reference(env, cons, 1, &sConstructor);
assert(status == napi_ok);
status = napi_set_named_property(env, exports, "X509Crl", cons);
assert(status == napi_ok);
return exports;
}
napi_status X509Crl::unwrap(napi_env env, napi_value value, X509Crl **crl)
{
napi_status status;
napi_value cons;
napi_valuetype type;
bool is_instance = false;
status = napi_get_reference_value(env, sConstructor, &cons);
assert(status == napi_ok);
status = napi_typeof(env, value, &type);
assert(status == napi_ok);
status = napi_instanceof(env, value, cons, &is_instance);
assert(status == napi_ok);
if (type == napi_null)
{
*crl = nullptr;
}
else if (is_instance)
{
status = napi_unwrap(env, value, reinterpret_cast<void **>(crl));
assert(status == napi_ok);
}
else
{
return napi_invalid_arg;
}
return status;
}
X509Crl::X509Crl(napi_env env)
{
mbedtls_x509_crl_init(this);
}
X509Crl::~X509Crl(void)
{
mbedtls_x509_crl_free(this);
}
napi_value X509Crl::New(napi_env env, napi_callback_info info)
{
napi_status status;
napi_value target;
status = napi_get_new_target(env, info, &target);
assert(status == napi_ok);
bool is_constructor = target != nullptr;
if (is_constructor) { // called with new
napi_value jsthis;
status = napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr);
assert(status == napi_ok);
X509Crl *obj = new X509Crl(env);
status = napi_wrap(env, jsthis, reinterpret_cast<void*>(obj),
Finalize, nullptr,
nullptr);
assert(status == napi_ok);
return jsthis;
} else { // called as function
napi_value cons;
status = napi_get_reference_value(env, sConstructor, &cons);
assert(status == napi_ok);
napi_value instance;
status = napi_new_instance(env, cons, 0, nullptr, &instance);
assert(status == napi_ok);
return instance;
}
}
void X509Crl::Finalize(napi_env env, void *nativeObject, void *finalize_hint)
{
delete reinterpret_cast<X509Crl *>(nativeObject);
}
napi_value X509Crl::Parse(napi_env env, napi_callback_info info)
{
napi_status status;
napi_value jsthis;
size_t argc = 1;
napi_value args[1];
napi_value jsret;
X509Crl* self;
bool is_buffer;
uint8_t *buf;
size_t buf_len;
int ret;
status = napi_get_cb_info(env, info, &argc, args, &jsthis, nullptr);
assert(status == napi_ok);
// get params: self (this), buffer
if (argc < 1)
{
status = napi_throw_type_error(env, nullptr, "Missing arguments");
assert(status == napi_ok);
return nullptr;
}
status = napi_unwrap(env, jsthis, reinterpret_cast<void**>(&self));
assert(status == napi_ok);
status = napi_is_buffer(env, args[0], &is_buffer);
assert(status == napi_ok);
if (is_buffer)
{
status = napi_get_buffer_info(env, args[0],
reinterpret_cast<void **>(&buf), &buf_len);
assert(status == napi_ok);
}
else
{
status = napi_throw_type_error(env, nullptr, "Buffer expected");
assert(status == napi_ok);
return nullptr;
}
// call
ret = mbedtls_x509_crl_parse(self, buf, buf_len);
// return
status = napi_create_int32(env, ret, &jsret);
assert(status == napi_ok);
return jsret;
}