int main(int argc, char **argv) { isc_mem_t *mctx; unsigned char buffer[512]; isc_entropy_t *ent; isc_entropysource_t *source; unsigned int returned; unsigned int flags; isc_result_t result; isc_keyboard_t kbd; UNUSED(argc); UNUSED(argv); mctx = NULL; CHECK("isc_mem_create()", isc_mem_create(0, 0, &mctx)); ent = NULL; CHECK("isc_entropy_create()", isc_entropy_create(mctx, &ent)); isc_entropy_stats(ent, stderr); source = NULL; result = isc_entropy_createcallbacksource(ent, start, get, stop, &kbd, &source); CHECK("isc_entropy_createcallbacksource()", result); fprintf(stderr, "Reading 32 bytes of GOOD random data only, partial OK\n"); flags = 0; flags |= ISC_ENTROPY_GOODONLY; flags |= ISC_ENTROPY_PARTIAL; flags |= ISC_ENTROPY_BLOCKING; returned = 0; result = isc_entropy_getdata(ent, buffer, 32, &returned, flags); if (result == ISC_R_NOENTROPY) { fprintf(stderr, "No entropy.\r\n"); } isc_entropy_stopcallbacksources(ent); hex_dump("good data only:", buffer, returned); isc_entropy_stats(ent, stderr); isc_entropy_destroysource(&source); isc_entropy_detach(&ent); isc_mem_stats(mctx, stderr); isc_mem_destroy(&mctx); return (0); }
void cleanup_entropy(isc_entropy_t **ectx) { entropysource_t *source; while (!ISC_LIST_EMPTY(sources)) { source = ISC_LIST_HEAD(sources); ISC_LIST_UNLINK(sources, source, link); isc_entropy_destroysource(&source->source); isc_mem_put(source->mctx, source, sizeof(*source)); } isc_entropy_detach(ectx); }
int main(int argc, char **argv) { UNUSED(argc); UNUSED(argv); rsa = RSA_new(); e = BN_new(); pkey = EVP_PKEY_new(); if ((rsa == NULL) || (e == NULL) || (pkey == NULL) || !EVP_PKEY_set1_RSA(pkey, rsa)) { fprintf(stderr, "fatal error: basic OpenSSL failure\n"); exit(1); } /* e = 0x1000000000001 */ BN_set_bit(e, 0); BN_set_bit(e, 48); if (RSA_generate_key_ex(rsa, bits, e, NULL)) { BN_free(e); RSA_free(rsa); } else { fprintf(stderr, "fatal error: RSA_generate_key_ex() fails " "at file %s line %d\n", __FILE__, __LINE__); exit(1); } dns_result_register(); CHECK(isc_mem_create(0, 0, &mctx), "isc_mem_create()"); CHECK(isc_entropy_create(mctx, &ectx), "isc_entropy_create()"); CHECK(isc_entropy_usebestsource(ectx, &source, "../random.data", ISC_ENTROPY_KEYBOARDNO), "isc_entropy_usebestsource(\"../random.data\")"); CHECK(dst_lib_init2(mctx, ectx, NULL, 0), "dst_lib_init2()"); CHECK(isc_log_create(mctx, &log_, &logconfig), "isc_log_create()"); isc_log_setcontext(log_); dns_log_init(log_); dns_log_setcontext(log_); CHECK(isc_log_settag(logconfig, "bigkey"), "isc_log_settag()"); destination.file.stream = stderr; destination.file.name = NULL; destination.file.versions = ISC_LOG_ROLLNEVER; destination.file.maximum_size = 0; CHECK(isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC, level, &destination, ISC_LOG_PRINTTAG | ISC_LOG_PRINTLEVEL), "isc_log_createchannel()"); CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL), "isc_log_usechannel()"); dns_fixedname_init(&fname); name = dns_fixedname_name(&fname); isc_buffer_constinit(&buf, "example.", strlen("example.")); isc_buffer_add(&buf, strlen("example.")); CHECK(dns_name_fromtext(name, &buf, dns_rootname, 0, NULL), "dns_name_fromtext(\"example.\")"); CHECK(dst_key_buildinternal(name, DNS_KEYALG_RSASHA1, bits, DNS_KEYOWNER_ZONE, DNS_KEYPROTO_DNSSEC, dns_rdataclass_in, pkey, mctx, &key), "dst_key_buildinternal(...)"); CHECK(dst_key_tofile(key, DST_TYPE_PRIVATE | DST_TYPE_PUBLIC, NULL), "dst_key_tofile()"); isc_buffer_init(&buf, filename, sizeof(filename) - 1); isc_buffer_clear(&buf); CHECK(dst_key_buildfilename(key, 0, NULL, &buf), "dst_key_buildfilename()"); printf("%s\n", filename); dst_key_free(&key); isc_log_destroy(&log_); isc_log_setcontext(NULL); dns_log_setcontext(NULL); if (source != NULL) isc_entropy_destroysource(&source); isc_entropy_detach(&ectx); dst_lib_destroy(); dns_name_destroy(); isc_mem_destroy(&mctx); return (0); }
/*% * Generate a key of size 'keysize' using entropy source 'randomfile', * and place it in 'key_txtbuffer' */ void generate_key(isc_mem_t *mctx, const char *randomfile, dns_secalg_t alg, int keysize, isc_buffer_t *key_txtbuffer) { isc_result_t result = ISC_R_SUCCESS; isc_entropysource_t *entropy_source = NULL; int open_keyboard = ISC_ENTROPY_KEYBOARDMAYBE; int entropy_flags = 0; isc_entropy_t *ectx = NULL; isc_buffer_t key_rawbuffer; isc_region_t key_rawregion; char key_rawsecret[64]; dst_key_t *key = NULL; switch (alg) { case DST_ALG_HMACMD5: case DST_ALG_HMACSHA1: case DST_ALG_HMACSHA224: case DST_ALG_HMACSHA256: if (keysize < 1 || keysize > 512) fatal("keysize %d out of range (must be 1-512)\n", keysize); break; case DST_ALG_HMACSHA384: case DST_ALG_HMACSHA512: if (keysize < 1 || keysize > 1024) fatal("keysize %d out of range (must be 1-1024)\n", keysize); break; default: fatal("unsupported algorithm %d\n", alg); } DO("create entropy context", isc_entropy_create(mctx, &ectx)); if (randomfile != NULL && strcmp(randomfile, "keyboard") == 0) { randomfile = NULL; open_keyboard = ISC_ENTROPY_KEYBOARDYES; } DO("start entropy source", isc_entropy_usebestsource(ectx, &entropy_source, randomfile, open_keyboard)); entropy_flags = ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY; DO("initialize dst library", dst_lib_init(mctx, ectx, entropy_flags)); DO("generate key", dst_key_generate(dns_rootname, alg, keysize, 0, 0, DNS_KEYPROTO_ANY, dns_rdataclass_in, mctx, &key)); isc_buffer_init(&key_rawbuffer, &key_rawsecret, sizeof(key_rawsecret)); DO("dump key to buffer", dst_key_tobuffer(key, &key_rawbuffer)); isc_buffer_usedregion(&key_rawbuffer, &key_rawregion); DO("bsse64 encode secret", isc_base64_totext(&key_rawregion, -1, "", key_txtbuffer)); /* * Shut down the entropy source now so the "stop typing" message * does not muck with the output. */ if (entropy_source != NULL) isc_entropy_destroysource(&entropy_source); if (key != NULL) dst_key_free(&key); isc_entropy_detach(&ectx); dst_lib_destroy(); }
int main(int argc, char **argv) { isc_boolean_t show_final_mem = ISC_FALSE; isc_buffer_t key_rawbuffer; isc_buffer_t key_txtbuffer; isc_region_t key_rawregion; isc_mem_t *mctx = NULL; isc_entropy_t *ectx = NULL; isc_entropysource_t *entropy_source = NULL; isc_result_t result = ISC_R_SUCCESS; dst_key_t *key = NULL; const char *keyname = NULL; const char *randomfile = NULL; const char *serveraddr = NULL; char key_rawsecret[64]; char key_txtsecret[256]; char *p; int ch; int port; int keysize; int entropy_flags = 0; int open_keyboard = ISC_ENTROPY_KEYBOARDMAYBE; struct in_addr addr4_dummy; struct in6_addr addr6_dummy; char *chrootdir = NULL; char *user = NULL; isc_boolean_t keyonly = ISC_FALSE; int len; keydef = keyfile = RNDC_KEYFILE; result = isc_file_progname(*argv, program, sizeof(program)); if (result != ISC_R_SUCCESS) memcpy(program, "rndc-confgen", 13); progname = program; keyname = DEFAULT_KEYNAME; keysize = DEFAULT_KEYLENGTH; serveraddr = DEFAULT_SERVER; port = DEFAULT_PORT; while ((ch = isc_commandline_parse(argc, argv, "ab:c:hk:Mmp:r:s:t:u:Vy")) != -1) { switch (ch) { case 'a': keyonly = ISC_TRUE; break; case 'b': keysize = strtol(isc_commandline_argument, &p, 10); if (*p != '\0' || keysize < 0) fatal("-b requires a non-negative number"); if (keysize < 1 || keysize > 512) fatal("-b must be in the range 1 through 512"); break; case 'c': keyfile = isc_commandline_argument; break; case 'h': usage(0); case 'k': case 'y': /* Compatible with rndc -y. */ keyname = isc_commandline_argument; break; case 'M': isc_mem_debugging = ISC_MEM_DEBUGTRACE; break; case 'm': show_final_mem = ISC_TRUE; break; case 'p': port = strtol(isc_commandline_argument, &p, 10); if (*p != '\0' || port < 0 || port > 65535) fatal("port '%s' out of range", isc_commandline_argument); break; case 'r': randomfile = isc_commandline_argument; break; case 's': serveraddr = isc_commandline_argument; if (inet_pton(AF_INET, serveraddr, &addr4_dummy) != 1 && inet_pton(AF_INET6, serveraddr, &addr6_dummy) != 1) fatal("-s should be an IPv4 or IPv6 address"); break; case 't': chrootdir = isc_commandline_argument; break; case 'u': user = isc_commandline_argument; break; case 'V': verbose = ISC_TRUE; break; case '?': usage(1); break; default: fatal("unexpected error parsing command arguments: " "got %c\n", ch); break; } } argc -= isc_commandline_index; argv += isc_commandline_index; if (argc > 0) usage(1); DO("create memory context", isc_mem_create(0, 0, &mctx)); DO("create entropy context", isc_entropy_create(mctx, &ectx)); if (randomfile != NULL && strcmp(randomfile, "keyboard") == 0) { randomfile = NULL; open_keyboard = ISC_ENTROPY_KEYBOARDYES; } DO("start entropy source", isc_entropy_usebestsource(ectx, &entropy_source, randomfile, open_keyboard)); entropy_flags = ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY; DO("initialize dst library", dst_lib_init(mctx, ectx, entropy_flags)); DO("generate key", dst_key_generate(dns_rootname, DST_ALG_HMACMD5, keysize, 0, 0, DNS_KEYPROTO_ANY, dns_rdataclass_in, mctx, &key)); isc_buffer_init(&key_rawbuffer, &key_rawsecret, sizeof(key_rawsecret)); DO("dump key to buffer", dst_key_tobuffer(key, &key_rawbuffer)); isc_buffer_init(&key_txtbuffer, &key_txtsecret, sizeof(key_txtsecret)); isc_buffer_usedregion(&key_rawbuffer, &key_rawregion); DO("bsse64 encode secret", isc_base64_totext(&key_rawregion, -1, "", &key_txtbuffer)); /* * Shut down the entropy source now so the "stop typing" message * does not muck with the output. */ if (entropy_source != NULL) isc_entropy_destroysource(&entropy_source); if (key != NULL) dst_key_free(&key); isc_entropy_detach(&ectx); dst_lib_destroy(); if (keyonly) { write_key_file(keyfile, chrootdir == NULL ? user : NULL, keyname, &key_txtbuffer); if (chrootdir != NULL) { char *buf; len = strlen(chrootdir) + strlen(keyfile) + 2; buf = isc_mem_get(mctx, len); if (buf == NULL) fatal("isc_mem_get(%d) failed\n", len); snprintf(buf, len, "%s%s%s", chrootdir, (*keyfile != '/') ? "/" : "", keyfile); write_key_file(buf, user, keyname, &key_txtbuffer); isc_mem_put(mctx, buf, len); } } else { printf("\ # Start of rndc.conf\n\ key \"%s\" {\n\ algorithm hmac-md5;\n\ secret \"%.*s\";\n\ };\n\ \n\ options {\n\ default-key \"%s\";\n\ default-server %s;\n\ default-port %d;\n\ };\n\ # End of rndc.conf\n\ \n\ # Use with the following in named.conf, adjusting the allow list as needed:\n\ # key \"%s\" {\n\ # algorithm hmac-md5;\n\ # secret \"%.*s\";\n\ # };\n\ # \n\ # controls {\n\ # inet %s port %d\n\ # allow { %s; } keys { \"%s\"; };\n\ # };\n\ # End of named.conf\n", keyname, (int)isc_buffer_usedlength(&key_txtbuffer), (char *)isc_buffer_base(&key_txtbuffer), keyname, serveraddr, port, keyname, (int)isc_buffer_usedlength(&key_txtbuffer), (char *)isc_buffer_base(&key_txtbuffer), serveraddr, port, serveraddr, keyname); } if (show_final_mem) isc_mem_stats(mctx, stderr); isc_mem_destroy(&mctx); return (0); }