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

zhaozg / lua-openssl / 25780119971

13 May 2026 05:24AM UTC coverage: 92.808% (-1.0%) from 93.832%
25780119971

push

travis-ci

web-flow
Merge 00b7c4e2f into d7aec501b

995 of 1143 new or added lines in 12 files covered. (87.05%)

15 existing lines in 2 files now uncovered.

10607 of 11429 relevant lines covered (92.81%)

1920.04 hits per line

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

90.97
/src/ssl.c
1
/***
2
ssl modules to create SSL/TLS server or client, send and recv data over SSL channels.
3

4
@module ssl
5
@usage
6
  ssl = require('openssl').ssl
7
*/
8
#include <openssl/dh.h>
9
#include <openssl/ec.h>
10
#include <openssl/rsa.h>
11
#include <openssl/ssl.h>
12

13
#include "openssl.h"
14
#include "private.h"
15
#include "ssl_options.h"
16

17
#include <stdint.h>
18

19
#if OPENSSL_VERSION_NUMBER > 0x30000000
20
#ifndef SSL_get_peer_certificate
21
#define SSL_get_peer_certificate SSL_get1_peer_certificate
22
#endif
23
#ifndef SSL_DEFAULT_CIPHER_LIST
24
#define SSL_DEFAULT_CIPHER_LIST OSSL_default_cipher_list()
25
#endif
26
#endif
27

28
/***
29
create ssl_ctx object, which mapping to SSL_CTX in openssl.
30
@function ctx_new
31
@tparam string protocol support 'SSLv3', 'SSLv23', 'SSLv2', 'TSLv1', 'TSLv1_1','TSLv1_2','TLS',
32
'DTLSv1','DTLSv1_2', and can be follow by '_server' or '_client', in general you should use 'TLS' to
33
negotiate highest available SSL/TLS version
34
@tparam[opt] string support_ciphers, if not given, default of openssl will be used
35
@treturn openssl.ssl_ctx
36
*/
37
#if OPENSSL_VERSION_NUMBER > 0x10100000L
38
#define TLS_PROTOCOL_TIPS                                                                          \
39
  "only support TLS, DTLS to negotiate highest available SSL/TLS or DTLS "                         \
40
  "version above openssl v1.1.0\n"                                                                 \
41
  "optional followed by _client or _server\n"                                                      \
42
  "default is TLS\n"
43
#define DEFAULT_PROTOCOL "TLS"
44
#else
45
#define TLS_PROTOCOL_TIPS                                                                          \
46
  "SSLv23, TLSv1_2, TLSv1_1, TLSv1, DTLSv1_2 or DTLSv1, optional followed by _client or _server\n" \
47
  "optional followed by _client or _server\n"                                                      \
48
  "default is SSLv23 to negotiate highest available SSL/TLS\n"
49
#define DEFAULT_PROTOCOL "SSLv23"
50
#endif
51

52
typedef enum
53
{
54
  SSL_CTX_SESSION_ADD = 0,
55
  SSL_CTX_SESSION_GET,
56
  SSL_CTX_SESSION_DEL,
57
#if OPENSSL_VERSION_NUMBER < 0x10100000L
58

59
  SSL_CTX_TEMP_DH,
60
  SSL_CTX_TEMP_RSA,
61
  SSL_CTX_TEMP_ECDH,
62
#endif
63
  SSL_CTX_MAX_IDX
64
} SSL_CTX_INDEX;
65

66
/***
67
create a new SSL context object
68
@function new
69
@tparam[opt="TLS"] string method SSL/TLS protocol method ("TLS", "SSLv23", "TLSv1", "TLSv1_1", "TLSv1_2", etc.)
70
@treturn openssl.ssl_ctx SSL context object
71
*/
72
static int
73
openssl_ssl_ctx_new(lua_State *L)
793✔
74
{
75
  const char *meth = luaL_optstring(L, 1, DEFAULT_PROTOCOL);
793✔
76
#if OPENSSL_VERSION_NUMBER >= 0x01000000L
77
  const
78
#endif
79
    SSL_METHOD *method
793✔
80
    = NULL;
81
  const char *ciphers;
82
  SSL_CTX    *ctx;
83

84
  if (strcmp(meth, "SSLv23") == 0)
793✔
85
    method = SSLv23_method();
3✔
86
  else if (strcmp(meth, "SSLv23_server") == 0)
790✔
87
    method = SSLv23_server_method();
6✔
88
  else if (strcmp(meth, "SSLv23_client") == 0)
784✔
89
    method = SSLv23_client_method();
5✔
90

91
#if OPENSSL_VERSION_NUMBER > 0x10100000L
92
  else if (strcmp(meth, "TLS") == 0)
753✔
93
    method = TLS_method();
642✔
94
  else if (strcmp(meth, "TLS_server") == 0)
111✔
95
    method = TLS_server_method();
21✔
96
  else if (strcmp(meth, "TLS_client") == 0)
90✔
97
    method = TLS_client_method();
6✔
98

99
  else if (strcmp(meth, "DTLS") == 0)
84✔
100
    method = DTLS_method();
3✔
101
  else if (strcmp(meth, "DTLS_server") == 0)
81✔
102
    method = DTLS_server_method();
3✔
103
  else if (strcmp(meth, "DTLS_client") == 0)
78✔
104
    method = DTLS_client_method();
6✔
105
#endif
106

107
#if !defined(OPENSSL_NO_DEPRECATED) && !defined(OPENSSL_NO_DTLS1_2_METHOD) && \
108
  OPENSSL_VERSION_NUMBER < 0x10100000L
109
  else if (strcmp(meth, "DTLSv1_2") == 0)
26✔
110
    method = DTLSv1_2_method();
111
  else if (strcmp(meth, "DTLSv1_2_server") == 0)
26✔
112
    method = DTLSv1_2_server_method();
113
  else if (strcmp(meth, "DTLSv1_2_client") == 0)
26✔
114
    method = DTLSv1_2_client_method();
1✔
115
#endif
116

117
#if !defined(OPENSSL_NO_DEPRECATED) && !defined(OPENSSL_NO_DTLS1_METHOD) && \
118
  OPENSSL_VERSION_NUMBER < 0x10100000L
119

120
  else if (strcmp(meth, "DTLSv1") == 0)
25✔
121
    method = DTLSv1_method();
122
  else if (strcmp(meth, "DTLSv1_server") == 0)
25✔
123
    method = DTLSv1_server_method();
124
  else if (strcmp(meth, "DTLSv1_client") == 0)
25✔
125
    method = DTLSv1_client_method();
1✔
126
#endif
127

128
#if !defined(OPENSSL_NO_DEPRECATED) && !defined(OPENSSL_NO_TLS1_2_METHOD) && \
129
  OPENSSL_VERSION_NUMBER < 0x10100000L
130

131
  else if (strcmp(meth, "TLSv1_2") == 0)
24✔
132
    method = TLSv1_2_method();
133
  else if (strcmp(meth, "TLSv1_2_server") == 0)
24✔
134
    method = TLSv1_2_server_method();
135
  else if (strcmp(meth, "TLSv1_2_client") == 0)
24✔
136
    method = TLSv1_2_client_method();
1✔
137
#endif
138

139
#if !defined(OPENSSL_NO_DEPRECATED) && !defined(OPENSSL_NO_TLS1_1_METHOD) && \
140
  OPENSSL_VERSION_NUMBER < 0x10100000L
141

142
  else if (strcmp(meth, "TLSv1_1") == 0)
23✔
143
    method = TLSv1_1_method();
144
  else if (strcmp(meth, "TLSv1_1_server") == 0)
23✔
145
    method = TLSv1_1_server_method();
146
  else if (strcmp(meth, "TLSv1_1_client") == 0)
23✔
147
    method = TLSv1_1_client_method();
1✔
148
#endif
149

150
#if !defined(OPENSSL_NO_DEPRECATED) && !defined(OPENSSL_NO_TLS1_METHOD) && \
151
  OPENSSL_VERSION_NUMBER < 0x10100000L
152

153
  else if (strcmp(meth, "TLSv1") == 0)
22✔
154
    method = TLSv1_method();
155
  else if (strcmp(meth, "TLSv1_server") == 0)
22✔
156
    method = TLSv1_server_method();
157
  else if (strcmp(meth, "TLSv1_client") == 0)
22✔
158
    method = TLSv1_client_method();
1✔
159
#endif
160

161
#if OPENSSL_VERSION_NUMBER < 0x40000000L && !defined(OPENSSL_NO_SSL3_METHOD)
162
  else if (strcmp(meth, "SSLv3") == 0)
21✔
163
    method = SSLv3_method();
164
  else if (strcmp(meth, "SSLv3_server") == 0)
21✔
165
    method = SSLv3_server_method();
166
  else if (strcmp(meth, "SSLv3_client") == 0)
21✔
167
    method = SSLv3_client_method();
1✔
168
#endif
169

170
#ifdef LOAD_SSL_CUSTOM
171
  LOAD_SSL_CUSTOM
172
#endif
173
  else luaL_argerror(L, 1, TLS_PROTOCOL_TIPS);
92✔
174

175
  ctx = SSL_CTX_new(method);
701✔
176
  if (!ctx) luaL_argerror(L, 1, TLS_PROTOCOL_TIPS);
701✔
177

178
  ciphers = luaL_optstring(L, 2, SSL_DEFAULT_CIPHER_LIST);
701✔
179
#if OPENSSL_VERSION_NUMBER > 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
180
  if (!SSL_CTX_set_ciphersuites(ctx, ciphers) && !SSL_CTX_set_cipher_list(ctx, ciphers))
684✔
181
#else
182
  if (!SSL_CTX_set_cipher_list(ctx, ciphers))
17✔
183
#endif
184
    luaL_argerror(L, 2, "Error to set cipher list");
×
185

186
  PUSH_OBJECT(ctx, "openssl.ssl_ctx");
701✔
187
  SSL_CTX_set_app_data(ctx, openssl_mainthread(L));
701✔
188
  openssl_newvalue(L, ctx);
701✔
189

190
  return 1;
701✔
191
}
192

193
/***
194
get alert_type for ssl state
195
@function alert_type
196
@tparam number alert
197
@tparam[opt=false] boolean long
198
@treturn string alert type
199
*/
200
static int
201
openssl_ssl_alert_type(lua_State *L)
24✔
202
{
203
  int         v = luaL_checkint(L, 1);
24✔
204
  int         _long = lua_isnone(L, 2) ? 0 : auxiliar_checkboolean(L, 2);
24✔
205
  const char *val;
206

207
  if (_long)
24✔
208
    val = SSL_alert_type_string_long(v << 8);
12✔
209
  else
210
    val = SSL_alert_type_string(v << 8);
12✔
211
  lua_pushstring(L, val);
24✔
212

213
  return 1;
24✔
214
}
215

216
/***
217
get alert_desc for ssl state
218
@function alert_desc
219
@tparam number alert
220
@tparam[opt=false] boolean long
221
@treturn string alert type
222
@treturn string desc string, if long set true will return long info
223
*/
224
static int
225
openssl_ssl_alert_desc(lua_State *L)
96✔
226
{
227
  int         v = luaL_checkint(L, 1);
96✔
228
  int         _long = lua_isnone(L, 2) ? 0 : auxiliar_checkboolean(L, 2);
96✔
229
  const char *val;
230

231
  if (_long)
96✔
232
    val = SSL_alert_desc_string_long(v);
48✔
233
  else
234
    val = SSL_alert_desc_string(v);
48✔
235
  lua_pushstring(L, val);
96✔
236

237
  return 1;
96✔
238
}
239

240
/***
241
create new SSL session object
242
@function session_new
243
@treturn ssl_session new SSL session object
244
*/
245
static int
246
openssl_ssl_session_new(lua_State *L)
4✔
247
{
248
  SSL_SESSION *ss = SSL_SESSION_new();
4✔
249
  PUSH_OBJECT(ss, "openssl.ssl_session");
4✔
250
  return 1;
4✔
251
}
252

253
/***
254
read SSL session from BIO or string data
255
@function session_read
256
@tparam bio|string input BIO object or string containing session data (PEM or DER format)
257
@treturn ssl_session|nil SSL session object or nil on error
258
*/
259
static int
260
openssl_ssl_session_read(lua_State *L)
4✔
261
{
262
  BIO         *in = load_bio_object(L, 1);
4✔
263
  SSL_SESSION *ss = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL);
4✔
264
  if (!ss) {
4✔
265
    (void)BIO_reset(in);
×
266
    ss = d2i_SSL_SESSION_bio(in, NULL);
×
267
  }
268
  BIO_free(in);
4✔
269
  if (ss) {
4✔
270
    PUSH_OBJECT(ss, "openssl.ssl_session");
4✔
271
    return 1;
4✔
272
  }
273
  return openssl_pushresult(L, 0);
×
274
}
275

276
static luaL_Reg R[] = {
277
  { "ctx_new",      openssl_ssl_ctx_new      },
278
  { "alert_type",   openssl_ssl_alert_type   },
279
  { "alert_desc",   openssl_ssl_alert_desc   },
280

281
  { "session_new",  openssl_ssl_session_new  },
282
  { "session_read", openssl_ssl_session_read },
283
  { NULL,           NULL                     }
284
};
285

286
/* SSL CTX object */
287
/***
288
openssl.ssl_ctx object
289
@type ssl_ctx
290
*/
291

292
/***
293
tell ssl_ctx use private key and certificate, and check private key
294
@function use
295
@tparam openssl.evp_pkey pkey
296
@tparam openssl.x509 cert
297
@treturn boolean result return true for ok, or nil followed by errmsg and errval
298
*/
299
static int
300
openssl_ssl_ctx_use(lua_State *L)
666✔
301
{
302
  int       ret;
303
  SSL_CTX  *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
666✔
304
  EVP_PKEY *pkey = CHECK_OBJECT(2, EVP_PKEY, "openssl.evp_pkey");
666✔
305

306
  if (lua_isstring(L, 3)) {
666✔
307
    ret = SSL_CTX_use_certificate_chain_file(ctx, luaL_checkstring(L, 3));
×
308
  } else {
309
    X509 *cert = CHECK_OBJECT(3, X509, "openssl.x509");
666✔
310
    ret = SSL_CTX_use_certificate(ctx, cert);
666✔
311
  }
312
  if (ret == 1) {
666✔
313
    ret = SSL_CTX_use_PrivateKey(ctx, pkey);
666✔
314
    if (ret == 1) {
666✔
315
      ret = SSL_CTX_check_private_key(ctx);
666✔
316
    }
317
  }
318
  return openssl_pushresult(L, ret);
666✔
319
}
320

321
/***
322
add client ca cert and option extra chain cert
323
@function add
324
@tparam openssl.x509 clientca
325
@tparam[opt] table extra_chain_cert_array
326
@treturn boolean result
327
*/
328
static int
329
openssl_ssl_ctx_add(lua_State *L)
4✔
330
{
331
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
4✔
332
  X509    *x = CHECK_OBJECT(2, X509, "openssl.x509");
4✔
333
  int      ret = SSL_CTX_add_client_CA(ctx, x);
4✔
334
  if (ret == 1 && !lua_isnone(L, 3)) {
4✔
335
    size_t i;
336
    luaL_checktable(L, 3);
4✔
337

338
    for (i = 1; ret == 1 && i <= lua_rawlen(L, 3); i++) {
16✔
339
      lua_rawgeti(L, 3, i);
12✔
340
      x = CHECK_OBJECT(2, X509, "openssl.x509");
12✔
341
      lua_pop(L, 1);
12✔
342
      X509_up_ref(x);
12✔
343
      ret = SSL_CTX_add_extra_chain_cert(ctx, x);
12✔
344
    }
345
  }
346
  return openssl_pushresult(L, ret);
4✔
347
}
348

