Commit | Line | Data |
---|---|---|
2592e6c0 JH |
1 | /* |
2 | * PDKIM - a RFC4871 (DKIM) implementation | |
3 | * | |
4 | * Copyright (C) 2016 Exim maintainers | |
5 | * | |
d73e45df | 6 | * signing/verification interface |
2592e6c0 JH |
7 | */ |
8 | ||
9 | #include "../exim.h" | |
10 | ||
11 | #ifndef DISABLE_DKIM /* entire file */ | |
12 | ||
13 | #ifndef SUPPORT_TLS | |
14 | # error Need SUPPORT_TLS for DKIM | |
15 | #endif | |
16 | ||
17 | #include "crypt_ver.h" | |
9b2583c4 | 18 | #include "signing.h" |
2592e6c0 JH |
19 | |
20 | ||
21 | /******************************************************************************/ | |
d73e45df | 22 | #ifdef SIGN_GNUTLS |
2592e6c0 JH |
23 | |
24 | void | |
9b2583c4 | 25 | exim_dkim_init(void) |
2592e6c0 JH |
26 | { |
27 | } | |
28 | ||
29 | ||
30 | /* accumulate data (gnutls-only). String to be appended must be nul-terminated. */ | |
31 | blob * | |
9b2583c4 | 32 | exim_dkim_data_append(blob * b, int * alloc, uschar * s) |
2592e6c0 JH |
33 | { |
34 | int len = b->len; | |
35 | b->data = string_append(b->data, alloc, &len, 1, s); | |
36 | b->len = len; | |
37 | return b; | |
38 | } | |
39 | ||
40 | ||
41 | ||
42 | /* import private key from PEM string in memory. | |
43 | Return: NULL for success, or an error string */ | |
44 | ||
45 | const uschar * | |
9b2583c4 | 46 | exim_dkim_signing_init(uschar * privkey_pem, es_ctx * sign_ctx) |
2592e6c0 JH |
47 | { |
48 | gnutls_datum_t k; | |
49 | int rc; | |
50 | ||
51 | k.data = privkey_pem; | |
52 | k.size = strlen(privkey_pem); | |
53 | ||
d73e45df JH |
54 | if ( (rc = gnutls_x509_privkey_init(&sign_ctx->key)) != GNUTLS_E_SUCCESS |
55 | || (rc = gnutls_x509_privkey_import(sign_ctx->key, &k, | |
2592e6c0 JH |
56 | GNUTLS_X509_FMT_PEM)) != GNUTLS_E_SUCCESS |
57 | ) | |
58 | return gnutls_strerror(rc); | |
59 | ||
60 | return NULL; | |
61 | } | |
62 | ||
63 | ||
64 | ||
65 | /* allocate mem for signature (when signing) */ | |
66 | /* sign data (gnutls_only) | |
67 | OR | |
68 | sign hash. | |
69 | ||
70 | Return: NULL for success, or an error string */ | |
71 | ||
72 | const uschar * | |
d73e45df | 73 | exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig) |
2592e6c0 | 74 | { |
d73e45df | 75 | gnutls_digest_algorithm_t dig; |
2592e6c0 JH |
76 | gnutls_datum_t k; |
77 | size_t sigsize = 0; | |
78 | int rc; | |
79 | const uschar * ret = NULL; | |
80 | ||
d73e45df JH |
81 | switch (hash) |
82 | { | |
83 | case HASH_SHA1: dig = GNUTLS_DIG_SHA1; break; | |
84 | case HASH_SHA2_256: dig = GNUTLS_DIG_SHA256; break; | |
85 | case HASH_SHA2_512: dig = GNUTLS_DIG_SHA512; break; | |
86 | default: return US"nonhandled hash type"; | |
87 | } | |
88 | ||
2592e6c0 JH |
89 | /* Allocate mem for signature */ |
90 | k.data = data->data; | |
91 | k.size = data->len; | |
d73e45df | 92 | (void) gnutls_x509_privkey_sign_data(sign_ctx->key, dig, |
2592e6c0 JH |
93 | 0, &k, NULL, &sigsize); |
94 | ||
95 | sig->data = store_get(sigsize); | |
96 | sig->len = sigsize; | |
97 | ||
98 | /* Do signing */ | |
d73e45df | 99 | if ((rc = gnutls_x509_privkey_sign_data(sign_ctx->key, dig, |
2592e6c0 JH |
100 | 0, &k, sig->data, &sigsize)) != GNUTLS_E_SUCCESS |
101 | ) | |
102 | ret = gnutls_strerror(rc); | |
103 | ||
d73e45df | 104 | gnutls_x509_privkey_deinit(sign_ctx->key); |
2592e6c0 JH |
105 | return ret; |
106 | } | |
107 | ||
108 | ||
109 | ||
110 | /* import public key (from DER in memory) | |
111 | Return: NULL for success, or an error string */ | |
112 | ||
113 | const uschar * | |
9b2583c4 | 114 | exim_dkim_verify_init(blob * pubkey_der, ev_ctx * verify_ctx) |
2592e6c0 JH |
115 | { |
116 | gnutls_datum_t k; | |
117 | int rc; | |
118 | const uschar * ret = NULL; | |
119 | ||
d73e45df | 120 | gnutls_pubkey_init(&verify_ctx->key); |
2592e6c0 JH |
121 | |
122 | k.data = pubkey_der->data; | |
123 | k.size = pubkey_der->len; | |
124 | ||
d73e45df | 125 | if ((rc = gnutls_pubkey_import(verify_ctx->key, &k, GNUTLS_X509_FMT_DER)) |
2592e6c0 JH |
126 | != GNUTLS_E_SUCCESS) |
127 | ret = gnutls_strerror(rc); | |
128 | return ret; | |
129 | } | |
130 | ||
131 | ||
132 | /* verify signature (of hash) (given pubkey & alleged sig) | |
133 | Return: NULL for success, or an error string */ | |
134 | ||
135 | const uschar * | |
d73e45df | 136 | exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data_hash, blob * sig) |
2592e6c0 | 137 | { |
d73e45df | 138 | gnutls_sign_algorithm_t algo; |
2592e6c0 JH |
139 | gnutls_datum_t k, s; |
140 | int rc; | |
141 | const uschar * ret = NULL; | |
142 | ||
d73e45df JH |
143 | /*XXX needs extension for non-rsa */ |
144 | switch (hash) | |
145 | { | |
146 | case HASH_SHA1: algo = GNUTLS_SIGN_RSA_SHA1; break; | |
147 | case HASH_SHA2_256: algo = GNUTLS_SIGN_RSA_SHA256; break; | |
148 | case HASH_SHA2_512: algo = GNUTLS_SIGN_RSA_SHA512; break; | |
149 | default: return US"nonhandled hash type"; | |
150 | } | |
151 | ||
2592e6c0 JH |
152 | k.data = data_hash->data; |
153 | k.size = data_hash->len; | |
154 | s.data = sig->data; | |
155 | s.size = sig->len; | |
d73e45df | 156 | if ((rc = gnutls_pubkey_verify_hash2(verify_ctx->key, algo, 0, &k, &s)) < 0) |
2592e6c0 JH |
157 | ret = gnutls_strerror(rc); |
158 | ||
d73e45df | 159 | gnutls_pubkey_deinit(verify_ctx->key); |
2592e6c0 JH |
160 | return ret; |
161 | } | |
162 | ||
163 | ||
164 | ||
165 | ||
d73e45df | 166 | #elif defined(SIGN_GCRYPT) |
2592e6c0 | 167 | /******************************************************************************/ |
d73e45df | 168 | /* This variant is used under pre-3.0.0 GnuTLS. Only rsa-sha1 and rsa-sha256 */ |
2592e6c0 JH |
169 | |
170 | ||
171 | /* Internal service routine: | |
172 | Read and move past an asn.1 header, checking class & tag, | |
173 | optionally returning the data-length */ | |
174 | ||
175 | static int | |
176 | as_tag(blob * der, uschar req_cls, long req_tag, long * alen) | |
177 | { | |
178 | int rc; | |
179 | uschar tag_class; | |
180 | int taglen; | |
181 | long tag, len; | |
182 | ||
6e4aaa85 | 183 | /* debug_printf_indent("as_tag: %02x %02x %02x %02x\n", |
2592e6c0 JH |
184 | der->data[0], der->data[1], der->data[2], der->data[3]); */ |
185 | ||
186 | if ((rc = asn1_get_tag_der(der->data++, der->len--, &tag_class, &taglen, &tag)) | |
187 | != ASN1_SUCCESS) | |
188 | return rc; | |
189 | ||
190 | if (tag_class != req_cls || tag != req_tag) return ASN1_ELEMENT_NOT_FOUND; | |
191 | ||
192 | if ((len = asn1_get_length_der(der->data, der->len, &taglen)) < 0) | |
193 | return ASN1_DER_ERROR; | |
194 | if (alen) *alen = len; | |
195 | ||
6e4aaa85 | 196 | /* debug_printf_indent("as_tag: tlen %d dlen %d\n", taglen, (int)len); */ |
2592e6c0 JH |
197 | |
198 | der->data += taglen; | |
199 | der->len -= taglen; | |
200 | return rc; | |
201 | } | |
202 | ||
203 | /* Internal service routine: | |
204 | Read and move over an asn.1 integer, setting an MPI to the value | |
205 | */ | |
206 | ||
207 | static uschar * | |
208 | as_mpi(blob * der, gcry_mpi_t * mpi) | |
209 | { | |
210 | long alen; | |
211 | int rc; | |
212 | gcry_error_t gerr; | |
213 | ||
214 | /* integer; move past the header */ | |
215 | if ((rc = as_tag(der, 0, ASN1_TAG_INTEGER, &alen)) != ASN1_SUCCESS) | |
216 | return US asn1_strerror(rc); | |
217 | ||
218 | /* read to an MPI */ | |
219 | if ((gerr = gcry_mpi_scan(mpi, GCRYMPI_FMT_STD, der->data, alen, NULL))) | |
220 | return US gcry_strerror(gerr); | |
221 | ||
222 | /* move over the data */ | |
223 | der->data += alen; der->len -= alen; | |
224 | return NULL; | |
225 | } | |
226 | ||
227 | ||
228 | ||
229 | void | |
9b2583c4 | 230 | exim_dkim_init(void) |
2592e6c0 JH |
231 | { |
232 | /* Version check should be the very first call because it | |
233 | makes sure that important subsystems are initialized. */ | |
234 | if (!gcry_check_version (GCRYPT_VERSION)) | |
235 | { | |
236 | fputs ("libgcrypt version mismatch\n", stderr); | |
237 | exit (2); | |
238 | } | |
239 | ||
240 | /* We don't want to see any warnings, e.g. because we have not yet | |
241 | parsed program options which might be used to suppress such | |
242 | warnings. */ | |
243 | gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN); | |
244 | ||
245 | /* ... If required, other initialization goes here. Note that the | |
246 | process might still be running with increased privileges and that | |
247 | the secure memory has not been initialized. */ | |
248 | ||
249 | /* Allocate a pool of 16k secure memory. This make the secure memory | |
250 | available and also drops privileges where needed. */ | |
251 | gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0); | |
252 | ||
253 | /* It is now okay to let Libgcrypt complain when there was/is | |
254 | a problem with the secure memory. */ | |
255 | gcry_control (GCRYCTL_RESUME_SECMEM_WARN); | |
256 | ||
257 | /* ... If required, other initialization goes here. */ | |
258 | ||
259 | /* Tell Libgcrypt that initialization has completed. */ | |
260 | gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); | |
261 | ||
262 | return; | |
263 | } | |
264 | ||
265 | ||
266 | ||
267 | ||
268 | /* Accumulate data (gnutls-only). | |
269 | String to be appended must be nul-terminated. */ | |
270 | ||
271 | blob * | |
9b2583c4 | 272 | exim_dkim_data_append(blob * b, int * alloc, uschar * s) |
2592e6c0 JH |
273 | { |
274 | return b; /*dummy*/ | |
275 | } | |
276 | ||
277 | ||
278 | ||
279 | /* import private key from PEM string in memory. | |
280 | Return: NULL for success, or an error string */ | |
281 | ||
282 | const uschar * | |
9b2583c4 | 283 | exim_dkim_signing_init(uschar * privkey_pem, es_ctx * sign_ctx) |
2592e6c0 JH |
284 | { |
285 | uschar * s1, * s2; | |
286 | blob der; | |
287 | long alen; | |
288 | int rc; | |
289 | ||
9b2583c4 JH |
290 | /*XXX will need extension to _spot_ as well as handle a |
291 | non-RSA key? I think... */ | |
292 | ||
2592e6c0 JH |
293 | /* |
294 | * RSAPrivateKey ::= SEQUENCE | |
295 | * version Version, | |
296 | * modulus INTEGER, -- n | |
297 | * publicExponent INTEGER, -- e | |
298 | * privateExponent INTEGER, -- d | |
299 | * prime1 INTEGER, -- p | |
300 | * prime2 INTEGER, -- q | |
301 | * exponent1 INTEGER, -- d mod (p-1) | |
302 | * exponent2 INTEGER, -- d mod (q-1) | |
303 | * coefficient INTEGER, -- (inverse of q) mod p | |
304 | * otherPrimeInfos OtherPrimeInfos OPTIONAL | |
305 | */ | |
306 | ||
307 | if ( !(s1 = Ustrstr(CS privkey_pem, "-----BEGIN RSA PRIVATE KEY-----")) | |
308 | || !(s2 = Ustrstr(CS (s1+=31), "-----END RSA PRIVATE KEY-----" )) | |
309 | ) | |
310 | return US"Bad PEM wrapper"; | |
311 | ||
312 | *s2 = '\0'; | |
313 | ||
314 | if ((der.len = b64decode(s1, &der.data)) < 0) | |
315 | return US"Bad PEM-DER b64 decode"; | |
316 | ||
317 | /* untangle asn.1 */ | |
318 | ||
319 | /* sequence; just move past the header */ | |
320 | if ((rc = as_tag(&der, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, NULL)) | |
321 | != ASN1_SUCCESS) goto asn_err; | |
322 | ||
323 | /* integer version; move past the header, check is zero */ | |
324 | if ((rc = as_tag(&der, 0, ASN1_TAG_INTEGER, &alen)) != ASN1_SUCCESS) | |
325 | goto asn_err; | |
326 | if (alen != 1 || *der.data != 0) | |
327 | return US"Bad version number"; | |
328 | der.data++; der.len--; | |
329 | ||
330 | if ( (s1 = as_mpi(&der, &sign_ctx->n)) | |
331 | || (s1 = as_mpi(&der, &sign_ctx->e)) | |
332 | || (s1 = as_mpi(&der, &sign_ctx->d)) | |
333 | || (s1 = as_mpi(&der, &sign_ctx->p)) | |
334 | || (s1 = as_mpi(&der, &sign_ctx->q)) | |
335 | || (s1 = as_mpi(&der, &sign_ctx->dp)) | |
336 | || (s1 = as_mpi(&der, &sign_ctx->dq)) | |
337 | || (s1 = as_mpi(&der, &sign_ctx->qp)) | |
338 | ) | |
339 | return s1; | |
340 | ||
349d0869 | 341 | #ifdef extreme_debug |
6e4aaa85 | 342 | DEBUG(D_acl) debug_printf_indent("rsa_signing_init:\n"); |
2592e6c0 JH |
343 | { |
344 | uschar * s; | |
345 | gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->n); | |
6e4aaa85 | 346 | debug_printf_indent(" N : %s\n", s); |
2592e6c0 | 347 | gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->e); |
6e4aaa85 | 348 | debug_printf_indent(" E : %s\n", s); |
2592e6c0 | 349 | gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->d); |
6e4aaa85 | 350 | debug_printf_indent(" D : %s\n", s); |
2592e6c0 | 351 | gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->p); |
6e4aaa85 | 352 | debug_printf_indent(" P : %s\n", s); |
2592e6c0 | 353 | gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->q); |
6e4aaa85 | 354 | debug_printf_indent(" Q : %s\n", s); |
2592e6c0 | 355 | gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->dp); |
6e4aaa85 | 356 | debug_printf_indent(" DP: %s\n", s); |
2592e6c0 | 357 | gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->dq); |
6e4aaa85 | 358 | debug_printf_indent(" DQ: %s\n", s); |
2592e6c0 | 359 | gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->qp); |
6e4aaa85 | 360 | debug_printf_indent(" QP: %s\n", s); |
2592e6c0 | 361 | } |
349d0869 | 362 | #endif |
2592e6c0 JH |
363 | return NULL; |
364 | ||
365 | asn_err: return US asn1_strerror(rc); | |
366 | } | |
367 | ||
368 | ||
369 | ||
370 | /* allocate mem for signature (when signing) */ | |
371 | /* sign data (gnutls_only) | |
372 | OR | |
373 | sign hash. | |
374 | ||
375 | Return: NULL for success, or an error string */ | |
376 | ||
377 | const uschar * | |
d73e45df | 378 | exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig) |
2592e6c0 | 379 | { |
349d0869 | 380 | char * sexp_hash; |
2592e6c0 JH |
381 | gcry_sexp_t s_hash = NULL, s_key = NULL, s_sig = NULL; |
382 | gcry_mpi_t m_sig; | |
383 | uschar * errstr; | |
384 | gcry_error_t gerr; | |
385 | ||
9b2583c4 JH |
386 | /*XXX will need extension for hash types (though, possibly, should |
387 | be re-specced to not rehash but take an already-hashed value? Actually | |
d73e45df | 388 | current impl looks WRONG - it _is_ given a hash so should not be |
9b2583c4 JH |
389 | re-hashing. Has this been tested? |
390 | ||
391 | Will need extension for non-RSA sugning algos. */ | |
392 | ||
d73e45df JH |
393 | switch (hash) |
394 | { | |
349d0869 JH |
395 | case HASH_SHA1: sexp_hash = "(data(flags pkcs1)(hash sha1 %b))"; break; |
396 | case HASH_SHA2_256: sexp_hash = "(data(flags pkcs1)(hash sha256 %b))"; break; | |
d73e45df JH |
397 | default: return US"nonhandled hash type"; |
398 | } | |
399 | ||
2592e6c0 JH |
400 | #define SIGSPACE 128 |
401 | sig->data = store_get(SIGSPACE); | |
402 | ||
403 | if (gcry_mpi_cmp (sign_ctx->p, sign_ctx->q) > 0) | |
404 | { | |
405 | gcry_mpi_swap (sign_ctx->p, sign_ctx->q); | |
406 | gcry_mpi_invm (sign_ctx->qp, sign_ctx->p, sign_ctx->q); | |
407 | } | |
408 | ||
409 | if ( (gerr = gcry_sexp_build (&s_key, NULL, | |
410 | "(private-key (rsa (n%m)(e%m)(d%m)(p%m)(q%m)(u%m)))", | |
411 | sign_ctx->n, sign_ctx->e, | |
412 | sign_ctx->d, sign_ctx->p, | |
413 | sign_ctx->q, sign_ctx->qp)) | |
349d0869 | 414 | || (gerr = gcry_sexp_build (&s_hash, NULL, sexp_hash, |
2592e6c0 JH |
415 | (int) data->len, CS data->data)) |
416 | || (gerr = gcry_pk_sign (&s_sig, s_hash, s_key)) | |
417 | ) | |
418 | return US gcry_strerror(gerr); | |
419 | ||
420 | /* gcry_sexp_dump(s_sig); */ | |
421 | ||
422 | if ( !(s_sig = gcry_sexp_find_token(s_sig, "s", 0)) | |
423 | ) | |
424 | return US"no sig result"; | |
425 | ||
426 | m_sig = gcry_sexp_nth_mpi(s_sig, 1, GCRYMPI_FMT_USG); | |
427 | ||
349d0869 | 428 | #ifdef extreme_debug |
2592e6c0 JH |
429 | DEBUG(D_acl) |
430 | { | |
431 | uschar * s; | |
432 | gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, m_sig); | |
6e4aaa85 | 433 | debug_printf_indent(" SG: %s\n", s); |
2592e6c0 | 434 | } |
349d0869 | 435 | #endif |
2592e6c0 JH |
436 | |
437 | gerr = gcry_mpi_print(GCRYMPI_FMT_USG, sig->data, SIGSPACE, &sig->len, m_sig); | |
438 | if (gerr) | |
439 | { | |
6e4aaa85 | 440 | debug_printf_indent("signature conversion from MPI to buffer failed\n"); |
2592e6c0 JH |
441 | return US gcry_strerror(gerr); |
442 | } | |
443 | #undef SIGSPACE | |
444 | ||
445 | return NULL; | |
446 | } | |
447 | ||
448 | ||
449 | /* import public key (from DER in memory) | |
450 | Return: NULL for success, or an error string */ | |
451 | ||
452 | const uschar * | |
9b2583c4 | 453 | exim_dkim_verify_init(blob * pubkey_der, ev_ctx * verify_ctx) |
2592e6c0 JH |
454 | { |
455 | /* | |
456 | in code sequence per b81207d2bfa92 rsa_parse_public_key() and asn1_get_mpi() | |
457 | */ | |
458 | uschar tag_class; | |
459 | int taglen; | |
460 | long alen; | |
461 | int rc; | |
462 | uschar * errstr; | |
463 | gcry_error_t gerr; | |
464 | uschar * stage = US"S1"; | |
465 | ||
466 | /* | |
467 | sequence | |
468 | sequence | |
469 | OBJECT:rsaEncryption | |
470 | NULL | |
471 | BIT STRING:RSAPublicKey | |
472 | sequence | |
473 | INTEGER:Public modulus | |
474 | INTEGER:Public exponent | |
475 | ||
476 | openssl rsa -in aux-fixed/dkim/dkim.private -pubout -outform DER | od -t x1 | head; | |
477 | openssl rsa -in aux-fixed/dkim/dkim.private -pubout | openssl asn1parse -dump; | |
478 | openssl rsa -in aux-fixed/dkim/dkim.private -pubout | openssl asn1parse -dump -offset 22; | |
479 | */ | |
480 | ||
481 | /* sequence; just move past the header */ | |
482 | if ((rc = as_tag(pubkey_der, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, NULL)) | |
483 | != ASN1_SUCCESS) goto asn_err; | |
484 | ||
485 | /* sequence; skip the entire thing */ | |
486 | DEBUG(D_acl) stage = US"S2"; | |
487 | if ((rc = as_tag(pubkey_der, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, &alen)) | |
488 | != ASN1_SUCCESS) goto asn_err; | |
489 | pubkey_der->data += alen; pubkey_der->len -= alen; | |
490 | ||
491 | ||
492 | /* bitstring: limit range to size of bitstring; | |
493 | move over header + content wrapper */ | |
494 | DEBUG(D_acl) stage = US"BS"; | |
495 | if ((rc = as_tag(pubkey_der, 0, ASN1_TAG_BIT_STRING, &alen)) != ASN1_SUCCESS) | |
496 | goto asn_err; | |
497 | pubkey_der->len = alen; | |
498 | pubkey_der->data++; pubkey_der->len--; | |
499 | ||
500 | /* sequence; just move past the header */ | |
501 | DEBUG(D_acl) stage = US"S3"; | |
502 | if ((rc = as_tag(pubkey_der, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, NULL)) | |
503 | != ASN1_SUCCESS) goto asn_err; | |
504 | ||
505 | /* read two integers */ | |
506 | DEBUG(D_acl) stage = US"MPI"; | |
507 | if ( (errstr = as_mpi(pubkey_der, &verify_ctx->n)) | |
508 | || (errstr = as_mpi(pubkey_der, &verify_ctx->e)) | |
509 | ) | |
510 | return errstr; | |
511 | ||
349d0869 | 512 | #ifdef extreme_debug |
6e4aaa85 | 513 | DEBUG(D_acl) debug_printf_indent("rsa_verify_init:\n"); |
2592e6c0 JH |
514 | { |
515 | uschar * s; | |
516 | gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, verify_ctx->n); | |
6e4aaa85 | 517 | debug_printf_indent(" N : %s\n", s); |
2592e6c0 | 518 | gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, verify_ctx->e); |
6e4aaa85 | 519 | debug_printf_indent(" E : %s\n", s); |
2592e6c0 JH |
520 | } |
521 | ||
349d0869 | 522 | #endif |
2592e6c0 JH |
523 | return NULL; |
524 | ||
525 | asn_err: | |
526 | DEBUG(D_acl) return string_sprintf("%s: %s", stage, asn1_strerror(rc)); | |
527 | return US asn1_strerror(rc); | |
528 | } | |
529 | ||
530 | ||
531 | /* verify signature (of hash) (given pubkey & alleged sig) | |
532 | Return: NULL for success, or an error string */ | |
533 | ||
534 | const uschar * | |
d73e45df | 535 | exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data_hash, blob * sig) |
2592e6c0 JH |
536 | { |
537 | /* | |
538 | cf. libgnutls 2.8.5 _wrap_gcry_pk_verify() | |
539 | */ | |
349d0869 | 540 | char * sexp_hash; |
2592e6c0 JH |
541 | gcry_mpi_t m_sig; |
542 | gcry_sexp_t s_sig = NULL, s_hash = NULL, s_pkey = NULL; | |
543 | gcry_error_t gerr; | |
544 | uschar * stage; | |
545 | ||
349d0869 | 546 | /*XXX needs extension for SHA512 */ |
d73e45df JH |
547 | switch (hash) |
548 | { | |
349d0869 JH |
549 | case HASH_SHA1: sexp_hash = "(data(flags pkcs1)(hash sha1 %b))"; break; |
550 | case HASH_SHA2_256: sexp_hash = "(data(flags pkcs1)(hash sha256 %b))"; break; | |
551 | default: return US"nonhandled hash type"; | |
d73e45df JH |
552 | } |
553 | ||
2592e6c0 JH |
554 | if ( (stage = US"pkey sexp build", |
555 | gerr = gcry_sexp_build (&s_pkey, NULL, "(public-key(rsa(n%m)(e%m)))", | |
556 | verify_ctx->n, verify_ctx->e)) | |
557 | || (stage = US"data sexp build", | |
349d0869 | 558 | gerr = gcry_sexp_build (&s_hash, NULL, sexp_hash, |
2592e6c0 JH |
559 | (int) data_hash->len, CS data_hash->data)) |
560 | || (stage = US"sig mpi scan", | |
561 | gerr = gcry_mpi_scan(&m_sig, GCRYMPI_FMT_USG, sig->data, sig->len, NULL)) | |
562 | || (stage = US"sig sexp build", | |
563 | gerr = gcry_sexp_build (&s_sig, NULL, "(sig-val(rsa(s%m)))", m_sig)) | |
564 | || (stage = US"verify", | |
565 | gerr = gcry_pk_verify (s_sig, s_hash, s_pkey)) | |
566 | ) | |
567 | { | |
6e4aaa85 | 568 | DEBUG(D_acl) debug_printf_indent("verify: error in stage '%s'\n", stage); |
2592e6c0 JH |
569 | return US gcry_strerror(gerr); |
570 | } | |
571 | ||
572 | if (s_sig) gcry_sexp_release (s_sig); | |
573 | if (s_hash) gcry_sexp_release (s_hash); | |
574 | if (s_pkey) gcry_sexp_release (s_pkey); | |
575 | gcry_mpi_release (m_sig); | |
576 | gcry_mpi_release (verify_ctx->n); | |
577 | gcry_mpi_release (verify_ctx->e); | |
578 | ||
579 | return NULL; | |
580 | } | |
581 | ||
582 | ||
583 | ||
584 | ||
d73e45df | 585 | #elif defined(SIGN_OPENSSL) |
2592e6c0 JH |
586 | /******************************************************************************/ |
587 | ||
588 | void | |
9b2583c4 | 589 | exim_dkim_init(void) |
2592e6c0 | 590 | { |
9e70917d | 591 | ERR_load_crypto_strings(); |
2592e6c0 JH |
592 | } |
593 | ||
594 | ||
595 | /* accumulate data (gnutls-only) */ | |
596 | blob * | |
9b2583c4 | 597 | exim_dkim_data_append(blob * b, int * alloc, uschar * s) |
2592e6c0 JH |
598 | { |
599 | return b; /*dummy*/ | |
600 | } | |
601 | ||
602 | ||
603 | /* import private key from PEM string in memory. | |
604 | Return: NULL for success, or an error string */ | |
605 | ||
606 | const uschar * | |
9b2583c4 | 607 | exim_dkim_signing_init(uschar * privkey_pem, es_ctx * sign_ctx) |
2592e6c0 | 608 | { |
d73e45df | 609 | BIO * bp = BIO_new_mem_buf(privkey_pem, -1); |
2592e6c0 | 610 | |
d73e45df JH |
611 | if (!(sign_ctx->key = PEM_read_bio_PrivateKey(bp, NULL, NULL, NULL))) |
612 | return ERR_error_string(ERR_get_error(), NULL); | |
2592e6c0 JH |
613 | return NULL; |
614 | } | |
615 | ||
616 | ||
617 | ||
618 | /* allocate mem for signature (when signing) */ | |
619 | /* sign data (gnutls_only) | |
620 | OR | |
621 | sign hash. | |
622 | ||
9e70917d | 623 | Return: NULL for success with the signaature in the sig blob, or an error string */ |
2592e6c0 JH |
624 | |
625 | const uschar * | |
d73e45df | 626 | exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig) |
2592e6c0 | 627 | { |
d73e45df JH |
628 | const EVP_MD * md; |
629 | EVP_PKEY_CTX * ctx; | |
630 | size_t siglen; | |
9b2583c4 | 631 | |
d73e45df JH |
632 | switch (hash) |
633 | { | |
634 | case HASH_SHA1: md = EVP_sha1(); break; | |
635 | case HASH_SHA2_256: md = EVP_sha256(); break; | |
636 | case HASH_SHA2_512: md = EVP_sha512(); break; | |
637 | default: return US"nonhandled hash type"; | |
638 | } | |
2592e6c0 | 639 | |
d73e45df JH |
640 | if ( (ctx = EVP_PKEY_CTX_new(sign_ctx->key, NULL)) |
641 | && EVP_PKEY_sign_init(ctx) > 0 | |
642 | && EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) > 0 | |
643 | && EVP_PKEY_CTX_set_signature_md(ctx, md) > 0 | |
644 | && EVP_PKEY_sign(ctx, NULL, &siglen, data->data, data->len) > 0 | |
645 | ) | |
2592e6c0 | 646 | { |
d73e45df JH |
647 | /* Allocate mem for signature */ |
648 | sig->data = store_get(siglen); | |
d73e45df JH |
649 | |
650 | if (EVP_PKEY_sign(ctx, sig->data, &siglen, data->data, data->len) > 0) | |
9e70917d JH |
651 | { |
652 | EVP_PKEY_CTX_free(ctx); | |
653 | sig->len = siglen; | |
654 | return NULL; | |
655 | } | |
2592e6c0 JH |
656 | } |
657 | ||
d73e45df JH |
658 | if (ctx) EVP_PKEY_CTX_free(ctx); |
659 | return ERR_error_string(ERR_get_error(), NULL); | |
2592e6c0 JH |
660 | } |
661 | ||
662 | ||
663 | ||
664 | /* import public key (from DER in memory) | |
9b2583c4 | 665 | Return: NULL for success, or an error string */ |
2592e6c0 JH |
666 | |
667 | const uschar * | |
9b2583c4 | 668 | exim_dkim_verify_init(blob * pubkey_der, ev_ctx * verify_ctx) |
2592e6c0 | 669 | { |
d73e45df | 670 | const uschar * s = pubkey_der->data; |
2592e6c0 | 671 | |
d73e45df | 672 | /*XXX hmm, we never free this */ |
9b2583c4 | 673 | |
d73e45df JH |
674 | if ((verify_ctx->key = d2i_PUBKEY(NULL, &s, pubkey_der->len))) |
675 | return NULL; | |
676 | return ERR_error_string(ERR_get_error(), NULL); | |
2592e6c0 JH |
677 | } |
678 | ||
679 | ||
680 | ||
681 | ||
682 | /* verify signature (of hash) (given pubkey & alleged sig) | |
683 | Return: NULL for success, or an error string */ | |
684 | ||
685 | const uschar * | |
d73e45df | 686 | exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data_hash, blob * sig) |
2592e6c0 | 687 | { |
d73e45df JH |
688 | const EVP_MD * md; |
689 | EVP_PKEY_CTX * ctx; | |
2592e6c0 | 690 | |
d73e45df | 691 | switch (hash) |
2592e6c0 | 692 | { |
d73e45df JH |
693 | case HASH_SHA1: md = EVP_sha1(); break; |
694 | case HASH_SHA2_256: md = EVP_sha256(); break; | |
695 | case HASH_SHA2_512: md = EVP_sha512(); break; | |
696 | default: return US"nonhandled hash type"; | |
2592e6c0 | 697 | } |
d73e45df JH |
698 | |
699 | if ( (ctx = EVP_PKEY_CTX_new(verify_ctx->key, NULL)) | |
700 | && EVP_PKEY_verify_init(ctx) > 0 | |
701 | && EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) > 0 | |
702 | && EVP_PKEY_CTX_set_signature_md(ctx, md) > 0 | |
703 | && EVP_PKEY_verify(ctx, sig->data, sig->len, | |
704 | data_hash->data, data_hash->len) == 1 | |
705 | ) | |
706 | { EVP_PKEY_CTX_free(ctx); return NULL; } | |
707 | ||
708 | if (ctx) EVP_PKEY_CTX_free(ctx); | |
709 | return ERR_error_string(ERR_get_error(), NULL); | |
2592e6c0 JH |
710 | } |
711 | ||
712 | ||
d73e45df | 713 | |
2592e6c0 JH |
714 | #endif |
715 | /******************************************************************************/ | |
716 | ||
717 | #endif /*DISABLE_DKIM*/ | |
718 | /* End of File */ |