| 1 |
--[[ |
| 2 |
Copyright (c) 2023, 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 |
local argparse = require "argparse" |
| 18 |
local rspamd_util = require "rspamd_util" |
| 19 |
local rspamd_cryptobox = require "rspamd_cryptobox" |
| 20 |
|
| 21 |
local parser = argparse() |
| 22 |
:name 'rspamadm dkim_keygen' |
| 23 |
:description 'Create key pairs for dkim signing' |
| 24 |
:help_description_margin(30) |
| 25 |
parser:option '-d --domain' |
| 26 |
:description 'Create a key for a specific domain' |
| 27 |
:default "example.com" |
| 28 |
parser:option '-s --selector' |
| 29 |
:description 'Create a key for a specific DKIM selector' |
| 30 |
:default "mail" |
| 31 |
parser:option '-k --privkey' |
| 32 |
:description 'Save private key to file instead of printing it to stdout' |
| 33 |
parser:option '-b --bits' |
| 34 |
:convert(function(input) |
| 35 |
local n = tonumber(input) |
| 36 |
if not n or n < 512 or n > 4096 then |
| 37 |
return nil |
| 38 |
end |
| 39 |
return n |
| 40 |
end) |
| 41 |
:description 'Generate an RSA key with the specified number of bits (512 to 4096)' |
| 42 |
:default(1024) |
| 43 |
parser:option '-t --type' |
| 44 |
:description 'Key type: RSA, ED25519 or ED25119-seed' |
| 45 |
:convert { |
| 46 |
['rsa'] = 'rsa', |
| 47 |
['RSA'] = 'rsa', |
| 48 |
['ed25519'] = 'ed25519', |
| 49 |
['ED25519'] = 'ed25519', |
| 50 |
['ed25519-seed'] = 'ed25519-seed', |
| 51 |
['ED25519-seed'] = 'ed25519-seed', |
| 52 |
} |
| 53 |
:default 'rsa' |
| 54 |
parser:option '-o --output' |
| 55 |
:description 'Output public key in the following format: dns, dnskey or plain' |
| 56 |
:convert { |
| 57 |
['dns'] = 'dns', |
| 58 |
['plain'] = 'plain', |
| 59 |
['dnskey'] = 'dnskey', |
| 60 |
} |
| 61 |
:default 'dns' |
| 62 |
parser:option '--priv-output' |
| 63 |
:description 'Output private key in the following format: PEM or DER (for RSA)' |
| 64 |
:convert { |
| 65 |
['pem'] = 'pem', |
| 66 |
['der'] = 'der', |
| 67 |
} |
| 68 |
:default 'pem' |
| 69 |
parser:flag '-f --force' |
| 70 |
:description 'Force overwrite of existing files' |
| 71 |
|
| 72 |
local function split_string(input, max_length) |
| 73 |
max_length = max_length or 253 |
| 74 |
local pieces = {} |
| 75 |
local index = 1 |
| 76 |
|
| 77 |
while index <= #input do |
| 78 |
local piece = input:sub(index, index + max_length - 1) |
| 79 |
table.insert(pieces, piece) |
| 80 |
index = index + max_length |
| 81 |
end |
| 82 |
|
| 83 |
return pieces |
| 84 |
end |
| 85 |
|
| 86 |
local function print_public_key_dns(opts, base64_pk) |
| 87 |
local key_type = opts.type == 'rsa' and 'rsa' or 'ed25519' |
| 88 |
if #base64_pk < 255 - 2 then |
| 89 |
io.write(string.format('%s._domainkey IN TXT ( "v=DKIM1; k=%s;" \n\t"p=%s" ) ;\n', |
| 90 |
opts.selector, key_type, base64_pk)) |
| 91 |
else |
| 92 |
-- Split it by parts |
| 93 |
local parts = split_string(base64_pk) |
| 94 |
io.write(string.format('%s._domainkey IN TXT ( "v=DKIM1; k=%s; "\n', opts.selector, key_type)) |
| 95 |
for i, part in ipairs(parts) do |
| 96 |
if i == 1 then |
| 97 |
io.write(string.format('\t"p=%s"\n', part)) |
| 98 |
else |
| 99 |
io.write(string.format('\t"%s"\n', part)) |
| 100 |
end |
| 101 |
end |
| 102 |
io.write(") ; \n") |
| 103 |
end |
| 104 |
|
| 105 |
end |
| 106 |
|
| 107 |
local function print_public_key(opts, pk, need_base64) |
| 108 |
local key_type = opts.type == 'rsa' and 'rsa' or 'ed25519' |
| 109 |
local base64_pk = need_base64 and tostring(rspamd_util.encode_base64(pk)) or tostring(pk) |
| 110 |
if opts.output == 'plain' then |
| 111 |
io.write(base64_pk) |
| 112 |
io.write("\n") |
| 113 |
elseif opts.output == 'dns' then |
| 114 |
print_public_key_dns(opts, base64_pk, false) |
| 115 |
elseif opts.output == 'dnskey' then |
| 116 |
io.write(string.format('v=DKIM1; k=%s; p=%s\n', key_type, base64_pk)) |
| 117 |
end |
| 118 |
end |
| 119 |
|
| 120 |
local function gen_rsa_key(opts) |
| 121 |
local rsa = require "rspamd_rsa" |
| 122 |
|
| 123 |
local sk, pk = rsa.keypair(opts.bits or 1024) |
| 124 |
if opts.privkey then |
| 125 |
if opts.force then |
| 126 |
os.remove(opts.privkey) |
| 127 |
end |
| 128 |
sk:save(opts.privkey, opts.priv_output) |
| 129 |
else |
| 130 |
sk:save("-", opts.priv_output) |
| 131 |
end |
| 132 |
|
| 133 |
-- We generate key directly via lua_rsa and it returns unencoded raw data |
| 134 |
print_public_key(opts, tostring(pk), true) |
| 135 |
end |
| 136 |
|
| 137 |
local function gen_eddsa_key(opts) |
| 138 |
local sk, pk = rspamd_cryptobox.gen_dkim_keypair(opts.type) |
| 139 |
|
| 140 |
if opts.privkey and opts.force then |
| 141 |
os.remove(opts.privkey) |
| 142 |
end |
| 143 |
if not sk:save_in_file(opts.privkey, tonumber('0600', 8)) then |
| 144 |
io.stderr:write('cannot save private key to ' .. (opts.privkey or 'stdout') .. '\n') |
| 145 |
os.exit(1) |
| 146 |
end |
| 147 |
|
| 148 |
if not opts.privkey then |
| 149 |
io.write("\n") |
| 150 |
io.flush() |
| 151 |
end |
| 152 |
|
| 153 |
-- gen_dkim_keypair function returns everything encoded in base64, so no need to do anything |
| 154 |
print_public_key(opts, tostring(pk), false) |
| 155 |
end |
| 156 |
|
| 157 |
local function handler(args) |
| 158 |
local opts = parser:parse(args) |
| 159 |
|
| 160 |
if not opts then |
| 161 |
os.exit(1) |
| 162 |
end |
| 163 |
|
| 164 |
if opts.type == 'rsa' then |
| 165 |
gen_rsa_key(opts) |
| 166 |
else |
| 167 |
gen_eddsa_key(opts) |
| 168 |
end |
| 169 |
end |
| 170 |
|
| 171 |
return { |
| 172 |
name = 'dkim_keygen', |
| 173 |
aliases = { 'dkimkeygen' }, |
| 174 |
handler = handler, |
| 175 |
description = parser._description |
| 176 |
} |
| 177 |
|
| 178 |
|