Tidy build under pre-v3 GnuTLS
[exim.git] / src / src / pdkim / signing.c
1 /*
2 * PDKIM - a RFC4871 (DKIM) implementation
3 *
4 * Copyright (C) 2016 Exim maintainers
5 *
6 * signing/verification interface
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"
18 #include "signing.h"
19
20
21 /******************************************************************************/
22 #ifdef SIGN_GNUTLS
23
24 void
25 exim_dkim_init(void)
26 {
27 }
28
29
30 /* accumulate data (gnutls-only). String to be appended must be nul-terminated. */
31 blob *
32 exim_dkim_data_append(blob * b, int * alloc, uschar * s)
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 *
46 exim_dkim_signing_init(uschar * privkey_pem, es_ctx * sign_ctx)
47 {
48 gnutls_datum_t k;
49 int rc;
50
51 k.data = privkey_pem;
52 k.size = strlen(privkey_pem);
53
54 if ( (rc = gnutls_x509_privkey_init(&sign_ctx->key)) != GNUTLS_E_SUCCESS
55 || (rc = gnutls_x509_privkey_import(sign_ctx->key, &k,
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 *
73 exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig)
74 {
75 gnutls_digest_algorithm_t dig;
76 gnutls_datum_t k;
77 size_t sigsize = 0;
78 int rc;
79 const uschar * ret = NULL;
80
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
89 /* Allocate mem for signature */
90 k.data = data->data;
91 k.size = data->len;
92 (void) gnutls_x509_privkey_sign_data(sign_ctx->key, dig,
93 0, &k, NULL, &sigsize);
94
95 sig->data = store_get(sigsize);
96 sig->len = sigsize;
97
98 /* Do signing */
99 if ((rc = gnutls_x509_privkey_sign_data(sign_ctx->key, dig,
100 0, &k, sig->data, &sigsize)) != GNUTLS_E_SUCCESS
101 )
102 ret = gnutls_strerror(rc);
103
104 gnutls_x509_privkey_deinit(sign_ctx->key);
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 *
114 exim_dkim_verify_init(blob * pubkey_der, ev_ctx * verify_ctx)
115 {
116 gnutls_datum_t k;
117 int rc;
118 const uschar * ret = NULL;
119
120 gnutls_pubkey_init(&verify_ctx->key);
121
122 k.data = pubkey_der->data;
123 k.size = pubkey_der->len;
124
125 if ((rc = gnutls_pubkey_import(verify_ctx->key, &k, GNUTLS_X509_FMT_DER))
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 *
136 exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data_hash, blob * sig)
137 {
138 gnutls_sign_algorithm_t algo;
139 gnutls_datum_t k, s;
140 int rc;
141 const uschar * ret = NULL;
142
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
152 k.data = data_hash->data;
153 k.size = data_hash->len;
154 s.data = sig->data;
155 s.size = sig->len;
156 if ((rc = gnutls_pubkey_verify_hash2(verify_ctx->key, algo, 0, &k, &s)) < 0)
157 ret = gnutls_strerror(rc);
158
159 gnutls_pubkey_deinit(verify_ctx->key);
160 return ret;
161 }
162
163
164
165
166 #elif defined(SIGN_GCRYPT)
167 /******************************************************************************/
168 /* This variant is used under pre-3.0.0 GnuTLS. Only rsa-sha1 and rsa-sha256 */
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
183 /* debug_printf_indent("as_tag: %02x %02x %02x %02x\n",
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
196 /* debug_printf_indent("as_tag: tlen %d dlen %d\n", taglen, (int)len); */
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
230 exim_dkim_init(void)
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 *
272 exim_dkim_data_append(blob * b, int * alloc, uschar * s)
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 *
283 exim_dkim_signing_init(uschar * privkey_pem, es_ctx * sign_ctx)
284 {
285 uschar * s1, * s2;
286 blob der;
287 long alen;
288 int rc;
289
290 /*XXX will need extension to _spot_ as well as handle a
291 non-RSA key? I think... */
292
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
341 #ifdef extreme_debug
342 DEBUG(D_acl) debug_printf_indent("rsa_signing_init:\n");
343 {
344 uschar * s;
345 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->n);
346 debug_printf_indent(" N : %s\n", s);
347 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->e);
348 debug_printf_indent(" E : %s\n", s);
349 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->d);
350 debug_printf_indent(" D : %s\n", s);
351 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->p);
352 debug_printf_indent(" P : %s\n", s);
353 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->q);
354 debug_printf_indent(" Q : %s\n", s);
355 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->dp);
356 debug_printf_indent(" DP: %s\n", s);
357 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->dq);
358 debug_printf_indent(" DQ: %s\n", s);
359 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->qp);
360 debug_printf_indent(" QP: %s\n", s);
361 }
362 #endif
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 *
378 exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig)
379 {
380 char * sexp_hash;
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
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
388 current impl looks WRONG - it _is_ given a hash so should not be
389 re-hashing. Has this been tested?
390
391 Will need extension for non-RSA sugning algos. */
392
393 switch (hash)
394 {
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;
397 default: return US"nonhandled hash type";
398 }
399
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))
414 || (gerr = gcry_sexp_build (&s_hash, NULL, sexp_hash,
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
428 #ifdef extreme_debug
429 DEBUG(D_acl)
430 {
431 uschar * s;
432 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, m_sig);
433 debug_printf_indent(" SG: %s\n", s);
434 }
435 #endif
436
437 gerr = gcry_mpi_print(GCRYMPI_FMT_USG, sig->data, SIGSPACE, &sig->len, m_sig);
438 if (gerr)
439 {
440 debug_printf_indent("signature conversion from MPI to buffer failed\n");
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 *
453 exim_dkim_verify_init(blob * pubkey_der, ev_ctx * verify_ctx)
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
512 #ifdef extreme_debug
513 DEBUG(D_acl) debug_printf_indent("rsa_verify_init:\n");
514 {
515 uschar * s;
516 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, verify_ctx->n);
517 debug_printf_indent(" N : %s\n", s);
518 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, verify_ctx->e);
519 debug_printf_indent(" E : %s\n", s);
520 }
521
522 #endif
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 *
535 exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data_hash, blob * sig)
536 {
537 /*
538 cf. libgnutls 2.8.5 _wrap_gcry_pk_verify()
539 */
540 char * sexp_hash;
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
546 /*XXX needs extension for SHA512 */
547 switch (hash)
548 {
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";
552 }
553
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",
558 gerr = gcry_sexp_build (&s_hash, NULL, sexp_hash,
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 {
568 DEBUG(D_acl) debug_printf_indent("verify: error in stage '%s'\n", stage);
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
585 #elif defined(SIGN_OPENSSL)
586 /******************************************************************************/
587
588 void
589 exim_dkim_init(void)
590 {
591 ERR_load_crypto_strings();
592 }
593
594
595 /* accumulate data (gnutls-only) */
596 blob *
597 exim_dkim_data_append(blob * b, int * alloc, uschar * s)
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 *
607 exim_dkim_signing_init(uschar * privkey_pem, es_ctx * sign_ctx)
608 {
609 BIO * bp = BIO_new_mem_buf(privkey_pem, -1);
610
611 if (!(sign_ctx->key = PEM_read_bio_PrivateKey(bp, NULL, NULL, NULL)))
612 return ERR_error_string(ERR_get_error(), NULL);
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
623 Return: NULL for success with the signaature in the sig blob, or an error string */
624
625 const uschar *
626 exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig)
627 {
628 const EVP_MD * md;
629 EVP_PKEY_CTX * ctx;
630 size_t siglen;
631
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 }
639
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 )
646 {
647 /* Allocate mem for signature */
648 sig->data = store_get(siglen);
649
650 if (EVP_PKEY_sign(ctx, sig->data, &siglen, data->data, data->len) > 0)
651 {
652 EVP_PKEY_CTX_free(ctx);
653 sig->len = siglen;
654 return NULL;
655 }
656 }
657
658 if (ctx) EVP_PKEY_CTX_free(ctx);
659 return ERR_error_string(ERR_get_error(), NULL);
660 }
661
662
663
664 /* import public key (from DER in memory)
665 Return: NULL for success, or an error string */
666
667 const uschar *
668 exim_dkim_verify_init(blob * pubkey_der, ev_ctx * verify_ctx)
669 {
670 const uschar * s = pubkey_der->data;
671
672 /*XXX hmm, we never free this */
673
674 if ((verify_ctx->key = d2i_PUBKEY(NULL, &s, pubkey_der->len)))
675 return NULL;
676 return ERR_error_string(ERR_get_error(), NULL);
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 *
686 exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data_hash, blob * sig)
687 {
688 const EVP_MD * md;
689 EVP_PKEY_CTX * ctx;
690
691 switch (hash)
692 {
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";
697 }
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);
710 }
711
712
713
714 #endif
715 /******************************************************************************/
716
717 #endif /*DISABLE_DKIM*/
718 /* End of File */