ARC initial implementation. Experimental. Bug 2162
[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 - 2018  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 #define PDKIM_VERIFY_POLICY    BIT(31)
61
62 #define PDKIM_VERIFY_FAIL_BODY                    1
63 #define PDKIM_VERIFY_FAIL_MESSAGE                 2
64 #define PDKIM_VERIFY_FAIL_SIG_ALGO_MISMATCH       3
65 #define PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE   4
66 #define PDKIM_VERIFY_INVALID_BUFFER_SIZE          5
67 #define PDKIM_VERIFY_INVALID_PUBKEY_DNSRECORD     6
68 #define PDKIM_VERIFY_INVALID_PUBKEY_IMPORT        7
69 #define PDKIM_VERIFY_INVALID_SIGNATURE_ERROR      8
70 #define PDKIM_VERIFY_INVALID_DKIM_VERSION         9
71
72 /* -------------------------------------------------------------------------- */
73 /* Some parameter values */
74 #define PDKIM_QUERYMETHOD_DNS_TXT 0
75
76 /*#define PDKIM_ALGO_RSA_SHA256     0 */
77 /*#define PDKIM_ALGO_RSA_SHA1       1 */
78
79 #define PDKIM_CANON_SIMPLE        0
80 #define PDKIM_CANON_RELAXED       1
81
82 /* -------------------------------------------------------------------------- */
83 /* Some required forward declarations, please ignore */
84 typedef struct pdkim_stringlist pdkim_stringlist;
85 typedef struct pdkim_str pdkim_str;
86 typedef struct sha1_context sha1_context;
87 typedef struct sha2_context sha2_context;
88 #define HAVE_SHA1_CONTEXT
89 #define HAVE_SHA2_CONTEXT
90
91 /* -------------------------------------------------------------------------- */
92 /* Some concessions towards Redmond */
93 #ifdef WINDOWS
94 #define snprintf _snprintf
95 #define strcasecmp _stricmp
96 #define strncasecmp _strnicmp
97 #define DLLEXPORT __declspec(dllexport)
98 #else
99 #define DLLEXPORT
100 #endif
101
102
103 /* -------------------------------------------------------------------------- */
104 /* Public key as (usually) fetched from DNS */
105 typedef struct pdkim_pubkey {
106   const uschar * version;         /* v=  */
107   const uschar *granularity;      /* g=  */
108
109   const uschar * hashes;          /* h=  */
110   const uschar * keytype;         /* k=  */
111   const uschar * srvtype;         /* s=  */
112   uschar *notes;                  /* n=  */
113
114   blob  key;                      /* p=  */
115   int   testing;                  /* t=y */
116   int   no_subdomaining;          /* t=s */
117 } pdkim_pubkey;
118
119 /* -------------------------------------------------------------------------- */
120 /* Body-hash to be calculated */
121 typedef struct pdkim_bodyhash {
122   struct pdkim_bodyhash *       next;
123   int                           hashtype;
124   int                           canon_method;
125   long                          bodylength;
126
127   hctx                          body_hash_ctx;
128   unsigned long                 signed_body_bytes;      /* done so far */
129   int                           num_buffered_blanklines;
130
131   blob                          bh;                     /* completed hash */
132 } pdkim_bodyhash;
133
134 /* -------------------------------------------------------------------------- */
135 /* Signature as it appears in a DKIM-Signature header */
136 typedef struct pdkim_signature {
137   struct pdkim_signature * next;
138
139   /* Bits stored in a DKIM signature header --------------------------- */
140
141   /* (v=) The version, as an integer. Currently, always "1" */
142   int version;
143
144   /* (a=) The signature algorithm. Either PDKIM_ALGO_RSA_SHA256 */
145   int keytype;  /* pdkim_keytypes index */
146   int hashtype; /* pdkim_hashes index */
147
148   /* (c=x/) Header canonicalization method. Either PDKIM_CANON_SIMPLE
149      or PDKIM_CANON_RELAXED */
150   int canon_headers;
151
152   /* (c=/x) Body canonicalization method. Either PDKIM_CANON_SIMPLE
153      or PDKIM_CANON_RELAXED */
154   int canon_body;
155
156   /* (q=) Query Method. Currently, only PDKIM_QUERYMETHOD_DNS_TXT
157      is specified */
158   int querymethod;
159
160   /* (s=) The selector string as given in the signature */
161   uschar *selector;
162
163   /* (d=) The domain as given in the signature */
164   uschar *domain;
165
166   /* (i=) The identity as given in the signature */
167   uschar *identity;
168
169   /* (t=) Timestamp of signature creation */
170   unsigned long created;
171
172   /* (x=) Timestamp of expiry of signature */
173   unsigned long expires;
174
175   /* (l=) Amount of hashed body bytes (after canonicalization). Default
176      is -1. Note: a value of 0 means that the body is unsigned! */
177   long bodylength;
178
179   /* (h=) Colon-separated list of header names that are included in the
180      signature */
181   uschar *headernames;
182
183   /* (z=) */
184   uschar *copiedheaders;
185
186   /* (b=) Raw signature data, along with its length in bytes */
187   blob sighash;
188
189   /* (bh=) Raw body hash data, along with its length in bytes */
190   blob bodyhash;
191
192   /* Folded DKIM-Signature: header. Signing only, NULL for verifying.
193      Ready for insertion into the message. Note: Folded using CRLFTB,
194      but final line terminator is NOT included. Note2: This buffer is
195      free()d when you call pdkim_free_ctx(). */
196   uschar *signature_header;
197
198   /* The main verification status. Verification only. One of:
199
200      PDKIM_VERIFY_NONE      Verification was not attempted. This status
201                             should not appear.
202
203      PDKIM_VERIFY_INVALID   There was an error while trying to verify
204                             the signature. A more precise description
205                             is available in verify_ext_status.
206
207      PDKIM_VERIFY_FAIL      Verification failed because either the body
208                             hash did not match, or the signature verification
209                             failed. This means the message was modified.
210                             Check verify_ext_status for the exact reason.
211
212      PDKIM_VERIFY_PASS      Verification succeeded.
213   */
214   int verify_status;
215
216   /* Extended verification status. Verification only. Depending on the value
217      of verify_status, it can contain:
218
219      For verify_status == PDKIM_VERIFY_INVALID:
220
221         PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE
222           Unable to retrieve a public key container.
223
224         PDKIM_VERIFY_INVALID_BUFFER_SIZE
225           Either the DNS name constructed to retrieve the public key record
226           does not fit into PDKIM_DNS_TXT_MAX_NAMELEN bytes, or the retrieved
227           record is longer than PDKIM_DNS_TXT_MAX_RECLEN bytes.
228
229         PDKIM_VERIFY_INVALID_PUBKEY_PARSING
230           (Syntax) error while parsing the retrieved public key record.
231
232
233      For verify_status == PDKIM_VERIFY_FAIL:
234
235         PDKIM_VERIFY_FAIL_BODY
236           The calculated body hash does not match the advertised body hash
237           from the bh= tag of the signature.
238
239         PDKIM_VERIFY_FAIL_MESSAGE
240           RSA verification of the signature (b= tag) failed.
241   */
242   int verify_ext_status;
243
244   /* Pointer to a public key record that was used to verify the signature.
245      See pdkim_pubkey declaration above for more information.
246      Caution: is NULL if signing or if no record was retrieved. */
247   pdkim_pubkey *pubkey;
248
249   /* Properties below this point are used internally only ------------- */
250
251   /* Per-signature helper variables ----------------------------------- */
252   pdkim_bodyhash *calc_body_hash;       /* hash to be / being calculated */
253
254   pdkim_stringlist *headers;            /* Raw headers included in the sig */
255
256   /* Signing specific ------------------------------------------------- */
257   uschar * privkey;          /* Private key                                 */
258   uschar * sign_headers;    /* To-be-signed header names                   */
259   uschar * rawsig_no_b_val; /* Original signature header w/o b= tag value. */
260 } pdkim_signature;
261
262
263 /* -------------------------------------------------------------------------- */
264 /* Context to keep state between all operations. */
265 typedef struct pdkim_ctx {
266
267 #define PDKIM_MODE_SIGN   BIT(0)        /* if unset, mode==verify */
268 #define PDKIM_DOT_TERM    BIT(1)        /* dot termination and unstuffing */
269 #define PDKIM_SEEN_CR     BIT(2)
270 #define PDKIM_SEEN_LF     BIT(3)
271 #define PDKIM_PAST_HDRS   BIT(4)
272 #define PDKIM_SEEN_EOD    BIT(5)
273   unsigned   flags;
274
275   /* One (signing) or several chained (verification) signatures */
276   pdkim_signature *sig;
277
278   /* One (signing) or several chained (verification) bodyhashes */
279   pdkim_bodyhash *bodyhash;
280
281   /* Callback for dns/txt query method (verification only) */
282   uschar * (*dns_txt_callback)(uschar *);
283
284   /* Coder's little helpers */
285   gstring   *cur_header;
286   uschar    *linebuf;
287   int        linebuf_offset;
288   int        num_headers;
289   pdkim_stringlist *headers; /* Raw headers for verification         */
290 } pdkim_ctx;
291
292
293 /******************************************************************************/
294
295 typedef struct {
296   const uschar * dkim_hashname;
297   hashmethod     exim_hashmethod;
298 } pdkim_hashtype;
299 extern const pdkim_hashtype pdkim_hashes[];
300
301 /******************************************************************************/
302
303
304 /* -------------------------------------------------------------------------- */
305 /* API functions. Please see the sample code in sample/test_sign.c and
306    sample/test_verify.c for documentation.
307 */
308
309 #ifdef __cplusplus
310 extern "C" {
311 #endif
312
313 void       pdkim_init         (void);
314
315 void       pdkim_init_context (pdkim_ctx *, BOOL, uschar * (*)(uschar *));
316
317 DLLEXPORT
318 pdkim_signature *pdkim_init_sign    (pdkim_ctx *,
319                                uschar *, uschar *, uschar *, uschar *,
320                                const uschar **);
321
322 DLLEXPORT
323 pdkim_ctx *pdkim_init_verify  (uschar * (*)(uschar *), BOOL);
324
325 DLLEXPORT
326 void       pdkim_set_optional (pdkim_signature *, char *, char *,int, int,
327                                long,
328                                unsigned long,
329                                unsigned long);
330
331 int             pdkim_hashname_to_hashtype(const uschar *, unsigned);
332 void            pdkim_cstring_to_canons(const uschar *, unsigned, int *, int *);
333 pdkim_bodyhash *pdkim_set_bodyhash(pdkim_ctx *, int, int, long);
334 pdkim_bodyhash *pdkim_set_sig_bodyhash(pdkim_ctx *, pdkim_signature *);
335
336 DLLEXPORT
337 int        pdkim_feed         (pdkim_ctx *, uschar *, int);
338 DLLEXPORT
339 int        pdkim_feed_finish  (pdkim_ctx *, pdkim_signature **, const uschar **);
340
341 DLLEXPORT
342 void       pdkim_free_ctx     (pdkim_ctx *);
343
344
345 const uschar *  pdkim_errstr(int);
346
347 extern uschar *         pdkim_encode_base64(blob *);
348 extern void             pdkim_decode_base64(const uschar *, blob *);
349 extern void             pdkim_hexprint(const uschar *, int);
350 extern void             pdkim_quoteprint(const uschar *, int);
351 extern pdkim_pubkey *   pdkim_parse_pubkey_record(const uschar *);
352 extern uschar *         pdkim_relax_header_n(const uschar *, int, BOOL);
353 extern uschar *         pdkim_relax_header(const uschar *, BOOL);
354 extern uschar *         dkim_sig_to_a_tag(const pdkim_signature *);
355
356 #ifdef __cplusplus
357 }
358 #endif
359
360 #endif