349
static int
350
openssl_ssl_ctx_gc(lua_State *L)
701✔
351
{
352
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
701✔
353
  SSL_CTX_free(ctx);
701✔
354
  openssl_freevalue(L, ctx);
701✔
355

356
  return 0;
701✔
357
}
358

359
/***
360
get timeout
361
@function timeout
362
@return number
363
*/
364
/***
365
set timeout
366
@function timeout
367
@tparam number timeout
368
@treturn number previous timeout
369
*/
370
static int
371
openssl_ssl_ctx_timeout(lua_State *L)
26✔
372
{
373
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
26✔
374
  long     t;
375
  if (!lua_isnone(L, 2)) {
26✔
376
    t = SSL_CTX_set_timeout(ctx, luaL_checkint(L, 2));
13✔
377
    lua_pushinteger(L, t);
13✔
378
    return 1;
13✔
379
  }
380
  t = SSL_CTX_get_timeout(ctx);
13✔
381
  lua_pushinteger(L, t);
13✔
382
  return 1;
13✔
383
}
384

385
static const int iMode_options[] = { SSL_MODE_ENABLE_PARTIAL_WRITE,
386
                                     SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER,
387
                                     SSL_MODE_AUTO_RETRY,
388
                                     SSL_MODE_NO_AUTO_CHAIN,
389
#ifdef SSL_MODE_RELEASE_BUFFERS
390
                                     SSL_MODE_RELEASE_BUFFERS,
391
#endif
392
                                     0 };
393

394
static const char *sMode_options[] = { "enable_partial_write",
395
                                       "accept_moving_write_buffer",
396
                                       "auto_retry",
397
                                       "no_auto_chain",
398
#ifdef SSL_MODE_RELEASE_BUFFERS
399
                                       "release_buffers",
400
#endif
401
                                       NULL };
402

403
/***
404
clean given mode
405
mode support
406
'enable_partial_write','accept_moving_write_buffer','auto_retry','no_auto_chain','release_buffers'
407
@function mode
408
@tparam boolean clear must be true
409
@tparam string mode
410
@param[opt] ...
411
@treturn string
412
@treturn ...
413
@usage
414
 modes = { ssl_ctx:mode('enable_partial_write','accept_moving_write_buffer','auto_retry') },
415

416
  for  i, v in ipairs(modes)
417
    print(v)
418
 end
419
 --output 'enable_partial_write','accept_moving_write_buffer','auto_retry'
420
*/
421
static int
422
openssl_ssl_ctx_mode(lua_State *L)
8✔
423
{
424
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
8✔
425
  int      mode = 0;
8✔
426
  int      ret;
427
  int      i;
428
  if (!lua_isnoneornil(L, 2)) {
8✔
429
    int clear = 0;
8✔
430
    if (lua_isboolean(L, 2)) {
8✔
431
      clear = lua_toboolean(L, 2);
8✔
432
      i = 3;
8✔
433
    } else
434
      i = 2;
×
435
    while (i <= lua_gettop(L)) {
40✔
436
      mode = mode | auxiliar_checkoption(L, i++, NULL, sMode_options, iMode_options);
32✔
437
    }
438
    if (clear != 0)
8✔
439
      mode = SSL_CTX_set_mode(ctx, mode);
4✔
440
    else
441
      mode = SSL_CTX_clear_mode(ctx, mode);
4✔
442
  } else
443
    mode = SSL_CTX_get_mode(ctx);
×
444
  ret = 0;
8✔
445
  for (i = 0; i < sizeof(iMode_options) / sizeof(int); i++) {
56✔
446
    if (mode & iMode_options[i]) {
48✔
447
      lua_pushstring(L, sMode_options[i]);
16✔
448
      ret++;
16✔
449
    }
450
  }
451
  return ret;
8✔
452
};
453

454
/***
455
get options
456
@function options
457
@treturn table string list of current options
458
*/
459

460
/***
461
set options
462
@function options
463
@tparam string option, support "microsoft_sess_id_bug", "netscape_challenge_bug",
464
"netscape_reuse_cipher_change_bug", "sslref2_reuse_cert_type_bug", "microsoft_big_sslv3_buffer",
465
"msie_sslv3_rsa_padding","ssleay_080_client_dh_bug",
466
"tls_d5_bug","tls_block_padding_bug","dont_insert_empty_fragments","all", please to see
467
ssl_options.h
468
@treturn table string list of current options after set new option
469
*/
470

471
/***
472
clear options
473
@function options
474
@tparam boolean clear set true to clear options
475
@tparam string option, support "microsoft_sess_id_bug", "netscape_challenge_bug",
476
"netscape_reuse_cipher_change_bug", "sslref2_reuse_cert_type_bug", "microsoft_big_sslv3_buffer",
477
"msie_sslv3_rsa_padding","ssleay_080_client_dh_bug",
478
"tls_d5_bug","tls_block_padding_bug","dont_insert_empty_fragments","all",  please to see
479
ssl_options.h
480
@treturn table string list of current options after clear some option
481
*/
482
static int
483
openssl_ssl_ctx_options(lua_State *L)
687✔
484
{
485
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
687✔
486
  long     options = 0;
687✔
487
  int      ret;
488
  int      i;
489
  if (!lua_isnone(L, 2)) {
687✔
490
    int top = lua_gettop(L);
40✔
491
    int clear = 0;
40✔
492
    if (lua_isboolean(L, 2)) {
40✔
493
      clear = lua_toboolean(L, 2);
8✔
494
      i = 3;
8✔
495
    } else
496
      i = 2;
32✔
497
    for (; i <= top; i++) {
84✔
498
      if (lua_isnumber(L, i))
48✔
499
        options |= (long)luaL_checkinteger(L, i);
36✔
500
      else {
501
        const char *s = luaL_checkstring(L, i);
12✔
502
        int         j;
503
        for (j = 0; ssl_options[j].name; j++) {
176✔
504
          LuaL_Enumeration e = ssl_options[j];
176✔
505
          if (strcasecmp(s, e.name) == 0) {
176✔
506
            options |= e.val;
8✔
507
            break;
8✔
508
          }
509
        }
510
      }
511
    }
512

513
    if (clear != 0)
36✔
514
      options = SSL_CTX_clear_options(ctx, options);
4✔
515
    else
516
      options = SSL_CTX_set_options(ctx, options);
32✔
517
  } else
518
    options = SSL_CTX_get_options(ctx);
647✔
519

520
  lua_newtable(L);
683✔
521
  ret = 0;
683✔
522
  for (i = 0; ssl_options[i].name; i++) {
23,905✔
523
    LuaL_Enumeration e = ssl_options[i];
23,222✔
524
    if (options & e.val) {
23,222✔
525
      lua_pushstring(L, e.name);
1,189✔
526
      ret++;
1,189✔
527
      lua_rawseti(L, -2, ret);
1,189✔
528
    }
529
  }
530
  return 1;
683✔
531
}
532

533
/***
534
get min_proto_version and max_proto_version
535
@function version
536
@treturn[1] integer min_proto_version
537
@treturn[2] integer man_proto_version
538
*/
539

540
/***
541
set min_proto_version and max_proto_version
542
@function options
543
@tparam integer min
544
@tparam integer max
545
@treturn boolean result or fail
546
*/
547
#if OPENSSL_VERSION_NUMBER > 0x10100000L
548
static int
549
openssl_ssl_ctx_version(lua_State *L)
21✔
550
{
551
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
21✔
552
  int      ret;
553
  int      minv = SSL_CTX_get_min_proto_version(ctx);
21✔
554
  int      maxv = SSL_CTX_get_max_proto_version(ctx);
21✔
555

556
  if (lua_isnone(L, 2)) {
21✔
557
    lua_pushinteger(L, minv);
558
    lua_pushinteger(L, maxv);
559
    return 2;
560
  }
561

562
  minv = luaL_optinteger(L, 2, minv);
21✔
563
  maxv = luaL_optinteger(L, 3, maxv);
21✔
564
  luaL_argcheck(L, minv <= maxv, 3, "max version can't less than min");
21✔
565

566
  ret = SSL_CTX_set_min_proto_version(ctx, minv);
21✔
567
  if (ret == 1) ret = SSL_CTX_set_min_proto_version(ctx, maxv);
21✔
568

569
  if (ret == 1) {
21✔
570
    lua_pushvalue(L, 1);
21✔
571
    return 1;
21✔
572
  }
573
  return openssl_pushresult(L, ret);
574
}
575
#endif
576

577
/***
578
get quit_shutdown is set or not
579
Normally when a SSL connection is finished, the parties must send out
580
"close notify" alert messages using ***SSL:shutdown"*** for a clean shutdown.
581
@function quiet_shutdown
582
@treturn boolean result
583
*/
584
/***
585
set quiet_shutdown
586
@function quiet_shutdown
587
@tparam boolean quiet
588
When setting the "quiet shutdown" flag to 1, ***SSL:shutdown*** will set the internal flags
589
to SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN. ***SSL:shutdown*** then behaves like
590
***SSL:set_shutdown*** called with SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN.
591
The session is thus considered to be shutdown, but no "close notify" alert
592
is sent to the peer. This behaviour violates the TLS standard.
593
The default is normal shutdown behaviour as described by the TLS standard.
594
@treturn boolean result
595
*/
596
static int
597
openssl_ssl_ctx_quiet_shutdown(lua_State *L)
26✔
598
{
599
  SSL_CTX *s = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
26✔
600
  if (lua_isnone(L, 2)) {
26✔
601
    int m = SSL_CTX_get_quiet_shutdown(s);
13✔
602
    lua_pushinteger(L, m);
13✔
603
    return 1;
13✔
604
  } else {
605
    int m = luaL_checkint(L, 2);
13✔
606
    SSL_CTX_set_quiet_shutdown(s, m);
13✔
607
    return 0;
13✔
608
  }
609
};
610

611
/***
612
set verify locations with cafile and capath
613
ssl_ctx:verify_locations specifies the locations for *ctx*, at
614
which CA certificates for verification purposes are located. The certificates
615
available via *CAfile* and *CApath* are trusted.
616
@function verify_locations
617
@tparam string cafile
618
@tparam string capath
619
@treturn boolean result
620
*/
621
static int
622
openssl_ssl_ctx_load_verify_locations(lua_State *L)
646✔
623
{
624
  SSL_CTX    *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
646✔
625
  const char *CAfile = luaL_checkstring(L, 2);
646✔
626
  const char *CApath = luaL_optstring(L, 3, NULL);
646✔
627
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
628
  int ret = !(CAfile == NULL && CApath == NULL);
430✔
629
  if (CAfile != NULL) ret = SSL_CTX_load_verify_file(ctx, CAfile);
430✔
630
  if (ret == 1 && CApath != NULL) ret = SSL_CTX_load_verify_dir(ctx, CApath);
430✔
631
#else
632
  int ret = SSL_CTX_load_verify_locations(ctx, CAfile, CApath);
216✔
633
#endif
634
  return openssl_pushresult(L, ret);
646✔
635
}
636

637
/***
638
get certificate verification store of ssl_ctx
639
@function cert_store
640
@treturn x509_store store
641
*/
642
/***
643
set or replaces then certificate verification store of ssl_ctx
644
@function cert_store
645
@tparam x509_store store
646
@treturn x509_store store
647
*/
648
static int
649
openssl_ssl_ctx_cert_store(lua_State *L)
649✔
650
{
651
#if OPENSSL_VERSION_NUMBER > 0x10002000L
652
  SSL_CTX    *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
649✔
653
  X509_STORE *store = NULL;
649✔
654
  if (lua_isnone(L, 2)) {
649✔
655
    store = SSL_CTX_get_cert_store(ctx);
13✔
656
    X509_STORE_up_ref(store);
13✔
657
    PUSH_OBJECT(store, "openssl.x509_store");
13✔
658
    return 1;
13✔
659
  } else {
660
    store = CHECK_OBJECT(2, X509_STORE, "openssl.x509_store");
636✔
661
    X509_STORE_up_ref(store);
636✔
662
    SSL_CTX_set_cert_store(ctx, store);
636✔
663
    X509_STORE_set_trust(store, 1);
636✔
664
    return 0;
636✔
665
  }
666
#else
667
  luaL_error(L, "NYI, openssl below 1.0.2 not fully support this feature");
668
  return 0;
669
#endif
670
}
671

672
#ifndef OPENSSL_NO_ENGINE
673
/***
674
set client certificate engine for SSL context
675
@function set_engine
676
@tparam openssl.engine eng engine object to use for client certificates
677
@treturn boolean result true for success
678
*/
679
static int
680
openssl_ssl_ctx_set_engine(lua_State *L)
4✔
681
{
682
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
4✔
683
  ENGINE  *eng = CHECK_OBJECT(2, ENGINE, "openssl.engine");
4✔
684
  int      ret = SSL_CTX_set_client_cert_engine(ctx, eng);
4✔
685
  return openssl_pushresult(L, ret);
4✔
686
}
687
#endif
688

