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

zhaozg / lua-openssl / 14016924506

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

push

travis-ci

zhaozg
style: .clang-format

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

148 existing lines in 23 files now uncovered.

9291 of 9971 relevant lines covered (93.18%)

1907.56 hits per line

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

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

9
#include "openssl.h"
10
#include "private.h"
11

12
#include <stdint.h>
13

14
static int
15
verify_cb(int preverify_ok, X509_STORE_CTX *xctx, lua_State *L, SSL *ssl, SSL_CTX *ctx)
2,518✔
16
{
17
  int   err = X509_STORE_CTX_get_error(xctx);
2,518✔
18
  int   depth = X509_STORE_CTX_get_error_depth(xctx);
2,518✔
19
  X509 *current = X509_STORE_CTX_get_current_cert(xctx);
2,518✔
20

21
  if (L) {
2,518✔
22
    /* get verify_cert state */
23
    openssl_valueget(L, ssl, "verify_cert");
2,518✔
24
    if (lua_isnil(L, -1)) {
2,518✔
25
      lua_newtable(L);
1,221✔
26
      openssl_valueset(L, ssl, "verify_cert");
1,221✔
27
      openssl_valueget(L, ssl, "verify_cert");
1,221✔
28
    }
29

30
    /* create current verify state table */
31
    lua_newtable(L);
2,518✔
32
    if (preverify_ok != -1) {
2,518✔
33
      lua_pushboolean(L, preverify_ok);
1,503✔
34
      lua_setfield(L, -2, "preverify_ok");
1,503✔
35
    }
36
    lua_pushinteger(L, err);
2,518✔
37
    lua_setfield(L, -2, "error");
2,518✔
38
    lua_pushstring(L, X509_verify_cert_error_string(err));
2,518✔
39
    lua_setfield(L, -2, "error_string");
2,518✔
40
    lua_pushinteger(L, X509_STORE_CTX_get_error_depth(xctx));
2,518✔
41
    lua_setfield(L, -2, "error_depth");
2,518✔
42
    if (current) {
2,518✔
43
      PUSH_OBJECT(current, "openssl.x509");
1,503✔
44
      X509_up_ref(current);
1,503✔
45
      lua_setfield(L, -2, "current_cert");
1,503✔
46
    }
47

48
    openssl_valueget(L, ctx, preverify_ok == -1 ? "cert_verify_cb" : "verify_cb");
2,518✔
49
    if (lua_isfunction(L, -1)) {
2,518✔
50
      /* this is set by  SSL_CTX_set_verify */
51
      lua_pushvalue(L, -2); /* current verify state */
2,102✔
52
      if (lua_pcall(L, 1, 1, 0) == 0) {
2,102✔
53
        preverify_ok = lua_toboolean(L, -1);
2,102✔
54
        lua_pop(L, 1);
2,102✔
55
      } else
UNCOV
56
        luaL_error(L, lua_tostring(L, -1));
×
57
    } else {
58
      int always_continue, verify_depth;
59
      openssl_valueget(L, ctx, "verify_cb_flags");
416✔
60
      /*
61
      int verify_depth;
62
      int always_continue;
63
      */
64
      if (lua_istable(L, -1)) {
416✔
65
        lua_getfield(L, -1, "always_continue");
413✔
66
        always_continue = lua_toboolean(L, -1);
413✔
67
        lua_pop(L, 1);
413✔
68

69
        lua_getfield(L, -1, "verify_depth");
413✔
70
        verify_depth = lua_toboolean(L, -1);
413✔
71
        lua_pop(L, 1);
413✔
72

73
        if (depth > verify_depth) {
413✔
UNCOV
74
          preverify_ok = 0;
×
75
          X509_STORE_CTX_set_error(xctx, X509_V_ERR_CERT_CHAIN_TOO_LONG);
×
76
        }
77
        if (always_continue) preverify_ok = 1;
413✔
78
      }
79
      lua_pop(L, 1);
416✔
80
    }
81

82
    /* set current state to chain */
83
    lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
2,518✔
84

85
    /* balance lua stack */
86
    lua_pop(L, 1);
2,518✔
87
  }
88

89
  return preverify_ok;
2,518✔
90
}
91

92
int
93
openssl_verify_cb(int preverify_ok, X509_STORE_CTX *xctx)
1,503✔
94
{
95
  SSL       *ssl = X509_STORE_CTX_get_ex_data(xctx, SSL_get_ex_data_X509_STORE_CTX_idx());
1,503✔
96
  SSL_CTX   *ctx = ssl ? SSL_get_SSL_CTX(ssl) : NULL;
1,503✔
97
  lua_State *L = ctx ? SSL_CTX_get_app_data(ctx) : NULL;
1,503✔
98
  if (ssl) openssl_newvalue(L, ssl);
1,503✔
99
  return ctx ? verify_cb(preverify_ok, xctx, L, ssl, ctx) : 0;
1,503✔
100
};
101

102
int
103
openssl_cert_verify_cb(X509_STORE_CTX *xctx, void *u)
1,015✔
104
{
105
  int        preverify_ok = 0;
1,015✔
106
  lua_State *L = (lua_State *)u;
1,015✔
107
  SSL       *ssl = X509_STORE_CTX_get_ex_data(xctx, SSL_get_ex_data_X509_STORE_CTX_idx());
1,015✔
108
  SSL_CTX   *ctx = ssl ? SSL_get_SSL_CTX(ssl) : NULL;
1,015✔
109
  if (ssl) openssl_newvalue(L, ssl);
1,015✔
110
  preverify_ok = ctx ? verify_cb(-1, xctx, L, ssl, ctx) : 0;
1,015✔
111
  return preverify_ok == -1 ? 0 : preverify_ok;
1,015✔
112
};
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