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