689
/* ssl functions */
690
/***
691
create SSL object from SSL context
692

693
This function creates a new SSL object from an SSL context. It supports two modes:
694
1. Using a file descriptor (fd)
695
2. Using BIO objects for input/output
696

697
@function ssl
698
@tparam openssl.ssl_ctx ctx SSL context object
699
@tparam number|openssl.bio fd_or_input File descriptor or input BIO
700
@tparam[opt=false] boolean|openssl.bio server_or_output Server mode flag or output BIO
701
@treturn[1] openssl.ssl SSL object on success
702
@treturn[2] nil on error
703
@treturn[2] string error message
704
-- @see OpenSSL function: SSL_new
705
-- @see OpenSSL function: SSL_set_fd
706
-- @see OpenSSL function: SSL_set_bio
707
@usage
708
  -- Create SSL object from file descriptor
709
  local ssl_ctx = require('openssl').ssl.ctx_new('TLS')
710
  local fd = 5  -- Assume fd 5 is a connected socket
711
  local ssl = ssl_ctx:ssl(fd)
712

713
  -- Create SSL server object from BIO
714
  local bio_in = require('openssl').bio.new('mem')
715
  local bio_out = require('openssl').bio.new('mem')
716
  local ssl = ssl_ctx:ssl(bio_in, bio_out, true)  -- true for server mode
717
*/
718
static int
719
openssl_ssl_ctx_new_ssl(lua_State *L)
1,244✔
720
{
721
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
1,244✔
722
  int      server = 0;
1,244✔
723
  int      mode_idx = 2;
1,244✔
724
  SSL     *ssl = SSL_new(ctx);
1,244✔
725
  int      ret = 1;
1,244✔
726

727
  if (auxiliar_getclassudata(L, "openssl.bio", 2)) {
1,244✔
728
    BIO *bi = CHECK_OBJECT(2, BIO, "openssl.bio");
1,240✔
729
    BIO *bo = bi;
1,240✔
730

731
    /* avoid bi be gc */
732
    BIO_up_ref(bi);
1,240✔
733

734
    if (auxiliar_getclassudata(L, "openssl.bio", 3)) {
1,240✔
735
      bo = CHECK_OBJECT(3, BIO, "openssl.bio");
8✔
736
      mode_idx = 4;
8✔
737
    } else
738
      mode_idx = 3;
1,232✔
739

740
    /* avoid bo be gc */
741
    BIO_up_ref(bo);
1,240✔
742

743
#if OPENSSL_VERSION_NUMBER > 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
744
    SSL_set0_rbio(ssl, bi);
1,230✔
745
    SSL_set0_wbio(ssl, bo);
1,230✔
746
#else
747
    SSL_set_bio(ssl, bi, bo);
10✔
748
#endif
749
    ret = 1;
1,240✔
750
  } else if (lua_isnumber(L, 2)) {
4✔
751
    ret = SSL_set_fd(ssl, luaL_checkint(L, 2));
×
752
    mode_idx = 3;
×
753
  }
754

755
  if (ret == 1 && !lua_isnone(L, mode_idx)) {
1,244✔
756
    server = lua_isnil(L, mode_idx) ? 0 : auxiliar_checkboolean(L, mode_idx);
1,224✔
757
  }
758

759
  if (ret == 1) {
1,244✔
760
    if (server)
1,244✔
761
      SSL_set_accept_state(ssl);
612✔
762
    else
763
      SSL_set_connect_state(ssl);
632✔
764

765
    PUSH_OBJECT(ssl, "openssl.ssl");
1,244✔
766
    openssl_newvalue(L, ssl);
1,244✔
767

768
    /* ref to ctx */
769
    lua_pushvalue(L, 1);
1,244✔
770
    openssl_valueset(L, ssl, "ctx");
1,244✔
771
  } else {
772
    SSL_free(ssl);
×
773
    openssl_freevalue(L, ssl);
×
774
    return openssl_pushresult(L, ret);
×
775
  }
776
  return 1;
1,244✔
777
}
778

779
/***
780
create bio object
781
@function bio
782
@tparam string host_addr format like 'host:port'
783
@tparam[opt=false] boolean server, true listen at host_addr,false connect to host_addr
784
@tparam[opt=true] boolean autoretry ssl operation autoretry mode
785
@treturn openssl.bio bio object
786
*/
787
static int
788
openssl_ssl_ctx_new_bio(lua_State *L)
909✔
789
{
790
  SSL_CTX    *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
909✔
791
  const char *host_addr = luaL_checkstring(L, 2);
909✔
792
  int         server = lua_isnone(L, 3) ? 0 : auxiliar_checkboolean(L, 3);
909✔
793
  int         autoretry = lua_isnone(L, 4) ? 1 : auxiliar_checkboolean(L, 4);
909✔
794

795
  BIO *bio = server ? BIO_new_ssl(ctx, 0) : BIO_new_ssl_connect(ctx);
909✔
796
  if (bio) {
909✔
797
    int ret = 0;
909✔
798
    if (autoretry) {
909✔
799
      SSL *ssl = NULL;
909✔
800
      ret = BIO_get_ssl(bio, &ssl);
909✔
801
      if (ret == 1) SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
909✔
802
    }
803
    if (server) {
909✔
804
      BIO *acpt = BIO_new_accept((char *)host_addr);
9✔
805
      BIO_set_accept_bios(acpt, bio);
9✔
806
      bio = acpt;
9✔
807
    } else {
808
      ret = BIO_set_conn_hostname(bio, host_addr);
900✔
809
    }
810
    if (ret == 1) {
909✔
811
      PUSH_OBJECT(bio, "openssl.bio");
909✔
812
      return 1;
909✔
813
    } else
814
      return openssl_pushresult(L, ret);
×
815
  } else {
816
    BIO_free(bio);
×
817
    bio = NULL;
×
818
    return 0;
×
819
  }
820
}
821

822
/***
823
get verify depth when cert chain veirition
824
@function verify_depth
825
@treturn number depth
826
*/
827
/***
828
set verify depth when cert chain veirition
829
@function verify_depth
830
@tparam number depth
831
@treturn number depth
832
*/
833
static int
834
openssl_ssl_ctx_verify_depth(lua_State *L)
10✔
835
{
836
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
10✔
837
  int      depth;
838
  if (!lua_isnone(L, 2)) {
10✔
839
    depth = luaL_checkint(L, 2);
10✔
840
    SSL_CTX_set_verify_depth(ctx, depth);
10✔
841
  }
842
  depth = SSL_CTX_get_verify_depth(ctx);
10✔
843
  lua_pushinteger(L, depth);
10✔
844
  return 1;
10✔
845
}
846

847
static const int iVerifyMode_Options[] = { SSL_VERIFY_NONE,
848
                                           SSL_VERIFY_PEER,
849
                                           SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
850
                                           SSL_VERIFY_CLIENT_ONCE,
851
                                           0 };
852

853
static const char *sVerifyMode_Options[] = { "none",
854
                                             "peer",
855
                                             "fail", /* fail_if_no_peer_cert */
856
                                             "once",
857
                                             NULL };
858

859
/***
860
get verify_mode, return number mode and all string modes list
861
@function verify_mode
862
@treturn number mode_code
863
@return ...
864
  none: not verify client cert
865
  peer: verify client cert
866
  fail: if client not have cert, will failure
867
  once: verify client only once.
868
@usage
869
  mode = {ctx:verify_mode()}
870
  print('integer mode',mode[1])
871
  for i=2, #mode then
872
    print('string mode:'..mode[i])
873
  end
874
*/
875
/***
876
set ssl verify mode and callback
877
@function verify_mode
878
@tparam number mode, mode set to ctx, must be ssl.none or ssl.peer, and ssl.peer support combine
879
with ssl.fail or ssl.once
880
@tparam[opt=nil] function ssl verify callback in lua function, not give will use default openssl
881
callback, when mode is 'none', will be ignore this verify_cb must be boolean function(verifyarg)
882
prototype, return true to continue or false to end ssl handshake verifyarg has field 'error',
883
'error_string','error_depth','current_cert', and 'preverify_ok'
884
@treturn boolean result
885
*/
886
static int
887
openssl_ssl_ctx_verify_mode(lua_State *L)
1,271✔
888
{
889
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
1,271✔
890
  if (lua_gettop(L) > 1) {
1,271✔
891
    int mode = luaL_checkint(L, 2);
1,261✔
892
    luaL_argcheck(
1,261✔
893
      L,
894
      mode == SSL_VERIFY_NONE
895
        || (mode & ~(SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_CLIENT_ONCE))
896
             == 0,
897
      2,
898
      "must be none or peer(combined with fail, once or none");
899

900
    luaL_argcheck(L, lua_isnone(L, 3) || lua_isfunction(L, 3), 3, "must be callback function");
1,261✔
901

902
    if (lua_isfunction(L, 3)) {
1,261✔
903
      lua_pushvalue(L, 3);
622✔
904
      openssl_valueset(L, ctx, "verify_cb");
622✔
905
      SSL_CTX_set_verify(ctx, mode, openssl_verify_cb);
622✔
906
    } else {
907
      lua_pushnil(L);
639✔
908
      openssl_valueset(L, ctx, "verify_cb");
639✔
909
      SSL_CTX_set_verify(ctx, mode, openssl_verify_cb);
639✔
910
    }
911
    return 0;
1,261✔
912
  } else {
913
    int i = 0;
10✔
914
    int mode = SSL_CTX_get_verify_mode(ctx);
10✔
915
    lua_pushinteger(L, mode);
10✔
916
    i += 1;
10✔
917

918
    if (mode == SSL_VERIFY_NONE) {
10✔
919
      lua_pushstring(L, "none");
4✔
920
      i += 1;
4✔
921
    } else {
922
      if (mode & SSL_VERIFY_PEER) {
6✔
923
        lua_pushstring(L, "peer");
6✔
924
        i += 1;
6✔
925

926
        if (mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
6✔
927
          lua_pushstring(L, "fail");
6✔
928
          i += 1;
6✔
929
        }
930
        if (mode & SSL_VERIFY_CLIENT_ONCE) {
6✔
931
          lua_pushstring(L, "once");
×
932
          i += 1;
×
933
        }
934
      }
935
    }
936
    return i;
10✔
937
  }
938
}
939

940
/***
941
set certificate verify callback function
942
@function set_cert_verify
943
@tparam[opt] function cert_verify_cb with boolean function(verifyargs) prototype, if nil or none
944
will use openssl default callback verifyargs has field 'error',
945
'error_string','error_depth','current_cert'
946
@treturn various return value
947
*/
948
/***
949
set certificate verify options
950
@function set_cert_verify
951
@tparam table verify_cb_flag support field always_continue with boolean value and verify_depth with
952
number value.
953
@treturn boolean result true for success
954
*/
955
static int
956
openssl_ssl_ctx_set_cert_verify(lua_State *L)
635✔
957
{
958
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
635✔
959
  luaL_argcheck(L,
635✔
960
                lua_isnone(L, 2) || lua_isfunction(L, 2) || lua_istable(L, 2),
961
                2,
962
                "need function or table contains flags");
963
  if (lua_istable(L, 2)) {
635✔
964
    lua_pushvalue(L, 2);
21✔
965
    openssl_valueset(L, ctx, "verify_cb_flags");
21✔
966
    SSL_CTX_set_cert_verify_callback(ctx, openssl_cert_verify_cb, openssl_mainthread(L));
21✔
967
  } else if (lua_isfunction(L, 2)) {
614✔
968
    lua_pushvalue(L, 2);
606✔
969
    openssl_valueset(L, ctx, "cert_verify_cb");
606✔
970
    SSL_CTX_set_cert_verify_callback(ctx, openssl_cert_verify_cb, openssl_mainthread(L));
606✔
971
  } else
972
    SSL_CTX_set_cert_verify_callback(ctx, NULL, NULL);
8✔
973
  return 0;
635✔
974
}
975

976
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
977
/***
978
set the list of client ALPN protocols available to be negotiated by the server
979
@function set_alpn_protos
980
@tparam table protos the protocol list
981
@treturn various return value
982
*/
983
static int
984
openssl_ssl_ctx_set_alpn_protos(lua_State *L)
600✔
985
{
986
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
600✔
987

988
  if (lua_istable(L, 2)) {
600✔
989
    size_t         proto_list_len = 0;
600✔
990
    size_t         proto_list_size = 256;
600✔
991
    unsigned char *proto_list = malloc(proto_list_size);
600✔
992
    size_t         proto_len;
993
    const char    *proto;
994
    int            i;
995
    int            l = lua_rawlen(L, 2);
600✔
996
    char          *err = NULL;
600✔
997

998
    for (i = 1; i <= l; i++) {
1,800✔
999
      lua_rawgeti(L, 2, i);
1,200✔
1000
      proto = lua_tolstring(L, -1, &proto_len);
1,200✔
1001
      lua_pop(L, 1);
1,200✔
1002
      if (proto == NULL) {
1,200✔
1003
        err = "invalid protocol value";
×
1004
        break;
×
1005
      }
1006
      if (proto_len > 255) {
1,200✔
1007
        err = "too long protocol value";
×
1008
        break;
×
1009
      }
1010
      if (proto_list_len + proto_len >= proto_list_size) {
1,200✔
1011
        unsigned char *tmp;
1012
        do {
1013
          if (proto_list_size > (size_t)(-1) / 2) {
×
1014
            free(proto_list);
×
1015
            return luaL_error(L, "protocol list too large");
×
1016
          }
1017
          proto_list_size = proto_list_size * 2;
×
1018
        } while (proto_list_len + proto_len >= proto_list_size);
×
1019
        tmp = realloc(proto_list, proto_list_size);
×
1020
        if (tmp == NULL) {
×
1021
          free(proto_list);
×
1022
          return luaL_error(L, "fail to allocate protocol list");
×
1023
        }
1024
        proto_list = tmp;
×
1025
      }
1026
      if (proto_list == NULL) {
1,200✔
1027
        err = "fail to allocate protocol list";
×
1028
        break;
×
1029
      }
1030
      proto_list[proto_list_len++] = proto_len;
1,200✔
1031
      memcpy(proto_list + proto_list_len, proto, proto_len);
1,200✔
1032
      proto_list_len += proto_len;
1,200✔
1033
    }
1034
    if (err == NULL) {
600✔
1035
      if (SSL_CTX_set_alpn_protos(ctx, proto_list, proto_list_len) != 0) {
600✔
1036
        err = "fail to set ALPN protocols";
×
1037
      }
1038
    }
1039
    free(proto_list);
600✔
1040
    if (err != NULL) return luaL_error(L, err);
600✔
1041
  } else
1042
    return luaL_error(L, "table expected");
×
1043

1044
  return 0;
600✔
1045
}
1046

1047
static int
1048
openssl_mem_search(const unsigned char *mem, int memsize, const unsigned char *part, int partsize)
300✔
1049
{
1050
  int mempos, partpos;
1051
  for (mempos = 0; mempos <= memsize - partsize; mempos++) {
600✔
1052
    for (partpos = 0; partpos < partsize; partpos++)
3,000✔
1053
      if (mem[mempos + partpos] != part[partpos]) break;
2,700✔
1054
    if (partpos == partsize) return mempos;
600✔
1055
  }
1056
  return -1;
×
1057
}
1058

1059
static int
1060
openssl_alpn_select_cb(SSL                  *ssl,
300✔
1061
                       const unsigned char **out,
1062
                       unsigned char        *outlen,
1063
                       const unsigned char  *in,
1064
                       unsigned int          inlen,
1065
                       void                 *arg)
1066
{
1067
  lua_State *L = (lua_State *)arg;
300✔
1068
  int        result = SSL_TLSEXT_ERR_ALERT_FATAL;
300✔
1069
  SSL_CTX   *ctx = SSL_get_SSL_CTX(ssl);
300✔
1070

1071
  if (L) {
300✔
1072
    openssl_valueget(L, ctx, "alpn_select_cb");
300✔
1073
    if (lua_isfunction(L, -1)) {
300✔
1074
      int index = 1;
300✔
1075
      int pos = 0;
300✔
1076
      // the function is on the stack, adding the table of protocols to be selected
1077
      lua_newtable(L);
300✔
1078
      while (pos < inlen) {
900✔
1079
        int len = in[pos++];
600✔
1080
        lua_pushlstring(L, (const char *)in + pos, len);
600✔
1081
        lua_rawseti(L, -2, index++);
600✔
1082
        pos += len;
600✔
1083
      }
1084
      if (lua_pcall(L, 1, 1, 0) == 0) {
300✔
1085
        size_t      sellen;
1086
        const char *selected = lua_tolstring(L, -1, &sellen);
300✔
1087
        if (selected) {
300✔
1088
          pos = openssl_mem_search(in, inlen, (const unsigned char *)selected, sellen);
300✔
1089
          if (pos != -1) {
300✔
1090
            *out = in + pos;
300✔
1091
            *outlen = sellen;
300✔
1092
            result = SSL_TLSEXT_ERR_OK;
300✔
1093
          }
1094
        }
1095
      } else {
1096
        fprintf(stderr, "alpn select callback error: %s\n", lua_tostring(L, -1));
×
1097
      }
1098
      lua_pop(L, 1);
300✔
1099
    }
1100
  }
1101
  return result;
300✔
1102
}
1103

