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.c 70KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164
  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 "rspamd.h"
  18. #include "message.h"
  19. #include "dkim.h"
  20. #include "dns.h"
  21. #include "utlist.h"
  22. #include "unix-std.h"
  23. #include "mempool_vars_internal.h"
  24. #include "libcryptobox/ed25519/ed25519.h"
  25. #include <openssl/evp.h>
  26. #include <openssl/rsa.h>
  27. #include <openssl/engine.h>
  28. /* special DNS tokens */
  29. #define DKIM_DNSKEYNAME "_domainkey"
  30. /* ed25519 key lengths */
  31. #define ED25519_B64_BYTES 45
  32. #define ED25519_BYTES 32
  33. /* Canonization methods */
  34. #define DKIM_CANON_UNKNOWN (-1) /* unknown method */
  35. #define DKIM_CANON_SIMPLE 0 /* as specified in DKIM spec */
  36. #define DKIM_CANON_RELAXED 1 /* as specified in DKIM spec */
  37. #define DKIM_CANON_DEFAULT DKIM_CANON_SIMPLE
  38. #define RSPAMD_SHORT_BH_LEN 8
  39. /* Params */
  40. enum rspamd_dkim_param_type {
  41. DKIM_PARAM_UNKNOWN = -1,
  42. DKIM_PARAM_SIGNATURE = 0,
  43. DKIM_PARAM_SIGNALG,
  44. DKIM_PARAM_DOMAIN,
  45. DKIM_PARAM_CANONALG,
  46. DKIM_PARAM_QUERYMETHOD,
  47. DKIM_PARAM_SELECTOR,
  48. DKIM_PARAM_HDRLIST,
  49. DKIM_PARAM_VERSION,
  50. DKIM_PARAM_IDENTITY,
  51. DKIM_PARAM_TIMESTAMP,
  52. DKIM_PARAM_EXPIRATION,
  53. DKIM_PARAM_COPIEDHDRS,
  54. DKIM_PARAM_BODYHASH,
  55. DKIM_PARAM_BODYLENGTH,
  56. DKIM_PARAM_IDX,
  57. DKIM_PARAM_CV,
  58. DKIM_PARAM_IGNORE
  59. };
  60. #define RSPAMD_DKIM_MAX_ARC_IDX 10
  61. #define msg_err_dkim(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
  62. "dkim", ctx->pool->tag.uid, \
  63. G_STRFUNC, \
  64. __VA_ARGS__)
  65. #define msg_warn_dkim(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
  66. "dkim", ctx->pool->tag.uid, \
  67. G_STRFUNC, \
  68. __VA_ARGS__)
  69. #define msg_info_dkim(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
  70. "dkim", ctx->pool->tag.uid, \
  71. G_STRFUNC, \
  72. __VA_ARGS__)
  73. #define msg_debug_dkim(...) rspamd_conditional_debug_fast (NULL, NULL, \
  74. rspamd_dkim_log_id, "dkim", ctx->pool->tag.uid, \
  75. G_STRFUNC, \
  76. __VA_ARGS__)
  77. #define msg_debug_dkim_taskless(...) rspamd_conditional_debug_fast (NULL, NULL, \
  78. rspamd_dkim_log_id, "dkim", "", \
  79. G_STRFUNC, \
  80. __VA_ARGS__)
  81. INIT_LOG_MODULE(dkim)
  82. #define RSPAMD_DKIM_FLAG_OVERSIGN (1u << 0)
  83. union rspamd_dkim_header_stat {
  84. struct _st {
  85. guint16 count;
  86. guint16 flags;
  87. } s;
  88. guint32 n;
  89. };
  90. struct rspamd_dkim_common_ctx {
  91. rspamd_mempool_t *pool;
  92. guint64 sig_hash;
  93. gsize len;
  94. gint header_canon_type;
  95. gint body_canon_type;
  96. GPtrArray *hlist;
  97. GHashTable *htable; /* header -> count mapping */
  98. EVP_MD_CTX *headers_hash;
  99. EVP_MD_CTX *body_hash;
  100. enum rspamd_dkim_type type;
  101. guint idx;
  102. };
  103. enum rspamd_arc_seal_cv {
  104. RSPAMD_ARC_UNKNOWN = 0,
  105. RSPAMD_ARC_NONE,
  106. RSPAMD_ARC_INVALID,
  107. RSPAMD_ARC_FAIL,
  108. RSPAMD_ARC_PASS
  109. };
  110. struct rspamd_dkim_context_s {
  111. struct rspamd_dkim_common_ctx common;
  112. rspamd_mempool_t *pool;
  113. gsize blen;
  114. gsize bhlen;
  115. gint sig_alg;
  116. guint ver;
  117. time_t timestamp;
  118. time_t expiration;
  119. gchar *domain;
  120. gchar *selector;
  121. gint8 *b;
  122. gchar *short_b;
  123. gint8 *bh;
  124. gchar *dns_key;
  125. enum rspamd_arc_seal_cv cv;
  126. const gchar *dkim_header;
  127. };
  128. struct rspamd_dkim_key_s {
  129. guint8 *keydata;
  130. gsize keylen;
  131. gsize decoded_len;
  132. guint ttl;
  133. union {
  134. RSA *key_rsa;
  135. EC_KEY *key_ecdsa;
  136. guchar *key_eddsa;
  137. } key;
  138. enum rspamd_dkim_key_type type;
  139. BIO *key_bio;
  140. EVP_PKEY *key_evp;
  141. time_t mtime;
  142. ref_entry_t ref;
  143. };
  144. struct rspamd_dkim_sign_context_s {
  145. struct rspamd_dkim_common_ctx common;
  146. rspamd_dkim_sign_key_t *key;
  147. };
  148. struct rspamd_dkim_header {
  149. const gchar *name;
  150. guint count;
  151. };
  152. /* Parser of dkim params */
  153. typedef gboolean (*dkim_parse_param_f) (rspamd_dkim_context_t * ctx,
  154. const gchar *param, gsize len, GError **err);
  155. static gboolean rspamd_dkim_parse_signature (rspamd_dkim_context_t * ctx,
  156. const gchar *param,
  157. gsize len,
  158. GError **err);
  159. static gboolean rspamd_dkim_parse_signalg (rspamd_dkim_context_t * ctx,
  160. const gchar *param,
  161. gsize len,
  162. GError **err);
  163. static gboolean rspamd_dkim_parse_domain (rspamd_dkim_context_t * ctx,
  164. const gchar *param,
  165. gsize len,
  166. GError **err);
  167. static gboolean rspamd_dkim_parse_canonalg (rspamd_dkim_context_t * ctx,
  168. const gchar *param,
  169. gsize len,
  170. GError **err);
  171. static gboolean rspamd_dkim_parse_ignore (rspamd_dkim_context_t * ctx,
  172. const gchar *param,
  173. gsize len,
  174. GError **err);
  175. static gboolean rspamd_dkim_parse_selector (rspamd_dkim_context_t * ctx,
  176. const gchar *param,
  177. gsize len,
  178. GError **err);
  179. static gboolean rspamd_dkim_parse_hdrlist (rspamd_dkim_context_t * ctx,
  180. const gchar *param,
  181. gsize len,
  182. GError **err);
  183. static gboolean rspamd_dkim_parse_version (rspamd_dkim_context_t * ctx,
  184. const gchar *param,
  185. gsize len,
  186. GError **err);
  187. static gboolean rspamd_dkim_parse_timestamp (rspamd_dkim_context_t * ctx,
  188. const gchar *param,
  189. gsize len,
  190. GError **err);
  191. static gboolean rspamd_dkim_parse_expiration (rspamd_dkim_context_t * ctx,
  192. const gchar *param,
  193. gsize len,
  194. GError **err);
  195. static gboolean rspamd_dkim_parse_bodyhash (rspamd_dkim_context_t * ctx,
  196. const gchar *param,
  197. gsize len,
  198. GError **err);
  199. static gboolean rspamd_dkim_parse_bodylength (rspamd_dkim_context_t * ctx,
  200. const gchar *param,
  201. gsize len,
  202. GError **err);
  203. static gboolean rspamd_dkim_parse_idx (rspamd_dkim_context_t * ctx,
  204. const gchar *param,
  205. gsize len,
  206. GError **err);
  207. static gboolean rspamd_dkim_parse_cv (rspamd_dkim_context_t * ctx,
  208. const gchar *param,
  209. gsize len,
  210. GError **err);
  211. static const dkim_parse_param_f parser_funcs[] = {
  212. [DKIM_PARAM_SIGNATURE] = rspamd_dkim_parse_signature,
  213. [DKIM_PARAM_SIGNALG] = rspamd_dkim_parse_signalg,
  214. [DKIM_PARAM_DOMAIN] = rspamd_dkim_parse_domain,
  215. [DKIM_PARAM_CANONALG] = rspamd_dkim_parse_canonalg,
  216. [DKIM_PARAM_QUERYMETHOD] = rspamd_dkim_parse_ignore,
  217. [DKIM_PARAM_SELECTOR] = rspamd_dkim_parse_selector,
  218. [DKIM_PARAM_HDRLIST] = rspamd_dkim_parse_hdrlist,
  219. [DKIM_PARAM_VERSION] = rspamd_dkim_parse_version,
  220. [DKIM_PARAM_IDENTITY] = rspamd_dkim_parse_ignore,
  221. [DKIM_PARAM_TIMESTAMP] = rspamd_dkim_parse_timestamp,
  222. [DKIM_PARAM_EXPIRATION] = rspamd_dkim_parse_expiration,
  223. [DKIM_PARAM_COPIEDHDRS] = rspamd_dkim_parse_ignore,
  224. [DKIM_PARAM_BODYHASH] = rspamd_dkim_parse_bodyhash,
  225. [DKIM_PARAM_BODYLENGTH] = rspamd_dkim_parse_bodylength,
  226. [DKIM_PARAM_IDX] = rspamd_dkim_parse_idx,
  227. [DKIM_PARAM_CV] = rspamd_dkim_parse_cv,
  228. [DKIM_PARAM_IGNORE] = rspamd_dkim_parse_ignore,
  229. };
  230. #define DKIM_ERROR dkim_error_quark ()
  231. GQuark
  232. dkim_error_quark (void)
  233. {
  234. return g_quark_from_static_string ("dkim-error-quark");
  235. }
  236. /* Parsers implementation */
  237. static gboolean
  238. rspamd_dkim_parse_signature (rspamd_dkim_context_t * ctx,
  239. const gchar *param,
  240. gsize len,
  241. GError **err)
  242. {
  243. ctx->b = rspamd_mempool_alloc0 (ctx->pool, len);
  244. ctx->short_b = rspamd_mempool_alloc0 (ctx->pool, RSPAMD_SHORT_BH_LEN + 1);
  245. rspamd_strlcpy (ctx->short_b, param, MIN (len, RSPAMD_SHORT_BH_LEN + 1));
  246. (void)rspamd_cryptobox_base64_decode (param, len, ctx->b, &ctx->blen);
  247. return TRUE;
  248. }
  249. static gboolean
  250. rspamd_dkim_parse_signalg (rspamd_dkim_context_t * ctx,
  251. const gchar *param,
  252. gsize len,
  253. GError **err)
  254. {
  255. /* XXX: ugly size comparison, improve this code style some day */
  256. if (len == 8) {
  257. if (memcmp (param, "rsa-sha1", len) == 0) {
  258. ctx->sig_alg = DKIM_SIGN_RSASHA1;
  259. return TRUE;
  260. }
  261. }
  262. else if (len == 10) {
  263. if (memcmp (param, "rsa-sha256", len) == 0) {
  264. ctx->sig_alg = DKIM_SIGN_RSASHA256;
  265. return TRUE;
  266. }
  267. else if (memcmp (param, "rsa-sha512", len) == 0) {
  268. ctx->sig_alg = DKIM_SIGN_RSASHA512;
  269. return TRUE;
  270. }
  271. }
  272. else if (len == 15) {
  273. if (memcmp (param, "ecdsa256-sha256", len) == 0) {
  274. ctx->sig_alg = DKIM_SIGN_ECDSASHA256;
  275. return TRUE;
  276. }
  277. else if (memcmp (param, "ecdsa256-sha512", len) == 0) {
  278. ctx->sig_alg = DKIM_SIGN_ECDSASHA512;
  279. return TRUE;
  280. }
  281. }
  282. else if (len == 14) {
  283. if (memcmp (param, "ed25519-sha256", len) == 0) {
  284. ctx->sig_alg = DKIM_SIGN_EDDSASHA256;
  285. return TRUE;
  286. }
  287. }
  288. g_set_error (err,
  289. DKIM_ERROR,
  290. DKIM_SIGERROR_INVALID_A,
  291. "invalid dkim sign algorithm");
  292. return FALSE;
  293. }
  294. static gboolean
  295. rspamd_dkim_parse_domain (rspamd_dkim_context_t * ctx,
  296. const gchar *param,
  297. gsize len,
  298. GError **err)
  299. {
  300. ctx->domain = rspamd_mempool_alloc (ctx->pool, len + 1);
  301. rspamd_strlcpy (ctx->domain, param, len + 1);
  302. return TRUE;
  303. }
  304. static gboolean
  305. rspamd_dkim_parse_canonalg (rspamd_dkim_context_t * ctx,
  306. const gchar *param,
  307. gsize len,
  308. GError **err)
  309. {
  310. const gchar *p, *slash = NULL, *end = param + len;
  311. gsize sl = 0;
  312. p = param;
  313. while (p != end) {
  314. if (*p == '/') {
  315. slash = p;
  316. break;
  317. }
  318. p++;
  319. sl++;
  320. }
  321. if (slash == NULL) {
  322. /* Only check header */
  323. if (len == 6 && memcmp (param, "simple", len) == 0) {
  324. ctx->common.header_canon_type = DKIM_CANON_SIMPLE;
  325. return TRUE;
  326. }
  327. else if (len == 7 && memcmp (param, "relaxed", len) == 0) {
  328. ctx->common.header_canon_type = DKIM_CANON_RELAXED;
  329. return TRUE;
  330. }
  331. }
  332. else {
  333. /* First check header */
  334. if (sl == 6 && memcmp (param, "simple", sl) == 0) {
  335. ctx->common.header_canon_type = DKIM_CANON_SIMPLE;
  336. }
  337. else if (sl == 7 && memcmp (param, "relaxed", sl) == 0) {
  338. ctx->common.header_canon_type = DKIM_CANON_RELAXED;
  339. }
  340. else {
  341. goto err;
  342. }
  343. /* Check body */
  344. len -= sl + 1;
  345. slash++;
  346. if (len == 6 && memcmp (slash, "simple", len) == 0) {
  347. ctx->common.body_canon_type = DKIM_CANON_SIMPLE;
  348. return TRUE;
  349. }
  350. else if (len == 7 && memcmp (slash, "relaxed", len) == 0) {
  351. ctx->common.body_canon_type = DKIM_CANON_RELAXED;
  352. return TRUE;
  353. }
  354. }
  355. err:
  356. g_set_error (err,
  357. DKIM_ERROR,
  358. DKIM_SIGERROR_INVALID_A,
  359. "invalid dkim canonization algorithm");
  360. return FALSE;
  361. }
  362. static gboolean
  363. rspamd_dkim_parse_ignore (rspamd_dkim_context_t * ctx,
  364. const gchar *param,
  365. gsize len,
  366. GError **err)
  367. {
  368. /* Just ignore unused params */
  369. return TRUE;
  370. }
  371. static gboolean
  372. rspamd_dkim_parse_selector (rspamd_dkim_context_t * ctx,
  373. const gchar *param,
  374. gsize len,
  375. GError **err)
  376. {
  377. ctx->selector = rspamd_mempool_alloc (ctx->pool, len + 1);
  378. rspamd_strlcpy (ctx->selector, param, len + 1);
  379. return TRUE;
  380. }
  381. static void
  382. rspamd_dkim_hlist_free (void *ud)
  383. {
  384. GPtrArray *a = ud;
  385. g_ptr_array_free (a, TRUE);
  386. }
  387. static gboolean
  388. rspamd_dkim_parse_hdrlist_common (struct rspamd_dkim_common_ctx *ctx,
  389. const gchar *param,
  390. gsize len,
  391. gboolean sign,
  392. GError **err)
  393. {
  394. const gchar *c, *p, *end = param + len;
  395. gchar *h;
  396. gboolean from_found = FALSE, oversign;
  397. guint count = 0;
  398. struct rspamd_dkim_header *new;
  399. gpointer found;
  400. union rspamd_dkim_header_stat u;
  401. p = param;
  402. while (p <= end) {
  403. if ((p == end || *p == ':')) {
  404. count++;
  405. }
  406. p++;
  407. }
  408. if (count > 0) {
  409. ctx->hlist = g_ptr_array_sized_new (count);
  410. }
  411. else {
  412. return FALSE;
  413. }
  414. c = param;
  415. p = param;
  416. ctx->htable = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
  417. while (p <= end) {
  418. if ((p == end || *p == ':') && p - c > 0) {
  419. oversign = FALSE;
  420. h = rspamd_mempool_alloc (ctx->pool, p - c + 1);
  421. rspamd_strlcpy (h, c, p - c + 1);
  422. g_strstrip (h);
  423. if (sign && rspamd_lc_cmp (h, "(o)", 3) == 0) {
  424. oversign = TRUE;
  425. h += 3;
  426. msg_debug_dkim ("oversign header: %s", h);
  427. }
  428. /* Check mandatory from */
  429. if (!from_found && g_ascii_strcasecmp (h, "from") == 0) {
  430. from_found = TRUE;
  431. }
  432. new = rspamd_mempool_alloc (ctx->pool,
  433. sizeof (struct rspamd_dkim_header));
  434. new->name = h;
  435. new->count = 0;
  436. u.n = 0;
  437. g_ptr_array_add (ctx->hlist, new);
  438. found = g_hash_table_lookup (ctx->htable, h);
  439. if (oversign) {
  440. if (found) {
  441. msg_err_dkim ("specified oversigned header more than once: %s",
  442. h);
  443. }
  444. u.s.flags |= RSPAMD_DKIM_FLAG_OVERSIGN;
  445. u.s.count = 0;
  446. }
  447. else {
  448. if (found != NULL) {
  449. u.n = GPOINTER_TO_UINT (found);
  450. new->count = u.s.count;
  451. u.s.count ++;
  452. }
  453. else {
  454. /* Insert new header order to the list */
  455. u.s.count = new->count + 1;
  456. }
  457. }
  458. g_hash_table_insert (ctx->htable, h, GUINT_TO_POINTER (u.n));
  459. c = p + 1;
  460. p++;
  461. }
  462. else {
  463. p++;
  464. }
  465. }
  466. if (!ctx->hlist) {
  467. g_set_error (err,
  468. DKIM_ERROR,
  469. DKIM_SIGERROR_INVALID_H,
  470. "invalid dkim header list");
  471. return FALSE;
  472. }
  473. else {
  474. if (!from_found) {
  475. g_ptr_array_free (ctx->hlist, TRUE);
  476. g_set_error (err,
  477. DKIM_ERROR,
  478. DKIM_SIGERROR_INVALID_H,
  479. "invalid dkim header list, from header is missing");
  480. return FALSE;
  481. }
  482. rspamd_mempool_add_destructor (ctx->pool,
  483. (rspamd_mempool_destruct_t)rspamd_dkim_hlist_free,
  484. ctx->hlist);
  485. rspamd_mempool_add_destructor (ctx->pool,
  486. (rspamd_mempool_destruct_t)g_hash_table_unref,
  487. ctx->htable);
  488. }
  489. return TRUE;
  490. }
  491. static gboolean
  492. rspamd_dkim_parse_hdrlist (rspamd_dkim_context_t *ctx,
  493. const gchar *param,
  494. gsize len,
  495. GError **err)
  496. {
  497. return rspamd_dkim_parse_hdrlist_common (&ctx->common, param, len, FALSE, err);
  498. }
  499. static gboolean
  500. rspamd_dkim_parse_version (rspamd_dkim_context_t * ctx,
  501. const gchar *param,
  502. gsize len,
  503. GError **err)
  504. {
  505. if (len != 1 || *param != '1') {
  506. g_set_error (err,
  507. DKIM_ERROR,
  508. DKIM_SIGERROR_VERSION,
  509. "invalid dkim version");
  510. return FALSE;
  511. }
  512. ctx->ver = 1;
  513. return TRUE;
  514. }
  515. static gboolean
  516. rspamd_dkim_parse_timestamp (rspamd_dkim_context_t * ctx,
  517. const gchar *param,
  518. gsize len,
  519. GError **err)
  520. {
  521. gulong val;
  522. if (!rspamd_strtoul (param, len, &val)) {
  523. g_set_error (err,
  524. DKIM_ERROR,
  525. DKIM_SIGERROR_UNKNOWN,
  526. "invalid dkim timestamp");
  527. return FALSE;
  528. }
  529. ctx->timestamp = val;
  530. return TRUE;
  531. }
  532. static gboolean
  533. rspamd_dkim_parse_expiration (rspamd_dkim_context_t * ctx,
  534. const gchar *param,
  535. gsize len,
  536. GError **err)
  537. {
  538. gulong val;
  539. if (!rspamd_strtoul (param, len, &val)) {
  540. g_set_error (err,
  541. DKIM_ERROR,
  542. DKIM_SIGERROR_UNKNOWN,
  543. "invalid dkim expiration");
  544. return FALSE;
  545. }
  546. ctx->expiration = val;
  547. return TRUE;
  548. }
  549. static gboolean
  550. rspamd_dkim_parse_bodyhash (rspamd_dkim_context_t * ctx,
  551. const gchar *param,
  552. gsize len,
  553. GError **err)
  554. {
  555. ctx->bh = rspamd_mempool_alloc0 (ctx->pool, len);
  556. (void)rspamd_cryptobox_base64_decode (param, len, ctx->bh, &ctx->bhlen);
  557. return TRUE;
  558. }
  559. static gboolean
  560. rspamd_dkim_parse_bodylength (rspamd_dkim_context_t * ctx,
  561. const gchar *param,
  562. gsize len,
  563. GError **err)
  564. {
  565. gulong val;
  566. if (!rspamd_strtoul (param, len, &val)) {
  567. g_set_error (err,
  568. DKIM_ERROR,
  569. DKIM_SIGERROR_INVALID_L,
  570. "invalid dkim body length");
  571. return FALSE;
  572. }
  573. ctx->common.len = val;
  574. return TRUE;
  575. }
  576. static gboolean
  577. rspamd_dkim_parse_idx (rspamd_dkim_context_t * ctx,
  578. const gchar *param,
  579. gsize len,
  580. GError **err)
  581. {
  582. gulong val;
  583. if (!rspamd_strtoul (param, len, &val)) {
  584. g_set_error (err,
  585. DKIM_ERROR,
  586. DKIM_SIGERROR_INVALID_L,
  587. "invalid ARC idx");
  588. return FALSE;
  589. }
  590. ctx->common.idx = val;
  591. return TRUE;
  592. }
  593. static gboolean
  594. rspamd_dkim_parse_cv (rspamd_dkim_context_t * ctx,
  595. const gchar *param,
  596. gsize len,
  597. GError **err)
  598. {
  599. /* Only check header */
  600. if (len == 4 && memcmp (param, "fail", len) == 0) {
  601. ctx->cv = RSPAMD_ARC_FAIL;
  602. return TRUE;
  603. }
  604. else if (len == 4 && memcmp (param, "pass", len) == 0) {
  605. ctx->cv = RSPAMD_ARC_PASS;
  606. return TRUE;
  607. }
  608. else if (len == 4 && memcmp (param, "none", len) == 0) {
  609. ctx->cv = RSPAMD_ARC_NONE;
  610. return TRUE;
  611. }
  612. else if (len == 7 && memcmp (param, "invalid", len) == 0) {
  613. ctx->cv = RSPAMD_ARC_INVALID;
  614. return TRUE;
  615. }
  616. g_set_error (err,
  617. DKIM_ERROR,
  618. DKIM_SIGERROR_UNKNOWN,
  619. "invalid arc seal verification result");
  620. return FALSE;
  621. }
  622. static void
  623. rspamd_dkim_add_arc_seal_headers (rspamd_mempool_t *pool,
  624. struct rspamd_dkim_common_ctx *ctx)
  625. {
  626. struct rspamd_dkim_header *hdr;
  627. guint count = ctx->idx, i;
  628. ctx->hlist = g_ptr_array_sized_new (count * 3 - 1);
  629. for (i = 0; i < count; i ++) {
  630. /* Authentication results */
  631. hdr = rspamd_mempool_alloc (pool, sizeof (*hdr));
  632. hdr->name = RSPAMD_DKIM_ARC_AUTHHEADER;
  633. hdr->count = i;
  634. g_ptr_array_add (ctx->hlist, hdr);
  635. /* Arc signature */
  636. hdr = rspamd_mempool_alloc (pool, sizeof (*hdr));
  637. hdr->name = RSPAMD_DKIM_ARC_SIGNHEADER;
  638. hdr->count = i;
  639. g_ptr_array_add (ctx->hlist, hdr);
  640. /* Arc seal (except last one) */
  641. if (i != count - 1) {
  642. hdr = rspamd_mempool_alloc (pool, sizeof (*hdr));
  643. hdr->name = RSPAMD_DKIM_ARC_SEALHEADER;
  644. hdr->count = i;
  645. g_ptr_array_add (ctx->hlist, hdr);
  646. }
  647. }
  648. }
  649. /**
  650. * Create new dkim context from signature
  651. * @param sig message's signature
  652. * @param pool pool to allocate memory from
  653. * @param err pointer to error object
  654. * @return new context or NULL
  655. */
  656. rspamd_dkim_context_t *
  657. rspamd_create_dkim_context (const gchar *sig,
  658. rspamd_mempool_t *pool,
  659. guint time_jitter,
  660. enum rspamd_dkim_type type,
  661. GError **err)
  662. {
  663. const gchar *p, *c, *tag = NULL, *end;
  664. gsize taglen;
  665. gint param = DKIM_PARAM_UNKNOWN;
  666. const EVP_MD *md_alg;
  667. time_t now;
  668. rspamd_dkim_context_t *ctx;
  669. enum {
  670. DKIM_STATE_TAG = 0,
  671. DKIM_STATE_AFTER_TAG,
  672. DKIM_STATE_VALUE,
  673. DKIM_STATE_SKIP_SPACES = 99,
  674. DKIM_STATE_ERROR = 100
  675. } state, next_state;
  676. if (sig == NULL) {
  677. g_set_error (err,
  678. DKIM_ERROR,
  679. DKIM_SIGERROR_EMPTY_B,
  680. "empty signature");
  681. return NULL;
  682. }
  683. ctx = rspamd_mempool_alloc0 (pool, sizeof (rspamd_dkim_context_t));
  684. ctx->pool = pool;
  685. if (type == RSPAMD_DKIM_ARC_SEAL) {
  686. ctx->common.header_canon_type = DKIM_CANON_RELAXED;
  687. ctx->common.body_canon_type = DKIM_CANON_RELAXED;
  688. }
  689. else {
  690. ctx->common.header_canon_type = DKIM_CANON_DEFAULT;
  691. ctx->common.body_canon_type = DKIM_CANON_DEFAULT;
  692. }
  693. ctx->sig_alg = DKIM_SIGN_UNKNOWN;
  694. ctx->common.pool = pool;
  695. ctx->common.type = type;
  696. /* A simple state machine of parsing tags */
  697. state = DKIM_STATE_SKIP_SPACES;
  698. next_state = DKIM_STATE_TAG;
  699. taglen = 0;
  700. p = sig;
  701. c = sig;
  702. end = p + strlen (p);
  703. ctx->common.sig_hash = rspamd_cryptobox_fast_hash (sig, end - sig,
  704. rspamd_hash_seed ());
  705. while (p <= end) {
  706. switch (state) {
  707. case DKIM_STATE_TAG:
  708. if (g_ascii_isspace (*p)) {
  709. taglen = p - c;
  710. while (*p && g_ascii_isspace (*p)) {
  711. /* Skip spaces before '=' sign */
  712. p++;
  713. }
  714. if (*p != '=') {
  715. g_set_error (err,
  716. DKIM_ERROR,
  717. DKIM_SIGERROR_UNKNOWN,
  718. "invalid dkim param");
  719. state = DKIM_STATE_ERROR;
  720. }
  721. else {
  722. state = DKIM_STATE_SKIP_SPACES;
  723. next_state = DKIM_STATE_AFTER_TAG;
  724. param = DKIM_PARAM_UNKNOWN;
  725. p++;
  726. tag = c;
  727. }
  728. }
  729. else if (*p == '=') {
  730. state = DKIM_STATE_SKIP_SPACES;
  731. next_state = DKIM_STATE_AFTER_TAG;
  732. param = DKIM_PARAM_UNKNOWN;
  733. p++;
  734. tag = c;
  735. }
  736. else {
  737. taglen++;
  738. p++;
  739. }
  740. break;
  741. case DKIM_STATE_AFTER_TAG:
  742. /* We got tag at tag and len at taglen */
  743. switch (taglen) {
  744. case 0:
  745. g_set_error (err,
  746. DKIM_ERROR,
  747. DKIM_SIGERROR_UNKNOWN,
  748. "zero length dkim param");
  749. state = DKIM_STATE_ERROR;
  750. break;
  751. case 1:
  752. /* Simple tags */
  753. switch (*tag) {
  754. case 'v':
  755. if (type == RSPAMD_DKIM_NORMAL) {
  756. param = DKIM_PARAM_VERSION;
  757. }
  758. else {
  759. g_set_error (err,
  760. DKIM_ERROR,
  761. DKIM_SIGERROR_UNKNOWN,
  762. "invalid ARC v param");
  763. state = DKIM_STATE_ERROR;
  764. break;
  765. }
  766. break;
  767. case 'a':
  768. param = DKIM_PARAM_SIGNALG;
  769. break;
  770. case 'b':
  771. param = DKIM_PARAM_SIGNATURE;
  772. break;
  773. case 'c':
  774. param = DKIM_PARAM_CANONALG;
  775. break;
  776. case 'd':
  777. param = DKIM_PARAM_DOMAIN;
  778. break;
  779. case 'h':
  780. if (type == RSPAMD_DKIM_ARC_SEAL) {
  781. g_set_error (err,
  782. DKIM_ERROR,
  783. DKIM_SIGERROR_UNKNOWN,
  784. "ARC seal must NOT have h= tag");
  785. state = DKIM_STATE_ERROR;
  786. break;
  787. }
  788. else {
  789. param = DKIM_PARAM_HDRLIST;
  790. }
  791. break;
  792. case 'i':
  793. if (type == RSPAMD_DKIM_NORMAL) {
  794. param = DKIM_PARAM_IDENTITY;
  795. }
  796. else {
  797. param = DKIM_PARAM_IDX;
  798. }
  799. break;
  800. case 'l':
  801. param = DKIM_PARAM_BODYLENGTH;
  802. break;
  803. case 'q':
  804. param = DKIM_PARAM_QUERYMETHOD;
  805. break;
  806. case 's':
  807. param = DKIM_PARAM_SELECTOR;
  808. break;
  809. case 't':
  810. param = DKIM_PARAM_TIMESTAMP;
  811. break;
  812. case 'x':
  813. param = DKIM_PARAM_EXPIRATION;
  814. break;
  815. case 'z':
  816. param = DKIM_PARAM_COPIEDHDRS;
  817. break;
  818. case 'r':
  819. param = DKIM_PARAM_IGNORE;
  820. break;
  821. default:
  822. g_set_error (err,
  823. DKIM_ERROR,
  824. DKIM_SIGERROR_UNKNOWN,
  825. "invalid dkim param: %c",
  826. *tag);
  827. state = DKIM_STATE_ERROR;
  828. break;
  829. }
  830. break;
  831. case 2:
  832. if (tag[0] == 'b' && tag[1] == 'h') {
  833. if (type == RSPAMD_DKIM_ARC_SEAL) {
  834. g_set_error (err,
  835. DKIM_ERROR,
  836. DKIM_SIGERROR_UNKNOWN,
  837. "ARC seal must NOT have bh= tag");
  838. state = DKIM_STATE_ERROR;
  839. break;
  840. }
  841. else {
  842. param = DKIM_PARAM_BODYHASH;
  843. }
  844. }
  845. else if (tag[0] == 'c' && tag[1] == 'v') {
  846. if (type != RSPAMD_DKIM_ARC_SEAL) {
  847. g_set_error (err,
  848. DKIM_ERROR,
  849. DKIM_SIGERROR_UNKNOWN,
  850. "cv tag is valid for ARC-Seal only");
  851. state = DKIM_STATE_ERROR;
  852. break;
  853. }
  854. else {
  855. param = DKIM_PARAM_CV;
  856. }
  857. }
  858. else {
  859. g_set_error (err,
  860. DKIM_ERROR,
  861. DKIM_SIGERROR_UNKNOWN,
  862. "invalid dkim param: %c%c",
  863. tag[0],
  864. tag[1]);
  865. state = DKIM_STATE_ERROR;
  866. }
  867. break;
  868. default:
  869. g_set_error (err,
  870. DKIM_ERROR,
  871. DKIM_SIGERROR_UNKNOWN,
  872. "invalid dkim param length: %zd",
  873. taglen);
  874. state = DKIM_STATE_ERROR;
  875. break;
  876. }
  877. if (state != DKIM_STATE_ERROR) {
  878. /* Skip spaces */
  879. state = DKIM_STATE_SKIP_SPACES;
  880. next_state = DKIM_STATE_VALUE;
  881. }
  882. break;
  883. case DKIM_STATE_VALUE:
  884. if (*p == ';') {
  885. if (param == DKIM_PARAM_UNKNOWN ||
  886. p - c == 0) {
  887. state = DKIM_STATE_ERROR;
  888. }
  889. else {
  890. /* Cut trailing spaces for value */
  891. gint tlen = p - c;
  892. const gchar *tmp = p - 1;
  893. while (tlen > 0) {
  894. if (!g_ascii_isspace (*tmp)) {
  895. break;
  896. }
  897. tlen --;
  898. tmp --;
  899. }
  900. if (!parser_funcs[param](ctx, c, tlen, err)) {
  901. state = DKIM_STATE_ERROR;
  902. }
  903. else {
  904. state = DKIM_STATE_SKIP_SPACES;
  905. next_state = DKIM_STATE_TAG;
  906. p++;
  907. taglen = 0;
  908. }
  909. }
  910. }
  911. else if (p == end) {
  912. if (param == DKIM_PARAM_UNKNOWN) {
  913. state = DKIM_STATE_ERROR;
  914. }
  915. else {
  916. gint tlen = p - c;
  917. const gchar *tmp = p - 1;
  918. while (tlen > 0) {
  919. if (!g_ascii_isspace (*tmp)) {
  920. break;
  921. }
  922. tlen --;
  923. tmp --;
  924. }
  925. if (!parser_funcs[param](ctx, c, tlen, err)) {
  926. state = DKIM_STATE_ERROR;
  927. }
  928. /* Finish processing */
  929. p++;
  930. }
  931. }
  932. else {
  933. p++;
  934. }
  935. break;
  936. case DKIM_STATE_SKIP_SPACES:
  937. if (g_ascii_isspace (*p)) {
  938. p++;
  939. }
  940. else {
  941. c = p;
  942. state = next_state;
  943. }
  944. break;
  945. case DKIM_STATE_ERROR:
  946. if (err && *err) {
  947. msg_info_dkim ("dkim parse failed: %s", (*err)->message);
  948. return NULL;
  949. }
  950. else {
  951. msg_info_dkim ("dkim parse failed: unknown error when parsing %c tag",
  952. *tag);
  953. return NULL;
  954. }
  955. break;
  956. }
  957. }
  958. if (type == RSPAMD_DKIM_ARC_SEAL) {
  959. rspamd_dkim_add_arc_seal_headers (pool, &ctx->common);
  960. }
  961. /* Now check validity of signature */
  962. if (ctx->b == NULL) {
  963. g_set_error (err,
  964. DKIM_ERROR,
  965. DKIM_SIGERROR_EMPTY_B,
  966. "b parameter missing");
  967. return NULL;
  968. }
  969. if (ctx->common.type != RSPAMD_DKIM_ARC_SEAL && ctx->bh == NULL) {
  970. g_set_error (err,
  971. DKIM_ERROR,
  972. DKIM_SIGERROR_EMPTY_BH,
  973. "bh parameter missing");
  974. return NULL;
  975. }
  976. if (ctx->domain == NULL) {
  977. g_set_error (err,
  978. DKIM_ERROR,
  979. DKIM_SIGERROR_EMPTY_D,
  980. "domain parameter missing");
  981. return NULL;
  982. }
  983. if (ctx->selector == NULL) {
  984. g_set_error (err,
  985. DKIM_ERROR,
  986. DKIM_SIGERROR_EMPTY_S,
  987. "selector parameter missing");
  988. return NULL;
  989. }
  990. if (ctx->common.type == RSPAMD_DKIM_NORMAL && ctx->ver == 0) {
  991. g_set_error (err,
  992. DKIM_ERROR,
  993. DKIM_SIGERROR_EMPTY_V,
  994. "v parameter missing");
  995. return NULL;
  996. }
  997. if (ctx->common.hlist == NULL) {
  998. g_set_error (err,
  999. DKIM_ERROR,
  1000. DKIM_SIGERROR_EMPTY_H,
  1001. "h parameter missing");
  1002. return NULL;
  1003. }
  1004. if (ctx->sig_alg == DKIM_SIGN_UNKNOWN) {
  1005. g_set_error (err,
  1006. DKIM_ERROR,
  1007. DKIM_SIGERROR_EMPTY_S,
  1008. "s parameter missing");
  1009. return NULL;
  1010. }
  1011. if (type != RSPAMD_DKIM_ARC_SEAL) {
  1012. if (ctx->sig_alg == DKIM_SIGN_RSASHA1) {
  1013. /* Check bh length */
  1014. if (ctx->bhlen != (guint) EVP_MD_size (EVP_sha1 ())) {
  1015. g_set_error (err,
  1016. DKIM_ERROR,
  1017. DKIM_SIGERROR_BADSIG,
  1018. "signature has incorrect length: %zu",
  1019. ctx->bhlen);
  1020. return NULL;
  1021. }
  1022. } else if (ctx->sig_alg == DKIM_SIGN_RSASHA256 ||
  1023. ctx->sig_alg == DKIM_SIGN_ECDSASHA256) {
  1024. if (ctx->bhlen !=
  1025. (guint) EVP_MD_size (EVP_sha256 ())) {
  1026. g_set_error (err,
  1027. DKIM_ERROR,
  1028. DKIM_SIGERROR_BADSIG,
  1029. "signature has incorrect length: %zu",
  1030. ctx->bhlen);
  1031. return NULL;
  1032. }
  1033. } else if (ctx->sig_alg == DKIM_SIGN_RSASHA512 ||
  1034. ctx->sig_alg == DKIM_SIGN_ECDSASHA512) {
  1035. if (ctx->bhlen !=
  1036. (guint) EVP_MD_size (EVP_sha512 ())) {
  1037. g_set_error (err,
  1038. DKIM_ERROR,
  1039. DKIM_SIGERROR_BADSIG,
  1040. "signature has incorrect length: %zu",
  1041. ctx->bhlen);
  1042. return NULL;
  1043. }
  1044. }
  1045. }
  1046. /* Check expiration */
  1047. now = time (NULL);
  1048. if (ctx->timestamp && now < ctx->timestamp && ctx->timestamp - now >
  1049. (gint)time_jitter) {
  1050. g_set_error (err,
  1051. DKIM_ERROR,
  1052. DKIM_SIGERROR_FUTURE,
  1053. "signature was made in future, ignoring");
  1054. return NULL;
  1055. }
  1056. if (ctx->expiration && ctx->expiration < now) {
  1057. g_set_error (err,
  1058. DKIM_ERROR,
  1059. DKIM_SIGERROR_EXPIRED,
  1060. "signature has expired");
  1061. return NULL;
  1062. }
  1063. if (ctx->common.type != RSPAMD_DKIM_NORMAL && (ctx->common.idx == 0 ||
  1064. ctx->common.idx > RSPAMD_DKIM_MAX_ARC_IDX)) {
  1065. g_set_error (err,
  1066. DKIM_ERROR,
  1067. DKIM_SIGERROR_UNKNOWN,
  1068. "i parameter missing or invalid for ARC");
  1069. return NULL;
  1070. }
  1071. if (ctx->common.type == RSPAMD_DKIM_ARC_SEAL) {
  1072. if (ctx->cv == RSPAMD_ARC_UNKNOWN) {
  1073. g_set_error (err,
  1074. DKIM_ERROR,
  1075. DKIM_SIGERROR_UNKNOWN,
  1076. "cv parameter missing or invalid for ARC");
  1077. return NULL;
  1078. }
  1079. }
  1080. /* Now create dns key to request further */
  1081. taglen = strlen (ctx->domain) + strlen (ctx->selector) +
  1082. sizeof (DKIM_DNSKEYNAME) + 2;
  1083. ctx->dns_key = rspamd_mempool_alloc (ctx->pool, taglen);
  1084. rspamd_snprintf (ctx->dns_key,
  1085. taglen,
  1086. "%s.%s.%s",
  1087. ctx->selector,
  1088. DKIM_DNSKEYNAME,
  1089. ctx->domain);
  1090. /* Create checksums for further operations */
  1091. if (ctx->sig_alg == DKIM_SIGN_RSASHA1) {
  1092. md_alg = EVP_sha1 ();
  1093. }
  1094. else if (ctx->sig_alg == DKIM_SIGN_RSASHA256 ||
  1095. ctx->sig_alg == DKIM_SIGN_ECDSASHA256 ||
  1096. ctx->sig_alg == DKIM_SIGN_EDDSASHA256) {
  1097. md_alg = EVP_sha256 ();
  1098. }
  1099. else if (ctx->sig_alg == DKIM_SIGN_RSASHA512 ||
  1100. ctx->sig_alg == DKIM_SIGN_ECDSASHA512) {
  1101. md_alg = EVP_sha512 ();
  1102. }
  1103. else {
  1104. g_set_error (err,
  1105. DKIM_ERROR,
  1106. DKIM_SIGERROR_BADSIG,
  1107. "signature has unsupported signature algorithm");
  1108. return NULL;
  1109. }
  1110. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  1111. ctx->common.body_hash = EVP_MD_CTX_create ();
  1112. EVP_DigestInit_ex (ctx->common.body_hash, md_alg, NULL);
  1113. ctx->common.headers_hash = EVP_MD_CTX_create ();
  1114. EVP_DigestInit_ex (ctx->common.headers_hash, md_alg, NULL);
  1115. rspamd_mempool_add_destructor (pool,
  1116. (rspamd_mempool_destruct_t)EVP_MD_CTX_destroy, ctx->common.body_hash);
  1117. rspamd_mempool_add_destructor (pool,
  1118. (rspamd_mempool_destruct_t)EVP_MD_CTX_destroy, ctx->common.headers_hash);
  1119. #else
  1120. ctx->common.body_hash = EVP_MD_CTX_new ();
  1121. EVP_DigestInit_ex (ctx->common.body_hash, md_alg, NULL);
  1122. ctx->common.headers_hash = EVP_MD_CTX_new ();
  1123. EVP_DigestInit_ex (ctx->common.headers_hash, md_alg, NULL);
  1124. rspamd_mempool_add_destructor (pool,
  1125. (rspamd_mempool_destruct_t)EVP_MD_CTX_free, ctx->common.body_hash);
  1126. rspamd_mempool_add_destructor (pool,
  1127. (rspamd_mempool_destruct_t)EVP_MD_CTX_free, ctx->common.headers_hash);
  1128. #endif
  1129. ctx->dkim_header = sig;
  1130. return ctx;
  1131. }
  1132. struct rspamd_dkim_key_cbdata {
  1133. rspamd_dkim_context_t *ctx;
  1134. dkim_key_handler_f handler;
  1135. gpointer ud;
  1136. };
  1137. rspamd_dkim_key_t *
  1138. rspamd_dkim_make_key (const gchar *keydata,
  1139. guint keylen, enum rspamd_dkim_key_type type, GError **err)
  1140. {
  1141. rspamd_dkim_key_t *key = NULL;
  1142. if (keylen < 3) {
  1143. g_set_error (err,
  1144. DKIM_ERROR,
  1145. DKIM_SIGERROR_KEYFAIL,
  1146. "DKIM key is too short to be valid");
  1147. return NULL;
  1148. }
  1149. key = g_malloc0 (sizeof (rspamd_dkim_key_t));
  1150. REF_INIT_RETAIN (key, rspamd_dkim_key_free);
  1151. key->keydata = g_malloc0 (keylen + 1);
  1152. key->decoded_len = keylen;
  1153. key->keylen = keylen;
  1154. key->type = type;
  1155. rspamd_cryptobox_base64_decode (keydata, keylen, key->keydata,
  1156. &key->decoded_len);
  1157. if (key->type == RSPAMD_DKIM_KEY_EDDSA) {
  1158. key->key.key_eddsa = key->keydata;
  1159. if (key->decoded_len != rspamd_cryptobox_pk_sig_bytes (
  1160. RSPAMD_CRYPTOBOX_MODE_25519)) {
  1161. g_set_error (err,
  1162. DKIM_ERROR,
  1163. DKIM_SIGERROR_KEYFAIL,
  1164. "DKIM key is has invalid length %d for eddsa",
  1165. (gint)key->decoded_len);
  1166. REF_RELEASE (key);
  1167. return NULL;
  1168. }
  1169. }
  1170. else {
  1171. key->key_bio = BIO_new_mem_buf (key->keydata, key->decoded_len);
  1172. if (key->key_bio == NULL) {
  1173. g_set_error (err,
  1174. DKIM_ERROR,
  1175. DKIM_SIGERROR_KEYFAIL,
  1176. "cannot make ssl bio from key");
  1177. REF_RELEASE (key);
  1178. return NULL;
  1179. }
  1180. key->key_evp = d2i_PUBKEY_bio (key->key_bio, NULL);
  1181. if (key->key_evp == NULL) {
  1182. g_set_error (err,
  1183. DKIM_ERROR,
  1184. DKIM_SIGERROR_KEYFAIL,
  1185. "cannot extract pubkey from bio");
  1186. REF_RELEASE (key);
  1187. return NULL;
  1188. }
  1189. if (type == RSPAMD_DKIM_KEY_RSA) {
  1190. key->key.key_rsa = EVP_PKEY_get1_RSA (key->key_evp);
  1191. if (key->key.key_rsa == NULL) {
  1192. g_set_error (err,
  1193. DKIM_ERROR,
  1194. DKIM_SIGERROR_KEYFAIL,
  1195. "cannot extract rsa key from evp key");
  1196. REF_RELEASE (key);
  1197. return NULL;
  1198. }
  1199. } else {
  1200. key->key.key_ecdsa = EVP_PKEY_get1_EC_KEY (key->key_evp);
  1201. if (key->key.key_ecdsa == NULL) {
  1202. g_set_error (err,
  1203. DKIM_ERROR,
  1204. DKIM_SIGERROR_KEYFAIL,
  1205. "cannot extract ecdsa key from evp key");
  1206. REF_RELEASE (key);
  1207. return NULL;
  1208. }
  1209. }
  1210. }
  1211. return key;
  1212. }
  1213. /**
  1214. * Free DKIM key
  1215. * @param key
  1216. */
  1217. void
  1218. rspamd_dkim_key_free (rspamd_dkim_key_t *key)
  1219. {
  1220. if (key->key_evp) {
  1221. EVP_PKEY_free (key->key_evp);
  1222. }
  1223. if (key->type == RSPAMD_DKIM_KEY_RSA) {
  1224. if (key->key.key_rsa) {
  1225. RSA_free (key->key.key_rsa);
  1226. }
  1227. }
  1228. else if (key->type == RSPAMD_DKIM_KEY_ECDSA) {
  1229. if (key->key.key_ecdsa) {
  1230. EC_KEY_free (key->key.key_ecdsa);
  1231. }
  1232. }
  1233. /* Nothing in case of eddsa key */
  1234. if (key->key_bio) {
  1235. BIO_free (key->key_bio);
  1236. }
  1237. g_free (key->keydata);
  1238. g_free (key);
  1239. }
  1240. void
  1241. rspamd_dkim_sign_key_free (rspamd_dkim_sign_key_t *key)
  1242. {
  1243. if (key->key_evp) {
  1244. EVP_PKEY_free (key->key_evp);
  1245. }
  1246. if (key->type == RSPAMD_DKIM_KEY_RSA) {
  1247. if (key->key.key_rsa) {
  1248. RSA_free (key->key.key_rsa);
  1249. }
  1250. }
  1251. if (key->key_bio) {
  1252. BIO_free (key->key_bio);
  1253. }
  1254. if (key->type == RSPAMD_DKIM_KEY_EDDSA) {
  1255. rspamd_explicit_memzero (key->key.key_eddsa, key->keylen);
  1256. g_free (key->keydata);
  1257. }
  1258. g_free (key);
  1259. }
  1260. rspamd_dkim_key_t *
  1261. rspamd_dkim_parse_key (const gchar *txt, gsize *keylen, GError **err)
  1262. {
  1263. const gchar *c, *p, *end, *key = NULL, *alg = "rsa";
  1264. enum {
  1265. read_tag = 0,
  1266. read_eqsign,
  1267. read_p_tag,
  1268. read_k_tag,
  1269. } state = read_tag;
  1270. gchar tag = '\0';
  1271. gsize klen = 0, alglen = 0;
  1272. c = txt;
  1273. p = txt;
  1274. end = txt + strlen (txt);
  1275. while (p < end) {
  1276. switch (state) {
  1277. case read_tag:
  1278. if (*p == '=') {
  1279. state = read_eqsign;
  1280. } else {
  1281. tag = *p;
  1282. }
  1283. p++;
  1284. break;
  1285. case read_eqsign:
  1286. if (tag == 'p') {
  1287. state = read_p_tag;
  1288. c = p;
  1289. } else if (tag == 'k') {
  1290. state = read_k_tag;
  1291. c = p;
  1292. } else {
  1293. /* Unknown tag, ignore */
  1294. state = read_tag;
  1295. tag = '\0';
  1296. p++;
  1297. }
  1298. break;
  1299. case read_p_tag:
  1300. if (*p == ';') {
  1301. klen = p - c;
  1302. key = c;
  1303. state = read_tag;
  1304. tag = '\0';
  1305. }
  1306. p++;
  1307. break;
  1308. case read_k_tag:
  1309. if (*p == ';') {
  1310. alglen = p - c;
  1311. alg = c;
  1312. state = read_tag;
  1313. tag = '\0';
  1314. }
  1315. p++;
  1316. break;
  1317. default:
  1318. break;
  1319. }
  1320. }
  1321. /* Leftover */
  1322. switch (state) {
  1323. case read_p_tag:
  1324. klen = p - c;
  1325. key = c;
  1326. break;
  1327. case read_k_tag:
  1328. alglen = p - c;
  1329. alg = c;
  1330. break;
  1331. default:
  1332. break;
  1333. }
  1334. if (klen == 0 || key == NULL) {
  1335. g_set_error (err,
  1336. DKIM_ERROR,
  1337. DKIM_SIGERROR_KEYFAIL,
  1338. "key is missing");
  1339. return NULL;
  1340. }
  1341. if (alglen == 0 || alg == NULL) {
  1342. alg = "rsa"; /* Implicit */
  1343. alglen = 3;
  1344. }
  1345. if (keylen) {
  1346. *keylen = klen;
  1347. }
  1348. if (alglen == 8 && rspamd_lc_cmp (alg, "ecdsa256", alglen) == 0) {
  1349. return rspamd_dkim_make_key (c, klen,
  1350. RSPAMD_DKIM_KEY_ECDSA, err);
  1351. }
  1352. else if (alglen == 7 && rspamd_lc_cmp (alg, "ed25519", alglen) == 0) {
  1353. return rspamd_dkim_make_key (c, klen,
  1354. RSPAMD_DKIM_KEY_EDDSA, err);
  1355. }
  1356. else {
  1357. /* We assume RSA default in all cases */
  1358. return rspamd_dkim_make_key (c, klen,
  1359. RSPAMD_DKIM_KEY_RSA, err);
  1360. }
  1361. g_assert_not_reached ();
  1362. return NULL;
  1363. }
  1364. /* Get TXT request data and parse it */
  1365. static void
  1366. rspamd_dkim_dns_cb (struct rdns_reply *reply, gpointer arg)
  1367. {
  1368. struct rspamd_dkim_key_cbdata *cbdata = arg;
  1369. rspamd_dkim_key_t *key = NULL;
  1370. GError *err = NULL;
  1371. struct rdns_reply_entry *elt;
  1372. gsize keylen = 0;
  1373. if (reply->code != RDNS_RC_NOERROR) {
  1374. gint err_code = DKIM_SIGERROR_NOKEY;
  1375. if (reply->code == RDNS_RC_NOREC) {
  1376. err_code = DKIM_SIGERROR_NOREC;
  1377. }
  1378. else if (reply->code == RDNS_RC_NXDOMAIN) {
  1379. err_code = DKIM_SIGERROR_NOREC;
  1380. }
  1381. g_set_error (&err,
  1382. DKIM_ERROR,
  1383. err_code,
  1384. "dns request to %s failed: %s",
  1385. cbdata->ctx->dns_key,
  1386. rdns_strerror (reply->code));
  1387. cbdata->handler (NULL, 0, cbdata->ctx, cbdata->ud, err);
  1388. }
  1389. else {
  1390. LL_FOREACH (reply->entries, elt)
  1391. {
  1392. if (elt->type == RDNS_REQUEST_TXT) {
  1393. if (err != NULL) {
  1394. /* Free error as it is insignificant */
  1395. g_error_free (err);
  1396. err = NULL;
  1397. }
  1398. key = rspamd_dkim_parse_key (elt->content.txt.data,
  1399. &keylen,
  1400. &err);
  1401. if (key) {
  1402. key->ttl = elt->ttl;
  1403. break;
  1404. }
  1405. }
  1406. }
  1407. cbdata->handler (key, keylen, cbdata->ctx, cbdata->ud, err);
  1408. }
  1409. }
  1410. /**
  1411. * Make DNS request for specified context and obtain and parse key
  1412. * @param ctx dkim context from signature
  1413. * @param resolver dns resolver object
  1414. * @param s async session to make request
  1415. * @return
  1416. */
  1417. gboolean
  1418. rspamd_get_dkim_key (rspamd_dkim_context_t *ctx,
  1419. struct rspamd_task *task,
  1420. dkim_key_handler_f handler,
  1421. gpointer ud)
  1422. {
  1423. struct rspamd_dkim_key_cbdata *cbdata;
  1424. g_return_val_if_fail (ctx != NULL, FALSE);
  1425. g_return_val_if_fail (ctx->dns_key != NULL, FALSE);
  1426. cbdata =
  1427. rspamd_mempool_alloc (ctx->pool,
  1428. sizeof (struct rspamd_dkim_key_cbdata));
  1429. cbdata->ctx = ctx;
  1430. cbdata->handler = handler;
  1431. cbdata->ud = ud;
  1432. return make_dns_request_task_forced (task,
  1433. rspamd_dkim_dns_cb,
  1434. cbdata,
  1435. RDNS_REQUEST_TXT,
  1436. ctx->dns_key);
  1437. }
  1438. static gboolean
  1439. rspamd_dkim_relaxed_body_step (struct rspamd_dkim_common_ctx *ctx, EVP_MD_CTX *ck,
  1440. const gchar **start, guint size,
  1441. guint *remain)
  1442. {
  1443. const gchar *h;
  1444. static gchar buf[BUFSIZ];
  1445. gchar *t;
  1446. guint len, inlen, added = 0;
  1447. gboolean got_sp;
  1448. len = size;
  1449. inlen = sizeof (buf) - 1;
  1450. h = *start;
  1451. t = buf;
  1452. got_sp = FALSE;
  1453. while (len && inlen) {
  1454. if (*h == '\r' || *h == '\n') {
  1455. if (got_sp) {
  1456. /* Ignore spaces at the end of line */
  1457. t --;
  1458. }
  1459. *t++ = '\r';
  1460. *t++ = '\n';
  1461. if (len > 1 && (*h == '\r' && h[1] == '\n')) {
  1462. h += 2;
  1463. len -= 2;
  1464. }
  1465. else {
  1466. h ++;
  1467. len --;
  1468. added ++;
  1469. }
  1470. break;
  1471. }
  1472. else if (g_ascii_isspace (*h)) {
  1473. if (got_sp) {
  1474. /* Ignore multiply spaces */
  1475. h++;
  1476. len--;
  1477. continue;
  1478. }
  1479. else {
  1480. *t++ = ' ';
  1481. h++;
  1482. inlen--;
  1483. len--;
  1484. got_sp = TRUE;
  1485. continue;
  1486. }
  1487. }
  1488. else {
  1489. got_sp = FALSE;
  1490. }
  1491. *t++ = *h++;
  1492. inlen--;
  1493. len--;
  1494. }
  1495. *start = h;
  1496. if (*remain > 0) {
  1497. size_t cklen = MIN(t - buf, *remain + added);
  1498. EVP_DigestUpdate (ck, buf, cklen);
  1499. *remain = *remain - (cklen - added);
  1500. #if 0
  1501. msg_debug_dkim ("update signature with buffer (%ud size, %ud remain, %ud added): %*s",
  1502. cklen, *remain, added, cklen, buf);
  1503. #else
  1504. msg_debug_dkim ("update signature with body buffer "
  1505. "(%ud size, %ud remain, %ud added)",
  1506. cklen, *remain, added);
  1507. #endif
  1508. }
  1509. return (len != 0);
  1510. }
  1511. static gboolean
  1512. rspamd_dkim_simple_body_step (struct rspamd_dkim_common_ctx *ctx,
  1513. EVP_MD_CTX *ck, const gchar **start, guint size,
  1514. guint *remain)
  1515. {
  1516. const gchar *h;
  1517. static gchar buf[BUFSIZ];
  1518. gchar *t;
  1519. guint len, inlen, added = 0;
  1520. len = size;
  1521. inlen = sizeof (buf) - 1;
  1522. h = *start;
  1523. t = &buf[0];
  1524. while (len && inlen) {
  1525. if (*h == '\r' || *h == '\n') {
  1526. *t++ = '\r';
  1527. *t++ = '\n';
  1528. if (len > 1 && (*h == '\r' && h[1] == '\n')) {
  1529. h += 2;
  1530. len -= 2;
  1531. }
  1532. else {
  1533. h ++;
  1534. len --;
  1535. added ++;
  1536. }
  1537. break;
  1538. }
  1539. *t++ = *h++;
  1540. inlen--;
  1541. len--;
  1542. }
  1543. *start = h;
  1544. if (*remain > 0) {
  1545. size_t cklen = MIN(t - buf, *remain + added);
  1546. EVP_DigestUpdate (ck, buf, cklen);
  1547. *remain = *remain - (cklen - added);
  1548. msg_debug_dkim ("update signature with body buffer "
  1549. "(%ud size, %ud remain, %ud added)",
  1550. cklen, *remain, added);
  1551. }
  1552. return (len != 0);
  1553. }
  1554. static const gchar *
  1555. rspamd_dkim_skip_empty_lines (const gchar *start, const gchar *end,
  1556. guint type, gboolean sign, gboolean *need_crlf)
  1557. {
  1558. const gchar *p = end - 1, *t;
  1559. enum {
  1560. init = 0,
  1561. init_2,
  1562. got_cr,
  1563. got_lf,
  1564. got_crlf,
  1565. test_spaces,
  1566. } state = init;
  1567. guint skip = 0;
  1568. while (p >= start) {
  1569. switch (state) {
  1570. case init:
  1571. if (*p == '\r') {
  1572. state = got_cr;
  1573. }
  1574. else if (*p == '\n') {
  1575. state = got_lf;
  1576. }
  1577. else if (type == DKIM_CANON_RELAXED && *p == ' ') {
  1578. skip = 0;
  1579. state = test_spaces;
  1580. }
  1581. else {
  1582. if (sign || type != DKIM_CANON_RELAXED) {
  1583. *need_crlf = TRUE;
  1584. }
  1585. goto end;
  1586. }
  1587. break;
  1588. case init_2:
  1589. if (*p == '\r') {
  1590. state = got_cr;
  1591. }
  1592. else if (*p == '\n') {
  1593. state = got_lf;
  1594. }
  1595. else if (type == DKIM_CANON_RELAXED && (*p == ' ' || *p == '\t')) {
  1596. skip = 0;
  1597. state = test_spaces;
  1598. }
  1599. else {
  1600. goto end;
  1601. }
  1602. break;
  1603. case got_cr:
  1604. if (p >= start + 1) {
  1605. if (*(p - 1) == '\r') {
  1606. p --;
  1607. state = got_cr;
  1608. }
  1609. else if (*(p - 1) == '\n') {
  1610. if ((*p - 2) == '\r') {
  1611. /* \r\n\r -> we know about one line */
  1612. p -= 1;
  1613. state = got_crlf;
  1614. }
  1615. else {
  1616. /* \n\r -> we know about one line */
  1617. p -= 1;
  1618. state = got_lf;
  1619. }
  1620. }
  1621. else if (type == DKIM_CANON_RELAXED && (*(p - 1) == ' ' ||
  1622. *(p - 1) == '\t')) {
  1623. skip = 1;
  1624. state = test_spaces;
  1625. }
  1626. else {
  1627. goto end;
  1628. }
  1629. }
  1630. else {
  1631. if (g_ascii_isspace (*(p - 1))) {
  1632. if (type == DKIM_CANON_RELAXED) {
  1633. p -= 1;
  1634. }
  1635. }
  1636. goto end;
  1637. }
  1638. break;
  1639. case got_lf:
  1640. if (p >= start + 1) {
  1641. if (*(p - 1) == '\r') {
  1642. state = got_crlf;
  1643. }
  1644. else if (*(p - 1) == '\n') {
  1645. /* We know about one line */
  1646. p --;
  1647. state = got_lf;
  1648. }
  1649. else if (type == DKIM_CANON_RELAXED && (*(p - 1) == ' ' ||
  1650. *(p - 1) == '\t')) {
  1651. skip = 1;
  1652. state = test_spaces;
  1653. }
  1654. else {
  1655. goto end;
  1656. }
  1657. }
  1658. else {
  1659. if (g_ascii_isspace (*(p - 1))) {
  1660. if (type == DKIM_CANON_RELAXED) {
  1661. p -= 1;
  1662. }
  1663. }
  1664. goto end;
  1665. }
  1666. break;
  1667. case got_crlf:
  1668. if (p >= start + 2) {
  1669. if (*(p - 2) == '\r') {
  1670. p -= 2;
  1671. state = got_cr;
  1672. }
  1673. else if (*(p - 2) == '\n') {
  1674. p -= 2;
  1675. state = got_lf;
  1676. }
  1677. else if (type == DKIM_CANON_RELAXED && (*(p - 2) == ' ' ||
  1678. *(p - 2) == '\t')) {
  1679. skip = 2;
  1680. state = test_spaces;
  1681. }
  1682. else {
  1683. goto end;
  1684. }
  1685. }
  1686. else {
  1687. if (g_ascii_isspace (*(p - 2))) {
  1688. if (type == DKIM_CANON_RELAXED) {
  1689. p -= 2;
  1690. }
  1691. }
  1692. goto end;
  1693. }
  1694. break;
  1695. case test_spaces:
  1696. t = p - skip;
  1697. while (t >= start + 2 && (*t == ' ' || *t == '\t')) {
  1698. t --;
  1699. }
  1700. if (*t == '\r') {
  1701. p = t;
  1702. state = got_cr;
  1703. }
  1704. else if (*t == '\n') {
  1705. p = t;
  1706. state = got_lf;
  1707. }
  1708. else {
  1709. goto end;
  1710. }
  1711. break;
  1712. }
  1713. }
  1714. end:
  1715. return p;
  1716. }
  1717. static gboolean
  1718. rspamd_dkim_canonize_body (struct rspamd_dkim_common_ctx *ctx,
  1719. const gchar *start,
  1720. const gchar *end,
  1721. gboolean sign)
  1722. {
  1723. const gchar *p;
  1724. guint remain = ctx->len ? ctx->len : (guint)(end - start);
  1725. gboolean need_crlf = FALSE;
  1726. if (start == NULL) {
  1727. /* Empty body */
  1728. if (ctx->body_canon_type == DKIM_CANON_SIMPLE) {
  1729. EVP_DigestUpdate (ctx->body_hash, CRLF, sizeof (CRLF) - 1);
  1730. }
  1731. else {
  1732. EVP_DigestUpdate (ctx->body_hash, "", 0);
  1733. }
  1734. }
  1735. else {
  1736. /* Strip extra ending CRLF */
  1737. p = rspamd_dkim_skip_empty_lines (start, end, ctx->body_canon_type,
  1738. sign, &need_crlf);
  1739. end = p + 1;
  1740. if (end == start) {
  1741. /* Empty body */
  1742. if (ctx->body_canon_type == DKIM_CANON_SIMPLE) {
  1743. EVP_DigestUpdate (ctx->body_hash, CRLF, sizeof (CRLF) - 1);
  1744. }
  1745. else {
  1746. EVP_DigestUpdate (ctx->body_hash, "", 0);
  1747. }
  1748. }
  1749. else {
  1750. if (ctx->body_canon_type == DKIM_CANON_SIMPLE) {
  1751. /* Simple canonization */
  1752. while (rspamd_dkim_simple_body_step (ctx, ctx->body_hash,
  1753. &start, end - start, &remain));
  1754. if (need_crlf) {
  1755. start = "\r\n";
  1756. end = start + 2;
  1757. remain = 2;
  1758. rspamd_dkim_simple_body_step (ctx, ctx->body_hash,
  1759. &start, end - start, &remain);
  1760. }
  1761. }
  1762. else {
  1763. while (rspamd_dkim_relaxed_body_step (ctx, ctx->body_hash,
  1764. &start, end - start, &remain)) ;
  1765. if (need_crlf) {
  1766. start = "\r\n";
  1767. end = start + 2;
  1768. remain = 2;
  1769. rspamd_dkim_relaxed_body_step (ctx, ctx->body_hash,
  1770. &start, end - start, &remain);
  1771. }
  1772. }
  1773. }
  1774. return TRUE;
  1775. }
  1776. /* TODO: Implement relaxed algorithm */
  1777. return FALSE;
  1778. }
  1779. /* Update hash converting all CR and LF to CRLF */
  1780. static void
  1781. rspamd_dkim_hash_update (EVP_MD_CTX *ck, const gchar *begin, gsize len)
  1782. {
  1783. const gchar *p, *c, *end;
  1784. end = begin + len;
  1785. p = begin;
  1786. c = p;
  1787. while (p < end) {
  1788. if (*p == '\r') {
  1789. EVP_DigestUpdate (ck, c, p - c);
  1790. EVP_DigestUpdate (ck, CRLF, sizeof (CRLF) - 1);
  1791. p++;
  1792. if (p < end && *p == '\n') {
  1793. p++;
  1794. }
  1795. c = p;
  1796. }
  1797. else if (*p == '\n') {
  1798. EVP_DigestUpdate (ck, c, p - c);
  1799. EVP_DigestUpdate (ck, CRLF, sizeof (CRLF) - 1);
  1800. p++;
  1801. c = p;
  1802. }
  1803. else {
  1804. p++;
  1805. }
  1806. }
  1807. if (p > c) {
  1808. EVP_DigestUpdate (ck, c, p - c);
  1809. }
  1810. }
  1811. /* Update hash by signature value (ignoring b= tag) */
  1812. static void
  1813. rspamd_dkim_signature_update (struct rspamd_dkim_common_ctx *ctx,
  1814. const gchar *begin,
  1815. guint len)
  1816. {
  1817. const gchar *p, *c, *end;
  1818. gboolean tag, skip;
  1819. end = begin + len;
  1820. p = begin;
  1821. c = begin;
  1822. tag = TRUE;
  1823. skip = FALSE;
  1824. while (p < end) {
  1825. if (tag && p[0] == 'b' && p[1] == '=') {
  1826. /* Add to signature */
  1827. msg_debug_dkim ("initial update hash with signature part: %*s",
  1828. p - c + 2,
  1829. c);
  1830. rspamd_dkim_hash_update (ctx->headers_hash, c, p - c + 2);
  1831. skip = TRUE;
  1832. }
  1833. else if (skip && (*p == ';' || p == end - 1)) {
  1834. skip = FALSE;
  1835. c = p;
  1836. }
  1837. else if (!tag && *p == ';') {
  1838. tag = TRUE;
  1839. }
  1840. else if (tag && *p == '=') {
  1841. tag = FALSE;
  1842. }
  1843. p++;
  1844. }
  1845. p--;
  1846. /* Skip \r\n at the end */
  1847. while ((*p == '\r' || *p == '\n') && p >= c) {
  1848. p--;
  1849. }
  1850. if (p - c + 1 > 0) {
  1851. msg_debug_dkim ("final update hash with signature part: %*s", p - c + 1, c);
  1852. rspamd_dkim_hash_update (ctx->headers_hash, c, p - c + 1);
  1853. }
  1854. }
  1855. goffset
  1856. rspamd_dkim_canonize_header_relaxed_str (const gchar *hname,
  1857. const gchar *hvalue,
  1858. gchar *out,
  1859. gsize outlen)
  1860. {
  1861. gchar *t;
  1862. const guchar *h;
  1863. gboolean got_sp;
  1864. /* Name part */
  1865. t = out;
  1866. h = hname;
  1867. while (*h && t - out < outlen) {
  1868. *t++ = lc_map[*h++];
  1869. }
  1870. if (t - out >= outlen) {
  1871. return -1;
  1872. }
  1873. *t++ = ':';
  1874. /* Value part */
  1875. h = hvalue;
  1876. /* Skip spaces at the beginning */
  1877. while (g_ascii_isspace (*h)) {
  1878. h++;
  1879. }
  1880. got_sp = FALSE;
  1881. while (*h && (t - out < outlen)) {
  1882. if (g_ascii_isspace (*h)) {
  1883. if (got_sp) {
  1884. h++;
  1885. continue;
  1886. }
  1887. else {
  1888. got_sp = TRUE;
  1889. *t++ = ' ';
  1890. h++;
  1891. continue;
  1892. }
  1893. }
  1894. else {
  1895. got_sp = FALSE;
  1896. }
  1897. *t++ = *h++;
  1898. }
  1899. if (g_ascii_isspace (*(t - 1))) {
  1900. t--;
  1901. }
  1902. if (t - out >= outlen - 2) {
  1903. return -1;
  1904. }
  1905. *t++ = '\r';
  1906. *t++ = '\n';
  1907. *t = '\0';
  1908. return t - out;
  1909. }
  1910. static gboolean
  1911. rspamd_dkim_canonize_header_relaxed (struct rspamd_dkim_common_ctx *ctx,
  1912. const gchar *header,
  1913. const gchar *header_name,
  1914. gboolean is_sign)
  1915. {
  1916. static gchar st_buf[8192];
  1917. gchar *buf;
  1918. guint inlen;
  1919. goffset r;
  1920. gboolean allocated = FALSE;
  1921. inlen = strlen (header) + strlen (header_name) + sizeof (":" CRLF);
  1922. if (inlen > sizeof (st_buf)) {
  1923. buf = g_malloc (inlen);
  1924. allocated = TRUE;
  1925. }
  1926. else {
  1927. /* Faster */
  1928. buf = st_buf;
  1929. }
  1930. r = rspamd_dkim_canonize_header_relaxed_str (header_name, header, buf, inlen);
  1931. g_assert (r != -1);
  1932. if (!is_sign) {
  1933. msg_debug_dkim ("update signature with header: %s", buf);
  1934. EVP_DigestUpdate (ctx->headers_hash, buf, r);
  1935. }
  1936. else {
  1937. rspamd_dkim_signature_update (ctx, buf, r);
  1938. }
  1939. if (allocated) {
  1940. g_free (buf);
  1941. }
  1942. return TRUE;
  1943. }
  1944. static gboolean
  1945. rspamd_dkim_canonize_header (struct rspamd_dkim_common_ctx *ctx,
  1946. struct rspamd_task *task,
  1947. const gchar *header_name,
  1948. guint count,
  1949. const gchar *dkim_header,
  1950. const gchar *dkim_domain)
  1951. {
  1952. struct rspamd_mime_header *rh;
  1953. gint rh_num = 0;
  1954. GPtrArray *ar;
  1955. if (dkim_header == NULL) {
  1956. ar = g_hash_table_lookup (task->raw_headers, header_name);
  1957. if (ar) {
  1958. /* Check uniqueness of the header */
  1959. rh = g_ptr_array_index (ar, 0);
  1960. if ((rh->type & RSPAMD_HEADER_UNIQUE) && ar->len > 1) {
  1961. guint64 random_cookie = ottery_rand_uint64 ();
  1962. msg_warn_dkim ("header %s is intended to be unique by"
  1963. " email standards, but we have %d headers of this"
  1964. " type, artificially break DKIM check", header_name,
  1965. ar->len);
  1966. rspamd_dkim_hash_update (ctx->headers_hash,
  1967. (const gchar *)&random_cookie,
  1968. sizeof (random_cookie));
  1969. return FALSE;
  1970. }
  1971. if (ar->len > count) {
  1972. /* Set skip count */
  1973. rh_num = ar->len - count - 1;
  1974. }
  1975. else {
  1976. /*
  1977. * If DKIM has less headers requested than there are in a
  1978. * message, then it's fine, it allows adding extra headers
  1979. */
  1980. return TRUE;
  1981. }
  1982. rh = g_ptr_array_index (ar, rh_num);
  1983. if (ctx->header_canon_type == DKIM_CANON_SIMPLE) {
  1984. rspamd_dkim_hash_update (ctx->headers_hash, rh->raw_value,
  1985. rh->raw_len);
  1986. msg_debug_dkim ("update signature with header: %*s",
  1987. (gint)rh->raw_len, rh->raw_value);
  1988. }
  1989. else {
  1990. if (!rspamd_dkim_canonize_header_relaxed (ctx, rh->value,
  1991. header_name, FALSE)) {
  1992. return FALSE;
  1993. }
  1994. }
  1995. }
  1996. }
  1997. else {
  1998. /* For signature check just use the saved dkim header */
  1999. if (ctx->header_canon_type == DKIM_CANON_SIMPLE) {
  2000. /* We need to find our own signature and use it */
  2001. guint i;
  2002. ar = g_hash_table_lookup (task->raw_headers, header_name);
  2003. if (ar) {
  2004. /* We need to find our own signature */
  2005. if (!dkim_domain) {
  2006. return FALSE;
  2007. }
  2008. PTR_ARRAY_FOREACH (ar, i, rh) {
  2009. guint64 th = rspamd_cryptobox_fast_hash (rh->decoded,
  2010. strlen (rh->decoded), rspamd_hash_seed ());
  2011. if (th == ctx->sig_hash) {
  2012. rspamd_dkim_signature_update (ctx, rh->raw_value,
  2013. rh->raw_len);
  2014. break;
  2015. }
  2016. }
  2017. }
  2018. else {
  2019. return FALSE;
  2020. }
  2021. }
  2022. else {
  2023. if (!rspamd_dkim_canonize_header_relaxed (ctx,
  2024. dkim_header,
  2025. header_name,
  2026. TRUE)) {
  2027. return FALSE;
  2028. }
  2029. }
  2030. }
  2031. return TRUE;
  2032. }
  2033. struct rspamd_dkim_cached_hash {
  2034. guchar *digest_normal;
  2035. guchar *digest_cr;
  2036. guchar *digest_crlf;
  2037. gchar *type;
  2038. };
  2039. static struct rspamd_dkim_cached_hash *
  2040. rspamd_dkim_check_bh_cached (struct rspamd_dkim_common_ctx *ctx,
  2041. struct rspamd_task *task, gsize bhlen, gboolean is_sign)
  2042. {
  2043. gchar typebuf[64];
  2044. struct rspamd_dkim_cached_hash *res;
  2045. rspamd_snprintf (typebuf, sizeof (typebuf),
  2046. RSPAMD_MEMPOOL_DKIM_BH_CACHE "%z_%s_%d_%z",
  2047. bhlen,
  2048. ctx->body_canon_type == DKIM_CANON_RELAXED ? "1" : "0",
  2049. !!is_sign,
  2050. ctx->len);
  2051. res = rspamd_mempool_get_variable (task->task_pool,
  2052. typebuf);
  2053. if (!res) {
  2054. res = rspamd_mempool_alloc0 (task->task_pool, sizeof (*res));
  2055. res->type = rspamd_mempool_strdup (task->task_pool, typebuf);
  2056. rspamd_mempool_set_variable (task->task_pool,
  2057. res->type, res, NULL);
  2058. }
  2059. return res;
  2060. }
  2061. /**
  2062. * Check task for dkim context using dkim key
  2063. * @param ctx dkim verify context
  2064. * @param key dkim key (from cache or from dns request)
  2065. * @param task task to check
  2066. * @return
  2067. */
  2068. struct rspamd_dkim_check_result *
  2069. rspamd_dkim_check (rspamd_dkim_context_t *ctx,
  2070. rspamd_dkim_key_t *key,
  2071. struct rspamd_task *task)
  2072. {
  2073. const gchar *body_end, *body_start;
  2074. guchar raw_digest[EVP_MAX_MD_SIZE];
  2075. struct rspamd_dkim_cached_hash *cached_bh = NULL;
  2076. EVP_MD_CTX *cpy_ctx = NULL;
  2077. gsize dlen = 0;
  2078. struct rspamd_dkim_check_result *res;
  2079. guint i;
  2080. struct rspamd_dkim_header *dh;
  2081. gint nid;
  2082. g_return_val_if_fail (ctx != NULL, NULL);
  2083. g_return_val_if_fail (key != NULL, NULL);
  2084. g_return_val_if_fail (task->msg.len > 0, NULL);
  2085. /* First of all find place of body */
  2086. body_end = task->msg.begin + task->msg.len;
  2087. body_start = task->raw_headers_content.body_start;
  2088. res = rspamd_mempool_alloc0 (task->task_pool, sizeof (*res));
  2089. res->ctx = ctx;
  2090. res->selector = ctx->selector;
  2091. res->domain = ctx->domain;
  2092. res->fail_reason = NULL;
  2093. res->short_b = ctx->short_b;
  2094. res->rcode = DKIM_CONTINUE;
  2095. if (!body_start) {
  2096. res->rcode = DKIM_ERROR;
  2097. return res;
  2098. }
  2099. if (ctx->common.type != RSPAMD_DKIM_ARC_SEAL) {
  2100. dlen = EVP_MD_CTX_size (ctx->common.body_hash);
  2101. cached_bh = rspamd_dkim_check_bh_cached (&ctx->common, task,
  2102. dlen, FALSE);
  2103. if (!cached_bh->digest_normal) {
  2104. /* Start canonization of body part */
  2105. if (!rspamd_dkim_canonize_body (&ctx->common, body_start, body_end,
  2106. FALSE)) {
  2107. res->rcode = DKIM_RECORD_ERROR;
  2108. return res;
  2109. }
  2110. }
  2111. }
  2112. /* Now canonize headers */
  2113. for (i = 0; i < ctx->common.hlist->len; i++) {
  2114. dh = g_ptr_array_index (ctx->common.hlist, i);
  2115. rspamd_dkim_canonize_header (&ctx->common, task, dh->name, dh->count,
  2116. NULL, NULL);
  2117. }
  2118. /* Canonize dkim signature */
  2119. switch (ctx->common.type) {
  2120. case RSPAMD_DKIM_NORMAL:
  2121. rspamd_dkim_canonize_header (&ctx->common, task, RSPAMD_DKIM_SIGNHEADER, 0,
  2122. ctx->dkim_header, ctx->domain);
  2123. break;
  2124. case RSPAMD_DKIM_ARC_SIG:
  2125. rspamd_dkim_canonize_header (&ctx->common, task, RSPAMD_DKIM_ARC_SIGNHEADER, 0,
  2126. ctx->dkim_header, ctx->domain);
  2127. break;
  2128. case RSPAMD_DKIM_ARC_SEAL:
  2129. rspamd_dkim_canonize_header (&ctx->common, task, RSPAMD_DKIM_ARC_SEALHEADER, 0,
  2130. ctx->dkim_header, ctx->domain);
  2131. break;
  2132. }
  2133. if (ctx->common.type != RSPAMD_DKIM_ARC_SEAL) {
  2134. if (!cached_bh->digest_normal) {
  2135. /* Copy md_ctx to deal with broken CRLF at the end */
  2136. cpy_ctx = EVP_MD_CTX_create ();
  2137. EVP_MD_CTX_copy (cpy_ctx, ctx->common.body_hash);
  2138. EVP_DigestFinal_ex (cpy_ctx, raw_digest, NULL);
  2139. cached_bh->digest_normal = rspamd_mempool_alloc (task->task_pool,
  2140. sizeof (raw_digest));
  2141. memcpy (cached_bh->digest_normal, raw_digest, sizeof (raw_digest));
  2142. }
  2143. /* Check bh field */
  2144. if (memcmp (ctx->bh, cached_bh->digest_normal, ctx->bhlen) != 0) {
  2145. if (cpy_ctx) {
  2146. msg_debug_dkim (
  2147. "bh value mismatch: %*xs versus %*xs, try add CRLF",
  2148. dlen, ctx->bh,
  2149. dlen, cached_bh->digest_normal);
  2150. /* Try add CRLF */
  2151. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  2152. EVP_MD_CTX_cleanup (cpy_ctx);
  2153. #else
  2154. EVP_MD_CTX_reset (cpy_ctx);
  2155. #endif
  2156. EVP_MD_CTX_copy (cpy_ctx, ctx->common.body_hash);
  2157. EVP_DigestUpdate (cpy_ctx, "\r\n", 2);
  2158. EVP_DigestFinal_ex (cpy_ctx, raw_digest, NULL);
  2159. cached_bh->digest_crlf = rspamd_mempool_alloc (task->task_pool,
  2160. sizeof (raw_digest));
  2161. memcpy (cached_bh->digest_crlf, raw_digest, sizeof (raw_digest));
  2162. if (memcmp (ctx->bh, raw_digest, ctx->bhlen) != 0) {
  2163. msg_debug_dkim (
  2164. "bh value mismatch: %*xs versus %*xs, try add LF",
  2165. dlen, ctx->bh,
  2166. dlen, raw_digest);
  2167. /* Try add LF */
  2168. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  2169. EVP_MD_CTX_cleanup (cpy_ctx);
  2170. #else
  2171. EVP_MD_CTX_reset (cpy_ctx);
  2172. #endif
  2173. EVP_MD_CTX_copy (cpy_ctx, ctx->common.body_hash);
  2174. EVP_DigestUpdate (cpy_ctx, "\n", 1);
  2175. EVP_DigestFinal_ex (cpy_ctx, raw_digest, NULL);
  2176. cached_bh->digest_cr = rspamd_mempool_alloc (task->task_pool,
  2177. sizeof (raw_digest));
  2178. memcpy (cached_bh->digest_cr, raw_digest, sizeof (raw_digest));
  2179. if (memcmp (ctx->bh, raw_digest, ctx->bhlen) != 0) {
  2180. msg_debug_dkim ("bh value mismatch: %*xs versus %*xs",
  2181. dlen, ctx->bh,
  2182. dlen, raw_digest);
  2183. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  2184. EVP_MD_CTX_cleanup (cpy_ctx);
  2185. #else
  2186. EVP_MD_CTX_reset (cpy_ctx);
  2187. #endif
  2188. res->fail_reason = "body hash did not verify";
  2189. res->rcode = DKIM_REJECT;
  2190. EVP_MD_CTX_destroy (cpy_ctx);
  2191. return res;
  2192. }
  2193. }
  2194. }
  2195. else if (cached_bh->digest_crlf) {
  2196. if (memcmp (ctx->bh, cached_bh->digest_crlf, ctx->bhlen) != 0) {
  2197. msg_debug_dkim ("bh value mismatch: %*xs versus %*xs",
  2198. dlen, ctx->bh,
  2199. dlen, cached_bh->digest_crlf);
  2200. if (cached_bh->digest_cr) {
  2201. if (memcmp (ctx->bh, cached_bh->digest_cr, ctx->bhlen) != 0) {
  2202. msg_debug_dkim (
  2203. "bh value mismatch: %*xs versus %*xs",
  2204. dlen, ctx->bh,
  2205. dlen, cached_bh->digest_cr);
  2206. res->fail_reason = "body hash did not verify";
  2207. res->rcode = DKIM_REJECT;
  2208. return res;
  2209. }
  2210. }
  2211. else {
  2212. res->fail_reason = "body hash did not verify";
  2213. res->rcode = DKIM_REJECT;
  2214. return res;
  2215. }
  2216. }
  2217. }
  2218. else {
  2219. msg_debug_dkim (
  2220. "bh value mismatch: %*xs versus %*xs",
  2221. dlen, ctx->bh,
  2222. dlen, cached_bh->digest_normal);
  2223. res->fail_reason = "body hash did not verify";
  2224. res->rcode = DKIM_REJECT;
  2225. return res;
  2226. }
  2227. }
  2228. if (cpy_ctx) {
  2229. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  2230. EVP_MD_CTX_cleanup (cpy_ctx);
  2231. #else
  2232. EVP_MD_CTX_reset (cpy_ctx);
  2233. #endif
  2234. EVP_MD_CTX_destroy (cpy_ctx);
  2235. }
  2236. }
  2237. dlen = EVP_MD_CTX_size (ctx->common.headers_hash);
  2238. EVP_DigestFinal_ex (ctx->common.headers_hash, raw_digest, NULL);
  2239. /* Check headers signature */
  2240. if (ctx->sig_alg == DKIM_SIGN_RSASHA1) {
  2241. nid = NID_sha1;
  2242. }
  2243. else if (ctx->sig_alg == DKIM_SIGN_RSASHA256 ||
  2244. ctx->sig_alg == DKIM_SIGN_ECDSASHA256 ||
  2245. ctx->sig_alg == DKIM_SIGN_EDDSASHA256) {
  2246. nid = NID_sha256;
  2247. }
  2248. else if (ctx->sig_alg == DKIM_SIGN_RSASHA512 ||
  2249. ctx->sig_alg == DKIM_SIGN_ECDSASHA512) {
  2250. nid = NID_sha512;
  2251. }
  2252. else {
  2253. /* Not reached */
  2254. nid = NID_sha1;
  2255. }
  2256. switch (key->type) {
  2257. case RSPAMD_DKIM_KEY_RSA:
  2258. if (RSA_verify (nid, raw_digest, dlen, ctx->b, ctx->blen,
  2259. key->key.key_rsa) != 1) {
  2260. msg_debug_dkim ("rsa verify failed");
  2261. res->rcode = DKIM_REJECT;
  2262. res->fail_reason = "rsa verify failed";
  2263. }
  2264. break;
  2265. case RSPAMD_DKIM_KEY_ECDSA:
  2266. if (ECDSA_verify (nid, raw_digest, dlen, ctx->b, ctx->blen,
  2267. key->key.key_ecdsa) != 1) {
  2268. msg_debug_dkim ("ecdsa verify failed");
  2269. res->rcode = DKIM_REJECT;
  2270. res->fail_reason = "ecdsa verify failed";
  2271. }
  2272. break;
  2273. case RSPAMD_DKIM_KEY_EDDSA:
  2274. if (!rspamd_cryptobox_verify (ctx->b, ctx->blen, raw_digest, dlen,
  2275. key->key.key_eddsa, RSPAMD_CRYPTOBOX_MODE_25519)) {
  2276. msg_debug_dkim ("eddsa verify failed");
  2277. res->rcode = DKIM_REJECT;
  2278. res->fail_reason = "eddsa verify failed";
  2279. }
  2280. break;
  2281. }
  2282. if (ctx->common.type == RSPAMD_DKIM_ARC_SEAL && res == DKIM_CONTINUE) {
  2283. switch (ctx->cv) {
  2284. case RSPAMD_ARC_INVALID:
  2285. msg_info_dkim ("arc seal is invalid i=%d", ctx->common.idx);
  2286. res->rcode = DKIM_PERM_ERROR;
  2287. res->fail_reason = "arc seal is invalid";
  2288. break;
  2289. case RSPAMD_ARC_FAIL:
  2290. msg_info_dkim ("arc seal failed i=%d", ctx->common.idx);
  2291. res->rcode = DKIM_REJECT;
  2292. res->fail_reason = "arc seal failed";
  2293. break;
  2294. default:
  2295. break;
  2296. }
  2297. }
  2298. return res;
  2299. }
  2300. struct rspamd_dkim_check_result *
  2301. rspamd_dkim_create_result (rspamd_dkim_context_t *ctx,
  2302. enum rspamd_dkim_check_rcode rcode,
  2303. struct rspamd_task *task)
  2304. {
  2305. struct rspamd_dkim_check_result *res;
  2306. res = rspamd_mempool_alloc0 (task->task_pool, sizeof (*res));
  2307. res->ctx = ctx;
  2308. res->selector = ctx->selector;
  2309. res->domain = ctx->domain;
  2310. res->fail_reason = NULL;
  2311. res->short_b = ctx->short_b;
  2312. res->rcode = rcode;
  2313. return res;
  2314. }
  2315. rspamd_dkim_key_t *
  2316. rspamd_dkim_key_ref (rspamd_dkim_key_t *k)
  2317. {
  2318. REF_RETAIN (k);
  2319. return k;
  2320. }
  2321. void
  2322. rspamd_dkim_key_unref (rspamd_dkim_key_t *k)
  2323. {
  2324. REF_RELEASE (k);
  2325. }
  2326. rspamd_dkim_sign_key_t *
  2327. rspamd_dkim_sign_key_ref (rspamd_dkim_sign_key_t *k)
  2328. {
  2329. REF_RETAIN (k);
  2330. return k;
  2331. }
  2332. void
  2333. rspamd_dkim_sign_key_unref (rspamd_dkim_sign_key_t *k)
  2334. {
  2335. REF_RELEASE (k);
  2336. }
  2337. const gchar*
  2338. rspamd_dkim_get_domain (rspamd_dkim_context_t *ctx)
  2339. {
  2340. if (ctx) {
  2341. return ctx->domain;
  2342. }
  2343. return NULL;
  2344. }
  2345. const gchar*
  2346. rspamd_dkim_get_selector (rspamd_dkim_context_t *ctx)
  2347. {
  2348. if (ctx) {
  2349. return ctx->selector;
  2350. }
  2351. return NULL;
  2352. }
  2353. guint
  2354. rspamd_dkim_key_get_ttl (rspamd_dkim_key_t *k)
  2355. {
  2356. if (k) {
  2357. return k->ttl;
  2358. }
  2359. return 0;
  2360. }
  2361. const gchar*
  2362. rspamd_dkim_get_dns_key (rspamd_dkim_context_t *ctx)
  2363. {
  2364. if (ctx) {
  2365. return ctx->dns_key;
  2366. }
  2367. return NULL;
  2368. }
  2369. #define PEM_SIG "-----BEGIN"
  2370. rspamd_dkim_sign_key_t*
  2371. rspamd_dkim_sign_key_load (const gchar *key, gsize len,
  2372. enum rspamd_dkim_key_format type,
  2373. GError **err)
  2374. {
  2375. guchar *map = NULL, *tmp = NULL;
  2376. gsize maplen;
  2377. rspamd_dkim_sign_key_t *nkey;
  2378. time_t mtime = time (NULL);
  2379. if (type < 0 || type > RSPAMD_DKIM_KEY_UNKNOWN || len == 0 || key == NULL) {
  2380. g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL,
  2381. "invalid key type to load: %d", type);
  2382. return NULL;
  2383. }
  2384. nkey = g_malloc0 (sizeof (*nkey));
  2385. nkey->mtime = mtime;
  2386. msg_debug_dkim_taskless ("got public key with length %d and type %d",
  2387. len, type);
  2388. /* Load key file if needed */
  2389. if (type == RSPAMD_DKIM_KEY_FILE) {
  2390. struct stat st;
  2391. if (stat (key, &st) != 0) {
  2392. g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL,
  2393. "cannot stat key file: '%s' %s", key, strerror (errno));
  2394. g_free (nkey);
  2395. return NULL;
  2396. }
  2397. nkey->mtime = st.st_mtime;
  2398. map = rspamd_file_xmap (key, PROT_READ, &maplen, TRUE);
  2399. if (map == NULL) {
  2400. g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL,
  2401. "cannot map key file: '%s' %s", key, strerror (errno));
  2402. g_free (nkey);
  2403. return NULL;
  2404. }
  2405. key = map;
  2406. len = maplen;
  2407. if (maplen > sizeof (PEM_SIG) &&
  2408. strncmp (map, PEM_SIG, sizeof (PEM_SIG) - 1) == 0) {
  2409. type = RSPAMD_DKIM_KEY_PEM;
  2410. }
  2411. else if (rspamd_cryptobox_base64_is_valid (map, maplen)) {
  2412. type = RSPAMD_DKIM_KEY_BASE64;
  2413. }
  2414. else {
  2415. type = RSPAMD_DKIM_KEY_RAW;
  2416. }
  2417. }
  2418. if (type == RSPAMD_DKIM_KEY_UNKNOWN) {
  2419. if (len > sizeof (PEM_SIG) &&
  2420. memcmp (key, PEM_SIG, sizeof (PEM_SIG) - 1) == 0) {
  2421. type = RSPAMD_DKIM_KEY_PEM;
  2422. }
  2423. else {
  2424. type = RSPAMD_DKIM_KEY_RAW;
  2425. }
  2426. }
  2427. if (type == RSPAMD_DKIM_KEY_BASE64) {
  2428. type = RSPAMD_DKIM_KEY_RAW;
  2429. tmp = g_malloc (len);
  2430. rspamd_cryptobox_base64_decode (key, len, tmp, &len);
  2431. key = tmp;
  2432. }
  2433. if (type == RSPAMD_DKIM_KEY_RAW && (len == 32 ||
  2434. len == rspamd_cryptobox_sk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519))) {
  2435. if (len == 32) {
  2436. /* Seeded key, need scalarmult */
  2437. unsigned char pk[32];
  2438. nkey->type = RSPAMD_DKIM_KEY_EDDSA;
  2439. nkey->key.key_eddsa = g_malloc (
  2440. rspamd_cryptobox_sk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519));
  2441. ed25519_seed_keypair (pk, nkey->key.key_eddsa, (char *) key);
  2442. nkey->keylen = rspamd_cryptobox_sk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519);
  2443. }
  2444. else {
  2445. /* Full ed25519 key */
  2446. unsigned klen = rspamd_cryptobox_sk_sig_bytes (RSPAMD_CRYPTOBOX_MODE_25519);
  2447. nkey->type = RSPAMD_DKIM_KEY_EDDSA;
  2448. nkey->key.key_eddsa = g_malloc (klen);
  2449. memcpy (nkey->key.key_eddsa, key, klen);
  2450. nkey->keylen = klen;
  2451. }
  2452. }
  2453. else {
  2454. nkey->key_bio = BIO_new_mem_buf (key, len);
  2455. if (type == RSPAMD_DKIM_KEY_RAW) {
  2456. if (d2i_PrivateKey_bio (nkey->key_bio, &nkey->key_evp) == NULL) {
  2457. g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL,
  2458. "cannot parse raw private key: %s",
  2459. ERR_error_string (ERR_get_error (), NULL));
  2460. rspamd_dkim_sign_key_free (nkey);
  2461. nkey = NULL;
  2462. goto end;
  2463. }
  2464. }
  2465. else {
  2466. if (!PEM_read_bio_PrivateKey (nkey->key_bio, &nkey->key_evp, NULL, NULL)) {
  2467. g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL,
  2468. "cannot parse pem private key: %s",
  2469. ERR_error_string (ERR_get_error (), NULL));
  2470. rspamd_dkim_sign_key_free (nkey);
  2471. nkey = NULL;
  2472. goto end;
  2473. }
  2474. }
  2475. nkey->key.key_rsa = EVP_PKEY_get1_RSA (nkey->key_evp);
  2476. if (nkey->key.key_rsa == NULL) {
  2477. g_set_error (err,
  2478. DKIM_ERROR,
  2479. DKIM_SIGERROR_KEYFAIL,
  2480. "cannot extract rsa key from evp key");
  2481. rspamd_dkim_sign_key_free (nkey);
  2482. nkey = NULL;
  2483. goto end;
  2484. }
  2485. nkey->type = RSPAMD_DKIM_KEY_RSA;
  2486. }
  2487. REF_INIT_RETAIN (nkey, rspamd_dkim_sign_key_free);
  2488. end:
  2489. if (map != NULL) {
  2490. munmap (map, maplen);
  2491. }
  2492. if (tmp != NULL) {
  2493. rspamd_explicit_memzero (tmp, len);
  2494. g_free (tmp);
  2495. }
  2496. return nkey;
  2497. }
  2498. #undef PEM_SIG
  2499. gboolean
  2500. rspamd_dkim_sign_key_maybe_invalidate (rspamd_dkim_sign_key_t *key, time_t mtime)
  2501. {
  2502. if (mtime > key->mtime) {
  2503. return TRUE;
  2504. }
  2505. return FALSE;
  2506. }
  2507. rspamd_dkim_sign_context_t *
  2508. rspamd_create_dkim_sign_context (struct rspamd_task *task,
  2509. rspamd_dkim_sign_key_t *priv_key,
  2510. gint headers_canon,
  2511. gint body_canon,
  2512. const gchar *headers,
  2513. enum rspamd_dkim_type type,
  2514. GError **err)
  2515. {
  2516. rspamd_dkim_sign_context_t *nctx;
  2517. if (headers_canon != DKIM_CANON_SIMPLE && headers_canon != DKIM_CANON_RELAXED) {
  2518. g_set_error (err,
  2519. DKIM_ERROR,
  2520. DKIM_SIGERROR_INVALID_HC,
  2521. "bad headers canonicalisation");
  2522. return NULL;
  2523. }
  2524. if (body_canon != DKIM_CANON_SIMPLE && body_canon != DKIM_CANON_RELAXED) {
  2525. g_set_error (err,
  2526. DKIM_ERROR,
  2527. DKIM_SIGERROR_INVALID_BC,
  2528. "bad body canonicalisation");
  2529. return NULL;
  2530. }
  2531. if (!priv_key || (!priv_key->key.key_rsa && !priv_key->key.key_eddsa)) {
  2532. g_set_error (err,
  2533. DKIM_ERROR,
  2534. DKIM_SIGERROR_KEYFAIL,
  2535. "bad key to sign");
  2536. return NULL;
  2537. }
  2538. nctx = rspamd_mempool_alloc0 (task->task_pool, sizeof (*nctx));
  2539. nctx->common.pool = task->task_pool;
  2540. nctx->common.header_canon_type = headers_canon;
  2541. nctx->common.body_canon_type = body_canon;
  2542. nctx->common.type = type;
  2543. if (type != RSPAMD_DKIM_ARC_SEAL) {
  2544. if (!rspamd_dkim_parse_hdrlist_common (&nctx->common, headers,
  2545. strlen (headers), TRUE,
  2546. err)) {
  2547. return NULL;
  2548. }
  2549. }
  2550. else {
  2551. rspamd_dkim_add_arc_seal_headers (task->task_pool, &nctx->common);
  2552. }
  2553. nctx->key = rspamd_dkim_sign_key_ref (priv_key);
  2554. rspamd_mempool_add_destructor (task->task_pool,
  2555. (rspamd_mempool_destruct_t)rspamd_dkim_sign_key_unref, priv_key);
  2556. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  2557. nctx->common.body_hash = EVP_MD_CTX_create ();
  2558. EVP_DigestInit_ex (nctx->common.body_hash, EVP_sha256 (), NULL);
  2559. nctx->common.headers_hash = EVP_MD_CTX_create ();
  2560. EVP_DigestInit_ex (nctx->common.headers_hash, EVP_sha256 (), NULL);
  2561. rspamd_mempool_add_destructor (task->task_pool,
  2562. (rspamd_mempool_destruct_t)EVP_MD_CTX_destroy, nctx->common.body_hash);
  2563. rspamd_mempool_add_destructor (task->task_pool,
  2564. (rspamd_mempool_destruct_t)EVP_MD_CTX_destroy, nctx->common.headers_hash);
  2565. #else
  2566. nctx->common.body_hash = EVP_MD_CTX_new ();
  2567. EVP_DigestInit_ex (nctx->common.body_hash, EVP_sha256 (), NULL);
  2568. nctx->common.headers_hash = EVP_MD_CTX_new ();
  2569. EVP_DigestInit_ex (nctx->common.headers_hash, EVP_sha256 (), NULL);
  2570. rspamd_mempool_add_destructor (task->task_pool,
  2571. (rspamd_mempool_destruct_t)EVP_MD_CTX_free, nctx->common.body_hash);
  2572. rspamd_mempool_add_destructor (task->task_pool,
  2573. (rspamd_mempool_destruct_t)EVP_MD_CTX_free, nctx->common.headers_hash);
  2574. #endif
  2575. return nctx;
  2576. }
  2577. GString *
  2578. rspamd_dkim_sign (struct rspamd_task *task, const gchar *selector,
  2579. const gchar *domain, time_t expire, gsize len, guint idx,
  2580. const gchar *arc_cv, rspamd_dkim_sign_context_t *ctx)
  2581. {
  2582. GString *hdr;
  2583. struct rspamd_dkim_header *dh;
  2584. const gchar *body_end, *body_start, *hname;
  2585. guchar raw_digest[EVP_MAX_MD_SIZE];
  2586. struct rspamd_dkim_cached_hash *cached_bh = NULL;
  2587. gsize dlen = 0;
  2588. guint i, j;
  2589. gchar *b64_data;
  2590. guchar *sig_buf;
  2591. guint sig_len;
  2592. guint headers_len = 0, cur_len = 0;
  2593. union rspamd_dkim_header_stat hstat;
  2594. g_assert (ctx != NULL);
  2595. /* First of all find place of body */
  2596. body_end = task->msg.begin + task->msg.len;
  2597. body_start = task->raw_headers_content.body_start;
  2598. if (len > 0) {
  2599. ctx->common.len = len;
  2600. }
  2601. if (!body_start) {
  2602. return NULL;
  2603. }
  2604. /* Start canonization of body part */
  2605. if (ctx->common.type != RSPAMD_DKIM_ARC_SEAL) {
  2606. dlen = EVP_MD_CTX_size (ctx->common.body_hash);
  2607. cached_bh = rspamd_dkim_check_bh_cached (&ctx->common, task,
  2608. dlen, TRUE);
  2609. if (!cached_bh->digest_normal) {
  2610. /* Start canonization of body part */
  2611. if (!rspamd_dkim_canonize_body (&ctx->common, body_start, body_end,
  2612. TRUE)) {
  2613. return NULL;
  2614. }
  2615. }
  2616. }
  2617. hdr = g_string_sized_new (255);
  2618. if (ctx->common.type == RSPAMD_DKIM_NORMAL) {
  2619. rspamd_printf_gstring (hdr, "v=1; a=%s; c=%s/%s; d=%s; s=%s; ",
  2620. ctx->key->type == RSPAMD_DKIM_KEY_RSA ?
  2621. "rsa-sha256" : "ed25519-sha256",
  2622. ctx->common.header_canon_type == DKIM_CANON_RELAXED ?
  2623. "relaxed" : "simple",
  2624. ctx->common.body_canon_type == DKIM_CANON_RELAXED ?
  2625. "relaxed" : "simple",
  2626. domain, selector);
  2627. }
  2628. else if (ctx->common.type == RSPAMD_DKIM_ARC_SIG) {
  2629. rspamd_printf_gstring (hdr, "i=%d; a=%s; c=%s/%s; d=%s; s=%s; ",
  2630. idx,
  2631. ctx->key->type == RSPAMD_DKIM_KEY_RSA ?
  2632. "rsa-sha256" : "ed25519-sha256",
  2633. ctx->common.header_canon_type == DKIM_CANON_RELAXED ?
  2634. "relaxed" : "simple",
  2635. ctx->common.body_canon_type == DKIM_CANON_RELAXED ?
  2636. "relaxed" : "simple",
  2637. domain, selector);
  2638. }
  2639. else {
  2640. g_assert (arc_cv != NULL);
  2641. rspamd_printf_gstring (hdr, "i=%d; a=%s; c=%s/%s; d=%s; s=%s; cv=%s; ",
  2642. arc_cv,
  2643. ctx->key->type == RSPAMD_DKIM_KEY_RSA ?
  2644. "rsa-sha256" : "ed25519-sha256",
  2645. idx,
  2646. domain, selector);
  2647. }
  2648. if (expire > 0) {
  2649. rspamd_printf_gstring (hdr, "x=%t; ", expire);
  2650. }
  2651. if (ctx->common.type != RSPAMD_DKIM_ARC_SEAL) {
  2652. if (len > 0) {
  2653. rspamd_printf_gstring (hdr, "l=%z; ", len);
  2654. }
  2655. }
  2656. rspamd_printf_gstring (hdr, "t=%t; h=", time (NULL));
  2657. /* Now canonize headers */
  2658. for (i = 0; i < ctx->common.hlist->len; i++) {
  2659. dh = g_ptr_array_index (ctx->common.hlist, i);
  2660. /* We allow oversigning if dh->count > number of headers with this name */
  2661. hstat.n = GPOINTER_TO_UINT (g_hash_table_lookup (ctx->common.htable, dh->name));
  2662. if (hstat.s.flags & RSPAMD_DKIM_FLAG_OVERSIGN) {
  2663. /* Do oversigning */
  2664. GPtrArray *ar;
  2665. guint count = 0;
  2666. ar = g_hash_table_lookup (task->raw_headers, dh->name);
  2667. if (ar) {
  2668. count = ar->len;
  2669. }
  2670. for (j = 0; j < count; j ++) {
  2671. /* Sign all existing headers */
  2672. rspamd_dkim_canonize_header (&ctx->common, task, dh->name, j,
  2673. NULL, NULL);
  2674. }
  2675. /* Now add one more entry to oversign */
  2676. cur_len = (strlen (dh->name) + 1) * (count + 1);
  2677. headers_len += cur_len;
  2678. if (headers_len > 70 && i > 0 && i < ctx->common.hlist->len - 1) {
  2679. rspamd_printf_gstring (hdr, " ");
  2680. headers_len = cur_len;
  2681. }
  2682. for (j = 0; j < count + 1; j++) {
  2683. rspamd_printf_gstring (hdr, "%s:", dh->name);
  2684. }
  2685. }
  2686. else {
  2687. if (g_hash_table_lookup (task->raw_headers, dh->name)) {
  2688. if (hstat.s.count > 0) {
  2689. cur_len = (strlen (dh->name) + 1) * (hstat.s.count);
  2690. headers_len += cur_len;
  2691. if (headers_len > 70 && i > 0 && i < ctx->common.hlist->len - 1) {
  2692. rspamd_printf_gstring (hdr, " ");
  2693. headers_len = cur_len;
  2694. }
  2695. for (j = 0; j < hstat.s.count; j++) {
  2696. rspamd_printf_gstring (hdr, "%s:", dh->name);
  2697. }
  2698. }
  2699. rspamd_dkim_canonize_header (&ctx->common, task,
  2700. dh->name, dh->count,
  2701. NULL, NULL);
  2702. }
  2703. }
  2704. g_hash_table_remove (ctx->common.htable, dh->name);
  2705. }
  2706. /* Replace the last ':' with ';' */
  2707. hdr->str[hdr->len - 1] = ';';
  2708. if (ctx->common.type != RSPAMD_DKIM_ARC_SEAL) {
  2709. if (!cached_bh->digest_normal) {
  2710. EVP_DigestFinal_ex (ctx->common.body_hash, raw_digest, NULL);
  2711. cached_bh->digest_normal = rspamd_mempool_alloc (task->task_pool,
  2712. sizeof (raw_digest));
  2713. memcpy (cached_bh->digest_normal, raw_digest, sizeof (raw_digest));
  2714. }
  2715. b64_data = rspamd_encode_base64 (cached_bh->digest_normal, dlen, 0, NULL);
  2716. rspamd_printf_gstring (hdr, " bh=%s; b=", b64_data);
  2717. g_free (b64_data);
  2718. }
  2719. else {
  2720. rspamd_printf_gstring (hdr, " b=");
  2721. }
  2722. switch (ctx->common.type) {
  2723. case RSPAMD_DKIM_NORMAL:
  2724. default:
  2725. hname = RSPAMD_DKIM_SIGNHEADER;
  2726. break;
  2727. case RSPAMD_DKIM_ARC_SIG:
  2728. hname = RSPAMD_DKIM_ARC_SIGNHEADER;
  2729. break;
  2730. case RSPAMD_DKIM_ARC_SEAL:
  2731. hname = RSPAMD_DKIM_ARC_SEALHEADER;
  2732. break;
  2733. }
  2734. if (ctx->common.header_canon_type == DKIM_CANON_RELAXED) {
  2735. if (!rspamd_dkim_canonize_header_relaxed (&ctx->common,
  2736. hdr->str,
  2737. hname,
  2738. TRUE)) {
  2739. g_string_free (hdr, TRUE);
  2740. return NULL;
  2741. }
  2742. }
  2743. else {
  2744. /* Will likely have issues with folding */
  2745. rspamd_dkim_hash_update (ctx->common.headers_hash, hdr->str,
  2746. hdr->len);
  2747. msg_debug_task ("update signature with header: %*s",
  2748. (gint)hdr->len, hdr->str);
  2749. }
  2750. dlen = EVP_MD_CTX_size (ctx->common.headers_hash);
  2751. EVP_DigestFinal_ex (ctx->common.headers_hash, raw_digest, NULL);
  2752. if (ctx->key->type == RSPAMD_DKIM_KEY_RSA) {
  2753. sig_len = RSA_size (ctx->key->key.key_rsa);
  2754. sig_buf = g_alloca (sig_len);
  2755. if (RSA_sign (NID_sha256, raw_digest, dlen, sig_buf, &sig_len,
  2756. ctx->key->key.key_rsa) != 1) {
  2757. g_string_free (hdr, TRUE);
  2758. msg_err_task ("rsa sign error: %s",
  2759. ERR_error_string (ERR_get_error (), NULL));
  2760. return NULL;
  2761. }
  2762. } else if (ctx->key->type == RSPAMD_DKIM_KEY_EDDSA) {
  2763. sig_len = rspamd_cryptobox_signature_bytes (RSPAMD_CRYPTOBOX_MODE_25519);
  2764. sig_buf = g_alloca (sig_len);
  2765. rspamd_cryptobox_sign (sig_buf, NULL, raw_digest, dlen,
  2766. ctx->key->key.key_eddsa, RSPAMD_CRYPTOBOX_MODE_25519);
  2767. } else {
  2768. g_string_free (hdr, TRUE);
  2769. msg_err_task ("unsupported key type for signing");
  2770. return NULL;
  2771. }
  2772. if (task->flags & RSPAMD_TASK_FLAG_MILTER) {
  2773. b64_data = rspamd_encode_base64_fold (sig_buf, sig_len, 70, NULL,
  2774. RSPAMD_TASK_NEWLINES_LF);
  2775. }
  2776. else {
  2777. b64_data = rspamd_encode_base64_fold (sig_buf, sig_len, 70, NULL,
  2778. task->nlines_type);
  2779. }
  2780. rspamd_printf_gstring (hdr, "%s", b64_data);
  2781. g_free (b64_data);
  2782. return hdr;
  2783. }
  2784. gboolean
  2785. rspamd_dkim_match_keys (rspamd_dkim_key_t *pk,
  2786. rspamd_dkim_sign_key_t *sk,
  2787. GError **err)
  2788. {
  2789. if (pk == NULL || sk == NULL) {
  2790. g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL,
  2791. "missing public or private key");
  2792. return FALSE;
  2793. }
  2794. if (pk->type != sk->type) {
  2795. g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYFAIL,
  2796. "public and private key types do not match");
  2797. return FALSE;
  2798. }
  2799. if (pk->type == RSPAMD_DKIM_KEY_EDDSA) {
  2800. if (memcmp(sk->key.key_eddsa + 32, pk->key.key_eddsa, 32) != 0) {
  2801. g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYHASHMISMATCH,
  2802. "pubkey does not match private key");
  2803. return FALSE;
  2804. }
  2805. }
  2806. else if (EVP_PKEY_cmp (pk->key_evp, sk->key_evp) != 1) {
  2807. g_set_error (err, dkim_error_quark (), DKIM_SIGERROR_KEYHASHMISMATCH,
  2808. "pubkey does not match private key");
  2809. return FALSE;
  2810. }
  2811. return TRUE;
  2812. }