You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dkim_keygen.c 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  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. #include "config.h"
  17. #include "rspamadm.h"
  18. #include "printf.h"
  19. #include "str_util.h"
  20. #include "libcryptobox/cryptobox.h"
  21. #include "contrib/libottery/ottery.h"
  22. #include "lua/lua_common.h"
  23. #include <openssl/rsa.h>
  24. #include <openssl/bn.h>
  25. #include <openssl/pem.h>
  26. static gchar *privkey_file = NULL;
  27. static gchar *selector = NULL;
  28. static gchar *domain = NULL;
  29. static guint bits = 1024;
  30. static gchar *type = "rsa";
  31. static void rspamadm_dkim_keygen (gint argc, gchar **argv,
  32. const struct rspamadm_command *cmd);
  33. static const char *rspamadm_dkim_keygen_help (gboolean full_help,
  34. const struct rspamadm_command *cmd);
  35. static void rspamadm_dkim_keygen_lua_subrs (gpointer pL);
  36. struct rspamadm_command dkim_keygen_command = {
  37. .name = "dkim_keygen",
  38. .flags = 0,
  39. .help = rspamadm_dkim_keygen_help,
  40. .run = rspamadm_dkim_keygen,
  41. .lua_subrs = rspamadm_dkim_keygen_lua_subrs,
  42. };
  43. static GOptionEntry entries[] = {
  44. {"domain", 'd', 0, G_OPTION_ARG_STRING, &domain,
  45. "Use the specified domain", NULL},
  46. {"selector", 's', 0, G_OPTION_ARG_STRING, &selector,
  47. "Use the specified selector", NULL},
  48. {"privkey", 'k', 0, G_OPTION_ARG_STRING, &privkey_file,
  49. "Save private key in the specified file", NULL},
  50. {"bits", 'b', 0, G_OPTION_ARG_INT, &bits,
  51. "Set key length to N bits (1024 by default)", NULL},
  52. {"type", 't', 0, G_OPTION_ARG_STRING, &type,
  53. "Key type: rsa or ed25519 (rsa by default)", NULL},
  54. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
  55. };
  56. static const char *
  57. rspamadm_dkim_keygen_help (gboolean full_help, const struct rspamadm_command *cmd)
  58. {
  59. const char *help_str;
  60. if (full_help) {
  61. help_str = "Create key pairs for dkim signing\n\n"
  62. "Usage: rspamadm dkim_keygen -s selector -d domain [-k privkey] [-b bits]\n"
  63. "Where options are:\n\n"
  64. "-d: use the specified domain\n"
  65. "-s: use the specified selector\n"
  66. "-k: save private key to file instead of printing it to stdout\n"
  67. "-b: set number of bits instead of 1024\n"
  68. "--help: shows available options and commands";
  69. }
  70. else {
  71. help_str = "Create dkim key pairs";
  72. }
  73. return help_str;
  74. }
  75. static void
  76. rspamd_dkim_generate_rsa_keypair (const gchar *domain, const gchar *selector,
  77. const gchar *priv_fname, const gchar *pub_fname,
  78. guint keylen)
  79. {
  80. BIGNUM *e;
  81. RSA *r;
  82. BIO *pubout, *privout;
  83. EVP_PKEY *pk;
  84. gint rc;
  85. glong publen;
  86. gsize b64_len;
  87. gchar *pubdata, *b64_data;
  88. FILE *pubfile = NULL;
  89. if (bits > 4096 || bits < 512) {
  90. fprintf (stderr, "Bits number must be in the interval 512...4096\n");
  91. exit (EXIT_FAILURE);
  92. }
  93. e = BN_new ();
  94. r = RSA_new ();
  95. pk = EVP_PKEY_new ();
  96. g_assert (BN_set_word (e, RSA_F4) == 1);
  97. g_assert (RSA_generate_key_ex (r, bits, e, NULL) == 1);
  98. g_assert (EVP_PKEY_set1_RSA (pk, r) == 1);
  99. if (priv_fname) {
  100. privout = BIO_new_file (priv_fname, "w");
  101. if (privout == NULL) {
  102. rspamd_fprintf (stderr, "cannot open output file %s: %s\n",
  103. priv_fname, strerror (errno));
  104. exit (EXIT_FAILURE);
  105. }
  106. } else {
  107. privout = BIO_new_fp (stdout, 0);
  108. }
  109. rc = PEM_write_bio_PrivateKey (privout, pk, NULL, NULL, 0, NULL, NULL);
  110. if (rc != 1) {
  111. rspamd_fprintf (stderr, "cannot write key to the output file %s: %s\n",
  112. priv_fname ? priv_fname : "stdout", strerror (errno));
  113. exit (EXIT_FAILURE);
  114. }
  115. BIO_free (privout);
  116. fflush (stdout);
  117. pubout = BIO_new (BIO_s_mem ());
  118. rc = i2d_RSA_PUBKEY_bio (pubout, r);
  119. publen = BIO_get_mem_data (pubout, &pubdata);
  120. g_assert (publen > 0);
  121. b64_data = rspamd_encode_base64 (pubdata, publen, -1, &b64_len);
  122. if (pub_fname) {
  123. pubfile = fopen (pub_fname, "w");
  124. if (pubfile == NULL) {
  125. rspamd_fprintf (stderr, "cannot open output file %s: %s\n",
  126. pub_fname, strerror (errno));
  127. exit (EXIT_FAILURE);
  128. }
  129. } else {
  130. pubfile = stdout;
  131. }
  132. if (b64_len < 255 - 2) {
  133. rspamd_fprintf (pubfile, "%s._domainkey IN TXT ( \"v=DKIM1; k=rsa; \"\n"
  134. "\t\"p=%s\" ) ;\n",
  135. selector ? selector : "selector",
  136. b64_data);
  137. } else {
  138. guint i;
  139. gint step = 253, remain = b64_len;
  140. rspamd_fprintf (pubfile, "%s._domainkey IN TXT ( \"v=DKIM1; k=rsa; \"\n",
  141. selector ? selector : "selector");
  142. for (i = 0; i < b64_len; i += step, remain -= step) {
  143. if (i == 0) {
  144. rspamd_fprintf (pubfile, "\t\"p=%*s\"\n", MIN(step, remain), &b64_data[i]);
  145. } else {
  146. step = 255;
  147. rspamd_fprintf (pubfile, "\t\"%*s\"\n", MIN(step, remain), &b64_data[i]);
  148. }
  149. }
  150. rspamd_fprintf (pubfile, ") ; \n");
  151. }
  152. if (pubfile != stdout) {
  153. fclose (pubfile);
  154. }
  155. g_free (b64_data);
  156. BIO_free (pubout);
  157. EVP_PKEY_free (pk);
  158. RSA_free (r);
  159. BN_free (e);
  160. }
  161. static void
  162. rspamd_dkim_generate_ed25519_keypair (const gchar *domain, const gchar *selector,
  163. const gchar *priv_fname, const gchar *pub_fname,
  164. guint keylen, gboolean seeded)
  165. {
  166. rspamd_sig_sk_t ed_sk;
  167. rspamd_sig_pk_t ed_pk;
  168. gchar *base64_pk, *base64_sk;
  169. FILE *pubfile = NULL, *privfile = NULL;
  170. rspamd_cryptobox_keypair_sig (ed_pk, ed_sk, RSPAMD_CRYPTOBOX_MODE_25519);
  171. if (seeded) {
  172. /* Just encode seed, not the full sk */
  173. base64_sk = rspamd_encode_base64_common (ed_sk, 32, 0, NULL, FALSE,
  174. RSPAMD_TASK_NEWLINES_LF);
  175. }
  176. else {
  177. base64_sk = rspamd_encode_base64_common (ed_sk,
  178. rspamd_cryptobox_sk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519),
  179. 0, NULL, FALSE,
  180. RSPAMD_TASK_NEWLINES_LF);
  181. }
  182. base64_pk = rspamd_encode_base64_common (ed_pk, sizeof (ed_pk), 0, NULL, FALSE,
  183. RSPAMD_TASK_NEWLINES_LF);
  184. /* Cleanup sensitive data */
  185. rspamd_explicit_memzero (ed_sk, sizeof (ed_sk));
  186. if (priv_fname) {
  187. privfile = fopen (priv_fname, "w");
  188. if (privfile == NULL) {
  189. rspamd_fprintf (stderr, "cannot open output file %s: %s\n",
  190. priv_fname, strerror (errno));
  191. rspamd_explicit_memzero (base64_sk, strlen (base64_sk));
  192. g_free (base64_sk);
  193. g_free (base64_pk);
  194. exit (EXIT_FAILURE);
  195. }
  196. }
  197. else {
  198. privfile = stdout;
  199. }
  200. if (rspamd_fprintf (privfile, "%s\n", base64_sk) == -1) {
  201. rspamd_fprintf (stderr, "cannot write to output file %s: %s\n",
  202. priv_fname, strerror (errno));
  203. rspamd_explicit_memzero (base64_sk, strlen (base64_sk));
  204. g_free (base64_sk);
  205. g_free (base64_pk);
  206. if (privfile != stdout) {
  207. fclose (privfile);
  208. }
  209. exit (EXIT_FAILURE);
  210. }
  211. if (privfile != stdout) {
  212. fclose (privfile);
  213. }
  214. if (pub_fname) {
  215. pubfile = fopen (pub_fname, "w");
  216. if (pubfile == NULL) {
  217. rspamd_fprintf (stderr, "cannot open output file %s: %s\n",
  218. pub_fname, strerror (errno));
  219. rspamd_explicit_memzero (base64_sk, strlen (base64_sk));
  220. g_free (base64_sk);
  221. g_free (base64_pk);
  222. exit (EXIT_FAILURE);
  223. }
  224. }
  225. else {
  226. pubfile = stdout;
  227. }
  228. rspamd_fprintf (pubfile, "%s._domainkey IN TXT ( \"v=DKIM1; k=ed25519; \"\n"
  229. "\t\"p=%s\" ) ;\n",
  230. selector ? selector : "selector",
  231. base64_pk);
  232. if (pubfile != stdout) {
  233. fclose (pubfile);
  234. }
  235. rspamd_explicit_memzero (base64_sk, strlen (base64_sk));
  236. g_free (base64_sk);
  237. g_free (base64_pk);
  238. }
  239. static void
  240. rspamadm_dkim_generate_keypair (const gchar *domain, const gchar *selector,
  241. const gchar *priv_fname, const gchar *pub_fname, guint keylen)
  242. {
  243. if (strcmp (type, "rsa") == 0) {
  244. rspamd_dkim_generate_rsa_keypair (domain, selector, priv_fname,
  245. pub_fname, keylen);
  246. }
  247. else if (strcmp (type, "ed25519") == 0) {
  248. rspamd_dkim_generate_ed25519_keypair (domain, selector, priv_fname,
  249. pub_fname, keylen, FALSE);
  250. }
  251. else if (strcmp (type, "ed25519-seed") == 0) {
  252. rspamd_dkim_generate_ed25519_keypair (domain, selector, priv_fname,
  253. pub_fname, keylen, TRUE);
  254. }
  255. else {
  256. fprintf (stderr, "invalid key type: %s\n", type);
  257. exit (EXIT_FAILURE);
  258. }
  259. }
  260. static gint
  261. rspamadm_dkim_keygen_lua_generate (lua_State *L)
  262. {
  263. const gchar *domain = luaL_checkstring (L, 1);
  264. const gchar *selector = luaL_checkstring (L, 2);
  265. const gchar *privfile = NULL, *pubfile = NULL;
  266. guint key_bits = 1024;
  267. if (domain == NULL || selector == NULL) {
  268. return luaL_error (L, "invalid arguments");
  269. }
  270. if (lua_type (L, 3) == LUA_TSTRING) {
  271. privfile = lua_tostring (L, 3);
  272. }
  273. if (lua_type (L, 4) == LUA_TSTRING) {
  274. pubfile = lua_tostring (L, 4);
  275. }
  276. if (lua_type (L, 5) == LUA_TNUMBER) {
  277. key_bits = lua_tonumber (L, 5);
  278. }
  279. rspamadm_dkim_generate_keypair (domain, selector, privfile, pubfile, key_bits);
  280. return 0;
  281. }
  282. static void
  283. rspamadm_dkim_keygen_lua_subrs (gpointer pL)
  284. {
  285. lua_State *L = pL;
  286. lua_pushstring (L, "dkim_keygen");
  287. lua_pushcfunction (L, rspamadm_dkim_keygen_lua_generate);
  288. lua_settable (L, -3);
  289. }
  290. static void
  291. rspamadm_dkim_keygen (gint argc, gchar **argv, const struct rspamadm_command *cmd)
  292. {
  293. GOptionContext *context;
  294. GError *error = NULL;
  295. context = g_option_context_new (
  296. "dkim_keygen - create dkim keys");
  297. g_option_context_set_summary (context,
  298. "Summary:\n Rspamd administration utility version "
  299. RVERSION
  300. "\n Release id: "
  301. RID);
  302. g_option_context_add_main_entries (context, entries, NULL);
  303. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  304. fprintf (stderr, "option parsing failed: %s\n", error->message);
  305. g_error_free (error);
  306. g_option_context_free (context);
  307. exit (1);
  308. }
  309. g_option_context_free (context);
  310. rspamadm_dkim_generate_keypair (domain, selector, privkey_file, NULL, bits);
  311. }