1104
/***
1105
set ALPN server protocol selection callback to select which protocol to use for the incoming
1106
connection
1107
@function set_alpn_select_cb
1108
@tparam[opt] function alpn_select_cb callback that receive the prototype list as a table and return
1109
the one selected as a string
1110
@treturn various return value
1111
*/
1112
static int
1113
openssl_ssl_ctx_set_alpn_select_cb(lua_State *L)
6✔
1114
{
1115
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
6✔
1116

1117
  if (lua_isfunction(L, 2)) {
6✔
1118
    lua_pushvalue(L, 2);
6✔
1119
    openssl_valueset(L, ctx, "alpn_select_cb");
6✔
1120
    SSL_CTX_set_alpn_select_cb(ctx, openssl_alpn_select_cb, openssl_mainthread(L));
6✔
1121
  } else
1122
    SSL_CTX_set_alpn_select_cb(ctx, NULL, NULL);
×
1123

1124
  return 0;
6✔
1125
}
1126
#endif
1127

1128
#if OPENSSL_VERSION_NUMBER < 0x10100000L
1129
static DH *
1130
tmp_dh_callback(SSL *ssl, int is_export, int keylength)
1131
{
1132
  DH        *dh_tmp = NULL;
1133
  SSL_CTX   *ctx = SSL_get_SSL_CTX(ssl);
1134
  lua_State *L = SSL_CTX_get_app_data(ctx);
1135

1136
  int type = openssl_valuegeti(L, ctx, SSL_CTX_TEMP_DH);
1137
  if (type == LUA_TFUNCTION) {
1138
    int ret;
1139
    /* top is callback function */
1140
    /* Invoke the callback */
1141
    lua_pushboolean(L, is_export);
1142
    lua_pushnumber(L, keylength);
1143
    ret = lua_pcall(L, 2, 1, 0);
1144
    if (ret == 0) {
1145
      BIO *bio;
1146
      /* Load parameters from returned value */
1147
      if (lua_type(L, -1) != LUA_TSTRING) {
1148
        lua_pop(L, 2); /* Remove values from stack */
1149
        return NULL;
1150
      }
1151
      bio = BIO_new_mem_buf((void *)lua_tostring(L, -1), lua_rawlen(L, -1));
1152
      if (bio) {
1153
        dh_tmp = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1154
        BIO_free(bio);
1155
      }
1156
    } else {
1157
      lua_error(L);
1158
    }
1159

1160
    lua_pop(L, 2); /* Remove values from stack */
1161
    return dh_tmp;
1162
  }
1163
  lua_pop(L, 1);
1164
  return NULL;
1165
}
1166

1167
static RSA *
1168
tmp_rsa_callback(SSL *ssl, int is_export, int keylength)
1169
{
1170
  RSA       *rsa_tmp = NULL;
1171
  SSL_CTX   *ctx = SSL_get_SSL_CTX(ssl);
1172
  lua_State *L = SSL_CTX_get_app_data(ctx);
1173
  int        type = openssl_valuegeti(L, ctx, SSL_CTX_TEMP_RSA);
1174
  if (type == LUA_TFUNCTION) {
1175
    int ret;
1176
    /* top is callback function */
1177
    /* Invoke the callback */
1178
    lua_pushboolean(L, is_export);
1179
    lua_pushnumber(L, keylength);
1180
    ret = lua_pcall(L, 2, 1, 0);
1181
    if (ret == 0) {
1182
      BIO *bio;
1183
      /* Load parameters from returned value */
1184
      if (lua_type(L, -1) != LUA_TSTRING) {
1185
        lua_pop(L, 2); /* Remove values from stack */
1186
        return NULL;
1187
      }
1188
      bio = BIO_new_mem_buf((void *)lua_tostring(L, -1), lua_rawlen(L, -1));
1189
      if (bio) {
1190
        rsa_tmp = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL);
1191
        BIO_free(bio);
1192
      }
1193
    } else {
1194
      lua_error(L);
1195
    }
1196

1197
    lua_pop(L, 2); /* Remove values from stack */
1198
    return rsa_tmp;
1199
  }
1200
  lua_pop(L, 1);
1201
  return NULL;
1202
}
1203

1204
static EC_KEY *
1205
tmp_ecdh_callback(SSL *ssl, int is_export, int keylength)
1206
{
1207
  EC_KEY    *ec_tmp = NULL;
1208
  SSL_CTX   *ctx = SSL_get_SSL_CTX(ssl);
1209
  lua_State *L = SSL_CTX_get_app_data(ctx);
1210
  int        type = openssl_valuegeti(L, ctx, SSL_CTX_TEMP_ECDH);
1211
  if (type == LUA_TFUNCTION) {
1212
    int ret;
1213
    /* top is callback function */
1214
    /* Invoke the callback */
1215
    lua_pushboolean(L, is_export);
1216
    lua_pushnumber(L, keylength);
1217
    ret = lua_pcall(L, 2, 1, 0);
1218
    if (ret == 0) {
1219
      BIO *bio;
1220
      /* Load parameters from returned value */
1221
      if (lua_type(L, -1) != LUA_TSTRING) {
1222
        lua_pop(L, 2); /* Remove values from stack */
1223
        return NULL;
1224
      }
1225
      bio = BIO_new_mem_buf((void *)lua_tostring(L, -1), lua_rawlen(L, -1));
1226
      if (bio) {
1227
        ec_tmp = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL);
1228
        BIO_free(bio);
1229
      }
1230
    } else {
1231
      lua_error(L);
1232
    }
1233

1234
    lua_pop(L, 2); /* Remove values from stack */
1235
    return ec_tmp;
1236
  }
1237
  lua_pop(L, 1);
1238
  return NULL;
1239
}
1240

1241
/***
1242
set temp callback
1243
@function set_tmp
1244
@tparam string keytype, 'dh','ecdh',or 'rsa'
1245
@tparam function tmp_cb
1246
@param[opt] vararg
1247
@treturn userdata object created
1248
*/
1249
/***
1250
set tmp key content pem format
1251
@function set_tmp
1252
@tparam string keytype, 'dh','ecdh',or 'rsa'
1253
@tparam[opt] string private key file
1254
@treturn boolean result true for success
1255
*/
1256

1257
static int
1258
openssl_ssl_ctx_set_tmp(lua_State *L)
1259
{
1260
  SSL_CTX           *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
1261
  static const char *which[] = { "dh", "rsa", "ecdh", NULL };
1262

1263
  int nwhich = luaL_checkoption(L, 2, "rsa", which);
1264

1265
  if (lua_isfunction(L, 3)) {
1266
    lua_pushvalue(L, 3);
1267
    /* set callback function */
1268
    switch (nwhich) {
1269
    case 0:
1270
      openssl_valueseti(L, ctx, SSL_CTX_TEMP_DH);
1271
      SSL_CTX_set_tmp_dh_callback(ctx, tmp_dh_callback);
1272
      break;
1273
    case 1:
1274
      openssl_valueseti(L, ctx, SSL_CTX_TEMP_RSA);
1275
      SSL_CTX_set_tmp_rsa_callback(ctx, tmp_rsa_callback);
1276
      break;
1277
    case 2:
1278
      openssl_valueseti(L, ctx, SSL_CTX_TEMP_ECDH);
1279
      SSL_CTX_set_tmp_ecdh_callback(ctx, tmp_ecdh_callback);
1280
      break;
1281
    }
1282
    lua_pushboolean(L, 1);
1283
    return 1;
1284
  } else if (lua_isuserdata(L, 3)) {
1285
    luaL_argerror(L, 3, "userdata arg NYI");
1286
  } else {
1287
    int  ret;
1288
    BIO *bio = lua_isstring(L, 3) ? load_bio_object(L, 3) : NULL;
1289
    switch (nwhich) {
1290
    case 0: {
1291
      DH *dh = NULL;
1292
      if (bio) {
1293
        dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1294
        BIO_free(bio);
1295
      } else {
1296
        int bits = 1024;
1297
        int generator = 2;
1298
        dh = DH_new();
1299
        ret = DH_generate_parameters_ex(dh, bits, generator, NULL);
1300
        if (ret == 1) {
1301
          ret = DH_generate_key(dh);
1302
        }
1303
        if (ret != 1) {
1304
          DH_free(dh);
1305
          dh = NULL;
1306
        }
1307
      }
1308
      if (dh) {
1309
        ret = SSL_CTX_set_tmp_dh(ctx, dh);
1310
        if (ret)
1311
          PUSH_OBJECT(dh, "openssl.dh");
1312
        else {
1313
          DH_free(dh);
1314
          lua_pushnil(L);
1315
        }
1316
        return 1;
1317
      } else
1318
        luaL_error(L, "load or generate new tmp dh fail");
1319
    } break;
1320
    case 1: {
1321
      RSA *rsa = NULL;
1322
      if (bio) {
1323
        rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL);
1324
        BIO_free(bio);
1325
      } else {
1326
        BIGNUM *e = BN_new();
1327
        rsa = RSA_new();
1328
        BN_set_word(e, RSA_F4);
1329
        ret = RSA_generate_key_ex(rsa, 2048, e, NULL);
1330
        BN_free(e);
1331
        if (ret != 0) {
1332
          RSA_free(rsa);
1333
          rsa = NULL;
1334
        }
1335
      }
1336

1337
      if (rsa) {
1338
        ret = SSL_CTX_set_tmp_rsa(ctx, rsa);
1339
        if (ret) {
1340
          PUSH_OBJECT(rsa, "openssl.rsa");
1341
        } else {
1342
          RSA_free(rsa);
1343
          lua_pushnil(L);
1344
        }
1345
        return 1;
1346
      } else
1347
        luaL_error(L, "load or generate new tmp rsa fail");
1348
    } break;
1349
    case 2: {
1350
      int       nid = NID_undef;
1351
      EC_GROUP *g = NULL;
1352
      EC_KEY   *ec = NULL;
1353

1354
      if (lua_isstring(L, 3)) {
1355
        nid = OBJ_txt2nid(lua_tostring(L, 3));
1356
        if (nid != NID_undef) {
1357
          BIO_free(bio);
1358
          bio = NULL;
1359
        }
1360
      } else {
1361
        nid = OBJ_txt2nid("prime256v1");
1362
      }
1363
      if (nid != NID_undef) g = EC_GROUP_new_by_curve_name(nid);
1364

1365
      if (bio) {
1366
        ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL);
1367
        BIO_free(bio);
1368
      } else if (g) {
1369
        ec = EC_KEY_new();
1370
        EC_KEY_set_group(ec, g);
1371
        EC_GROUP_free(g);
1372
        ret = EC_KEY_generate_key(ec);
1373
        if (ret != 1) {
1374
          EC_KEY_free(ec);
1375
          ec = NULL;
1376
        }
1377
      }
1378

1379
      if (ec) {
1380
        ret = SSL_CTX_set_tmp_ecdh(ctx, ec);
1381
        if (ret) {
1382
          PUSH_OBJECT(ec, "openssl.ec_key");
1383
        } else {
1384
          EC_KEY_free(ec);
1385
          lua_pushnil(L);
1386
        }
1387
        return 1;
1388
      } else
1389
        luaL_error(L, "load or generate new tmp ec_key fail");
1390
    } break;
1391
    }
1392
  }
1393

1394
  return openssl_pushresult(L, 0);
1395
}
1396
#endif
1397

1398
static int
1399
tlsext_servername_callback(SSL *ssl, int *ad, void *arg)
621✔
1400
{
1401
  SSL_CTX    *newctx = NULL;
621✔
1402
  SSL_CTX    *ctx = arg;
621✔
1403
  lua_State  *L = SSL_CTX_get_app_data(ctx);
621✔
1404
  const char *name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
621✔
1405
  (void)ad;
1406

1407
  /* No name, use default context */
1408
  if (!name) return SSL_TLSEXT_ERR_NOACK;
621✔
1409

1410
  /* Search for the name in the map */
1411
  openssl_valueget(L, ctx, "tlsext_servername");
16✔
1412
  if (lua_istable(L, -1)) {
16✔
1413
    lua_getfield(L, -1, name);
13✔
1414
    newctx = GET_OBJECT(-1, SSL_CTX, "openssl.ssl_ctx");
13✔
1415
    lua_pop(L, 2);
13✔
1416
  } else {
1417
    lua_pushstring(L, name);
3✔
1418
    if (lua_pcall(L, 1, 1, 0) == 0) {
3✔
1419
      newctx = GET_OBJECT(-1, SSL_CTX, "openssl.ssl_ctx");
3✔
1420
    } else
1421
      return lua_error(L);
×
1422
  }
1423
  if (newctx) {
16✔
1424
    SSL_set_SSL_CTX(ssl, newctx);
16✔
1425
    return SSL_TLSEXT_ERR_OK;
16✔
1426
  }
1427

1428
  return SSL_TLSEXT_ERR_ALERT_FATAL;
×
1429
}
1430

1431
/***
1432
set servername callback
1433
@function set_servername_callback
1434
@tparam table|function info `function(name) return ctx end` or table
1435
@treturn ssl_ctx|fail
1436
*/
1437
static int
1438
openssl_ssl_ctx_set_servername_callback(lua_State *L)
14✔
1439
{
1440
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
14✔
1441
  luaL_argcheck(L, lua_istable(L, 2) || lua_isfunction(L, 2), 2, "must be table or function");
14✔
1442

1443
  lua_pushvalue(L, 2);
14✔
1444
  openssl_valueset(L, ctx, "tlsext_servername");
14✔
1445
  SSL_CTX_set_tlsext_servername_callback(ctx, tlsext_servername_callback);
14✔
1446
  SSL_CTX_set_tlsext_servername_arg(ctx, ctx);
14✔
1447

1448
  return 0;
14✔
1449
}
1450

1451
/***
1452
set session callback
1453
@function set_session_callback
1454
@tparam function new
1455
@tparam function get
1456
@tparam function remove
1457
@treturn various return value
1458
*/
1459

1460
static int
1461
openssl_add_session(SSL *ssl, SSL_SESSION *session)
47✔
1462
{
1463
  int        ret;
1464
  SSL_CTX   *ctx = SSL_get_SSL_CTX(ssl);
47✔
1465
  lua_State *L = SSL_CTX_get_app_data(ctx);
47✔
1466

1467
  openssl_valuegeti(L, ctx, SSL_CTX_SESSION_ADD);
47✔
1468
  SSL_up_ref(ssl);
47✔
1469
  PUSH_OBJECT(ssl, "openssl.ssl");
47✔
1470
  openssl_newvalue(L, ssl);
47✔
1471
  PUSH_OBJECT(session, "openssl.ssl_session");
47✔
1472

1473
  ret = lua_pcall(L, 2, 1, 0);
47✔
1474
  if (ret != LUA_OK) {
47✔
1475
    fprintf(stderr, "add session callback error: %s\n", lua_tostring(L, -1));
×
1476
    ret = 0;
×
1477
  } else
1478
    ret = lua_isboolean(L, -1) ? lua_toboolean(L, -1) : lua_tointeger(L, -1);
47✔
1479

1480
  lua_pop(L, 1);
47✔
1481
  return ret;
47✔
1482
}
1483

