• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zhaozg / lua-openssl / 14016924506

23 Mar 2025 07:55AM UTC coverage: 93.18% (+0.3%) from 92.866%
14016924506

push

travis-ci

zhaozg
style: .clang-format

3374 of 3453 new or added lines in 34 files covered. (97.71%)

148 existing lines in 23 files now uncovered.

9291 of 9971 relevant lines covered (93.18%)

1907.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.52
/src/misc.c
1
/*=========================================================================*\
2
* misc.h
3
* misc routines for lua-openssl binding
4
*
5
* Author:  george zhao <zhaozg(at)gmail.com>
6
\*=========================================================================*/
7
#include "openssl.h"
8
#include "private.h"
9
const char *format[] = { "auto", "der", "pem", "smime", NULL };
10

11
BIO *
12
load_bio_object(lua_State *L, int idx)
3,872✔
13
{
14
  BIO *bio = NULL;
3,872✔
15
  if (lua_isstring(L, idx)) {
3,872✔
16
    size_t      l = 0;
3,872✔
17
    const char *ctx = lua_tolstring(L, idx, &l);
3,872✔
18
    /* read only */
19
    bio = (BIO *)BIO_new_mem_buf((void *)ctx, l);
3,872✔
NEW
20
  } else if (auxiliar_getclassudata(L, "openssl.bio", idx)) {
×
21
    bio = CHECK_OBJECT(idx, BIO, "openssl.bio");
×
22
    BIO_up_ref(bio);
×
23
  } else
UNCOV
24
    luaL_argerror(L, idx, "only support string or openssl.bio");
×
25
  return bio;
3,872✔
26
}
27

28
int
29
bio_is_der(BIO *bio)
3,035✔
30
{
31
  byte head[1];
32
  int  len = BIO_read(bio, head, sizeof(head));
3,035✔
33
  (void)BIO_reset(bio);
3,035✔
34
  if (len == sizeof(head) && head[0] == 0x30) return 1;
3,035✔
35

36
  return 0;
3,023✔
37
}
38

39
const EVP_MD *
40
opt_digest(lua_State *L, int idx, const char *alg)
758✔
41
{
42
  const EVP_MD *md = NULL;
758✔
43
  switch (lua_type(L, idx)) {
758✔
44
  case LUA_TSTRING:
333✔
45
    md = EVP_get_digestbyname(lua_tostring(L, idx));
333✔
46
    break;
333✔
47
  case LUA_TNUMBER:
3✔
48
    md = EVP_get_digestbynid(lua_tointeger(L, idx));
3✔
49
    break;
3✔
50
  case LUA_TUSERDATA:
27✔
51
    if (auxiliar_getclassudata(L, "openssl.asn1_object", idx))
27✔
52
      md = EVP_get_digestbyobj(CHECK_OBJECT(idx, ASN1_OBJECT, "openssl.asn1_object"));
3✔
53
    else if (auxiliar_getclassudata(L, "openssl.evp_digest", idx))
24✔
54
      md = CHECK_OBJECT(idx, EVP_MD, "openssl.evp_digest");
24✔
55
    break;
27✔
56
  case LUA_TNONE:
395✔
57
  case LUA_TNIL:
58
    if (alg != NULL) md = EVP_get_digestbyname(alg);
395✔
59
    break;
395✔
60
  }
61

62
  if (alg != NULL && md == NULL) {
758✔
UNCOV
63
    luaL_argerror(L, idx, "must be a string, NID number or asn1_object identity digest method");
×
64
  }
65

66
  return md;
758✔
67
}
68

69
const EVP_MD *
70
get_digest(lua_State *L, int idx, const char *alg)
758✔
71
{
72
  const EVP_MD *md = opt_digest(L, idx, alg);
758✔
73
  if (md == NULL)
758✔
74
    luaL_argerror(L, idx, "must be a string, NID number or asn1_object identity digest method");
×
75
  return md;
758✔
76
}
77

