ldns  1.7.0
keys.c
Go to the documentation of this file.
1 /*
2  * keys.c handle private keys for use in DNSSEC
3  *
4  * This module should hide some of the openSSL complexities
5  * and give a general interface for private keys and hmac
6  * handling
7  *
8  * (c) NLnet Labs, 2004-2006
9  *
10  * See the file LICENSE for the license
11  */
12 
13 #include <ldns/config.h>
14 
15 #include <ldns/ldns.h>
16 
17 #ifdef HAVE_SSL
18 #include <openssl/ui.h>
19 #include <openssl/ssl.h>
20 #include <openssl/rand.h>
21 #include <openssl/bn.h>
22 #include <openssl/rsa.h>
23 #ifdef USE_DSA
24 #include <openssl/dsa.h>
25 #endif
26 #ifndef OPENSSL_NO_ENGINE
27 #include <openssl/engine.h>
28 #endif
29 #endif /* HAVE_SSL */
30 
32  { LDNS_SIGN_RSAMD5, "RSAMD5" },
33  { LDNS_SIGN_RSASHA1, "RSASHA1" },
34  { LDNS_SIGN_RSASHA1_NSEC3, "RSASHA1-NSEC3-SHA1" },
35 #ifdef USE_SHA2
36  { LDNS_SIGN_RSASHA256, "RSASHA256" },
37  { LDNS_SIGN_RSASHA512, "RSASHA512" },
38 #endif
39 #ifdef USE_GOST
40  { LDNS_SIGN_ECC_GOST, "ECC-GOST" },
41 #endif
42 #ifdef USE_ECDSA
43  { LDNS_SIGN_ECDSAP256SHA256, "ECDSAP256SHA256" },
44  { LDNS_SIGN_ECDSAP384SHA384, "ECDSAP384SHA384" },
45 #endif
46 #ifdef USE_ED25519
47  { LDNS_SIGN_ED25519, "ED25519" },
48 #endif
49 #ifdef USE_ED448
50  { LDNS_SIGN_ED448, "ED448" },
51 #endif
52 #ifdef USE_DSA
53  { LDNS_SIGN_DSA, "DSA" },
54  { LDNS_SIGN_DSA_NSEC3, "DSA-NSEC3-SHA1" },
55 #endif
56  { LDNS_SIGN_HMACMD5, "hmac-md5.sig-alg.reg.int" },
57  { LDNS_SIGN_HMACSHA1, "hmac-sha1" },
58  { LDNS_SIGN_HMACSHA256, "hmac-sha256" },
59  { LDNS_SIGN_HMACSHA224, "hmac-sha224" },
60  { LDNS_SIGN_HMACSHA384, "hmac-sha384" },
61  { LDNS_SIGN_HMACSHA512, "hmac-sha512" },
62  { 0, NULL }
63 };
64 
67 {
69  if (!key_list) {
70  return NULL;
71  } else {
72  key_list->_key_count = 0;
73  key_list->_keys = NULL;
74  return key_list;
75  }
76 }
77 
78 ldns_key *
80 {
81  ldns_key *newkey;
82 
83  newkey = LDNS_MALLOC(ldns_key);
84  if (!newkey) {
85  return NULL;
86  } else {
87  /* some defaults - not sure wether to do this */
88  ldns_key_set_use(newkey, true);
90  ldns_key_set_origttl(newkey, 0);
91  ldns_key_set_keytag(newkey, 0);
92  ldns_key_set_inception(newkey, 0);
93  ldns_key_set_expiration(newkey, 0);
94  ldns_key_set_pubkey_owner(newkey, NULL);
95 #ifdef HAVE_SSL
96  ldns_key_set_evp_key(newkey, NULL);
97 #endif /* HAVE_SSL */
98  ldns_key_set_hmac_key(newkey, NULL);
99  ldns_key_set_external_key(newkey, NULL);
100  return newkey;
101  }
102 }
103 
106 {
107  return ldns_key_new_frm_fp_l(k, fp, NULL);
108 }
109 
110 #if defined(HAVE_SSL) && !defined(OPENSSL_NO_ENGINE)
112 ldns_key_new_frm_engine(ldns_key **key, ENGINE *e, char *key_id, ldns_algorithm alg)
113 {
114  ldns_key *k;
115 
116  k = ldns_key_new();
117  if(!k) return LDNS_STATUS_MEM_ERR;
118 #ifndef S_SPLINT_S
119  k->_key.key = ENGINE_load_private_key(e, key_id, UI_OpenSSL(), NULL);
120  if(!k->_key.key) {
121  ldns_key_free(k);
122  return LDNS_STATUS_ERR;
123  }
125  if (!k->_key.key) {
126  ldns_key_free(k);
128  }
129 #endif /* splint */
130  *key = k;
131  return LDNS_STATUS_OK;
132 }
133 #endif
134 
135 #ifdef USE_GOST
136 
137 ENGINE* ldns_gost_engine = NULL;
138 
139 int
141 {
142  static int gost_id = 0;
143  const EVP_PKEY_ASN1_METHOD* meth;
144  ENGINE* e;
145 
146  if(gost_id) return gost_id;
147 
148  /* see if configuration loaded gost implementation from other engine*/
149  meth = EVP_PKEY_asn1_find_str(NULL, "gost2001", -1);
150  if(meth) {
151  EVP_PKEY_asn1_get0_info(&gost_id, NULL, NULL, NULL, NULL, meth);
152  return gost_id;
153  }
154 
155  /* see if engine can be loaded already */
156  e = ENGINE_by_id("gost");
157  if(!e) {
158  /* load it ourself, in case statically linked */
159  ENGINE_load_builtin_engines();
160  ENGINE_load_dynamic();
161  e = ENGINE_by_id("gost");
162  }
163  if(!e) {
164  /* no gost engine in openssl */
165  return 0;
166  }
167  if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
168  ENGINE_finish(e);
169  ENGINE_free(e);
170  return 0;
171  }
172 
173  meth = EVP_PKEY_asn1_find_str(&e, "gost2001", -1);
174  if(!meth) {
175  /* algo not found */
176  ENGINE_finish(e);
177  ENGINE_free(e);
178  return 0;
179  }
180  /* Note: do not ENGINE_finish and ENGINE_free the acquired engine
181  * on some platforms this frees up the meth and unloads gost stuff */
182  ldns_gost_engine = e;
183 
184  EVP_PKEY_asn1_get0_info(&gost_id, NULL, NULL, NULL, NULL, meth);
185  return gost_id;
186 }
187 
188 void ldns_key_EVP_unload_gost(void)
189 {
190  if(ldns_gost_engine) {
191  ENGINE_finish(ldns_gost_engine);
192  ENGINE_free(ldns_gost_engine);
193  ldns_gost_engine = NULL;
194  }
195 }
196 
198 static EVP_PKEY*
199 ldns_key_new_frm_fp_gost_l(FILE* fp, int* line_nr)
200 {
201  char token[16384];
202  const unsigned char* pp;
203  int gost_id;
204  EVP_PKEY* pkey;
205  ldns_rdf* b64rdf = NULL;
206 
207  gost_id = ldns_key_EVP_load_gost_id();
208  if(!gost_id)
209  return NULL;
210 
211  if (ldns_fget_keyword_data_l(fp, "GostAsn1", ": ", token, "\n",
212  sizeof(token), line_nr) == -1)
213  return NULL;
214  while(strlen(token) < 96) {
215  /* read more b64 from the file, b64 split on multiple lines */
216  if(ldns_fget_token_l(fp, token+strlen(token), "\n",
217  sizeof(token)-strlen(token), line_nr) == -1)
218  return NULL;
219  }
220  if(ldns_str2rdf_b64(&b64rdf, token) != LDNS_STATUS_OK)
221  return NULL;
222  pp = (unsigned char*)ldns_rdf_data(b64rdf);
223  pkey = d2i_PrivateKey(gost_id, NULL, &pp, (int)ldns_rdf_size(b64rdf));
224  ldns_rdf_deep_free(b64rdf);
225  return pkey;
226 }
227 #endif
228 
229 #ifdef USE_ECDSA
230 
231 static int
232 ldns_EC_KEY_calc_public(EC_KEY* ec)
233 {
234  EC_POINT* pub_key;
235  const EC_GROUP* group;
236  group = EC_KEY_get0_group(ec);
237  pub_key = EC_POINT_new(group);
238  if(!pub_key) return 0;
239  if(!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group))) {
240  EC_POINT_free(pub_key);
241  return 0;
242  }
243  if(!EC_POINT_mul(group, pub_key, EC_KEY_get0_private_key(ec),
244  NULL, NULL, NULL)) {
245  EC_POINT_free(pub_key);
246  return 0;
247  }
248  if(EC_KEY_set_public_key(ec, pub_key) == 0) {
249  EC_POINT_free(pub_key);
250  return 0;
251  }
252  EC_POINT_free(pub_key);
253  return 1;
254 }
255 
257 static EVP_PKEY*
258 ldns_key_new_frm_fp_ecdsa_l(FILE* fp, ldns_algorithm alg, int* line_nr)
259 {
260  char token[16384];
261  ldns_rdf* b64rdf = NULL;
262  unsigned char* pp;
263  BIGNUM* bn;
264  EVP_PKEY* evp_key;
265  EC_KEY* ec;
266  if (ldns_fget_keyword_data_l(fp, "PrivateKey", ": ", token, "\n",
267  sizeof(token), line_nr) == -1)
268  return NULL;
269  if(ldns_str2rdf_b64(&b64rdf, token) != LDNS_STATUS_OK)
270  return NULL;
271  pp = (unsigned char*)ldns_rdf_data(b64rdf);
272 
273  if(alg == LDNS_ECDSAP256SHA256)
274  ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
275  else if(alg == LDNS_ECDSAP384SHA384)
276  ec = EC_KEY_new_by_curve_name(NID_secp384r1);
277  else ec = NULL;
278  if(!ec) {
279  ldns_rdf_deep_free(b64rdf);
280  return NULL;
281  }
282  bn = BN_bin2bn(pp, (int)ldns_rdf_size(b64rdf), NULL);
283  ldns_rdf_deep_free(b64rdf);
284  if(!bn) {
285  EC_KEY_free(ec);
286  return NULL;
287  }
288  EC_KEY_set_private_key(ec, bn);
289  BN_free(bn);
290  if(!ldns_EC_KEY_calc_public(ec)) {
291  EC_KEY_free(ec);
292  return NULL;
293  }
294 
295  evp_key = EVP_PKEY_new();
296  if(!evp_key) {
297  EC_KEY_free(ec);
298  return NULL;
299  }
300  if (!EVP_PKEY_assign_EC_KEY(evp_key, ec)) {
301  EVP_PKEY_free(evp_key);
302  EC_KEY_free(ec);
303  return NULL;
304  }
305  return evp_key;
306 }
307 #endif
308 
309 #ifdef USE_ED25519
310 
311 static EVP_PKEY*
312 ldns_ed25519_priv_raw(uint8_t* pkey, int plen)
313 {
314  const unsigned char* pp;
315  uint8_t buf[256];
316  int buflen = 0;
317  uint8_t pre[] = {0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
318  0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20};
319  int pre_len = 16;
320  /* ASN looks like this for ED25519 public key
321  * 302a300506032b6570032100 <32byteskey>
322  * for ED25519 private key
323  * 302e020100300506032b657004220420 <32bytes>
324  *
325  * for X25519 this was
326  * 30320201010420 <32byteskey>
327  * andparameters a00b06092b06010401da470f01
328  * (noparameters, preamble is 30250201010420).
329  * the key is reversed (little endian).
330  */
331  buflen = pre_len + plen;
332  if((size_t)buflen > sizeof(buf))
333  return NULL;
334  memmove(buf, pre, pre_len);
335  memmove(buf+pre_len, pkey, plen);
336  /* reverse the pkey into the buf - key is not reversed it seems */
337  /* for(i=0; i<plen; i++)
338  buf[pre_len+i] = pkey[plen-1-i]; */
339  pp = buf;
340  return d2i_PrivateKey(NID_ED25519, NULL, &pp, buflen);
341 }
342 
344 static EVP_PKEY*
345 ldns_key_new_frm_fp_ed25519_l(FILE* fp, int* line_nr)
346 {
347  char token[16384];
348  ldns_rdf* b64rdf = NULL;
349  EVP_PKEY* evp_key;
350  if (ldns_fget_keyword_data_l(fp, "PrivateKey", ": ", token, "\n",
351  sizeof(token), line_nr) == -1)
352  return NULL;
353  if(ldns_str2rdf_b64(&b64rdf, token) != LDNS_STATUS_OK)
354  return NULL;
355 
356  /* we use d2i_ECPrivateKey because it calculates the public key
357  * from the private part, which others, EC_KEY_set_private_key,
358  * and o2i methods, do not do */
359  /* for that the private key has to be encoded in ASN1 notation
360  * with a ED25519 prefix on it */
361 
362  evp_key = ldns_ed25519_priv_raw(ldns_rdf_data(b64rdf),
363  (int)ldns_rdf_size(b64rdf));
364  ldns_rdf_deep_free(b64rdf);
365  return evp_key;
366 }
367 #endif
368 
369 #ifdef USE_ED448
370 
371 static EVP_PKEY*
372 ldns_ed448_priv_raw(uint8_t* pkey, int plen)
373 {
374  const unsigned char* pp;
375  uint8_t buf[256];
376  int buflen = 0;
377  uint8_t pre[] = {0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39};
378  int pre_len = 16;
379  /* ASN looks like this for ED448
380  * 3047020100300506032b6571043b0439 <57bytekey>
381  * the key is reversed (little endian).
382  */
383  buflen = pre_len + plen;
384  if((size_t)buflen > sizeof(buf))
385  return NULL;
386  memmove(buf, pre, pre_len);
387  memmove(buf+pre_len, pkey, plen);
388  /* reverse the pkey into the buf - key is not reversed it seems */
389  /* for(i=0; i<plen; i++)
390  buf[pre_len+i] = pkey[plen-1-i]; */
391  pp = buf;
392  return d2i_PrivateKey(NID_ED448, NULL, &pp, buflen);
393 }
394 
396 static EVP_PKEY*
397 ldns_key_new_frm_fp_ed448_l(FILE* fp, int* line_nr)
398 {
399  char token[16384];
400  ldns_rdf* b64rdf = NULL;
401  EVP_PKEY* evp_key;
402  if (ldns_fget_keyword_data_l(fp, "PrivateKey", ": ", token, "\n",
403  sizeof(token), line_nr) == -1)
404  return NULL;
405  if(ldns_str2rdf_b64(&b64rdf, token) != LDNS_STATUS_OK)
406  return NULL;
407 
408  /* convert private key into ASN notation and then convert that */
409  evp_key = ldns_ed448_priv_raw(ldns_rdf_data(b64rdf),
410  (int)ldns_rdf_size(b64rdf));
411  ldns_rdf_deep_free(b64rdf);
412  return evp_key;
413 }
414 #endif
415 
417 ldns_key_new_frm_fp_l(ldns_key **key, FILE *fp, int *line_nr)
418 {
419  ldns_key *k;
420  char *d;
422  ldns_rr *key_rr;
423 #ifdef HAVE_SSL
424  RSA *rsa;
425 #ifdef USE_DSA
426  DSA *dsa;
427 #endif
428  unsigned char *hmac;
429  size_t hmac_size;
430 #endif /* HAVE_SSL */
431 
432  k = ldns_key_new();
433 
434  d = LDNS_XMALLOC(char, LDNS_MAX_LINELEN);
435  if (!k || !d) {
436  ldns_key_free(k);
437  LDNS_FREE(d);
438  return LDNS_STATUS_MEM_ERR;
439  }
440 
441  alg = 0;
442 
443  /* the file is highly structured. Do this in sequence */
444  /* RSA:
445  * Private-key-format: v1.x.
446  * Algorithm: 1 (RSA)
447 
448  */
449  /* get the key format version number */
450  if (ldns_fget_keyword_data_l(fp, "Private-key-format", ": ", d, "\n",
451  LDNS_MAX_LINELEN, line_nr) == -1) {
452  /* no version information */
453  ldns_key_free(k);
454  LDNS_FREE(d);
455  return LDNS_STATUS_SYNTAX_ERR;
456  }
457  if (strncmp(d, "v1.", 3) != 0) {
458  ldns_key_free(k);
459  LDNS_FREE(d);
461  }
462 
463  /* get the algorithm type, our file function strip ( ) so there are
464  * not in the return string! */
465  if (ldns_fget_keyword_data_l(fp, "Algorithm", ": ", d, "\n",
466  LDNS_MAX_LINELEN, line_nr) == -1) {
467  /* no alg information */
468  ldns_key_free(k);
469  LDNS_FREE(d);
471  }
472 
473  if (strncmp(d, "1 RSA", 2) == 0) {
474  alg = LDNS_SIGN_RSAMD5;
475  }
476  if (strncmp(d, "2 DH", 2) == 0) {
478  }
479  if (strncmp(d, "3 DSA", 2) == 0) {
480 #ifdef USE_DSA
481  alg = LDNS_SIGN_DSA;
482 #else
483 # ifdef STDERR_MSGS
484  fprintf(stderr, "Warning: DSA not compiled into this ");
485  fprintf(stderr, "version of ldns\n");
486 # endif
487 #endif
488  }
489  if (strncmp(d, "4 ECC", 2) == 0) {
491  }
492  if (strncmp(d, "5 RSASHA1", 2) == 0) {
493  alg = LDNS_SIGN_RSASHA1;
494  }
495  if (strncmp(d, "6 DSA", 2) == 0) {
496 #ifdef USE_DSA
497  alg = LDNS_SIGN_DSA_NSEC3;
498 #else
499 # ifdef STDERR_MSGS
500  fprintf(stderr, "Warning: DSA not compiled into this ");
501  fprintf(stderr, "version of ldns\n");
502 # endif
503 #endif
504  }
505  if (strncmp(d, "7 RSASHA1", 2) == 0) {
507  }
508 
509  if (strncmp(d, "8 RSASHA256", 2) == 0) {
510 #ifdef USE_SHA2
511  alg = LDNS_SIGN_RSASHA256;
512 #else
513 # ifdef STDERR_MSGS
514  fprintf(stderr, "Warning: SHA256 not compiled into this ");
515  fprintf(stderr, "version of ldns\n");
516 # endif
517 #endif
518  }
519  if (strncmp(d, "10 RSASHA512", 3) == 0) {
520 #ifdef USE_SHA2
521  alg = LDNS_SIGN_RSASHA512;
522 #else
523 # ifdef STDERR_MSGS
524  fprintf(stderr, "Warning: SHA512 not compiled into this ");
525  fprintf(stderr, "version of ldns\n");
526 # endif
527 #endif
528  }
529  if (strncmp(d, "12 ECC-GOST", 3) == 0) {
530 #ifdef USE_GOST
531  alg = LDNS_SIGN_ECC_GOST;
532 #else
533 # ifdef STDERR_MSGS
534  fprintf(stderr, "Warning: ECC-GOST not compiled into this ");
535  fprintf(stderr, "version of ldns, use --enable-gost\n");
536 # endif
537 #endif
538  }
539  if (strncmp(d, "13 ECDSAP256SHA256", 3) == 0) {
540 #ifdef USE_ECDSA
542 #else
543 # ifdef STDERR_MSGS
544  fprintf(stderr, "Warning: ECDSA not compiled into this ");
545  fprintf(stderr, "version of ldns, use --enable-ecdsa\n");
546 # endif
547 #endif
548  }
549  if (strncmp(d, "14 ECDSAP384SHA384", 3) == 0) {
550 #ifdef USE_ECDSA
552 #else
553 # ifdef STDERR_MSGS
554  fprintf(stderr, "Warning: ECDSA not compiled into this ");
555  fprintf(stderr, "version of ldns, use --enable-ecdsa\n");
556 # endif
557 #endif
558  }
559  if (strncmp(d, "15 ED25519", 3) == 0) {
560 #ifdef USE_ED25519
561  alg = LDNS_SIGN_ED25519;
562 #else
563 # ifdef STDERR_MSGS
564  fprintf(stderr, "Warning: ED25519 not compiled into this ");
565  fprintf(stderr, "version of ldns, use --enable-ed25519\n");
566 # endif
567 #endif
568  }
569  if (strncmp(d, "16 ED448", 3) == 0) {
570 #ifdef USE_ED448
571  alg = LDNS_SIGN_ED448;
572 #else
573 # ifdef STDERR_MSGS
574  fprintf(stderr, "Warning: ED448 not compiled into this ");
575  fprintf(stderr, "version of ldns, use --enable-ed448\n");
576 # endif
577 #endif
578  }
579  if (strncmp(d, "157 HMAC-MD5", 4) == 0) {
580  alg = LDNS_SIGN_HMACMD5;
581  }
582  if (strncmp(d, "158 HMAC-SHA1", 4) == 0) {
583  alg = LDNS_SIGN_HMACSHA1;
584  }
585  if (strncmp(d, "159 HMAC-SHA256", 4) == 0) {
586  alg = LDNS_SIGN_HMACSHA256;
587  }
588  /* For compatibility with dnssec-keygen */
589  if (strncmp(d, "161 ", 4) == 0) {
590  alg = LDNS_SIGN_HMACSHA1;
591  }
592  if (strncmp(d, "162 HMAC-SHA224", 4) == 0) {
593  alg = LDNS_SIGN_HMACSHA224;
594  }
595  /* For compatibility with dnssec-keygen */
596  if (strncmp(d, "163 ", 4) == 0) {
597  alg = LDNS_SIGN_HMACSHA256;
598  }
599  if (strncmp(d, "164 HMAC-SHA384", 4) == 0) {
600  alg = LDNS_SIGN_HMACSHA384;
601  }
602  if (strncmp(d, "165 HMAC-SHA512", 4) == 0) {
603  alg = LDNS_SIGN_HMACSHA512;
604  }
605  LDNS_FREE(d);
606 
607  switch(alg) {
608  case LDNS_SIGN_RSAMD5:
609  case LDNS_SIGN_RSASHA1:
611 #ifdef USE_SHA2
612  case LDNS_SIGN_RSASHA256:
613  case LDNS_SIGN_RSASHA512:
614 #endif
615  ldns_key_set_algorithm(k, alg);
616 #ifdef HAVE_SSL
617  rsa = ldns_key_new_frm_fp_rsa_l(fp, line_nr);
618  if (!rsa) {
619  ldns_key_free(k);
620  return LDNS_STATUS_ERR;
621  }
622  ldns_key_assign_rsa_key(k, rsa);
623 #endif /* HAVE_SSL */
624  break;
625 #ifdef USE_DSA
626  case LDNS_SIGN_DSA:
627  case LDNS_SIGN_DSA_NSEC3:
628  ldns_key_set_algorithm(k, alg);
629 #ifdef HAVE_SSL
630  dsa = ldns_key_new_frm_fp_dsa_l(fp, line_nr);
631  if (!dsa) {
632  ldns_key_free(k);
633  return LDNS_STATUS_ERR;
634  }
635  ldns_key_assign_dsa_key(k, dsa);
636 #endif /* HAVE_SSL */
637  break;
638 #endif /* USE_DSA */
639  case LDNS_SIGN_HMACMD5:
640  case LDNS_SIGN_HMACSHA1:
645  ldns_key_set_algorithm(k, alg);
646 #ifdef HAVE_SSL
647  hmac = ldns_key_new_frm_fp_hmac_l(fp, line_nr, &hmac_size);
648  if (!hmac) {
649  ldns_key_free(k);
650  return LDNS_STATUS_ERR;
651  }
652  ldns_key_set_hmac_size(k, hmac_size);
653  ldns_key_set_hmac_key(k, hmac);
654 #endif /* HAVE_SSL */
655  break;
656  case LDNS_SIGN_ECC_GOST:
657  ldns_key_set_algorithm(k, alg);
658 #if defined(HAVE_SSL) && defined(USE_GOST)
660  ldns_key_free(k);
662  }
664  ldns_key_new_frm_fp_gost_l(fp, line_nr));
665 #ifndef S_SPLINT_S
666  if(!k->_key.key) {
667  ldns_key_free(k);
668  return LDNS_STATUS_ERR;
669  }
670 #endif /* splint */
671 #endif
672  break;
673 #ifdef USE_ECDSA
676  ldns_key_set_algorithm(k, alg);
678  ldns_key_new_frm_fp_ecdsa_l(fp, (ldns_algorithm)alg, line_nr));
679 #ifndef S_SPLINT_S
680  if(!k->_key.key) {
681  ldns_key_free(k);
682  return LDNS_STATUS_ERR;
683  }
684 #endif /* splint */
685  break;
686 #endif
687 #ifdef USE_ED25519
688  case LDNS_SIGN_ED25519:
689  ldns_key_set_algorithm(k, alg);
691  ldns_key_new_frm_fp_ed25519_l(fp, line_nr));
692 #ifndef S_SPLINT_S
693  if(!k->_key.key) {
694  ldns_key_free(k);
695  return LDNS_STATUS_ERR;
696  }
697 #endif /* splint */
698  break;
699 #endif
700 #ifdef USE_ED448
701  case LDNS_SIGN_ED448:
702  ldns_key_set_algorithm(k, alg);
704  ldns_key_new_frm_fp_ed448_l(fp, line_nr));
705 #ifndef S_SPLINT_S
706  if(!k->_key.key) {
707  ldns_key_free(k);
708  return LDNS_STATUS_ERR;
709  }
710 #endif /* splint */
711  break;
712 #endif
713  default:
714  ldns_key_free(k);
716  }
717  key_rr = ldns_key2rr(k);
719  ldns_rr_free(key_rr);
720 
721  if (key) {
722  *key = k;
723  return LDNS_STATUS_OK;
724  }
725  ldns_key_free(k);
726  return LDNS_STATUS_ERR;
727 }
728 
729 #ifdef HAVE_SSL
730 RSA *
732 {
733  return ldns_key_new_frm_fp_rsa_l(f, NULL);
734 }
735 
736 RSA *
737 ldns_key_new_frm_fp_rsa_l(FILE *f, int *line_nr)
738 {
739  /* we parse
740  * Modulus:
741  * PublicExponent:
742  * PrivateExponent:
743  * Prime1:
744  * Prime2:
745  * Exponent1:
746  * Exponent2:
747  * Coefficient:
748  *
749  * man 3 RSA:
750  *
751  * struct
752  * {
753  * BIGNUM *n; // public modulus
754  * BIGNUM *e; // public exponent
755  * BIGNUM *d; // private exponent
756  * BIGNUM *p; // secret prime factor
757  * BIGNUM *q; // secret prime factor
758  * BIGNUM *dmp1; // d mod (p-1)
759  * BIGNUM *dmq1; // d mod (q-1)
760  * BIGNUM *iqmp; // q^-1 mod p
761  * // ...
762  *
763  */
764  char *b;
765  RSA *rsa;
766  uint8_t *buf;
767  int i;
768  BIGNUM *n=NULL, *e=NULL, *d=NULL, *p=NULL, *q=NULL,
769  *dmp1=NULL, *dmq1=NULL, *iqmp=NULL;
770 
771  b = LDNS_XMALLOC(char, LDNS_MAX_LINELEN);
772  buf = LDNS_XMALLOC(uint8_t, LDNS_MAX_LINELEN);
773  rsa = RSA_new();
774  if (!b || !rsa || !buf) {
775  goto error;
776  }
777 
778  /* I could use functions again, but that seems an overkill,
779  * allthough this also looks tedious
780  */
781 
782  /* Modules, rsa->n */
783  if (ldns_fget_keyword_data_l(f, "Modulus", ": ", b, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
784  goto error;
785  }
786  i = ldns_b64_pton((const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
787 #ifndef S_SPLINT_S
788  n = BN_bin2bn((const char unsigned*)buf, i, NULL);
789  if (!n) {
790  goto error;
791  }
792 
793  /* PublicExponent, rsa->e */
794  if (ldns_fget_keyword_data_l(f, "PublicExponent", ": ", b, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
795  goto error;
796  }
797  i = ldns_b64_pton((const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
798  e = BN_bin2bn((const char unsigned*)buf, i, NULL);
799  if (!e) {
800  goto error;
801  }
802 
803  /* PrivateExponent, rsa->d */
804  if (ldns_fget_keyword_data_l(f, "PrivateExponent", ": ", b, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
805  goto error;
806  }
807  i = ldns_b64_pton((const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
808  d = BN_bin2bn((const char unsigned*)buf, i, NULL);
809  if (!d) {
810  goto error;
811  }
812 
813  /* Prime1, rsa->p */
814  if (ldns_fget_keyword_data_l(f, "Prime1", ": ", b, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
815  goto error;
816  }
817  i = ldns_b64_pton((const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
818  p = BN_bin2bn((const char unsigned*)buf, i, NULL);
819  if (!p) {
820  goto error;
821  }
822 
823  /* Prime2, rsa->q */
824  if (ldns_fget_keyword_data_l(f, "Prime2", ": ", b, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
825  goto error;
826  }
827  i = ldns_b64_pton((const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
828  q = BN_bin2bn((const char unsigned*)buf, i, NULL);
829  if (!q) {
830  goto error;
831  }
832 
833  /* Exponent1, rsa->dmp1 */
834  if (ldns_fget_keyword_data_l(f, "Exponent1", ": ", b, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
835  goto error;
836  }
837  i = ldns_b64_pton((const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
838  dmp1 = BN_bin2bn((const char unsigned*)buf, i, NULL);
839  if (!dmp1) {
840  goto error;
841  }
842 
843  /* Exponent2, rsa->dmq1 */
844  if (ldns_fget_keyword_data_l(f, "Exponent2", ": ", b, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
845  goto error;
846  }
847  i = ldns_b64_pton((const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
848  dmq1 = BN_bin2bn((const char unsigned*)buf, i, NULL);
849  if (!dmq1) {
850  goto error;
851  }
852 
853  /* Coefficient, rsa->iqmp */
854  if (ldns_fget_keyword_data_l(f, "Coefficient", ": ", b, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
855  goto error;
856  }
857  i = ldns_b64_pton((const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
858  iqmp = BN_bin2bn((const char unsigned*)buf, i, NULL);
859  if (!iqmp) {
860  goto error;
861  }
862 #endif /* splint */
863 
864 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL)
865 # ifndef S_SPLINT_S
866  rsa->n = n;
867  rsa->e = e;
868  rsa->d = d;
869  rsa->p = p;
870  rsa->q = q;
871  rsa->dmp1 = dmp1;
872  rsa->dmq1 = dmq1;
873  rsa->iqmp = iqmp;
874 # endif
875 #else
876  if(!RSA_set0_key(rsa, n, e, d))
877  goto error;
878  n = NULL;
879  e = NULL;
880  d = NULL;
881  if(!RSA_set0_factors(rsa, p, q))
882  goto error;
883  p = NULL;
884  q = NULL;
885  if(!RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp))
886  goto error;
887 #endif
888 
889  LDNS_FREE(buf);
890  LDNS_FREE(b);
891  return rsa;
892 
893 error:
894  RSA_free(rsa);
895  LDNS_FREE(b);
896  LDNS_FREE(buf);
897  BN_free(n);
898  BN_free(e);
899  BN_free(d);
900  BN_free(p);
901  BN_free(q);
902  BN_free(dmp1);
903  BN_free(dmq1);
904  BN_free(iqmp);
905  return NULL;
906 }
907 
908 DSA *
910 {
911  return ldns_key_new_frm_fp_dsa_l(f, NULL);
912 }
913 
914 DSA *
915 ldns_key_new_frm_fp_dsa_l(FILE *f, ATTR_UNUSED(int *line_nr))
916 {
917  int i;
918  char *d;
919  DSA *dsa;
920  uint8_t *buf;
921  BIGNUM *p=NULL, *q=NULL, *g=NULL, *priv_key=NULL, *pub_key=NULL;
922 
923  d = LDNS_XMALLOC(char, LDNS_MAX_LINELEN);
924  buf = LDNS_XMALLOC(uint8_t, LDNS_MAX_LINELEN);
925  dsa = DSA_new();
926  if (!d || !dsa || !buf) {
927  goto error;
928  }
929 
930  /* the line parser removes the () from the input... */
931 
932  /* Prime, dsa->p */
933  if (ldns_fget_keyword_data_l(f, "Primep", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
934  goto error;
935  }
936  i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
937 #ifndef S_SPLINT_S
938  p = BN_bin2bn((const char unsigned*)buf, i, NULL);
939  if (!p) {
940  goto error;
941  }
942 
943  /* Subprime, dsa->q */
944  if (ldns_fget_keyword_data_l(f, "Subprimeq", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
945  goto error;
946  }
947  i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
948  q = BN_bin2bn((const char unsigned*)buf, i, NULL);
949  if (!q) {
950  goto error;
951  }
952 
953  /* Base, dsa->g */
954  if (ldns_fget_keyword_data_l(f, "Baseg", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
955  goto error;
956  }
957  i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
958  g = BN_bin2bn((const char unsigned*)buf, i, NULL);
959  if (!g) {
960  goto error;
961  }
962 
963  /* Private key, dsa->priv_key */
964  if (ldns_fget_keyword_data_l(f, "Private_valuex", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
965  goto error;
966  }
967  i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
968  priv_key = BN_bin2bn((const char unsigned*)buf, i, NULL);
969  if (!priv_key) {
970  goto error;
971  }
972 
973  /* Public key, dsa->priv_key */
974  if (ldns_fget_keyword_data_l(f, "Public_valuey", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
975  goto error;
976  }
977  i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
978  pub_key = BN_bin2bn((const char unsigned*)buf, i, NULL);
979  if (!pub_key) {
980  goto error;
981  }
982 #endif /* splint */
983 
984 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL)
985 # ifndef S_SPLINT_S
986  dsa->p = p;
987  dsa->q = q;
988  dsa->g = g;
989  dsa->priv_key = priv_key;
990  dsa->pub_key = pub_key;
991 # endif
992 #else
993  if(!DSA_set0_pqg(dsa, p, q, g))
994  goto error;
995  p = NULL;
996  q = NULL;
997  g = NULL;
998  if(!DSA_set0_key(dsa, pub_key, priv_key))
999  goto error;
1000 #endif
1001 
1002  LDNS_FREE(buf);
1003  LDNS_FREE(d);
1004 
1005  return dsa;
1006 
1007 error:
1008  LDNS_FREE(d);
1009  LDNS_FREE(buf);
1010  DSA_free(dsa);
1011  BN_free(p);
1012  BN_free(q);
1013  BN_free(g);
1014  BN_free(priv_key);
1015  BN_free(pub_key);
1016  return NULL;
1017 }
1018 
1019 unsigned char *
1020 ldns_key_new_frm_fp_hmac(FILE *f, size_t *hmac_size)
1021 {
1022  return ldns_key_new_frm_fp_hmac_l(f, NULL, hmac_size);
1023 }
1024 
1025 unsigned char *
1027  , ATTR_UNUSED(int *line_nr)
1028  , size_t *hmac_size
1029  )
1030 {
1031  size_t bufsz;
1032  char d[LDNS_MAX_LINELEN];
1033  unsigned char *buf = NULL;
1034 
1035  *hmac_size = ldns_fget_keyword_data_l(f, "Key", ": ", d, "\n",
1036  LDNS_MAX_LINELEN, line_nr) == -1
1037  ? 0
1038  : (buf = LDNS_XMALLOC( unsigned char, (bufsz =
1039  ldns_b64_ntop_calculate_size(strlen(d))))) == NULL
1040  ? 0
1041  : (size_t) ldns_b64_pton((const char*)d, buf, bufsz);
1042  return buf;
1043 }
1044 #endif /* HAVE_SSL */
1045 
1046 #ifdef USE_GOST
1047 static EVP_PKEY*
1048 ldns_gen_gost_key(void)
1049 {
1050  EVP_PKEY_CTX* ctx;
1051  EVP_PKEY* p = NULL;
1052  int gost_id = ldns_key_EVP_load_gost_id();
1053  if(!gost_id)
1054  return NULL;
1055  ctx = EVP_PKEY_CTX_new_id(gost_id, NULL);
1056  if(!ctx) {
1057  /* the id should be available now */
1058  return NULL;
1059  }
1060  if(EVP_PKEY_CTX_ctrl_str(ctx, "paramset", "A") <= 0) {
1061  /* cannot set paramset */
1062  EVP_PKEY_CTX_free(ctx);
1063  return NULL;
1064  }
1065 
1066  if(EVP_PKEY_keygen_init(ctx) <= 0) {
1067  EVP_PKEY_CTX_free(ctx);
1068  return NULL;
1069  }
1070  if(EVP_PKEY_keygen(ctx, &p) <= 0) {
1071  EVP_PKEY_free(p);
1072  EVP_PKEY_CTX_free(ctx);
1073  return NULL;
1074  }
1075  EVP_PKEY_CTX_free(ctx);
1076  return p;
1077 }
1078 #endif
1079 
1080 ldns_key *
1082 {
1083  ldns_key *k;
1084 #ifdef HAVE_SSL
1085 #ifdef USE_DSA
1086  DSA *d;
1087 #endif /* USE_DSA */
1088 # ifdef USE_ECDSA
1089  EC_KEY *ec = NULL;
1090 # endif
1091 # ifdef HAVE_EVP_PKEY_KEYGEN
1092  EVP_PKEY_CTX *ctx;
1093 # else
1094  RSA *r;
1095 # endif
1096 #else
1097  int i;
1098  uint16_t offset = 0;
1099 #endif
1100  unsigned char *hmac;
1101 
1102  k = ldns_key_new();
1103  if (!k) {
1104  return NULL;
1105  }
1106  switch(alg) {
1107  case LDNS_SIGN_RSAMD5:
1108  case LDNS_SIGN_RSASHA1:
1110  case LDNS_SIGN_RSASHA256:
1111  case LDNS_SIGN_RSASHA512:
1112 #ifdef HAVE_SSL
1113 #ifdef HAVE_EVP_PKEY_KEYGEN
1114  ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
1115  if(!ctx) {
1116  ldns_key_free(k);
1117  return NULL;
1118  }
1119  if(EVP_PKEY_keygen_init(ctx) <= 0) {
1120  ldns_key_free(k);
1121  EVP_PKEY_CTX_free(ctx);
1122  return NULL;
1123  }
1124  if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, size) <= 0) {
1125  ldns_key_free(k);
1126  EVP_PKEY_CTX_free(ctx);
1127  return NULL;
1128  }
1129 #ifndef S_SPLINT_S
1130  if (EVP_PKEY_keygen(ctx, &k->_key.key) <= 0) {
1131  ldns_key_free(k);
1132  EVP_PKEY_CTX_free(ctx);
1133  return NULL;
1134  }
1135 #endif
1136  EVP_PKEY_CTX_free(ctx);
1137 #else /* HAVE_EVP_PKEY_KEYGEN */
1138  r = RSA_generate_key((int)size, RSA_F4, NULL, NULL);
1139  if(!r) {
1140  ldns_key_free(k);
1141  return NULL;
1142  }
1143  if (RSA_check_key(r) != 1) {
1144  ldns_key_free(k);
1145  return NULL;
1146  }
1147  ldns_key_set_rsa_key(k, r);
1148  RSA_free(r);
1149 #endif /* HAVE_EVP_PKEY_KEYGEN */
1150 #endif /* HAVE_SSL */
1151  break;
1152  case LDNS_SIGN_DSA:
1153  case LDNS_SIGN_DSA_NSEC3:
1154 #ifdef USE_DSA
1155 #ifdef HAVE_SSL
1156 # if OPENSSL_VERSION_NUMBER < 0x00908000L
1157  d = DSA_generate_parameters((int)size, NULL, 0, NULL, NULL, NULL, NULL);
1158  if (!d) {
1159  ldns_key_free(k);
1160  return NULL;
1161  }
1162 
1163 # else
1164  if (! (d = DSA_new())) {
1165  ldns_key_free(k);
1166  return NULL;
1167  }
1168  if (! DSA_generate_parameters_ex(d, (int)size, NULL, 0, NULL, NULL, NULL)) {
1169  DSA_free(d);
1170  ldns_key_free(k);
1171  return NULL;
1172  }
1173 # endif
1174  if (DSA_generate_key(d) != 1) {
1175  ldns_key_free(k);
1176  return NULL;
1177  }
1178  ldns_key_set_dsa_key(k, d);
1179  DSA_free(d);
1180 #endif /* HAVE_SSL */
1181 #endif /* USE_DSA */
1182  break;
1183  case LDNS_SIGN_HMACMD5:
1184  case LDNS_SIGN_HMACSHA1:
1185  case LDNS_SIGN_HMACSHA224:
1186  case LDNS_SIGN_HMACSHA256:
1187  case LDNS_SIGN_HMACSHA384:
1188  case LDNS_SIGN_HMACSHA512:
1189 #ifdef HAVE_SSL
1190 #ifndef S_SPLINT_S
1191  k->_key.key = NULL;
1192 #endif /* splint */
1193 #endif /* HAVE_SSL */
1194  size = size / 8;
1195  ldns_key_set_hmac_size(k, size);
1196 
1197  hmac = LDNS_XMALLOC(unsigned char, size);
1198  if(!hmac) {
1199  ldns_key_free(k);
1200  return NULL;
1201  }
1202 #ifdef HAVE_SSL
1203  if (RAND_bytes(hmac, (int) size) != 1) {
1204  LDNS_FREE(hmac);
1205  ldns_key_free(k);
1206  return NULL;
1207  }
1208 #else
1209  while (offset + sizeof(i) < size) {
1210  i = random();
1211  memcpy(&hmac[offset], &i, sizeof(i));
1212  offset += sizeof(i);
1213  }
1214  if (offset < size) {
1215  i = random();
1216  memcpy(&hmac[offset], &i, size - offset);
1217  }
1218 #endif /* HAVE_SSL */
1219  ldns_key_set_hmac_key(k, hmac);
1220 
1221  ldns_key_set_flags(k, 0);
1222  break;
1223  case LDNS_SIGN_ECC_GOST:
1224 #if defined(HAVE_SSL) && defined(USE_GOST)
1225  ldns_key_set_evp_key(k, ldns_gen_gost_key());
1226 #ifndef S_SPLINT_S
1227  if(!k->_key.key) {
1228  ldns_key_free(k);
1229  return NULL;
1230  }
1231 #endif /* splint */
1232 #else
1233  ldns_key_free(k);
1234  return NULL;
1235 #endif /* HAVE_SSL and USE_GOST */
1236  break;
1239 #ifdef USE_ECDSA
1240  if(alg == LDNS_SIGN_ECDSAP256SHA256)
1241  ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
1242  else if(alg == LDNS_SIGN_ECDSAP384SHA384)
1243  ec = EC_KEY_new_by_curve_name(NID_secp384r1);
1244  if(!ec) {
1245  ldns_key_free(k);
1246  return NULL;
1247  }
1248  if(!EC_KEY_generate_key(ec)) {
1249  ldns_key_free(k);
1250  EC_KEY_free(ec);
1251  return NULL;
1252  }
1253 #ifndef S_SPLINT_S
1254  k->_key.key = EVP_PKEY_new();
1255  if(!k->_key.key) {
1256  ldns_key_free(k);
1257  EC_KEY_free(ec);
1258  return NULL;
1259  }
1260  if (!EVP_PKEY_assign_EC_KEY(k->_key.key, ec)) {
1261  ldns_key_free(k);
1262  EC_KEY_free(ec);
1263  return NULL;
1264  }
1265 #endif /* splint */
1266 #else
1267  ldns_key_free(k);
1268  return NULL;
1269 #endif /* ECDSA */
1270  break;
1271 #ifdef USE_ED25519
1272  case LDNS_SIGN_ED25519:
1273 #ifdef HAVE_EVP_PKEY_KEYGEN
1274  ctx = EVP_PKEY_CTX_new_id(NID_ED25519, NULL);
1275  if(!ctx) {
1276  ldns_key_free(k);
1277  return NULL;
1278  }
1279  if(EVP_PKEY_keygen_init(ctx) <= 0) {
1280  ldns_key_free(k);
1281  EVP_PKEY_CTX_free(ctx);
1282  return NULL;
1283  }
1284  if (EVP_PKEY_keygen(ctx, &k->_key.key) <= 0) {
1285  ldns_key_free(k);
1286  EVP_PKEY_CTX_free(ctx);
1287  return NULL;
1288  }
1289  EVP_PKEY_CTX_free(ctx);
1290 #endif
1291  break;
1292 #endif /* ED25519 */
1293 #ifdef USE_ED448
1294  case LDNS_SIGN_ED448:
1295 #ifdef HAVE_EVP_PKEY_KEYGEN
1296  ctx = EVP_PKEY_CTX_new_id(NID_ED448, NULL);
1297  if(!ctx) {
1298  ldns_key_free(k);
1299  return NULL;
1300  }
1301  if(EVP_PKEY_keygen_init(ctx) <= 0) {
1302  ldns_key_free(k);
1303  EVP_PKEY_CTX_free(ctx);
1304  return NULL;
1305  }
1306  if (EVP_PKEY_keygen(ctx, &k->_key.key) <= 0) {
1307  ldns_key_free(k);
1308  EVP_PKEY_CTX_free(ctx);
1309  return NULL;
1310  }
1311  EVP_PKEY_CTX_free(ctx);
1312 #endif
1313  break;
1314 #endif /* ED448 */
1315  }
1316  ldns_key_set_algorithm(k, alg);
1317  return k;
1318 }
1319 
1320 void
1321 ldns_key_print(FILE *output, const ldns_key *k)
1322 {
1323  char *str = ldns_key2str(k);
1324  if (str) {
1325  fprintf(output, "%s", str);
1326  } else {
1327  fprintf(output, "Unable to convert private key to string\n");
1328  }
1329  LDNS_FREE(str);
1330 }
1331 
1332 
1333 void
1335 {
1336  k->_alg = l;
1337 }
1338 
1339 void
1341 {
1342  k->_extra.dnssec.flags = f;
1343 }
1344 
1345 #ifdef HAVE_SSL
1346 #ifndef S_SPLINT_S
1347 void
1349 {
1350  k->_key.key = e;
1351 }
1352 
1353 void
1355 {
1356  EVP_PKEY *key = EVP_PKEY_new();
1357  EVP_PKEY_set1_RSA(key, r);
1358  k->_key.key = key;
1359 }
1360 
1361 void
1363 {
1364 #ifdef USE_DSA
1365  EVP_PKEY *key = EVP_PKEY_new();
1366  EVP_PKEY_set1_DSA(key, d);
1367  k->_key.key = key;
1368 #else
1369  (void)k; (void)d;
1370 #endif
1371 }
1372 
1373 void
1375 {
1376  EVP_PKEY *key = EVP_PKEY_new();
1377  EVP_PKEY_assign_RSA(key, r);
1378  k->_key.key = key;
1379 }
1380 
1381 void
1383 {
1384 #ifdef USE_DSA
1385  EVP_PKEY *key = EVP_PKEY_new();
1386  EVP_PKEY_assign_DSA(key, d);
1387  k->_key.key = key;
1388 #else
1389  (void)k; (void)d;
1390 #endif
1391 }
1392 #endif /* splint */
1393 #endif /* HAVE_SSL */
1394 
1395 void
1396 ldns_key_set_hmac_key(ldns_key *k, unsigned char *hmac)
1397 {
1398  k->_key.hmac.key = hmac;
1399 }
1400 
1401 void
1402 ldns_key_set_hmac_size(ldns_key *k, size_t hmac_size)
1403 {
1404  k->_key.hmac.size = hmac_size;
1405 }
1406 
1407 void
1408 ldns_key_set_external_key(ldns_key *k, void *external_key)
1409 {
1410  k->_key.external_key = external_key;
1411 }
1412 
1413 void
1415 {
1416  k->_extra.dnssec.orig_ttl = t;
1417 }
1418 
1419 void
1421 {
1422  k->_extra.dnssec.inception = i;
1423 }
1424 
1425 void
1427 {
1428  k->_extra.dnssec.expiration = e;
1429 }
1430 
1431 void
1433 {
1434  k->_pubkey_owner = r;
1435 }
1436 
1437 void
1439 {
1440  k->_extra.dnssec.keytag = tag;
1441 }
1442 
1443 /* read */
1444 size_t
1446 {
1447  return key_list->_key_count;
1448 }
1449 
1450 ldns_key *
1451 ldns_key_list_key(const ldns_key_list *key, size_t nr)
1452 {
1453  if (nr < ldns_key_list_key_count(key)) {
1454  return key->_keys[nr];
1455  } else {
1456  return NULL;
1457  }
1458 }
1459 
1462 {
1463  return k->_alg;
1464 }
1465 
1466 void
1468 {
1469  if (k) {
1470  k->_use = v;
1471  }
1472 }
1473 
1474 bool
1476 {
1477  if (k) {
1478  return k->_use;
1479  }
1480  return false;
1481 }
1482 
1483 #ifdef HAVE_SSL
1484 #ifndef S_SPLINT_S
1485 EVP_PKEY *
1487 {
1488  return k->_key.key;
1489 }
1490 
1491 RSA *
1493 {
1494  if (k->_key.key) {
1495  return EVP_PKEY_get1_RSA(k->_key.key);
1496  } else {
1497  return NULL;
1498  }
1499 }
1500 
1501 DSA *
1503 {
1504 #ifdef USE_DSA
1505  if (k->_key.key) {
1506  return EVP_PKEY_get1_DSA(k->_key.key);
1507  } else {
1508  return NULL;
1509  }
1510 #else
1511  (void)k;
1512  return NULL;
1513 #endif
1514 }
1515 #endif /* splint */
1516 #endif /* HAVE_SSL */
1517 
1518 unsigned char *
1520 {
1521  if (k->_key.hmac.key) {
1522  return k->_key.hmac.key;
1523  } else {
1524  return NULL;
1525  }
1526 }
1527 
1528 size_t
1530 {
1531  if (k->_key.hmac.size) {
1532  return k->_key.hmac.size;
1533  } else {
1534  return 0;
1535  }
1536 }
1537 
1538 void *
1540 {
1541  return k->_key.external_key;
1542 }
1543 
1544 uint32_t
1546 {
1547  return k->_extra.dnssec.orig_ttl;
1548 }
1549 
1550 uint16_t
1552 {
1553  return k->_extra.dnssec.flags;
1554 }
1555 
1556 uint32_t
1558 {
1559  return k->_extra.dnssec.inception;
1560 }
1561 
1562 uint32_t
1564 {
1565  return k->_extra.dnssec.expiration;
1566 }
1567 
1568 uint16_t
1570 {
1571  return k->_extra.dnssec.keytag;
1572 }
1573 
1574 ldns_rdf *
1576 {
1577  return k->_pubkey_owner;
1578 }
1579 
1580 /* write */
1581 void
1583 {
1584  size_t i;
1585 
1586  for (i = 0; i < ldns_key_list_key_count(keys); i++) {
1587  ldns_key_set_use(ldns_key_list_key(keys, i), v);
1588  }
1589 }
1590 
1591 void
1593 {
1594  key->_key_count = count;
1595 }
1596 
1597 bool
1599 {
1600  size_t key_count;
1601  ldns_key **keys;
1602 
1603  key_count = ldns_key_list_key_count(key_list);
1604 
1605  /* grow the array */
1606  keys = LDNS_XREALLOC(
1607  key_list->_keys, ldns_key *, key_count + 1);
1608  if (!keys) {
1609  return false;
1610  }
1611 
1612  /* add the new member */
1613  key_list->_keys = keys;
1614  key_list->_keys[key_count] = key;
1615 
1616  ldns_key_list_set_key_count(key_list, key_count + 1);
1617  return true;
1618 }
1619 
1620 ldns_key *
1622 {
1623  size_t key_count;
1624  ldns_key** a;
1625  ldns_key *pop;
1626 
1627  if (!key_list) {
1628  return NULL;
1629  }
1630 
1631  key_count = ldns_key_list_key_count(key_list);
1632  if (key_count == 0) {
1633  return NULL;
1634  }
1635 
1636  pop = ldns_key_list_key(key_list, key_count);
1637 
1638  /* shrink the array */
1639  a = LDNS_XREALLOC(key_list->_keys, ldns_key *, key_count - 1);
1640  if(a) {
1641  key_list->_keys = a;
1642  }
1643 
1644  ldns_key_list_set_key_count(key_list, key_count - 1);
1645 
1646  return pop;
1647 }
1648 
1649 #ifdef HAVE_SSL
1650 #ifndef S_SPLINT_S
1651 /* data pointer must be large enough (LDNS_MAX_KEYLEN) */
1652 static bool
1653 ldns_key_rsa2bin(unsigned char *data, RSA *k, uint16_t *size)
1654 {
1655  int i,j;
1656  const BIGNUM *n=NULL, *e=NULL;
1657 
1658  if (!k) {
1659  return false;
1660  }
1661 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL)
1662  n = k->n;
1663  e = k->e;
1664 #else
1665  RSA_get0_key(k, &n, &e, NULL);
1666 #endif
1667 
1668  if (BN_num_bytes(e) <= 256) {
1669  /* normally only this path is executed (small factors are
1670  * more common
1671  */
1672  data[0] = (unsigned char) BN_num_bytes(e);
1673  i = BN_bn2bin(e, data + 1);
1674  j = BN_bn2bin(n, data + i + 1);
1675  *size = (uint16_t) i + j;
1676  } else if (BN_num_bytes(e) <= 65536) {
1677  data[0] = 0;
1678  /* BN_bn2bin does bigendian, _uint16 also */
1679  ldns_write_uint16(data + 1, (uint16_t) BN_num_bytes(e));
1680 
1681  BN_bn2bin(e, data + 3);
1682  BN_bn2bin(n, data + 4 + BN_num_bytes(e));
1683  *size = (uint16_t) BN_num_bytes(n) + 6;
1684  } else {
1685  return false;
1686  }
1687  return true;
1688 }
1689 
1690 #ifdef USE_DSA
1691 /* data pointer must be large enough (LDNS_MAX_KEYLEN) */
1692 static bool
1693 ldns_key_dsa2bin(unsigned char *data, DSA *k, uint16_t *size)
1694 {
1695  uint8_t T;
1696  const BIGNUM *p, *q, *g;
1697  const BIGNUM *pub_key, *priv_key;
1698 
1699  if (!k) {
1700  return false;
1701  }
1702 
1703  /* See RFC2536 */
1704 # ifdef HAVE_DSA_GET0_PQG
1705  DSA_get0_pqg(k, &p, &q, &g);
1706 # else
1707  p = k->p; q = k->q; g = k->g;
1708 # endif
1709 # ifdef HAVE_DSA_GET0_KEY
1710  DSA_get0_key(k, &pub_key, &priv_key);
1711 # else
1712  pub_key = k->pub_key; priv_key = k->priv_key;
1713 # endif
1714  (void)priv_key;
1715  *size = (uint16_t)BN_num_bytes(p);
1716  T = (*size - 64) / 8;
1717 
1718  if (T > 8) {
1719 #ifdef STDERR_MSGS
1720  fprintf(stderr, "DSA key with T > 8 (ie. > 1024 bits)");
1721  fprintf(stderr, " not implemented\n");
1722 #endif
1723  return false;
1724  }
1725 
1726  /* size = 64 + (T * 8); */
1727  memset(data, 0, 21 + *size * 3);
1728  data[0] = (unsigned char)T;
1729  BN_bn2bin(q, data + 1 ); /* 20 octects */
1730  BN_bn2bin(p, data + 21 ); /* offset octects */
1731  BN_bn2bin(g, data + 21 + *size * 2 - BN_num_bytes(g));
1732  BN_bn2bin(pub_key,data + 21 + *size * 3 - BN_num_bytes(pub_key));
1733  *size = 21 + *size * 3;
1734  return true;
1735 }
1736 #endif /* USE_DSA */
1737 
1738 #ifdef USE_GOST
1739 static bool
1740 ldns_key_gost2bin(unsigned char* data, EVP_PKEY* k, uint16_t* size)
1741 {
1742  int i;
1743  unsigned char* pp = NULL;
1744  if(i2d_PUBKEY(k, &pp) != 37 + 64) {
1745  /* expect 37 byte(ASN header) and 64 byte(X and Y) */
1746  free(pp);
1747  return false;
1748  }
1749  /* omit ASN header */
1750  for(i=0; i<64; i++)
1751  data[i] = pp[i+37];
1752  free(pp);
1753  *size = 64;
1754  return true;
1755 }
1756 #endif /* USE_GOST */
1757 
1758 #ifdef USE_ED25519
1759 static bool
1760 ldns_key_ed255192bin(unsigned char* data, EVP_PKEY* k, uint16_t* size)
1761 {
1762  int i;
1763  unsigned char* pp = NULL;
1764  if(i2d_PUBKEY(k, &pp) != 12 + 32) {
1765  /* expect 12 byte(ASN header) and 32 byte(pubkey) */
1766  free(pp);
1767  return false;
1768  }
1769  /* omit ASN header */
1770  for(i=0; i<32; i++)
1771  data[i] = pp[i+12];
1772  free(pp);
1773  *size = 32;
1774  return true;
1775 }
1776 #endif /* USE_ED25519 */
1777 
1778 #ifdef USE_ED448
1779 static bool
1780 ldns_key_ed4482bin(unsigned char* data, EVP_PKEY* k, uint16_t* size)
1781 {
1782  int i;
1783  unsigned char* pp = NULL;
1784  if(i2d_PUBKEY(k, &pp) != 12 + 57) {
1785  /* expect 12 byte(ASN header) and 57 byte(pubkey) */
1786  free(pp);
1787  return false;
1788  }
1789  /* omit ASN header */
1790  for(i=0; i<57; i++)
1791  data[i] = pp[i+12];
1792  free(pp);
1793  *size = 57;
1794  return true;
1795 }
1796 #endif /* USE_ED448 */
1797 #endif /* splint */
1798 #endif /* HAVE_SSL */
1799 
1800 ldns_rr *
1802 {
1803  /* this function will convert a the keydata contained in
1804  * rsa/dsa pointers to a DNSKEY rr. It will fill in as
1805  * much as it can, but it does not know about key-flags
1806  * for instance
1807  */
1808  ldns_rr *pubkey;
1809  ldns_rdf *keybin;
1810  unsigned char *bin = NULL;
1811  uint16_t size = 0;
1812 #ifdef HAVE_SSL
1813  RSA *rsa = NULL;
1814 #ifdef USE_DSA
1815  DSA *dsa = NULL;
1816 #endif /* USE_DSA */
1817 #endif /* HAVE_SSL */
1818 #ifdef USE_ECDSA
1819  EC_KEY* ec;
1820 #endif
1821  int internal_data = 0;
1822 
1823  if (!k) {
1824  return NULL;
1825  }
1826  pubkey = ldns_rr_new();
1827 
1828  switch (ldns_key_algorithm(k)) {
1829  case LDNS_SIGN_HMACMD5:
1830  case LDNS_SIGN_HMACSHA1:
1831  case LDNS_SIGN_HMACSHA224:
1832  case LDNS_SIGN_HMACSHA256:
1833  case LDNS_SIGN_HMACSHA384:
1834  case LDNS_SIGN_HMACSHA512:
1836  break;
1837  default:
1839  break;
1840  }
1841  /* zero-th rdf - flags */
1842  ldns_rr_push_rdf(pubkey,
1844  ldns_key_flags(k)));
1845  /* first - proto */
1846  ldns_rr_push_rdf(pubkey,
1848 
1849  if (ldns_key_pubkey_owner(k)) {
1851  }
1852 
1853  /* third - da algorithm */
1854  switch(ldns_key_algorithm(k)) {
1855  case LDNS_SIGN_RSAMD5:
1856  case LDNS_SIGN_RSASHA1:
1858  case LDNS_SIGN_RSASHA256:
1859  case LDNS_SIGN_RSASHA512:
1860  ldns_rr_push_rdf(pubkey,
1862 #ifdef HAVE_SSL
1863  rsa = ldns_key_rsa_key(k);
1864  if (rsa) {
1865  bin = LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN);
1866  if (!bin) {
1867  ldns_rr_free(pubkey);
1868  return NULL;
1869  }
1870  if (!ldns_key_rsa2bin(bin, rsa, &size)) {
1871  LDNS_FREE(bin);
1872  ldns_rr_free(pubkey);
1873  return NULL;
1874  }
1875  RSA_free(rsa);
1876  internal_data = 1;
1877  }
1878 #endif
1879  size++;
1880  break;
1881  case LDNS_SIGN_DSA:
1882  ldns_rr_push_rdf(pubkey,
1884 #ifdef USE_DSA
1885 #ifdef HAVE_SSL
1886  dsa = ldns_key_dsa_key(k);
1887  if (dsa) {
1888  bin = LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN);
1889  if (!bin) {
1890  ldns_rr_free(pubkey);
1891  return NULL;
1892  }
1893  if (!ldns_key_dsa2bin(bin, dsa, &size)) {
1894  LDNS_FREE(bin);
1895  ldns_rr_free(pubkey);
1896  return NULL;
1897  }
1898  DSA_free(dsa);
1899  internal_data = 1;
1900  }
1901 #endif /* HAVE_SSL */
1902 #endif /* USE_DSA */
1903  break;
1904  case LDNS_SIGN_DSA_NSEC3:
1905  ldns_rr_push_rdf(pubkey,
1907 #ifdef USE_DSA
1908 #ifdef HAVE_SSL
1909  dsa = ldns_key_dsa_key(k);
1910  if (dsa) {
1911  bin = LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN);
1912  if (!bin) {
1913  ldns_rr_free(pubkey);
1914  return NULL;
1915  }
1916  if (!ldns_key_dsa2bin(bin, dsa, &size)) {
1917  LDNS_FREE(bin);
1918  ldns_rr_free(pubkey);
1919  return NULL;
1920  }
1921  DSA_free(dsa);
1922  internal_data = 1;
1923  }
1924 #endif /* HAVE_SSL */
1925 #endif /* USE_DSA */
1926  break;
1927  case LDNS_SIGN_ECC_GOST:
1930 #if defined(HAVE_SSL) && defined(USE_GOST)
1931  bin = LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN);
1932  if (!bin) {
1933  ldns_rr_free(pubkey);
1934  return NULL;
1935  }
1936 #ifndef S_SPLINT_S
1937  if (!ldns_key_gost2bin(bin, k->_key.key, &size)) {
1938  LDNS_FREE(bin);
1939  ldns_rr_free(pubkey);
1940  return NULL;
1941  }
1942 #endif /* splint */
1943  internal_data = 1;
1944 #else
1945  ldns_rr_free(pubkey);
1946  return NULL;
1947 #endif /* HAVE_SSL and USE_GOST */
1948  break;
1951 #ifdef USE_ECDSA
1954  bin = NULL;
1955 #ifndef S_SPLINT_S
1956  ec = EVP_PKEY_get1_EC_KEY(k->_key.key);
1957 #endif
1958  EC_KEY_set_conv_form(ec, POINT_CONVERSION_UNCOMPRESSED);
1959  size = (uint16_t)i2o_ECPublicKey(ec, NULL);
1960  if(!i2o_ECPublicKey(ec, &bin)) {
1961  EC_KEY_free(ec);
1962  ldns_rr_free(pubkey);
1963  return NULL;
1964  }
1965  if(size > 1) {
1966  /* move back one byte to shave off the 0x02
1967  * 'uncompressed' indicator that openssl made
1968  * Actually its 0x04 (from implementation).
1969  */
1970  assert(bin[0] == POINT_CONVERSION_UNCOMPRESSED);
1971  size -= 1;
1972  memmove(bin, bin+1, size);
1973  }
1974  /* down the reference count for ec, its still assigned
1975  * to the pkey */
1976  EC_KEY_free(ec);
1977  internal_data = 1;
1978 #else
1979  ldns_rr_free(pubkey);
1980  return NULL;
1981 #endif /* ECDSA */
1982  break;
1983 #ifdef USE_ED25519
1984  case LDNS_SIGN_ED25519:
1987  bin = LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN);
1988  if (!bin) {
1989  ldns_rr_free(pubkey);
1990  return NULL;
1991  }
1992  if (!ldns_key_ed255192bin(bin, k->_key.key, &size)) {
1993  LDNS_FREE(bin);
1994  ldns_rr_free(pubkey);
1995  return NULL;
1996  }
1997  internal_data = 1;
1998  break;
1999 #endif
2000 #ifdef USE_ED448
2001  case LDNS_SIGN_ED448:
2004  bin = LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN);
2005  if (!bin) {
2006  ldns_rr_free(pubkey);
2007  return NULL;
2008  }
2009  if (!ldns_key_ed4482bin(bin, k->_key.key, &size)) {
2010  LDNS_FREE(bin);
2011  ldns_rr_free(pubkey);
2012  return NULL;
2013  }
2014  internal_data = 1;
2015  break;
2016 #endif
2017  case LDNS_SIGN_HMACMD5:
2018  case LDNS_SIGN_HMACSHA1:
2019  case LDNS_SIGN_HMACSHA224:
2020  case LDNS_SIGN_HMACSHA256:
2021  case LDNS_SIGN_HMACSHA384:
2022  case LDNS_SIGN_HMACSHA512:
2023  bin = LDNS_XMALLOC(unsigned char, ldns_key_hmac_size(k));
2024  if (!bin) {
2025  ldns_rr_free(pubkey);
2026  return NULL;
2027  }
2028  ldns_rr_push_rdf(pubkey,
2030  ldns_key_algorithm(k)));
2031  size = ldns_key_hmac_size(k);
2032  memcpy(bin, ldns_key_hmac_key(k), size);
2033  internal_data = 1;
2034  break;
2035  }
2036  /* fourth the key bin material */
2037  if (internal_data) {
2038  keybin = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, size, bin);
2039  LDNS_FREE(bin);
2040  ldns_rr_push_rdf(pubkey, keybin);
2041  }
2042  return pubkey;
2043 }
2044 
2045 void
2047 {
2048  LDNS_FREE(key);
2049 }
2050 
2051 void
2053 {
2054  unsigned char* hmac;
2055  if (ldns_key_pubkey_owner(key)) {
2057  }
2058 #ifdef HAVE_SSL
2059  if (ldns_key_evp_key(key)) {
2060  EVP_PKEY_free(ldns_key_evp_key(key));
2061  }
2062 #endif /* HAVE_SSL */
2063  if (ldns_key_hmac_key(key)) {
2064  hmac = ldns_key_hmac_key(key);
2065  LDNS_FREE(hmac);
2066  }
2067  LDNS_FREE(key);
2068 }
2069 
2070 void
2072 {
2073  size_t i;
2074  for (i = 0; i < ldns_key_list_key_count(key_list); i++) {
2075  ldns_key_deep_free(ldns_key_list_key(key_list, i));
2076  }
2077  LDNS_FREE(key_list->_keys);
2078  LDNS_FREE(key_list);
2079 }
2080 
2081 ldns_rr *
2082 ldns_read_anchor_file(const char *filename)
2083 {
2084  FILE *fp;
2085  /*char line[LDNS_MAX_PACKETLEN];*/
2086  char *line = LDNS_XMALLOC(char, LDNS_MAX_PACKETLEN);
2087  int c;
2088  size_t i = 0;
2089  ldns_rr *r;
2090  ldns_status status;
2091  if(!line) {
2092  return NULL;
2093  }
2094 
2095  fp = fopen(filename, "r");
2096  if (!fp) {
2097 #ifdef STDERR_MSGS
2098  fprintf(stderr, "Unable to open %s: %s\n", filename, strerror(errno));
2099 #endif
2100  LDNS_FREE(line);
2101  return NULL;
2102  }
2103 
2104  while ((c = fgetc(fp)) && i+1 < LDNS_MAX_PACKETLEN && c != EOF) {
2105  line[i] = c;
2106  i++;
2107  }
2108  line[i] = '\0';
2109 
2110  fclose(fp);
2111 
2112  if (i <= 0) {
2113 #ifdef STDERR_MSGS
2114  fprintf(stderr, "nothing read from %s", filename);
2115 #endif
2116  LDNS_FREE(line);
2117  return NULL;
2118  } else {
2119  status = ldns_rr_new_frm_str(&r, line, 0, NULL, NULL);
2121  LDNS_FREE(line);
2122  return r;
2123  } else {
2124 #ifdef STDERR_MSGS
2125  fprintf(stderr, "Error creating DNSKEY or DS rr from %s: %s\n", filename, ldns_get_errorstr_by_id(status));
2126 #endif
2127  LDNS_FREE(line);
2128  return NULL;
2129  }
2130  }
2131 }
2132 
2133 char *
2135 {
2136  ldns_buffer *buffer;
2137  char *file_base_name;
2138 
2139  buffer = ldns_buffer_new(255);
2140  ldns_buffer_printf(buffer, "K");
2142  ldns_buffer_printf(buffer,
2143  "+%03u+%05u",
2144  ldns_key_algorithm(key),
2145  ldns_key_keytag(key));
2146  file_base_name = ldns_buffer_export(buffer);
2147  ldns_buffer_free(buffer);
2148  return file_base_name;
2149 }
2150 
2152 {
2154  while(lt->name) {
2155  if(lt->id == algo)
2156  return 1;
2157  lt++;
2158  }
2159  return 0;
2160 }
2161 
2163 {
2164  /* list of (signing algorithm id, alias_name) */
2165  ldns_lookup_table aliases[] = {
2166  /* from bind dnssec-keygen */
2167  {LDNS_SIGN_HMACMD5, "HMAC-MD5"},
2168  {LDNS_SIGN_DSA_NSEC3, "NSEC3DSA"},
2169  {LDNS_SIGN_RSASHA1_NSEC3, "NSEC3RSASHA1"},
2170  /* old ldns usage, now RFC names */
2171 #ifdef USE_DSA
2172  {LDNS_SIGN_DSA_NSEC3, "DSA_NSEC3" },
2173 #endif
2174  {LDNS_SIGN_RSASHA1_NSEC3, "RSASHA1_NSEC3" },
2175 #ifdef USE_GOST
2176  {LDNS_SIGN_ECC_GOST, "GOST"},
2177 #endif
2178  /* compat with possible output */
2179  {LDNS_DH, "DH"},
2180  {LDNS_ECC, "ECC"},
2181  {LDNS_INDIRECT, "INDIRECT"},
2182  {LDNS_PRIVATEDNS, "PRIVATEDNS"},
2183  {LDNS_PRIVATEOID, "PRIVATEOID"},
2184  {0, NULL}};
2187  char *endptr;
2188 
2189  while(lt->name) {
2190  if(strcasecmp(lt->name, name) == 0)
2191  return lt->id;
2192  lt++;
2193  }
2194  lt = aliases;
2195  while(lt->name) {
2196  if(strcasecmp(lt->name, name) == 0)
2197  return lt->id;
2198  lt++;
2199  }
2200  a = strtol(name, &endptr, 10);
2201  if (*name && !*endptr)
2202  return a;
2203 
2204  return 0;
2205 }
void ldns_key_set_external_key(ldns_key *k, void *external_key)
Set the key id data.
Definition: keys.c:1408
implementation of buffers to ease operations
Definition: buffer.h:50
DSA * ldns_key_new_frm_fp_dsa_l(FILE *f, int *line_nr __attribute__((unused)))
Definition: keys.c:915
void ldns_key_set_origttl(ldns_key *k, uint32_t t)
Set the key's original ttl.
Definition: keys.c:1414
DSA * ldns_key_new_frm_fp_dsa(FILE *f)
frm_fp helper function.
Definition: keys.c:909
ldns_key * ldns_key_list_key(const ldns_key_list *key, size_t nr)
returns a pointer to the key in the list at the given position
Definition: keys.c:1451
void ldns_key_assign_dsa_key(ldns_key *k, DSA *d)
Assign the key's dsa data The dsa data will be freed automatically when the key is freed.
Definition: keys.c:1382
void * ldns_buffer_export(ldns_buffer *buffer)
Makes the buffer fixed and returns a pointer to the data.
Definition: buffer.c:150
ldns_rdf * ldns_key_pubkey_owner(const ldns_key *k)
return the public key's owner
Definition: keys.c:1575
void ldns_rdf_deep_free(ldns_rdf *rd)
frees a rdf structure and frees the data.
Definition: rdata.c:230
ldns_signing_algorithm ldns_get_signing_algorithm_by_name(const char *name)
Get signing algorithm by name.
Definition: keys.c:2162
void ldns_rr_set_type(ldns_rr *rr, ldns_rr_type rr_type)
sets the type in the rr.
Definition: rr.c:823
ldns_key * ldns_key_list_pop_key(ldns_key_list *key_list)
pops the last rr from a keylist
Definition: keys.c:1621
b64 string
Definition: rdata.h:68
ldns_rdf * ldns_native2rdf_int16(ldns_rdf_type type, uint16_t value)
returns the rdf containing the native uint16_t representation.
Definition: rdata.c:132
void ldns_key_set_algorithm(ldns_key *k, ldns_signing_algorithm l)
Set the key's algorithm.
Definition: keys.c:1334
void ldns_key_set_use(ldns_key *k, bool v)
Definition: keys.c:1467
int ldns_buffer_printf(ldns_buffer *buffer, const char *format,...)
prints to the buffer, increasing the capacity if required using buffer_reserve().
Definition: buffer.c:99
void ldns_key_set_flags(ldns_key *k, uint16_t f)
Set the key's flags.
Definition: keys.c:1340
ldns_key ** _keys
Definition: keys.h:179
uint32_t ldns_key_origttl(const ldns_key *k)
return the original ttl of the key
Definition: keys.c:1545
Definition: keys.h:48
#define LDNS_XMALLOC(type, count)
Definition: util.h:51
DSA * ldns_key_dsa_key(const ldns_key *k)
returns the (openssl) DSA struct contained in the key
Definition: keys.c:1502
ssize_t ldns_fget_keyword_data_l(FILE *f, const char *keyword, const char *k_del, char *data, const char *d_del, size_t data_limit, int *line_nr)
Definition: parse.c:188
size_t ldns_rdf_size(const ldns_rdf *rd)
returns the size of the rdf.
Definition: rdata.c:24
void * external_key
the key structure can also just point to some external key data
Definition: keys.h:150
void ldns_buffer_free(ldns_buffer *buffer)
frees the buffer.
Definition: buffer.c:137
char * ldns_key_get_file_base_name(const ldns_key *key)
Returns the 'default base name' for key files; IE.
Definition: keys.c:2134
struct ldns_struct_key::@0 _key
Storage pointers for the types of keys supported.
ldns_rdf * ldns_rdf_clone(const ldns_rdf *rd)
clones a rdf structure.
Definition: rdata.c:222
void ldns_key_free(ldns_key *key)
frees a key structure, but not its internal data structures
Definition: keys.c:2046
void ldns_key_set_rsa_key(ldns_key *k, RSA *r)
Set the key's rsa data.
Definition: keys.c:1354
#define LDNS_MAX_PACKETLEN
Definition: packet.h:24
void ldns_rr_free(ldns_rr *rr)
frees an RR structure
Definition: rr.c:75
unsigned char * ldns_key_new_frm_fp_hmac(FILE *f, size_t *hmac_size)
frm_fp helper function.
Definition: keys.c:1020
2535typecode
Definition: rr.h:128
EVP_PKEY * key
Definition: keys.h:137
void ldns_key_set_pubkey_owner(ldns_key *k, ldns_rdf *r)
Set the key's pubkey owner.
Definition: keys.c:1432
#define LDNS_XREALLOC(ptr, type, count)
Definition: util.h:57
void ldns_key_list_set_use(ldns_key_list *keys, bool v)
Definition: keys.c:1582
Resource Record.
Definition: rr.h:307
size_t ldns_key_hmac_size(const ldns_key *k)
return the hmac key size
Definition: keys.c:1529
unsigned char * ldns_key_new_frm_fp_hmac_l(FILE *f, int *line_nr __attribute__((unused)), size_t *hmac_size)
Definition: keys.c:1026
ldns_status ldns_key_new_frm_engine(ldns_key **key, ENGINE *e, char *key_id, ldns_algorithm alg)
Read the key with the given id from the given engine and store it in the given ldns_key structure.
Definition: keys.c:112
Including this file will include all ldns files, and define some lookup tables.
ldns_rdf * ldns_native2rdf_int8(ldns_rdf_type type, uint8_t value)
returns the rdf containing the native uint8_t repr.
Definition: rdata.c:126
void ldns_key_set_hmac_size(ldns_key *k, size_t hmac_size)
Set the key's hmac size.
Definition: keys.c:1402
signed char _use
Whether to use this key when signing.
Definition: keys.h:129
General key structure, can contain all types of keys that are used in DNSSEC.
Definition: keys.h:126
uint8_t * ldns_rdf_data(const ldns_rdf *rd)
returns the data of the rdf.
Definition: rdata.c:38
int ldns_key_algo_supported(int algo)
See if a key algorithm is supported.
Definition: keys.c:2151
ldns_rr * ldns_read_anchor_file(const char *filename)
Instantiates a DNSKEY or DS RR from file.
Definition: keys.c:2082
#define ATTR_UNUSED(x)
Definition: common.h:69
a key algorithm
Definition: rdata.h:80
enum ldns_enum_signing_algorithm ldns_signing_algorithm
Definition: keys.h:114
#define LDNS_MAX_KEYLEN
Definition: dnssec.h:41
void ldns_key_set_expiration(ldns_key *k, uint32_t e)
Set the key's expiration date (seconds after epoch)
Definition: keys.c:1426
struct ldns_struct_key::@1::@3 dnssec
Some values that influence generated signatures.
ldns_rdf * _pubkey_owner
Owner name of the key.
Definition: keys.h:169
#define LDNS_KEY_ZONE_KEY
Definition: keys.h:37
union ldns_struct_key::@1 _extra
Depending on the key we can have extra data.
bool ldns_key_use(const ldns_key *k)
return the use flag
Definition: keys.c:1475
Same as rr_list, but now for keys.
Definition: keys.h:176
int ldns_key_EVP_load_gost_id(void)
Get the PKEY id for GOST, loads GOST into openssl as a side effect.
ldns_status ldns_key_new_frm_fp_l(ldns_key **key, FILE *fp, int *line_nr)
Creates a new private key based on the contents of the file pointed by fp.
Definition: keys.c:417
uint16_t ldns_calc_keytag(const ldns_rr *key)
calculates a keytag of a key for use in DNSSEC.
Definition: dnssec.c:277
RSA * ldns_key_new_frm_fp_rsa_l(FILE *f, int *line_nr)
frm_fp helper function.
Definition: keys.c:737
ldns_status ldns_rdf2buffer_str_dname(ldns_buffer *output, const ldns_rdf *dname)
Print the ldns_rdf containing a dname to the buffer.
Definition: host2str.c:334
void ldns_key_list_free(ldns_key_list *key_list)
Frees a key list structure.
Definition: keys.c:2071
RSA * ldns_key_rsa_key(const ldns_key *k)
returns the (openssl) RSA struct contained in the key
Definition: keys.c:1492
void ldns_key_set_evp_key(ldns_key *k, EVP_PKEY *e)
Set the key's evp key.
Definition: keys.c:1348
RFC4034, RFC3658.
Definition: rr.h:164
bool ldns_key_list_push_key(ldns_key_list *key_list, ldns_key *key)
pushes a key to a keylist
Definition: keys.c:1598
ssize_t ldns_fget_token_l(FILE *f, char *token, const char *delim, size_t limit, int *line_nr)
returns a token/char from the stream F.
Definition: parse.c:31
RSA * ldns_key_new_frm_fp_rsa(FILE *f)
frm_fp helper function.
Definition: keys.c:731
void * ldns_key_external_key(const ldns_key *k)
return the key id key data
Definition: keys.c:1539
void ldns_rr_set_owner(ldns_rr *rr, ldns_rdf *owner)
sets the owner in the rr structure.
Definition: rr.c:799
uint32_t ldns_key_inception(const ldns_key *k)
return the key's inception date
Definition: keys.c:1557
void ldns_key_print(FILE *output, const ldns_key *k)
print a private key to the file output
Definition: keys.c:1321
ldns_rr_type ldns_rr_get_type(const ldns_rr *rr)
returns the type of the rr.
Definition: rr.c:938
unsigned char * ldns_key_hmac_key(const ldns_key *k)
return the hmac key data
Definition: keys.c:1519
const char * name
Definition: util.h:158
enum ldns_enum_status ldns_status
Definition: error.h:134
EVP_PKEY * ldns_key_evp_key(const ldns_key *k)
returns the (openssl) EVP struct contained in the key
Definition: keys.c:1486
ldns_rdf * ldns_rdf_new_frm_data(ldns_rdf_type type, size_t size, const void *data)
allocates a new rdf structure and fills it.
Definition: rdata.c:193
#define LDNS_MALLOC(type)
Memory management macros.
Definition: util.h:49
void ldns_key_set_inception(ldns_key *k, uint32_t i)
Set the key's inception date (seconds after epoch)
Definition: keys.c:1420
ldns_buffer * ldns_buffer_new(size_t capacity)
creates a new buffer with the specified capacity.
Definition: buffer.c:16
Definition: keys.h:47
A general purpose lookup table.
Definition: util.h:156
ldns_signing_algorithm ldns_key_algorithm(const ldns_key *k)
return the signing alg of the key
Definition: keys.c:1461
uint32_t ldns_key_expiration(const ldns_key *k)
return the key's expiration date
Definition: keys.c:1563
ldns_signing_algorithm _alg
Definition: keys.h:127
void ldns_key_set_keytag(ldns_key *k, uint16_t tag)
Set the key's key tag.
Definition: keys.c:1438
size_t _key_count
Definition: keys.h:178
ldns_status ldns_rr_new_frm_str(ldns_rr **newrr, const char *str, uint32_t default_ttl, const ldns_rdf *origin, ldns_rdf **prev)
creates an rr from a string.
Definition: rr.c:668
ldns_status ldns_key_new_frm_fp(ldns_key **k, FILE *fp)
Creates a new priv key based on the contents of the file pointed by fp.
Definition: keys.c:105
bool ldns_rr_push_rdf(ldns_rr *rr, const ldns_rdf *f)
sets rd_field member, it will be placed in the next available spot.
Definition: rr.c:852
ldns_status ldns_str2rdf_b64(ldns_rdf **rd, const char *str)
convert the string with the b64 data into wireformat
Definition: str2host.c:583
ldns_rr * ldns_key2rr(const ldns_key *k)
converts a ldns_key to a public key rr If the key data exists at an external point,...
Definition: keys.c:1801
#define LDNS_MAX_LINELEN
Definition: parse.h:23
Resource record data field.
Definition: rdata.h:177
size_t ldns_key_list_key_count(const ldns_key_list *key_list)
returns the number of keys in the key list
Definition: keys.c:1445
#define LDNS_DNSSEC_KEYPROTO
Definition: dnssec.h:42
ldns_rr * ldns_rr_new(void)
creates a new rr structure.
Definition: rr.c:24
#define LDNS_FREE(ptr)
Definition: util.h:60
void ldns_key_assign_rsa_key(ldns_key *k, RSA *r)
Assign the key's rsa data The rsa data will be freed automatically when the key is freed.
Definition: keys.c:1374
void ldns_key_deep_free(ldns_key *key)
frees a key structure and all its internal data structures, except the data set by ldns_key_set_exter...
Definition: keys.c:2052
uint16_t ldns_key_keytag(const ldns_key *k)
return the keytag
Definition: keys.c:1569
void ldns_key_EVP_unload_gost(void)
Release the engine reference held for the GOST engine.
struct ldns_struct_key::@0::@2 hmac
The key can be an HMAC key.
const char * ldns_get_errorstr_by_id(ldns_status err)
look up a descriptive text by each error.
Definition: error.c:164
void ldns_key_set_dsa_key(ldns_key *k, DSA *d)
Set the key's dsa data The dsa data should be freed by the user.
Definition: keys.c:1362
uint16_t ldns_key_flags(const ldns_key *k)
return the flag of the key
Definition: keys.c:1551
char * ldns_key2str(const ldns_key *k)
Converts a private key to the test presentation fmt and returns that as a char *.
Definition: host2str.c:2507
Definition: keys.h:49
int ldns_b64_pton(char const *src, uint8_t *target, size_t targsize)
void ldns_key_list_set_key_count(ldns_key_list *key, size_t count)
Set the keylist's key count to count.
Definition: keys.c:1592
ldns_lookup_table ldns_signing_algorithms[]
Definition: keys.c:31
ldns_key_list * ldns_key_list_new(void)
Creates a new empty key list.
Definition: keys.c:66
ldns_key * ldns_key_new_frm_algorithm(ldns_signing_algorithm alg, uint16_t size)
Creates a new key based on the algorithm.
Definition: keys.c:1081
ldns_key * ldns_key_new(void)
Creates a new empty key structure.
Definition: keys.c:79
void ldns_key_set_hmac_key(ldns_key *k, unsigned char *hmac)
Set the key's hmac data.
Definition: keys.c:1396
enum ldns_enum_algorithm ldns_algorithm
Definition: keys.h:72