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