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