summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/openssl.c122
1 files changed, 84 insertions, 38 deletions
diff --git a/src/openssl.c b/src/openssl.c
index 34f893a..8e95622 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -76,12 +76,18 @@
76#include "compat52.h" 76#include "compat52.h"
77#endif 77#endif
78 78
79#define OPENSSL_PREREQ(M, m, p) \
80 (OPENSSL_VERSION_NUMBER >= (((M) << 28) | ((m) << 20) | ((p) << 12)) && !defined LIBRESSL_VERSION_NUMBER)
81
82#define LIBRESSL_PREREQ(M, m, p) \
83 (LIBRESSL_VERSION_NUMBER >= (((M) << 28) | ((m) << 20) | ((p) << 12)))
84
79#ifndef HAVE_DLADDR 85#ifndef HAVE_DLADDR
80#define HAVE_DLADDR (!defined _AIX) /* TODO: https://root.cern.ch/drupal/content/aix-and-dladdr */ 86#define HAVE_DLADDR (!defined _AIX) /* TODO: https://root.cern.ch/drupal/content/aix-and-dladdr */
81#endif 87#endif
82 88
83#ifndef HAVE_SSL_CTX_SET_ALPN_PROTOS 89#ifndef HAVE_SSL_CTX_SET_ALPN_PROTOS
84#define HAVE_SSL_CTX_SET_ALPN_PROTOS (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined LIBRESSL_VERSION_NUMBER) 90#define HAVE_SSL_CTX_SET_ALPN_PROTOS OPENSSL_PREREQ(1, 0, 2)
85#endif 91#endif
86 92
87#ifndef HAVE_SSL_CTX_SET_ALPN_SELECT_CB 93#ifndef HAVE_SSL_CTX_SET_ALPN_SELECT_CB
@@ -96,6 +102,30 @@
96#define HAVE_SSL_GET0_ALPN_SELECTED HAVE_SSL_CTX_SET_ALPN_PROTOS 102#define HAVE_SSL_GET0_ALPN_SELECTED HAVE_SSL_CTX_SET_ALPN_PROTOS
97#endif 103#endif
98 104
105#ifndef HAVE_DTLSV1_CLIENT_METHOD
106#define HAVE_DTLSV1_CLIENT_METHOD (!defined OPENSSL_NO_DTLS1)
107#endif
108
109#ifndef HAVE_DTLSV1_SERVER_METHOD
110#define HAVE_DTLSV1_SERVER_METHOD HAVE_DTLSV1_CLIENT_METHOD
111#endif
112
113#ifndef HAVE_DTLS_CLIENT_METHOD
114#define HAVE_DTLS_CLIENT_METHOD (OPENSSL_PREREQ(1, 0, 2) && !defined OPENSSL_NO_DTLS1)
115#endif
116
117#ifndef HAVE_DTLS_SERVER_METHOD
118#define HAVE_DTLS_SERVER_METHOD HAVE_DTLS_CLIENT_METHOD
119#endif
120
121#ifndef HAVE_DTLSV1_2_CLIENT_METHOD
122#define HAVE_DTLSV1_2_CLIENT_METHOD (OPENSSL_PREREQ(1, 0, 2) && !defined OPENSSL_NO_DTLS1)
123#endif
124
125#ifndef HAVE_DTLSV1_2_SERVER_METHOD
126#define HAVE_DTLSV1_2_SERVER_METHOD HAVE_DTLSV1_2_CLIENT_METHOD
127#endif
128
99#ifndef STRERROR_R_CHAR_P 129#ifndef STRERROR_R_CHAR_P
100#define STRERROR_R_CHAR_P (defined __GLIBC__ && (_GNU_SOURCE || !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600))) 130#define STRERROR_R_CHAR_P (defined __GLIBC__ && (_GNU_SOURCE || !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)))
101#endif 131#endif
@@ -279,7 +309,13 @@ static void addclass(lua_State *L, const char *name, const luaL_Reg *methods, co
279} /* addclass() */ 309} /* addclass() */
280 310
281 311
282static int checkoption(struct lua_State *L, int index, const char *def, const char *const opts[]) { 312static int badoption(lua_State *L, int index, const char *opt) {
313 opt = (opt)? opt : luaL_checkstring(L, index);
314
315 return luaL_argerror(L, index, lua_pushfstring(L, "invalid option %s", opt));
316} /* badoption() */
317
318static int checkoption(lua_State *L, int index, const char *def, const char *const opts[]) {
283 const char *opt = (def)? luaL_optstring(L, index, def) : luaL_checkstring(L, index); 319 const char *opt = (def)? luaL_optstring(L, index, def) : luaL_checkstring(L, index);
284 int i; 320 int i;
285 321
@@ -288,7 +324,7 @@ static int checkoption(struct lua_State *L, int index, const char *def, const ch
288 return i; 324 return i;
289 } 325 }
290 326
291 return luaL_argerror(L, index, lua_pushfstring(L, "invalid option %s", opt)); 327 return badoption(L, index, opt);
292} /* checkoption() */ 328} /* checkoption() */
293 329
294 330
@@ -820,7 +856,6 @@ static void compat_init_X509_STORE_onfree(void *store, void *data NOTUSED, CRYPT
820static int compat_init(void) { 856static int compat_init(void) {
821 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 857 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
822 static int store_index = -1, ssl_ctx_index = -1, done; 858 static int store_index = -1, ssl_ctx_index = -1, done;
823 X509_STORE *store;
824 int error = 0; 859 int error = 0;
825 860
826 if ((error = pthread_mutex_lock(&mutex))) 861 if ((error = pthread_mutex_lock(&mutex)))
@@ -1615,7 +1650,6 @@ static int bn__mul(lua_State *L) {
1615 1650
1616static int bn__div(lua_State *L) { 1651static int bn__div(lua_State *L) {
1617 BIGNUM *r, *a, *b; 1652 BIGNUM *r, *a, *b;
1618 BN_CTX *ctx;
1619 1653
1620 bn_prepops(L, &r, &a, &b, 0); 1654 bn_prepops(L, &r, &a, &b, 0);
1621 1655
@@ -1628,7 +1662,6 @@ static int bn__div(lua_State *L) {
1628 1662
1629static int bn__mod(lua_State *L) { 1663static int bn__mod(lua_State *L) {
1630 BIGNUM *r, *a, *b; 1664 BIGNUM *r, *a, *b;
1631 BN_CTX *ctx;
1632 1665
1633 bn_prepops(L, &r, &a, &b, 0); 1666 bn_prepops(L, &r, &a, &b, 0);
1634 1667
@@ -1641,7 +1674,6 @@ static int bn__mod(lua_State *L) {
1641 1674
1642static int bn__pow(lua_State *L) { 1675static int bn__pow(lua_State *L) {
1643 BIGNUM *r, *a, *b; 1676 BIGNUM *r, *a, *b;
1644 BN_CTX *ctx;
1645 1677
1646 bn_prepops(L, &r, &a, &b, 0); 1678 bn_prepops(L, &r, &a, &b, 0);
1647 1679
@@ -2291,7 +2323,6 @@ static int pk__tostring(lua_State *L) {
2291 BIO *bio = getbio(L); 2323 BIO *bio = getbio(L);
2292 char *data; 2324 char *data;
2293 long len; 2325 long len;
2294 int ok = 0;
2295 2326
2296 switch (type) { 2327 switch (type) {
2297 case X509_PEM: 2328 case X509_PEM:
@@ -2843,7 +2874,6 @@ static int xe_new(lua_State *L) {
2843 2874
2844 CONF *conf = NULL; 2875 CONF *conf = NULL;
2845 X509V3_CTX cbuf = { 0 }, *ctx = NULL; 2876 X509V3_CTX cbuf = { 0 }, *ctx = NULL;
2846 X509_EXTENSION *ext = NULL;
2847 2877
2848 if (!lua_isnil(L, 3)) { 2878 if (!lua_isnil(L, 3)) {
2849 const char *cdata = luaL_checkstring(L, 3); 2879 const char *cdata = luaL_checkstring(L, 3);
@@ -3166,7 +3196,6 @@ static double timeutc(ASN1_TIME *time) {
3166 char buf[32] = "", *cp; 3196 char buf[32] = "", *cp;
3167 struct tm tm = { 0 }; 3197 struct tm tm = { 0 };
3168 int gmtoff = 0, year, i; 3198 int gmtoff = 0, year, i;
3169 double ts;
3170 3199
3171 if (!ASN1_TIME_check(time)) 3200 if (!ASN1_TIME_check(time))
3172 return 0; 3201 return 0;
@@ -3264,7 +3293,6 @@ static int xc_getLifetime(lua_State *L) {
3264 3293
3265static int xc_setLifetime(lua_State *L) { 3294static int xc_setLifetime(lua_State *L) {
3266 X509 *crt = checksimple(L, 1, X509_CERT_CLASS); 3295 X509 *crt = checksimple(L, 1, X509_CERT_CLASS);
3267 ASN1_TIME *time;
3268 double ut; 3296 double ut;
3269 const char *dt; 3297 const char *dt;
3270 3298
@@ -4197,7 +4225,6 @@ static int xx_getLastUpdate(lua_State *L) {
4197static int xx_setLastUpdate(lua_State *L) { 4225static int xx_setLastUpdate(lua_State *L) {
4198 X509_CRL *crl = checksimple(L, 1, X509_CRL_CLASS); 4226 X509_CRL *crl = checksimple(L, 1, X509_CRL_CLASS);
4199 double updated = luaL_checknumber(L, 2); 4227 double updated = luaL_checknumber(L, 2);
4200 ASN1_TIME *time = NULL;
4201 4228
4202 /* lastUpdate always present */ 4229 /* lastUpdate always present */
4203 if (!ASN1_TIME_set(X509_CRL_get_lastUpdate(crl), updated)) 4230 if (!ASN1_TIME_set(X509_CRL_get_lastUpdate(crl), updated))
@@ -4965,15 +4992,17 @@ int luaopen__openssl_pkcs12(lua_State *L) {
4965 */ 4992 */
4966static int sx_new(lua_State *L) { 4993static int sx_new(lua_State *L) {
4967 static const char *const opts[] = { 4994 static const char *const opts[] = {
4968 "SSLv2", "SSLv3", "SSLv23", 4995 [0] = "SSL",
4969 "TLSv1", "TLSv1.0", 4996 [1] = "TLS",
4970#if defined SSL_OP_NO_TLSv1_1 4997 [2] = "SSLv2",
4971 "TLSv1_1", "TLSv1.1", 4998 [3] = "SSLv3",
4972#endif 4999 [4] = "SSLv23",
4973#if defined SSL_OP_NO_TLSv1_2 5000 [5] = "TLSv1", [6] = "TLSv1.0",
4974 "TLSv1_2", "TLSv1.2", 5001 [7] = "TLSv1_1", [8] = "TLSv1.1",
4975#endif 5002 [9] = "TLSv1_2", [10] = "TLSv1.2",
4976 "SSL", "TLS", 5003 [11] = "DTLS",
5004 [12] = "DTLSv1", [13] = "DTLSv1.0",
5005 [14] = "DTLSv1_2", [15] = "DTLSv1.2",
4977 NULL 5006 NULL
4978 }; 5007 };
4979 /* later versions of SSL declare a const qualifier on the return type */ 5008 /* later versions of SSL declare a const qualifier on the return type */
@@ -4986,41 +5015,60 @@ static int sx_new(lua_State *L) {
4986 srv = lua_toboolean(L, 2); 5015 srv = lua_toboolean(L, 2);
4987 5016
4988 switch (checkoption(L, 1, "TLS", opts)) { 5017 switch (checkoption(L, 1, "TLS", opts)) {
5018 case 0: /* SSL */
5019 method = (srv)? &SSLv23_server_method : &SSLv23_client_method;
5020 options = SSL_OP_NO_SSLv2;
5021 break;
5022 case 1: /* TLS */
5023 method = (srv)? &SSLv23_server_method : &SSLv23_client_method;
5024 options = SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3;
5025 break;
4989#ifndef OPENSSL_NO_SSL2 5026#ifndef OPENSSL_NO_SSL2
4990 case 0: /* SSLv2 */ 5027 case 2: /* SSLv2 */
4991 method = (srv)? &SSLv2_server_method : &SSLv2_client_method; 5028 method = (srv)? &SSLv2_server_method : &SSLv2_client_method;
4992 break; 5029 break;
4993#endif 5030#endif
4994 case 1: /* SSLv3 */ 5031 case 3: /* SSLv3 */
4995 method = (srv)? &SSLv3_server_method : &SSLv3_client_method; 5032 method = (srv)? &SSLv3_server_method : &SSLv3_client_method;
4996 break; 5033 break;
4997 case 2: /* SSLv23 */ 5034 case 4: /* SSLv23 */
4998 method = (srv)? &SSLv23_server_method : &SSLv23_client_method; 5035 method = (srv)? &SSLv23_server_method : &SSLv23_client_method;
4999 break; 5036 break;
5000 case 3: /* TLSv1 */ 5037 case 5: /* TLSv1 */
5001 case 4: /* TLSv1.0 */ 5038 case 6: /* TLSv1.0 */
5002 method = (srv)? &TLSv1_server_method : &TLSv1_client_method; 5039 method = (srv)? &TLSv1_server_method : &TLSv1_client_method;
5003 break; 5040 break;
5004#if defined SSL_OP_NO_TLSv1_1 5041#if defined SSL_OP_NO_TLSv1_1
5005 case 5: /* TLSv1_1 */ 5042 case 7: /* TLSv1_1 */
5006 case 6: /* TLSv1.1 */ 5043 case 8: /* TLSv1.1 */
5007 method = (srv)? &TLSv1_1_server_method : &TLSv1_1_client_method; 5044 method = (srv)? &TLSv1_1_server_method : &TLSv1_1_client_method;
5008 break; 5045 break;
5009#endif 5046#endif
5010#if defined SSL_OP_NO_TLSv1_2 5047#if defined SSL_OP_NO_TLSv1_2
5011 case 7: /* TLSv1_2 */ 5048 case 9: /* TLSv1_2 */
5012 case 8: /* TLSv1.2 */ 5049 case 10: /* TLSv1.2 */
5013 method = (srv)? &TLSv1_2_server_method : &TLSv1_2_client_method; 5050 method = (srv)? &TLSv1_2_server_method : &TLSv1_2_client_method;
5014 break; 5051 break;
5015#endif 5052#endif
5016 case 9: /* SSL */ 5053#if HAVE_DTLS_CLIENT_METHOD
5017 method = (srv)? &SSLv23_server_method : &SSLv23_client_method; 5054 case 11: /* DTLS */
5018 options = SSL_OP_NO_SSLv2; 5055 method = (srv)? &DTLS_server_method : &DTLS_client_method;
5019 break; 5056 break;
5020 case 10: /* TLS */ 5057#endif
5021 method = (srv)? &SSLv23_server_method : &SSLv23_client_method; 5058#if HAVE_DTLSV1_CLIENT_METHOD
5022 options = SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3; 5059 case 12: /* DTLSv1 */
5060 case 13: /* DTLSv1.0 */
5061 method = (srv)? &DTLSv1_server_method : &DTLSv1_client_method;
5062 break;
5063#endif
5064#if HAVE_DTLSV1_2_CLIENT_METHOD
5065 case 14: /* DTLSv1_2 */
5066 case 15: /* DTLSv1.2 */
5067 method = (srv)? &DTLSv1_server_method : &DTLSv1_client_method;
5023 break; 5068 break;
5069#endif
5070 default:
5071 return badoption(L, 1, NULL);
5024 } 5072 }
5025 5073
5026 ud = prepsimple(L, SSL_CTX_CLASS); 5074 ud = prepsimple(L, SSL_CTX_CLASS);
@@ -5302,7 +5350,6 @@ noack:
5302 5350
5303static int sx_setAlpnSelect(lua_State *L) { 5351static int sx_setAlpnSelect(lua_State *L) {
5304 SSL_CTX *ctx = checksimple(L, 1, SSL_CTX_CLASS); 5352 SSL_CTX *ctx = checksimple(L, 1, SSL_CTX_CLASS);
5305 struct ex_data *data;
5306 int error; 5353 int error;
5307 5354
5308 luaL_checktype(L, 2, LUA_TFUNCTION); 5355 luaL_checktype(L, 2, LUA_TFUNCTION);
@@ -5786,7 +5833,6 @@ static void md_update_(lua_State *L, EVP_MD_CTX *ctx, int from, int to) {
5786 5833
5787static int md_update(lua_State *L) { 5834static int md_update(lua_State *L) {
5788 EVP_MD_CTX *ctx = luaL_checkudata(L, 1, DIGEST_CLASS); 5835 EVP_MD_CTX *ctx = luaL_checkudata(L, 1, DIGEST_CLASS);
5789 int i;
5790 5836
5791 md_update_(L, ctx, 2, lua_gettop(L)); 5837 md_update_(L, ctx, 2, lua_gettop(L));
5792 5838