Annotate

util-src/strbitop.c @ 13801:a5d5fefb8b68 13.0

mod_tls: Enable Prosody's certificate checking for incoming s2s connections (fixes #1916) (thanks Damian, Zash) Various options in Prosody allow control over the behaviour of the certificate verification process For example, some deployments choose to allow falling back to traditional "dialback" authentication (XEP-0220), while others verify via DANE, hard-coded fingerprints, or other custom plugins. Implementing this flexibility requires us to override OpenSSL's default certificate verification, to allow Prosody to verify the certificate itself, apply custom policies and make decisions based on the outcome. To enable our custom logic, we have to suppress OpenSSL's default behaviour of aborting the connection with a TLS alert message. With LuaSec, this can be achieved by using the verifyext "lsec_continue" flag. We also need to use the lsec_ignore_purpose flag, because XMPP s2s uses server certificates as "client" certificates (for mutual TLS verification in outgoing s2s connections). Commit 99d2100d2918 moved these settings out of the defaults and into mod_s2s, because we only really need these changes for s2s, and they should be opt-in, rather than automatically applied to all TLS services we offer. That commit was incomplete, because it only added the flags for incoming direct TLS connections. StartTLS connections are handled by mod_tls, which was not applying the lsec_* flags. It previously worked because they were already in the defaults. This resulted in incoming s2s connections with "invalid" certificates being aborted early by OpenSSL, even if settings such as `s2s_secure_auth = false` or DANE were present in the config. Outgoing s2s connections inherit verify "none" from the defaults, which means OpenSSL will receive the cert but will not terminate the connection when it is deemed invalid. This means we don't need lsec_continue there, and we also don't need lsec_ignore_purpose (because the remote peer is a "server"). Wondering why we can't just use verify "none" for incoming s2s? It's because in that mode, OpenSSL won't request a certificate from the peer for incoming connections. Setting verify "peer" is how you ask OpenSSL to request a certificate from the client, but also what triggers its built-in verification.
author Matthew Wild <mwild1@gmail.com>
date Tue, 01 Apr 2025 17:26:56 +0100
parent 13429:6cdc6923d65a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 /*
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 * This project is MIT licensed. Please see the
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 * COPYING file in the source package for more information.
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 *
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
5 * Copyright (C) 2016 Kim Alvefur
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 */
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 #include <lua.h>
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 #include <lauxlib.h>
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
13429
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
11 #include <sys/param.h>
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
12 #include <limits.h>
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 /* TODO Deduplicate code somehow */
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
12469
2b3adaa6d38e util.strbitop: Reduce scope of functions
Kim Alvefur <zash@zash.se>
parents: 11175
diff changeset
16 static int strop_and(lua_State *L) {
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 size_t a, b, i;
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
19 const char *str_a = luaL_checklstring(L, 1, &a);
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
20 const char *str_b = luaL_checklstring(L, 2, &b);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21
11175
235537247aa3 Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents: 11172
diff changeset
22 luaL_buffinit(L, &buf);
235537247aa3 Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents: 11172
diff changeset
23
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 for(i = 0; i < a; i++) {
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
30 luaL_addchar(&buf, str_a[i] & str_b[i % b]);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36
12469
2b3adaa6d38e util.strbitop: Reduce scope of functions
Kim Alvefur <zash@zash.se>
parents: 11175
diff changeset
37 static int strop_or(lua_State *L) {
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 size_t a, b, i;
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
40 const char *str_a = luaL_checklstring(L, 1, &a);
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
41 const char *str_b = luaL_checklstring(L, 2, &b);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42
11175
235537247aa3 Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents: 11172
diff changeset
43 luaL_buffinit(L, &buf);
235537247aa3 Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents: 11172
diff changeset
44
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 for(i = 0; i < a; i++) {
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
51 luaL_addchar(&buf, str_a[i] | str_b[i % b]);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57
12469
2b3adaa6d38e util.strbitop: Reduce scope of functions
Kim Alvefur <zash@zash.se>
parents: 11175
diff changeset
58 static int strop_xor(lua_State *L) {
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 size_t a, b, i;
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
61 const char *str_a = luaL_checklstring(L, 1, &a);
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
62 const char *str_b = luaL_checklstring(L, 2, &b);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
64 luaL_buffinit(L, &buf);
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
65
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 for(i = 0; i < a; i++) {
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
72 luaL_addchar(&buf, str_a[i] ^ str_b[i % b]);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78
13429
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
79 unsigned int clz(unsigned char c) {
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
80 #if __GNUC__
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
81 return __builtin_clz((unsigned int) c) - ((sizeof(int)-1)*CHAR_BIT);
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
82 #else
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
83 if(c & 0x80) return 0;
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
84 if(c & 0x40) return 1;
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
85 if(c & 0x20) return 2;
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
86 if(c & 0x10) return 3;
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
87 if(c & 0x08) return 4;
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
88 if(c & 0x04) return 5;
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
89 if(c & 0x02) return 6;
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
90 if(c & 0x01) return 7;
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
91 return 8;
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
92 #endif
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
93 }
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
94
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
95 LUA_API int strop_common_prefix_bits(lua_State *L) {
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
96 size_t a, b, i;
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
97 const char *str_a = luaL_checklstring(L, 1, &a);
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
98 const char *str_b = luaL_checklstring(L, 2, &b);
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
99
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
100 size_t min_len = MIN(a, b);
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
101
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
102 for(i=0; i<min_len; i++) {
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
103 if(str_a[i] != str_b[i]) {
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
104 lua_pushinteger(L, i*8 + (clz(str_a[i] ^ str_b[i])));
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
105 return 1;
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
106 }
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
107 }
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
108
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
109 lua_pushinteger(L, i*8);
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
110 return 1;
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
111 }
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
112
12976
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12575
diff changeset
113 LUA_API int luaopen_prosody_util_strbitop(lua_State *L) {
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 luaL_Reg exports[] = {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 { "sand", strop_and },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 { "sor", strop_or },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 { "sxor", strop_xor },
13429
6cdc6923d65a util.strbitop: Add common_prefix_bits() method
Matthew Wild <mwild1@gmail.com>
parents: 12976
diff changeset
118 { "common_prefix_bits", strop_common_prefix_bits },
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 { NULL, NULL }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 };
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 lua_newtable(L);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 luaL_setfuncs(L, exports, 0);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 }
12976
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12575
diff changeset
126
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12575
diff changeset
127 LUA_API int luaopen_util_strbitop(lua_State *L) {
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12575
diff changeset
128 return luaopen_prosody_util_strbitop(L);
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12575
diff changeset
129 }