Commit | Line | Data |
---|---|---|
80a47a2c TK |
1 | /* |
2 | * PDKIM - a RFC4871 (DKIM) implementation | |
3 | * | |
4 | * Copyright (C) 2009 Tom Kistner <tom@duncanthrax.net> | |
5 | * | |
6 | * http://duncanthrax.net/pdkim/ | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
22 | ||
23 | /* $Cambridge: exim/src/src/pdkim/pdkim.h,v 1.2 2009/06/10 07:34:05 tom Exp $ */ | |
24 | ||
25 | /* -------------------------------------------------------------------------- */ | |
26 | /* Debugging. This can also be enabled/disabled at run-time. I recommend to | |
27 | leave it defined. */ | |
28 | #define PDKIM_DEBUG | |
29 | ||
30 | /* -------------------------------------------------------------------------- */ | |
31 | /* Length of the preallocated buffer for the "answer" from the dns/txt | |
32 | callback function. */ | |
33 | #define PDKIM_DNS_TXT_MAX_RECLEN 4096 | |
34 | ||
35 | /* -------------------------------------------------------------------------- */ | |
36 | /* Function success / error codes */ | |
37 | #define PDKIM_OK 0 | |
38 | #define PDKIM_FAIL -1 | |
39 | #define PDKIM_ERR_OOM -100 | |
40 | #define PDKIM_ERR_RSA_PRIVKEY -101 | |
41 | #define PDKIM_ERR_RSA_SIGNING -102 | |
42 | #define PDKIM_ERR_LONG_LINE -103 | |
43 | #define PDKIM_ERR_BUFFER_TOO_SMALL -104 | |
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_PARSING 5 | |
57 | ||
58 | /* -------------------------------------------------------------------------- */ | |
59 | /* Some parameter values */ | |
60 | #define PDKIM_QUERYMETHOD_DNS_TXT 0 | |
61 | ||
62 | #define PDKIM_ALGO_RSA_SHA256 0 | |
63 | #define PDKIM_ALGO_RSA_SHA1 1 | |
64 | ||
65 | #define PDKIM_CANON_SIMPLE 0 | |
66 | #define PDKIM_CANON_RELAXED 1 | |
67 | ||
68 | #define PDKIM_HASH_SHA256 0 | |
69 | #define PDKIM_HASH_SHA1 1 | |
70 | ||
71 | #define PDKIM_KEYTYPE_RSA 0 | |
72 | ||
73 | /* -------------------------------------------------------------------------- */ | |
74 | /* Some required forward declarations, please ignore */ | |
75 | typedef struct pdkim_stringlist pdkim_stringlist; | |
76 | typedef struct pdkim_str pdkim_str; | |
77 | typedef struct sha1_context sha1_context; | |
78 | typedef struct sha2_context sha2_context; | |
79 | #define HAVE_SHA1_CONTEXT | |
80 | #define HAVE_SHA2_CONTEXT | |
81 | ||
82 | /* -------------------------------------------------------------------------- */ | |
83 | /* Some concessions towards Redmond */ | |
84 | #ifdef WINDOWS | |
85 | #define snprintf _snprintf | |
86 | #define strcasecmp _stricmp | |
87 | #define strncasecmp _strnicmp | |
88 | #define DLLEXPORT __declspec(dllexport) | |
89 | #else | |
90 | #define DLLEXPORT | |
91 | #endif | |
92 | ||
93 | ||
94 | /* -------------------------------------------------------------------------- */ | |
95 | /* Public key as (usually) fetched from DNS */ | |
96 | typedef struct pdkim_pubkey { | |
97 | char *version; /* v= */ | |
98 | char *granularity; /* g= */ | |
99 | ||
100 | char *hashes; /* h= */ | |
101 | char *keytype; /* k= */ | |
102 | char *srvtype; /* s= */ | |
103 | char *notes; /* n= */ | |
104 | ||
105 | char *key; /* p= */ | |
106 | int key_len; | |
107 | ||
108 | int testing; /* t=y */ | |
109 | int no_subdomaining; /* t=s */ | |
110 | } pdkim_pubkey; | |
111 | ||
112 | /* -------------------------------------------------------------------------- */ | |
113 | /* Signature as it appears in a DKIM-Signature header */ | |
114 | typedef struct pdkim_signature { | |
115 | ||
116 | /* Bits stored in a DKIM signature header --------------------------- */ | |
117 | ||
118 | /* (v=) The version, as an integer. Currently, always "1" */ | |
119 | int version; | |
120 | ||
121 | /* (a=) The signature algorithm. Either PDKIM_ALGO_RSA_SHA256 | |
122 | or PDKIM_ALGO_RSA_SHA1 */ | |
123 | int algo; | |
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 | char *selector; | |
139 | ||
140 | /* (d=) The domain as given in the signature */ | |
141 | char *domain; | |
142 | ||
143 | /* (i=) The identity as given in the signature */ | |
144 | char *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 | char *headernames; | |
159 | ||
160 | /* (z=) */ | |
161 | char *copiedheaders; | |
162 | ||
163 | /* (b=) Raw signature data, along with its length in bytes */ | |
164 | char *sigdata; | |
165 | int sigdata_len; | |
166 | ||
167 | /* (bh=) Raw body hash data, along with its length in bytes */ | |
168 | char *bodyhash; | |
169 | int bodyhash_len; | |
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 | char *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 | sha1_context *sha1_body; /* SHA1 block */ | |
236 | sha2_context *sha2_body; /* SHA256 block */ | |
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 | char *rsa_privkey; /* Private RSA key */ | |
241 | char *sign_headers; /* To-be-signed header names */ | |
242 | /* Verification specific -------------------------------------------- */ | |
243 | char *hnames_check; /* Tick-off header list that we use to keep | |
244 | track of header names that we have already | |
245 | added to the signature candidates. */ | |
246 | char *rawsig_no_b_val; /* Original signature header w/o b= tag value. */ | |
247 | } pdkim_signature; | |
248 | ||
249 | ||
250 | /* -------------------------------------------------------------------------- */ | |
251 | /* Context to keep state between all operations. */ | |
252 | #define PDKIM_MODE_SIGN 0 | |
253 | #define PDKIM_MODE_VERIFY 1 | |
254 | #define PDKIM_INPUT_NORMAL 0 | |
255 | #define PDKIM_INPUT_SMTP 1 | |
256 | typedef struct pdkim_ctx { | |
257 | ||
258 | /* PDKIM_MODE_VERIFY or PDKIM_MODE_SIGN */ | |
259 | int mode; | |
260 | ||
261 | /* PDKIM_INPUT_SMTP or PDKIM_INPUT_NORMAL */ | |
262 | int input_mode; | |
263 | ||
264 | /* One (signing) or several chained (verification) signatures */ | |
265 | pdkim_signature *sig; | |
266 | ||
267 | /* Callback for dns/txt query method (verification only) */ | |
268 | int(*dns_txt_callback)(char *, char *); | |
269 | ||
270 | /* Coder's little helpers */ | |
271 | pdkim_str *cur_header; | |
272 | char *linebuf; | |
273 | int linebuf_offset; | |
274 | int seen_lf; | |
275 | int seen_eod; | |
276 | int past_headers; | |
277 | int num_buffered_crlf; | |
278 | int num_headers; | |
279 | ||
280 | #ifdef PDKIM_DEBUG | |
281 | /* A FILE pointer. When not NULL, debug output will be generated | |
282 | and sent to this stream */ | |
283 | FILE *debug_stream; | |
284 | #endif | |
285 | ||
286 | } pdkim_ctx; | |
287 | ||
288 | ||
289 | /* -------------------------------------------------------------------------- */ | |
290 | /* API functions. Please see the sample code in sample/test_sign.c and | |
291 | sample/test_verify.c for documentation. | |
292 | */ | |
293 | ||
294 | #ifdef __cplusplus | |
295 | extern "C" { | |
296 | #endif | |
297 | ||
298 | DLLEXPORT | |
299 | pdkim_ctx *pdkim_init_sign (int, char *, char *, char *); | |
300 | ||
301 | DLLEXPORT | |
302 | pdkim_ctx *pdkim_init_verify (int, int(*)(char *, char *)); | |
303 | ||
304 | DLLEXPORT | |
305 | int pdkim_set_optional (pdkim_ctx *, char *, char *,int, int, | |
306 | long, int, | |
307 | unsigned long, | |
308 | unsigned long); | |
309 | ||
310 | DLLEXPORT | |
311 | int pdkim_feed (pdkim_ctx *, char *, int); | |
312 | DLLEXPORT | |
313 | int pdkim_feed_finish (pdkim_ctx *, pdkim_signature **); | |
314 | ||
315 | DLLEXPORT | |
316 | void pdkim_free_ctx (pdkim_ctx *); | |
317 | ||
318 | #ifdef PDKIM_DEBUG | |
319 | DLLEXPORT | |
320 | void pdkim_set_debug_stream(pdkim_ctx *, FILE *); | |
321 | #endif | |
322 | ||
323 | #ifdef __cplusplus | |
324 | } | |
325 | #endif |