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" },
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
;
371 fprintf(stderr
, "Warning: SHA256 not compiled into this ");
372 fprintf(stderr
, "version of ldns\n");
375 if (strncmp(d
, "10 RSASHA512", 3) == 0) {
377 alg
= LDNS_SIGN_RSASHA512
;
379 fprintf(stderr
, "Warning: SHA512 not compiled into this ");
380 fprintf(stderr
, "version of ldns\n");
383 if (strncmp(d
, "12 ECC-GOST", 3) == 0) {
385 alg
= LDNS_SIGN_ECC_GOST
;
387 fprintf(stderr
, "Warning: ECC-GOST not compiled into this ");
388 fprintf(stderr
, "version of ldns, use --enable-gost\n");
391 if (strncmp(d
, "13 ECDSAP256SHA256", 3) == 0) {
393 alg
= LDNS_SIGN_ECDSAP256SHA256
;
395 fprintf(stderr
, "Warning: ECDSA not compiled into this ");
396 fprintf(stderr
, "version of ldns, use --enable-ecdsa\n");
399 if (strncmp(d
, "14 ECDSAP384SHA384", 3) == 0) {
401 alg
= LDNS_SIGN_ECDSAP384SHA384
;
403 fprintf(stderr
, "Warning: ECDSA not compiled into this ");
404 fprintf(stderr
, "version of ldns, use --enable-ecdsa\n");
407 if (strncmp(d
, "157 HMAC-MD5", 4) == 0) {
408 alg
= LDNS_SIGN_HMACMD5
;
410 if (strncmp(d
, "158 HMAC-SHA1", 4) == 0) {
411 alg
= LDNS_SIGN_HMACSHA1
;
413 if (strncmp(d
, "159 HMAC-SHA256", 4) == 0) {
414 alg
= LDNS_SIGN_HMACSHA256
;
420 case LDNS_SIGN_RSAMD5
:
421 case LDNS_SIGN_RSASHA1
:
422 case LDNS_SIGN_RSASHA1_NSEC3
:
424 case LDNS_SIGN_RSASHA256
:
425 case LDNS_SIGN_RSASHA512
:
427 ldns_key_set_algorithm(k
, alg
);
429 rsa
= ldns_key_new_frm_fp_rsa_l(fp
, line_nr
);
432 return LDNS_STATUS_ERR
;
434 ldns_key_assign_rsa_key(k
, rsa
);
435 #endif /* HAVE_SSL */
438 case LDNS_SIGN_DSA_NSEC3
:
439 ldns_key_set_algorithm(k
, alg
);
441 dsa
= ldns_key_new_frm_fp_dsa_l(fp
, line_nr
);
444 return LDNS_STATUS_ERR
;
446 ldns_key_assign_dsa_key(k
, dsa
);
447 #endif /* HAVE_SSL */
449 case LDNS_SIGN_HMACMD5
:
450 case LDNS_SIGN_HMACSHA1
:
451 case LDNS_SIGN_HMACSHA256
:
452 ldns_key_set_algorithm(k
, alg
);
454 hmac
= ldns_key_new_frm_fp_hmac_l(fp
, line_nr
, &hmac_size
);
457 return LDNS_STATUS_ERR
;
459 ldns_key_set_hmac_size(k
, hmac_size
);
460 ldns_key_set_hmac_key(k
, hmac
);
461 #endif /* HAVE_SSL */
463 case LDNS_SIGN_ECC_GOST
:
464 ldns_key_set_algorithm(k
, alg
);
465 #if defined(HAVE_SSL) && defined(USE_GOST)
466 if(!ldns_key_EVP_load_gost_id()) {
468 return LDNS_STATUS_CRYPTO_ALGO_NOT_IMPL
;
470 ldns_key_set_evp_key(k
,
471 ldns_key_new_frm_fp_gost_l(fp
, line_nr
));
475 return LDNS_STATUS_ERR
;
481 case LDNS_SIGN_ECDSAP256SHA256
:
482 case LDNS_SIGN_ECDSAP384SHA384
:
483 ldns_key_set_algorithm(k
, alg
);
484 ldns_key_set_evp_key(k
,
485 ldns_key_new_frm_fp_ecdsa_l(fp
, (ldns_algorithm
)alg
, line_nr
));
489 return LDNS_STATUS_ERR
;
496 return LDNS_STATUS_SYNTAX_ALG_ERR
;
498 key_rr
= ldns_key2rr(k
);
499 ldns_key_set_keytag(k
, ldns_calc_keytag(key_rr
));
500 ldns_rr_free(key_rr
);
504 return LDNS_STATUS_OK
;
507 return LDNS_STATUS_ERR
;
512 ldns_key_new_frm_fp_rsa(FILE *f
)
514 return ldns_key_new_frm_fp_rsa_l(f
, NULL
);
518 ldns_key_new_frm_fp_rsa_l(FILE *f
, int *line_nr
)
534 * BIGNUM *n; // public modulus
535 * BIGNUM *e; // public exponent
536 * BIGNUM *d; // private exponent
537 * BIGNUM *p; // secret prime factor
538 * BIGNUM *q; // secret prime factor
539 * BIGNUM *dmp1; // d mod (p-1)
540 * BIGNUM *dmq1; // d mod (q-1)
541 * BIGNUM *iqmp; // q^-1 mod p
550 d
= LDNS_XMALLOC(char, LDNS_MAX_LINELEN
);
551 buf
= LDNS_XMALLOC(uint8_t, LDNS_MAX_LINELEN
);
553 if (!d
|| !rsa
|| !buf
) {
557 /* I could use functions again, but that seems an overkill,
558 * allthough this also looks tedious
561 /* Modules, rsa->n */
562 if (ldns_fget_keyword_data_l(f
, "Modulus", ": ", d
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
565 i
= ldns_b64_pton((const char*)d
, buf
, ldns_b64_ntop_calculate_size(strlen(d
)));
567 rsa
->n
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
572 /* PublicExponent, rsa->e */
573 if (ldns_fget_keyword_data_l(f
, "PublicExponent", ": ", d
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
576 i
= ldns_b64_pton((const char*)d
, buf
, ldns_b64_ntop_calculate_size(strlen(d
)));
577 rsa
->e
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
582 /* PrivateExponent, rsa->d */
583 if (ldns_fget_keyword_data_l(f
, "PrivateExponent", ": ", 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
->d
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
593 if (ldns_fget_keyword_data_l(f
, "Prime1", ": ", 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
->p
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
603 if (ldns_fget_keyword_data_l(f
, "Prime2", ": ", 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
->q
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
612 /* Exponent1, rsa->dmp1 */
613 if (ldns_fget_keyword_data_l(f
, "Exponent1", ": ", 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
->dmp1
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
622 /* Exponent2, rsa->dmq1 */
623 if (ldns_fget_keyword_data_l(f
, "Exponent2", ": ", 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
->dmq1
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
632 /* Coefficient, rsa->iqmp */
633 if (ldns_fget_keyword_data_l(f
, "Coefficient", ": ", 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
->iqmp
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
655 ldns_key_new_frm_fp_dsa(FILE *f
)
657 return ldns_key_new_frm_fp_dsa_l(f
, NULL
);
661 ldns_key_new_frm_fp_dsa_l(FILE *f
, ATTR_UNUSED(int *line_nr
))
668 d
= LDNS_XMALLOC(char, LDNS_MAX_LINELEN
);
669 buf
= LDNS_XMALLOC(uint8_t, LDNS_MAX_LINELEN
);
671 if (!d
|| !dsa
|| !buf
) {
675 /* the line parser removes the () from the input... */
678 if (ldns_fget_keyword_data_l(f
, "Primep", ": ", d
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
681 i
= ldns_b64_pton((const char*)d
, buf
, ldns_b64_ntop_calculate_size(strlen(d
)));
683 dsa
->p
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
688 /* Subprime, dsa->q */
689 if (ldns_fget_keyword_data_l(f
, "Subprimeq", ": ", d
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
692 i
= ldns_b64_pton((const char*)d
, buf
, ldns_b64_ntop_calculate_size(strlen(d
)));
693 dsa
->q
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
699 if (ldns_fget_keyword_data_l(f
, "Baseg", ": ", 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
->g
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
708 /* Private key, dsa->priv_key */
709 if (ldns_fget_keyword_data_l(f
, "Private_valuex", ": ", 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
->priv_key
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
714 if (!dsa
->priv_key
) {
718 /* Public key, dsa->priv_key */
719 if (ldns_fget_keyword_data_l(f
, "Public_valuey", ": ", 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
->pub_key
= BN_bin2bn((const char unsigned*)buf
, i
, NULL
);
742 ldns_key_new_frm_fp_hmac(FILE *f
, size_t *hmac_size
)
744 return ldns_key_new_frm_fp_hmac_l(f
, NULL
, hmac_size
);
748 ldns_key_new_frm_fp_hmac_l( FILE *f
749 , ATTR_UNUSED(int *line_nr
)
754 char d
[LDNS_MAX_LINELEN
];
755 unsigned char *buf
= NULL
;
757 if (ldns_fget_keyword_data_l(f
, "Key", ": ", d
, "\n", LDNS_MAX_LINELEN
, line_nr
) == -1) {
760 bufsz
= ldns_b64_ntop_calculate_size(strlen(d
));
761 buf
= LDNS_XMALLOC(unsigned char, bufsz
);
762 i
= (size_t) ldns_b64_pton((const char*)d
, buf
, bufsz
);
772 #endif /* HAVE_SSL */
776 ldns_gen_gost_key(void)
780 int gost_id
= ldns_key_EVP_load_gost_id();
783 ctx
= EVP_PKEY_CTX_new_id(gost_id
, NULL
);
785 /* the id should be available now */
788 if(EVP_PKEY_CTX_ctrl_str(ctx
, "paramset", "A") <= 0) {
789 /* cannot set paramset */
790 EVP_PKEY_CTX_free(ctx
);
794 if(EVP_PKEY_keygen_init(ctx
) <= 0) {
795 EVP_PKEY_CTX_free(ctx
);
798 if(EVP_PKEY_keygen(ctx
, &p
) <= 0) {
800 EVP_PKEY_CTX_free(ctx
);
803 EVP_PKEY_CTX_free(ctx
);
809 ldns_key_new_frm_algorithm(ldns_signing_algorithm alg
, uint16_t size
)
829 case LDNS_SIGN_RSAMD5
:
830 case LDNS_SIGN_RSASHA1
:
831 case LDNS_SIGN_RSASHA1_NSEC3
:
832 case LDNS_SIGN_RSASHA256
:
833 case LDNS_SIGN_RSASHA512
:
835 r
= RSA_generate_key((int)size
, RSA_F4
, NULL
, NULL
);
840 if (RSA_check_key(r
) != 1) {
844 ldns_key_set_rsa_key(k
, r
);
846 #endif /* HAVE_SSL */
849 case LDNS_SIGN_DSA_NSEC3
:
851 d
= DSA_generate_parameters((int)size
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
856 if (DSA_generate_key(d
) != 1) {
860 ldns_key_set_dsa_key(k
, d
);
862 #endif /* HAVE_SSL */
864 case LDNS_SIGN_HMACMD5
:
865 case LDNS_SIGN_HMACSHA1
:
866 case LDNS_SIGN_HMACSHA256
:
871 #endif /* HAVE_SSL */
873 ldns_key_set_hmac_size(k
, size
);
875 hmac
= LDNS_XMALLOC(unsigned char, size
);
881 if (RAND_bytes(hmac
, (int) size
) != 1) {
887 while (offset
+ sizeof(i
) < size
) {
889 memcpy(&hmac
[offset
], &i
, sizeof(i
));
894 memcpy(&hmac
[offset
], &i
, size
- offset
);
896 #endif /* HAVE_SSL */
897 ldns_key_set_hmac_key(k
, hmac
);
899 ldns_key_set_flags(k
, 0);
901 case LDNS_SIGN_ECC_GOST
:
902 #if defined(HAVE_SSL) && defined(USE_GOST)
903 ldns_key_set_evp_key(k
, ldns_gen_gost_key());
913 #endif /* HAVE_SSL and USE_GOST */
915 case LDNS_SIGN_ECDSAP256SHA256
:
916 case LDNS_SIGN_ECDSAP384SHA384
:
918 if(alg
== LDNS_SIGN_ECDSAP256SHA256
)
919 ec
= EC_KEY_new_by_curve_name(NID_X9_62_prime256v1
);
920 else if(alg
== LDNS_SIGN_ECDSAP384SHA384
)
921 ec
= EC_KEY_new_by_curve_name(NID_secp384r1
);
926 if(!EC_KEY_generate_key(ec
)) {
932 k
->_key
.key
= EVP_PKEY_new();
938 if (!EVP_PKEY_assign_EC_KEY(k
->_key
.key
, ec
)) {
950 ldns_key_set_algorithm(k
, alg
);
955 ldns_key_print(FILE *output
, const ldns_key
*k
)
957 char *str
= ldns_key2str(k
);
959 fprintf(output
, "%s", str
);
961 fprintf(output
, "Unable to convert private key to string\n");
968 ldns_key_set_algorithm(ldns_key
*k
, ldns_signing_algorithm l
)
974 ldns_key_set_flags(ldns_key
*k
, uint16_t f
)
976 k
->_extra
.dnssec
.flags
= f
;
982 ldns_key_set_evp_key(ldns_key
*k
, EVP_PKEY
*e
)
988 ldns_key_set_rsa_key(ldns_key
*k
, RSA
*r
)
990 EVP_PKEY
*key
= EVP_PKEY_new();
991 EVP_PKEY_set1_RSA(key
, r
);
996 ldns_key_set_dsa_key(ldns_key
*k
, DSA
*d
)
998 EVP_PKEY
*key
= EVP_PKEY_new();
999 EVP_PKEY_set1_DSA(key
, d
);
1004 ldns_key_assign_rsa_key(ldns_key
*k
, RSA
*r
)
1006 EVP_PKEY
*key
= EVP_PKEY_new();
1007 EVP_PKEY_assign_RSA(key
, r
);
1012 ldns_key_assign_dsa_key(ldns_key
*k
, DSA
*d
)
1014 EVP_PKEY
*key
= EVP_PKEY_new();
1015 EVP_PKEY_assign_DSA(key
, d
);
1019 #endif /* HAVE_SSL */
1022 ldns_key_set_hmac_key(ldns_key
*k
, unsigned char *hmac
)
1024 k
->_key
.hmac
.key
= hmac
;
1028 ldns_key_set_hmac_size(ldns_key
*k
, size_t hmac_size
)
1030 k
->_key
.hmac
.size
= hmac_size
;
1034 ldns_key_set_external_key(ldns_key
*k
, void *external_key
)
1036 k
->_key
.external_key
= external_key
;
1040 ldns_key_set_origttl(ldns_key
*k
, uint32_t t
)
1042 k
->_extra
.dnssec
.orig_ttl
= t
;
1046 ldns_key_set_inception(ldns_key
*k
, uint32_t i
)
1048 k
->_extra
.dnssec
.inception
= i
;
1052 ldns_key_set_expiration(ldns_key
*k
, uint32_t e
)
1054 k
->_extra
.dnssec
.expiration
= e
;
1058 ldns_key_set_pubkey_owner(ldns_key
*k
, ldns_rdf
*r
)
1060 k
->_pubkey_owner
= r
;
1064 ldns_key_set_keytag(ldns_key
*k
, uint16_t tag
)
1066 k
->_extra
.dnssec
.keytag
= tag
;
1071 ldns_key_list_key_count(const ldns_key_list
*key_list
)
1073 return key_list
->_key_count
;
1077 ldns_key_list_key(const ldns_key_list
*key
, size_t nr
)
1079 if (nr
< ldns_key_list_key_count(key
)) {
1080 return key
->_keys
[nr
];
1086 ldns_signing_algorithm
1087 ldns_key_algorithm(const ldns_key
*k
)
1093 ldns_key_set_use(ldns_key
*k
, bool v
)
1101 ldns_key_use(const ldns_key
*k
)
1112 ldns_key_evp_key(const ldns_key
*k
)
1118 ldns_key_rsa_key(const ldns_key
*k
)
1121 return EVP_PKEY_get1_RSA(k
->_key
.key
);
1128 ldns_key_dsa_key(const ldns_key
*k
)
1131 return EVP_PKEY_get1_DSA(k
->_key
.key
);
1137 #endif /* HAVE_SSL */
1140 ldns_key_hmac_key(const ldns_key
*k
)
1142 if (k
->_key
.hmac
.key
) {
1143 return k
->_key
.hmac
.key
;
1150 ldns_key_hmac_size(const ldns_key
*k
)
1152 if (k
->_key
.hmac
.size
) {
1153 return k
->_key
.hmac
.size
;
1160 ldns_key_external_key(const ldns_key
*k
)
1162 return k
->_key
.external_key
;
1166 ldns_key_origttl(const ldns_key
*k
)
1168 return k
->_extra
.dnssec
.orig_ttl
;
1172 ldns_key_flags(const ldns_key
*k
)
1174 return k
->_extra
.dnssec
.flags
;
1178 ldns_key_inception(const ldns_key
*k
)
1180 return k
->_extra
.dnssec
.inception
;
1184 ldns_key_expiration(const ldns_key
*k
)
1186 return k
->_extra
.dnssec
.expiration
;
1190 ldns_key_keytag(const ldns_key
*k
)
1192 return k
->_extra
.dnssec
.keytag
;
1196 ldns_key_pubkey_owner(const ldns_key
*k
)
1198 return k
->_pubkey_owner
;
1203 ldns_key_list_set_use(ldns_key_list
*keys
, bool v
)
1207 for (i
= 0; i
< ldns_key_list_key_count(keys
); i
++) {
1208 ldns_key_set_use(ldns_key_list_key(keys
, i
), v
);
1213 ldns_key_list_set_key_count(ldns_key_list
*key
, size_t count
)
1215 key
->_key_count
= count
;
1219 ldns_key_list_push_key(ldns_key_list
*key_list
, ldns_key
*key
)
1224 key_count
= ldns_key_list_key_count(key_list
);
1226 /* grow the array */
1227 keys
= LDNS_XREALLOC(
1228 key_list
->_keys
, ldns_key
*, key_count
+ 1);
1233 /* add the new member */
1234 key_list
->_keys
= keys
;
1235 key_list
->_keys
[key_count
] = key
;
1237 ldns_key_list_set_key_count(key_list
, key_count
+ 1);
1242 ldns_key_list_pop_key(ldns_key_list
*key_list
)
1252 key_count
= ldns_key_list_key_count(key_list
);
1253 if (key_count
== 0) {
1257 pop
= ldns_key_list_key(key_list
, key_count
);
1259 /* shrink the array */
1260 a
= LDNS_XREALLOC(key_list
->_keys
, ldns_key
*, key_count
- 1);
1262 key_list
->_keys
= a
;
1265 ldns_key_list_set_key_count(key_list
, key_count
- 1);
1272 /* data pointer must be large enough (LDNS_MAX_KEYLEN) */
1274 ldns_key_rsa2bin(unsigned char *data
, RSA
*k
, uint16_t *size
)
1282 if (BN_num_bytes(k
->e
) <= 256) {
1283 /* normally only this path is executed (small factors are
1286 data
[0] = (unsigned char) BN_num_bytes(k
->e
);
1287 i
= BN_bn2bin(k
->e
, data
+ 1);
1288 j
= BN_bn2bin(k
->n
, data
+ i
+ 1);
1289 *size
= (uint16_t) i
+ j
;
1290 } else if (BN_num_bytes(k
->e
) <= 65536) {
1292 /* BN_bn2bin does bigendian, _uint16 also */
1293 ldns_write_uint16(data
+ 1, (uint16_t) BN_num_bytes(k
->e
));
1295 BN_bn2bin(k
->e
, data
+ 3);
1296 BN_bn2bin(k
->n
, data
+ 4 + BN_num_bytes(k
->e
));
1297 *size
= (uint16_t) BN_num_bytes(k
->n
) + 6;
1304 /* data pointer must be large enough (LDNS_MAX_KEYLEN) */
1306 ldns_key_dsa2bin(unsigned char *data
, DSA
*k
, uint16_t *size
)
1315 *size
= (uint16_t)BN_num_bytes(k
->p
);
1316 T
= (*size
- 64) / 8;
1317 memcpy(data
, &T
, 1);
1320 fprintf(stderr
, "DSA key with T > 8 (ie. > 1024 bits)");
1321 fprintf(stderr
, " not implemented\n");
1325 /* size = 64 + (T * 8); */
1326 data
[0] = (unsigned char)T
;
1327 BN_bn2bin(k
->q
, data
+ 1 ); /* 20 octects */
1328 BN_bn2bin(k
->p
, data
+ 21 ); /* offset octects */
1329 BN_bn2bin(k
->g
, data
+ 21 + *size
); /* offset octets */
1330 BN_bn2bin(k
->pub_key
, data
+ 21 + *size
+ *size
); /* offset octets */
1331 *size
= 21 + (*size
* 3);
1337 ldns_key_gost2bin(unsigned char* data
, EVP_PKEY
* k
, uint16_t* size
)
1340 unsigned char* pp
= NULL
;
1341 if(i2d_PUBKEY(k
, &pp
) != 37 + 64) {
1342 /* expect 37 byte(ASN header) and 64 byte(X and Y) */
1346 /* omit ASN header */
1353 #endif /* USE_GOST */
1355 #endif /* HAVE_SSL */
1358 ldns_key2rr(const ldns_key
*k
)
1360 /* this function will convert a the keydata contained in
1361 * rsa/dsa pointers to a DNSKEY rr. It will fill in as
1362 * much as it can, but it does not know about key-flags
1367 unsigned char *bin
= NULL
;
1372 #endif /* HAVE_SSL */
1376 int internal_data
= 0;
1381 pubkey
= ldns_rr_new();
1383 switch (ldns_key_algorithm(k
)) {
1384 case LDNS_SIGN_HMACMD5
:
1385 case LDNS_SIGN_HMACSHA1
:
1386 case LDNS_SIGN_HMACSHA256
:
1387 ldns_rr_set_type(pubkey
, LDNS_RR_TYPE_KEY
);
1390 ldns_rr_set_type(pubkey
, LDNS_RR_TYPE_DNSKEY
);
1393 /* zero-th rdf - flags */
1394 ldns_rr_push_rdf(pubkey
,
1395 ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16
,
1396 ldns_key_flags(k
)));
1398 ldns_rr_push_rdf(pubkey
,
1399 ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8
, LDNS_DNSSEC_KEYPROTO
));
1401 if (ldns_key_pubkey_owner(k
)) {
1402 ldns_rr_set_owner(pubkey
, ldns_rdf_clone(ldns_key_pubkey_owner(k
)));
1405 /* third - da algorithm */
1406 switch(ldns_key_algorithm(k
)) {
1407 case LDNS_SIGN_RSAMD5
:
1408 case LDNS_SIGN_RSASHA1
:
1409 case LDNS_SIGN_RSASHA1_NSEC3
:
1410 case LDNS_SIGN_RSASHA256
:
1411 case LDNS_SIGN_RSASHA512
:
1412 ldns_rr_push_rdf(pubkey
,
1413 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG
, ldns_key_algorithm(k
)));
1415 rsa
= ldns_key_rsa_key(k
);
1417 bin
= LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN
);
1419 ldns_rr_free(pubkey
);
1422 if (!ldns_key_rsa2bin(bin
, rsa
, &size
)) {
1424 ldns_rr_free(pubkey
);
1434 ldns_rr_push_rdf(pubkey
,
1435 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG
, LDNS_DSA
));
1437 dsa
= ldns_key_dsa_key(k
);
1439 bin
= LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN
);
1441 ldns_rr_free(pubkey
);
1444 if (!ldns_key_dsa2bin(bin
, dsa
, &size
)) {
1446 ldns_rr_free(pubkey
);
1452 #endif /* HAVE_SSL */
1454 case LDNS_SIGN_DSA_NSEC3
:
1455 ldns_rr_push_rdf(pubkey
,
1456 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG
, LDNS_DSA_NSEC3
));
1458 dsa
= ldns_key_dsa_key(k
);
1460 bin
= LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN
);
1462 ldns_rr_free(pubkey
);
1465 if (!ldns_key_dsa2bin(bin
, dsa
, &size
)) {
1467 ldns_rr_free(pubkey
);
1473 #endif /* HAVE_SSL */
1475 case LDNS_SIGN_ECC_GOST
:
1476 ldns_rr_push_rdf(pubkey
, ldns_native2rdf_int8(
1477 LDNS_RDF_TYPE_ALG
, ldns_key_algorithm(k
)));
1478 #if defined(HAVE_SSL) && defined(USE_GOST)
1479 bin
= LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN
);
1481 ldns_rr_free(pubkey
);
1485 if (!ldns_key_gost2bin(bin
, k
->_key
.key
, &size
)) {
1487 ldns_rr_free(pubkey
);
1493 ldns_rr_free(pubkey
);
1495 #endif /* HAVE_SSL and USE_GOST */
1497 case LDNS_SIGN_ECDSAP256SHA256
:
1498 case LDNS_SIGN_ECDSAP384SHA384
:
1500 ldns_rr_push_rdf(pubkey
, ldns_native2rdf_int8(
1501 LDNS_RDF_TYPE_ALG
, ldns_key_algorithm(k
)));
1504 ec
= EVP_PKEY_get1_EC_KEY(k
->_key
.key
);
1506 EC_KEY_set_conv_form(ec
, POINT_CONVERSION_UNCOMPRESSED
);
1507 size
= (uint16_t)i2o_ECPublicKey(ec
, NULL
);
1508 if(!i2o_ECPublicKey(ec
, &bin
)) {
1510 ldns_rr_free(pubkey
);
1514 /* move back one byte to shave off the 0x02
1515 * 'uncompressed' indicator that openssl made
1516 * Actually its 0x04 (from implementation).
1518 assert(bin
[0] == POINT_CONVERSION_UNCOMPRESSED
);
1520 memmove(bin
, bin
+1, size
);
1522 /* down the reference count for ec, its still assigned
1527 ldns_rr_free(pubkey
);
1531 case LDNS_SIGN_HMACMD5
:
1532 case LDNS_SIGN_HMACSHA1
:
1533 case LDNS_SIGN_HMACSHA256
:
1534 bin
= LDNS_XMALLOC(unsigned char, ldns_key_hmac_size(k
));
1536 ldns_rr_free(pubkey
);
1539 ldns_rr_push_rdf(pubkey
,
1540 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG
,
1541 ldns_key_algorithm(k
)));
1542 size
= ldns_key_hmac_size(k
);
1543 memcpy(bin
, ldns_key_hmac_key(k
), size
);
1547 /* fourth the key bin material */
1548 if (internal_data
) {
1549 keybin
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, size
, bin
);
1551 ldns_rr_push_rdf(pubkey
, keybin
);
1557 ldns_key_free(ldns_key
*key
)
1563 ldns_key_deep_free(ldns_key
*key
)
1565 unsigned char* hmac
;
1566 if (ldns_key_pubkey_owner(key
)) {
1567 ldns_rdf_deep_free(ldns_key_pubkey_owner(key
));
1570 if (ldns_key_evp_key(key
)) {
1571 EVP_PKEY_free(ldns_key_evp_key(key
));
1573 #endif /* HAVE_SSL */
1574 if (ldns_key_hmac_key(key
)) {
1575 hmac
= ldns_key_hmac_key(key
);
1582 ldns_key_list_free(ldns_key_list
*key_list
)
1585 for (i
= 0; i
< ldns_key_list_key_count(key_list
); i
++) {
1586 ldns_key_deep_free(ldns_key_list_key(key_list
, i
));
1588 LDNS_FREE(key_list
->_keys
);
1589 LDNS_FREE(key_list
);
1593 ldns_read_anchor_file(const char *filename
)
1596 /*char line[LDNS_MAX_PACKETLEN];*/
1597 char *line
= LDNS_XMALLOC(char, LDNS_MAX_PACKETLEN
);
1606 fp
= fopen(filename
, "r");
1608 fprintf(stderr
, "Unable to open %s: %s\n", filename
, strerror(errno
));
1613 while ((c
= fgetc(fp
)) && i
+1 < LDNS_MAX_PACKETLEN
&& c
!= EOF
) {
1622 fprintf(stderr
, "nothing read from %s", filename
);
1626 status
= ldns_rr_new_frm_str(&r
, line
, 0, NULL
, NULL
);
1627 if (status
== LDNS_STATUS_OK
&& (ldns_rr_get_type(r
) == LDNS_RR_TYPE_DNSKEY
|| ldns_rr_get_type(r
) == LDNS_RR_TYPE_DS
)) {
1631 fprintf(stderr
, "Error creating DNSKEY or DS rr from %s: %s\n", filename
, ldns_get_errorstr_by_id(status
));
1639 ldns_key_get_file_base_name(ldns_key
*key
)
1641 ldns_buffer
*buffer
;
1642 char *file_base_name
;
1644 buffer
= ldns_buffer_new(255);
1645 ldns_buffer_printf(buffer
, "K");
1646 (void)ldns_rdf2buffer_str_dname(buffer
, ldns_key_pubkey_owner(key
));
1647 ldns_buffer_printf(buffer
,
1649 ldns_key_algorithm(key
),
1650 ldns_key_keytag(key
));
1651 file_base_name
= ldns_buffer_export(buffer
);
1652 ldns_buffer_free(buffer
);
1653 return file_base_name
;
1656 int ldns_key_algo_supported(int algo
)
1658 ldns_lookup_table
*lt
= ldns_signing_algorithms
;
1667 ldns_signing_algorithm
ldns_get_signing_algorithm_by_name(const char* name
)
1669 /* list of (signing algorithm id, alias_name) */
1670 ldns_lookup_table aliases
[] = {
1671 /* from bind dnssec-keygen */
1672 {LDNS_SIGN_HMACMD5
, "HMAC-MD5"},
1673 {LDNS_SIGN_DSA_NSEC3
, "NSEC3DSA"},
1674 {LDNS_SIGN_RSASHA1_NSEC3
, "NSEC3RSASHA1"},
1675 /* old ldns usage, now RFC names */
1676 {LDNS_SIGN_DSA_NSEC3
, "DSA_NSEC3" },
1677 {LDNS_SIGN_RSASHA1_NSEC3
, "RSASHA1_NSEC3" },
1679 {LDNS_SIGN_ECC_GOST
, "GOST"},
1681 /* compat with possible output */
1684 {LDNS_INDIRECT
, "INDIRECT"},
1685 {LDNS_PRIVATEDNS
, "PRIVATEDNS"},
1686 {LDNS_PRIVATEOID
, "PRIVATEOID"},
1688 ldns_lookup_table
* lt
= ldns_signing_algorithms
;
1690 if(strcasecmp(lt
->name
, name
) == 0)
1696 if(strcasecmp(lt
->name
, name
) == 0)