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