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

zhaozg / lua-openssl / 17889091673

21 Sep 2025 04:42AM UTC coverage: 93.583%. Remained the same
17889091673

Pull #325

travis-ci

Copilot
Comprehensive LDoc documentation improvements and fixes

Co-authored-by: zhaozg <542599+zhaozg@users.noreply.github.com>
Pull Request #325: Comprehensive LDoc documentation improvements: 100% module coverage and enhanced function documentation

246 of 252 new or added lines in 15 files covered. (97.62%)

71 existing lines in 2 files now uncovered.

9640 of 10301 relevant lines covered (93.58%)

2241.2 hits per line

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

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

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

11
Diffie-Hellman (DH) key exchange is a method of securely exchanging 
12
cryptographic keys over a public channel. The module provides functionality
13
for DH parameter generation, key generation and key agreement operations.
14

15
@module dh
16
@usage
17
  dh = require('openssl').dh
18
*/
19
#include <openssl/dh.h>
20
#include <openssl/engine.h>
21

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

25
#if !defined(OPENSSL_NO_DH)
26
static LUA_FUNCTION(openssl_dh_free)
16✔
27
{
28
  DH *dh = CHECK_OBJECT(1, DH, "openssl.dh");
16✔
29
  DH_free(dh);
16✔
30
  return 0;
16✔
31
};
32

33
static LUA_FUNCTION(openssl_dh_parse)
8✔
34
{
35
  const BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *pri = NULL;
8✔
36
  DH           *dh = CHECK_OBJECT(1, DH, "openssl.dh");
8✔
37
  lua_newtable(L);
8✔
38

39
  lua_pushinteger(L, DH_size(dh));
8✔
40
  lua_setfield(L, -2, "size");
8✔
41

42
  lua_pushinteger(L, DH_bits(dh));
8✔
43
  lua_setfield(L, -2, "bits");
8✔
44

45
  DH_get0_pqg(dh, &p, &q, &g);
8✔
46
  DH_get0_key(dh, &pub, &pri);
8✔
47

48
  OPENSSL_PKEY_GET_BN(p, p);
8✔
49
  OPENSSL_PKEY_GET_BN(q, q);
8✔
50
  OPENSSL_PKEY_GET_BN(g, g);
8✔
51
  OPENSSL_PKEY_GET_BN(pub, pub_key);
8✔
52
  OPENSSL_PKEY_GET_BN(pri, priv_key);
8✔
53

54
  return 1;
8✔
55
}
56

57
static LUA_FUNCTION(openssl_dh_check)
8✔
58
{
59
  const DH *dh = CHECK_OBJECT(1, DH, "openssl.dh");
8✔
60
  int       ret = 0;
8✔
61
  int       codes = 0;
8✔
62

63
  if (lua_isuserdata(L, 2)) {
8✔
64
    const BIGNUM *pub = CHECK_OBJECT(2, BIGNUM, "openssl.bn");
4✔
65
    ret = DH_check_pub_key(dh, pub, &codes);
4✔
66
  } else
67
    ret = DH_check(dh, &codes);
4✔
68

69
  lua_pushboolean(L, ret);
8✔
70
  lua_pushinteger(L, codes);
8✔
71
  return 2;
8✔
72
}
73

74
static int
75
openssl_dh_generate_parameters(lua_State *L)
4✔
76
{
77
  int     bits = luaL_optint(L, 1, 1024);
4✔
78
  int     generator = luaL_optint(L, 2, 2);
4✔
79
  ENGINE *eng = lua_isnoneornil(L, 3) ? NULL : CHECK_OBJECT(3, ENGINE, "openssl.engine");
4✔
80
  int     ret = 0;
4✔
81

82
  DH *dh = eng ? DH_new_method(eng) : DH_new();
4✔
83
  ret = DH_generate_parameters_ex(dh, bits, generator, NULL);
4✔
84

85
  if (ret == 1) {
4✔
86
    PUSH_OBJECT(dh, "openssl.dh");
4✔
87
    return 1;
4✔
88
  }
NEW
89
  DH_free(dh);
×
NEW
90
  return openssl_pushresult(L, ret);
×
91
}
92

93
static int
94
openssl_dh_generate_key(lua_State *L)
4✔
95
{
96
  DH *dhparamater = CHECK_OBJECT(1, DH, "openssl.dh");
4✔
97
  DH *dh = DHparams_dup(dhparamater);
4✔
98

99
  int ret = DH_generate_key(dh);
4✔
100
  if (ret == 1) {
4✔
101
    PUSH_OBJECT(dh, "openssl.dh");
4✔
102
    return 1;
4✔
103
  }
104
  DH_free(dh);
×
105
  return openssl_pushresult(L, ret);
×
106
}
107