78
const EVP_CIPHER *
79
opt_cipher(lua_State *L, int idx, const char *alg)
345✔
80
{
81
  const EVP_CIPHER *cipher = NULL;
345✔
82

83
  switch (lua_type(L, idx)) {
345✔
84
  case LUA_TSTRING:
117✔
85
    cipher = EVP_get_cipherbyname(lua_tostring(L, idx));
117✔
86
    break;
117✔
87
  case LUA_TNUMBER:
3✔
88
    cipher = EVP_get_cipherbynid(lua_tointeger(L, idx));
3✔
89
    break;
3✔
90
  case LUA_TUSERDATA:
180✔
91
    if (auxiliar_getclassudata(L, "openssl.asn1_object", idx))
180✔
92
      cipher = EVP_get_cipherbyobj(CHECK_OBJECT(idx, ASN1_OBJECT, "openssl.asn1_object"));
3✔
93
    else if (auxiliar_getclassudata(L, "openssl.evp_cipher", idx))
177✔
94
      cipher = CHECK_OBJECT(idx, EVP_CIPHER, "openssl.evp_cipher");
177✔
95
    break;
180✔
96
  case LUA_TNONE:
45✔
97
  case LUA_TNIL:
98
    if (alg != NULL) cipher = EVP_get_cipherbyname(alg);
45✔
99
    break;
45✔
100
  }
101

102
  if (alg != NULL && cipher == NULL)
345✔
103
    luaL_argerror(L, idx, "must be a string, NID number or asn1_object identity cipher method");
×
104

105
  return cipher;
345✔
106
}
107

108
const EVP_CIPHER *
109
get_cipher(lua_State *L, int idx, const char *alg)
345✔
110
{
111
  const EVP_CIPHER *c = opt_cipher(L, idx, alg);
345✔
112
  if (c == NULL)
345✔
UNCOV
113
    luaL_argerror(L, idx, "must be a string, NID number or asn1_object identity cipher method");
×
114
  return c;
345✔
115
}
116

117
BIGNUM *
118
BN_get(lua_State *L, int i)
261✔
119
{
120
  BIGNUM *x = BN_new();
261✔
121
  switch (lua_type(L, i)) {
261✔
122
  case LUA_TNUMBER:
42✔
123
    BN_set_word(x, lua_tointeger(L, i));
42✔
124
    break;
42✔
125
  case LUA_TSTRING: {
18✔
126
    const char *s = lua_tostring(L, i);
18✔
127
    if (s[0] == 'X' || s[0] == 'x')
18✔
128
      BN_hex2bn(&x, s + 1);
3✔
129
    else
130
      BN_dec2bn(&x, s);
15✔
131
    break;
18✔
132
  }
133
  case LUA_TUSERDATA:
195✔
134
    BN_copy(x, CHECK_OBJECT(i, BIGNUM, "openssl.bn"));
195✔
135
    break;
195✔
136
  case LUA_TNIL:
6✔
137
    BN_free(x);
6✔
138
    x = NULL;
6✔
139
    break;
6✔
140
  }
141
  return x;
261✔
142
}
143

144
void
145
openssl_add_method_or_alias(const OBJ_NAME *name, void *arg)
2,589✔
146
{
147
  lua_State *L = (lua_State *)arg;
2,589✔
148
  int        i = lua_rawlen(L, -1);
2,589✔
149
  lua_pushstring(L, name->name);
2,589✔
150
  lua_rawseti(L, -2, i + 1);
2,589✔
151
}
2,589✔
152

153
void
154
openssl_add_method(const OBJ_NAME *name, void *arg)
738✔
155
{
156
  if (name->alias == 0) {
738✔
157
    openssl_add_method_or_alias(name, arg);
547✔
158
  }
159
}
738✔
160

161
int
162
openssl_pushresult(lua_State *L, int result)
6,295✔
163
{
164
  if (result >= 1) {
6,295✔
165
    lua_pushboolean(L, 1);
6,230✔
166
    return 1;
6,230✔
167
  } else {
168
    unsigned long val = ERR_get_error();
65✔
169
    lua_pushnil(L);
65✔
170
    if (val) {
65✔
171
      lua_pushstring(L, ERR_reason_error_string(val));
56✔
172
      lua_pushinteger(L, val);
56✔
173
    } else {
174
      lua_pushstring(L, "UNKNOWN ERROR");
9✔
175
      lua_pushnil(L);
9✔
176
    }
177
    return 3;
65✔
178
  }
179
}
180