1484
static SSL_SESSION *
1485
openssl_get_session(SSL *ssl, CONSTIFY_OPENSSL unsigned char *id, int idlen, int *do_copy)
4✔
1486
{
1487
  int          ret;
1488
  SSL_CTX     *ctx = SSL_get_SSL_CTX(ssl);
4✔
1489
  lua_State   *L = SSL_CTX_get_app_data(ctx);
4✔
1490
  SSL_SESSION *session = NULL;
4✔
1491

1492
  openssl_valuegeti(L, ctx, SSL_CTX_SESSION_GET);
4✔
1493
  SSL_up_ref(ssl);
4✔
1494
  PUSH_OBJECT(ssl, "openssl.ssl");
4✔
1495
  openssl_newvalue(L, ssl);
4✔
1496
  lua_pushlstring(L, (const char *)id, idlen);
4✔
1497

1498
  ret = lua_pcall(L, 2, 1, 0);
4✔
1499
  if (ret != LUA_OK) {
4✔
1500
    fprintf(stderr, "get session callback error: %s\n", lua_tostring(L, -1));
×
1501
    lua_pop(L, 1);
×
1502
    return NULL;
×
1503
  }
1504
  if (lua_isstring(L, -1)) {
4✔
1505
    size_t               size = 0;
×
1506
    const unsigned char *p = (const unsigned char *)lua_tolstring(L, -1, &size);
×
1507
    *do_copy = 0;
×
1508
    session = d2i_SSL_SESSION(NULL, &p, (int)size);
×
1509
  } else if ((session = GET_OBJECT(-1, SSL_SESSION, "openssl.ssl_session")) != NULL) {
4✔
1510
    *do_copy = 1;
4✔
1511
  } else if (lua_type(L, -1) != LUA_TNIL) {
×
1512
    fprintf(stderr,
×
1513
            "get session callback return unaccpet value: (type=%s)%s\n",
1514
            luaL_typename(L, -1),
1515
            lua_tostring(L, -1));
1516
  }
1517
  lua_pop(L, 1);
4✔
1518
  return session;
4✔
1519
}
1520

1521
static void
1522
openssl_del_session(SSL_CTX *ctx, SSL_SESSION *session)
16✔
1523
{
1524
  int          ret;
1525
  unsigned int len = 0;
16✔
1526
  ;
1527
  const unsigned char *id = NULL;
16✔
1528
  lua_State           *L = SSL_CTX_get_app_data(ctx);
16✔
1529

1530
  openssl_valuegeti(L, ctx, SSL_CTX_SESSION_DEL);
16✔
1531

1532
  id = SSL_SESSION_get_id(session, &len);
16✔
1533
  lua_pushlstring(L, (const char *)id, len);
16✔
1534

1535
  ret = lua_pcall(L, 1, 0, 0);
16✔
1536
  if (ret != LUA_OK) {
16✔
1537
    fprintf(stderr, "del session callback error: %s\n", lua_tostring(L, -1));
×
1538
    lua_pop(L, 1);
×
1539
  }
1540
}
16✔
1541

1542
static int
1543
openssl_ssl_ctx_set_session_callback(lua_State *L)
28✔
1544
{
1545
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
28✔
1546
  if (!lua_isnoneornil(L, 2)) {
28✔
1547
    luaL_checktype(L, 2, LUA_TFUNCTION);
28✔
1548
    lua_pushvalue(L, 2);
28✔
1549
    openssl_valueseti(L, ctx, SSL_CTX_SESSION_ADD);
28✔
1550
    SSL_CTX_sess_set_new_cb(ctx, openssl_add_session);
28✔
1551
  }
1552
  if (!lua_isnoneornil(L, 3)) {
28✔
1553
    luaL_checktype(L, 3, LUA_TFUNCTION);
28✔
1554
    lua_pushvalue(L, 3);
28✔
1555
    openssl_valueseti(L, ctx, SSL_CTX_SESSION_GET);
28✔
1556
    SSL_CTX_sess_set_get_cb(ctx, openssl_get_session);
28✔
1557
  }
1558
  if (!lua_isnoneornil(L, 4)) {
28✔
1559
    luaL_checktype(L, 4, LUA_TFUNCTION);
28✔
1560
    lua_pushvalue(L, 4);
28✔
1561
    openssl_valueseti(L, ctx, SSL_CTX_SESSION_DEL);
28✔
1562
    SSL_CTX_sess_set_remove_cb(ctx, openssl_del_session);
28✔
1563
  }
1564
  lua_pushvalue(L, 1);
28✔
1565
  return 1;
28✔
1566
}
1567

1568
/***
1569
flush sessions
1570
@function flush
1571
@treturn various return value
1572
*/
1573
static int
1574
openssl_ssl_ctx_flush_sessions(lua_State *L)
4✔
1575
{
1576
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
4✔
1577
  long     tm = luaL_checkinteger(L, 2);
4✔
1578
#if OPENSSL_VERSION_NUMBER >= 0x30400000L && !defined(LIBRESSL_VERSION_NUMBER)
1579
  /* Use SSL_CTX_flush_sessions_ex for OpenSSL 3.4+ (Y2038-safe) */
1580
  SSL_CTX_flush_sessions_ex(ctx, (time_t)tm);
1581
#else
1582
  SSL_CTX_flush_sessions(ctx, tm);
4✔
1583
#endif
1584
  return 0;
4✔
1585
}
1586

1587
/***
1588
set ssl session
1589
@function sessions
1590
@treturn various return value
1591
*/
1592
static int
1593
openssl_ssl_ctx_sessions(lua_State *L)
12✔
1594
{
1595
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
12✔
1596
  if (lua_isstring(L, 2)) {
12✔
1597
    size_t         s;
1598
    unsigned char *sid_ctx = (unsigned char *)luaL_checklstring(L, 2, &s);
4✔
1599
    int            ret = SSL_CTX_set_session_id_context(ctx, sid_ctx, s);
4✔
1600
    return openssl_pushresult(L, ret);
4✔
1601
  } else {
1602
    SSL_SESSION *s = CHECK_OBJECT(2, SSL_SESSION, "openssl.ssl_session");
8✔
1603
    int          add = lua_isnone(L, 3) ? 1 : auxiliar_checkboolean(L, 3);
8✔
1604

1605
    if (add)
8✔
1606
      add = SSL_CTX_add_session(ctx, s);
4✔
1607
    else
1608
      add = SSL_CTX_remove_session(ctx, s);
4✔
1609
    return openssl_pushresult(L, add);
8✔
1610
  }
1611
}
1612

1613
/***
1614
get current session cache mode
1615
@function session_cache_mode
1616
@treturn table modes as array, mode is 'no_auto_clear','server','client','both','off'
1617
*/
1618

1619
/***
1620
set session cache mode,and return old mode
1621
@function session_cache_mode
1622
@tparam string mode support 'no_auto_clear','server','client','both','off',
1623
@treturn table old modes as array
1624
*/
1625
static int
1626
openssl_session_cache_mode(lua_State *L)
76✔
1627
{
1628
  static const char *smode[] = { "off",
1629
                                 "client",
1630
                                 "server",
1631
                                 "both",
1632
                                 "no_auto_clear",
1633
                                 "no_internal_lookup",
1634
                                 "no_internal_store",
1635
                                 "no_internal",
1636
                                 NULL };
1637
  static const int   imode[] = { SSL_SESS_CACHE_OFF,
1638
                                 SSL_SESS_CACHE_CLIENT,
1639
                                 SSL_SESS_CACHE_SERVER,
1640
                                 SSL_SESS_CACHE_BOTH,
1641
                                 SSL_SESS_CACHE_NO_AUTO_CLEAR,
1642
                                 SSL_SESS_CACHE_NO_INTERNAL_LOOKUP,
1643
                                 SSL_SESS_CACHE_NO_INTERNAL_STORE,
1644
                                 SSL_SESS_CACHE_NO_INTERNAL,
1645
                                 -1 };
1646

1647
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
76✔
1648
  int      n = lua_gettop(L);
76✔
1649
  long     mode = 0;
76✔
1650
  int      i;
1651
  if (n > 1) {
76✔
1652
    if (lua_isnumber(L, 2)) {
48✔
1653
      mode = luaL_checkinteger(L, 2);
4✔
1654
      mode = SSL_CTX_set_session_cache_mode(ctx, mode);
4✔
1655
    } else {
1656
      for (i = 2; i <= n; i++) {
144✔
1657
        int j = auxiliar_checkoption(L, i, NULL, smode, imode);
100✔
1658
        mode |= j;
100✔
1659
      }
1660
      mode = SSL_CTX_set_session_cache_mode(ctx, mode);
44✔
1661
    }
1662
  } else {
1663
    mode = SSL_CTX_get_session_cache_mode(ctx);
28✔
1664
  };
1665

1666
  lua_newtable(L);
76✔
1667
  i = 0;
76✔
1668
  if (mode == SSL_SESS_CACHE_OFF) {
76✔
1669
    lua_pushstring(L, "off");
8✔
1670
    lua_rawseti(L, -2, ++i);
8✔
1671
  } else {
1672
    if (mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) {
68✔
1673
      lua_pushstring(L, "no_auto_clear");
8✔
1674
      lua_rawseti(L, -2, ++i);
8✔
1675
    }
1676
    if ((mode & SSL_SESS_CACHE_BOTH) == SSL_SESS_CACHE_BOTH) {
68✔
1677
      lua_pushstring(L, "both");
24✔
1678
      lua_rawseti(L, -2, ++i);
24✔
1679
    } else if (mode & SSL_SESS_CACHE_SERVER) {
44✔
1680
      lua_pushstring(L, "server");
36✔
1681
      lua_rawseti(L, -2, ++i);
36✔
1682
    } else if (mode & SSL_SESS_CACHE_CLIENT) {
8✔
1683
      lua_pushstring(L, "client");
8✔
1684
      lua_rawseti(L, -2, ++i);
8✔
1685
    }
1686
    if ((mode & SSL_SESS_CACHE_NO_INTERNAL) == SSL_SESS_CACHE_NO_INTERNAL) {
68✔
1687
      lua_pushstring(L, "no_internal");
24✔
1688
      lua_rawseti(L, -2, ++i);
24✔
1689
    } else if (mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP) {
44✔
1690
      lua_pushstring(L, "no_internal_lookup");
8✔
1691
      lua_rawseti(L, -2, ++i);
8✔
1692
    } else if (mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
36✔
1693
      lua_pushstring(L, "no_internal_store");
8✔
1694
      lua_rawseti(L, -2, ++i);
8✔
1695
    }
1696
  }
1697

1698
  return 1;
76✔
1699
}
1700

1701
#if OPENSSL_VERSION_NUMBER > 0x1010100FL && !defined(LIBRESSL_VERSION_NUMBER)
1702
/***
1703
get or set number of TLS tickets
1704
@function num_tickets
1705
@tparam[opt] number num number of tickets to set
1706
@treturn number current number of tickets
1707
*/
1708
static int
1709
openssl_ssl_ctx_num_tickets(lua_State *L)
6✔
1710
{
1711
  SSL_CTX *ctx = CHECK_OBJECT(1, SSL_CTX, "openssl.ssl_ctx");
6✔
1712
  size_t   num;
1713
  if (!lua_isnone(L, 2)) {
6✔
1714
    num = luaL_checkinteger(L, 2);
3✔
1715
    SSL_CTX_set_num_tickets(ctx, num);
3✔
1716
  } else
1717
    num = SSL_CTX_get_num_tickets(ctx);
3✔
1718

1719
  lua_pushinteger(L, num);
6✔
1720
  return 1;
6✔
1721
}
1722
#endif
1723

1724
#ifdef SSL_CTX_EXT_DEFINE
1725
SSL_CTX_EXT_DEFINE
1726
#endif
1727

1728
static luaL_Reg ssl_ctx_funcs[] = {
1729
  { "ssl",                     openssl_ssl_ctx_new_ssl                 },
1730
  { "bio",                     openssl_ssl_ctx_new_bio                 },
1731
#ifndef SSL_CTX_USE_EXT
1732
  { "use",                     openssl_ssl_ctx_use                     },
1733
#else
1734
  SSL_CTX_USE_EXT
1735
#endif
1736
  { "add",                     openssl_ssl_ctx_add                     },
1737
  { "mode",                    openssl_ssl_ctx_mode                    },
1738
  { "timeout",                 openssl_ssl_ctx_timeout                 },
1739
  { "options",                 openssl_ssl_ctx_options                 },
1740
#if OPENSSL_VERSION_NUMBER > 0x10100000L
1741
  { "version",                 openssl_ssl_ctx_version                 },
1742
#endif
1743
#if OPENSSL_VERSION_NUMBER > 0x1010100FL && !defined(LIBRESSL_VERSION_NUMBER)
1744
  { "num_tickets",             openssl_ssl_ctx_num_tickets             },
1745
#endif
1746
  { "quiet_shutdown",          openssl_ssl_ctx_quiet_shutdown          },
1747
  { "verify_locations",        openssl_ssl_ctx_load_verify_locations   },
1748
  { "cert_store",              openssl_ssl_ctx_cert_store              },
1749
#ifndef OPENSSL_NO_ENGINE
1750
  { "set_engine",              openssl_ssl_ctx_set_engine              },
1751
#endif
1752
  { "verify_mode",             openssl_ssl_ctx_verify_mode             },
1753
  { "set_cert_verify",         openssl_ssl_ctx_set_cert_verify         },
1754
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
1755
  { "set_alpn_protos",         openssl_ssl_ctx_set_alpn_protos         },
1756
  { "set_alpn_select_cb",      openssl_ssl_ctx_set_alpn_select_cb      },
1757
#endif
1758

1759
  { "verify_depth",            openssl_ssl_ctx_verify_depth            },
1760
#if OPENSSL_VERSION_NUMBER < 0x10100000L
1761
  { "set_tmp",                 openssl_ssl_ctx_set_tmp                 },
1762
#endif
1763
  { "flush_sessions",          openssl_ssl_ctx_flush_sessions          },
1764
  { "session",                 openssl_ssl_ctx_sessions                },
1765
  { "session_cache_mode",      openssl_session_cache_mode              },
1766
  { "set_session_callback",    openssl_ssl_ctx_set_session_callback    },
1767
  { "set_servername_callback", openssl_ssl_ctx_set_servername_callback },
1768

1769
  { "__gc",                    openssl_ssl_ctx_gc                      },
1770
  { "__tostring",              auxiliar_tostring                       },
1771

1772
  { NULL,                      NULL                                    },
1773
};
1774

1775
/* SSL SESSION functions */
1776
/***
1777
get peer certificate verify result
1778
@function getpeerverification
1779
@treturn boolean true for success
1780
@treturn table all certificate in chains verify result
1781
 preverify_ok as boolean verify result
1782
 error as number error code
1783
 error_string as string error message
1784
 error_depth as number verify depth
1785
 current_cert as x509 certificate to verified
1786
*/
1787
static int
1788
openssl_ssl_getpeerverification(lua_State *L)
604✔
1789
{
1790
  long err;
1791
  SSL *ssl = CHECK_OBJECT(1, SSL, "openssl.ssl");
604✔
1792

1793
  err = SSL_get_verify_result(ssl);
604✔
1794
  lua_pushboolean(L, err == X509_V_OK);
604✔
1795
  openssl_valueget(L, ssl, "verify_cert");
604✔
1796
  return 2;
604✔
1797
}
1798

