18 #include <openssl/ssl.h>
19 #include <openssl/rand.h>
20 #include <openssl/bn.h>
21 #include <openssl/rsa.h>
23 #include <openssl/dsa.h>
25 #ifndef OPENSSL_NO_ENGINE
26 #include <openssl/engine.h>
46 { LDNS_SIGN_ED25519,
"ED25519" },
49 { LDNS_SIGN_ED448,
"ED448" },
72 key_list->
_keys = NULL;
109 #if defined(HAVE_SSL) && !defined(OPENSSL_NO_ENGINE)
118 k->
_key.
key = ENGINE_load_private_key(e, key_id, UI_OpenSSL(), NULL);
141 static int gost_id = 0;
142 const EVP_PKEY_ASN1_METHOD* meth;
145 if(gost_id)
return gost_id;
148 meth = EVP_PKEY_asn1_find_str(NULL,
"gost2001", -1);
150 EVP_PKEY_asn1_get0_info(&gost_id, NULL, NULL, NULL, NULL, meth);
155 e = ENGINE_by_id(
"gost");
158 ENGINE_load_builtin_engines();
159 ENGINE_load_dynamic();
160 e = ENGINE_by_id(
"gost");
166 if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
172 meth = EVP_PKEY_asn1_find_str(&e,
"gost2001", -1);
183 EVP_PKEY_asn1_get0_info(&gost_id, NULL, NULL, NULL, NULL, meth);
198 ldns_key_new_frm_fp_gost_l(FILE* fp,
int* line_nr)
201 const unsigned char* pp;
211 sizeof(token), line_nr) == -1)
213 while(strlen(token) < 96) {
216 sizeof(token)-strlen(token), line_nr) == -1)
222 pkey = d2i_PrivateKey(gost_id, NULL, &pp, (
int)
ldns_rdf_size(b64rdf));
231 ldns_EC_KEY_calc_public(EC_KEY* ec)
234 const EC_GROUP* group;
235 group = EC_KEY_get0_group(ec);
236 pub_key = EC_POINT_new(group);
237 if(!pub_key)
return 0;
238 if(!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group))) {
239 EC_POINT_free(pub_key);
242 if(!EC_POINT_mul(group, pub_key, EC_KEY_get0_private_key(ec),
244 EC_POINT_free(pub_key);
247 if(EC_KEY_set_public_key(ec, pub_key) == 0) {
248 EC_POINT_free(pub_key);
251 EC_POINT_free(pub_key);
257 ldns_key_new_frm_fp_ecdsa_l(FILE* fp,
ldns_algorithm alg,
int* line_nr)
266 sizeof(token), line_nr) == -1)
273 ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
275 ec = EC_KEY_new_by_curve_name(NID_secp384r1);
287 EC_KEY_set_private_key(ec, bn);
289 if(!ldns_EC_KEY_calc_public(ec)) {
294 evp_key = EVP_PKEY_new();
299 if (!EVP_PKEY_assign_EC_KEY(evp_key, ec)) {
300 EVP_PKEY_free(evp_key);
311 ldns_ed25519_priv_raw(uint8_t* pkey,
int plen)
313 const unsigned char* pp;
316 uint8_t pre[] = {0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
317 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20};
330 buflen = pre_len + plen;
331 if((
size_t)buflen >
sizeof(buf))
333 memmove(buf, pre, pre_len);
334 memmove(buf+pre_len, pkey, plen);
339 return d2i_PrivateKey(NID_ED25519, NULL, &pp, buflen);
344 ldns_key_new_frm_fp_ed25519_l(FILE* fp,
int* line_nr)
350 sizeof(token), line_nr) == -1)
371 ldns_ed448_priv_raw(uint8_t* pkey,
int plen)
373 const unsigned char* pp;
376 uint8_t pre[] = {0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39};
382 buflen = pre_len + plen;
383 if((
size_t)buflen >
sizeof(buf))
385 memmove(buf, pre, pre_len);
386 memmove(buf+pre_len, pkey, plen);
391 return d2i_PrivateKey(NID_ED448, NULL, &pp, buflen);
396 ldns_key_new_frm_fp_ed448_l(FILE* fp,
int* line_nr)
402 sizeof(token), line_nr) == -1)
456 if (strncmp(d,
"v1.", 3) != 0) {
472 if (strncmp(d,
"1 RSA", 2) == 0) {
475 if (strncmp(d,
"2 DH", 2) == 0) {
478 if (strncmp(d,
"3 DSA", 2) == 0) {
483 fprintf(stderr,
"Warning: DSA not compiled into this ");
484 fprintf(stderr,
"version of ldns\n");
488 if (strncmp(d,
"4 ECC", 2) == 0) {
491 if (strncmp(d,
"5 RSASHA1", 2) == 0) {
494 if (strncmp(d,
"6 DSA", 2) == 0) {
499 fprintf(stderr,
"Warning: DSA not compiled into this ");
500 fprintf(stderr,
"version of ldns\n");
504 if (strncmp(d,
"7 RSASHA1", 2) == 0) {
508 if (strncmp(d,
"8 RSASHA256", 2) == 0) {
513 fprintf(stderr,
"Warning: SHA256 not compiled into this ");
514 fprintf(stderr,
"version of ldns\n");
518 if (strncmp(d,
"10 RSASHA512", 3) == 0) {
523 fprintf(stderr,
"Warning: SHA512 not compiled into this ");
524 fprintf(stderr,
"version of ldns\n");
528 if (strncmp(d,
"12 ECC-GOST", 3) == 0) {
533 fprintf(stderr,
"Warning: ECC-GOST not compiled into this ");
534 fprintf(stderr,
"version of ldns, use --enable-gost\n");
538 if (strncmp(d,
"13 ECDSAP256SHA256", 3) == 0) {
543 fprintf(stderr,
"Warning: ECDSA not compiled into this ");
544 fprintf(stderr,
"version of ldns, use --enable-ecdsa\n");
548 if (strncmp(d,
"14 ECDSAP384SHA384", 3) == 0) {
553 fprintf(stderr,
"Warning: ECDSA not compiled into this ");
554 fprintf(stderr,
"version of ldns, use --enable-ecdsa\n");
558 if (strncmp(d,
"15 ED25519", 3) == 0) {
560 alg = LDNS_SIGN_ED25519;
563 fprintf(stderr,
"Warning: ED25519 not compiled into this ");
564 fprintf(stderr,
"version of ldns, use --enable-ed25519\n");
568 if (strncmp(d,
"16 ED448", 3) == 0) {
570 alg = LDNS_SIGN_ED448;
573 fprintf(stderr,
"Warning: ED448 not compiled into this ");
574 fprintf(stderr,
"version of ldns, use --enable-ed448\n");
578 if (strncmp(d,
"157 HMAC-MD5", 4) == 0) {
581 if (strncmp(d,
"158 HMAC-SHA1", 4) == 0) {
584 if (strncmp(d,
"159 HMAC-SHA256", 4) == 0) {
588 if (strncmp(d,
"161 ", 4) == 0) {
591 if (strncmp(d,
"162 HMAC-SHA224", 4) == 0) {
595 if (strncmp(d,
"163 ", 4) == 0) {
598 if (strncmp(d,
"164 HMAC-SHA384", 4) == 0) {
601 if (strncmp(d,
"165 HMAC-SHA512", 4) == 0) {
657 #if defined(HAVE_SSL) && defined(USE_GOST)
663 ldns_key_new_frm_fp_gost_l(fp, line_nr));
687 case LDNS_SIGN_ED25519:
690 ldns_key_new_frm_fp_ed25519_l(fp, line_nr));
700 case LDNS_SIGN_ED448:
703 ldns_key_new_frm_fp_ed448_l(fp, line_nr));
767 BIGNUM *n=NULL, *e=NULL, *d=NULL, *p=NULL, *q=NULL,
768 *dmp1=NULL, *dmq1=NULL, *iqmp=NULL;
773 if (!b || !rsa || !buf) {
785 i =
ldns_b64_pton((
const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
787 n = BN_bin2bn((
const char unsigned*)buf, i, NULL);
796 i =
ldns_b64_pton((
const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
797 e = BN_bin2bn((
const char unsigned*)buf, i, NULL);
806 i =
ldns_b64_pton((
const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
807 d = BN_bin2bn((
const char unsigned*)buf, i, NULL);
816 i =
ldns_b64_pton((
const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
817 p = BN_bin2bn((
const char unsigned*)buf, i, NULL);
826 i =
ldns_b64_pton((
const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
827 q = BN_bin2bn((
const char unsigned*)buf, i, NULL);
836 i =
ldns_b64_pton((
const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
837 dmp1 = BN_bin2bn((
const char unsigned*)buf, i, NULL);
846 i =
ldns_b64_pton((
const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
847 dmq1 = BN_bin2bn((
const char unsigned*)buf, i, NULL);
856 i =
ldns_b64_pton((
const char*)b, buf, ldns_b64_ntop_calculate_size(strlen(b)));
857 iqmp = BN_bin2bn((
const char unsigned*)buf, i, NULL);
863 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL)
875 if(!RSA_set0_key(rsa, n, e, d))
880 if(!RSA_set0_factors(rsa, p, q))
884 if(!RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp))
920 BIGNUM *p=NULL, *q=NULL, *g=NULL, *priv_key=NULL, *pub_key=NULL;
925 if (!d || !dsa || !buf) {
935 i =
ldns_b64_pton((
const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
937 p = BN_bin2bn((
const char unsigned*)buf, i, NULL);
946 i =
ldns_b64_pton((
const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
947 q = BN_bin2bn((
const char unsigned*)buf, i, NULL);
956 i =
ldns_b64_pton((
const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
957 g = BN_bin2bn((
const char unsigned*)buf, i, NULL);
966 i =
ldns_b64_pton((
const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
967 priv_key = BN_bin2bn((
const char unsigned*)buf, i, NULL);
976 i =
ldns_b64_pton((
const char*)d, buf, ldns_b64_ntop_calculate_size(strlen(d)));
977 pub_key = BN_bin2bn((
const char unsigned*)buf, i, NULL);
983 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL)
988 dsa->priv_key = priv_key;
989 dsa->pub_key = pub_key;
992 if(!DSA_set0_pqg(dsa, p, q, g))
997 if(!DSA_set0_key(dsa, pub_key, priv_key))
1032 unsigned char *buf = NULL;
1038 ldns_b64_ntop_calculate_size(strlen(d))))) == NULL
1047 ldns_gen_gost_key(
void)
1054 ctx = EVP_PKEY_CTX_new_id(gost_id, NULL);
1059 if(EVP_PKEY_CTX_ctrl_str(ctx,
"paramset",
"A") <= 0) {
1061 EVP_PKEY_CTX_free(ctx);
1065 if(EVP_PKEY_keygen_init(ctx) <= 0) {
1066 EVP_PKEY_CTX_free(ctx);
1069 if(EVP_PKEY_keygen(ctx, &p) <= 0) {
1071 EVP_PKEY_CTX_free(ctx);
1074 EVP_PKEY_CTX_free(ctx);
1090 # ifdef HAVE_EVP_PKEY_KEYGEN
1097 uint16_t offset = 0;
1099 unsigned char *hmac;
1112 #ifdef HAVE_EVP_PKEY_KEYGEN
1113 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
1118 if(EVP_PKEY_keygen_init(ctx) <= 0) {
1120 EVP_PKEY_CTX_free(ctx);
1123 if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, size) <= 0) {
1125 EVP_PKEY_CTX_free(ctx);
1129 if (EVP_PKEY_keygen(ctx, &k->
_key.
key) <= 0) {
1131 EVP_PKEY_CTX_free(ctx);
1135 EVP_PKEY_CTX_free(ctx);
1137 r = RSA_generate_key((
int)size, RSA_F4, NULL, NULL);
1142 if (RSA_check_key(r) != 1) {
1155 # if OPENSSL_VERSION_NUMBER < 0x00908000L
1156 d = DSA_generate_parameters((
int)size, NULL, 0, NULL, NULL, NULL, NULL);
1163 if (! (d = DSA_new())) {
1167 if (! DSA_generate_parameters_ex(d, (
int)size, NULL, 0, NULL, NULL, NULL)) {
1173 if (DSA_generate_key(d) != 1) {
1202 if (RAND_bytes(hmac, (
int) size) != 1) {
1208 while (offset +
sizeof(i) < size) {
1210 memcpy(&hmac[offset], &i,
sizeof(i));
1211 offset +=
sizeof(i);
1213 if (offset < size) {
1215 memcpy(&hmac[offset], &i, size - offset);
1223 #if defined(HAVE_SSL) && defined(USE_GOST)
1240 ec = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
1242 ec = EC_KEY_new_by_curve_name(NID_secp384r1);
1247 if(!EC_KEY_generate_key(ec)) {
1259 if (!EVP_PKEY_assign_EC_KEY(k->
_key.
key, ec)) {
1271 case LDNS_SIGN_ED25519:
1272 #ifdef HAVE_EVP_PKEY_KEYGEN
1273 ctx = EVP_PKEY_CTX_new_id(NID_ED25519, NULL);
1278 if(EVP_PKEY_keygen_init(ctx) <= 0) {
1280 EVP_PKEY_CTX_free(ctx);
1283 if (EVP_PKEY_keygen(ctx, &k->
_key.
key) <= 0) {
1285 EVP_PKEY_CTX_free(ctx);
1288 EVP_PKEY_CTX_free(ctx);
1293 case LDNS_SIGN_ED448:
1294 #ifdef HAVE_EVP_PKEY_KEYGEN
1295 ctx = EVP_PKEY_CTX_new_id(NID_ED448, NULL);
1300 if(EVP_PKEY_keygen_init(ctx) <= 0) {
1302 EVP_PKEY_CTX_free(ctx);
1305 if (EVP_PKEY_keygen(ctx, &k->
_key.
key) <= 0) {
1307 EVP_PKEY_CTX_free(ctx);
1310 EVP_PKEY_CTX_free(ctx);
1324 fprintf(output,
"%s", str);
1326 fprintf(output,
"Unable to convert private key to string\n");
1355 EVP_PKEY *key = EVP_PKEY_new();
1356 EVP_PKEY_set1_RSA(key, r);
1364 EVP_PKEY *key = EVP_PKEY_new();
1365 EVP_PKEY_set1_DSA(key, d);
1375 EVP_PKEY *key = EVP_PKEY_new();
1376 EVP_PKEY_assign_RSA(key, r);
1384 EVP_PKEY *key = EVP_PKEY_new();
1385 EVP_PKEY_assign_DSA(key, d);
1453 return key->
_keys[nr];
1494 return EVP_PKEY_get1_RSA(k->
_key.
key);
1505 return EVP_PKEY_get1_DSA(k->
_key.
key);
1612 key_list->
_keys = keys;
1613 key_list->
_keys[key_count] = key;
1631 if (key_count == 0) {
1640 key_list->
_keys = a;
1652 ldns_key_rsa2bin(
unsigned char *data, RSA *k, uint16_t *size)
1655 const BIGNUM *n=NULL, *e=NULL;
1660 #if OPENSSL_VERSION_NUMBER < 0x10100000 || defined(HAVE_LIBRESSL)
1664 RSA_get0_key(k, &n, &e, NULL);
1667 if (BN_num_bytes(e) <= 256) {
1671 data[0] = (
unsigned char) BN_num_bytes(e);
1672 i = BN_bn2bin(e, data + 1);
1673 j = BN_bn2bin(n, data + i + 1);
1674 *size = (uint16_t) i + j;
1675 }
else if (BN_num_bytes(e) <= 65536) {
1678 ldns_write_uint16(data + 1, (uint16_t) BN_num_bytes(e));
1680 BN_bn2bin(e, data + 3);
1681 BN_bn2bin(n, data + 4 + BN_num_bytes(e));
1682 *size = (uint16_t) BN_num_bytes(n) + 6;
1692 ldns_key_dsa2bin(
unsigned char *data, DSA *k, uint16_t *size)
1695 const BIGNUM *p, *q, *g;
1696 const BIGNUM *pub_key, *priv_key;
1703 # ifdef HAVE_DSA_GET0_PQG
1704 DSA_get0_pqg(k, &p, &q, &g);
1706 p = k->p; q = k->q; g = k->g;
1708 # ifdef HAVE_DSA_GET0_KEY
1709 DSA_get0_key(k, &pub_key, &priv_key);
1711 pub_key = k->pub_key; priv_key = k->priv_key;
1714 *size = (uint16_t)BN_num_bytes(p);
1715 T = (*size - 64) / 8;
1719 fprintf(stderr,
"DSA key with T > 8 (ie. > 1024 bits)");
1720 fprintf(stderr,
" not implemented\n");
1726 memset(data, 0, 21 + *size * 3);
1727 data[0] = (
unsigned char)T;
1728 BN_bn2bin(q, data + 1 );
1729 BN_bn2bin(p, data + 21 );
1730 BN_bn2bin(g, data + 21 + *size * 2 - BN_num_bytes(g));
1731 BN_bn2bin(pub_key,data + 21 + *size * 3 - BN_num_bytes(pub_key));
1732 *size = 21 + *size * 3;
1739 ldns_key_gost2bin(
unsigned char* data, EVP_PKEY* k, uint16_t* size)
1742 unsigned char* pp = NULL;
1743 if(i2d_PUBKEY(k, &pp) != 37 + 64) {
1759 ldns_key_ed255192bin(
unsigned char* data, EVP_PKEY* k, uint16_t* size)
1762 unsigned char* pp = NULL;
1763 if(i2d_PUBKEY(k, &pp) != 12 + 32) {
1779 ldns_key_ed4482bin(
unsigned char* data, EVP_PKEY* k, uint16_t* size)
1782 unsigned char* pp = NULL;
1783 if(i2d_PUBKEY(k, &pp) != 12 + 57) {
1809 unsigned char *bin = NULL;
1820 int internal_data = 0;
1869 if (!ldns_key_rsa2bin(bin, rsa, &size)) {
1892 if (!ldns_key_dsa2bin(bin, dsa, &size)) {
1915 if (!ldns_key_dsa2bin(bin, dsa, &size)) {
1929 #if defined(HAVE_SSL) && defined(USE_GOST)
1936 if (!ldns_key_gost2bin(bin, k->
_key.
key, &size)) {
1955 ec = EVP_PKEY_get1_EC_KEY(k->
_key.
key);
1957 EC_KEY_set_conv_form(ec, POINT_CONVERSION_UNCOMPRESSED);
1958 size = (uint16_t)i2o_ECPublicKey(ec, NULL);
1959 if(!i2o_ECPublicKey(ec, &bin)) {
1969 assert(bin[0] == POINT_CONVERSION_UNCOMPRESSED);
1971 memmove(bin, bin+1, size);
1983 case LDNS_SIGN_ED25519:
1991 if (!ldns_key_ed255192bin(bin, k->
_key.
key, &size)) {
2000 case LDNS_SIGN_ED448:
2008 if (!ldns_key_ed4482bin(bin, k->
_key.
key, &size)) {
2036 if (internal_data) {
2053 unsigned char* hmac;
2094 fp = fopen(filename,
"r");
2097 fprintf(stderr,
"Unable to open %s: %s\n", filename, strerror(errno));
2113 fprintf(stderr,
"nothing read from %s", filename);
2136 char *file_base_name;
2147 return file_base_name;
2189 if(strcasecmp(lt->
name, name) == 0)
2195 if(strcasecmp(lt->
name, name) == 0)
2199 a = strtol(name, &endptr, 10);
2200 if (*name && !*endptr)