2 * PDKIM - a RFC4871 (DKIM) implementation
3 * Copyright (c) The Exim Maintainers 1995 - 2024
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");
67 exim_dkim_signers_init(void)
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.
129 sign_ctx library-specific context for a signature (incl. key)
130 hash hash method to apply to data
131 data data to be signed
132 sig returned signature
134 Return: NULL for success, or an error string */
137 exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, const blob * data, blob * sig)
139 gnutls_datum_t k_data = { .data = data->data, .size = data->len };
140 gnutls_digest_algorithm_t dig;
141 gnutls_datum_t k_sig;
146 case HASH_SHA1: dig = GNUTLS_DIG_SHA1; break;
147 case HASH_SHA2_256: dig = GNUTLS_DIG_SHA256; break;
148 case HASH_SHA2_512: dig = GNUTLS_DIG_SHA512; break;
149 default: return US"nonhandled hash type";
152 if ((rc = gnutls_privkey_sign_data(sign_ctx->key, dig, 0, &k_data, &k_sig)))
153 return CUS gnutls_strerror(rc);
155 /* Don't care about deinit for the key; shortlived process */
157 sig->data = k_sig.data;
158 sig->len = k_sig.size;
164 /* import public key (from blob in memory)
165 Return: NULL for success, or an error string */
168 exim_dkim_verify_init(const blob * pubkey, keyformat fmt, ev_ctx * verify_ctx,
173 const uschar * ret = NULL;
175 gnutls_pubkey_init(&verify_ctx->key);
176 k.data = pubkey->data;
177 k.size = pubkey->len;
182 if ((rc = gnutls_pubkey_import(verify_ctx->key, &k, GNUTLS_X509_FMT_DER)))
183 ret = US gnutls_strerror(rc);
185 #ifdef SIGN_HAVE_ED25519
186 case KEYFMT_ED25519_BARE:
187 if ((rc = gnutls_pubkey_import_ecc_raw(verify_ctx->key,
188 GNUTLS_ECC_CURVE_ED25519, &k, NULL)))
189 ret = US gnutls_strerror(rc);
193 ret = US"pubkey format not handled";
196 if (!ret && bits) gnutls_pubkey_get_pk_algorithm(verify_ctx->key, bits);
201 /* verify signature (of hash if RSA sig, of data if EC sig. No way to do incremental)
202 (given pubkey & alleged sig)
203 Return: NULL for success, or an error string */
206 exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, const blob * data_hash,
209 gnutls_datum_t k = { .data = data_hash->data, .size = data_hash->len };
210 gnutls_datum_t s = { .data = sig->data, .size = sig->len };
212 const uschar * ret = NULL;
214 #ifdef SIGN_HAVE_ED25519
215 if (verify_ctx->keytype == KEYTYPE_ED25519)
217 if ((rc = gnutls_pubkey_verify_data2(verify_ctx->key,
218 GNUTLS_SIGN_EDDSA_ED25519, 0, &k, &s)) < 0)
219 ret = US gnutls_strerror(rc);
224 gnutls_sign_algorithm_t algo;
227 case HASH_SHA1: algo = GNUTLS_SIGN_RSA_SHA1; break;
228 case HASH_SHA2_256: algo = GNUTLS_SIGN_RSA_SHA256; break;
229 case HASH_SHA2_512: algo = GNUTLS_SIGN_RSA_SHA512; break;
230 default: return US"nonhandled hash type";
233 if ((rc = gnutls_pubkey_verify_hash2(verify_ctx->key, algo,
234 GNUTLS_VERIFY_ALLOW_BROKEN, &k, &s)) < 0)
235 ret = US gnutls_strerror(rc);
238 gnutls_pubkey_deinit(verify_ctx->key);
245 #elif defined(SIGN_GCRYPT)
246 /******************************************************************************/
247 /* This variant is used under pre-3.0.0 GnuTLS. Only rsa-sha1 and rsa-sha256 */
250 /* Internal service routine:
251 Read and move past an asn.1 header, checking class & tag,
252 optionally returning the data-length */
255 as_tag(blob * der, uschar req_cls, long req_tag, long * alen)
262 debug_printf_indent("as_tag: %02x %02x %02x %02x\n",
263 der->data[0], der->data[1], der->data[2], der->data[3]);
265 if ((rc = asn1_get_tag_der(der->data++, der->len--, &tag_class, &taglen, &tag))
269 if (tag_class != req_cls || tag != req_tag) return ASN1_ELEMENT_NOT_FOUND;
271 if ((len = asn1_get_length_der(der->data, der->len, &taglen)) < 0)
272 return ASN1_DER_ERROR;
273 if (alen) *alen = len;
275 /* debug_printf_indent("as_tag: tlen %d dlen %d\n", taglen, (int)len); */
282 /* Internal service routine:
283 Read and move over an asn.1 integer, setting an MPI to the value
287 as_mpi(blob * der, gcry_mpi_t * mpi)
293 debug_printf_indent("%s\n", __FUNCTION__);
295 /* integer; move past the header */
296 if ((rc = as_tag(der, 0, ASN1_TAG_INTEGER, &alen)) != ASN1_SUCCESS)
297 return US asn1_strerror(rc);
300 if ((gerr = gcry_mpi_scan(mpi, GCRYMPI_FMT_STD, der->data, alen, NULL)))
301 return US gcry_strerror(gerr);
303 /* move over the data */
304 der->data += alen; der->len -= alen;
311 exim_dkim_signers_init(void)
313 /* Version check should be the very first call because it
314 makes sure that important subsystems are initialized. */
315 if (!gcry_check_version (GCRYPT_VERSION))
317 fputs ("libgcrypt version mismatch\n", stderr);
321 /* We don't want to see any warnings, e.g. because we have not yet
322 parsed program options which might be used to suppress such
324 gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
326 /* ... If required, other initialization goes here. Note that the
327 process might still be running with increased privileges and that
328 the secure memory has not been initialized. */
330 /* Allocate a pool of 16k secure memory. This make the secure memory
331 available and also drops privileges where needed. */
332 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
334 /* It is now okay to let Libgcrypt complain when there was/is
335 a problem with the secure memory. */
336 gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
338 /* ... If required, other initialization goes here. */
340 /* Tell Libgcrypt that initialization has completed. */
341 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
349 /* Accumulate data (gnutls-only).
350 String to be appended must be nul-terminated. */
353 exim_dkim_data_append(gstring * g, uschar * s)
360 /* import private key from PEM string in memory.
361 Only handles RSA keys.
362 Return: NULL for success, or an error string */
365 exim_dkim_signing_init(const uschar * privkey_pem, es_ctx * sign_ctx)
372 /*XXX will need extension to _spot_ as well as handle a
373 non-RSA key? I think...
374 So... this is not a PrivateKeyInfo - which would have a field
375 identifying the keytype - PrivateKeyAlgorithmIdentifier -
376 but a plain RSAPrivateKey (wrapped in PEM-headers. Can we
377 use those as a type tag? What forms are there? "BEGIN EC PRIVATE KEY" (cf. ec(1ssl))
379 How does OpenSSL PEM_read_bio_PrivateKey() deal with it?
380 gnutls_x509_privkey_import() ?
384 * RSAPrivateKey ::= SEQUENCE
386 * modulus INTEGER, -- n
387 * publicExponent INTEGER, -- e
388 * privateExponent INTEGER, -- d
389 * prime1 INTEGER, -- p
390 * prime2 INTEGER, -- q
391 * exponent1 INTEGER, -- d mod (p-1)
392 * exponent2 INTEGER, -- d mod (q-1)
393 * coefficient INTEGER, -- (inverse of q) mod p
394 * otherPrimeInfos OtherPrimeInfos OPTIONAL
396 * ECPrivateKey ::= SEQUENCE {
397 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
398 * privateKey OCTET STRING,
399 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
400 * publicKey [1] BIT STRING OPTIONAL
402 * Hmm, only 1 useful item, and not even an integer? Wonder how we might use it...
404 - actually, gnutls_x509_privkey_import() appears to require a curve name parameter
405 value for that is an OID? a local-only integer (it's an enum in GnuTLS)?
409 ssh-keygen -t ecdsa -f foo.privkey
410 ssh-keygen -t ecdsa -b384 -f foo.privkey
411 ssh-keygen -t ecdsa -b521 -f foo.privkey
412 ssh-keygen -t ed25519 -f foo.privkey
414 < foo openssl pkcs8 -in /dev/stdin -inform PEM -nocrypt -topk8 -outform DER | od -x
416 openssl asn1parse -in foo -inform PEM -dump
417 openssl asn1parse -in foo -inform PEM -dump -stroffset 24 (??)
418 (not good for ed25519)
422 if ( !(s1 = Ustrstr(CS privkey_pem, "-----BEGIN RSA PRIVATE KEY-----"))
423 || !(s2 = Ustrstr(CS (s1+=31), "-----END RSA PRIVATE KEY-----" ))
425 return US"Bad PEM wrapper";
429 if ((rc = b64decode(s1, &der.data, s1) < 0))
430 return US"Bad PEM-DER b64 decode";
435 /* sequence; just move past the header */
436 if ((rc = as_tag(&der, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, NULL))
437 != ASN1_SUCCESS) goto asn_err;
439 /* integer version; move past the header, check is zero */
440 if ((rc = as_tag(&der, 0, ASN1_TAG_INTEGER, &alen)) != ASN1_SUCCESS)
442 if (alen != 1 || *der.data != 0)
443 return US"Bad version number";
444 der.data++; der.len--;
446 if ( (s1 = as_mpi(&der, &sign_ctx->n))
447 || (s1 = as_mpi(&der, &sign_ctx->e))
448 || (s1 = as_mpi(&der, &sign_ctx->d))
449 || (s1 = as_mpi(&der, &sign_ctx->p))
450 || (s1 = as_mpi(&der, &sign_ctx->q))
451 || (s1 = as_mpi(&der, &sign_ctx->dp))
452 || (s1 = as_mpi(&der, &sign_ctx->dq))
453 || (s1 = as_mpi(&der, &sign_ctx->qp))
458 DEBUG(D_acl) debug_printf_indent("rsa_signing_init:\n");
461 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->n);
462 debug_printf_indent(" N : %s\n", s);
463 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->e);
464 debug_printf_indent(" E : %s\n", s);
465 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->d);
466 debug_printf_indent(" D : %s\n", s);
467 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->p);
468 debug_printf_indent(" P : %s\n", s);
469 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->q);
470 debug_printf_indent(" Q : %s\n", s);
471 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->dp);
472 debug_printf_indent(" DP: %s\n", s);
473 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->dq);
474 debug_printf_indent(" DQ: %s\n", s);
475 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, sign_ctx->qp);
476 debug_printf_indent(" QP: %s\n", s);
480 sign_ctx->keytype = KEYTYPE_RSA;
483 asn_err: return US asn1_strerror(rc);
488 /* allocate mem for signature (when signing) */
489 /* sign already-hashed data.
491 Return: NULL for success, or an error string */
494 exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, const blob * data, blob * sig)
497 gcry_sexp_t s_hash = NULL, s_key = NULL, s_sig = NULL;
502 /*XXX will need extension for hash types (though, possibly, should
503 be re-specced to not rehash but take an already-hashed value? Actually
504 current impl looks WRONG - it _is_ given a hash so should not be
505 re-hashing. Has this been tested?
507 Will need extension for non-RSA sugning algos. */
511 case HASH_SHA1: sexp_hash = "(data(flags pkcs1)(hash sha1 %b))"; break;
512 case HASH_SHA2_256: sexp_hash = "(data(flags pkcs1)(hash sha256 %b))"; break;
513 default: return US"nonhandled hash type";
517 sig->data = store_get(SIGSPACE, GET_UNTAINTED);
519 if (gcry_mpi_cmp (sign_ctx->p, sign_ctx->q) > 0)
521 gcry_mpi_swap (sign_ctx->p, sign_ctx->q);
522 gcry_mpi_invm (sign_ctx->qp, sign_ctx->p, sign_ctx->q);
525 if ( (gerr = gcry_sexp_build (&s_key, NULL,
526 "(private-key (rsa (n%m)(e%m)(d%m)(p%m)(q%m)(u%m)))",
527 sign_ctx->n, sign_ctx->e,
528 sign_ctx->d, sign_ctx->p,
529 sign_ctx->q, sign_ctx->qp))
530 || (gerr = gcry_sexp_build (&s_hash, NULL, sexp_hash,
531 (int) data->len, CS data->data))
532 || (gerr = gcry_pk_sign (&s_sig, s_hash, s_key))
534 return US gcry_strerror(gerr);
536 /* gcry_sexp_dump(s_sig); */
538 if ( !(s_sig = gcry_sexp_find_token(s_sig, "s", 0))
540 return US"no sig result";
542 m_sig = gcry_sexp_nth_mpi(s_sig, 1, GCRYMPI_FMT_USG);
548 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, m_sig);
549 debug_printf_indent(" SG: %s\n", s);
553 gerr = gcry_mpi_print(GCRYMPI_FMT_USG, sig->data, SIGSPACE, &sig->len, m_sig);
556 debug_printf_indent("signature conversion from MPI to buffer failed\n");
557 return US gcry_strerror(gerr);
565 /* import public key (from blob in memory)
566 Return: NULL for success, or an error string */
569 exim_dkim_verify_init(const blob * pubkey, keyformat fmt, ev_ctx * verify_ctx,
573 in code sequence per b81207d2bfa92 rsa_parse_public_key() and asn1_get_mpi()
582 uschar * stage = US"S1";
584 if (fmt != KEYFMT_DER) return US"pubkey format not handled";
591 BIT STRING:RSAPublicKey
593 INTEGER:Public modulus
594 INTEGER:Public exponent
596 openssl rsa -in aux-fixed/dkim/dkim.private -pubout -outform DER | od -t x1 | head;
597 openssl rsa -in aux-fixed/dkim/dkim.private -pubout | openssl asn1parse -dump;
598 openssl rsa -in aux-fixed/dkim/dkim.private -pubout | openssl asn1parse -dump -offset 22;
601 /* sequence; just move past the header */
602 if ((rc = as_tag(pubkey, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, NULL))
603 != ASN1_SUCCESS) goto asn_err;
605 /* sequence; skip the entire thing */
606 DEBUG(D_acl) stage = US"S2";
607 if ((rc = as_tag(pubkey, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, &alen))
608 != ASN1_SUCCESS) goto asn_err;
609 pubkey->data += alen; pubkey->len -= alen;
612 /* bitstring: limit range to size of bitstring;
613 move over header + content wrapper */
614 DEBUG(D_acl) stage = US"BS";
615 if ((rc = as_tag(pubkey, 0, ASN1_TAG_BIT_STRING, &alen)) != ASN1_SUCCESS)
618 pubkey->data++; pubkey->len--;
620 /* sequence; just move past the header */
621 DEBUG(D_acl) stage = US"S3";
622 if ((rc = as_tag(pubkey, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, NULL))
623 != ASN1_SUCCESS) goto asn_err;
625 /* read two integers */
626 DEBUG(D_acl) stage = US"MPI";
628 if ((errstr = as_mpi(pubkey, &verify_ctx->n))) return errstr;
629 nbits = (nbits - pubkey->len) * 8;
630 if ((errstr = as_mpi(pubkey, &verify_ctx->e))) return errstr;
633 DEBUG(D_acl) debug_printf_indent("rsa_verify_init:\n");
636 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, verify_ctx->n);
637 debug_printf_indent(" N : %s\n", s);
638 gcry_mpi_aprint (GCRYMPI_FMT_HEX, &s, NULL, verify_ctx->e);
639 debug_printf_indent(" E : %s\n", s);
643 if (bits) *bits = nbits;
647 DEBUG(D_acl) return string_sprintf("%s: %s", stage, asn1_strerror(rc));
648 return US asn1_strerror(rc);
652 /* verify signature (of hash)
653 XXX though we appear to be doing a hash, too!
654 (given pubkey & alleged sig)
655 Return: NULL for success, or an error string */
658 exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, const blob * data_hash,
662 cf. libgnutls 2.8.5 _wrap_gcry_pk_verify()
666 gcry_sexp_t s_sig = NULL, s_hash = NULL, s_pkey = NULL;
670 /*XXX needs extension for SHA512 */
673 case HASH_SHA1: sexp_hash = "(data(flags pkcs1)(hash sha1 %b))"; break;
674 case HASH_SHA2_256: sexp_hash = "(data(flags pkcs1)(hash sha256 %b))"; break;
675 default: return US"nonhandled hash type";
678 if ( (stage = US"pkey sexp build",
679 gerr = gcry_sexp_build (&s_pkey, NULL, "(public-key(rsa(n%m)(e%m)))",
680 verify_ctx->n, verify_ctx->e))
681 || (stage = US"data sexp build",
682 gerr = gcry_sexp_build (&s_hash, NULL, sexp_hash,
683 (int) data_hash->len, CS data_hash->data))
684 || (stage = US"sig mpi scan",
685 gerr = gcry_mpi_scan(&m_sig, GCRYMPI_FMT_USG, sig->data, sig->len, NULL))
686 || (stage = US"sig sexp build",
687 gerr = gcry_sexp_build (&s_sig, NULL, "(sig-val(rsa(s%m)))", m_sig))
688 || (stage = US"verify",
689 gerr = gcry_pk_verify (s_sig, s_hash, s_pkey))
692 DEBUG(D_acl) debug_printf_indent("verify: error in stage '%s'\n", stage);
693 return US gcry_strerror(gerr);
696 if (s_sig) gcry_sexp_release (s_sig);
697 if (s_hash) gcry_sexp_release (s_hash);
698 if (s_pkey) gcry_sexp_release (s_pkey);
699 gcry_mpi_release (m_sig);
700 gcry_mpi_release (verify_ctx->n);
701 gcry_mpi_release (verify_ctx->e);
709 #elif defined(SIGN_OPENSSL)
710 /******************************************************************************/
713 exim_dkim_signers_init(void)
715 ERR_load_crypto_strings();
719 /* accumulate data (was gnutls-only but now needed for OpenSSL non-EC too
720 because now using hash-and-sign interface) */
722 exim_dkim_data_append(gstring * g, uschar * s)
724 return string_cat(g, s);
728 /* import private key from PEM string in memory.
729 Return: NULL for success, or an error string */
732 exim_dkim_signing_init(const uschar * privkey_pem, es_ctx * sign_ctx)
734 BIO * bp = BIO_new_mem_buf((void *)privkey_pem, -1);
736 if (!(sign_ctx->key = PEM_read_bio_PrivateKey(bp, NULL, NULL, NULL)))
737 return string_sprintf("privkey PEM-block import: %s",
738 ERR_error_string(ERR_get_error(), NULL));
741 #ifdef SIGN_HAVE_ED25519
742 EVP_PKEY_type(EVP_PKEY_id(sign_ctx->key)) == EVP_PKEY_ED25519
743 ? KEYTYPE_ED25519 : KEYTYPE_RSA;
752 /* allocate mem for signature (when signing) */
753 /* hash & sign data. Incremental not supported.
755 Return: NULL for success with the signaature in the sig blob, or an error string */
758 exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, const blob * data, blob * sig)
766 case HASH_NULL: md = NULL; break; /* Ed25519 signing */
767 case HASH_SHA1: md = EVP_sha1(); break;
768 case HASH_SHA2_256: md = EVP_sha256(); break;
769 case HASH_SHA2_512: md = EVP_sha512(); break;
770 default: return US"nonhandled hash type";
773 #ifdef SIGN_HAVE_ED25519
774 if ( (ctx = EVP_MD_CTX_new())
775 && EVP_DigestSignInit(ctx, NULL, md, NULL, sign_ctx->key) > 0
776 && EVP_DigestSign(ctx, NULL, &siglen, NULL, 0) > 0
777 && (sig->data = store_get(siglen, GET_UNTAINTED))
779 /* Obtain the signature (slen could change here!) */
780 && EVP_DigestSign(ctx, sig->data, &siglen, data->data, data->len) > 0
783 EVP_MD_CTX_destroy(ctx);
788 /*XXX renamed to EVP_MD_CTX_new() in 1.1.0 */
789 if ( (ctx = EVP_MD_CTX_create())
790 && EVP_DigestSignInit(ctx, NULL, md, NULL, sign_ctx->key) > 0
791 && EVP_DigestSignUpdate(ctx, data->data, data->len) > 0
792 && EVP_DigestSignFinal(ctx, NULL, &siglen) > 0
793 && (sig->data = store_get(siglen, GET_UNTAINTED))
795 /* Obtain the signature (slen could change here!) */
796 && EVP_DigestSignFinal(ctx, sig->data, &siglen) > 0
799 EVP_MD_CTX_destroy(ctx);
805 if (ctx) EVP_MD_CTX_destroy(ctx);
806 return US ERR_error_string(ERR_get_error(), NULL);
811 /* import public key (from blob in memory)
812 Return: NULL for success, or an error string */
815 exim_dkim_verify_init(const blob * pubkey, keyformat fmt, ev_ctx * verify_ctx,
818 const uschar * s = pubkey->data;
824 /*XXX hmm, we never free this */
825 if (!(verify_ctx->key = d2i_PUBKEY(NULL, &s, pubkey->len)))
826 ret = US ERR_error_string(ERR_get_error(), NULL);
828 #ifdef SIGN_HAVE_ED25519
829 case KEYFMT_ED25519_BARE:
830 if (!(verify_ctx->key = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL,
832 ret = US ERR_error_string(ERR_get_error(), NULL);
836 ret = US"pubkey format not handled";
840 if (!ret && bits) *bits = EVP_PKEY_bits(verify_ctx->key);
847 /* verify signature (of hash, except Ed25519 where of-data)
848 (given pubkey & alleged sig)
849 Return: NULL for success, or an error string */
852 exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, const blob * data,
859 case HASH_NULL: md = NULL; break;
860 case HASH_SHA1: md = EVP_sha1(); break;
861 case HASH_SHA2_256: md = EVP_sha256(); break;
862 case HASH_SHA2_512: md = EVP_sha512(); break;
863 default: return US"nonhandled hash type";
866 #ifdef SIGN_HAVE_ED25519
871 if ((ctx = EVP_MD_CTX_new()))
873 if ( EVP_DigestVerifyInit(ctx, NULL, md, NULL, verify_ctx->key) > 0
874 && EVP_DigestVerify(ctx, sig->data, sig->len, data->data, data->len) > 0
876 { EVP_MD_CTX_free(ctx); return NULL; }
877 EVP_MD_CTX_free(ctx);
885 if ((ctx = EVP_PKEY_CTX_new(verify_ctx->key, NULL)))
887 if ( EVP_PKEY_verify_init(ctx) > 0
888 && EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) > 0
889 && EVP_PKEY_CTX_set_signature_md(ctx, md) > 0
890 && EVP_PKEY_verify(ctx, sig->data, sig->len,
891 data->data, data->len) == 1
893 { EVP_PKEY_CTX_free(ctx); return NULL; }
894 EVP_PKEY_CTX_free(ctx);
897 if (Ustrcmp(ERR_reason_error_string(ERR_peek_error()), "wrong signature length") == 0)
898 debug_printf("sig len (from msg hdr): %d, expected (from dns pubkey) %d\n",
899 (int) sig->len, EVP_PKEY_size(verify_ctx->key));
903 return US ERR_error_string(ERR_get_error(), NULL);
909 /******************************************************************************/
911 #endif /*DISABLE_DKIM*/
912 #endif /*MACRO_PREDEF*/