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