0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2009, 2012-2016 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
adb42115869b8258cd38bc7fd044766f662bdd78Mark Andrews/* $Id: keygen.c,v 1.4 2009/11/12 14:02:38 marka Exp $ */
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt * Convert algorithm type to string.
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt return "hmac-md5";
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt return "hmac-sha1";
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt return "hmac-sha224";
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt return "hmac-sha256";
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt return "hmac-sha384";
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt return "hmac-sha512";
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt return "(unknown)";
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt * Convert string to algorithm type.
46bc64f4b1a0e84ab0397943453fe83a17baf2c4Evan Hunt const char *p = name;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt * Return default keysize for a given algorithm type.
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt * Generate a key of size 'keysize' using entropy source 'randomfile',
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt * and place it in 'key_txtbuffer'
351b62535d4c4f89883bfdba025999dd32490266Evan Huntgenerate_key(isc_mem_t *mctx, const char *randomfile, dns_secalg_t alg,
33b8db1bb3f0aa3a39db459e6a32a1082b8dce13Evan Hunt fatal("keysize %d out of range (must be 1-512)\n",
33b8db1bb3f0aa3a39db459e6a32a1082b8dce13Evan Hunt fatal("keysize %d out of range (must be 1-1024)\n",
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt DO("create entropy context", isc_entropy_create(mctx, &ectx));
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt if (randomfile != NULL && strcmp(randomfile, "keyboard") == 0) {
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt DO("start entropy source", isc_entropy_usebestsource(ectx,
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt entropy_flags = ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY;
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt DO("initialize dst library", dst_lib_init(mctx, ectx, entropy_flags));
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt DO("generate key", dst_key_generate(dns_rootname, alg,
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt isc_buffer_init(&key_rawbuffer, &key_rawsecret, sizeof(key_rawsecret));
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt DO("dump key to buffer", dst_key_tobuffer(key, &key_rawbuffer));
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt isc_buffer_usedregion(&key_rawbuffer, &key_rawregion);
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt DO("bsse64 encode secret", isc_base64_totext(&key_rawregion, -1, "",
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt * Shut down the entropy source now so the "stop typing" message
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt * does not muck with the output.
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt * Write a key file to 'keyfile'. If 'user' is non-NULL,
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt * make that user the owner of the file. The key will have
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt * the name 'keyname' and the secret in the buffer 'secret'.
351b62535d4c4f89883bfdba025999dd32490266Evan Huntwrite_key_file(const char *keyfile, const char *user,
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt DO("create keyfile", isc_file_safecreate(keyfile, &fd));
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt "\tsecret \"%.*s\";\n};\n",
351b62535d4c4f89883bfdba025999dd32490266Evan Hunt fprintf(stderr, "wrote key file \"%s\"\n", keyfile);