Use compressed form of ipv6 in $sender_host_address under -bh. Bug 3027
[exim.git] / src / src / pdkim / pdkim.h
1 /*
2  *  PDKIM - a RFC4871 (DKIM) implementation
3  *
4  *  Copyright (C) 2009 - 2012  Tom Kistner <tom@duncanthrax.net>
5  *  Copyright (c) 2016 - 2020  Jeremy Harris
6  *  SPDX-License-Identifier: GPL-2.0-or-later
7  *
8  *  http://duncanthrax.net/pdkim/
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 #ifndef PDKIM_H
25 #define PDKIM_H
26
27 #include "../blob.h"
28 #include "../hash.h"
29
30 #define PDKIM_DEFAULT_SIGN_HEADERS "From:Sender:Reply-To:Subject:Date:"\
31                              "Message-ID:To:Cc:MIME-Version:Content-Type:"\
32                              "Content-Transfer-Encoding:Content-ID:"\
33                              "Content-Description:Resent-Date:Resent-From:"\
34                              "Resent-Sender:Resent-To:Resent-Cc:"\
35                              "Resent-Message-ID:In-Reply-To:References:"\
36                              "List-Id:List-Help:List-Unsubscribe:"\
37                              "List-Subscribe:List-Post:List-Owner:List-Archive"
38
39 #define PDKIM_OVERSIGN_HEADERS "+From:+Sender:+Reply-To:+Subject:+Date:"\
40                              "+Message-ID:+To:+Cc:+MIME-Version:+Content-Type:"\
41                              "+Content-Transfer-Encoding:+Content-ID:"\
42                              "+Content-Description:+Resent-Date:+Resent-From:"\
43                              "+Resent-Sender:+Resent-To:+Resent-Cc:"\
44                              "+Resent-Message-ID:+In-Reply-To:+References:"\
45                              "+List-Id:+List-Help:+List-Unsubscribe:"\
46                              "+List-Subscribe:+List-Post:+List-Owner:+List-Archive"
47
48 /* -------------------------------------------------------------------------- */
49 /* Length of the preallocated buffer for the "answer" from the dns/txt
50    callback function. This should match the maximum RDLENGTH from DNS. */
51 #define PDKIM_DNS_TXT_MAX_RECLEN    (1 << 16)
52
53 /* -------------------------------------------------------------------------- */
54 /* Function success / error codes */
55 #define PDKIM_OK                      0
56 #define PDKIM_FAIL                   -1
57 #define PDKIM_ERR_RSA_PRIVKEY      -101
58 #define PDKIM_ERR_RSA_SIGNING      -102
59 #define PDKIM_ERR_LONG_LINE        -103
60 #define PDKIM_ERR_BUFFER_TOO_SMALL -104
61 #define PDKIM_ERR_EXCESS_SIGS      -105
62 #define PDKIM_SIGN_PRIVKEY_WRAP    -106
63 #define PDKIM_SIGN_PRIVKEY_B64D    -107
64
65 /* -------------------------------------------------------------------------- */
66 /* Main/Extended verification status */
67 #define PDKIM_VERIFY_NONE      0
68 #define PDKIM_VERIFY_INVALID   1
69 #define PDKIM_VERIFY_FAIL      2
70 #define PDKIM_VERIFY_PASS      3
71 #define PDKIM_VERIFY_POLICY    BIT(31)
72
73 #define PDKIM_VERIFY_FAIL_BODY                    1
74 #define PDKIM_VERIFY_FAIL_MESSAGE                 2
75 #define PDKIM_VERIFY_FAIL_SIG_ALGO_MISMATCH       3
76 #define PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE   4
77 #define PDKIM_VERIFY_INVALID_BUFFER_SIZE          5
78 #define PDKIM_VERIFY_INVALID_PUBKEY_DNSRECORD     6
79 #define PDKIM_VERIFY_INVALID_PUBKEY_IMPORT        7
80 #define PDKIM_VERIFY_INVALID_PUBKEY_KEYSIZE       8
81 #define PDKIM_VERIFY_INVALID_SIGNATURE_ERROR      9
82 #define PDKIM_VERIFY_INVALID_DKIM_VERSION         10
83
84 /* -------------------------------------------------------------------------- */
85 /* Some parameter values */
86 #define PDKIM_QUERYMETHOD_DNS_TXT 0
87
88 #define PDKIM_CANON_SIMPLE        0
89 #define PDKIM_CANON_RELAXED       1
90
91 /* -------------------------------------------------------------------------- */
92 /* Some required forward declarations, please ignore */
93 typedef struct pdkim_stringlist pdkim_stringlist;
94 typedef struct pdkim_str pdkim_str;
95 typedef struct sha1_context sha1_context;
96 typedef struct sha2_context sha2_context;
97 #define HAVE_SHA1_CONTEXT
98 #define HAVE_SHA2_CONTEXT
99
100 /* -------------------------------------------------------------------------- */
101 /* Some concessions towards Redmond */
102 #ifdef WINDOWS
103 #define snprintf _snprintf
104 #define strcasecmp _stricmp
105 #define strncasecmp _strnicmp
106 #define DLLEXPORT __declspec(dllexport)
107 #else
108 #define DLLEXPORT
109 #endif
110
111
112 /* -------------------------------------------------------------------------- */
113 /* Public key as (usually) fetched from DNS */
114 typedef struct pdkim_pubkey {
115   const uschar * version;         /* v=  */
116   const uschar *granularity;      /* g=  */
117
118   const uschar * hashes;          /* h=  */
119   const uschar * keytype;         /* k=  */
120   const uschar * srvtype;         /* s=  */
121   uschar *notes;                  /* n=  */
122
123   blob  key;                      /* p=  */
124   int   testing;                  /* t=y */
125   int   no_subdomaining;          /* t=s */
126 } pdkim_pubkey;
127
128 /* -------------------------------------------------------------------------- */
129 /* Body-hash to be calculated */
130 typedef struct pdkim_bodyhash {
131   struct pdkim_bodyhash *       next;
132   int                           hashtype;
133   int                           canon_method;
134   long                          bodylength;
135
136   hctx                          body_hash_ctx;
137   unsigned long                 signed_body_bytes;      /* done so far */
138   int                           num_buffered_blanklines;
139
140   blob                          bh;                     /* completed hash */
141 } pdkim_bodyhash;
142
143 /* -------------------------------------------------------------------------- */
144 /* Signature as it appears in a DKIM-Signature header */
145 typedef struct pdkim_signature {
146   struct pdkim_signature * next;
147
148   /* Bits stored in a DKIM signature header --------------------------- */
149
150   /* (v=) The version, as an integer. Currently, always "1" */
151   int version;
152
153   /* (a=) The signature algorithm. */
154   int           keytype;        /* pdkim_keytypes index */
155   unsigned      keybits;        /* size of the key */
156   int           hashtype;       /* pdkim_hashes index */
157
158   /* (c=x/) Header canonicalization method. Either PDKIM_CANON_SIMPLE
159      or PDKIM_CANON_RELAXED */
160   int canon_headers;
161
162   /* (c=/x) Body canonicalization method. Either PDKIM_CANON_SIMPLE
163      or PDKIM_CANON_RELAXED */
164   int canon_body;
165
166   /* (q=) Query Method. Currently, only PDKIM_QUERYMETHOD_DNS_TXT
167      is specified */
168   int querymethod;
169
170   /* (s=) The selector string as given in the signature */
171   uschar *selector;
172
173   /* (d=) The domain as given in the signature */
174   uschar *domain;
175
176   /* (i=) The identity as given in the signature */
177   uschar *identity;
178
179   /* (t=) Timestamp of signature creation */
180   unsigned long created;
181
182   /* (x=) Timestamp of expiry of signature */
183   unsigned long expires;
184
185   /* (l=) Amount of hashed body bytes (after canonicalization). Default
186      is -1. Note: a value of 0 means that the body is unsigned! */
187   long bodylength;
188
189   /* (h=) Colon-separated list of header names that are included in the
190      signature */
191   uschar *headernames;
192
193   /* (z=) */
194   uschar *copiedheaders;
195
196   /* (b=) Raw signature data, along with its length in bytes */
197   blob sighash;
198
199   /* (bh=) Raw body hash data, along with its length in bytes */
200   blob bodyhash;
201
202   /* Folded DKIM-Signature: header. Signing only, NULL for verifying.
203      Ready for insertion into the message. Note: Folded using CRLFTB,
204      but final line terminator is NOT included. Note2: This buffer is
205      free()d when you call pdkim_free_ctx(). */
206   uschar *signature_header;
207
208   /* The main verification status. Verification only. One of:
209
210      PDKIM_VERIFY_NONE      Verification was not attempted. This status
211                             should not appear.
212
213      PDKIM_VERIFY_INVALID   There was an error while trying to verify
214                             the signature. A more precise description
215                             is available in verify_ext_status.
216
217      PDKIM_VERIFY_FAIL      Verification failed because either the body
218                             hash did not match, or the signature verification
219                             failed. This means the message was modified.
220                             Check verify_ext_status for the exact reason.
221
222      PDKIM_VERIFY_PASS      Verification succeeded.
223   */
224   int verify_status;
225
226   /* Extended verification status. Verification only. Depending on the value
227      of verify_status, it can contain:
228
229      For verify_status == PDKIM_VERIFY_INVALID:
230
231         PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE
232           Unable to retrieve a public key container.
233
234         PDKIM_VERIFY_INVALID_BUFFER_SIZE
235           Either the DNS name constructed to retrieve the public key record
236           does not fit into PDKIM_DNS_TXT_MAX_NAMELEN bytes, or the retrieved
237           record is longer than PDKIM_DNS_TXT_MAX_RECLEN bytes.
238
239         PDKIM_VERIFY_INVALID_PUBKEY_PARSING
240           (Syntax) error while parsing the retrieved public key record.
241
242
243      For verify_status == PDKIM_VERIFY_FAIL:
244
245         PDKIM_VERIFY_FAIL_BODY
246           The calculated body hash does not match the advertised body hash
247           from the bh= tag of the signature.
248
249         PDKIM_VERIFY_FAIL_MESSAGE
250           RSA verification of the signature (b= tag) failed.
251   */
252   int verify_ext_status;
253
254   /* Pointer to a public key record that was used to verify the signature.
255      See pdkim_pubkey declaration above for more information.
256      Caution: is NULL if signing or if no record was retrieved. */
257   pdkim_pubkey *pubkey;
258
259   /* Properties below this point are used internally only ------------- */
260
261   /* Per-signature helper variables ----------------------------------- */
262   pdkim_bodyhash *calc_body_hash;       /* hash to be / being calculated */
263
264   pdkim_stringlist *headers;            /* Raw headers included in the sig */
265
266   /* Signing specific ------------------------------------------------- */
267   uschar * privkey;          /* Private key                                 */
268   uschar * sign_headers;    /* To-be-signed header names                   */
269   uschar * rawsig_no_b_val; /* Original signature header w/o b= tag value. */
270 } pdkim_signature;
271
272
273 /* -------------------------------------------------------------------------- */
274 /* Context to keep state between all operations. */
275 typedef struct pdkim_ctx {
276
277 #define PDKIM_MODE_SIGN   BIT(0)        /* if unset, mode==verify */
278 #define PDKIM_DOT_TERM    BIT(1)        /* dot termination and unstuffing */
279 #define PDKIM_SEEN_CR     BIT(2)
280 #define PDKIM_SEEN_LF     BIT(3)
281 #define PDKIM_PAST_HDRS   BIT(4)
282 #define PDKIM_SEEN_EOD    BIT(5)
283   unsigned   flags;
284
285   /* One (signing) or several chained (verification) signatures */
286   pdkim_signature *sig;
287
288   /* One (signing) or several chained (verification) bodyhashes */
289   pdkim_bodyhash *bodyhash;
290
291   /* Callback for dns/txt query method (verification only) */
292   uschar * (*dns_txt_callback)(const uschar *);
293
294   /* Coder's little helpers */
295   gstring   *cur_header;
296   uschar    *linebuf;
297   int        linebuf_offset;
298   int        num_headers;
299   pdkim_stringlist *headers; /* Raw headers for verification         */
300 } pdkim_ctx;
301
302
303 /******************************************************************************/
304
305 typedef struct {
306   const uschar * dkim_hashname;
307   hashmethod     exim_hashmethod;
308 } pdkim_hashtype;
309 extern const pdkim_hashtype pdkim_hashes[];
310
311 /******************************************************************************/
312
313
314 /* -------------------------------------------------------------------------- */
315 /* API functions. Please see the sample code in sample/test_sign.c and
316    sample/test_verify.c for documentation.
317 */
318
319 #ifdef __cplusplus
320 extern "C" {
321 #endif
322
323 void       pdkim_init         (void);
324
325 void       pdkim_init_context (pdkim_ctx *, BOOL, uschar * (*)(const uschar *));
326
327 DLLEXPORT
328 pdkim_signature *pdkim_init_sign    (pdkim_ctx *,
329                                uschar *, uschar *, uschar *, uschar *,
330                                const uschar **);
331
332 DLLEXPORT
333 pdkim_ctx *pdkim_init_verify  (uschar * (*)(const uschar *), BOOL);
334
335 DLLEXPORT
336 void       pdkim_set_optional (pdkim_signature *, char *, char *,int, int,
337                                long,
338                                unsigned long,
339                                unsigned long);
340
341 int             pdkim_hashname_to_hashtype(const uschar *, unsigned);
342 void            pdkim_cstring_to_canons(const uschar *, unsigned, int *, int *);
343 pdkim_bodyhash *pdkim_set_bodyhash(pdkim_ctx *, int, int, long);
344 pdkim_bodyhash *pdkim_set_sig_bodyhash(pdkim_ctx *, pdkim_signature *);
345
346 DLLEXPORT
347 int        pdkim_feed         (pdkim_ctx *, uschar *, int);
348 DLLEXPORT
349 int        pdkim_feed_finish  (pdkim_ctx *, pdkim_signature **, const uschar **);
350
351 DLLEXPORT
352 void       pdkim_free_ctx     (pdkim_ctx *);
353
354
355 const uschar *  pdkim_errstr(int);
356
357 extern uschar *         pdkim_encode_base64(blob *);
358 extern void             pdkim_decode_base64(const uschar *, blob *);
359 extern void             pdkim_hexprint(const uschar *, int);
360 extern void             pdkim_quoteprint(const uschar *, int);
361 extern pdkim_pubkey *   pdkim_parse_pubkey_record(const uschar *);
362 extern uschar *         pdkim_relax_header_n(const uschar *, int, BOOL);
363 extern uschar *         pdkim_relax_header(const uschar *, BOOL);
364 extern uschar *         dkim_sig_to_a_tag(const pdkim_signature *);
365
366 #ifdef __cplusplus
367 }
368 #endif
369
370 #endif