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