Debian Codegraph @

src/crypto.c

raw
1 /* dnsmasq is Copyright (c) 2000-2026 Simon Kelley
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include "dnsmasq.h"
18
19 #if defined(HAVE_DNSSEC)
20
21 /* Minimal version of nettle */
22
23 /* bignum.h includes version.h and works on
24 earlier releases of nettle which don't have version.h */
25 #include <nettle/bignum.h>
26 #if !defined(NETTLE_VERSION_MAJOR)
27 # define NETTLE_VERSION_MAJOR 2
28 # define NETTLE_VERSION_MINOR 0
29 #endif
30 #define MIN_VERSION(major, minor) ((NETTLE_VERSION_MAJOR == (major) && NETTLE_VERSION_MINOR >= (minor)) || \
31 (NETTLE_VERSION_MAJOR > (major)))
32
33 #include <nettle/rsa.h>
34 #include <nettle/ecdsa.h>
35 #include <nettle/ecc-curve.h>
36 #if MIN_VERSION(3, 1)
37 #include <nettle/eddsa.h>
38 #endif
39 #if MIN_VERSION(3, 6)
40 # include <nettle/gostdsa.h>
41 #endif
42
43 #if MIN_VERSION(3, 1)
44 /* Implement a "hash-function" to the nettle API, which simply returns
45 the input data, concatenated into a single, statically maintained, buffer.
46
47 Used for the EdDSA sigs, which operate on the whole message, rather
48 than a digest. */
49
50 struct null_hash_digest
51 {
52 uint8_t *buff;
53 size_t len;
54 };
55
56 struct null_hash_ctx
57 {
58 size_t len;
59 };
60
61 static size_t null_hash_buff_sz = 0;
62 static uint8_t *null_hash_buff = NULL;
63 #define BUFF_INCR 128
64
65 static void null_hash_init(void *ctx)
66 {
67 ((struct null_hash_ctx *)ctx)->len = 0;
68 }
69
70 static void null_hash_update(void *ctxv, size_t length, const uint8_t *src)
71 {
72 struct null_hash_ctx *ctx = ctxv;
73 size_t new_len = ctx->len + length;
74
75 if (new_len > null_hash_buff_sz)
76 {
77 uint8_t *new;
78
79 if (!(new = whine_malloc(new_len + BUFF_INCR)))
80 return;
81
82 if (null_hash_buff)
83 {
84 if (ctx->len != 0)
85 memcpy(new, null_hash_buff, ctx->len);
86 free(null_hash_buff);
87 }
88
89 null_hash_buff_sz = new_len + BUFF_INCR;
90 null_hash_buff = new;
91 }
92
93 memcpy(null_hash_buff + ctx->len, src, length);
94 ctx->len += length;
95 }
96
97 /* The prototype changes in nettle 4.0 to omit the length argument */
98 #if MIN_VERSION(4, 0)
99 static void null_hash_digest(void *ctx, uint8_t *dst)
100 {
101 ((struct null_hash_digest *)dst)->buff = null_hash_buff;
102 ((struct null_hash_digest *)dst)->len = ((struct null_hash_ctx *)ctx)->len;
103 }
104 #else
105 static void null_hash_digest(void *ctx, size_t length, uint8_t *dst)
106 {
107 (void)length;
108
109 ((struct null_hash_digest *)dst)->buff = null_hash_buff;
110 ((struct null_hash_digest *)dst)->len = ((struct null_hash_ctx *)ctx)->len;
111 }
112 #endif
113
114 static struct nettle_hash null_hash = {
115 "null_hash",
116 sizeof(struct null_hash_ctx),
117 sizeof(struct null_hash_digest),
118 0,
119 (nettle_hash_init_func *) null_hash_init,
120 (nettle_hash_update_func *) null_hash_update,
121 (nettle_hash_digest_func *) null_hash_digest
122 };
123
124 #endif /* MIN_VERSION(3, 1) */
125
126 /* expand ctx and digest memory allocations if necessary and init hash function */
127 int hash_init(const struct nettle_hash *hash, void **ctxp, unsigned char **digestp)
128 {
129 static void *ctx = NULL;
130 static unsigned char *digest = NULL;
131 static unsigned int ctx_sz = 0;
132 static unsigned int digest_sz = 0;
133
134 void *new;
135
136 if (ctx_sz < hash->context_size)
137 {
138 if (!(new = whine_realloc(ctx, hash->context_size)))
139 return 0;
140
141 ctx = new;
142 ctx_sz = hash->context_size;
143 }
144
145 if (digest_sz < hash->digest_size)
146 {
147 if (!(new = whine_realloc(digest, hash->digest_size)))
148 return 0;
149
150 digest = new;
151 digest_sz = hash->digest_size;
152 }
153
154 *ctxp = ctx;
155 *digestp = digest;
156
157 hash->init(ctx);
158
159 return 1;
160 }
161
162 static int dnsmasq_rsa_verify(struct blockdata *key_data, unsigned int key_len, unsigned char *sig, size_t sig_len,
163 unsigned char *digest, size_t digest_len, int algo)
164 {
165 unsigned char *p;
166 size_t exp_len;
167
168 static struct rsa_public_key *key = NULL;
169 static mpz_t sig_mpz;
170
171 (void)digest_len;
172
173 if (key == NULL)
174 {
175 if (!(key = whine_malloc(sizeof(struct rsa_public_key))))
176 return 0;
177
178 nettle_rsa_public_key_init(key);
179 mpz_init(sig_mpz);
180 }
181
182 if ((key_len < 3) || !(p = blockdata_retrieve(key_data, key_len, NULL)))
183 return 0;
184
185 key_len--;
186 if ((exp_len = *p++) == 0)
187 {
188 GETSHORT(exp_len, p);
189 key_len -= 2;
190 }
191
192 if (exp_len >= key_len)
193 return 0;
194
195 key->size = key_len - exp_len;
196 mpz_import(key->e, exp_len, 1, 1, 0, 0, p);
197 mpz_import(key->n, key->size, 1, 1, 0, 0, p + exp_len);
198
199 mpz_import(sig_mpz, sig_len, 1, 1, 0, 0, sig);
200
201 switch (algo)
202 {
203 case 5: case 7:
204 return nettle_rsa_sha1_verify_digest(key, digest, sig_mpz);
205 case 8:
206 return nettle_rsa_sha256_verify_digest(key, digest, sig_mpz);
207 case 10:
208 return nettle_rsa_sha512_verify_digest(key, digest, sig_mpz);
209 }
210
211 return 0;
212 }
213
214 static int dnsmasq_ecdsa_verify(struct blockdata *key_data, unsigned int key_len,
215 unsigned char *sig, size_t sig_len,
216 unsigned char *digest, size_t digest_len, int algo)
217 {
218 unsigned char *p;
219 unsigned int t;
220 struct ecc_point *key;
221
222 static struct ecc_point *key_256 = NULL, *key_384 = NULL;
223 static mpz_t x, y;
224 static struct dsa_signature *sig_struct;
225 #if !MIN_VERSION(3, 4)
226 #define nettle_get_secp_256r1() (&nettle_secp_256r1)
227 #define nettle_get_secp_384r1() (&nettle_secp_384r1)
228 #endif
229
230 if (!sig_struct)
231 {
232 if (!(sig_struct = whine_malloc(sizeof(struct dsa_signature))))
233 return 0;
234
235 nettle_dsa_signature_init(sig_struct);
236 mpz_init(x);
237 mpz_init(y);
238 }
239
240 switch (algo)
241 {
242 case 13:
243 if (!key_256)
244 {
245 if (!(key_256 = whine_malloc(sizeof(struct ecc_point))))
246 return 0;
247
248 nettle_ecc_point_init(key_256, nettle_get_secp_256r1());
249 }
250
251 key = key_256;
252 t = 32;
253 break;
254
255 case 14:
256 if (!key_384)
257 {
258 if (!(key_384 = whine_malloc(sizeof(struct ecc_point))))
259 return 0;
260
261 nettle_ecc_point_init(key_384, nettle_get_secp_384r1());
262 }
263
264 key = key_384;
265 t = 48;
266 break;
267
268 default:
269 return 0;
270 }
271
272 if (sig_len != 2*t || key_len != 2*t ||
273 !(p = blockdata_retrieve(key_data, key_len, NULL)))
274 return 0;
275
276 mpz_import(x, t , 1, 1, 0, 0, p);
277 mpz_import(y, t , 1, 1, 0, 0, p + t);
278
279 if (!ecc_point_set(key, x, y))
280 return 0;
281
282 mpz_import(sig_struct->r, t, 1, 1, 0, 0, sig);
283 mpz_import(sig_struct->s, t, 1, 1, 0, 0, sig + t);
284
285 return nettle_ecdsa_verify(key, digest_len, digest, sig_struct);
286 }
287
288 #if MIN_VERSION(3, 6)
289 static int dnsmasq_gostdsa_verify(struct blockdata *key_data, unsigned int key_len,
290 unsigned char *sig, size_t sig_len,
291 unsigned char *digest, size_t digest_len, int algo)
292 {
293 unsigned char *p;
294
295 static struct ecc_point *gost_key = NULL;
296 static mpz_t x, y;
297 static struct dsa_signature *sig_struct;
298
299 if (algo != 12 ||
300 sig_len != 64 || key_len != 64 ||
301 !(p = blockdata_retrieve(key_data, key_len, NULL)))
302 return 0;
303
304 if (!sig_struct)
305 {
306 if (!(sig_struct = whine_malloc(sizeof(struct dsa_signature))) ||
307 !(gost_key = whine_malloc(sizeof(struct ecc_point))))
308 return 0;
309
310 nettle_dsa_signature_init(sig_struct);
311 nettle_ecc_point_init(gost_key, nettle_get_gost_gc256b());
312 mpz_init(x);
313 mpz_init(y);
314 }
315
316 mpz_import(x, 32, -1, 1, 0, 0, p);
317 mpz_import(y, 32, -1, 1, 0, 0, p + 32);
318
319 if (!ecc_point_set(gost_key, x, y))
320 return 0;
321
322 mpz_import(sig_struct->s, 32, 1, 1, 0, 0, sig);
323 mpz_import(sig_struct->r, 32, 1, 1, 0, 0, sig + 32);
324
325 return nettle_gostdsa_verify(gost_key, digest_len, digest, sig_struct);
326 }
327 #endif
328
329 #if MIN_VERSION(3, 1)
330 static int dnsmasq_eddsa_verify(struct blockdata *key_data, unsigned int key_len,
331 unsigned char *sig, size_t sig_len,
332 unsigned char *digest, size_t digest_len, int algo)
333 {
334 unsigned char *p;
335
336 if (digest_len != sizeof(struct null_hash_digest) ||
337 !(p = blockdata_retrieve(key_data, key_len, NULL)))
338 return 0;
339
340 /* The "digest" returned by the null_hash function is simply a struct null_hash_digest
341 which has a pointer to the actual data and a length, because the buffer
342 may need to be extended during "hashing". */
343
344 switch (algo)
345 {
346 case 15:
347 if (key_len != ED25519_KEY_SIZE ||
348 sig_len != ED25519_SIGNATURE_SIZE)
349 return 0;
350
351 return ed25519_sha512_verify(p,
352 ((struct null_hash_digest *)digest)->len,
353 ((struct null_hash_digest *)digest)->buff,
354 sig);
355
356 #if MIN_VERSION(3, 6)
357 case 16:
358 if (key_len != ED448_KEY_SIZE ||
359 sig_len != ED448_SIGNATURE_SIZE)
360 return 0;
361
362 return ed448_shake256_verify(p,
363 ((struct null_hash_digest *)digest)->len,
364 ((struct null_hash_digest *)digest)->buff,
365 sig);
366 #endif
367
368 }
369
370 return 0;
371 }
372 #endif
373
374 static int (*verify_func(int algo))(struct blockdata *key_data, unsigned int key_len, unsigned char *sig, size_t sig_len,
375 unsigned char *digest, size_t digest_len, int algo)
376 {
377
378 /* Ensure at runtime that we have support for this digest */
379 if (!hash_find(algo_digest_name(algo)))
380 return NULL;
381
382 /* This switch defines which sig algorithms we support, can't introspect Nettle for that. */
383 switch (algo)
384 {
385 case 5: case 7: case 8: case 10:
386 return dnsmasq_rsa_verify;
387
388 #if MIN_VERSION(3, 6)
389 case 12:
390 return dnsmasq_gostdsa_verify;
391 #endif
392
393 case 13: case 14:
394 return dnsmasq_ecdsa_verify;
395
396 #if MIN_VERSION(3, 1)
397 case 15:
398 return dnsmasq_eddsa_verify;
399 #endif
400
401 #if MIN_VERSION(3, 6)
402 case 16:
403 return dnsmasq_eddsa_verify;
404 #endif
405 }
406
407 return NULL;
408 }
409
410 int verify(struct blockdata *key_data, unsigned int key_len, unsigned char *sig, size_t sig_len,
411 unsigned char *digest, size_t digest_len, int algo)
412 {
413
414 int (*func)(struct blockdata *key_data, unsigned int key_len, unsigned char *sig, size_t sig_len,
415 unsigned char *digest, size_t digest_len, int algo);
416
417 func = verify_func(algo);
418
419 if (!func)
420 return 0;
421
422 return (*func)(key_data, key_len, sig, sig_len, digest, digest_len, algo);
423 }
424
425 /* Note the ds_digest_name(), algo_digest_name() and nsec3_digest_name()
426 define which algo numbers we support. If algo_digest_name() returns
427 non-NULL for an algorithm number, we assume that algorithm is
428 supported by verify(). */
429
430 /* http://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */
431 char *ds_digest_name(int digest)
432 {
433 switch (digest)
434 {
435 case 1: return "sha1";
436 case 2: return "sha256";
437 #if MIN_VERSION(3, 6)
438 case 3: return "gosthash94cp";
439 #endif
440 case 4: return "sha384";
441 default: return NULL;
442 }
443 }
444
445 /* http://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */
446 char *algo_digest_name(int algo)
447 {
448 switch (algo)
449 {
450 case 1: return NULL; /* RSA/MD5 - Must Not Implement. RFC 6944 para 2.3. */
451 case 2: return NULL; /* Diffie-Hellman */
452 case 3: return NULL; ; /* DSA/SHA1 - Must Not Implement. RFC 8624 section 3.1 */
453 case 5: return "sha1"; /* RSA/SHA1 */
454 case 6: return NULL; /* DSA-NSEC3-SHA1 - Must Not Implement. RFC 8624 section 3.1 */
455 case 7: return "sha1"; /* RSASHA1-NSEC3-SHA1 */
456 case 8: return "sha256"; /* RSA/SHA-256 */
457 case 10: return "sha512"; /* RSA/SHA-512 */
458 #if MIN_VERSION(3, 6)
459 case 12: return "gosthash94cp"; /* ECC-GOST */
460 #endif
461 case 13: return "sha256"; /* ECDSAP256SHA256 */
462 case 14: return "sha384"; /* ECDSAP384SHA384 */
463 #if MIN_VERSION(3, 1)
464 case 15: return "null_hash"; /* ED25519 */
465 # if MIN_VERSION(3, 6)
466 case 16: return "null_hash"; /* ED448 */
467 # endif
468 #endif
469 default: return NULL;
470 }
471 }
472
473 /* http://www.iana.org/assignments/dnssec-nsec3-parameters/dnssec-nsec3-parameters.xhtml */
474 char *nsec3_digest_name(int digest)
475 {
476 switch (digest)
477 {
478 case 1: return "sha1";
479 default: return NULL;
480 }
481 }
482
483 /* Find pointer to correct hash function in nettle library */
484 const struct nettle_hash *hash_find(char *name)
485 {
486 if (!name)
487 return NULL;
488
489 #if MIN_VERSION(3,1) && defined(HAVE_DNSSEC)
490 /* We provide a "null" hash which returns the input data as digest. */
491 if (strcmp(null_hash.name, name) == 0)
492 return &null_hash;
493 #endif
494
495 /* libnettle >= 3.4 provides nettle_lookup_hash() which avoids nasty ABI
496 incompatibilities if sizeof(nettle_hashes) changes between library
497 versions. */
498 #if MIN_VERSION(3, 4)
499 return nettle_lookup_hash(name);
500 #else
501 {
502 int i;
503
504 for (i = 0; nettle_hashes[i]; i++)
505 if (strcmp(nettle_hashes[i]->name, name) == 0)
506 return nettle_hashes[i];
507 }
508
509 return NULL;
510 #endif
511 }
512
513 /* The prototype changes in nettle 4.0 to omit the length argument */
514 void nettle_digest_wrapper(const struct nettle_hash *hash, void *ctx, size_t length, uint8_t *dst)
515 {
516 #if MIN_VERSION(4, 0)
517 (void)length;
518 hash->digest(ctx, dst);
519 #else
520 hash->digest(ctx, length, dst);
521 #endif
522 }
523
524
525 #endif /* defined(HAVE_DNSSEC) */