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