2 * PDKIM - a RFC4871 (DKIM) implementation
3 * Copyright (c) The Exim Maintainers 1995 - 2022
4 * SPDX-License-Identifier: GPL-2.0-or-later
6 * signing/verification interface
10 #include "crypt_ver.h"
15 # include "../macro_predef.h"
20 # ifdef SIGN_HAVE_ED25519
21 builtin_macro_create(US"_CRYPTO_SIGN_ED25519");
23 # ifdef EXIM_HAVE_SHA3
24 builtin_macro_create(US"_CRYPTO_HASH_SHA3");
29 #ifndef DISABLE_DKIM /* rest of file */
32 # error Must no DISABLE_TLS, for DKIM
36 /******************************************************************************/
38 # define EXIM_GNUTLS_LIBRARY_LOG_LEVEL 3
40 # ifndef GNUTLS_VERIFY_ALLOW_BROKEN
41 # define GNUTLS_VERIFY_ALLOW_BROKEN 0
45 /* Logging function which can be registered with
46 * gnutls_global_set_log_function()
47 * gnutls_global_set_log_level() 0..9
49 #if EXIM_GNUTLS_LIBRARY_LOG_LEVEL >= 0
51 exim_gnutls_logger_cb(int level, const char *message)
53 size_t len = strlen(message);
56 DEBUG(D_tls) debug_printf("GnuTLS<%d> empty debug message\n", level);
59 DEBUG(D_tls) debug_printf("GnuTLS<%d>: %s%s", level, message,
60 message[len-1] == '\n' ? "" : "\n");
69 #if EXIM_GNUTLS_LIBRARY_LOG_LEVEL >= 0
72 gnutls_global_set_log_function(exim_gnutls_logger_cb);
73 /* arbitrarily chosen level; bump upto 9 for more */
74 gnutls_global_set_log_level(EXIM_GNUTLS_LIBRARY_LOG_LEVEL);
80 /* accumulate data (gnutls-only). String to be appended must be nul-terminated. */
82 exim_dkim_data_append(gstring * g, uschar * s)
84 return string_cat(g, s);
89 /* import private key from PEM string in memory.
90 Return: NULL for success, or an error string */
93 exim_dkim_signing_init(const uschar * privkey_pem, es_ctx * sign_ctx)
95 gnutls_datum_t k = { .data = (void *)privkey_pem, .size = Ustrlen(privkey_pem) };
96 gnutls_x509_privkey_t x509_key;
100 if ( (where = US"internal init", rc = gnutls_x509_privkey_init(&x509_key))
101 || (rc = gnutls_privkey_init(&sign_ctx->key))
102 || (where = US"privkey PEM-block import",
103 rc = gnutls_x509_privkey_import(x509_key, &k, GNUTLS_X509_FMT_PEM))
104 || (where = US"internal privkey transfer",
105 rc = gnutls_privkey_import_x509(sign_ctx->key, x509_key, 0))
107 return string_sprintf("%s: %s", where, gnutls_strerror(rc));
109 switch (rc = gnutls_privkey_get_pk_algorithm(sign_ctx->key, NULL))
111 case GNUTLS_PK_RSA: sign_ctx->keytype = KEYTYPE_RSA; break;
112 #ifdef SIGN_HAVE_ED25519
113 case GNUTLS_PK_EDDSA_ED25519: sign_ctx->keytype = KEYTYPE_ED25519; break;
115 default: return rc < 0
116 ? CUS gnutls_strerror(rc)
117 : string_sprintf("Unhandled key type: %d '%s'", rc, gnutls_pk_get_name(rc));
125 /* allocate mem for signature (when signing) */
126 /* hash & sign data. No way to do incremental.
128 Return: NULL for success, or an error string */
131 exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig)
133 gnutls_datum_t k_data = { .data = data->data, .size = data->len };
134 gnutls_digest_algorithm_t dig;
135 gnutls_datum_t k_sig;
140 case HASH_SHA1: dig = GNUTLS_DIG_SHA1; break;
141 case HASH_SHA2_256: dig = GNUTLS_DIG_SHA256; break;
142 case HASH_SHA2_512: dig = GNUTLS_DIG_SHA512; break;
143 default: return US"nonhandled hash type";
146 if ((rc = gnutls_privkey_sign_data(sign_ctx->key, dig, 0, &k_data, &k_sig)))
147 return CUS gnutls_strerror(rc);
149 /* Don't care about deinit for the key; shortlived process */
151 sig->data = k_sig.data;
152 sig->len = k_sig.size;
158 /* import public key (from blob in memory)
159 Return: NULL for success, or an error string */
162 exim_dkim_verify_init(blob * pubkey, keyformat fmt, ev_ctx * verify_ctx,
167 const uschar * ret = NULL;
169 gnutls_pubkey_init(&verify_ctx->key);
170 k.data = pubkey->data;
171 k.size = pubkey->len;
176 if ((rc = gnutls_pubkey_import(verify_ctx->key, &k, GNUTLS_X509_FMT_DER)))
177 ret = US gnutls_strerror(rc);
179 #ifdef SIGN_HAVE_ED25519
180 case KEYFMT_ED25519_BARE:
181 if ((rc = gnutls_pubkey_import_ecc_raw(verify_ctx->key,
182 GNUTLS_ECC_CURVE_ED25519, &k, NULL)))
183 ret = US gnutls_strerror(rc);
187 ret = US"pubkey format not handled";
190 if (!ret && bits) gnutls_pubkey_get_pk_algorithm(verify_ctx->key, bits);
195 /* verify signature (of hash if RSA sig, of data if EC sig. No way to do incremental)
196 (given pubkey & alleged sig)
197 Return: NULL for success, or an error string */
200 exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data_hash, blob * sig)
202 gnutls_datum_t k = { .data = data_hash->data, .size = data_hash->len };
203 gnutls_datum_t s = { .data = sig->data, .size = sig->len };
205 const uschar * ret = NULL;
207 #ifdef SIGN_HAVE_ED25519
208 if (verify_ctx->keytype == KEYTYPE_ED25519)
210 if ((rc = gnutls_pubkey_verify_data2(verify_ctx->key,
211 GNUTLS_SIGN_EDDSA_ED25519, 0, &k, &s)) < 0)
212 ret = US gnutls_strerror(rc);
217 gnutls_sign_algorithm_t algo;
220 case HASH_SHA1: algo = GNUTLS_SIGN_RSA_SHA1; break;
221 case HASH_SHA2_256: algo = GNUTLS_SIGN_RSA_SHA256; break;
222 case HASH_SHA2_512: algo = GNUTLS_SIGN_RSA_SHA512; break;
223 default: return US"nonhandled hash type";
226 if ((rc = gnutls_pubkey_verify_hash2(verify_ctx->key, algo,
227 GNUTLS_VERIFY_ALLOW_BROKEN, &k, &s)) < 0)
228 ret = US gnutls_strerror(rc);
231 gnutls_pubkey_deinit(verify_ctx->key);
238 #elif defined(SIGN_GCRYPT)
239 /******************************************************************************/
240 /* This variant is used under pre-3.0.0 GnuTLS. Only rsa-sha1 and rsa-sha256 */
243 /* Internal service routine:
244 Read and move past an asn.1 header, checking class & tag,
245 optionally returning the data-length */
248 as_tag(blob * der, uschar req_cls, long req_tag, long * alen)
255 debug_printf_indent("as_tag: %02x %02x %02x %02x\n",
256 der->data[0], der->data[1], der->data[2], der->data[3]);
258 if ((rc = asn1_get_tag_der(der->data++, der->len--, &tag_class, &taglen, &tag))
262 if (tag_class != req_cls || tag != req_tag) return ASN1_ELEMENT_NOT_FOUND;
264 if ((len = asn1_get_length_der(der->data, der->len, &taglen)) < 0)
265 return ASN1_DER_ERROR;
266 if (alen) *alen = len;
268 /* debug_printf_indent("as_tag: tlen %d dlen %d\n", taglen, (int)len); */
275 /* Internal service routine:
276 Read and move over an asn.1 integer, setting an MPI to the value
280 as_mpi(blob * der, gcry_mpi_t * mpi)
286 debug_printf_indent("%s\n", __FUNCTION__);
288 /* integer; move past the header */
289 if ((rc = as_tag(der, 0, ASN1_TAG_INTEGER, &alen)) != ASN1_SUCCESS)
290 return US asn1_strerror(rc);
293 if ((gerr = gcry_mpi_scan(mpi, GCRYMPI_FMT_STD, der->data, alen, NULL)))
294 return US gcry_strerror(gerr);
296 /* move over the data */
297 der->data += alen; der->len -= alen;
306 /* Version check should be the very first call because it
307 makes sure that important subsystems are initialized. */
308 if (!gcry_check_version (GCRYPT_VERSION))
310 fputs ("libgcrypt version mismatch\n", stderr);
314 /* We don't want to see any warnings, e.g. because we have not yet
315 parsed program options which might be used to suppress such
317 gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
319 /* ... If required, other initialization goes here. Note that the
320 process might still be running with increased privileges and that
321 the secure memory has not been initialized. */
323 /* Allocate a pool of 16k secure memory. This make the secure memory
324 available and also drops privileges where needed. */
325 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
327 /* It is now okay to let Libgcrypt complain when there was/is
328 a problem with the secure memory. */
329 gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
331 /* ... If required, other initialization goes here. */
333 /* Tell Libgcrypt that initialization has completed. */
334 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
342 /* Accumulate data (gnutls-only).
343 String to be appended must be nul-terminated. */
346 exim_dkim_data_append(gstring * g, uschar * s)
353 /* import private key from PEM string in memory.
354 Only handles RSA keys.
355 Return: NULL for success, or an error string */
358 exim_dkim_signing_init(const uschar * privkey_pem, es_ctx * sign_ctx)
365 /*XXX will need extension to _spot_ as well as handle a
366 non-RSA key? I think...
367 So... this is not a PrivateKeyInfo - which would have a field
368 identifying the keytype - PrivateKeyAlgorithmIdentifier -
369 but a plain RSAPrivateKey (wrapped in PEM-headers. Can we
370 use those as a type tag? What forms are there? "BEGIN EC PRIVATE KEY" (cf. ec(1ssl))
372 How does OpenSSL PEM_read_bio_PrivateKey() deal with it?
373 gnutls_x509_privkey_import() ?
377 * RSAPrivateKey ::= SEQUENCE
379 * modulus INTEGER, -- n
380 * publicExponent INTEGER, -- e
381 * privateExponent INTEGER, -- d
382 * prime1 INTEGER, -- p
383 * prime2 INTEGER, -- q
384 * exponent1 INTEGER, -- d mod (p-1)
385 * exponent2 INTEGER, -- d mod (q-1)
386 * coefficient INTEGER, -- (inverse of q) mod p
387 * otherPrimeInfos OtherPrimeInfos OPTIONAL
389 * ECPrivateKey ::= SEQUENCE {
390 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
391 * privateKey OCTET STRING,
392 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
393 * publicKey [1] BIT STRING OPTIONAL
395 * Hmm, only 1 useful item, and not even an integer? Wonder how we might use it...
397 - actually, gnutls_x509_privkey_import() appears to require a curve name parameter
398 value for that is an OID? a local-only integer (it's an enum in GnuTLS)?
402 ssh-keygen -t ecdsa -f foo.privkey
403 ssh-keygen -t ecdsa -b384 -f foo.privkey
404 ssh-keygen -t ecdsa -b521 -f foo.privkey
405 ssh-keygen -t ed25519 -f foo.privkey
407 < foo openssl pkcs8 -in /dev/stdin -inform PEM -nocrypt -topk8 -outform DER | od -x
409 openssl asn1parse -in foo -inform PEM -dump
410 openssl asn1parse -in foo -inform PEM -dump -stroffset 24 (??)
411 (not good for ed25519)
415 if ( !(s1 = Ustrstr(CS privkey_pem, "-----BEGIN RSA PRIVATE KEY-----"))
416 || !(s2 = Ustrstr(CS (s1+=31), "-----END RSA PRIVATE KEY-----" ))
418 return US"Bad PEM wrapper";
422 if ((rc = b64decode(s1, &der.data, s1) < 0))
423 return US"Bad PEM-DER b64 decode";
428 /* sequence; just move past the header */
429 if ((rc = as_tag(&der, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, NULL))
430 != ASN1_SUCCESS) goto asn_err;
432 /* integer version; move past the header, check is zero */
433 if ((rc = as_tag(&der, 0, ASN1_TAG_INTEGER, &alen)) != ASN1_SUCCESS)
435 if (alen != 1 || *der.data != 0)
436 return US"Bad version number";
437 der.data++; der.len--;
439 if ( (s1 = as_mpi(&der, &sign_ctx->n))
440 || (s1 = as_mpi(&der, &sign_ctx->e))
441 || (s1 = as_mpi(&der, &sign_ctx->d))
442 || (s1 = as_mpi(&der, &sign_ctx->p))
443 || (s1 = as_mpi(&der, &sign_ctx->q))
444 || (s1 = as_mpi(&der, &sign_ctx->dp))
445 || (s1 = as_mpi(&der, &sign_ctx->dq))
446 || (s1 = as_mpi(&der, &sign_ctx->qp))
451 DEBUG(D_acl) debug_printf_indent("rsa_signing_init:\n");
454 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->n);
455 debug_printf_indent(" N : %s\n", s);
456 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->e);
457 debug_printf_indent(" E : %s\n", s);
458 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->d);
459 debug_printf_indent(" D : %s\n", s);
460 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->p);
461 debug_printf_indent(" P : %s\n", s);
462 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->q);
463 debug_printf_indent(" Q : %s\n", s);
464 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->dp);
465 debug_printf_indent(" DP: %s\n", s);
466 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->dq);
467 debug_printf_indent(" DQ: %s\n", s);
468 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->qp);
469 debug_printf_indent(" QP: %s\n", s);
473 sign_ctx->keytype = KEYTYPE_RSA;
476 asn_err: return US asn1_strerror(rc);
481 /* allocate mem for signature (when signing) */
482 /* sign already-hashed data.
484 Return: NULL for success, or an error string */
487 exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig)
490 gcry_sexp_t s_hash = NULL, s_key = NULL, s_sig = NULL;
495 /*XXX will need extension for hash types (though, possibly, should
496 be re-specced to not rehash but take an already-hashed value? Actually
497 current impl looks WRONG - it _is_ given a hash so should not be
498 re-hashing. Has this been tested?
500 Will need extension for non-RSA sugning algos. */
504 case HASH_SHA1: sexp_hash = "(data(flags pkcs1)(hash sha1 %b))"; break;
505 case HASH_SHA2_256: sexp_hash = "(data(flags pkcs1)(hash sha256 %b))"; break;
506 default: return US"nonhandled hash type";
510 sig->data = store_get(SIGSPACE, GET_UNTAINTED);
512 if (gcry_mpi_cmp (sign_ctx->p, sign_ctx->q) > 0)
514 gcry_mpi_swap (sign_ctx->p, sign_ctx->q);
515 gcry_mpi_invm (sign_ctx->qp, sign_ctx->p, sign_ctx->q);
518 if ( (gerr = gcry_sexp_build (&s_key, NULL,
519 "(private-key (rsa (n%m)(e%m)(d%m)(p%m)(q%m)(u%m)))",
520 sign_ctx->n, sign_ctx->e,
521 sign_ctx->d, sign_ctx->p,
522 sign_ctx->q, sign_ctx->qp))
523 || (gerr = gcry_sexp_build (&s_hash, NULL, sexp_hash,
524 (int) data->len, CS data->data))
525 || (gerr = gcry_pk_sign (&s_sig, s_hash, s_key))
527 return US gcry_strerror(gerr);
529 /* gcry_sexp_dump(s_sig); */
531 if ( !(s_sig = gcry_sexp_find_token(s_sig, "s", 0))
533 return US"no sig result";
535 m_sig = gcry_sexp_nth_mpi(s_sig, 1, GCRYMPI_FMT_USG);
541 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, m_sig);
542 debug_printf_indent(" SG: %s\n", s);
546 gerr = gcry_mpi_print(GCRYMPI_FMT_USG, sig->data, SIGSPACE, &sig->len, m_sig);
549 debug_printf_indent("signature conversion from MPI to buffer failed\n");
550 return US gcry_strerror(gerr);
558 /* import public key (from blob in memory)
559 Return: NULL for success, or an error string */
562 exim_dkim_verify_init(blob * pubkey, keyformat fmt, ev_ctx * verify_ctx,
566 in code sequence per b81207d2bfa92 rsa_parse_public_key() and asn1_get_mpi()
575 uschar * stage = US"S1";
577 if (fmt != KEYFMT_DER) return US"pubkey format not handled";
584 BIT STRING:RSAPublicKey
586 INTEGER:Public modulus
587 INTEGER:Public exponent
589 openssl rsa -in aux-fixed/dkim/dkim.private -pubout -outform DER | od -t x1 | head;
590 openssl rsa -in aux-fixed/dkim/dkim.private -pubout | openssl asn1parse -dump;
591 openssl rsa -in aux-fixed/dkim/dkim.private -pubout | openssl asn1parse -dump -offset 22;
594 /* sequence; just move past the header */
595 if ((rc = as_tag(pubkey, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, NULL))
596 != ASN1_SUCCESS) goto asn_err;
598 /* sequence; skip the entire thing */
599 DEBUG(D_acl) stage = US"S2";
600 if ((rc = as_tag(pubkey, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, &alen))
601 != ASN1_SUCCESS) goto asn_err;
602 pubkey->data += alen; pubkey->len -= alen;
605 /* bitstring: limit range to size of bitstring;
606 move over header + content wrapper */
607 DEBUG(D_acl) stage = US"BS";
608 if ((rc = as_tag(pubkey, 0, ASN1_TAG_BIT_STRING, &alen)) != ASN1_SUCCESS)
611 pubkey->data++; pubkey->len--;
613 /* sequence; just move past the header */
614 DEBUG(D_acl) stage = US"S3";
615 if ((rc = as_tag(pubkey, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, NULL))
616 != ASN1_SUCCESS) goto asn_err;
618 /* read two integers */
619 DEBUG(D_acl) stage = US"MPI";
621 if ((errstr = as_mpi(pubkey, &verify_ctx->n))) return errstr;
622 nbits = (nbits - pubkey->len) * 8;
623 if ((errstr = as_mpi(pubkey, &verify_ctx->e))) return errstr;
626 DEBUG(D_acl) debug_printf_indent("rsa_verify_init:\n");
629 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, verify_ctx->n);
630 debug_printf_indent(" N : %s\n", s);
631 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, verify_ctx->e);
632 debug_printf_indent(" E : %s\n", s);
636 if (bits) *bits = nbits;
640 DEBUG(D_acl) return string_sprintf("%s: %s", stage, asn1_strerror(rc));
641 return US asn1_strerror(rc);
645 /* verify signature (of hash)
646 XXX though we appear to be doing a hash, too!
647 (given pubkey & alleged sig)
648 Return: NULL for success, or an error string */
651 exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data_hash, blob * sig)
654 cf. libgnutls 2.8.5 _wrap_gcry_pk_verify()
658 gcry_sexp_t s_sig = NULL, s_hash = NULL, s_pkey = NULL;
662 /*XXX needs extension for SHA512 */
665 case HASH_SHA1: sexp_hash = "(data(flags pkcs1)(hash sha1 %b))"; break;
666 case HASH_SHA2_256: sexp_hash = "(data(flags pkcs1)(hash sha256 %b))"; break;
667 default: return US"nonhandled hash type";
670 if ( (stage = US"pkey sexp build",
671 gerr = gcry_sexp_build (&s_pkey, NULL, "(public-key(rsa(n%m)(e%m)))",
672 verify_ctx->n, verify_ctx->e))
673 || (stage = US"data sexp build",
674 gerr = gcry_sexp_build (&s_hash, NULL, sexp_hash,
675 (int) data_hash->len, CS data_hash->data))
676 || (stage = US"sig mpi scan",
677 gerr = gcry_mpi_scan(&m_sig, GCRYMPI_FMT_USG, sig->data, sig->len, NULL))
678 || (stage = US"sig sexp build",
679 gerr = gcry_sexp_build (&s_sig, NULL, "(sig-val(rsa(s%m)))", m_sig))
680 || (stage = US"verify",
681 gerr = gcry_pk_verify (s_sig, s_hash, s_pkey))
684 DEBUG(D_acl) debug_printf_indent("verify: error in stage '%s'\n", stage);
685 return US gcry_strerror(gerr);
688 if (s_sig) gcry_sexp_release (s_sig);
689 if (s_hash) gcry_sexp_release (s_hash);
690 if (s_pkey) gcry_sexp_release (s_pkey);
691 gcry_mpi_release (m_sig);
692 gcry_mpi_release (verify_ctx->n);
693 gcry_mpi_release (verify_ctx->e);
701 #elif defined(SIGN_OPENSSL)
702 /******************************************************************************/
707 ERR_load_crypto_strings();
711 /* accumulate data (was gnutls-only but now needed for OpenSSL non-EC too
712 because now using hash-and-sign interface) */
714 exim_dkim_data_append(gstring * g, uschar * s)
716 return string_cat(g, s);
720 /* import private key from PEM string in memory.
721 Return: NULL for success, or an error string */
724 exim_dkim_signing_init(const uschar * privkey_pem, es_ctx * sign_ctx)
726 BIO * bp = BIO_new_mem_buf((void *)privkey_pem, -1);
728 if (!(sign_ctx->key = PEM_read_bio_PrivateKey(bp, NULL, NULL, NULL)))
729 return string_sprintf("privkey PEM-block import: %s",
730 ERR_error_string(ERR_get_error(), NULL));
733 #ifdef SIGN_HAVE_ED25519
734 EVP_PKEY_type(EVP_PKEY_id(sign_ctx->key)) == EVP_PKEY_ED25519
735 ? KEYTYPE_ED25519 : KEYTYPE_RSA;
744 /* allocate mem for signature (when signing) */
745 /* hash & sign data. Incremental not supported.
747 Return: NULL for success with the signaature in the sig blob, or an error string */
750 exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig)
758 case HASH_NULL: md = NULL; break; /* Ed25519 signing */
759 case HASH_SHA1: md = EVP_sha1(); break;
760 case HASH_SHA2_256: md = EVP_sha256(); break;
761 case HASH_SHA2_512: md = EVP_sha512(); break;
762 default: return US"nonhandled hash type";
765 #ifdef SIGN_HAVE_ED25519
766 if ( (ctx = EVP_MD_CTX_new())
767 && EVP_DigestSignInit(ctx, NULL, md, NULL, sign_ctx->key) > 0
768 && EVP_DigestSign(ctx, NULL, &siglen, NULL, 0) > 0
769 && (sig->data = store_get(siglen, GET_UNTAINTED))
771 /* Obtain the signature (slen could change here!) */
772 && EVP_DigestSign(ctx, sig->data, &siglen, data->data, data->len) > 0
775 EVP_MD_CTX_destroy(ctx);
780 /*XXX renamed to EVP_MD_CTX_new() in 1.1.0 */
781 if ( (ctx = EVP_MD_CTX_create())
782 && EVP_DigestSignInit(ctx, NULL, md, NULL, sign_ctx->key) > 0
783 && EVP_DigestSignUpdate(ctx, data->data, data->len) > 0
784 && EVP_DigestSignFinal(ctx, NULL, &siglen) > 0
785 && (sig->data = store_get(siglen, GET_UNTAINTED))
787 /* Obtain the signature (slen could change here!) */
788 && EVP_DigestSignFinal(ctx, sig->data, &siglen) > 0
791 EVP_MD_CTX_destroy(ctx);
797 if (ctx) EVP_MD_CTX_destroy(ctx);
798 return US ERR_error_string(ERR_get_error(), NULL);
803 /* import public key (from blob in memory)
804 Return: NULL for success, or an error string */
807 exim_dkim_verify_init(blob * pubkey, keyformat fmt, ev_ctx * verify_ctx,
810 const uschar * s = pubkey->data;
816 /*XXX hmm, we never free this */
817 if (!(verify_ctx->key = d2i_PUBKEY(NULL, &s, pubkey->len)))
818 ret = US ERR_error_string(ERR_get_error(), NULL);
820 #ifdef SIGN_HAVE_ED25519
821 case KEYFMT_ED25519_BARE:
822 if (!(verify_ctx->key = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL,
824 ret = US ERR_error_string(ERR_get_error(), NULL);
828 ret = US"pubkey format not handled";
832 if (!ret && bits) *bits = EVP_PKEY_bits(verify_ctx->key);
839 /* verify signature (of hash, except Ed25519 where of-data)
840 (given pubkey & alleged sig)
841 Return: NULL for success, or an error string */
844 exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data, blob * sig)
850 case HASH_NULL: md = NULL; break;
851 case HASH_SHA1: md = EVP_sha1(); break;
852 case HASH_SHA2_256: md = EVP_sha256(); break;
853 case HASH_SHA2_512: md = EVP_sha512(); break;
854 default: return US"nonhandled hash type";
857 #ifdef SIGN_HAVE_ED25519
862 if ((ctx = EVP_MD_CTX_new()))
864 if ( EVP_DigestVerifyInit(ctx, NULL, md, NULL, verify_ctx->key) > 0
865 && EVP_DigestVerify(ctx, sig->data, sig->len, data->data, data->len) > 0
867 { EVP_MD_CTX_free(ctx); return NULL; }
868 EVP_MD_CTX_free(ctx);
876 if ((ctx = EVP_PKEY_CTX_new(verify_ctx->key, NULL)))
878 if ( EVP_PKEY_verify_init(ctx) > 0
879 && EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) > 0
880 && EVP_PKEY_CTX_set_signature_md(ctx, md) > 0
881 && EVP_PKEY_verify(ctx, sig->data, sig->len,
882 data->data, data->len) == 1
884 { EVP_PKEY_CTX_free(ctx); return NULL; }
885 EVP_PKEY_CTX_free(ctx);
888 if (Ustrcmp(ERR_reason_error_string(ERR_peek_error()), "wrong signature length") == 0)
889 debug_printf("sig len (from msg hdr): %d, expected (from dns pubkey) %d\n",
890 (int) sig->len, EVP_PKEY_size(verify_ctx->key));
894 return US ERR_error_string(ERR_get_error(), NULL);
900 /******************************************************************************/
902 #endif /*DISABLE_DKIM*/
903 #endif /*MACRO_PREDEF*/