1799
/***
1800
get or set SSL session time
1801
@function time
1802
@tparam[opt] number time session time to set (Unix timestamp)
1803
@treturn number current session time (if getting) or previous time (if setting)
1804
*/
1805
static int
1806
openssl_ssl_session_time(lua_State *L)
8✔
1807
{
1808
  SSL_SESSION *session = CHECK_OBJECT(1, SSL_SESSION, "openssl.ssl_session");
8✔
1809
#if OPENSSL_VERSION_NUMBER >= 0x30400000L && !defined(LIBRESSL_VERSION_NUMBER)
1810
  time_t       time;
1811
  if (!lua_isnone(L, 2)) {
1812
    time = (time_t)luaL_checklong(L, 2);
1813
    time_t prev_time = SSL_SESSION_set_time_ex(session, time);
1814
    lua_pushinteger(L, (lua_Integer)prev_time);
1815
    return 1;
1816
  }
1817
  time = SSL_SESSION_get_time_ex(session);
1818
  lua_pushinteger(L, (lua_Integer)time);
1819
  return 1;
1820
#else
1821
  int          time;
1822
  if (!lua_isnone(L, 2)) {
8✔
1823
    time = luaL_checklong(L, 2);
4✔
1824
    time = SSL_SESSION_set_time(session, time);
4✔
1825
    lua_pushinteger(L, time);
4✔
1826
    return 1;
4✔
1827
  }
1828
  time = SSL_SESSION_get_time(session);
4✔
1829
  lua_pushinteger(L, time);
4✔
1830
  return 1;
4✔
1831
#endif
1832
}
1833

1834
/***
1835
get or set SSL session timeout
1836
@function timeout
1837
@tparam[opt] number timeout session timeout in seconds to set
1838
@treturn number current session timeout (if getting) or previous timeout (if setting)
1839
*/
1840
static int
1841
openssl_ssl_session_timeout(lua_State *L)
8✔
1842
{
1843
  SSL_SESSION *session = CHECK_OBJECT(1, SSL_SESSION, "openssl.ssl_session");
8✔
1844
  int          time;
1845
  if (!lua_isnone(L, 2)) {
8✔
1846
    time = luaL_checkint(L, 2);
4✔
1847
    time = SSL_SESSION_set_timeout(session, time);
4✔
1848
    lua_pushinteger(L, time);
4✔
1849
    return 1;
4✔
1850
  }
1851
  time = SSL_SESSION_get_timeout(session);
4✔
1852
  lua_pushinteger(L, time);
4✔
1853
  return 1;
4✔
1854
}
1855

1856
static int
1857
openssl_ssl_session_gc(lua_State *L)
63✔
1858
{
1859
  SSL_SESSION *session = CHECK_OBJECT(1, SSL_SESSION, "openssl.ssl_session");
63✔
1860
  SSL_SESSION_free(session);
63✔
1861
  return 0;
63✔
1862
}
1863

1864
/***
1865
get peer certificate from SSL session
1866
@function peer
1867
@treturn openssl.x509 peer certificate from the session
1868
*/
1869
static int
1870
openssl_ssl_session_peer(lua_State *L)
4✔
1871
{
1872
  SSL_SESSION *session = CHECK_OBJECT(1, SSL_SESSION, "openssl.ssl_session");
4✔
1873
  X509        *x = SSL_SESSION_get0_peer(session);
4✔
1874
  X509_up_ref(x);
4✔
1875
  PUSH_OBJECT(x, "openssl.x509");
4✔
1876
  return 1;
4✔
1877
}
1878

1879
/***
1880
get or set SSL session ID
1881
@function id
1882
@tparam[opt] string id optional session ID to set
1883
@treturn string current session ID when called without parameters
1884
@treturn boolean true when setting session ID successfully (OpenSSL 1.1.0+)
1885
*/
1886
static int
1887
openssl_ssl_session_id(lua_State *L)
67✔
1888
{
1889
  CONSTIFY_OPENSSL
1890
  SSL_SESSION *session = CHECK_OBJECT(1, SSL_SESSION, "openssl.ssl_session");
67✔
1891

1892
  if (lua_isnone(L, 2)) {
67✔
1893
    unsigned int         len;
1894
    const unsigned char *id = SSL_SESSION_get_id(session, &len);
59✔
1895
    lua_pushlstring(L, (const char *)id, len);
59✔
1896
    return 1;
59✔
1897
  } else {
1898
#if OPENSSL_VERSION_NUMBER > 0x10100000L
1899
    size_t      len;
1900
    const char *id = luaL_checklstring(L, 2, &len);
6✔
1901
    int         ret = SSL_SESSION_set1_id((SSL_SESSION *)session, (const unsigned char *)id, len);
6✔
1902
    lua_pushboolean(L, ret);
6✔
1903
#else
1904
    lua_pushnil(L);
2✔
1905
#endif
1906
    return 1;
8✔
1907
  }
1908
}
1909

1910
static int
1911
openssl_ssl_session_compress_id(lua_State *L)
4✔
1912
{
1913
  SSL_SESSION *session = CHECK_OBJECT(1, SSL_SESSION, "openssl.ssl_session");
4✔
1914
  unsigned int id = SSL_SESSION_get_compress_id(session);
4✔
1915
  lua_pushinteger(L, id);
4✔
1916
  return 1;
4✔
1917
}
1918

1919
/***
1920
export SSL session to PEM or DER format
1921
@function export
1922
@tparam[opt=true] boolean pem true for PEM format, false for DER format
1923
@treturn string exported session data in specified format
1924
*/
1925
static int
1926
openssl_ssl_session_export(lua_State *L)
4✔
1927
{
1928
  SSL_SESSION *session = CHECK_OBJECT(1, SSL_SESSION, "openssl.ssl_session");
4✔
1929
  int          pem = lua_isnone(L, 2) ? 1 : auxiliar_checkboolean(L, 2);
4✔
1930
  BIO         *bio = BIO_new(BIO_s_mem());
4✔
1931
  BUF_MEM     *bio_buf;
1932
  if (pem) {
4✔
1933
    PEM_write_bio_SSL_SESSION(bio, session);
4✔
1934
  } else {
1935
    i2d_SSL_SESSION_bio(bio, session);
×
1936
  }
1937

1938
  BIO_get_mem_ptr(bio, &bio_buf);
4✔
1939
  lua_pushlstring(L, bio_buf->data, bio_buf->length);
4✔
1940
  BIO_free(bio);
4✔
1941
  return 1;
4✔
1942
}
1943

1944
#if OPENSSL_VERSION_NUMBER > 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
1945
/***
1946
check if SSL session is resumable
1947
@function is_resumable
1948
@treturn boolean true if session can be resumed
1949
*/
1950
static int
1951
openssl_ssl_session_is_resumable(lua_State *L)
3✔
1952
{
1953
  SSL_SESSION *session = CHECK_OBJECT(1, SSL_SESSION, "openssl.ssl_session");
3✔
1954
  int          ret = SSL_SESSION_is_resumable(session);
3✔
1955
  lua_pushboolean(L, ret);
3✔
1956
  return 1;
3✔
1957
}
1958
#endif
1959

1960
#if OPENSSL_VERSION_NUMBER > 0x10100000L
1961
/***
1962
check if SSL session has a ticket
1963
@function has_ticket
1964
@treturn boolean true if session has a ticket
1965
*/
1966
static int
1967
openssl_ssl_session_has_ticket(lua_State *L)
3✔
1968
{
1969
  SSL_SESSION *session = CHECK_OBJECT(1, SSL_SESSION, "openssl.ssl_session");
3✔
1970
  int          ret = SSL_SESSION_has_ticket(session);
3✔
1971
  lua_pushboolean(L, ret);
3✔
1972
  return 1;
3✔
1973
}
1974
#endif
1975

1976
static luaL_Reg ssl_session_funcs[] = {
1977
  { "id",           openssl_ssl_session_id           },
1978
  { "time",         openssl_ssl_session_time         },
1979
  { "timeout",      openssl_ssl_session_timeout      },
1980
  { "compress_id",  openssl_ssl_session_compress_id  },
1981
  { "peer",         openssl_ssl_session_peer         },
1982
  { "export",       openssl_ssl_session_export       },
1983
#if OPENSSL_VERSION_NUMBER > 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
1984
  { "is_resumable", openssl_ssl_session_is_resumable },
1985
#endif
1986
#if OPENSSL_VERSION_NUMBER > 0x10100000L
1987
  { "has_ticket",   openssl_ssl_session_has_ticket   },
1988
#endif
1989

1990
  { "__gc",         openssl_ssl_session_gc           },
1991
  { "__tostring",   auxiliar_tostring                },
1992

1993
  { NULL,           NULL                             },
1994
};
1995

1996
/* SSL object */
1997
/***
1998
openssl.ssl object
1999
All SSL object IO operation methods(connect, accept, handshake, read,
2000
peek or write) return nil or false when fail or error.
2001
When nil returned, it followed by 'ssl' or 'syscall', means SSL layer or
2002
system layer error. When false returned, it followed by number 0,
2003
'want_read','want_write','want_x509_lookup','want_connect','want_accept'.
2004
Numnber 0 means SSL connection closed, others means you should do some
2005
SSL operation.
2006
@type ssl
2007
*/
2008

2009
/***
2010
reset ssl object to allow another connection
2011
@function clear
2012
@treturn boolean result true for success
2013
*/
2014
static int
2015
openssl_ssl_clear(lua_State *L)
8✔
2016
{
2017
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
8✔
2018
  lua_pushboolean(L, SSL_clear(s));
8✔
2019
  return 1;
8✔
2020
}
2021

2022
/***
2023
tell ssl use private key and certificate, and check private key
2024
@function use
2025
@tparam openssl.evp_pkey pkey
2026
@tparam[opt] openssl.x509 cert
2027
@treturn boolean result return true for ok, or nil followed by errmsg and errval
2028
*/
2029
static int
2030
openssl_ssl_use(lua_State *L)
8✔
2031
{
2032
  SSL      *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
8✔
2033
  X509     *x = CHECK_OBJECT(2, X509, "openssl.x509");
8✔
2034
  EVP_PKEY *pkey = CHECK_OBJECT(3, EVP_PKEY, "openssl.evp_pkey");
8✔
2035
  int       ret;
2036

2037
  ret = SSL_use_PrivateKey(s, pkey);
8✔
2038
  if (ret == 1) {
8✔
2039
    ret = SSL_use_certificate(s, x);
8✔
2040
    if (ret == 1) {
8✔
2041
      ret = SSL_check_private_key(s);
8✔
2042
    }
2043
  }
2044
  return openssl_pushresult(L, ret);
8✔
2045
}
2046

2047
/***
2048
get peer certificate and certificate chains
2049
@function peer
2050
@treturn[1] openssl.x509 certificate
2051
@treturn[1] sk_of_x509 chains of peer
2052
*/
2053
static int
2054
openssl_ssl_peer(lua_State *L)
20✔
2055
{
2056
  SSL  *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
20✔
2057
  X509 *x = SSL_get_peer_certificate(s);
20✔
2058
  STACK_OF(X509) *sk = SSL_get_peer_cert_chain(s);
20✔
2059
  PUSH_OBJECT(x, "openssl.x509");
20✔
2060
  if (sk) {
20✔
2061
    openssl_sk_x509_totable(L, sk);
20✔
2062
    return 2;
20✔
2063
  }
2064
  return 1;
×
2065
}
2066

2067
static int
2068
openssl_ssl_gc(lua_State *L)
2,499✔
2069
{
2070
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
2,499✔
2071
  SSL_free(s);
2,499✔
2072
  openssl_freevalue(L, s);
2,499✔
2073

2074
  return 0;
2,499✔
2075
}
2076

2077
/***
2078
get want to do
2079
@function want
2080
@treturn[1] string 'nothing', 'reading', 'writing', 'x509_lookup'
2081
@treturn[1] number state want
2082
*/
2083
static int
2084
openssl_ssl_want(lua_State *L)
17✔
2085
{
2086
  SSL        *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
17✔
2087
  int         st = SSL_want(s);
17✔
2088
  const char *state = NULL;
17✔
2089
  if (st == SSL_NOTHING)
17✔
2090
    state = "nothing";
9✔
2091
  else if (st == SSL_READING)
8✔
2092
    state = "reading";
8✔
2093
  else if (st == SSL_WRITING)
×
2094
    state = "writing";
×
2095
  else if (st == SSL_X509_LOOKUP)
×
2096
    state = "x509_lookup";
×
2097

2098
  lua_pushstring(L, state);
17✔
2099
  lua_pushinteger(L, st);
17✔
2100
  return 2;
17✔
2101
}
2102
#if !defined(OPENSSL_NO_COMP)
2103
/***
2104
get current compression name
2105
@function current_compression
2106
@treturn string
2107
*/
2108
static int
2109
openssl_ssl_current_compression(lua_State *L)
4✔
2110
{
2111
  SSL               *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
4✔
2112
  const COMP_METHOD *comp = SSL_get_current_compression(s);
4✔
2113
  if (comp)
4✔
UNCOV
2114
    lua_pushstring(L, SSL_COMP_get_name(comp));
×
2115
  else
2116
    lua_pushnil(L);
4✔
2117
  return 1;
4✔
2118
}
2119
#endif
2120

2121
/***
2122
get current cipher info
2123
@function current_cipher
2124
@treturn table include name,version,id,bits,algbits and description
2125
*/
2126
static int
2127
openssl_ssl_current_cipher(lua_State *L)
600✔
2128
{
2129
  SSL              *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
600✔
2130
  const SSL_CIPHER *c = SSL_get_current_cipher(s);
600✔
2131
  if (c) {
600✔
2132
    int  bits, algbits;
2133
    char err[LUAL_BUFFERSIZE] = { 0 };
600✔
2134

2135
    lua_newtable(L);
600✔
2136

2137
    AUXILIAR_SET(L, -1, "name", SSL_CIPHER_get_name(c), string);
600✔
2138
    AUXILIAR_SET(L, -1, "version", SSL_CIPHER_get_version(c), string);
600✔
2139

2140
    AUXILIAR_SET(L, -1, "id", SSL_CIPHER_get_id(c), integer);
600✔
2141
    bits = SSL_CIPHER_get_bits(c, &algbits);
600✔
2142
    AUXILIAR_SET(L, -1, "bits", bits, integer);
600✔
2143
    AUXILIAR_SET(L, -1, "algbits", algbits, integer);
600✔
2144

2145
    AUXILIAR_SET(
600✔
2146
      L, -1, "description", SSL_CIPHER_description((SSL_CIPHER *)c, err, sizeof(err)), string);
2147

2148
    return 1;
600✔
2149
  }
2150
  return 0;
×
2151
}
2152

2153
/***
2154
get number of bytes available inside SSL fro immediate read
2155
@function pending
2156
@treturn number
2157
*/
2158
static int
2159
openssl_ssl_pending(lua_State *L)
4✔
2160
{
2161
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
4✔
2162
  lua_pushinteger(L, SSL_pending(s));
4✔
2163
  return 1;
4✔
2164
}
2165