108
static luaL_Reg dh_funs[] = {
109
  { "generate_key", openssl_dh_generate_key },
110
  { "parse",        openssl_dh_parse        },
111
  { "check",        openssl_dh_check        },
112

113
  { "__gc",         openssl_dh_free         },
114
  { "__tostring",   auxiliar_tostring       },
115

116
  { NULL,           NULL                    }
117
};
118

119
static LuaL_Enumeration dh_problems[] = {
120
  { "DH_CHECK_P_NOT_PRIME",         DH_CHECK_P_NOT_PRIME         },
121
  { "DH_CHECK_P_NOT_SAFE_PRIME",    DH_CHECK_P_NOT_SAFE_PRIME    },
122
  { "DH_UNABLE_TO_CHECK_GENERATOR", DH_UNABLE_TO_CHECK_GENERATOR },
123
  { "DH_NOT_SUITABLE_GENERATOR",    DH_NOT_SUITABLE_GENERATOR    },
124
#ifdef DH_CHECK_Q_NOT_PRIME
125
  { "DH_CHECK_Q_NOT_PRIME",         DH_CHECK_Q_NOT_PRIME         },
126
#endif
127
#ifdef DH_CHECK_INVALID_Q_VALUE
128
  { "DH_CHECK_INVALID_Q_VALUE",     DH_CHECK_INVALID_Q_VALUE     },
129
#endif
130
#ifdef DH_CHECK_INVALID_J_VALUE
131
  { "DH_CHECK_INVALID_J_VALUE",     DH_CHECK_INVALID_J_VALUE     },
132
#endif
133

134
  { "DH_CHECK_PUBKEY_TOO_SMALL",    DH_CHECK_PUBKEY_TOO_SMALL    },
135
  { "DH_CHECK_PUBKEY_TOO_LARGE",    DH_CHECK_PUBKEY_TOO_LARGE    },
136
#ifdef DH_CHECK_PUBKEY_INVALID
137
  { "DH_CHECK_PUBKEY_INVALID",      DH_CHECK_PUBKEY_INVALID      },
138
#endif
139

140
  { NULL,                           -1                           }
141
};
142

143
static int
144
openssl_dh_problems(lua_State *L)
8✔
145
{
146
  int reason = luaL_checkint(L, 1);
8✔
147
  int pub = lua_toboolean(L, 2);
8✔
148
  int i = 1;
8✔
149

150
#define VAL(r, v)                                                                                  \
151
  if (r & DH_##v) {                                                                                \
152
    lua_pushliteral(L, #v);                                                                        \
153
    lua_rawseti(L, -2, i++);                                                                       \
154
  }
155

156
  lua_newtable(L);
8✔
157
  if (pub) {
8✔
158
    VAL(reason, CHECK_PUBKEY_TOO_SMALL);
4✔
159
    VAL(reason, CHECK_PUBKEY_TOO_LARGE);
4✔
160
#ifdef DH_CHECK_PUBKEY_INVALID
161
    VAL(reason, CHECK_PUBKEY_INVALID);
4✔
162
#endif
163
  } else {
164
    VAL(reason, CHECK_P_NOT_PRIME);
4✔
165
    VAL(reason, CHECK_PUBKEY_TOO_SMALL);
4✔
166
    VAL(reason, UNABLE_TO_CHECK_GENERATOR);
4✔
167
    VAL(reason, NOT_SUITABLE_GENERATOR);
4✔
168

169
#ifdef DH_CHECK_Q_NOT_PRIME
170
    VAL(reason, CHECK_Q_NOT_PRIME);
4✔
171
#endif
172
#ifdef DH_CHECK_INVALID_Q_VALUE
173
    VAL(reason, CHECK_INVALID_Q_VALUE);
4✔
174
#endif
175
#ifdef DH_CHECK_INVALID_J_VALUE
176
    VAL(reason, CHECK_INVALID_J_VALUE);
4✔
177
#endif
178
  }
179

180
#undef VAL
181

182
  return 1;
8✔
183
}
184

185
static luaL_Reg R[] = {
186
  { "generate_parameters", openssl_dh_generate_parameters },
187
  { "problems",            openssl_dh_problems            },
188

189
  { NULL,                  NULL                           }
190
};
191

192
int
193
luaopen_dh(lua_State *L)
43✔
194
{
195
  auxiliar_newclass(L, "openssl.dh", dh_funs);
43✔
196

197
  lua_newtable(L);
43✔
198
  luaL_setfuncs(L, R, 0);
43✔
199

200
  auxiliar_enumerate(L, -1, dh_problems);
43✔
201

202
  return 1;
43✔
203
}
204
#endif
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