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