1 /* dnsmasq is Copyright (c) 2000-2022 Simon Kelley
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.
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.
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/>.
19 #if defined(HAVE_DNSSEC) || defined(HAVE_CRYPTOHASH)
21 /* Minimal version of nettle */
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
30 #define MIN_VERSION(major, minor) ((NETTLE_VERSION_MAJOR == (major) && NETTLE_VERSION_MINOR >= (minor)) || \
31 (NETTLE_VERSION_MAJOR > (major)))
33 #endif /* defined(HAVE_DNSSEC) || defined(HAVE_CRYPTOHASH) */
35 #if defined(HAVE_DNSSEC)
36 #include <nettle/rsa.h>
37 #include <nettle/ecdsa.h>
38 #include <nettle/ecc-curve.h>
40 #include <nettle/eddsa.h>
43 # include <nettle/gostdsa.h>
47 /* Implement a "hash-function" to the nettle API, which simply returns
48 the input data, concatenated into a single, statically maintained, buffer.
50 Used for the EdDSA sigs, which operate on the whole message, rather
53 struct null_hash_digest
64 static size_t null_hash_buff_sz = 0;
65 static uint8_t *null_hash_buff = NULL;
68 static void null_hash_init(void *ctx)
70 ((struct null_hash_ctx *)ctx)->len = 0;
73 static void null_hash_update(void *ctxv, size_t length, const uint8_t *src)
75 struct null_hash_ctx *ctx = ctxv;
76 size_t new_len = ctx->len + length;
78 if (new_len > null_hash_buff_sz)
82 if (!(new = whine_malloc(new_len + BUFF_INCR)))
88 memcpy(new, null_hash_buff, ctx->len);
92 null_hash_buff_sz = new_len + BUFF_INCR;
96 memcpy(null_hash_buff + ctx->len, src, length);
100 static void null_hash_digest(void *ctx, size_t length, uint8_t *dst)
104 ((struct null_hash_digest *)dst)->buff = null_hash_buff;
105 ((struct null_hash_digest *)dst)->len = ((struct null_hash_ctx *)ctx)->len;
108 static struct nettle_hash null_hash = {
110 sizeof(struct null_hash_ctx),
111 sizeof(struct null_hash_digest),
113 (nettle_hash_init_func *) null_hash_init,
114 (nettle_hash_update_func *) null_hash_update,
115 (nettle_hash_digest_func *) null_hash_digest
118 #endif /* MIN_VERSION(3, 1) */
120 /* expand ctx and digest memory allocations if necessary and init hash function */
121 int hash_init(const struct nettle_hash *hash, void **ctxp, unsigned char **digestp)
123 static void *ctx = NULL;
124 static unsigned char *digest = NULL;
125 static unsigned int ctx_sz = 0;
126 static unsigned int digest_sz = 0;
130 if (ctx_sz < hash->context_size)
132 if (!(new = whine_malloc(hash->context_size)))
137 ctx_sz = hash->context_size;
140 if (digest_sz < hash->digest_size)
142 if (!(new = whine_malloc(hash->digest_size)))
147 digest_sz = hash->digest_size;
158 static int dnsmasq_rsa_verify(struct blockdata *key_data, unsigned int key_len, unsigned char *sig, size_t sig_len,
159 unsigned char *digest, size_t digest_len, int algo)
164 static struct rsa_public_key *key = NULL;
165 static mpz_t sig_mpz;
171 if (!(key = whine_malloc(sizeof(struct rsa_public_key))))
174 nettle_rsa_public_key_init(key);
178 if ((key_len < 3) || !(p = blockdata_retrieve(key_data, key_len, NULL)))
182 if ((exp_len = *p++) == 0)
184 GETSHORT(exp_len, p);
188 if (exp_len >= key_len)
191 key->size = key_len - exp_len;
192 mpz_import(key->e, exp_len, 1, 1, 0, 0, p);
193 mpz_import(key->n, key->size, 1, 1, 0, 0, p + exp_len);
195 mpz_import(sig_mpz, sig_len, 1, 1, 0, 0, sig);
200 return nettle_rsa_sha1_verify_digest(key, digest, sig_mpz);
202 return nettle_rsa_sha256_verify_digest(key, digest, sig_mpz);
204 return nettle_rsa_sha512_verify_digest(key, digest, sig_mpz);
210 static int dnsmasq_ecdsa_verify(struct blockdata *key_data, unsigned int key_len,
211 unsigned char *sig, size_t sig_len,
212 unsigned char *digest, size_t digest_len, int algo)
216 struct ecc_point *key;
218 static struct ecc_point *key_256 = NULL, *key_384 = NULL;
220 static struct dsa_signature *sig_struct;
221 #if !MIN_VERSION(3, 4)
222 #define nettle_get_secp_256r1() (&nettle_secp_256r1)
223 #define nettle_get_secp_384r1() (&nettle_secp_384r1)
228 if (!(sig_struct = whine_malloc(sizeof(struct dsa_signature))))
231 nettle_dsa_signature_init(sig_struct);
241 if (!(key_256 = whine_malloc(sizeof(struct ecc_point))))
244 nettle_ecc_point_init(key_256, nettle_get_secp_256r1());
254 if (!(key_384 = whine_malloc(sizeof(struct ecc_point))))
257 nettle_ecc_point_init(key_384, nettle_get_secp_384r1());
268 if (sig_len != 2*t || key_len != 2*t ||
269 !(p = blockdata_retrieve(key_data, key_len, NULL)))
272 mpz_import(x, t , 1, 1, 0, 0, p);
273 mpz_import(y, t , 1, 1, 0, 0, p + t);
275 if (!ecc_point_set(key, x, y))
278 mpz_import(sig_struct->r, t, 1, 1, 0, 0, sig);
279 mpz_import(sig_struct->s, t, 1, 1, 0, 0, sig + t);
281 return nettle_ecdsa_verify(key, digest_len, digest, sig_struct);
284 #if MIN_VERSION(3, 6)
285 static int dnsmasq_gostdsa_verify(struct blockdata *key_data, unsigned int key_len,
286 unsigned char *sig, size_t sig_len,
287 unsigned char *digest, size_t digest_len, int algo)
291 static struct ecc_point *gost_key = NULL;
293 static struct dsa_signature *sig_struct;
296 sig_len != 64 || key_len != 64 ||
297 !(p = blockdata_retrieve(key_data, key_len, NULL)))
302 if (!(sig_struct = whine_malloc(sizeof(struct dsa_signature))) ||
303 !(gost_key = whine_malloc(sizeof(struct ecc_point))))
306 nettle_dsa_signature_init(sig_struct);
307 nettle_ecc_point_init(gost_key, nettle_get_gost_gc256b());
312 mpz_import(x, 32, -1, 1, 0, 0, p);
313 mpz_import(y, 32, -1, 1, 0, 0, p + 32);
315 if (!ecc_point_set(gost_key, x, y))
318 mpz_import(sig_struct->s, 32, 1, 1, 0, 0, sig);
319 mpz_import(sig_struct->r, 32, 1, 1, 0, 0, sig + 32);
321 return nettle_gostdsa_verify(gost_key, digest_len, digest, sig_struct);
325 #if MIN_VERSION(3, 1)
326 static int dnsmasq_eddsa_verify(struct blockdata *key_data, unsigned int key_len,
327 unsigned char *sig, size_t sig_len,
328 unsigned char *digest, size_t digest_len, int algo)
332 if (digest_len != sizeof(struct null_hash_digest) ||
333 !(p = blockdata_retrieve(key_data, key_len, NULL)))
336 /* The "digest" returned by the null_hash function is simply a struct null_hash_digest
337 which has a pointer to the actual data and a length, because the buffer
338 may need to be extended during "hashing". */
343 if (key_len != ED25519_KEY_SIZE ||
344 sig_len != ED25519_SIGNATURE_SIZE)
347 return ed25519_sha512_verify(p,
348 ((struct null_hash_digest *)digest)->len,
349 ((struct null_hash_digest *)digest)->buff,
352 #if MIN_VERSION(3, 6)
354 if (key_len != ED448_KEY_SIZE ||
355 sig_len != ED448_SIGNATURE_SIZE)
358 return ed448_shake256_verify(p,
359 ((struct null_hash_digest *)digest)->len,
360 ((struct null_hash_digest *)digest)->buff,
370 static int (*verify_func(int algo))(struct blockdata *key_data, unsigned int key_len, unsigned char *sig, size_t sig_len,
371 unsigned char *digest, size_t digest_len, int algo)
374 /* Ensure at runtime that we have support for this digest */
375 if (!hash_find(algo_digest_name(algo)))
378 /* This switch defines which sig algorithms we support, can't introspect Nettle for that. */
381 case 5: case 7: case 8: case 10:
382 return dnsmasq_rsa_verify;
384 #if MIN_VERSION(3, 6)
386 return dnsmasq_gostdsa_verify;
390 return dnsmasq_ecdsa_verify;
392 #if MIN_VERSION(3, 1)
394 return dnsmasq_eddsa_verify;
397 #if MIN_VERSION(3, 6)
399 return dnsmasq_eddsa_verify;
406 int verify(struct blockdata *key_data, unsigned int key_len, unsigned char *sig, size_t sig_len,
407 unsigned char *digest, size_t digest_len, int algo)
410 int (*func)(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);
413 func = verify_func(algo);
418 return (*func)(key_data, key_len, sig, sig_len, digest, digest_len, algo);
421 /* Note the ds_digest_name(), algo_digest_name() and nsec3_digest_name()
422 define which algo numbers we support. If algo_digest_name() returns
423 non-NULL for an algorithm number, we assume that algorithm is
424 supported by verify(). */
426 /* http://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */
427 char *ds_digest_name(int digest)
431 case 1: return "sha1";
432 case 2: return "sha256";
433 #if MIN_VERSION(3, 6)
434 case 3: return "gosthash94cp";
436 case 4: return "sha384";
437 default: return NULL;
441 /* http://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */
442 char *algo_digest_name(int algo)
446 case 1: return NULL; /* RSA/MD5 - Must Not Implement. RFC 6944 para 2.3. */
447 case 2: return NULL; /* Diffie-Hellman */
448 case 3: return NULL; ; /* DSA/SHA1 - Must Not Implement. RFC 8624 section 3.1 */
449 case 5: return "sha1"; /* RSA/SHA1 */
450 case 6: return NULL; /* DSA-NSEC3-SHA1 - Must Not Implement. RFC 8624 section 3.1 */
451 case 7: return "sha1"; /* RSASHA1-NSEC3-SHA1 */
452 case 8: return "sha256"; /* RSA/SHA-256 */
453 case 10: return "sha512"; /* RSA/SHA-512 */
454 #if MIN_VERSION(3, 6)
455 case 12: return "gosthash94cp"; /* ECC-GOST */
457 case 13: return "sha256"; /* ECDSAP256SHA256 */
458 case 14: return "sha384"; /* ECDSAP384SHA384 */
459 #if MIN_VERSION(3, 1)
460 case 15: return "null_hash"; /* ED25519 */
461 # if MIN_VERSION(3, 6)
462 case 16: return "null_hash"; /* ED448 */
465 default: return NULL;
469 /* http://www.iana.org/assignments/dnssec-nsec3-parameters/dnssec-nsec3-parameters.xhtml */
470 char *nsec3_digest_name(int digest)
474 case 1: return "sha1";
475 default: return NULL;
479 #endif /* defined(HAVE_DNSSEC) */
481 #if defined(HAVE_DNSSEC) || defined(HAVE_CRYPTOHASH)
482 /* Find pointer to correct hash function in nettle library */
483 const struct nettle_hash *hash_find(char *name)
488 #if MIN_VERSION(3,1) && defined(HAVE_DNSSEC)
489 /* We provide a "null" hash which returns the input data as digest. */
490 if (strcmp(null_hash.name, name) == 0)
494 /* libnettle >= 3.4 provides nettle_lookup_hash() which avoids nasty ABI
495 incompatibilities if sizeof(nettle_hashes) changes between library
497 #if MIN_VERSION(3, 4)
498 return nettle_lookup_hash(name);
503 for (i = 0; nettle_hashes[i]; i++)
504 if (strcmp(nettle_hashes[i]->name, name) == 0)
505 return nettle_hashes[i];
512 #endif /* defined(HAVE_DNSSEC) || defined(HAVE_CRYPTOHASH) */