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