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

zhaozg / lua-openssl / 21310072613

24 Jan 2026 05:30AM UTC coverage: 88.396% (-5.5%) from 93.89%
21310072613

push

travis-ci

zhaozg
Correct the function's type definition for portability

5 of 5 new or added lines in 2 files covered. (100.0%)

515 existing lines in 15 files now uncovered.

9149 of 10350 relevant lines covered (88.4%)

1287.19 hits per line

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

88.52
/src/callback.c
1
/*=========================================================================*\
2
* callback.c
3
* callback for lua-openssl binding
4
*
5
* Author:  george zhao <zhaozg(at)gmail.com>
6
\*=========================================================================*/
7

8
/***
9
callback module for lua-openssl binding
10

11
This module provides callback functionality for SSL/TLS operations,
12
including certificate verification callbacks and other SSL event handling.
13
These callbacks allow customization of SSL/TLS behavior from Lua.
14

15
@module callback
16
@usage
17
  -- Internal module used by SSL module
18
*/
19
#include <openssl/ssl.h>
20

21
#include "openssl.h"
22
#include "private.h"
23

24
#include <stdint.h>
25

26
static int
27
verify_cb(int preverify_ok, X509_STORE_CTX *xctx, lua_State *L, SSL *ssl, SSL_CTX *ctx)
6✔
28
{
29
  int   err = X509_STORE_CTX_get_error(xctx);
6✔
30
  int   depth = X509_STORE_CTX_get_error_depth(xctx);
6✔
31
  X509 *current = X509_STORE_CTX_get_current_cert(xctx);
6✔
32

33
  if (L) {
6✔
34
    /* get verify_cert state */
35
    openssl_valueget(L, ssl, "verify_cert");
6✔
36
    if (lua_isnil(L, -1)) {
6✔
37
      lua_newtable(L);
5✔
38
      openssl_valueset(L, ssl, "verify_cert");
5✔
39
      openssl_valueget(L, ssl, "verify_cert");
5✔
40
    }
41

42
    /* create current verify state table */
43
    lua_newtable(L);
6✔
44
    if (preverify_ok != -1) {
6✔
45
      lua_pushboolean(L, preverify_ok);
1✔
46
      lua_setfield(L, -2, "preverify_ok");
1✔
47
    }
48
    lua_pushinteger(L, err);
6✔
49
    lua_setfield(L, -2, "error");
6✔
50
    lua_pushstring(L, X509_verify_cert_error_string(err));
6✔
51
    lua_setfield(L, -2, "error_string");
6✔
52
    lua_pushinteger(L, X509_STORE_CTX_get_error_depth(xctx));
6✔
53
    lua_setfield(L, -2, "error_depth");
6✔
54
    if (current) {
6✔
55
      PUSH_OBJECT(current, "openssl.x509");
1✔
56
      X509_up_ref(current);
1✔
57
      lua_setfield(L, -2, "current_cert");
1✔
58
    }
59

60
    openssl_valueget(L, ctx, preverify_ok == -1 ? "cert_verify_cb" : "verify_cb");
6✔
61
    if (lua_isfunction(L, -1)) {
6✔
62
      /* this is set by  SSL_CTX_set_verify */
UNCOV
63
      lua_pushvalue(L, -2); /* current verify state */
×
UNCOV
64
      if (lua_pcall(L, 1, 1, 0) == 0) {
×
UNCOV
65
        preverify_ok = lua_toboolean(L, -1);
×
UNCOV
66
        lua_pop(L, 1);
×
67
      } else
68
        luaL_error(L, lua_tostring(L, -1));
×
69
    } else {
70
      int always_continue, verify_depth;
71
      openssl_valueget(L, ctx, "verify_cb_flags");
6✔
72
      /*
73
      int verify_depth;
74
      int always_continue;
75
      */
76
      if (lua_istable(L, -1)) {
6✔
77
        lua_getfield(L, -1, "always_continue");
5✔
78
        always_continue = lua_toboolean(L, -1);
5✔
79
        lua_pop(L, 1);
5✔
80

81
        lua_getfield(L, -1, "verify_depth");
5✔
82
        verify_depth = lua_toboolean(L, -1);
5✔
83
        lua_pop(L, 1);
5✔
84

85
        if (depth > verify_depth) {
5✔
86
          preverify_ok = 0;
×
87
          X509_STORE_CTX_set_error(xctx, X509_V_ERR_CERT_CHAIN_TOO_LONG);
×
88
        }
89
        if (always_continue) preverify_ok = 1;
5✔
90
      }
91
      lua_pop(L, 1);
6✔
92
    }
93

94
    /* set current state to chain */
95
    lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
6✔
96

97
    /* balance lua stack */
98
    lua_pop(L, 1);
6✔
99
  }
100

101
  return preverify_ok;
6✔
102
}
103

104
int
105
openssl_verify_cb(int preverify_ok, X509_STORE_CTX *xctx)
1✔
106
{
107
  SSL       *ssl = X509_STORE_CTX_get_ex_data(xctx, SSL_get_ex_data_X509_STORE_CTX_idx());
1✔
108
  SSL_CTX   *ctx = ssl ? SSL_get_SSL_CTX(ssl) : NULL;
1✔
109
  lua_State *L = ctx ? SSL_CTX_get_app_data(ctx) : NULL;
1✔
110
  if (ssl) openssl_newvalue(L, ssl);
1✔
111
  return ctx ? verify_cb(preverify_ok, xctx, L, ssl, ctx) : 0;
1✔
112
};
113

114
/***
115
certificate verification callback function
116
@function cert_verify_cb
117
@tparam x509_store_ctx ctx X509 store context for verification
118
@tparam userdata u user data passed to callback
119
@treturn number verification result (1 for success, 0 for failure)
120
*/
121
int
122
openssl_cert_verify_cb(X509_STORE_CTX *xctx, void *u)
5✔
123
{
124
  int        preverify_ok = 0;
5✔
125
  lua_State *L = (lua_State *)u;
5✔
126
  SSL       *ssl = X509_STORE_CTX_get_ex_data(xctx, SSL_get_ex_data_X509_STORE_CTX_idx());
5✔
127
  SSL_CTX   *ctx = ssl ? SSL_get_SSL_CTX(ssl) : NULL;
5✔
128
  if (ssl) openssl_newvalue(L, ssl);
5✔
129
  preverify_ok = ctx ? verify_cb(-1, xctx, L, ssl, ctx) : 0;
5✔
130
  return preverify_ok == -1 ? 0 : preverify_ok;
5✔
131
};
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