Fix mariadb/mysql macro confusion
[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
349d0869 341#ifdef extreme_debug
6e4aaa85 342DEBUG(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
363return NULL;
364
365asn_err: return US asn1_strerror(rc);
366}
367
368
369
370/* allocate mem for signature (when signing) */
371/* sign data (gnutls_only)
372OR
373sign hash.
374
375Return: NULL for success, or an error string */
376
377const uschar *
d73e45df 378exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig)
2592e6c0 379{
349d0869 380char * sexp_hash;
2592e6c0
JH
381gcry_sexp_t s_hash = NULL, s_key = NULL, s_sig = NULL;
382gcry_mpi_t m_sig;
383uschar * errstr;
384gcry_error_t gerr;
385
9b2583c4
JH
386/*XXX will need extension for hash types (though, possibly, should
387be re-specced to not rehash but take an already-hashed value? Actually
d73e45df 388current impl looks WRONG - it _is_ given a hash so should not be
9b2583c4
JH
389re-hashing. Has this been tested?
390
391Will need extension for non-RSA sugning algos. */
392
d73e45df
JH
393switch (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
401sig->data = store_get(SIGSPACE);
402
403if (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
409if ( (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
422if ( !(s_sig = gcry_sexp_find_token(s_sig, "s", 0))
423 )
424 return US"no sig result";
425
426m_sig = gcry_sexp_nth_mpi(s_sig, 1, GCRYMPI_FMT_USG);
427
349d0869 428#ifdef extreme_debug
2592e6c0
JH
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 434 }
349d0869 435#endif
2592e6c0
JH
436
437gerr = gcry_mpi_print(GCRYMPI_FMT_USG, sig->data, SIGSPACE, &sig->len, m_sig);
438if (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
445return NULL;
446}
447
448
449/* import public key (from DER in memory)
450Return: NULL for success, or an error string */
451
452const uschar *
9b2583c4 453exim_dkim_verify_init(blob * pubkey_der, ev_ctx * verify_ctx)
2592e6c0
JH
454{
455/*
456in code sequence per b81207d2bfa92 rsa_parse_public_key() and asn1_get_mpi()
457*/
458uschar tag_class;
459int taglen;
460long alen;
461int rc;
462uschar * errstr;
463gcry_error_t gerr;
464uschar * stage = US"S1";
465
466/*
467sequence
468 sequence
469 OBJECT:rsaEncryption
470 NULL
471 BIT STRING:RSAPublicKey
472 sequence
473 INTEGER:Public modulus
474 INTEGER:Public exponent
475
476openssl rsa -in aux-fixed/dkim/dkim.private -pubout -outform DER | od -t x1 | head;
477openssl rsa -in aux-fixed/dkim/dkim.private -pubout | openssl asn1parse -dump;
478openssl rsa -in aux-fixed/dkim/dkim.private -pubout | openssl asn1parse -dump -offset 22;
479*/
480
481/* sequence; just move past the header */
482if ((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 */
486DEBUG(D_acl) stage = US"S2";
487if ((rc = as_tag(pubkey_der, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, &alen))
488 != ASN1_SUCCESS) goto asn_err;
489pubkey_der->data += alen; pubkey_der->len -= alen;
490
491
492/* bitstring: limit range to size of bitstring;
493move over header + content wrapper */
494DEBUG(D_acl) stage = US"BS";
495if ((rc = as_tag(pubkey_der, 0, ASN1_TAG_BIT_STRING, &alen)) != ASN1_SUCCESS)
496 goto asn_err;
497pubkey_der->len = alen;
498pubkey_der->data++; pubkey_der->len--;
499
500/* sequence; just move past the header */
501DEBUG(D_acl) stage = US"S3";
502if ((rc = as_tag(pubkey_der, ASN1_CLASS_STRUCTURED, ASN1_TAG_SEQUENCE, NULL))
503 != ASN1_SUCCESS) goto asn_err;
504
505/* read two integers */
506DEBUG(D_acl) stage = US"MPI";
507if ( (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 513DEBUG(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
523return NULL;
524
525asn_err:
526DEBUG(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)
532Return: NULL for success, or an error string */
533
534const uschar *
d73e45df 535exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data_hash, blob * sig)
2592e6c0
JH
536{
537/*
538cf. libgnutls 2.8.5 _wrap_gcry_pk_verify()
539*/
349d0869 540char * sexp_hash;
2592e6c0
JH
541gcry_mpi_t m_sig;
542gcry_sexp_t s_sig = NULL, s_hash = NULL, s_pkey = NULL;
543gcry_error_t gerr;
544uschar * stage;
545
349d0869 546/*XXX needs extension for SHA512 */
d73e45df
JH
547switch (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
554if ( (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
572if (s_sig) gcry_sexp_release (s_sig);
573if (s_hash) gcry_sexp_release (s_hash);
574if (s_pkey) gcry_sexp_release (s_pkey);
575gcry_mpi_release (m_sig);
576gcry_mpi_release (verify_ctx->n);
577gcry_mpi_release (verify_ctx->e);
578
579return NULL;
580}
581
582
583
584
d73e45df 585#elif defined(SIGN_OPENSSL)
2592e6c0
JH
586/******************************************************************************/
587
588void
9b2583c4 589exim_dkim_init(void)
2592e6c0 590{
9e70917d 591ERR_load_crypto_strings();
2592e6c0
JH
592}
593
594
595/* accumulate data (gnutls-only) */
596blob *
9b2583c4 597exim_dkim_data_append(blob * b, int * alloc, uschar * s)
2592e6c0
JH
598{
599return b; /*dummy*/
600}
601
602
603/* import private key from PEM string in memory.
604Return: NULL for success, or an error string */
605
606const uschar *
9b2583c4 607exim_dkim_signing_init(uschar * privkey_pem, es_ctx * sign_ctx)
2592e6c0 608{
d73e45df 609BIO * bp = BIO_new_mem_buf(privkey_pem, -1);
2592e6c0 610
d73e45df 611if (!(sign_ctx->key = PEM_read_bio_PrivateKey(bp, NULL, NULL, NULL)))
7845dbb3 612 return US ERR_error_string(ERR_get_error(), NULL);
2592e6c0
JH
613return NULL;
614}
615
616
617
618/* allocate mem for signature (when signing) */
619/* sign data (gnutls_only)
620OR
621sign hash.
622
9e70917d 623Return: NULL for success with the signaature in the sig blob, or an error string */
2592e6c0
JH
624
625const uschar *
d73e45df 626exim_dkim_sign(es_ctx * sign_ctx, hashmethod hash, blob * data, blob * sig)
2592e6c0 627{
d73e45df
JH
628const EVP_MD * md;
629EVP_PKEY_CTX * ctx;
630size_t siglen;
9b2583c4 631
d73e45df
JH
632switch (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
640if ( (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 658if (ctx) EVP_PKEY_CTX_free(ctx);
7845dbb3 659return US ERR_error_string(ERR_get_error(), NULL);
2592e6c0
JH
660}
661
662
663
664/* import public key (from DER in memory)
9b2583c4 665Return: NULL for success, or an error string */
2592e6c0
JH
666
667const uschar *
9b2583c4 668exim_dkim_verify_init(blob * pubkey_der, ev_ctx * verify_ctx)
2592e6c0 669{
d73e45df 670const uschar * s = pubkey_der->data;
2592e6c0 671
d73e45df 672/*XXX hmm, we never free this */
9b2583c4 673
d73e45df
JH
674if ((verify_ctx->key = d2i_PUBKEY(NULL, &s, pubkey_der->len)))
675 return NULL;
7845dbb3 676return US ERR_error_string(ERR_get_error(), NULL);
2592e6c0
JH
677}
678
679
680
681
682/* verify signature (of hash) (given pubkey & alleged sig)
683Return: NULL for success, or an error string */
684
685const uschar *
d73e45df 686exim_dkim_verify(ev_ctx * verify_ctx, hashmethod hash, blob * data_hash, blob * sig)
2592e6c0 687{
d73e45df
JH
688const EVP_MD * md;
689EVP_PKEY_CTX * ctx;
2592e6c0 690
d73e45df 691switch (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
699if ( (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
708if (ctx) EVP_PKEY_CTX_free(ctx);
7845dbb3 709return US 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 */