181
static const char *hex_tab = "0123456789abcdef";
182

183
void
184
to_hex(const char *in, int length, char *out)
27✔
185
{
186
  int i;
187
  for (i = 0; i < length; i++) {
567✔
188
    out[i * 2] = hex_tab[(in[i] >> 4) & 0xF];
540✔
189
    out[i * 2 + 1] = hex_tab[(in[i]) & 0xF];
540✔
190
  }
191
  out[i * 2] = '\0';
27✔
192
}
27✔
193

194
int
195
openssl_push_bit_string_bitname(lua_State *L, const BIT_STRING_BITNAME *name)
105✔
196
{
197
  lua_newtable(L);
105✔
198
  lua_pushinteger(L, name->bitnum);
105✔
199
  lua_setfield(L, -2, "bitnum");
105✔
200
  lua_pushstring(L, name->lname);
105✔
201
  lua_setfield(L, -2, "lname");
105✔
202
  lua_pushstring(L, name->sname);
105✔
203
  lua_setfield(L, -2, "sname");
105✔
204
  return 1;
105✔
205
}
206

207
static const char *sPadding[] = {
208
  "pkcs1",
209
#ifdef RSA_SSLV23_PADDING
210
#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER < 0x3020000fL
211
  "sslv23",
212
#endif
213
#endif
214
  "no",     "oaep", "x931", "pss", NULL,
215
};
216

217
static int iPadding[]
218
  = { RSA_PKCS1_PADDING,
219
#ifdef RSA_SSLV23_PADDING
220
#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER < 0x3020000fL
221
      RSA_SSLV23_PADDING,
222
#endif
223
#endif
224
      RSA_NO_PADDING,     RSA_PKCS1_OAEP_PADDING, RSA_X931_PADDING, RSA_PKCS1_PSS_PADDING };
225

226
int
227
openssl_get_padding(lua_State *L, int idx, const char *defval)
147✔
228
{
229
  return auxiliar_checkoption(L, idx, defval, sPadding, iPadding);
147✔
230
}
231

232
size_t
233
posrelat(ptrdiff_t pos, size_t len)
120✔
234
{
235
  if (pos >= 0)
120✔
236
    return (size_t)pos;
120✔
NEW
237
  else if (0u - (size_t)pos > len)
×
NEW
238
    return 0;
×
239
  else
NEW
240
    return len - ((size_t)-pos) + 1;
×
241
}
242

243
static const char hex[]
244
  = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
245
static const char bin[256] = {
246
  /*       0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
247
  /* 00 */ 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0,
248
  /* 10 */ 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0,
249
  /* 20 */ 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0,
250
  /* 30 */ 0, 1,  2,  3,  4,  5,  6,  7, 8, 9, 0, 0, 0, 0, 0, 0,
251
  /* 40 */ 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
252
  /* 50 */ 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0,
253
  /* 60 */ 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
254
  /* 70 */ 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0,
255
  /* 80 */ 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0,
256
  /* 90 */ 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0,
257
  /* a0 */ 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0,
258
  /* b0 */ 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0,
259
  /* c0 */ 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0,
260
  /* d0 */ 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0,
261
  /* e0 */ 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0,
262
  /* f0 */ 0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0,
263
};
264

265
int
266
hex2bin(const char *src, unsigned char *dst, int len)
3✔
267
{
268
  int i;
269
  if (len == 0) len = strlen(src);
3✔
270
  for (i = 0; i < len; i += 2) {
135✔
271
    unsigned char h = src[i];
132✔
272
    unsigned char l = src[i + 1];
132✔
273
    dst[i / 2] = bin[h] << 4 | bin[l];
132✔
274
  }
275
  return i / 2;
3✔
276
}
277
int
278
bin2hex(const unsigned char *src, char *dst, int len)
111✔
279
{
280
  int i;
281
  for (i = 0; i < len; i++) {
26,838✔
282
    unsigned char c = src[i];
26,727✔
283
    dst[i * 2] = hex[c >> 4];
26,727✔
284
    dst[i * 2 + 1] = hex[c & 0xf];
26,727✔
285
  }
286
  dst[i * 2] = '\0';
111✔
287
  return i * 2;
111✔
288
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc