2 * keys.c handle private keys for use in DNSSEC
4 * This module should hide some of the openSSL complexities
5 * and give a general interface for private keys and hmac
8 * (c) NLnet Labs, 2004-2006
10 * See the file LICENSE for the license
13 #include <ldns/config.h>
15 #include <ldns/ldns.h>
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>
24 #include <openssl/dsa.h>
26 #ifndef OPENSSL_NO_ENGINE
27 #include <openssl/engine.h>
31 ldns_lookup_table ldns_signing_algorithms
[] = {
32 { LDNS_SIGN_RSAMD5
, "RSAMD5" },
33 { LDNS_SIGN_RSASHA1
, "RSASHA1" },
34 { LDNS_SIGN_RSASHA1_NSEC3
, "RSASHA1-NSEC3-SHA1" },
36 { LDNS_SIGN_RSASHA256
, "RSASHA256" },
37 { LDNS_SIGN_RSASHA512
, "RSASHA512" },
40 { LDNS_SIGN_ECC_GOST
, "ECC-GOST" },
43 { LDNS_SIGN_ECDSAP256SHA256
, "ECDSAP256SHA256" },
44 { LDNS_SIGN_ECDSAP384SHA384
, "ECDSAP384SHA384" },
47 { LDNS_SIGN_ED25519
, "ED25519" },
50 { LDNS_SIGN_ED448
, "ED448" },
53 { LDNS_SIGN_DSA
, "DSA" },
54 { LDNS_SIGN_DSA_NSEC3
, "DSA-NSEC3-SHA1" },
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" },
66 ldns_key_list_new(void)
68 ldns_key_list
*key_list
= LDNS_MALLOC(ldns_key_list
);
72 key_list
->_key_count
= 0;
73 key_list
->_keys
= NULL
;
83 newkey
= LDNS_MALLOC(ldns_key
);
87 /* some defaults - not sure whether to do this */
88 ldns_key_set_use(newkey
, true);
89 ldns_key_set_flags(newkey
, LDNS_KEY_ZONE_KEY
);
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
);
96 ldns_key_set_evp_key(newkey
, NULL
);
98 ldns_key_set_hmac_key(newkey
, NULL
);
99 ldns_key_set_external_key(newkey
, NULL
);
105 ldns_key_new_frm_fp(ldns_key
**k
, FILE *fp
)
107 return ldns_key_new_frm_fp_l(k
, fp
, NULL
);
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
)
117 if(!k
) return LDNS_STATUS_MEM_ERR
;
119 k
->_key
.key
= ENGINE_load_private_key(e
, key_id
, UI_OpenSSL(), NULL
);
122 return LDNS_STATUS_ERR
;
124 ldns_key_set_algorithm(k
, (ldns_signing_algorithm
) alg
);
127 return LDNS_STATUS_ENGINE_KEY_NOT_LOADED
;
131 return LDNS_STATUS_OK
;
136 /** store GOST engine reference loaded into OpenSSL library */
137 ENGINE
* ldns_gost_engine
= NULL
;
140 ldns_key_EVP_load_gost_id(void)
142 static int gost_id
= 0;
143 const EVP_PKEY_ASN1_METHOD
* meth
;
146 if(gost_id
) return gost_id
;
148 /* see if configuration loaded gost implementation from other engine*/
149 meth
= EVP_PKEY_asn1_find_str(NULL
, "gost2001", -1);
151 EVP_PKEY_asn1_get0_info(&gost_id
, NULL
, NULL
, NULL
, NULL
, meth
);
155 /* see if engine can be loaded already */
156 e
= ENGINE_by_id("gost");
158 /* load it ourself, in case statically linked */
159 ENGINE_load_builtin_engines();
160 ENGINE_load_dynamic();
161 e
= ENGINE_by_id("gost");
164 /* no gost engine in openssl */
167 if(!ENGINE_set_default(e
, ENGINE_METHOD_ALL
)) {
173 meth
= EVP_PKEY_asn1_find_str(&e
, "gost2001", -1);
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
;
184 EVP_PKEY_asn1_get0_info(&gost_id
, NULL
, NULL
, NULL
, NULL
, meth
);
188 void ldns_key_EVP_unload_gost(void)
190 if(ldns_gost_engine
) {
191 ENGINE_finish(ldns_gost_engine
);
192 ENGINE_free(ldns_gost_engine
);
193 ldns_gost_engine
= NULL
;
197 /** read GOST private key */
199 ldns_key_new_frm_fp_gost_l(FILE* fp
, int* line_nr
)
202 const unsigned char* pp
;
205 ldns_rdf
* b64rdf
= NULL
;
207 gost_id
= ldns_key_EVP_load_gost_id();
211 if (ldns_fget_keyword_data_l(fp
, "GostAsn1", ": ", token
, "\n",
212 sizeof(token
), line_nr
) == -1)
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)
220 if(ldns_str2rdf_b64(&b64rdf
, token
) != LDNS_STATUS_OK
)
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
);
230 /** calculate public key from private key */
232 ldns_EC_KEY_calc_public(EC_KEY
* ec
)
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
);
243 if(!EC_POINT_mul(group
, pub_key
, EC_KEY_get0_private_key(ec
),
245 EC_POINT_free(pub_key
);
248 if(EC_KEY_set_public_key(ec
, pub_key
) == 0) {
249 EC_POINT_free(pub_key
);
252 EC_POINT_free(pub_key
);
256 /** read ECDSA private key */
258 ldns_key_new_frm_fp_ecdsa_l(FILE* fp
, ldns_algorithm alg
, int* line_nr
)
261 ldns_rdf
* b64rdf
= NULL
;
266 if (ldns_fget_keyword_data_l(fp
, "PrivateKey", ": ", token
, "\n",
267 sizeof(token
), line_nr
) == -1)
269 if(ldns_str2rdf_b64(&b64rdf
, token
) != LDNS_STATUS_OK
)
271 pp
= (unsigned char*)ldns_rdf_data(b64rdf
);
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
);
279 ldns_rdf_deep_free(b64rdf
);
282 bn
= BN_bin2bn(pp
, (int)ldns_rdf_size(b64rdf
), NULL
);
283 ldns_rdf_deep_free(b64rdf
);
288 EC_KEY_set_private_key(ec
, bn
);
290 if(!ldns_EC_KEY_calc_public(ec
)) {
295 evp_key
= EVP_PKEY_new();
300 if (!EVP_PKEY_assign_EC_KEY(evp_key
, ec
)) {
301 EVP_PKEY_free(evp_key
);
310 /** turn private key buffer into EC_KEY structure */
312 ldns_ed25519_priv_raw(uint8_t* pkey
, int plen
)
314 const unsigned char* pp
;
317 uint8_t pre
[] = {0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
318 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20};
320 /* ASN looks like this for ED25519 public key
321 * 302a300506032b6570032100 <32byteskey>
322 * for ED25519 private key
323 * 302e020100300506032b657004220420 <32bytes>
325 * for X25519 this was
326 * 30320201010420 <32byteskey>
327 * andparameters a00b06092b06010401da470f01
328 * (noparameters, preamble is 30250201010420).
329 * the key is reversed (little endian).
331 buflen
= pre_len
+ plen
;
332 if((size_t)buflen
> sizeof(buf
))
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]; */
340 return d2i_PrivateKey(NID_ED25519
, NULL
, &pp
, buflen
);
343 /** read ED25519 private key */
345 ldns_key_new_frm_fp_ed25519_l(FILE* fp
, int* line_nr
)
348 ldns_rdf
* b64rdf
= NULL
;
350 if (ldns_fget_keyword_data_l(fp
, "PrivateKey", ": ", token
, "\n",
351 sizeof(token
), line_nr
) == -1)
353 if(ldns_str2rdf_b64(&b64rdf
, token
) != LDNS_STATUS_OK
)
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 */
362 evp_key
= ldns_ed25519_priv_raw(ldns_rdf_data(b64rdf
),
363 (int)ldns_rdf_size(b64rdf
));
364 ldns_rdf_deep_free(b64rdf
);
370 /** turn private key buffer into EC_KEY structure */
372 ldns_ed448_priv_raw(uint8_t* pkey
, int plen
)
374 const unsigned char* pp
;
377 uint8_t pre
[] = {0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39};
379 /* ASN looks like this for ED448
380 * 3047020100300506032b6571043b0439 <57bytekey>
381 * the key is reversed (little endian).
383 buflen
= pre_len
+ plen
;
384 if((size_t)buflen
> sizeof(buf
))
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]; */
392 return d2i_PrivateKey(NID_ED448
, NULL
, &pp
, buflen
);
395 /** read ED448 private key */
397 ldns_key_new_frm_fp_ed448_l(FILE* fp
, int* line_nr
)
400 ldns_rdf
* b64rdf
= NULL
;
402 if (ldns_fget_keyword_data_l(fp
, "PrivateKey", ": ", token
, "\n",
403 sizeof(token
), line_nr
) == -1)
405 if(ldns_str2rdf_b64(&b64rdf
, token
) != LDNS_STATUS_OK
)
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
);
417 ldns_key_new_frm_fp_l(ldns_key
**key
, FILE *fp
, int *line_nr
)
421 ldns_signing_algorithm alg
;
430 #endif /* HAVE_SSL */
434 d
= LDNS_XMALLOC(char, LDNS_MAX_LINELEN
);
438 return LDNS_STATUS_MEM_ERR
;
443 /* the file is highly structured. Do this in sequence */
445 * Private-key-format: v1.x.
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 */
455 return LDNS_STATUS_SYNTAX_ERR
;
457 if (strncmp(d
, "v1.", 3) != 0) {
460 return LDNS_STATUS_SYNTAX_VERSION_ERR
;
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 */
470 return LDNS_STATUS_SYNTAX_ALG_ERR
;
473 if (strncmp(d
, "1 RSA", 2) == 0) {
474 alg
= LDNS_SIGN_RSAMD5
;
476 if (strncmp(d
, "2 DH", 2) == 0) {
477 alg
= (ldns_signing_algorithm
)LDNS_DH
;
479 if (strncmp(d
, "3 DSA", 2) == 0) {
484 fprintf(stderr
, "Warning: DSA not compiled into this ");
485 fprintf(stderr
, "version of ldns\n");
489 if (strncmp(d
, "4 ECC", 2) == 0) {
490 alg
= (ldns_signing_algorithm
)LDNS_ECC
;
492 if (strncmp(d
, "5 RSASHA1", 2) == 0) {
493 alg
= LDNS_SIGN_RSASHA1
;
495 if (strncmp(d
, "6 DSA", 2) == 0) {
497 alg
= LDNS_SIGN_DSA_NSEC3
;
500 fprintf(stderr
, "Warning: DSA not compiled into this ");
501 fprintf(stderr
, "version of ldns\n");
505 if (strncmp(d
, "7 RSASHA1", 2) == 0) {
506 alg
= LDNS_SIGN_RSASHA1_NSEC3
;
509 if (strncmp(d
, "8 RSASHA256", 2) == 0) {
511 alg
= LDNS_SIGN_RSASHA256
;
514 fprintf(stderr
, "Warning: SHA256 not compiled into this ");
515 fprintf(stderr
, "version of ldns\n");
519 if (strncmp(d
, "10 RSASHA512", 3) == 0) {
521 alg
= LDNS_SIGN_RSASHA512
;
524 fprintf(stderr
, "Warning: SHA512 not compiled into this ");
525 fprintf(stderr
, "version of ldns\n");
529 if (strncmp(d
, "12 ECC-GOST", 3) == 0) {
531 alg
= LDNS_SIGN_ECC_GOST
;
534 fprintf(stderr
, "Warning: ECC-GOST not compiled into this ");
535 fprintf(stderr
, "version of ldns, use --enable-gost\n");
539 if (strncmp(d
, "13 ECDSAP256SHA256", 3) == 0) {
541 alg
= LDNS_SIGN_ECDSAP256SHA256
;
544 fprintf(stderr
, "Warning: ECDSA not compiled into this ");
545 fprintf(stderr
, "version of ldns, use --enable-ecdsa\n");
549 if (strncmp(d
, "14 ECDSAP384SHA384", 3) == 0) {
551 alg
= LDNS_SIGN_ECDSAP384SHA384
;
554 fprintf(stderr
, "Warning: ECDSA not compiled into this ");
555 fprintf(stderr
, "version of ldns, use --enable-ecdsa\n");
559 if (strncmp(d
, "15 ED25519", 3) == 0) {
561 alg
= LDNS_SIGN_ED25519
;
564 fprintf(stderr
, "Warning: ED25519 not compiled into this ");
565 fprintf(stderr
, "version of ldns, use --enable-ed25519\n");
569 if (strncmp(d
, "16 ED448", 3) == 0) {
571 alg
= LDNS_SIGN_ED448
;
574 fprintf(stderr
, "Warning: ED448 not compiled into this ");
575 fprintf(stderr
, "version of ldns, use --enable-ed448\n");
579 if (strncmp(d
, "157 HMAC-MD5", 4) == 0) {
580 alg
= LDNS_SIGN_HMACMD5
;
582 if (strncmp(d
, "158 HMAC-SHA1", 4) == 0) {
583 alg
= LDNS_SIGN_HMACSHA1
;
585 if (strncmp(d
, "159 HMAC-SHA256", 4) == 0) {
586 alg
= LDNS_SIGN_HMACSHA256
;
588 /* For compatibility with dnssec-keygen */
589 if (strncmp(d
, "161 ", 4) == 0) {
590 alg
= LDNS_SIGN_HMACSHA1
;
592 if (strncmp(d
, "162 HMAC-SHA224", 4) == 0) {
593 alg
= LDNS_SIGN_HMACSHA224
;
595 /* For compatibility with dnssec-keygen */
596 if (strncmp(d
, "163 ", 4) == 0) {
597 alg
= LDNS_SIGN_HMACSHA256
;
599 if (strncmp(d
, "164 HMAC-SHA384", 4) == 0) {
600 alg
= LDNS_SIGN_HMACSHA384
;
602 if (strncmp(d
, "165 HMAC-SHA512", 4) == 0) {
603 alg
= LDNS_SIGN_HMACSHA512
;
608 case LDNS_SIGN_RSAMD5
:
609 case LDNS_SIGN_RSASHA1
:
610 case LDNS_SIGN_RSASHA1_NSEC3
:
612 case LDNS_SIGN_RSASHA256
:
613 case LDNS_SIGN_RSASHA512
:
615 ldns_key_set_algorithm(k
, alg
);
617 rsa
= ldns_key_new_frm_fp_rsa_l(fp
, line_nr
);
620 return LDNS_STATUS_ERR
;
622 ldns_key_assign_rsa_key(k
, rsa
);
623 #endif /* HAVE_SSL */
627 case LDNS_SIGN_DSA_NSEC3
:
628 ldns_key_set_algorithm(k
, alg
);
630 dsa
= ldns_key_new_frm_fp_dsa_l(fp
, line_nr
);
633 return LDNS_STATUS_ERR
;
635 ldns_key_assign_dsa_key(k
, dsa
);
636 #endif /* HAVE_SSL */
639 case LDNS_SIGN_HMACMD5
:
640 case LDNS_SIGN_HMACSHA1
:
641 case LDNS_SIGN_HMACSHA224
:
642 case LDNS_SIGN_HMACSHA256
:
643 case LDNS_SIGN_HMACSHA384
:
644 case LDNS_SIGN_HMACSHA512
:
645 ldns_key_set_algorithm(k
, alg
);
647 hmac
= ldns_key_new_frm_fp_hmac_l(fp
, line_nr
, &hmac_size
);
650 return LDNS_STATUS_ERR
;
652 ldns_key_set_hmac_size(k
, hmac_size
);
653 ldns_key_set_hmac_key(k
, hmac
);
654 #endif /* HAVE_SSL */
656 case LDNS_SIGN_ECC_GOST
:
657 ldns_key_set_algorithm(k
, alg
);
658 #if defined(HAVE_SSL) && defined(USE_GOST)
659 if(!ldns_key_EVP_load_gost_id()) {
661 return LDNS_STATUS_CRYPTO_ALGO_NOT_IMPL
;
663 ldns_key_set_evp_key(k
,
664 ldns_key_new_frm_fp_gost_l(fp
, line_nr
));
668 return LDNS_STATUS_ERR
;
674 case LDNS_SIGN_ECDSAP256SHA256
:
675 case LDNS_SIGN_ECDSAP384SHA384
:
676 ldns_key_set_algorithm(k
, alg
);
677 ldns_key_set_evp_key(k
,
678 ldns_key_new_frm_fp_ecdsa_l(fp
, (ldns_algorithm
)alg
, line_nr
));
682 return LDNS_STATUS_ERR
;
688 case LDNS_SIGN_ED25519
:
689 ldns_key_set_algorithm(k
, alg
);
690 ldns_key_set_evp_key(k
,
691 ldns_key_new_frm_fp_ed25519_l(fp
, line_nr
));
695 return LDNS_STATUS_ERR
;
701 case LDNS_SIGN_ED448
:
702 ldns_key_set_algorithm(k
, alg
);
703 ldns_key_set_evp_key(k
,
704 ldns_key_new_frm_fp_ed448_l(fp
, line_nr
));
708 return LDNS_STATUS_ERR
;
715 return LDNS_STATUS_SYNTAX_ALG_ERR
;
717 key_rr
= ldns_key2rr(k
);
718 ldns_key_set_keytag(k
, ldns_calc_keytag(key_rr
));
719 ldns_rr_free(key_rr
);
723 return LDNS_STATUS_OK
;
726 return LDNS_STATUS_ERR
;
731 ldns_key_new_frm_fp_rsa(FILE *f
)
733 return ldns_key_new_frm_fp_rsa_l(f
, NULL
);
737 ldns_key_new_frm_fp_rsa_l(FILE *f
, int *line_nr
)
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
768 BIGNUM
*n
=NULL
, *e
=NULL
, *d
=NULL
, *p
=NULL
, *q
=NULL
,
769 *dmp1
=NULL
, *dmq1
=NULL
, *iqmp
=NULL
;
771 b
= LDNS_XMALLOC(char, LDNS_MAX_LINELEN
);
772 buf
= LDNS_XMALLOC(uint8_t, LDNS_MAX_LINELEN
);
774 if (!b
|| !rsa
|| !buf
) {
778 /* I could use functions again, but that seems an overkill,
779 * although this also looks tedious
782 /* Modules, rsa->n */
783 if (ldns_fget_keyword_data_l(f
, "Modulus", ": ", b
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
786 i
= ldns_b64_pton((const char*)b
, buf
, ldns_b64_ntop_calculate_size(strlen(b
)));
788 n
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
793 /* PublicExponent, rsa->e */
794 if (ldns_fget_keyword_data_l(f
, "PublicExponent", ": ", b
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
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
);
803 /* PrivateExponent, rsa->d */
804 if (ldns_fget_keyword_data_l(f
, "PrivateExponent", ": ", b
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
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
);
814 if (ldns_fget_keyword_data_l(f
, "Prime1", ": ", b
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
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
);
824 if (ldns_fget_keyword_data_l(f
, "Prime2", ": ", b
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
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
);
833 /* Exponent1, rsa->dmp1 */
834 if (ldns_fget_keyword_data_l(f
, "Exponent1", ": ", b
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
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
);
843 /* Exponent2, rsa->dmq1 */
844 if (ldns_fget_keyword_data_l(f
, "Exponent2", ": ", b
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
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
);
853 /* Coefficient, rsa->iqmp */
854 if (ldns_fget_keyword_data_l(f
, "Coefficient", ": ", b
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
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
);
864 #if OPENSSL_VERSION_NUMBER < 0x10100000 || (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x20700000)
876 if(!RSA_set0_key(rsa
, n
, e
, d
))
881 if(!RSA_set0_factors(rsa
, p
, q
))
885 if(!RSA_set0_crt_params(rsa
, dmp1
, dmq1
, iqmp
))
910 ldns_key_new_frm_fp_dsa(FILE *f
)
912 return ldns_key_new_frm_fp_dsa_l(f
, NULL
);
916 ldns_key_new_frm_fp_dsa_l(FILE *f
, ATTR_UNUSED(int *line_nr
))
922 BIGNUM
*p
=NULL
, *q
=NULL
, *g
=NULL
, *priv_key
=NULL
, *pub_key
=NULL
;
924 d
= LDNS_XMALLOC(char, LDNS_MAX_LINELEN
);
925 buf
= LDNS_XMALLOC(uint8_t, LDNS_MAX_LINELEN
);
927 if (!d
|| !dsa
|| !buf
) {
931 /* the line parser removes the () from the input... */
934 if (ldns_fget_keyword_data_l(f
, "Primep", ": ", d
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
937 i
= ldns_b64_pton((const char*)d
, buf
, ldns_b64_ntop_calculate_size(strlen(d
)));
939 p
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
944 /* Subprime, dsa->q */
945 if (ldns_fget_keyword_data_l(f
, "Subprimeq", ": ", d
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
948 i
= ldns_b64_pton((const char*)d
, buf
, ldns_b64_ntop_calculate_size(strlen(d
)));
949 q
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
955 if (ldns_fget_keyword_data_l(f
, "Baseg", ": ", d
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
958 i
= ldns_b64_pton((const char*)d
, buf
, ldns_b64_ntop_calculate_size(strlen(d
)));
959 g
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
964 /* Private key, dsa->priv_key */
965 if (ldns_fget_keyword_data_l(f
, "Private_valuex", ": ", d
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
968 i
= ldns_b64_pton((const char*)d
, buf
, ldns_b64_ntop_calculate_size(strlen(d
)));
969 priv_key
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
974 /* Public key, dsa->priv_key */
975 if (ldns_fget_keyword_data_l(f
, "Public_valuey", ": ", d
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
978 i
= ldns_b64_pton((const char*)d
, buf
, ldns_b64_ntop_calculate_size(strlen(d
)));
979 pub_key
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
985 #if OPENSSL_VERSION_NUMBER < 0x10100000 || (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x20700000)
990 dsa
->priv_key
= priv_key
;
991 dsa
->pub_key
= pub_key
;
994 if(!DSA_set0_pqg(dsa
, p
, q
, g
))
999 if(!DSA_set0_key(dsa
, pub_key
, priv_key
))
1019 #endif /* USE_DSA */
1022 ldns_key_new_frm_fp_hmac(FILE *f
, size_t *hmac_size
)
1024 return ldns_key_new_frm_fp_hmac_l(f
, NULL
, hmac_size
);
1028 ldns_key_new_frm_fp_hmac_l( FILE *f
1029 , ATTR_UNUSED(int *line_nr
)
1034 char d
[LDNS_MAX_LINELEN
];
1035 unsigned char *buf
= NULL
;
1037 *hmac_size
= ldns_fget_keyword_data_l(f
, "Key", ": ", d
, "\n",
1038 LDNS_MAX_LINELEN
, line_nr
) == -1
1040 : (buf
= LDNS_XMALLOC( unsigned char, (bufsz
=
1041 ldns_b64_ntop_calculate_size(strlen(d
))))) == NULL
1043 : (size_t) ldns_b64_pton((const char*)d
, buf
, bufsz
);
1046 #endif /* HAVE_SSL */
1050 ldns_gen_gost_key(void)
1054 int gost_id
= ldns_key_EVP_load_gost_id();
1057 ctx
= EVP_PKEY_CTX_new_id(gost_id
, NULL
);
1059 /* the id should be available now */
1062 if(EVP_PKEY_CTX_ctrl_str(ctx
, "paramset", "A") <= 0) {
1063 /* cannot set paramset */
1064 EVP_PKEY_CTX_free(ctx
);
1068 if(EVP_PKEY_keygen_init(ctx
) <= 0) {
1069 EVP_PKEY_CTX_free(ctx
);
1072 if(EVP_PKEY_keygen(ctx
, &p
) <= 0) {
1074 EVP_PKEY_CTX_free(ctx
);
1077 EVP_PKEY_CTX_free(ctx
);
1083 ldns_key_new_frm_algorithm(ldns_signing_algorithm alg
, uint16_t size
)
1089 #endif /* USE_DSA */
1093 # ifdef HAVE_EVP_PKEY_KEYGEN
1100 uint16_t offset
= 0;
1102 unsigned char *hmac
;
1109 case LDNS_SIGN_RSAMD5
:
1110 case LDNS_SIGN_RSASHA1
:
1111 case LDNS_SIGN_RSASHA1_NSEC3
:
1112 case LDNS_SIGN_RSASHA256
:
1113 case LDNS_SIGN_RSASHA512
:
1115 #ifdef HAVE_EVP_PKEY_KEYGEN
1116 ctx
= EVP_PKEY_CTX_new_id(EVP_PKEY_RSA
, NULL
);
1121 if(EVP_PKEY_keygen_init(ctx
) <= 0) {
1123 EVP_PKEY_CTX_free(ctx
);
1126 if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx
, size
) <= 0) {
1128 EVP_PKEY_CTX_free(ctx
);
1132 if (EVP_PKEY_keygen(ctx
, &k
->_key
.key
) <= 0) {
1134 EVP_PKEY_CTX_free(ctx
);
1138 EVP_PKEY_CTX_free(ctx
);
1139 #else /* HAVE_EVP_PKEY_KEYGEN */
1140 r
= RSA_generate_key((int)size
, RSA_F4
, NULL
, NULL
);
1145 if (RSA_check_key(r
) != 1) {
1149 ldns_key_set_rsa_key(k
, r
);
1151 #endif /* HAVE_EVP_PKEY_KEYGEN */
1152 #endif /* HAVE_SSL */
1156 case LDNS_SIGN_DSA_NSEC3
:
1158 # if OPENSSL_VERSION_NUMBER < 0x00908000L
1159 d
= DSA_generate_parameters((int)size
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1166 if (! (d
= DSA_new())) {
1170 if (! DSA_generate_parameters_ex(d
, (int)size
, NULL
, 0, NULL
, NULL
, NULL
)) {
1176 if (DSA_generate_key(d
) != 1) {
1180 ldns_key_set_dsa_key(k
, d
);
1182 #endif /* HAVE_SSL */
1183 #endif /* USE_DSA */
1185 case LDNS_SIGN_HMACMD5
:
1186 case LDNS_SIGN_HMACSHA1
:
1187 case LDNS_SIGN_HMACSHA224
:
1188 case LDNS_SIGN_HMACSHA256
:
1189 case LDNS_SIGN_HMACSHA384
:
1190 case LDNS_SIGN_HMACSHA512
:
1195 #endif /* HAVE_SSL */
1197 ldns_key_set_hmac_size(k
, size
);
1199 hmac
= LDNS_XMALLOC(unsigned char, size
);
1205 if (RAND_bytes(hmac
, (int) size
) != 1) {
1211 while (offset
+ sizeof(i
) < size
) {
1213 memcpy(&hmac
[offset
], &i
, sizeof(i
));
1214 offset
+= sizeof(i
);
1216 if (offset
< size
) {
1218 memcpy(&hmac
[offset
], &i
, size
- offset
);
1220 #endif /* HAVE_SSL */
1221 ldns_key_set_hmac_key(k
, hmac
);
1223 ldns_key_set_flags(k
, 0);
1225 case LDNS_SIGN_ECC_GOST
:
1226 #if defined(HAVE_SSL) && defined(USE_GOST)
1227 ldns_key_set_evp_key(k
, ldns_gen_gost_key());
1237 #endif /* HAVE_SSL and USE_GOST */
1239 case LDNS_SIGN_ECDSAP256SHA256
:
1240 case LDNS_SIGN_ECDSAP384SHA384
:
1242 if(alg
== LDNS_SIGN_ECDSAP256SHA256
)
1243 ec
= EC_KEY_new_by_curve_name(NID_X9_62_prime256v1
);
1244 else if(alg
== LDNS_SIGN_ECDSAP384SHA384
)
1245 ec
= EC_KEY_new_by_curve_name(NID_secp384r1
);
1250 if(!EC_KEY_generate_key(ec
)) {
1256 k
->_key
.key
= EVP_PKEY_new();
1262 if (!EVP_PKEY_assign_EC_KEY(k
->_key
.key
, ec
)) {
1274 case LDNS_SIGN_ED25519
:
1275 #ifdef HAVE_EVP_PKEY_KEYGEN
1276 ctx
= EVP_PKEY_CTX_new_id(NID_ED25519
, NULL
);
1281 if(EVP_PKEY_keygen_init(ctx
) <= 0) {
1283 EVP_PKEY_CTX_free(ctx
);
1286 if (EVP_PKEY_keygen(ctx
, &k
->_key
.key
) <= 0) {
1288 EVP_PKEY_CTX_free(ctx
);
1291 EVP_PKEY_CTX_free(ctx
);
1294 #endif /* ED25519 */
1296 case LDNS_SIGN_ED448
:
1297 #ifdef HAVE_EVP_PKEY_KEYGEN
1298 ctx
= EVP_PKEY_CTX_new_id(NID_ED448
, NULL
);
1303 if(EVP_PKEY_keygen_init(ctx
) <= 0) {
1305 EVP_PKEY_CTX_free(ctx
);
1308 if (EVP_PKEY_keygen(ctx
, &k
->_key
.key
) <= 0) {
1310 EVP_PKEY_CTX_free(ctx
);
1313 EVP_PKEY_CTX_free(ctx
);
1318 ldns_key_set_algorithm(k
, alg
);
1323 ldns_key_print(FILE *output
, const ldns_key
*k
)
1325 char *str
= ldns_key2str(k
);
1327 fprintf(output
, "%s", str
);
1329 fprintf(output
, "Unable to convert private key to string\n");
1336 ldns_key_set_algorithm(ldns_key
*k
, ldns_signing_algorithm l
)
1342 ldns_key_set_flags(ldns_key
*k
, uint16_t f
)
1344 k
->_extra
.dnssec
.flags
= f
;
1350 ldns_key_set_evp_key(ldns_key
*k
, EVP_PKEY
*e
)
1356 ldns_key_set_rsa_key(ldns_key
*k
, RSA
*r
)
1358 EVP_PKEY
*key
= EVP_PKEY_new();
1359 EVP_PKEY_set1_RSA(key
, r
);
1364 ldns_key_set_dsa_key(ldns_key
*k
, DSA
*d
)
1367 EVP_PKEY
*key
= EVP_PKEY_new();
1368 EVP_PKEY_set1_DSA(key
, d
);
1376 ldns_key_assign_rsa_key(ldns_key
*k
, RSA
*r
)
1378 EVP_PKEY
*key
= EVP_PKEY_new();
1379 EVP_PKEY_assign_RSA(key
, r
);
1384 ldns_key_assign_dsa_key(ldns_key
*k
, DSA
*d
)
1387 EVP_PKEY
*key
= EVP_PKEY_new();
1388 EVP_PKEY_assign_DSA(key
, d
);
1395 #endif /* HAVE_SSL */
1398 ldns_key_set_hmac_key(ldns_key
*k
, unsigned char *hmac
)
1400 k
->_key
.hmac
.key
= hmac
;
1404 ldns_key_set_hmac_size(ldns_key
*k
, size_t hmac_size
)
1406 k
->_key
.hmac
.size
= hmac_size
;
1410 ldns_key_set_external_key(ldns_key
*k
, void *external_key
)
1412 k
->_key
.external_key
= external_key
;
1416 ldns_key_set_origttl(ldns_key
*k
, uint32_t t
)
1418 k
->_extra
.dnssec
.orig_ttl
= t
;
1422 ldns_key_set_inception(ldns_key
*k
, uint32_t i
)
1424 k
->_extra
.dnssec
.inception
= i
;
1428 ldns_key_set_expiration(ldns_key
*k
, uint32_t e
)
1430 k
->_extra
.dnssec
.expiration
= e
;
1434 ldns_key_set_pubkey_owner(ldns_key
*k
, ldns_rdf
*r
)
1436 k
->_pubkey_owner
= r
;
1440 ldns_key_set_keytag(ldns_key
*k
, uint16_t tag
)
1442 k
->_extra
.dnssec
.keytag
= tag
;
1447 ldns_key_list_key_count(const ldns_key_list
*key_list
)
1449 return key_list
? key_list
->_key_count
: 0;
1453 ldns_key_list_key(const ldns_key_list
*key
, size_t nr
)
1455 if (nr
< ldns_key_list_key_count(key
)) {
1456 return key
->_keys
[nr
];
1462 ldns_signing_algorithm
1463 ldns_key_algorithm(const ldns_key
*k
)
1469 ldns_key_set_use(ldns_key
*k
, bool v
)
1477 ldns_key_use(const ldns_key
*k
)
1488 ldns_key_evp_key(const ldns_key
*k
)
1494 ldns_key_rsa_key(const ldns_key
*k
)
1497 return EVP_PKEY_get1_RSA(k
->_key
.key
);
1504 ldns_key_dsa_key(const ldns_key
*k
)
1508 return EVP_PKEY_get1_DSA(k
->_key
.key
);
1518 #endif /* HAVE_SSL */
1521 ldns_key_hmac_key(const ldns_key
*k
)
1523 if (k
->_key
.hmac
.key
) {
1524 return k
->_key
.hmac
.key
;
1531 ldns_key_hmac_size(const ldns_key
*k
)
1533 if (k
->_key
.hmac
.size
) {
1534 return k
->_key
.hmac
.size
;
1541 ldns_key_external_key(const ldns_key
*k
)
1543 return k
->_key
.external_key
;
1547 ldns_key_origttl(const ldns_key
*k
)
1549 return k
->_extra
.dnssec
.orig_ttl
;
1553 ldns_key_flags(const ldns_key
*k
)
1555 return k
->_extra
.dnssec
.flags
;
1559 ldns_key_inception(const ldns_key
*k
)
1561 return k
->_extra
.dnssec
.inception
;
1565 ldns_key_expiration(const ldns_key
*k
)
1567 return k
->_extra
.dnssec
.expiration
;
1571 ldns_key_keytag(const ldns_key
*k
)
1573 return k
->_extra
.dnssec
.keytag
;
1577 ldns_key_pubkey_owner(const ldns_key
*k
)
1579 return k
->_pubkey_owner
;
1584 ldns_key_list_set_use(ldns_key_list
*keys
, bool v
)
1588 for (i
= 0; i
< ldns_key_list_key_count(keys
); i
++) {
1589 ldns_key_set_use(ldns_key_list_key(keys
, i
), v
);
1594 ldns_key_list_set_key_count(ldns_key_list
*key
, size_t count
)
1596 key
->_key_count
= count
;
1600 ldns_key_list_push_key(ldns_key_list
*key_list
, ldns_key
*key
)
1605 key_count
= ldns_key_list_key_count(key_list
);
1607 /* grow the array */
1608 keys
= LDNS_XREALLOC(
1609 key_list
->_keys
, ldns_key
*, key_count
+ 1);
1614 /* add the new member */
1615 key_list
->_keys
= keys
;
1616 key_list
->_keys
[key_count
] = key
;
1618 ldns_key_list_set_key_count(key_list
, key_count
+ 1);
1623 ldns_key_list_pop_key(ldns_key_list
*key_list
)
1633 key_count
= ldns_key_list_key_count(key_list
);
1634 if (key_count
== 0) {
1638 pop
= ldns_key_list_key(key_list
, key_count
);
1640 /* shrink the array */
1641 a
= LDNS_XREALLOC(key_list
->_keys
, ldns_key
*, key_count
- 1);
1643 key_list
->_keys
= a
;
1646 ldns_key_list_set_key_count(key_list
, key_count
- 1);
1653 /* data pointer must be large enough (LDNS_MAX_KEYLEN) */
1655 ldns_key_rsa2bin(unsigned char *data
, RSA
*k
, uint16_t *size
)
1658 const BIGNUM
*n
=NULL
, *e
=NULL
;
1663 #if OPENSSL_VERSION_NUMBER < 0x10100000 || (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x20700000)
1667 RSA_get0_key(k
, &n
, &e
, NULL
);
1670 if (BN_num_bytes(e
) <= 256) {
1671 /* normally only this path is executed (small factors are
1674 data
[0] = (unsigned char) BN_num_bytes(e
);
1675 i
= BN_bn2bin(e
, data
+ 1);
1676 j
= BN_bn2bin(n
, data
+ i
+ 1);
1677 *size
= (uint16_t) i
+ j
;
1678 } else if (BN_num_bytes(e
) <= 65536) {
1680 /* BN_bn2bin does bigendian, _uint16 also */
1681 ldns_write_uint16(data
+ 1, (uint16_t) BN_num_bytes(e
));
1683 BN_bn2bin(e
, data
+ 3);
1684 BN_bn2bin(n
, data
+ 4 + BN_num_bytes(e
));
1685 *size
= (uint16_t) BN_num_bytes(n
) + 6;
1693 /* data pointer must be large enough (LDNS_MAX_KEYLEN) */
1695 ldns_key_dsa2bin(unsigned char *data
, DSA
*k
, uint16_t *size
)
1698 const BIGNUM
*p
, *q
, *g
;
1699 const BIGNUM
*pub_key
, *priv_key
;
1706 # ifdef HAVE_DSA_GET0_PQG
1707 DSA_get0_pqg(k
, &p
, &q
, &g
);
1709 p
= k
->p
; q
= k
->q
; g
= k
->g
;
1711 # ifdef HAVE_DSA_GET0_KEY
1712 DSA_get0_key(k
, &pub_key
, &priv_key
);
1714 pub_key
= k
->pub_key
; priv_key
= k
->priv_key
;
1717 *size
= (uint16_t)BN_num_bytes(p
);
1718 T
= (*size
- 64) / 8;
1722 fprintf(stderr
, "DSA key with T > 8 (ie. > 1024 bits)");
1723 fprintf(stderr
, " not implemented\n");
1728 /* size = 64 + (T * 8); */
1729 memset(data
, 0, 21 + *size
* 3);
1730 data
[0] = (unsigned char)T
;
1731 BN_bn2bin(q
, data
+ 1 ); /* 20 octects */
1732 BN_bn2bin(p
, data
+ 21 ); /* offset octects */
1733 BN_bn2bin(g
, data
+ 21 + *size
* 2 - BN_num_bytes(g
));
1734 BN_bn2bin(pub_key
,data
+ 21 + *size
* 3 - BN_num_bytes(pub_key
));
1735 *size
= 21 + *size
* 3;
1738 #endif /* USE_DSA */
1742 ldns_key_gost2bin(unsigned char* data
, EVP_PKEY
* k
, uint16_t* size
)
1745 unsigned char* pp
= NULL
;
1746 if(i2d_PUBKEY(k
, &pp
) != 37 + 64) {
1747 /* expect 37 byte(ASN header) and 64 byte(X and Y) */
1751 /* omit ASN header */
1758 #endif /* USE_GOST */
1762 ldns_key_ed255192bin(unsigned char* data
, EVP_PKEY
* k
, uint16_t* size
)
1765 unsigned char* pp
= NULL
;
1766 if(i2d_PUBKEY(k
, &pp
) != 12 + 32) {
1767 /* expect 12 byte(ASN header) and 32 byte(pubkey) */
1771 /* omit ASN header */
1778 #endif /* USE_ED25519 */
1782 ldns_key_ed4482bin(unsigned char* data
, EVP_PKEY
* k
, uint16_t* size
)
1785 unsigned char* pp
= NULL
;
1786 if(i2d_PUBKEY(k
, &pp
) != 12 + 57) {
1787 /* expect 12 byte(ASN header) and 57 byte(pubkey) */
1791 /* omit ASN header */
1798 #endif /* USE_ED448 */
1800 #endif /* HAVE_SSL */
1803 ldns_key2rr(const ldns_key
*k
)
1805 /* this function will convert a the keydata contained in
1806 * rsa/dsa pointers to a DNSKEY rr. It will fill in as
1807 * much as it can, but it does not know about key-flags
1812 unsigned char *bin
= NULL
;
1818 #endif /* USE_DSA */
1819 #endif /* HAVE_SSL */
1823 int internal_data
= 0;
1828 pubkey
= ldns_rr_new();
1830 switch (ldns_key_algorithm(k
)) {
1831 case LDNS_SIGN_HMACMD5
:
1832 case LDNS_SIGN_HMACSHA1
:
1833 case LDNS_SIGN_HMACSHA224
:
1834 case LDNS_SIGN_HMACSHA256
:
1835 case LDNS_SIGN_HMACSHA384
:
1836 case LDNS_SIGN_HMACSHA512
:
1837 ldns_rr_set_type(pubkey
, LDNS_RR_TYPE_KEY
);
1840 ldns_rr_set_type(pubkey
, LDNS_RR_TYPE_DNSKEY
);
1843 /* zero-th rdf - flags */
1844 ldns_rr_push_rdf(pubkey
,
1845 ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16
,
1846 ldns_key_flags(k
)));
1848 ldns_rr_push_rdf(pubkey
,
1849 ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8
, LDNS_DNSSEC_KEYPROTO
));
1851 if (ldns_key_pubkey_owner(k
)) {
1852 ldns_rr_set_owner(pubkey
, ldns_rdf_clone(ldns_key_pubkey_owner(k
)));
1855 /* third - da algorithm */
1856 switch(ldns_key_algorithm(k
)) {
1857 case LDNS_SIGN_RSAMD5
:
1858 case LDNS_SIGN_RSASHA1
:
1859 case LDNS_SIGN_RSASHA1_NSEC3
:
1860 case LDNS_SIGN_RSASHA256
:
1861 case LDNS_SIGN_RSASHA512
:
1862 ldns_rr_push_rdf(pubkey
,
1863 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG
, ldns_key_algorithm(k
)));
1865 rsa
= ldns_key_rsa_key(k
);
1867 bin
= LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN
);
1869 ldns_rr_free(pubkey
);
1872 if (!ldns_key_rsa2bin(bin
, rsa
, &size
)) {
1874 ldns_rr_free(pubkey
);
1885 ldns_rr_push_rdf(pubkey
,
1886 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG
, LDNS_DSA
));
1888 dsa
= ldns_key_dsa_key(k
);
1890 bin
= LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN
);
1892 ldns_rr_free(pubkey
);
1895 if (!ldns_key_dsa2bin(bin
, dsa
, &size
)) {
1897 ldns_rr_free(pubkey
);
1903 #endif /* HAVE_SSL */
1904 #endif /* USE_DSA */
1907 case LDNS_SIGN_DSA_NSEC3
:
1908 ldns_rr_push_rdf(pubkey
,
1909 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG
, LDNS_DSA_NSEC3
));
1911 dsa
= ldns_key_dsa_key(k
);
1913 bin
= LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN
);
1915 ldns_rr_free(pubkey
);
1918 if (!ldns_key_dsa2bin(bin
, dsa
, &size
)) {
1920 ldns_rr_free(pubkey
);
1926 #endif /* HAVE_SSL */
1927 #endif /* USE_DSA */
1929 case LDNS_SIGN_ECC_GOST
:
1930 ldns_rr_push_rdf(pubkey
, ldns_native2rdf_int8(
1931 LDNS_RDF_TYPE_ALG
, ldns_key_algorithm(k
)));
1932 #if defined(HAVE_SSL) && defined(USE_GOST)
1933 bin
= LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN
);
1935 ldns_rr_free(pubkey
);
1939 if (!ldns_key_gost2bin(bin
, k
->_key
.key
, &size
)) {
1941 ldns_rr_free(pubkey
);
1947 ldns_rr_free(pubkey
);
1949 #endif /* HAVE_SSL and USE_GOST */
1951 case LDNS_SIGN_ECDSAP256SHA256
:
1952 case LDNS_SIGN_ECDSAP384SHA384
:
1954 ldns_rr_push_rdf(pubkey
, ldns_native2rdf_int8(
1955 LDNS_RDF_TYPE_ALG
, ldns_key_algorithm(k
)));
1958 ec
= EVP_PKEY_get1_EC_KEY(k
->_key
.key
);
1960 EC_KEY_set_conv_form(ec
, POINT_CONVERSION_UNCOMPRESSED
);
1961 size
= (uint16_t)i2o_ECPublicKey(ec
, NULL
);
1962 if(!i2o_ECPublicKey(ec
, &bin
)) {
1964 ldns_rr_free(pubkey
);
1968 /* move back one byte to shave off the 0x02
1969 * 'uncompressed' indicator that openssl made
1970 * Actually its 0x04 (from implementation).
1972 assert(bin
[0] == POINT_CONVERSION_UNCOMPRESSED
);
1974 memmove(bin
, bin
+1, size
);
1976 /* down the reference count for ec, its still assigned
1981 ldns_rr_free(pubkey
);
1986 case LDNS_SIGN_ED25519
:
1987 ldns_rr_push_rdf(pubkey
, ldns_native2rdf_int8(
1988 LDNS_RDF_TYPE_ALG
, ldns_key_algorithm(k
)));
1989 bin
= LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN
);
1991 ldns_rr_free(pubkey
);
1994 if (!ldns_key_ed255192bin(bin
, k
->_key
.key
, &size
)) {
1996 ldns_rr_free(pubkey
);
2003 case LDNS_SIGN_ED448
:
2004 ldns_rr_push_rdf(pubkey
, ldns_native2rdf_int8(
2005 LDNS_RDF_TYPE_ALG
, ldns_key_algorithm(k
)));
2006 bin
= LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN
);
2008 ldns_rr_free(pubkey
);
2011 if (!ldns_key_ed4482bin(bin
, k
->_key
.key
, &size
)) {
2013 ldns_rr_free(pubkey
);
2019 case LDNS_SIGN_HMACMD5
:
2020 case LDNS_SIGN_HMACSHA1
:
2021 case LDNS_SIGN_HMACSHA224
:
2022 case LDNS_SIGN_HMACSHA256
:
2023 case LDNS_SIGN_HMACSHA384
:
2024 case LDNS_SIGN_HMACSHA512
:
2025 bin
= LDNS_XMALLOC(unsigned char, ldns_key_hmac_size(k
));
2027 ldns_rr_free(pubkey
);
2030 ldns_rr_push_rdf(pubkey
,
2031 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG
,
2032 ldns_key_algorithm(k
)));
2033 size
= ldns_key_hmac_size(k
);
2034 memcpy(bin
, ldns_key_hmac_key(k
), size
);
2038 /* fourth the key bin material */
2039 if (internal_data
) {
2040 keybin
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, size
, bin
);
2042 ldns_rr_push_rdf(pubkey
, keybin
);
2048 ldns_key_free(ldns_key
*key
)
2054 ldns_key_deep_free(ldns_key
*key
)
2056 unsigned char* hmac
;
2057 if (ldns_key_pubkey_owner(key
)) {
2058 ldns_rdf_deep_free(ldns_key_pubkey_owner(key
));
2061 if (ldns_key_evp_key(key
)) {
2062 EVP_PKEY_free(ldns_key_evp_key(key
));
2064 #endif /* HAVE_SSL */
2065 if (ldns_key_hmac_key(key
)) {
2066 hmac
= ldns_key_hmac_key(key
);
2073 ldns_key_list_free(ldns_key_list
*key_list
)
2076 for (i
= 0; i
< ldns_key_list_key_count(key_list
); i
++) {
2077 ldns_key_deep_free(ldns_key_list_key(key_list
, i
));
2079 LDNS_FREE(key_list
->_keys
);
2080 LDNS_FREE(key_list
);
2084 ldns_read_anchor_file(const char *filename
)
2087 /*char line[LDNS_MAX_PACKETLEN];*/
2088 char *line
= LDNS_XMALLOC(char, LDNS_MAX_PACKETLEN
);
2097 fp
= fopen(filename
, "r");
2100 fprintf(stderr
, "Unable to open %s: %s\n", filename
, strerror(errno
));
2106 while ((c
= fgetc(fp
)) && i
+1 < LDNS_MAX_PACKETLEN
&& c
!= EOF
) {
2116 fprintf(stderr
, "nothing read from %s", filename
);
2121 status
= ldns_rr_new_frm_str(&r
, line
, 0, NULL
, NULL
);
2122 if (status
== LDNS_STATUS_OK
&& (ldns_rr_get_type(r
) == LDNS_RR_TYPE_DNSKEY
|| ldns_rr_get_type(r
) == LDNS_RR_TYPE_DS
)) {
2127 fprintf(stderr
, "Error creating DNSKEY or DS rr from %s: %s\n", filename
, ldns_get_errorstr_by_id(status
));
2136 ldns_key_get_file_base_name(const ldns_key
*key
)
2138 ldns_buffer
*buffer
;
2139 char *file_base_name
;
2141 buffer
= ldns_buffer_new(255);
2142 ldns_buffer_printf(buffer
, "K");
2143 (void)ldns_rdf2buffer_str_dname(buffer
, ldns_key_pubkey_owner(key
));
2144 ldns_buffer_printf(buffer
,
2146 ldns_key_algorithm(key
),
2147 ldns_key_keytag(key
));
2148 file_base_name
= ldns_buffer_export(buffer
);
2149 ldns_buffer_free(buffer
);
2150 return file_base_name
;
2153 int ldns_key_algo_supported(int algo
)
2155 ldns_lookup_table
*lt
= ldns_signing_algorithms
;
2164 ldns_signing_algorithm
ldns_get_signing_algorithm_by_name(const char* name
)
2166 /* list of (signing algorithm id, alias_name) */
2167 ldns_lookup_table aliases
[] = {
2168 /* from bind dnssec-keygen */
2169 {LDNS_SIGN_HMACMD5
, "HMAC-MD5"},
2171 {LDNS_SIGN_DSA_NSEC3
, "NSEC3DSA"},
2172 #endif /* USE_DSA */
2173 {LDNS_SIGN_RSASHA1_NSEC3
, "NSEC3RSASHA1"},
2174 /* old ldns usage, now RFC names */
2176 {LDNS_SIGN_DSA_NSEC3
, "DSA_NSEC3" },
2178 {LDNS_SIGN_RSASHA1_NSEC3
, "RSASHA1_NSEC3" },
2180 {LDNS_SIGN_ECC_GOST
, "GOST"},
2182 /* compat with possible output */
2185 {LDNS_INDIRECT
, "INDIRECT"},
2186 {LDNS_PRIVATEDNS
, "PRIVATEDNS"},
2187 {LDNS_PRIVATEOID
, "PRIVATEOID"},
2189 ldns_lookup_table
* lt
= ldns_signing_algorithms
;
2190 ldns_signing_algorithm a
;
2194 if(strcasecmp(lt
->name
, name
) == 0)
2200 if(strcasecmp(lt
->name
, name
) == 0)
2204 a
= strtol(name
, &endptr
, 10);
2205 if (*name
&& !*endptr
)