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