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/ssl.h>
19 #include <openssl/engine.h>
20 #include <openssl/rand.h>
23 ldns_lookup_table ldns_signing_algorithms[] = {
24 { LDNS_SIGN_RSAMD5, "RSAMD5" },
25 { LDNS_SIGN_RSASHA1, "RSASHA1" },
26 { LDNS_SIGN_RSASHA1_NSEC3, "RSASHA1-NSEC3-SHA1" },
28 { LDNS_SIGN_RSASHA256, "RSASHA256" },
29 { LDNS_SIGN_RSASHA512, "RSASHA512" },
32 { LDNS_SIGN_ECC_GOST, "ECC-GOST" },
35 { LDNS_SIGN_ECDSAP256SHA256, "ECDSAP256SHA256" },
36 { LDNS_SIGN_ECDSAP384SHA384, "ECDSAP384SHA384" },
38 { LDNS_SIGN_DSA, "DSA" },
39 { LDNS_SIGN_DSA_NSEC3, "DSA-NSEC3-SHA1" },
40 { LDNS_SIGN_HMACMD5, "hmac-md5.sig-alg.reg.int" },
41 { LDNS_SIGN_HMACSHA1, "hmac-sha1" },
42 { LDNS_SIGN_HMACSHA256, "hmac-sha256" },
47 ldns_key_list_new(void)
49 ldns_key_list *key_list = LDNS_MALLOC(ldns_key_list);
53 key_list->_key_count = 0;
54 key_list->_keys = NULL;
64 newkey = LDNS_MALLOC(ldns_key);
68 /* some defaults - not sure wether to do this */
69 ldns_key_set_use(newkey, true);
70 ldns_key_set_flags(newkey, LDNS_KEY_ZONE_KEY);
71 ldns_key_set_origttl(newkey, 0);
72 ldns_key_set_keytag(newkey, 0);
73 ldns_key_set_inception(newkey, 0);
74 ldns_key_set_expiration(newkey, 0);
75 ldns_key_set_pubkey_owner(newkey, NULL);
77 ldns_key_set_evp_key(newkey, NULL);
79 ldns_key_set_hmac_key(newkey, NULL);
80 ldns_key_set_external_key(newkey, NULL);
86 ldns_key_new_frm_fp(ldns_key **k, FILE *fp)
88 return ldns_key_new_frm_fp_l(k, fp, NULL);
93 ldns_key_new_frm_engine(ldns_key **key, ENGINE *e, char *key_id, ldns_algorithm alg)
98 if(!k) return LDNS_STATUS_MEM_ERR;
100 k->_key.key = ENGINE_load_private_key(e, key_id, UI_OpenSSL(), NULL);
103 return LDNS_STATUS_ERR;
105 ldns_key_set_algorithm(k, (ldns_signing_algorithm) alg);
108 return LDNS_STATUS_ENGINE_KEY_NOT_LOADED;
112 return LDNS_STATUS_OK;
117 /** store GOST engine reference loaded into OpenSSL library */
118 ENGINE* ldns_gost_engine = NULL;
121 ldns_key_EVP_load_gost_id(void)
123 static int gost_id = 0;
124 const EVP_PKEY_ASN1_METHOD* meth;
127 if(gost_id) return gost_id;
129 /* see if configuration loaded gost implementation from other engine*/
130 meth = EVP_PKEY_asn1_find_str(NULL, "gost2001", -1);
132 EVP_PKEY_asn1_get0_info(&gost_id, NULL, NULL, NULL, NULL, meth);
136 /* see if engine can be loaded already */
137 e = ENGINE_by_id("gost");
139 /* load it ourself, in case statically linked */
140 ENGINE_load_builtin_engines();
141 ENGINE_load_dynamic();
142 e = ENGINE_by_id("gost");
145 /* no gost engine in openssl */
148 if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
154 meth = EVP_PKEY_asn1_find_str(&e, "gost2001", -1);
161 /* Note: do not ENGINE_finish and ENGINE_free the acquired engine
162 * on some platforms this frees up the meth and unloads gost stuff */
163 ldns_gost_engine = e;
165 EVP_PKEY_asn1_get0_info(&gost_id, NULL, NULL, NULL, NULL, meth);
169 void ldns_key_EVP_unload_gost(void)
171 if(ldns_gost_engine) {
172 ENGINE_finish(ldns_gost_engine);
173 ENGINE_free(ldns_gost_engine);
174 ldns_gost_engine = NULL;
178 /** read GOST private key */
180 ldns_key_new_frm_fp_gost_l(FILE* fp, int* line_nr)
183 const unsigned char* pp;
186 ldns_rdf* b64rdf = NULL;
188 gost_id = ldns_key_EVP_load_gost_id();
192 if (ldns_fget_keyword_data_l(fp, "GostAsn1", ": ", token, "\n",
193 sizeof(token), line_nr) == -1)
195 while(strlen(token) < 96) {
196 /* read more b64 from the file, b64 split on multiple lines */
197 if(ldns_fget_token_l(fp, token+strlen(token), "\n",
198 sizeof(token)-strlen(token), line_nr) == -1)
201 if(ldns_str2rdf_b64(&b64rdf, token) != LDNS_STATUS_OK)
203 pp = (unsigned char*)ldns_rdf_data(b64rdf);
204 pkey = d2i_PrivateKey(gost_id, NULL, &pp, (int)ldns_rdf_size(b64rdf));
205 ldns_rdf_deep_free(b64rdf);
211 /** calculate public key from private key */
213 ldns_EC_KEY_calc_public(EC_KEY* ec)
216 const EC_GROUP* group;
217 group = EC_KEY_get0_group(ec);
218 pub_key = EC_POINT_new(group);
219 if(!pub_key) return 0;
220 if(!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group))) {
221 EC_POINT_free(pub_key);
224 if(!EC_POINT_mul(group, pub_key, EC_KEY_get0_private_key(ec),
226 EC_POINT_free(pub_key);
229 if(EC_KEY_set_public_key(ec, pub_key) == 0) {
230 EC_POINT_free(pub_key);
233 EC_POINT_free(pub_key);
237 /** read ECDSA private key */
239 ldns_key_new_frm_fp_ecdsa_l(FILE* fp, ldns_algorithm alg, int* line_nr)
242 ldns_rdf* b64rdf = NULL;
247 if (ldns_fget_keyword_data_l(fp, "PrivateKey", ": ", token, "\n",
248 sizeof(token), line_nr) == -1)
250 if(ldns_str2rdf_b64(&b64rdf, token) != LDNS_STATUS_OK)
252 pp = (unsigned char*)ldns_rdf_data(b64rdf);
254 if(alg == LDNS_ECDSAP256SHA256)
255 ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
256 else if(alg == LDNS_ECDSAP384SHA384)
257 ec = EC_KEY_new_by_curve_name(NID_secp384r1);
260 ldns_rdf_deep_free(b64rdf);
263 bn = BN_bin2bn(pp, (int)ldns_rdf_size(b64rdf), NULL);
264 ldns_rdf_deep_free(b64rdf);
269 EC_KEY_set_private_key(ec, bn);
271 if(!ldns_EC_KEY_calc_public(ec)) {
276 evp_key = EVP_PKEY_new();
281 if (!EVP_PKEY_assign_EC_KEY(evp_key, ec)) {
282 EVP_PKEY_free(evp_key);
291 ldns_key_new_frm_fp_l(ldns_key **key, FILE *fp, int *line_nr)
295 ldns_signing_algorithm alg;
302 #endif /* HAVE_SSL */
306 d = LDNS_XMALLOC(char, LDNS_MAX_LINELEN);
310 return LDNS_STATUS_MEM_ERR;
315 /* the file is highly structured. Do this in sequence */
317 * Private-key-format: v1.x.
321 /* get the key format version number */
322 if (ldns_fget_keyword_data_l(fp, "Private-key-format", ": ", d, "\n",
323 LDNS_MAX_LINELEN, line_nr) == -1) {
324 /* no version information */
327 return LDNS_STATUS_SYNTAX_ERR;
329 if (strncmp(d, "v1.", 3) != 0) {
332 return LDNS_STATUS_SYNTAX_VERSION_ERR;
335 /* get the algorithm type, our file function strip ( ) so there are
336 * not in the return string! */
337 if (ldns_fget_keyword_data_l(fp, "Algorithm", ": ", d, "\n",
338 LDNS_MAX_LINELEN, line_nr) == -1) {
339 /* no alg information */
342 return LDNS_STATUS_SYNTAX_ALG_ERR;
345 if (strncmp(d, "1 RSA", 2) == 0) {
346 alg = LDNS_SIGN_RSAMD5;
348 if (strncmp(d, "2 DH", 2) == 0) {
349 alg = (ldns_signing_algorithm)LDNS_DH;
351 if (strncmp(d, "3 DSA", 2) == 0) {
354 if (strncmp(d, "4 ECC", 2) == 0) {
355 alg = (ldns_signing_algorithm)LDNS_ECC;
357 if (strncmp(d, "5 RSASHA1", 2) == 0) {
358 alg = LDNS_SIGN_RSASHA1;
360 if (strncmp(d, "6 DSA", 2) == 0) {
361 alg = LDNS_SIGN_DSA_NSEC3;
363 if (strncmp(d, "7 RSASHA1", 2) == 0) {
364 alg = LDNS_SIGN_RSASHA1_NSEC3;
367 if (strncmp(d, "8 RSASHA256", 2) == 0) {
369 alg = LDNS_SIGN_RSASHA256;
372 fprintf(stderr, "Warning: SHA256 not compiled into this ");
373 fprintf(stderr, "version of ldns\n");
377 if (strncmp(d, "10 RSASHA512", 3) == 0) {
379 alg = LDNS_SIGN_RSASHA512;
382 fprintf(stderr, "Warning: SHA512 not compiled into this ");
383 fprintf(stderr, "version of ldns\n");
387 if (strncmp(d, "12 ECC-GOST", 3) == 0) {
389 alg = LDNS_SIGN_ECC_GOST;
392 fprintf(stderr, "Warning: ECC-GOST not compiled into this ");
393 fprintf(stderr, "version of ldns, use --enable-gost\n");
397 if (strncmp(d, "13 ECDSAP256SHA256", 3) == 0) {
399 alg = LDNS_SIGN_ECDSAP256SHA256;
402 fprintf(stderr, "Warning: ECDSA not compiled into this ");
403 fprintf(stderr, "version of ldns, use --enable-ecdsa\n");
407 if (strncmp(d, "14 ECDSAP384SHA384", 3) == 0) {
409 alg = LDNS_SIGN_ECDSAP384SHA384;
412 fprintf(stderr, "Warning: ECDSA not compiled into this ");
413 fprintf(stderr, "version of ldns, use --enable-ecdsa\n");
417 if (strncmp(d, "157 HMAC-MD5", 4) == 0) {
418 alg = LDNS_SIGN_HMACMD5;
420 if (strncmp(d, "158 HMAC-SHA1", 4) == 0) {
421 alg = LDNS_SIGN_HMACSHA1;
423 if (strncmp(d, "159 HMAC-SHA256", 4) == 0) {
424 alg = LDNS_SIGN_HMACSHA256;
430 case LDNS_SIGN_RSAMD5:
431 case LDNS_SIGN_RSASHA1:
432 case LDNS_SIGN_RSASHA1_NSEC3:
434 case LDNS_SIGN_RSASHA256:
435 case LDNS_SIGN_RSASHA512:
437 ldns_key_set_algorithm(k, alg);
439 rsa = ldns_key_new_frm_fp_rsa_l(fp, line_nr);
442 return LDNS_STATUS_ERR;
444 ldns_key_assign_rsa_key(k, rsa);
445 #endif /* HAVE_SSL */
448 case LDNS_SIGN_DSA_NSEC3:
449 ldns_key_set_algorithm(k, alg);
451 dsa = ldns_key_new_frm_fp_dsa_l(fp, line_nr);
454 return LDNS_STATUS_ERR;
456 ldns_key_assign_dsa_key(k, dsa);
457 #endif /* HAVE_SSL */
459 case LDNS_SIGN_HMACMD5:
460 case LDNS_SIGN_HMACSHA1:
461 case LDNS_SIGN_HMACSHA256:
462 ldns_key_set_algorithm(k, alg);
464 hmac = ldns_key_new_frm_fp_hmac_l(fp, line_nr, &hmac_size);
467 return LDNS_STATUS_ERR;
469 ldns_key_set_hmac_size(k, hmac_size);
470 ldns_key_set_hmac_key(k, hmac);
471 #endif /* HAVE_SSL */
473 case LDNS_SIGN_ECC_GOST:
474 ldns_key_set_algorithm(k, alg);
475 #if defined(HAVE_SSL) && defined(USE_GOST)
476 if(!ldns_key_EVP_load_gost_id()) {
478 return LDNS_STATUS_CRYPTO_ALGO_NOT_IMPL;
480 ldns_key_set_evp_key(k,
481 ldns_key_new_frm_fp_gost_l(fp, line_nr));
485 return LDNS_STATUS_ERR;
491 case LDNS_SIGN_ECDSAP256SHA256:
492 case LDNS_SIGN_ECDSAP384SHA384:
493 ldns_key_set_algorithm(k, alg);
494 ldns_key_set_evp_key(k,
495 ldns_key_new_frm_fp_ecdsa_l(fp, (ldns_algorithm)alg, line_nr));
499 return LDNS_STATUS_ERR;
506 return LDNS_STATUS_SYNTAX_ALG_ERR;
508 key_rr = ldns_key2rr(k);
509 ldns_key_set_keytag(k, ldns_calc_keytag(key_rr));
510 ldns_rr_free(key_rr);
514 return LDNS_STATUS_OK;
517 return LDNS_STATUS_ERR;
522 ldns_key_new_frm_fp_rsa(FILE *f)
524 return ldns_key_new_frm_fp_rsa_l(f, NULL);
528 ldns_key_new_frm_fp_rsa_l(FILE *f, int *line_nr)
544 * BIGNUM *n; // public modulus
545 * BIGNUM *e; // public exponent
546 * BIGNUM *d; // private exponent
547 * BIGNUM *p; // secret prime factor
548 * BIGNUM *q; // secret prime factor
549 * BIGNUM *dmp1; // d mod (p-1)
550 * BIGNUM *dmq1; // d mod (q-1)
551 * BIGNUM *iqmp; // q^-1 mod p
560 d = LDNS_XMALLOC(char, LDNS_MAX_LINELEN);
561 buf = LDNS_XMALLOC(uint8_t, LDNS_MAX_LINELEN);
563 if (!d || !rsa || !buf) {
567 /* I could use functions again, but that seems an overkill,
568 * allthough this also looks tedious
571 /* Modules, rsa->n */
572 if (ldns_fget_keyword_data_l(f, "Modulus", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
575 i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
577 rsa->n = BN_bin2bn((const char unsigned*)buf, i, NULL);
582 /* PublicExponent, rsa->e */
583 if (ldns_fget_keyword_data_l(f, "PublicExponent", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
586 i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
587 rsa->e = BN_bin2bn((const char unsigned*)buf, i, NULL);
592 /* PrivateExponent, rsa->d */
593 if (ldns_fget_keyword_data_l(f, "PrivateExponent", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
596 i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
597 rsa->d = BN_bin2bn((const char unsigned*)buf, i, NULL);
603 if (ldns_fget_keyword_data_l(f, "Prime1", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
606 i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
607 rsa->p = BN_bin2bn((const char unsigned*)buf, i, NULL);
613 if (ldns_fget_keyword_data_l(f, "Prime2", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
616 i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
617 rsa->q = BN_bin2bn((const char unsigned*)buf, i, NULL);
622 /* Exponent1, rsa->dmp1 */
623 if (ldns_fget_keyword_data_l(f, "Exponent1", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
626 i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
627 rsa->dmp1 = BN_bin2bn((const char unsigned*)buf, i, NULL);
632 /* Exponent2, rsa->dmq1 */
633 if (ldns_fget_keyword_data_l(f, "Exponent2", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
636 i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
637 rsa->dmq1 = BN_bin2bn((const char unsigned*)buf, i, NULL);
642 /* Coefficient, rsa->iqmp */
643 if (ldns_fget_keyword_data_l(f, "Coefficient", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
646 i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
647 rsa->iqmp = BN_bin2bn((const char unsigned*)buf, i, NULL);
665 ldns_key_new_frm_fp_dsa(FILE *f)
667 return ldns_key_new_frm_fp_dsa_l(f, NULL);
671 ldns_key_new_frm_fp_dsa_l(FILE *f, ATTR_UNUSED(int *line_nr))
678 d = LDNS_XMALLOC(char, LDNS_MAX_LINELEN);
679 buf = LDNS_XMALLOC(uint8_t, LDNS_MAX_LINELEN);
681 if (!d || !dsa || !buf) {
685 /* the line parser removes the () from the input... */
688 if (ldns_fget_keyword_data_l(f, "Primep", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
691 i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
693 dsa->p = BN_bin2bn((const char unsigned*)buf, i, NULL);
698 /* Subprime, dsa->q */
699 if (ldns_fget_keyword_data_l(f, "Subprimeq", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
702 i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
703 dsa->q = BN_bin2bn((const char unsigned*)buf, i, NULL);
709 if (ldns_fget_keyword_data_l(f, "Baseg", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
712 i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
713 dsa->g = BN_bin2bn((const char unsigned*)buf, i, NULL);
718 /* Private key, dsa->priv_key */
719 if (ldns_fget_keyword_data_l(f, "Private_valuex", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
722 i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
723 dsa->priv_key = BN_bin2bn((const char unsigned*)buf, i, NULL);
724 if (!dsa->priv_key) {
728 /* Public key, dsa->priv_key */
729 if (ldns_fget_keyword_data_l(f, "Public_valuey", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
732 i = ldns_b64_pton((const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
733 dsa->pub_key = BN_bin2bn((const char unsigned*)buf, i, NULL);
752 ldns_key_new_frm_fp_hmac(FILE *f, size_t *hmac_size)
754 return ldns_key_new_frm_fp_hmac_l(f, NULL, hmac_size);
758 ldns_key_new_frm_fp_hmac_l( FILE *f
759 , ATTR_UNUSED(int *line_nr)
764 char d[LDNS_MAX_LINELEN];
765 unsigned char *buf = NULL;
767 if (ldns_fget_keyword_data_l(f, "Key", ": ", d, "\n", LDNS_MAX_LINELEN, line_nr) == -1) {
770 bufsz = ldns_b64_ntop_calculate_size(strlen(d));
771 buf = LDNS_XMALLOC(unsigned char, bufsz);
772 i = (size_t) ldns_b64_pton((const char*)d, buf, bufsz);
782 #endif /* HAVE_SSL */
786 ldns_gen_gost_key(void)
790 int gost_id = ldns_key_EVP_load_gost_id();
793 ctx = EVP_PKEY_CTX_new_id(gost_id, NULL);
795 /* the id should be available now */
798 if(EVP_PKEY_CTX_ctrl_str(ctx, "paramset", "A") <= 0) {
799 /* cannot set paramset */
800 EVP_PKEY_CTX_free(ctx);
804 if(EVP_PKEY_keygen_init(ctx) <= 0) {
805 EVP_PKEY_CTX_free(ctx);
808 if(EVP_PKEY_keygen(ctx, &p) <= 0) {
810 EVP_PKEY_CTX_free(ctx);
813 EVP_PKEY_CTX_free(ctx);
819 ldns_key_new_frm_algorithm(ldns_signing_algorithm alg, uint16_t size)
839 case LDNS_SIGN_RSAMD5:
840 case LDNS_SIGN_RSASHA1:
841 case LDNS_SIGN_RSASHA1_NSEC3:
842 case LDNS_SIGN_RSASHA256:
843 case LDNS_SIGN_RSASHA512:
845 r = RSA_generate_key((int)size, RSA_F4, NULL, NULL);
850 if (RSA_check_key(r) != 1) {
854 ldns_key_set_rsa_key(k, r);
856 #endif /* HAVE_SSL */
859 case LDNS_SIGN_DSA_NSEC3:
861 d = DSA_generate_parameters((int)size, NULL, 0, NULL, NULL, NULL, NULL);
866 if (DSA_generate_key(d) != 1) {
870 ldns_key_set_dsa_key(k, d);
872 #endif /* HAVE_SSL */
874 case LDNS_SIGN_HMACMD5:
875 case LDNS_SIGN_HMACSHA1:
876 case LDNS_SIGN_HMACSHA256:
881 #endif /* HAVE_SSL */
883 ldns_key_set_hmac_size(k, size);
885 hmac = LDNS_XMALLOC(unsigned char, size);
891 if (RAND_bytes(hmac, (int) size) != 1) {
897 while (offset + sizeof(i) < size) {
899 memcpy(&hmac[offset], &i, sizeof(i));
904 memcpy(&hmac[offset], &i, size - offset);
906 #endif /* HAVE_SSL */
907 ldns_key_set_hmac_key(k, hmac);
909 ldns_key_set_flags(k, 0);
911 case LDNS_SIGN_ECC_GOST:
912 #if defined(HAVE_SSL) && defined(USE_GOST)
913 ldns_key_set_evp_key(k, ldns_gen_gost_key());
923 #endif /* HAVE_SSL and USE_GOST */
925 case LDNS_SIGN_ECDSAP256SHA256:
926 case LDNS_SIGN_ECDSAP384SHA384:
928 if(alg == LDNS_SIGN_ECDSAP256SHA256)
929 ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
930 else if(alg == LDNS_SIGN_ECDSAP384SHA384)
931 ec = EC_KEY_new_by_curve_name(NID_secp384r1);
936 if(!EC_KEY_generate_key(ec)) {
942 k->_key.key = EVP_PKEY_new();
948 if (!EVP_PKEY_assign_EC_KEY(k->_key.key, ec)) {
960 ldns_key_set_algorithm(k, alg);
965 ldns_key_print(FILE *output, const ldns_key *k)
967 char *str = ldns_key2str(k);
969 fprintf(output, "%s", str);
971 fprintf(output, "Unable to convert private key to string\n");
978 ldns_key_set_algorithm(ldns_key *k, ldns_signing_algorithm l)
984 ldns_key_set_flags(ldns_key *k, uint16_t f)
986 k->_extra.dnssec.flags = f;
992 ldns_key_set_evp_key(ldns_key *k, EVP_PKEY *e)
998 ldns_key_set_rsa_key(ldns_key *k, RSA *r)
1000 EVP_PKEY *key = EVP_PKEY_new();
1001 EVP_PKEY_set1_RSA(key, r);
1006 ldns_key_set_dsa_key(ldns_key *k, DSA *d)
1008 EVP_PKEY *key = EVP_PKEY_new();
1009 EVP_PKEY_set1_DSA(key, d);
1014 ldns_key_assign_rsa_key(ldns_key *k, RSA *r)
1016 EVP_PKEY *key = EVP_PKEY_new();
1017 EVP_PKEY_assign_RSA(key, r);
1022 ldns_key_assign_dsa_key(ldns_key *k, DSA *d)
1024 EVP_PKEY *key = EVP_PKEY_new();
1025 EVP_PKEY_assign_DSA(key, d);
1029 #endif /* HAVE_SSL */
1032 ldns_key_set_hmac_key(ldns_key *k, unsigned char *hmac)
1034 k->_key.hmac.key = hmac;
1038 ldns_key_set_hmac_size(ldns_key *k, size_t hmac_size)
1040 k->_key.hmac.size = hmac_size;
1044 ldns_key_set_external_key(ldns_key *k, void *external_key)
1046 k->_key.external_key = external_key;
1050 ldns_key_set_origttl(ldns_key *k, uint32_t t)
1052 k->_extra.dnssec.orig_ttl = t;
1056 ldns_key_set_inception(ldns_key *k, uint32_t i)
1058 k->_extra.dnssec.inception = i;
1062 ldns_key_set_expiration(ldns_key *k, uint32_t e)
1064 k->_extra.dnssec.expiration = e;
1068 ldns_key_set_pubkey_owner(ldns_key *k, ldns_rdf *r)
1070 k->_pubkey_owner = r;
1074 ldns_key_set_keytag(ldns_key *k, uint16_t tag)
1076 k->_extra.dnssec.keytag = tag;
1081 ldns_key_list_key_count(const ldns_key_list *key_list)
1083 return key_list->_key_count;
1087 ldns_key_list_key(const ldns_key_list *key, size_t nr)
1089 if (nr < ldns_key_list_key_count(key)) {
1090 return key->_keys[nr];
1096 ldns_signing_algorithm
1097 ldns_key_algorithm(const ldns_key *k)
1103 ldns_key_set_use(ldns_key *k, bool v)
1111 ldns_key_use(const ldns_key *k)
1122 ldns_key_evp_key(const ldns_key *k)
1128 ldns_key_rsa_key(const ldns_key *k)
1131 return EVP_PKEY_get1_RSA(k->_key.key);
1138 ldns_key_dsa_key(const ldns_key *k)
1141 return EVP_PKEY_get1_DSA(k->_key.key);
1147 #endif /* HAVE_SSL */
1150 ldns_key_hmac_key(const ldns_key *k)
1152 if (k->_key.hmac.key) {
1153 return k->_key.hmac.key;
1160 ldns_key_hmac_size(const ldns_key *k)
1162 if (k->_key.hmac.size) {
1163 return k->_key.hmac.size;
1170 ldns_key_external_key(const ldns_key *k)
1172 return k->_key.external_key;
1176 ldns_key_origttl(const ldns_key *k)
1178 return k->_extra.dnssec.orig_ttl;
1182 ldns_key_flags(const ldns_key *k)
1184 return k->_extra.dnssec.flags;
1188 ldns_key_inception(const ldns_key *k)
1190 return k->_extra.dnssec.inception;
1194 ldns_key_expiration(const ldns_key *k)
1196 return k->_extra.dnssec.expiration;
1200 ldns_key_keytag(const ldns_key *k)
1202 return k->_extra.dnssec.keytag;
1206 ldns_key_pubkey_owner(const ldns_key *k)
1208 return k->_pubkey_owner;
1213 ldns_key_list_set_use(ldns_key_list *keys, bool v)
1217 for (i = 0; i < ldns_key_list_key_count(keys); i++) {
1218 ldns_key_set_use(ldns_key_list_key(keys, i), v);
1223 ldns_key_list_set_key_count(ldns_key_list *key, size_t count)
1225 key->_key_count = count;
1229 ldns_key_list_push_key(ldns_key_list *key_list, ldns_key *key)
1234 key_count = ldns_key_list_key_count(key_list);
1236 /* grow the array */
1237 keys = LDNS_XREALLOC(
1238 key_list->_keys, ldns_key *, key_count + 1);
1243 /* add the new member */
1244 key_list->_keys = keys;
1245 key_list->_keys[key_count] = key;
1247 ldns_key_list_set_key_count(key_list, key_count + 1);
1252 ldns_key_list_pop_key(ldns_key_list *key_list)
1262 key_count = ldns_key_list_key_count(key_list);
1263 if (key_count == 0) {
1267 pop = ldns_key_list_key(key_list, key_count);
1269 /* shrink the array */
1270 a = LDNS_XREALLOC(key_list->_keys, ldns_key *, key_count - 1);
1272 key_list->_keys = a;
1275 ldns_key_list_set_key_count(key_list, key_count - 1);
1282 /* data pointer must be large enough (LDNS_MAX_KEYLEN) */
1284 ldns_key_rsa2bin(unsigned char *data, RSA *k, uint16_t *size)
1292 if (BN_num_bytes(k->e) <= 256) {
1293 /* normally only this path is executed (small factors are
1296 data[0] = (unsigned char) BN_num_bytes(k->e);
1297 i = BN_bn2bin(k->e, data + 1);
1298 j = BN_bn2bin(k->n, data + i + 1);
1299 *size = (uint16_t) i + j;
1300 } else if (BN_num_bytes(k->e) <= 65536) {
1302 /* BN_bn2bin does bigendian, _uint16 also */
1303 ldns_write_uint16(data + 1, (uint16_t) BN_num_bytes(k->e));
1305 BN_bn2bin(k->e, data + 3);
1306 BN_bn2bin(k->n, data + 4 + BN_num_bytes(k->e));
1307 *size = (uint16_t) BN_num_bytes(k->n) + 6;
1314 /* data pointer must be large enough (LDNS_MAX_KEYLEN) */
1316 ldns_key_dsa2bin(unsigned char *data, DSA *k, uint16_t *size)
1325 *size = (uint16_t)BN_num_bytes(k->p);
1326 T = (*size - 64) / 8;
1327 memcpy(data, &T, 1);
1331 fprintf(stderr, "DSA key with T > 8 (ie. > 1024 bits)");
1332 fprintf(stderr, " not implemented\n");
1337 /* size = 64 + (T * 8); */
1338 data[0] = (unsigned char)T;
1339 BN_bn2bin(k->q, data + 1 ); /* 20 octects */
1340 BN_bn2bin(k->p, data + 21 ); /* offset octects */
1341 BN_bn2bin(k->g, data + 21 + *size); /* offset octets */
1342 BN_bn2bin(k->pub_key, data + 21 + *size + *size); /* offset octets */
1343 *size = 21 + (*size * 3);
1349 ldns_key_gost2bin(unsigned char* data, EVP_PKEY* k, uint16_t* size)
1352 unsigned char* pp = NULL;
1353 if(i2d_PUBKEY(k, &pp) != 37 + 64) {
1354 /* expect 37 byte(ASN header) and 64 byte(X and Y) */
1358 /* omit ASN header */
1365 #endif /* USE_GOST */
1367 #endif /* HAVE_SSL */
1370 ldns_key2rr(const ldns_key *k)
1372 /* this function will convert a the keydata contained in
1373 * rsa/dsa pointers to a DNSKEY rr. It will fill in as
1374 * much as it can, but it does not know about key-flags
1379 unsigned char *bin = NULL;
1384 #endif /* HAVE_SSL */
1388 int internal_data = 0;
1393 pubkey = ldns_rr_new();
1395 switch (ldns_key_algorithm(k)) {
1396 case LDNS_SIGN_HMACMD5:
1397 case LDNS_SIGN_HMACSHA1:
1398 case LDNS_SIGN_HMACSHA256:
1399 ldns_rr_set_type(pubkey, LDNS_RR_TYPE_KEY);
1402 ldns_rr_set_type(pubkey, LDNS_RR_TYPE_DNSKEY);
1405 /* zero-th rdf - flags */
1406 ldns_rr_push_rdf(pubkey,
1407 ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16,
1408 ldns_key_flags(k)));
1410 ldns_rr_push_rdf(pubkey,
1411 ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8, LDNS_DNSSEC_KEYPROTO));
1413 if (ldns_key_pubkey_owner(k)) {
1414 ldns_rr_set_owner(pubkey, ldns_rdf_clone(ldns_key_pubkey_owner(k)));
1417 /* third - da algorithm */
1418 switch(ldns_key_algorithm(k)) {
1419 case LDNS_SIGN_RSAMD5:
1420 case LDNS_SIGN_RSASHA1:
1421 case LDNS_SIGN_RSASHA1_NSEC3:
1422 case LDNS_SIGN_RSASHA256:
1423 case LDNS_SIGN_RSASHA512:
1424 ldns_rr_push_rdf(pubkey,
1425 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG, ldns_key_algorithm(k)));
1427 rsa = ldns_key_rsa_key(k);
1429 bin = LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN);
1431 ldns_rr_free(pubkey);
1434 if (!ldns_key_rsa2bin(bin, rsa, &size)) {
1436 ldns_rr_free(pubkey);
1446 ldns_rr_push_rdf(pubkey,
1447 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG, LDNS_DSA));
1449 dsa = ldns_key_dsa_key(k);
1451 bin = LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN);
1453 ldns_rr_free(pubkey);
1456 if (!ldns_key_dsa2bin(bin, dsa, &size)) {
1458 ldns_rr_free(pubkey);
1464 #endif /* HAVE_SSL */
1466 case LDNS_SIGN_DSA_NSEC3:
1467 ldns_rr_push_rdf(pubkey,
1468 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG, LDNS_DSA_NSEC3));
1470 dsa = ldns_key_dsa_key(k);
1472 bin = LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN);
1474 ldns_rr_free(pubkey);
1477 if (!ldns_key_dsa2bin(bin, dsa, &size)) {
1479 ldns_rr_free(pubkey);
1485 #endif /* HAVE_SSL */
1487 case LDNS_SIGN_ECC_GOST:
1488 ldns_rr_push_rdf(pubkey, ldns_native2rdf_int8(
1489 LDNS_RDF_TYPE_ALG, ldns_key_algorithm(k)));
1490 #if defined(HAVE_SSL) && defined(USE_GOST)
1491 bin = LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN);
1493 ldns_rr_free(pubkey);
1497 if (!ldns_key_gost2bin(bin, k->_key.key, &size)) {
1499 ldns_rr_free(pubkey);
1505 ldns_rr_free(pubkey);
1507 #endif /* HAVE_SSL and USE_GOST */
1509 case LDNS_SIGN_ECDSAP256SHA256:
1510 case LDNS_SIGN_ECDSAP384SHA384:
1512 ldns_rr_push_rdf(pubkey, ldns_native2rdf_int8(
1513 LDNS_RDF_TYPE_ALG, ldns_key_algorithm(k)));
1516 ec = EVP_PKEY_get1_EC_KEY(k->_key.key);
1518 EC_KEY_set_conv_form(ec, POINT_CONVERSION_UNCOMPRESSED);
1519 size = (uint16_t)i2o_ECPublicKey(ec, NULL);
1520 if(!i2o_ECPublicKey(ec, &bin)) {
1522 ldns_rr_free(pubkey);
1526 /* move back one byte to shave off the 0x02
1527 * 'uncompressed' indicator that openssl made
1528 * Actually its 0x04 (from implementation).
1530 assert(bin[0] == POINT_CONVERSION_UNCOMPRESSED);
1532 memmove(bin, bin+1, size);
1534 /* down the reference count for ec, its still assigned
1539 ldns_rr_free(pubkey);
1543 case LDNS_SIGN_HMACMD5:
1544 case LDNS_SIGN_HMACSHA1:
1545 case LDNS_SIGN_HMACSHA256:
1546 bin = LDNS_XMALLOC(unsigned char, ldns_key_hmac_size(k));
1548 ldns_rr_free(pubkey);
1551 ldns_rr_push_rdf(pubkey,
1552 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG,
1553 ldns_key_algorithm(k)));
1554 size = ldns_key_hmac_size(k);
1555 memcpy(bin, ldns_key_hmac_key(k), size);
1559 /* fourth the key bin material */
1560 if (internal_data) {
1561 keybin = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, size, bin);
1563 ldns_rr_push_rdf(pubkey, keybin);
1569 ldns_key_free(ldns_key *key)
1575 ldns_key_deep_free(ldns_key *key)
1577 unsigned char* hmac;
1578 if (ldns_key_pubkey_owner(key)) {
1579 ldns_rdf_deep_free(ldns_key_pubkey_owner(key));
1582 if (ldns_key_evp_key(key)) {
1583 EVP_PKEY_free(ldns_key_evp_key(key));
1585 #endif /* HAVE_SSL */
1586 if (ldns_key_hmac_key(key)) {
1587 hmac = ldns_key_hmac_key(key);
1594 ldns_key_list_free(ldns_key_list *key_list)
1597 for (i = 0; i < ldns_key_list_key_count(key_list); i++) {
1598 ldns_key_deep_free(ldns_key_list_key(key_list, i));
1600 LDNS_FREE(key_list->_keys);
1601 LDNS_FREE(key_list);
1605 ldns_read_anchor_file(const char *filename)
1608 /*char line[LDNS_MAX_PACKETLEN];*/
1609 char *line = LDNS_XMALLOC(char, LDNS_MAX_PACKETLEN);
1618 fp = fopen(filename, "r");
1621 fprintf(stderr, "Unable to open %s: %s\n", filename, strerror(errno));
1627 while ((c = fgetc(fp)) && i+1 < LDNS_MAX_PACKETLEN && c != EOF) {
1637 fprintf(stderr, "nothing read from %s", filename);
1642 status = ldns_rr_new_frm_str(&r, line, 0, NULL, NULL);
1643 if (status == LDNS_STATUS_OK && (ldns_rr_get_type(r) == LDNS_RR_TYPE_DNSKEY || ldns_rr_get_type(r) == LDNS_RR_TYPE_DS)) {
1648 fprintf(stderr, "Error creating DNSKEY or DS rr from %s: %s\n", filename, ldns_get_errorstr_by_id(status));
1657 ldns_key_get_file_base_name(ldns_key *key)
1659 ldns_buffer *buffer;
1660 char *file_base_name;
1662 buffer = ldns_buffer_new(255);
1663 ldns_buffer_printf(buffer, "K");
1664 (void)ldns_rdf2buffer_str_dname(buffer, ldns_key_pubkey_owner(key));
1665 ldns_buffer_printf(buffer,
1667 ldns_key_algorithm(key),
1668 ldns_key_keytag(key));
1669 file_base_name = ldns_buffer_export(buffer);
1670 ldns_buffer_free(buffer);
1671 return file_base_name;
1674 int ldns_key_algo_supported(int algo)
1676 ldns_lookup_table *lt = ldns_signing_algorithms;
1685 ldns_signing_algorithm ldns_get_signing_algorithm_by_name(const char* name)
1687 /* list of (signing algorithm id, alias_name) */
1688 ldns_lookup_table aliases[] = {
1689 /* from bind dnssec-keygen */
1690 {LDNS_SIGN_HMACMD5, "HMAC-MD5"},
1691 {LDNS_SIGN_DSA_NSEC3, "NSEC3DSA"},
1692 {LDNS_SIGN_RSASHA1_NSEC3, "NSEC3RSASHA1"},
1693 /* old ldns usage, now RFC names */
1694 {LDNS_SIGN_DSA_NSEC3, "DSA_NSEC3" },
1695 {LDNS_SIGN_RSASHA1_NSEC3, "RSASHA1_NSEC3" },
1697 {LDNS_SIGN_ECC_GOST, "GOST"},
1699 /* compat with possible output */
1702 {LDNS_INDIRECT, "INDIRECT"},
1703 {LDNS_PRIVATEDNS, "PRIVATEDNS"},
1704 {LDNS_PRIVATEOID, "PRIVATEOID"},
1706 ldns_lookup_table* lt = ldns_signing_algorithms;
1708 if(strcasecmp(lt->name, name) == 0)
1714 if(strcasecmp(lt->name, name) == 0)