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.h 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #ifndef DKIM_H_
  17. #define DKIM_H_
  18. #include "config.h"
  19. #include "event.h"
  20. #include "dns.h"
  21. #include "ref.h"
  22. /* Main types and definitions */
  23. #define RSPAMD_DKIM_SIGNHEADER "DKIM-Signature"
  24. #define RSPAMD_DKIM_ARC_SIGNHEADER "ARC-Message-Signature"
  25. #define RSPAMD_DKIM_ARC_AUTHHEADER "ARC-Authentication-Results"
  26. #define RSPAMD_DKIM_ARC_SEALHEADER "ARC-Seal"
  27. /* DKIM signature header */
  28. /* Errors (from OpenDKIM) */
  29. #define DKIM_SIGERROR_UNKNOWN (-1) /* unknown error */
  30. #define DKIM_SIGERROR_OK 0 /* no error */
  31. #define DKIM_SIGERROR_VERSION 1 /* unsupported version */
  32. #define DKIM_SIGERROR_DOMAIN 2 /* invalid domain (d=/i=) */
  33. #define DKIM_SIGERROR_EXPIRED 3 /* signature expired */
  34. #define DKIM_SIGERROR_FUTURE 4 /* signature in the future */
  35. #define DKIM_SIGERROR_TIMESTAMPS 5 /* x= < t= */
  36. #define DKIM_SIGERROR_NOREC 6 /* No record */
  37. #define DKIM_SIGERROR_INVALID_HC 7 /* c= invalid (header) */
  38. #define DKIM_SIGERROR_INVALID_BC 8 /* c= invalid (body) */
  39. #define DKIM_SIGERROR_MISSING_A 9 /* a= missing */
  40. #define DKIM_SIGERROR_INVALID_A 10 /* a= invalid */
  41. #define DKIM_SIGERROR_MISSING_H 11 /* h= missing */
  42. #define DKIM_SIGERROR_INVALID_L 12 /* l= invalid */
  43. #define DKIM_SIGERROR_INVALID_Q 13 /* q= invalid */
  44. #define DKIM_SIGERROR_INVALID_QO 14 /* q= option invalid */
  45. #define DKIM_SIGERROR_MISSING_D 15 /* d= missing */
  46. #define DKIM_SIGERROR_EMPTY_D 16 /* d= empty */
  47. #define DKIM_SIGERROR_MISSING_S 17 /* s= missing */
  48. #define DKIM_SIGERROR_EMPTY_S 18 /* s= empty */
  49. #define DKIM_SIGERROR_MISSING_B 19 /* b= missing */
  50. #define DKIM_SIGERROR_EMPTY_B 20 /* b= empty */
  51. #define DKIM_SIGERROR_CORRUPT_B 21 /* b= corrupt */
  52. #define DKIM_SIGERROR_NOKEY 22 /* no key found in DNS */
  53. #define DKIM_SIGERROR_DNSSYNTAX 23 /* DNS reply corrupt */
  54. #define DKIM_SIGERROR_KEYFAIL 24 /* DNS query failed */
  55. #define DKIM_SIGERROR_MISSING_BH 25 /* bh= missing */
  56. #define DKIM_SIGERROR_EMPTY_BH 26 /* bh= empty */
  57. #define DKIM_SIGERROR_CORRUPT_BH 27 /* bh= corrupt */
  58. #define DKIM_SIGERROR_BADSIG 28 /* signature mismatch */
  59. #define DKIM_SIGERROR_SUBDOMAIN 29 /* unauthorized subdomain */
  60. #define DKIM_SIGERROR_MULTIREPLY 30 /* multiple records returned */
  61. #define DKIM_SIGERROR_EMPTY_H 31 /* h= empty */
  62. #define DKIM_SIGERROR_INVALID_H 32 /* h= missing req'd entries */
  63. #define DKIM_SIGERROR_TOOLARGE_L 33 /* l= value exceeds body size */
  64. #define DKIM_SIGERROR_MBSFAILED 34 /* "must be signed" failure */
  65. #define DKIM_SIGERROR_KEYVERSION 35 /* unknown key version */
  66. #define DKIM_SIGERROR_KEYUNKNOWNHASH 36 /* unknown key hash */
  67. #define DKIM_SIGERROR_KEYHASHMISMATCH 37 /* sig-key hash mismatch */
  68. #define DKIM_SIGERROR_NOTEMAILKEY 38 /* not an e-mail key */
  69. #define DKIM_SIGERROR_UNUSED2 39 /* OBSOLETE */
  70. #define DKIM_SIGERROR_KEYTYPEMISSING 40 /* key type missing */
  71. #define DKIM_SIGERROR_KEYTYPEUNKNOWN 41 /* key type unknown */
  72. #define DKIM_SIGERROR_KEYREVOKED 42 /* key revoked */
  73. #define DKIM_SIGERROR_KEYDECODE 43 /* key couldn't be decoded */
  74. #define DKIM_SIGERROR_MISSING_V 44 /* v= tag missing */
  75. #define DKIM_SIGERROR_EMPTY_V 45 /* v= tag empty */
  76. /* Check results */
  77. enum rspamd_dkim_check_rcode {
  78. DKIM_CONTINUE = 0,
  79. DKIM_REJECT,
  80. DKIM_TRYAGAIN,
  81. DKIM_NOTFOUND,
  82. DKIM_RECORD_ERROR,
  83. DKIM_PERM_ERROR,
  84. };
  85. #define DKIM_CANON_SIMPLE 0 /* as specified in DKIM spec */
  86. #define DKIM_CANON_RELAXED 1 /* as specified in DKIM spec */
  87. struct rspamd_dkim_context_s;
  88. typedef struct rspamd_dkim_context_s rspamd_dkim_context_t;
  89. struct rspamd_dkim_sign_context_s;
  90. typedef struct rspamd_dkim_sign_context_s rspamd_dkim_sign_context_t;
  91. struct rspamd_dkim_key_s;
  92. typedef struct rspamd_dkim_key_s rspamd_dkim_key_t;
  93. typedef struct rspamd_dkim_key_s rspamd_dkim_sign_key_t;
  94. struct rspamd_task;
  95. enum rspamd_dkim_key_format {
  96. RSPAMD_DKIM_KEY_FILE = 0,
  97. RSPAMD_DKIM_KEY_PEM,
  98. RSPAMD_DKIM_KEY_BASE64,
  99. RSPAMD_DKIM_KEY_RAW,
  100. RSPAMD_DKIM_KEY_UNKNOWN
  101. };
  102. enum rspamd_dkim_type {
  103. RSPAMD_DKIM_NORMAL,
  104. RSPAMD_DKIM_ARC_SIG,
  105. RSPAMD_DKIM_ARC_SEAL
  106. };
  107. /* Signature methods */
  108. enum rspamd_sign_type {
  109. DKIM_SIGN_UNKNOWN = -2,
  110. DKIM_SIGN_RSASHA1 = 0,
  111. DKIM_SIGN_RSASHA256,
  112. DKIM_SIGN_RSASHA512,
  113. DKIM_SIGN_ECDSASHA256,
  114. DKIM_SIGN_ECDSASHA512,
  115. DKIM_SIGN_EDDSASHA256,
  116. };
  117. enum rspamd_dkim_key_type {
  118. RSPAMD_DKIM_KEY_RSA = 0,
  119. RSPAMD_DKIM_KEY_ECDSA,
  120. RSPAMD_DKIM_KEY_EDDSA
  121. };
  122. struct rspamd_dkim_check_result {
  123. enum rspamd_dkim_check_rcode rcode;
  124. rspamd_dkim_context_t *ctx;
  125. /* Processed parts */
  126. const gchar *selector;
  127. const gchar *domain;
  128. const gchar *short_b;
  129. const gchar *fail_reason;
  130. };
  131. /* Err MUST be freed if it is not NULL, key is allocated by slice allocator */
  132. typedef void (*dkim_key_handler_f)(rspamd_dkim_key_t *key, gsize keylen,
  133. rspamd_dkim_context_t *ctx, gpointer ud, GError *err);
  134. /**
  135. * Create new dkim context from signature
  136. * @param sig message's signature
  137. * @param pool pool to allocate memory from
  138. * @param time_jitter jitter in seconds to allow time diff while checking
  139. * @param err pointer to error object
  140. * @return new context or NULL
  141. */
  142. rspamd_dkim_context_t * rspamd_create_dkim_context (const gchar *sig,
  143. rspamd_mempool_t *pool,
  144. guint time_jitter,
  145. enum rspamd_dkim_type type,
  146. GError **err);
  147. /**
  148. * Create new dkim context for making a signature
  149. * @param task
  150. * @param priv_key
  151. * @param err
  152. * @return
  153. */
  154. rspamd_dkim_sign_context_t * rspamd_create_dkim_sign_context (struct rspamd_task *task,
  155. rspamd_dkim_sign_key_t *priv_key,
  156. gint headers_canon,
  157. gint body_canon,
  158. const gchar *dkim_headers,
  159. enum rspamd_dkim_type type,
  160. GError **err);
  161. /**
  162. * Load dkim key
  163. * @param path
  164. * @param err
  165. * @return
  166. */
  167. rspamd_dkim_sign_key_t* rspamd_dkim_sign_key_load (const gchar *what, gsize len,
  168. enum rspamd_dkim_key_format type,
  169. GError **err);
  170. /**
  171. * Invalidate modified sign key
  172. * @param key
  173. * @return
  174. */
  175. gboolean rspamd_dkim_sign_key_maybe_invalidate (rspamd_dkim_sign_key_t *key,
  176. time_t mtime);
  177. /**
  178. * Make DNS request for specified context and obtain and parse key
  179. * @param ctx dkim context from signature
  180. * @param resolver dns resolver object
  181. * @param s async session to make request
  182. * @return
  183. */
  184. gboolean rspamd_get_dkim_key (rspamd_dkim_context_t *ctx,
  185. struct rspamd_task *task,
  186. dkim_key_handler_f handler,
  187. gpointer ud);
  188. /**
  189. * Check task for dkim context using dkim key
  190. * @param ctx dkim verify context
  191. * @param key dkim key (from cache or from dns request)
  192. * @param task task to check
  193. * @return
  194. */
  195. struct rspamd_dkim_check_result * rspamd_dkim_check (rspamd_dkim_context_t *ctx,
  196. rspamd_dkim_key_t *key,
  197. struct rspamd_task *task);
  198. struct rspamd_dkim_check_result *
  199. rspamd_dkim_create_result (rspamd_dkim_context_t *ctx,
  200. enum rspamd_dkim_check_rcode rcode,
  201. struct rspamd_task *task);
  202. GString *rspamd_dkim_sign (struct rspamd_task *task,
  203. const gchar *selector,
  204. const gchar *domain,
  205. time_t expire,
  206. gsize len,
  207. guint idx,
  208. const gchar *arc_cv,
  209. rspamd_dkim_sign_context_t *ctx);
  210. rspamd_dkim_key_t * rspamd_dkim_key_ref (rspamd_dkim_key_t *k);
  211. void rspamd_dkim_key_unref (rspamd_dkim_key_t *k);
  212. rspamd_dkim_sign_key_t * rspamd_dkim_sign_key_ref (rspamd_dkim_sign_key_t *k);
  213. void rspamd_dkim_sign_key_unref (rspamd_dkim_sign_key_t *k);
  214. const gchar* rspamd_dkim_get_domain (rspamd_dkim_context_t *ctx);
  215. const gchar* rspamd_dkim_get_selector (rspamd_dkim_context_t *ctx);
  216. const gchar* rspamd_dkim_get_dns_key (rspamd_dkim_context_t *ctx);
  217. guint rspamd_dkim_key_get_ttl (rspamd_dkim_key_t *k);
  218. /**
  219. * Create DKIM public key from a raw data
  220. * @param keydata
  221. * @param keylen
  222. * @param type
  223. * @param err
  224. * @return
  225. */
  226. rspamd_dkim_key_t * rspamd_dkim_make_key (const gchar *keydata, guint keylen,
  227. enum rspamd_dkim_key_type type,
  228. GError **err);
  229. /**
  230. * Parse DKIM public key from a TXT record
  231. * @param txt
  232. * @param keylen
  233. * @param err
  234. * @return
  235. */
  236. rspamd_dkim_key_t * rspamd_dkim_parse_key (const gchar *txt, gsize *keylen,
  237. GError **err);
  238. /**
  239. * Canonocalise header using relaxed algorithm
  240. * @param hname
  241. * @param hvalue
  242. * @param out
  243. * @param outlen
  244. * @return
  245. */
  246. goffset rspamd_dkim_canonize_header_relaxed_str (const gchar *hname,
  247. const gchar *hvalue,
  248. gchar *out,
  249. gsize outlen);
  250. /**
  251. * Checks public and private keys for match
  252. * @param pk
  253. * @param sk
  254. * @param err
  255. * @return
  256. */
  257. gboolean rspamd_dkim_match_keys (rspamd_dkim_key_t *pk,
  258. rspamd_dkim_sign_key_t *sk,
  259. GError **err);
  260. /**
  261. * Free DKIM key
  262. * @param key
  263. */
  264. void rspamd_dkim_key_free (rspamd_dkim_key_t *key);
  265. #endif /* DKIM_H_ */