2 * PDKIM - a RFC4871 (DKIM) implementation
4 * Copyright (C) 2009 - 2012 Tom Kistner <tom@duncanthrax.net>
6 * http://duncanthrax.net/pdkim/
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.
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.
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.
23 /* -------------------------------------------------------------------------- */
24 /* Debugging. This can also be enabled/disabled at run-time. I recommend to
28 /* -------------------------------------------------------------------------- */
29 /* Length of the preallocated buffer for the "answer" from the dns/txt
30 callback function. This should match the maximum RDLENGTH from DNS. */
31 #define PDKIM_DNS_TXT_MAX_RECLEN (1 << 16)
33 /* -------------------------------------------------------------------------- */
34 /* Function success / error codes */
37 #define PDKIM_ERR_OOM -100
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
43 /* -------------------------------------------------------------------------- */
44 /* Main/Extended verification status */
45 #define PDKIM_VERIFY_NONE 0
46 #define PDKIM_VERIFY_INVALID 1
47 #define PDKIM_VERIFY_FAIL 2
48 #define PDKIM_VERIFY_PASS 3
50 #define PDKIM_VERIFY_FAIL_BODY 1
51 #define PDKIM_VERIFY_FAIL_MESSAGE 2
52 #define PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE 3
53 #define PDKIM_VERIFY_INVALID_BUFFER_SIZE 4
54 #define PDKIM_VERIFY_INVALID_PUBKEY_PARSING 5
56 /* -------------------------------------------------------------------------- */
57 /* Some parameter values */
58 #define PDKIM_QUERYMETHOD_DNS_TXT 0
60 #define PDKIM_ALGO_RSA_SHA256 0
61 #define PDKIM_ALGO_RSA_SHA1 1
63 #define PDKIM_CANON_SIMPLE 0
64 #define PDKIM_CANON_RELAXED 1
66 #define PDKIM_HASH_SHA256 0
67 #define PDKIM_HASH_SHA1 1
69 #define PDKIM_KEYTYPE_RSA 0
71 /* -------------------------------------------------------------------------- */
72 /* Some required forward declarations, please ignore */
73 typedef struct pdkim_stringlist pdkim_stringlist
;
74 typedef struct pdkim_str pdkim_str
;
75 typedef struct sha1_context sha1_context
;
76 typedef struct sha2_context sha2_context
;
77 #define HAVE_SHA1_CONTEXT
78 #define HAVE_SHA2_CONTEXT
80 /* -------------------------------------------------------------------------- */
81 /* Some concessions towards Redmond */
83 #define snprintf _snprintf
84 #define strcasecmp _stricmp
85 #define strncasecmp _strnicmp
86 #define DLLEXPORT __declspec(dllexport)
92 /* -------------------------------------------------------------------------- */
93 /* Public key as (usually) fetched from DNS */
94 typedef struct pdkim_pubkey
{
95 char *version
; /* v= */
96 char *granularity
; /* g= */
98 char *hashes
; /* h= */
99 char *keytype
; /* k= */
100 char *srvtype
; /* s= */
101 char *notes
; /* n= */
106 int testing
; /* t=y */
107 int no_subdomaining
; /* t=s */
110 /* -------------------------------------------------------------------------- */
111 /* Signature as it appears in a DKIM-Signature header */
112 typedef struct pdkim_signature
{
114 /* Bits stored in a DKIM signature header --------------------------- */
116 /* (v=) The version, as an integer. Currently, always "1" */
119 /* (a=) The signature algorithm. Either PDKIM_ALGO_RSA_SHA256
120 or PDKIM_ALGO_RSA_SHA1 */
123 /* (c=x/) Header canonicalization method. Either PDKIM_CANON_SIMPLE
124 or PDKIM_CANON_RELAXED */
127 /* (c=/x) Body canonicalization method. Either PDKIM_CANON_SIMPLE
128 or PDKIM_CANON_RELAXED */
131 /* (q=) Query Method. Currently, only PDKIM_QUERYMETHOD_DNS_TXT
135 /* (s=) The selector string as given in the signature */
138 /* (d=) The domain as given in the signature */
141 /* (i=) The identity as given in the signature */
144 /* (t=) Timestamp of signature creation */
145 unsigned long created
;
147 /* (x=) Timestamp of expiry of signature */
148 unsigned long expires
;
150 /* (l=) Amount of hashed body bytes (after canonicalization). Default
151 is -1. Note: a value of 0 means that the body is unsigned! */
154 /* (h=) Colon-separated list of header names that are included in the
161 /* (b=) Raw signature data, along with its length in bytes */
165 /* (bh=) Raw body hash data, along with its length in bytes */
169 /* Folded DKIM-Signature: header. Singing only, NULL for verifying.
170 Ready for insertion into the message. Note: Folded using CRLFTB,
171 but final line terminator is NOT included. Note2: This buffer is
172 free()d when you call pdkim_free_ctx(). */
173 char *signature_header
;
175 /* The main verification status. Verification only. One of:
177 PDKIM_VERIFY_NONE Verification was not attempted. This status
180 PDKIM_VERIFY_INVALID There was an error while trying to verify
181 the signature. A more precise description
182 is available in verify_ext_status.
184 PDKIM_VERIFY_FAIL Verification failed because either the body
185 hash did not match, or the signature verification
186 failed. This means the message was modified.
187 Check verify_ext_status for the exact reason.
189 PDKIM_VERIFY_PASS Verification succeeded.
193 /* Extended verification status. Verification only. Depending on the value
194 of verify_status, it can contain:
196 For verify_status == PDKIM_VERIFY_INVALID:
198 PDKIM_VERIFY_INVALID_PUBKEY_UNAVAILABLE
199 Unable to retrieve a public key container.
201 PDKIM_VERIFY_INVALID_BUFFER_SIZE
202 Either the DNS name constructed to retrieve the public key record
203 does not fit into PDKIM_DNS_TXT_MAX_NAMELEN bytes, or the retrieved
204 record is longer than PDKIM_DNS_TXT_MAX_RECLEN bytes.
206 PDKIM_VERIFY_INVALID_PUBKEY_PARSING
207 (Syntax) error while parsing the retrieved public key record.
210 For verify_status == PDKIM_VERIFY_FAIL:
212 PDKIM_VERIFY_FAIL_BODY
213 The calculated body hash does not match the advertised body hash
214 from the bh= tag of the signature.
216 PDKIM_VERIFY_FAIL_MESSAGE
217 RSA verification of the signature (b= tag) failed.
219 int verify_ext_status
;
221 /* Pointer to a public key record that was used to verify the signature.
222 See pdkim_pubkey declaration above for more information.
223 Caution: is NULL if signing or if no record was retrieved. */
224 pdkim_pubkey
*pubkey
;
226 /* Pointer to the next pdkim_signature signature. NULL if signing or if
227 this is the last signature. */
230 /* Properties below this point are used internally only ------------- */
232 /* Per-signature helper variables ----------------------------------- */
233 sha1_context
*sha1_body
; /* SHA1 block */
234 sha2_context
*sha2_body
; /* SHA256 block */
235 unsigned long signed_body_bytes
; /* How many body bytes we hashed */
236 pdkim_stringlist
*headers
; /* Raw headers included in the sig */
237 /* Signing specific ------------------------------------------------- */
238 char *rsa_privkey
; /* Private RSA key */
239 char *sign_headers
; /* To-be-signed header names */
240 char *rawsig_no_b_val
; /* Original signature header w/o b= tag value. */
244 /* -------------------------------------------------------------------------- */
245 /* Context to keep state between all operations. */
246 #define PDKIM_MODE_SIGN 0
247 #define PDKIM_MODE_VERIFY 1
248 #define PDKIM_INPUT_NORMAL 0
249 #define PDKIM_INPUT_SMTP 1
250 typedef struct pdkim_ctx
{
252 /* PDKIM_MODE_VERIFY or PDKIM_MODE_SIGN */
255 /* PDKIM_INPUT_SMTP or PDKIM_INPUT_NORMAL */
258 /* One (signing) or several chained (verification) signatures */
259 pdkim_signature
*sig
;
261 /* Callback for dns/txt query method (verification only) */
262 int(*dns_txt_callback
)(char *, char *);
264 /* Coder's little helpers */
265 pdkim_str
*cur_header
;
271 int num_buffered_crlf
;
273 pdkim_stringlist
*headers
; /* Raw headers for verification */
276 /* A FILE pointer. When not NULL, debug output will be generated
277 and sent to this stream */
284 /* -------------------------------------------------------------------------- */
285 /* API functions. Please see the sample code in sample/test_sign.c and
286 sample/test_verify.c for documentation.
294 pdkim_ctx
*pdkim_init_sign (int, char *, char *, char *);
297 pdkim_ctx
*pdkim_init_verify (int, int(*)(char *, char *));
300 int pdkim_set_optional (pdkim_ctx
*, char *, char *,int, int,
306 int pdkim_feed (pdkim_ctx
*, char *, int);
308 int pdkim_feed_finish (pdkim_ctx
*, pdkim_signature
**);
311 void pdkim_free_ctx (pdkim_ctx
*);
315 void pdkim_set_debug_stream(pdkim_ctx
*, FILE *);