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

zhaozg / lua-openssl / 14788361818

02 May 2025 03:43AM UTC coverage: 88.812% (-4.7%) from 93.466%
14788361818

push

travis-ci

zhaozg
ci: valgrind combine

8954 of 10082 relevant lines covered (88.81%)

1094.64 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
#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)
6✔
16
{
17
  int   err = X509_STORE_CTX_get_error(xctx);
6✔
18
  int   depth = X509_STORE_CTX_get_error_depth(xctx);
6✔
19
  X509 *current = X509_STORE_CTX_get_current_cert(xctx);
6✔
20

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

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

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

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

73
        if (depth > verify_depth) {
5✔
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;
5✔
78
      }
79
      lua_pop(L, 1);
6✔
80
    }
81

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

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

89
  return preverify_ok;
6✔
90
}
91

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

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

© 2025 Coveralls, Inc