2166
/* Helper function to push SSL result */
2167
static int
2168
openssl_ssl_pushresult(lua_State *L, SSL *ssl, int ret_code)
3,140✔
2169
{
2170
  int err = SSL_get_error(ssl, ret_code);
3,140✔
2171
  switch (err) {
3,140✔
2172
  case SSL_ERROR_NONE:
1,850✔
2173
    lua_pushboolean(L, 1);
1,850✔
2174
    lua_pushinteger(L, ret_code);
1,850✔
2175
    break;
1,850✔
2176
  case SSL_ERROR_ZERO_RETURN:
600✔
2177
    lua_pushboolean(L, 0);
600✔
2178
    lua_pushinteger(L, 0);
600✔
2179
    break;
600✔
2180
  case SSL_ERROR_SSL:
21✔
2181
    lua_pushnil(L);
21✔
2182
    lua_pushstring(L, "ssl");
21✔
2183
    break;
21✔
2184
  case SSL_ERROR_WANT_READ:
53✔
2185
    lua_pushboolean(L, 0);
53✔
2186
    lua_pushstring(L, "want_read");
53✔
2187
    break;
53✔
2188
  case SSL_ERROR_WANT_WRITE:
×
2189
    lua_pushboolean(L, 0);
×
2190
    lua_pushstring(L, "want_write");
×
2191
    break;
×
2192
  case SSL_ERROR_WANT_X509_LOOKUP:
×
2193
    lua_pushboolean(L, 0);
×
2194
    lua_pushstring(L, "want_x509_lookup");
×
2195
    break;
×
2196
  case SSL_ERROR_SYSCALL:
616✔
2197
    lua_pushnil(L);
616✔
2198
    lua_pushstring(L, "syscall");
616✔
2199
    break;
616✔
2200
  case SSL_ERROR_WANT_CONNECT:
×
2201
    lua_pushboolean(L, 0);
×
2202
    lua_pushstring(L, "want_connect");
×
2203
    break;
×
2204
  case SSL_ERROR_WANT_ACCEPT:
×
2205
    lua_pushboolean(L, 0);
×
2206
    lua_pushstring(L, "want_accept");
×
2207
    break;
×
2208
  default:
×
2209
    return 0;
×
2210
  }
2211
  return 2;
3,140✔
2212
}
2213

2214
/***
2215
get socket fd of ssl
2216
@function getfd
2217
@treturn number fd
2218
*/
2219
static int
2220
openssl_ssl_getfd(lua_State *L)
600✔
2221
{
2222
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
600✔
2223
  lua_pushinteger(L, SSL_get_fd(s));
600✔
2224
  return 1;
600✔
2225
}
2226

2227
/***
2228
check SSL is a server
2229
@function is_server
2230
@treturn boolean is_server
2231
*/
2232
static int
2233
openssl_ssl_is_server(lua_State *L)
600✔
2234
{
2235
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
600✔
2236
  lua_pushboolean(L, SSL_is_server(s));
600✔
2237
  return 1;
600✔
2238
}
2239

2240
/***
2241
get value according to arg
2242
@function get
2243
@tparam string arg
2244
 <br/>certificate:  return SSL certificates
2245
 <br/>fd: return file or network connect fd
2246
 <br/>rfd:
2247
 <br/>wfd:
2248
 <br/>client_CA_list
2249
 <br/>read_ahead: -> boolean
2250
 <br/>shared_ciphers: string
2251
 <br/>cipher_list -> string
2252
 <br/>verify_mode: number
2253
 <br/>verify_depth
2254
 <br/>state_string
2255
 <br/>state_string_long
2256
 <br/>rstate_string
2257
 <br/>rstate_string_long
2258
 <br/>iversion
2259
 <br/>version
2260
 <br/>default_timeout,
2261
 <br/>certificate
2262
 <br/>verify_result
2263
 <br/>state
2264
 <br/>hostname
2265
 <br/>state_string
2266
 <br/>side
2267
@return according to arg
2268
*/
2269
static int
2270
openssl_ssl_get(lua_State *L)
696✔
2271
{
2272
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
696✔
2273
  int  i;
2274
  int  top = lua_gettop(L);
696✔
2275
  for (i = 2; i <= top; i++) {
1,392✔
2276
    const char *what = luaL_checklstring(L, i, NULL);
696✔
2277
    if (strcmp(what, "fd") == 0) {
696✔
2278
      lua_pushinteger(L, SSL_get_fd(s));
4✔
2279
    } else if (strcmp(what, "rfd") == 0) {
692✔
2280
      lua_pushinteger(L, SSL_get_rfd(s));
4✔
2281
    } else if (strcmp(what, "wfd") == 0) {
688✔
2282
      lua_pushinteger(L, SSL_get_wfd(s));
4✔
2283
    } else if (strcmp(what, "client_CA_list") == 0) {
684✔
2284
      STACK_OF(X509_NAME) *sn = SSL_get_client_CA_list(s);
4✔
2285
      openssl_sk_x509_name_totable(L, sn);
4✔
2286
    } else if (strcmp(what, "read_ahead") == 0) {
680✔
2287
      lua_pushboolean(L, SSL_get_read_ahead(s));
4✔
2288
    } else if (strcmp(what, "shared_ciphers") == 0) {
676✔
2289
      char buf[LUAL_BUFFERSIZE] = { 0 };
4✔
2290
      lua_pushstring(L, SSL_get_shared_ciphers(s, buf, sizeof(buf)));
4✔
2291
    } else if (strcmp(what, "cipher_list") == 0) {
672✔
2292
      lua_pushstring(L, SSL_get_cipher_list(s, 0));
4✔
2293
    } else if (strcmp(what, "verify_mode") == 0) {
668✔
2294
      lua_pushinteger(L, SSL_get_verify_mode(s));
4✔
2295
    } else if (strcmp(what, "verify_depth") == 0) {
664✔
2296
      lua_pushinteger(L, SSL_get_verify_depth(s));
4✔
2297
    } else if (strcmp(what, "state_string") == 0) {
660✔
2298
      lua_pushstring(L, SSL_state_string(s));
8✔
2299
    } else if (strcmp(what, "state_string_long") == 0) {
652✔
2300
      lua_pushstring(L, SSL_state_string_long(s));
4✔
2301
    } else if (strcmp(what, "rstate_string") == 0) {
648✔
2302
      lua_pushstring(L, SSL_rstate_string(s));
4✔
2303
    } else if (strcmp(what, "rstate_string_long") == 0) {
644✔
2304
      lua_pushstring(L, SSL_rstate_string_long(s));
4✔
2305
    } else if (strcmp(what, "version") == 0) {
640✔
2306
      lua_pushstring(L, SSL_get_version(s));
8✔
2307
    } else if (strcmp(what, "iversion") == 0) {
632✔
2308
      lua_pushinteger(L, SSL_version(s));
4✔
2309
    } else if (strcmp(what, "default_timeout") == 0) {
628✔
2310
      lua_pushinteger(L, SSL_get_default_timeout(s));
4✔
2311
    } else if (strcmp(what, "certificate") == 0) {
624✔
2312
      X509 *cert = SSL_get_certificate(s);
4✔
2313
      if (cert) {
4✔
2314
        X509_up_ref(cert);
4✔
2315
        PUSH_OBJECT(cert, "openssl.x509");
4✔
2316
      } else
2317
        lua_pushnil(L);
×
2318
    } else if (strcmp(what, "verify_result") == 0) {
620✔
2319
      long l = SSL_get_verify_result(s);
4✔
2320
      lua_pushinteger(L, l);
4✔
2321
    } else if (strcmp(what, "state") == 0) {
616✔
2322
      lua_pushinteger(L, SSL_get_state(s));
4✔
2323
    } else if (strcmp(what, "hostname") == 0) {
612✔
2324
      lua_pushstring(L, SSL_get_servername(s, TLSEXT_NAMETYPE_host_name));
8✔
2325
    } else if (strcmp(what, "side") == 0) {
604✔
2326
      lua_pushstring(L, SSL_is_server(s) ? "server" : "client");
604✔
2327
    } else
2328
      luaL_argerror(L, i, "can't understant");
×
2329
  }
2330
  return top - 1;
696✔
2331
}
2332

2333
/***
2334
set value according to arg
2335
@function set
2336
@tparam string arg
2337
 <br/>certificate:  return SSL certificates
2338
 <br/>fd: return file or network connect fd
2339
 <br/>rfd:
2340
 <br/>wfd:
2341
 <br/>client_CA:
2342
 <br/>read_ahead:
2343
 <br/>cipher_list:
2344
 <br/>verify_depth:
2345
 <br/>purpose:
2346
 <br/>trust:
2347
 <br/>verify_result:
2348
 <br/>hostname:
2349
@param value val type accroding to arg
2350
@return value
2351
*/
2352
static int
2353
openssl_ssl_set(lua_State *L)
60✔
2354
{
2355
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
60✔
2356
  int  i;
2357
  int  top = lua_gettop(L);
60✔
2358
  int  ret = 1;
60✔
2359
  for (i = 2; i <= top; i += 2) {
117✔
2360
    const char *what = luaL_checklstring(L, i, NULL);
60✔
2361
    if (strcmp(what, "fd") == 0) {
60✔
2362
      ret = SSL_set_fd(s, luaL_checkint(L, i + 1));
4✔
2363
    } else if (strcmp(what, "rfd") == 0) {
56✔
2364
      ret = SSL_set_wfd(s, luaL_checkint(L, i + 1));
4✔
2365
    } else if (strcmp(what, "wfd") == 0) {
52✔
2366
      ret = SSL_set_wfd(s, luaL_checkint(L, i + 1));
4✔
2367
    } else if (strcmp(what, "client_CA") == 0) {
48✔
2368
      X509 *x = CHECK_OBJECT(i + 1, X509, "openssl.x509");
4✔
2369
      ret = SSL_add_client_CA(s, x);
4✔
2370
    } else if (strcmp(what, "read_ahead") == 0) {
44✔
2371
      int yes = auxiliar_checkboolean(L, i + 1);
4✔
2372
      SSL_set_read_ahead(s, yes);
4✔
2373
    } else if (strcmp(what, "cipher_list") == 0) {
40✔
2374
      const char *list = lua_tostring(L, i + 1);
4✔
2375
      ret = SSL_set_cipher_list(s, list);
4✔
2376
    } else if (strcmp(what, "verify_depth") == 0) {
36✔
2377
      int depth = luaL_checkint(L, i + 1);
4✔
2378
      SSL_set_verify_depth(s, depth);
4✔
2379
    } else if (strcmp(what, "purpose") == 0) {
32✔
2380
      int purpose = luaL_checkint(L, i + 1);
4✔
2381
      ret = SSL_set_purpose(s, purpose);
4✔
2382
    } else if (strcmp(what, "trust") == 0) {
28✔
2383
      int trust = luaL_checkint(L, i + 1);
4✔
2384
      ret = SSL_set_trust(s, trust);
4✔
2385
    } else if (strcmp(what, "verify_result") == 0) {
24✔
2386
      int result = luaL_checkint(L, i + 1);
4✔
2387
      SSL_set_verify_result(s, result);
4✔
2388
    } else if (strcmp(what, "hostname") == 0) {
20✔
2389
      const char *hostname = luaL_checkstring(L, i + 1);
20✔
2390
      SSL_set_tlsext_host_name(s, hostname);
20✔
2391
    } else
2392
      luaL_argerror(L, i, "don't understand");
×
2393

2394
    if (ret != 1) return openssl_pushresult(L, ret);
60✔
2395
  }
2396
  return 0;
57✔
2397
}
2398

2399
/***
2400
do ssl server accept
2401
@function accept
2402
@treturn boolean true for success
2403
@treturn string fail reason
2404
*/
2405
static int
2406
openssl_ssl_accept(lua_State *L)
300✔
2407
{
2408
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
300✔
2409
  int  ret = SSL_accept(s);
300✔
2410
  return openssl_ssl_pushresult(L, s, ret);
300✔
2411
}
2412

2413
/***
2414
do ssl client connect
2415
@function connect
2416
@treturn boolean true for success
2417
@treturn string fail reasion
2418
*/
2419
static int
2420
openssl_ssl_connect(lua_State *L)
600✔
2421
{
2422
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
600✔
2423
  int  ret = SSL_connect(s);
600✔
2424
  return openssl_ssl_pushresult(L, s, ret);
600✔
2425
}
2426

2427
/***
2428
do ssl read
2429
@function read
2430
@tparam[opt=4096] number length to read
2431
@treturn string data, nil or false for fail
2432
@treturn string fail reason
2433
*/
2434
static int
2435
openssl_ssl_read(lua_State *L)
120,632✔
2436
{
2437
  SSL  *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
120,632✔
2438
  int   num = luaL_optint(L, 2, SSL_pending(s));
120,632✔
2439
  void *buf;
2440
  int   ret;
2441
  num = num ? num : 4096;
120,632✔
2442
  buf = malloc(num);
120,632✔
2443
  ret = SSL_read(s, buf, num);
120,632✔
2444
  if (ret > 0) {
120,632✔
2445
    lua_pushlstring(L, buf, ret);
120,024✔
2446
    ret = 1;
120,024✔
2447
  } else {
2448
    ret = openssl_ssl_pushresult(L, s, ret);
608✔
2449
  }
2450
  free(buf);
120,632✔
2451
  return ret;
120,632✔
2452
}
2453

2454
/***
2455
do ssl peak, data can be read again
2456
@function peek
2457
@tparam[opt=4096] number length to read
2458
@treturn string data, nil or false for fail
2459
@treturn string fail reason
2460
*/
2461
static int
2462
openssl_ssl_peek(lua_State *L)
4✔
2463
{
2464
  SSL  *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
4✔
2465
  int   num = luaL_optint(L, 2, SSL_pending(s));
4✔
2466
  void *buf;
2467
  int   ret;
2468

2469
  num = num ? num : 4096;
4✔
2470
  buf = malloc(num);
4✔
2471
  ret = SSL_peek(s, buf, num);
4✔
2472
  if (ret > 0) {
4✔
2473
    lua_pushlstring(L, buf, ret);
4✔
2474
    ret = 1;
4✔
2475
  } else {
2476
    ret = openssl_ssl_pushresult(L, s, ret);
×
2477
  }
2478
  free(buf);
4✔
2479
  return ret;
4✔
2480
}
2481

2482
/***
2483
do ssl write
2484
@function write
2485
@tparam string data
2486
@treturn number count of bytes write successfully
2487
@treturn string fail reason
2488
*/
2489
static int
2490
openssl_ssl_write(lua_State *L)
120,024✔
2491
{
2492
  SSL        *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
120,024✔
2493
  size_t      size;
2494
  const char *buf = luaL_checklstring(L, 2, &size);
120,024✔
2495
  int         ret = SSL_write(s, buf, size);
120,024✔
2496
  if (ret > 0) {
120,024✔
2497
    lua_pushinteger(L, ret);
120,024✔
2498
    return 1;
120,024✔
2499
  } else {
2500
    return openssl_ssl_pushresult(L, s, ret);
×
2501
  }
2502
}
2503

2504
/***
2505
do ssl handshake, support both server and client side
2506
@function handshake
2507
@treturn boolean true for success
2508
@treturn string fail reasion
2509
*/
2510
static int
2511
openssl_ssl_do_handshake(lua_State *L)
396✔
2512
{
2513
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
396✔
2514
  int  ret = SSL_do_handshake(s);
396✔
2515
  return openssl_ssl_pushresult(L, s, ret);
396✔
2516
}
2517

