forked from zhaozg/lua-openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.c
More file actions
307 lines (279 loc) · 7.87 KB
/
misc.c
File metadata and controls
307 lines (279 loc) · 7.87 KB
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
/*=========================================================================*\
* misc.h
* misc routines for lua-openssl binding
*
* Author: george zhao <zhaozg(at)gmail.com>
\*=========================================================================*/
/***
misc module with utility functions for lua-openssl
This module provides various utility functions and helpers that are
used throughout the lua-openssl library for data conversion,
formatting, and common operations.
@module misc
@usage
misc = require('openssl').misc
*/
#include "openssl.h"
#include "private.h"
const char *format[] = { "auto", "der", "pem", "smime", NULL };
BIO *
load_bio_object(lua_State *L, int idx)
{
BIO *bio = NULL;
if (lua_isstring(L, idx)) {
size_t l = 0;
const char *ctx = lua_tolstring(L, idx, &l);
/* read only */
bio = (BIO *)BIO_new_mem_buf((void *)ctx, l);
} else if (auxiliar_getclassudata(L, "openssl.bio", idx)) {
bio = CHECK_OBJECT(idx, BIO, "openssl.bio");
BIO_up_ref(bio);
} else
luaL_argerror(L, idx, "only support string or openssl.bio");
return bio;
}
int
bio_is_der(BIO *bio)
{
byte head[1];
int len = BIO_read(bio, head, sizeof(head));
(void)BIO_reset(bio);
if (len == sizeof(head) && head[0] == 0x30) return 1;
return 0;
}
const EVP_MD *
opt_digest(lua_State *L, int idx, const char *alg)
{
const EVP_MD *md = NULL;
switch (lua_type(L, idx)) {
case LUA_TSTRING:
md = EVP_get_digestbyname(lua_tostring(L, idx));
break;
case LUA_TNUMBER:
md = EVP_get_digestbynid(lua_tointeger(L, idx));
break;
case LUA_TUSERDATA:
if (auxiliar_getclassudata(L, "openssl.asn1_object", idx))
md = EVP_get_digestbyobj(CHECK_OBJECT(idx, ASN1_OBJECT, "openssl.asn1_object"));
else if (auxiliar_getclassudata(L, "openssl.evp_digest", idx))
md = CHECK_OBJECT(idx, EVP_MD, "openssl.evp_digest");
break;
case LUA_TNONE:
case LUA_TNIL:
if (alg != NULL) md = EVP_get_digestbyname(alg);
break;
}
if (alg != NULL && md == NULL) {
luaL_argerror(L, idx, "must be a string, NID number or asn1_object identity digest method");
}
return md;
}
const EVP_MD *
get_digest(lua_State *L, int idx, const char *alg)
{
const EVP_MD *md = opt_digest(L, idx, alg);
if (md == NULL)
luaL_argerror(L, idx, "must be a string, NID number or asn1_object identity digest method");
return md;
}
const EVP_CIPHER *
opt_cipher(lua_State *L, int idx, const char *alg)
{
const EVP_CIPHER *cipher = NULL;
switch (lua_type(L, idx)) {
case LUA_TSTRING:
cipher = EVP_get_cipherbyname(lua_tostring(L, idx));
break;
case LUA_TNUMBER:
cipher = EVP_get_cipherbynid(lua_tointeger(L, idx));
break;
case LUA_TUSERDATA:
if (auxiliar_getclassudata(L, "openssl.asn1_object", idx))
cipher = EVP_get_cipherbyobj(CHECK_OBJECT(idx, ASN1_OBJECT, "openssl.asn1_object"));
else if (auxiliar_getclassudata(L, "openssl.evp_cipher", idx))
cipher = CHECK_OBJECT(idx, EVP_CIPHER, "openssl.evp_cipher");
break;
case LUA_TNONE:
case LUA_TNIL:
if (alg != NULL) cipher = EVP_get_cipherbyname(alg);
break;
}
if (alg != NULL && cipher == NULL)
luaL_argerror(L, idx, "must be a string, NID number or asn1_object identity cipher method");
return cipher;
}
const EVP_CIPHER *
get_cipher(lua_State *L, int idx, const char *alg)
{
const EVP_CIPHER *c = opt_cipher(L, idx, alg);
if (c == NULL)
luaL_argerror(L, idx, "must be a string, NID number or asn1_object identity cipher method");
return c;
}
BIGNUM *
BN_get(lua_State *L, int i)
{
BIGNUM *x = BN_new();
switch (lua_type(L, i)) {
case LUA_TNUMBER: {
lua_Integer num = lua_tointeger(L, 3);
if (num < 0) {
BN_set_word(x, -num);
BN_set_negative(x, 1);
} else {
BN_set_word(x, num);
}
break;
}
case LUA_TSTRING: {
const char *s = lua_tostring(L, i);
if (s[0] == 'X' || s[0] == 'x')
BN_hex2bn(&x, s + 1);
else
BN_dec2bn(&x, s);
break;
}
case LUA_TUSERDATA:
BN_copy(x, CHECK_OBJECT(i, BIGNUM, "openssl.bn"));
break;
case LUA_TNIL:
BN_free(x);
x = NULL;
break;
}
return x;
}
void
openssl_add_method_or_alias(const OBJ_NAME *name, void *arg)
{
lua_State *L = (lua_State *)arg;
int i = lua_rawlen(L, -1);
lua_pushstring(L, name->name);
lua_rawseti(L, -2, i + 1);
}
void
openssl_add_method(const OBJ_NAME *name, void *arg)
{
if (name->alias == 0) {
openssl_add_method_or_alias(name, arg);
}
}
int
openssl_pushresult(lua_State *L, int result)
{
if (result >= 1) {
lua_pushboolean(L, 1);
return 1;
} else {
unsigned long val = ERR_get_error();
lua_pushnil(L);
if (val) {
lua_pushstring(L, ERR_reason_error_string(val));
lua_pushinteger(L, val);
} else {
lua_pushstring(L, "UNKNOWN ERROR");
lua_pushnil(L);
}
return 3;
}
}
static const char *hex_tab = "0123456789abcdef";
void
to_hex(const char *in, int length, char *out)
{
int i;
for (i = 0; i < length; i++) {
out[i * 2] = hex_tab[(in[i] >> 4) & 0xF];
out[i * 2 + 1] = hex_tab[(in[i]) & 0xF];
}
out[i * 2] = '\0';
}
int
openssl_push_bit_string_bitname(lua_State *L, const BIT_STRING_BITNAME *name)
{
lua_newtable(L);
lua_pushinteger(L, name->bitnum);
lua_setfield(L, -2, "bitnum");
lua_pushstring(L, name->lname);
lua_setfield(L, -2, "lname");
lua_pushstring(L, name->sname);
lua_setfield(L, -2, "sname");
return 1;
}
static const char *sPadding[] = {
"pkcs1",
#ifdef RSA_SSLV23_PADDING
#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER < 0x3020000fL
"sslv23",
#endif
#endif
"no", "oaep", "x931", "pss", NULL,
};
static int iPadding[]
= { RSA_PKCS1_PADDING,
#ifdef RSA_SSLV23_PADDING
#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER < 0x3020000fL
RSA_SSLV23_PADDING,
#endif
#endif
RSA_NO_PADDING, RSA_PKCS1_OAEP_PADDING, RSA_X931_PADDING, RSA_PKCS1_PSS_PADDING };
int
openssl_get_padding(lua_State *L, int idx, const char *defval)
{
return auxiliar_checkoption(L, idx, defval, sPadding, iPadding);
}
size_t
posrelat(ptrdiff_t pos, size_t len)
{
if (pos >= 0)
return (size_t)pos;
else if (0u - (size_t)pos > len)
return 0;
else
return len - ((size_t)-pos) + 1;
}
static const char hex[]
= { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
static const char bin[256] = {
/* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
/* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 30 */ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
/* 40 */ 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 60 */ 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 70 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* a0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* b0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* c0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* d0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* e0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* f0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
int
hex2bin(const char *src, unsigned char *dst, int len)
{
int i;
if (len == 0) len = strlen(src);
for (i = 0; i < len; i += 2) {
unsigned char h = src[i];
unsigned char l = src[i + 1];
dst[i / 2] = bin[h] << 4 | bin[l];
}
return i / 2;
}
int
bin2hex(const unsigned char *src, char *dst, int len)
{
int i;
for (i = 0; i < len; i++) {
unsigned char c = src[i];
dst[i * 2] = hex[c >> 4];
dst[i * 2 + 1] = hex[c & 0xf];
}
dst[i * 2] = '\0';
return i * 2;
}