| 1 |
--[[ |
| 2 |
Copyright (c) 2022, Vsevolod Stakhov <[email protected]> |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
]]-- |
| 16 |
|
| 17 |
--[[[ |
| 18 |
-- @module lua_ffi/dkim |
| 19 |
-- This module contains ffi interfaces to DKIM |
| 20 |
--]] |
| 21 |
|
| 22 |
local ffi = require 'ffi' |
| 23 |
|
| 24 |
ffi.cdef [[ |
| 25 |
struct rspamd_dkim_sign_context_s; |
| 26 |
struct rspamd_dkim_key_s; |
| 27 |
struct rspamd_task; |
| 28 |
enum rspamd_dkim_key_format { |
| 29 |
RSPAMD_DKIM_KEY_FILE = 0, |
| 30 |
RSPAMD_DKIM_KEY_PEM, |
| 31 |
RSPAMD_DKIM_KEY_BASE64, |
| 32 |
RSPAMD_DKIM_KEY_RAW, |
| 33 |
}; |
| 34 |
enum rspamd_dkim_type { |
| 35 |
RSPAMD_DKIM_NORMAL, |
| 36 |
RSPAMD_DKIM_ARC_SIG, |
| 37 |
RSPAMD_DKIM_ARC_SEAL |
| 38 |
}; |
| 39 |
struct rspamd_dkim_sign_context_s* |
| 40 |
rspamd_create_dkim_sign_context (struct rspamd_task *task, |
| 41 |
struct rspamd_dkim_key_s *priv_key, |
| 42 |
int headers_canon, |
| 43 |
int body_canon, |
| 44 |
const char *dkim_headers, |
| 45 |
enum rspamd_dkim_type type, |
| 46 |
void *unused); |
| 47 |
struct rspamd_dkim_key_s* rspamd_dkim_sign_key_load (const char *what, size_t len, |
| 48 |
enum rspamd_dkim_key_format, |
| 49 |
void *err); |
| 50 |
void rspamd_dkim_key_unref (struct rspamd_dkim_key_s *k); |
| 51 |
struct GString *rspamd_dkim_sign (struct rspamd_task *task, |
| 52 |
const char *selector, |
| 53 |
const char *domain, |
| 54 |
unsigned long expire, |
| 55 |
size_t len, |
| 56 |
unsigned int idx, |
| 57 |
const char *arc_cv, |
| 58 |
struct rspamd_dkim_sign_context_s *ctx); |
| 59 |
]] |
| 60 |
|
| 61 |
local function load_sign_key(what, format) |
| 62 |
if not format then |
| 63 |
format = ffi.C.RSPAMD_DKIM_KEY_PEM |
| 64 |
else |
| 65 |
if format == 'file' then |
| 66 |
format = ffi.C.RSPAMD_DKIM_KEY_FILE |
| 67 |
elseif format == 'base64' then |
| 68 |
format = ffi.C.RSPAMD_DKIM_KEY_BASE64 |
| 69 |
elseif format == 'raw' then |
| 70 |
format = ffi.C.RSPAMD_DKIM_KEY_RAW |
| 71 |
else |
| 72 |
return nil, 'unknown key format' |
| 73 |
end |
| 74 |
end |
| 75 |
|
| 76 |
return ffi.C.rspamd_dkim_sign_key_load(what, #what, format, nil) |
| 77 |
end |
| 78 |
|
| 79 |
local default_dkim_headers = "(o)from:(o)sender:(o)reply-to:(o)subject:(o)date:(o)message-id:" .. |
| 80 |
"(o)to:(o)cc:(o)mime-version:(o)content-type:(o)content-transfer-encoding:" .. |
| 81 |
"resent-to:resent-cc:resent-from:resent-sender:resent-message-id:" .. |
| 82 |
"(o)in-reply-to:(o)references:list-id:list-owner:list-unsubscribe:" .. |
| 83 |
"list-subscribe:list-post:(o)openpgp:(o)autocrypt" |
| 84 |
|
| 85 |
local function create_sign_context(task, privkey, dkim_headers, sign_type) |
| 86 |
if not task or not privkey then |
| 87 |
return nil, 'invalid arguments' |
| 88 |
end |
| 89 |
|
| 90 |
if not dkim_headers then |
| 91 |
dkim_headers = default_dkim_headers |
| 92 |
end |
| 93 |
|
| 94 |
if not sign_type then |
| 95 |
sign_type = 'dkim' |
| 96 |
end |
| 97 |
|
| 98 |
if sign_type == 'dkim' then |
| 99 |
sign_type = ffi.C.RSPAMD_DKIM_NORMAL |
| 100 |
elseif sign_type == 'arc-sig' then |
| 101 |
sign_type = ffi.C.RSPAMD_DKIM_ARC_SIG |
| 102 |
elseif sign_type == 'arc-seal' then |
| 103 |
sign_type = ffi.C.RSPAMD_DKIM_ARC_SEAL |
| 104 |
else |
| 105 |
return nil, 'invalid sign type' |
| 106 |
end |
| 107 |
|
| 108 |
return ffi.C.rspamd_create_dkim_sign_context(task:topointer(), privkey, |
| 109 |
1, 1, dkim_headers, sign_type, nil) |
| 110 |
end |
| 111 |
|
| 112 |
local function do_sign(task, sign_context, selector, domain, |
| 113 |
expire, len, arc_idx) |
| 114 |
if not task or not sign_context or not selector or not domain then |
| 115 |
return nil, 'invalid arguments' |
| 116 |
end |
| 117 |
|
| 118 |
if not expire then |
| 119 |
expire = 0 |
| 120 |
end |
| 121 |
if not len then |
| 122 |
len = 0 |
| 123 |
end |
| 124 |
if not arc_idx then |
| 125 |
arc_idx = 0 |
| 126 |
end |
| 127 |
|
| 128 |
local gstring = ffi.C.rspamd_dkim_sign(task:topointer(), selector, domain, expire, len, arc_idx, nil, sign_context) |
| 129 |
|
| 130 |
if not gstring then |
| 131 |
return nil, 'cannot sign' |
| 132 |
end |
| 133 |
|
| 134 |
local ret = ffi.string(gstring.str, gstring.len) |
| 135 |
ffi.C.g_string_free(gstring, true) |
| 136 |
|
| 137 |
return ret |
| 138 |
end |
| 139 |
|
| 140 |
return { |
| 141 |
load_sign_key = load_sign_key, |
| 142 |
create_sign_context = create_sign_context, |
| 143 |
do_sign = do_sign |
| 144 |
} |