2518
/***
2519
do ssl renegotiate
2520
@function renegotiate
2521
@treturn boolean true for success
2522
@treturn string fail reasion
2523
*/
2524
static int
2525
openssl_ssl_renegotiate(lua_State *L)
4✔
2526
{
2527
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
4✔
2528
  int  ret = SSL_renegotiate(s);
4✔
2529
  return openssl_ssl_pushresult(L, s, ret);
4✔
2530
}
2531

2532
/***
2533
perform abbreviated SSL renegotiation
2534
@function renegotiate_abbreviated
2535
@treturn boolean result true for success
2536
*/
2537
static int
2538
openssl_ssl_renegotiate_abbreviated(lua_State *L)
4✔
2539
{
2540
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
4✔
2541
  int  ret = SSL_renegotiate_abbreviated(s);
4✔
2542
  return openssl_ssl_pushresult(L, s, ret);
4✔
2543
}
2544

2545
/***
2546
get ssl renegotiate_pending
2547
@function renegotiate_pending
2548
@treturn boolean true for success
2549
@treturn string fail reasion
2550
*/
2551
/***
2552
do ssl renegotiate_pending
2553
@function renegotiate_pending
2554
@treturn boolean true for success
2555
@treturn string fail reasion
2556
*/
2557
static int
2558
openssl_ssl_renegotiate_pending(lua_State *L)
4✔
2559
{
2560
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
4✔
2561
  int  ret = SSL_renegotiate_pending(s);
4✔
2562
  return openssl_ssl_pushresult(L, s, ret);
4✔
2563
}
2564

2565
/***
2566
shutdown ssl connection with quite or noquite mode
2567
@function shutdown
2568
@tparam boolean mode
2569
@treturn boolean if mode is true, return true or false for quite
2570
@treturn string if mode is false, return 'read' or 'write' for shutdown direction
2571
*/
2572

2573
/***
2574
shutdown SSL connection
2575
@function shutdown
2576
@tparam[opt] string mode optional mode: 'read', 'write', 'quite', 'noquite'
2577
@treturn[1] boolean true for success when called without mode
2578
@treturn[2] number shutdown result when called with mode
2579
@treturn[3] nil when error occurs
2580
@treturn[3] string error message when error occurs
2581
*/
2582
static int
2583
openssl_ssl_shutdown(lua_State *L)
1,248✔
2584
{
2585
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
1,248✔
2586
  if (lua_isnone(L, 2)) {
1,248✔
2587
    int ret = SSL_shutdown(s);
1,224✔
2588
    return openssl_ssl_pushresult(L, s, ret);
1,224✔
2589
  } else if (lua_isstring(L, 2)) {
24✔
2590
    const static char *sMode[] = { "read", "write", "quiet", "noquiet", NULL };
2591
    int                mode = luaL_checkoption(L, 2, NULL, sMode);
16✔
2592
    if (mode == 0)
16✔
2593
      SSL_set_shutdown(s, SSL_RECEIVED_SHUTDOWN);
4✔
2594
    else if (mode == 1)
12✔
2595
      SSL_set_shutdown(s, SSL_SENT_SHUTDOWN);
4✔
2596
    else if (mode == 2)
8✔
2597
      SSL_set_quiet_shutdown(s, 1);
4✔
2598
    else if (mode == 3)
4✔
2599
      SSL_set_quiet_shutdown(s, 0);
4✔
2600
  } else if (lua_isboolean(L, 2)) {
8✔
2601
    int quiet = lua_toboolean(L, 2);
8✔
2602
    if (quiet)
8✔
2603
      lua_pushboolean(L, SSL_get_quiet_shutdown(s));
4✔
2604
    else {
2605
      int shut = SSL_get_shutdown(s);
4✔
2606
      if (shut == SSL_RECEIVED_SHUTDOWN)
4✔
2607
        lua_pushstring(L, "read");
×
2608
      else if (shut == SSL_SENT_SHUTDOWN)
4✔
2609
        lua_pushstring(L, "write");
4✔
2610
      else if (shut == 0)
×
2611
        lua_pushnil(L);
×
2612
      else
2613
        luaL_error(L, "Can't understand SSL_get_shutdown result");
×
2614
    }
2615
    return 1;
8✔
2616
  } else
2617
    luaL_argerror(L, 2, "should be boolean or string[read|write|quiet|noquite]");
×
2618

2619
  return 0;
16✔
2620
};
2621

2622
/***
2623
make ssl to client mode
2624
@function set_connect_state
2625
@treturn various return value
2626
*/
2627
static int
2628
openssl_ssl_set_connect_state(lua_State *L)
8✔
2629
{
2630
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
8✔
2631
  SSL_set_connect_state(s);
8✔
2632
  return 0;
8✔
2633
}
2634

2635
/***
2636
make ssl to server mode
2637
@function set_accept_state
2638
@treturn various return value
2639
*/
2640
static int
2641
openssl_ssl_set_accept_state(lua_State *L)
8✔
2642
{
2643
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
8✔
2644
  SSL_set_accept_state(s);
8✔
2645
  return 0;
8✔
2646
}
2647

2648
/***
2649
duplicate ssl object
2650
@treturn openssl.ssl
2651
@function dup
2652
*/
2653
static int
2654
openssl_ssl_dup(lua_State *L)
8✔
2655
{
2656
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
8✔
2657
  BIO *rio = SSL_get_rbio(s);
8✔
2658
  BIO *wio = SSL_get_wbio(s);
8✔
2659
  if (rio != NULL || wio != NULL) {
8✔
2660
    lua_pushnil(L);
4✔
2661
    lua_pushliteral(L, "invalid state: rbio or wbio already set");
4✔
2662
    return 2;
4✔
2663
  }
2664

2665
  s = SSL_dup(s);
4✔
2666
  if (s) {
4✔
2667
    PUSH_OBJECT(s, "openssl.ssl");
4✔
2668
    openssl_newvalue(L, s);
4✔
2669
    return 1;
4✔
2670
  }
2671
  return openssl_pushresult(L, 0);
×
2672
}
2673

2674
/***
2675
get ssl session resused
2676
@function session_reused
2677
@treturn boolean success status
2678
*/
2679
static int
2680
openssl_ssl_session_reused(lua_State *L)
4✔
2681
{
2682
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
4✔
2683
  int  ret = SSL_session_reused(s);
4✔
2684
  lua_pushboolean(L, ret);
4✔
2685
  return 1;
4✔
2686
}
2687

2688
/***
2689
check if SSL session was reused (cache hit)
2690
@function cache_hit
2691
@treturn boolean true if session was not reused (cache miss)
2692
*/
2693
static int
2694
openssl_ssl_cache_hit(lua_State *L)
4✔
2695
{
2696
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
4✔
2697
  int  ret = SSL_session_reused(s);
4✔
2698
  lua_pushboolean(L, ret == 0);
4✔
2699
  return 1;
4✔
2700
}
2701
#if OPENSSL_VERSION_NUMBER < 0x10100000L
2702
/***
2703
set debug level for SSL connection
2704
@function set_debug
2705
@tparam number debug debug level to set
2706
@treturn number always returns 0
2707
*/
2708
static int
2709
openssl_ssl_set_debug(lua_State *L)
2710
{
2711
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
2712
  int  debug = luaL_checkint(L, 2);
2713
  SSL_set_debug(s, debug);
2714
  return 0;
2715
}
2716
#endif
2717

2718
/***
2719
get ssl_ctx associate with current ssl
2720
@function ctx
2721
@treturn openssl.ssl_ctx
2722
*/
2723
/***
2724
set ssl_ctx associate to current ssl
2725
@function ctx
2726
@tparam openssl.ssl_ctx ctx
2727
@treturn openssl.ssl_ctx orgine ssl_ctx object
2728
*/
2729
static int
2730
openssl_ssl_ctx(lua_State *L)
8✔
2731
{
2732
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
8✔
2733
  if (!lua_isnone(L, 2)) {
8✔
2734
    SSL_CTX *ctx = CHECK_OBJECT(2, SSL_CTX, "openssl.ssl_ctx");
4✔
2735
    ctx = SSL_set_SSL_CTX(s, ctx);
4✔
2736
    lua_pushvalue(L, 2);
4✔
2737
    openssl_valueset(L, s, "ctx");
4✔
2738
  }
2739
  openssl_valueget(L, s, "ctx");
8✔
2740
  return 1;
8✔
2741
}
2742

2743
/***
2744
get ssl session
2745
@treturn ssl_session session object
2746
@function session
2747
*/
2748
/***
2749
set ssl session
2750
@function session
2751
@tparam string|ssl_session sesion
2752
 reuse session would speed up ssl handshake
2753
@treturn boolean result
2754
*/
2755
static int
2756
openssl_ssl_session(lua_State *L)
12✔
2757
{
2758
  SSL         *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
12✔
2759
  SSL_SESSION *ss;
2760

2761
  if (lua_isnone(L, 2)) {
12✔
2762
    ss = SSL_get1_session(s);
8✔
2763
    PUSH_OBJECT(ss, "openssl.ssl_session");
8✔
2764
  } else {
2765
    if (lua_isstring(L, 3)) {
4✔
2766
      size_t      sz;
2767
      const char *sid_ctx = luaL_checklstring(L, 2, &sz);
×
2768
      int         ret = SSL_set_session_id_context(s, (unsigned char *)sid_ctx, sz);
×
2769
      lua_pushboolean(L, ret);
×
2770
    } else {
2771
      ss = CHECK_OBJECT(2, SSL_SESSION, "openssl.ssl_session");
4✔
2772
      if (lua_isnone(L, 3)) {
4✔
2773
        int ret = SSL_set_session(s, ss);
4✔
2774
        lua_pushboolean(L, ret);
4✔
2775
      } else {
2776
#ifdef SSL_add_session
2777
        int add = auxiliar_checkboolean(L, 3);
2778
        if (add)
2779
          add = SSL_add_session(s, ss);
2780
        else
2781
          add = SSL_remove_session(s, ss);
2782
        lua_pushboolean(L, add);
2783
#endif
2784
      }
2785
    }
2786
  }
2787
  return 1;
12✔
2788
}
2789

2790
/***
2791
convert SSL object to string representation
2792
@function __tostring
2793
@treturn string string representation of SSL object
2794
*/
2795
static int
2796
openssl_ssl_tostring(lua_State *L)
651✔
2797
{
2798
  SSL *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
651✔
2799
  lua_pushfstring(L, "openssl.ssl %p", s);
651✔
2800
  return 1;
651✔
2801
}
2802

2803
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
2804
/***
2805
get the ALPN protocol selected
2806
@treturn the ALPN protocol selected or nil
2807
@function get_alpn_selected
2808
*/
2809
static int
2810
openssl_ssl_get_alpn_selected(lua_State *L)
600✔
2811
{
2812
  SSL                 *s = CHECK_OBJECT(1, SSL, "openssl.ssl");
600✔
2813
  const unsigned char *data;
2814
  unsigned             len = 0;
600✔
2815
  SSL_get0_alpn_selected(s, &data, &len);
600✔
2816
  if (len == 0)
600✔
2817
    lua_pushnil(L);
300✔
2818
  else
2819
    lua_pushlstring(L, (const char *)data, len);
300✔
2820
  return 1;
600✔
2821
}
2822
#endif
2823

2824
static luaL_Reg ssl_funcs[] = {
2825
  { "set",                     openssl_ssl_set                     },
2826
  { "get",                     openssl_ssl_get                     },
2827
  { "use",                     openssl_ssl_use                     },
2828
  { "peer",                    openssl_ssl_peer                    },
2829
  { "getfd",                   openssl_ssl_getfd                   },
2830
  { "is_server",               openssl_ssl_is_server               },
2831

2832
  { "current_cipher",          openssl_ssl_current_cipher          },
2833
#if !defined(OPENSSL_NO_COMP)
2834
  { "current_compression",     openssl_ssl_current_compression     },
2835
#endif
2836
  { "getpeerverification",     openssl_ssl_getpeerverification     },
2837

2838
  { "session",                 openssl_ssl_session                 },
2839

2840
  { "dup",                     openssl_ssl_dup                     },
2841
  { "ctx",                     openssl_ssl_ctx                     },
2842
  { "clear",                   openssl_ssl_clear                   },
2843
  { "want",                    openssl_ssl_want                    },
2844
  { "pending",                 openssl_ssl_pending                 },
2845
  { "accept",                  openssl_ssl_accept                  },
2846
  { "connect",                 openssl_ssl_connect                 },
2847
  { "read",                    openssl_ssl_read                    },
2848
  { "peek",                    openssl_ssl_peek                    },
2849
  { "write",                   openssl_ssl_write                   },
2850

2851
  { "renegotiate",             openssl_ssl_renegotiate             },
2852
  { "handshake",               openssl_ssl_do_handshake            },
2853
  { "shutdown",                openssl_ssl_shutdown                },
2854

2855
  { "session_reused",          openssl_ssl_session_reused          },
2856
#if OPENSSL_VERSION_NUMBER < 0x10100000L
2857
  { "set_debug",               openssl_ssl_set_debug               },
2858
#endif
2859
  { "cache_hit",               openssl_ssl_cache_hit               },
2860
  { "renegotiate_abbreviated", openssl_ssl_renegotiate_abbreviated },
2861
  { "renegotiate_pending",     openssl_ssl_renegotiate_pending     },
2862
  { "set_connect_state",       openssl_ssl_set_connect_state       },
2863
  { "set_accept_state",        openssl_ssl_set_accept_state        },
2864
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
2865
  { "get_alpn_selected",       openssl_ssl_get_alpn_selected       },
2866
#endif
2867

2868
  { "__gc",                    openssl_ssl_gc                      },
2869
  { "__tostring",              openssl_ssl_tostring                },
2870

2871
  { NULL,                      NULL                                },
2872
};
2873

2874
int
2875
luaopen_ssl(lua_State *L)
43✔
2876
{
2877
  int i;
2878

2879
  auxiliar_newclass(L, "openssl.ssl_ctx", ssl_ctx_funcs);
43✔
2880
  auxiliar_newclass(L, "openssl.ssl_session", ssl_session_funcs);
43✔
2881
  auxiliar_newclass(L, "openssl.ssl", ssl_funcs);
43✔
2882

2883
  lua_newtable(L);
43✔
2884
  luaL_setfuncs(L, R, 0);
43✔
2885

2886
  auxiliar_enumerate(L, -1, ssl_options);
43✔
2887
  for (i = 0; sVerifyMode_Options[i]; i++) {
215✔
2888
    lua_pushinteger(L, iVerifyMode_Options[i]);
172✔
2889
    lua_setfield(L, -2, sVerifyMode_Options[i]);
172✔
2890
  }
2891
  lua_pushstring(L, DEFAULT_PROTOCOL);
43✔
2892
  lua_setfield(L, -2, "default");
43✔
2893

2894
  lua_pushstring(L, SSL_DEFAULT_CIPHER_LIST);
43✔
2895
  lua_setfield(L, -2, "DEFAULT_CIPHER_LIST");
43✔
2896

2897
#if (OPENSSL_VERSION_NUMBER >= 0x30000000L) && !defined(LIBRESSL_VERSION_NUMBER)
2898
  /* Register PQC TLS integration methods into ssl.ctx */
2899
  ssl_pqc_register_ctx_methods(L, -1);
28✔
2900
#endif
2901

2902
  return 1;
43✔
2903
}
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