dst_api.c revision 65c4736d9c0ebc6d9b1d991593b55566909da9cd
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer/*
1599ac12be522feb3f0cf8dab9fdde695a93a03fTinderbox User * Portions Copyright (c) 1995-1999 by Network Associates, Inc.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * copyright notice and this permission notice appear in all copies.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND NETWORK ASSOCIATES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NETWORK ASSOCIATES BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer/*
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * Principal Author: Brian Wellington
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * $Id: dst_api.c,v 1.1 1999/07/12 20:08:28 bwelling Exp $
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <config.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <ctype.h>
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington#include <limits.h>
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson#include <stdio.h>
0e77d336861c519c0c7da88620038ad95b2105efFrancis Dupont#include <stdlib.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <string.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/assertions.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/buffer.h>
1893b56ef9f5f2bc2a0fbe80d3c6b69df1bdc7c2Michael Sawyer#include <isc/error.h>
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer#include <isc/int.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <isc/lex.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <isc/mem.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <isc/mutex.h>
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer#include <isc/once.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <isc/region.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/rdata.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/keyvalues.h>
7ddb4c86a95ab721a70d406821352ce7b730a1bdAndreas Gustafsson
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <openssl/rand.h>
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include "dst_internal.h"
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include "dst/result.h"
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt#define KEY_MAGIC 0x44535421U /* DST! */
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt#define VALID_KEY(key) (key != NULL && key->magic == KEY_MAGIC)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Huntdst_func *dst_t_func[DST_MAX_ALGS];
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington
fb2c34419d268d36acb222e08e6f789b45a81306Mark Andrewsstatic isc_mem_t *dst_memory_pool = NULL;
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellingtonstatic isc_once_t once = ISC_ONCE_INIT;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerstatic isc_mutex_t random_lock;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer/* Static functions */
3d711f2f75cb9a9ddcbf1fca9b2de192e75340e6Mark Andrewsstatic void initialize(void);
3d711f2f75cb9a9ddcbf1fca9b2de192e75340e6Mark Andrewsstatic dst_key_t * get_key_struct(const char *name, const int alg,
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt const int flags, const int protocol,
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt const int bits, isc_mem_t *mctx);
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellingtonstatic dst_result_t read_public_key(const char *name,
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington const isc_uint16_t id, int in_alg,
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington isc_mem_t *mctx, dst_key_t **keyp);
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellingtonstatic dst_result_t write_public_key(const dst_key_t *key);
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer/*
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington * dst_supported_algorithm
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson * This function determines if the crypto system for the specified
a5ed46c9fd270775c39770bfd0250a52d374ebf2Michael Sawyer * algorithm is present.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * Parameters
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * alg The algorithm to test
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * Returns
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * ISC_TRUE The algorithm is available.
da5d1cf1b1aa29ae53a0427be49291b04bd60549Mark Andrews * ISC_FALSE The algorithm is not available.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerisc_boolean_t
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerdst_supported_algorithm(const int alg) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (alg >= DST_MAX_ALGS || dst_t_func[alg] == NULL)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer return (ISC_FALSE);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer return (ISC_TRUE);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer/*
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * dst_sign
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * An incremental signing function. Data is signed in steps.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * First the context must be initialized (DST_SIG_MODE_INIT).
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * Then data is hashed (DST_SIG_MODE_UPDATE). Finally the signature
a5ed46c9fd270775c39770bfd0250a52d374ebf2Michael Sawyer * itself is created (DST_SIG_MODE_FINAL). This function can be called
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * once with DST_SIG_MODE_ALL set, or it can be called separately
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * for each step. The UPDATE step may be repeated.
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Parameters
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * mode A bit mask specifying operation(s) to be performed.
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * DST_SIG_MODE_INIT Initialize digest
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * DST_SIG_MODE_UPDATE Add data to digest
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington * DST_SIG_MODE_FINAL Generate signature
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington * DST_SIG_MODE_ALL Perform all operations
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington * key The private key used to sign the data
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington * context The state of the operation
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington * data The data to be signed.
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * sig The buffer to which the signature will be written.
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * mctx Memory context used for allocations
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Return
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * DST_R_SUCCESS Success
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington * !DST_R_SUCCESS Failure
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerdst_result_t
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerdst_sign(const int mode, dst_key_t *key, void **context,
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_region_t *data, isc_buffer_t *sig, isc_mem_t *mctx)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer{
e32394a2ac3466a2235f79ee32c247a11be42a8dAndreas Gustafsson RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer REQUIRE(VALID_KEY(key));
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer REQUIRE(mctx != NULL);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer REQUIRE((mode & DST_SIG_MODE_ALL) != 0);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington if ((mode & DST_SIG_MODE_UPDATE) != 0)
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington REQUIRE(data != NULL && data->base != NULL);
36c754f274dd439e5a7ebf2401427c1d51b21302Brian Wellington
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington if ((mode & DST_SIG_MODE_FINAL) != 0)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer REQUIRE(sig != NULL);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (dst_supported_algorithm(key->key_alg) == ISC_FALSE)
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington return (DST_R_UNSUPPORTED_ALG);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (key->opaque == NULL)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (DST_R_NULL_KEY);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (key->func->sign(mode, key, context, data, sig, mctx));
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington
9a762177e6a0aff3ea5c00f87568c8ae3cd0e6c7Andreas Gustafsson/*
9a762177e6a0aff3ea5c00f87568c8ae3cd0e6c7Andreas Gustafsson * dst_verify
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * An incremental verify function. Data is verified in steps.
066cb385cd8f801dd3757db2922c9b1f343e65edAndreas Gustafsson * First the context must be initialized (DST_SIG_MODE_INIT).
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Then data is hashed (DST_SIG_MODE_UPDATE). Finally the signature
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington * is verified (DST_SIG_MODE_FINAL). This function can be called
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington * once with DST_SIG_MODE_ALL set, or it can be called separately
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington * for each step. The UPDATE step may be repeated.
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * Parameters
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * mode A bit mask specifying operation(s) to be performed.
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * DST_SIG_MODE_INIT Initialize digest
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * DST_SIG_MODE_UPDATE Add data to digest
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * DST_SIG_MODE_FINAL Verify signature
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * DST_SIG_MODE_ALL Perform all operations
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * key The public key used to verify the signature.
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * context The state of the operation
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * data The data to be digested.
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * sig The signature.
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * mctx Memory context used for allocations
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * Returns
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * DST_R_SUCCESS Success
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont * !DST_R_SUCCESS Failure
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont */
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupontdst_result_t
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerdst_verify(const int mode, dst_key_t *key, void **context,
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_region_t *data, isc_region_t *sig, isc_mem_t *mctx)
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington{
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington REQUIRE(VALID_KEY(key));
cc48bb397fa6ba889f25157840492e68114dec8fBrian Wellington REQUIRE(mctx != NULL);
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington REQUIRE((mode & DST_SIG_MODE_ALL) != 0);
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington if ((mode & DST_SIG_MODE_UPDATE) != 0)
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington REQUIRE(data != NULL && data->base != NULL);
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington if ((mode & DST_SIG_MODE_FINAL) != 0)
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington REQUIRE(sig != NULL && sig->base != NULL);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cc48bb397fa6ba889f25157840492e68114dec8fBrian Wellington if (dst_supported_algorithm(key->key_alg) == ISC_FALSE)
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington return (DST_R_UNSUPPORTED_ALG);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington if (key->opaque == NULL)
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington return (DST_R_NULL_KEY);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington return (key->func->verify(mode, key, context, data, sig, mctx));
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington}
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington/*
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * dst_key_tofile
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * Writes a key to disk. The key can either be a public or private key.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * Parameters
c38b92000c0f1a95daaad5468777e165b8047de9Mark Andrews * key The key to be written.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * type Either DST_PUBLIC or DST_PRIVATE, or both
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * Returns
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * DST_R_SUCCESS Success
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * !DST_R_SUCCESS Failure
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington */
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellingtondst_result_t
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellingtondst_key_tofile(const dst_key_t *key, const int type) {
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington int ret = DST_R_SUCCESS;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington REQUIRE(VALID_KEY(key));
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington if (dst_supported_algorithm(key->key_alg) == ISC_FALSE)
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington return (DST_R_UNSUPPORTED_ALG);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington if ((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) == 0)
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington return (DST_R_UNSUPPORTED_TYPE);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington if (type & DST_TYPE_PUBLIC)
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington if ((ret = write_public_key(key)) != DST_R_SUCCESS)
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington return (ret);
2a23a625246acfbf6ff92b86a6b8b9df59dbeaa4Mark Andrews
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews if ((type & DST_TYPE_PRIVATE) &&
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews (key->key_flags & NS_KEY_TYPEMASK) != NS_KEY_TYPE_NO_KEY)
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews {
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews ret = key->func->to_file(key);
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews if (ret != DST_R_SUCCESS)
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews return (ret);
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews }
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews return (ret);
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews}
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington/*
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * dst_key_fromfile
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * Reads a key from disk. The key can either be a public or private
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * key, and is specified by name, algorithm, and id.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * Parameters
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * name The key name.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * id The id of the key.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * alg The algorithm of the key.
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * type Either DST_PUBLIC or DST_PRIVATE
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * mctx Memory context used to allocate key structure
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * keyp Returns the new key
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * Returns
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * DST_R_SUCCESS Success
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * !DST_R_SUCCESS Failure
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington */
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellingtondst_result_t
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellingtondst_key_fromfile(const char *name, const isc_uint16_t id, const int alg,
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington const int type, isc_mem_t *mctx, dst_key_t **keyp)
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington{
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington dst_key_t *key = NULL, *pubkey = NULL;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington dst_result_t ret;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington REQUIRE(name != NULL);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington REQUIRE(mctx != NULL);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington REQUIRE(keyp != NULL);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington *keyp = NULL;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (dst_supported_algorithm(alg) == ISC_FALSE)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (DST_R_UNSUPPORTED_ALG);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if ((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) == 0)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (DST_R_UNSUPPORTED_TYPE);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews ret = read_public_key(name, id, alg, mctx, &pubkey);
7ddb4c86a95ab721a70d406821352ce7b730a1bdAndreas Gustafsson if (ret != DST_R_SUCCESS)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (ret);
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson if (type == DST_TYPE_PUBLIC ||
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer (pubkey->key_flags & NS_KEY_TYPEMASK) == NS_KEY_TYPE_NO_KEY)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer *keyp = pubkey;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (DST_R_SUCCESS);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer key = get_key_struct(name, pubkey->key_alg, pubkey->key_flags,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer pubkey->key_proto, 0, mctx);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer dst_key_free(pubkey, mctx);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (key == NULL)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (DST_R_NOMEMORY);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer /* Fill in private key and some fields in the general key structure */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer ret = key->func->from_file(key, id, mctx);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (ret != DST_R_SUCCESS) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dst_key_free(key, mctx);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (ret);
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson }
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer *keyp = key;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (DST_R_SUCCESS);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer}
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson/*
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * dst_key_todns
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * Function to encode a public key into DNS KEY format
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Parameters
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * key Key structure to encode.
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson * target Buffer to write the encoded key into.
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson * Returns
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson * DST_R_SUCCESS Success
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * !DST_R_SUCCESS Failure
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerdst_result_t
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerdst_key_todns(const dst_key_t *key, isc_buffer_t *target) {
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson isc_region_t r;
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington REQUIRE(VALID_KEY(key));
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington REQUIRE(target != NULL);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews if (dst_supported_algorithm(key->key_alg) == ISC_FALSE)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (DST_R_UNSUPPORTED_ALG);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_buffer_available(target, &r);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (r.length < 4)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer return (DST_R_NOSPACE);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_buffer_putuint16(target, key->key_flags & 0xffff);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_buffer_putuint8(target, key->key_proto);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_buffer_putuint8(target, key->key_alg);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (key->key_flags & NS_KEY_EXTENDED_FLAGS) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_buffer_available(target, &r);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (r.length < 2)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer return (DST_R_NOSPACE);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer isc_buffer_putuint16(target, (key->key_flags >> 16) & 0xffff);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (key->opaque == NULL) /* NULL KEY */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer return (DST_R_SUCCESS);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews return (key->func->to_dns(key, target));
7ddb4c86a95ab721a70d406821352ce7b730a1bdAndreas Gustafsson}
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson/*
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * dst_key_fromdns
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer * This function converts the contents of a DNS KEY RR into a key
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * Paramters
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * name Name of the new key
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * source A buffer containing the KEY RR
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * mctx The memory context used to allocate the key
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson * keyp Returns the new key
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * Returns
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * DST_R_SUCCESS Success
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson * !DST_R_SUCCESS Failure
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafssondst_result_t
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyerdst_key_fromdns(const char *name, isc_buffer_t *source, isc_mem_t *mctx,
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer dst_key_t **keyp)
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson{
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer isc_region_t r;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer isc_uint8_t alg, proto;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer isc_uint32_t flags, extflags;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer dst_result_t ret;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer REQUIRE (name != NULL);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer REQUIRE (source != NULL);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer REQUIRE (mctx != NULL);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer REQUIRE (keyp != NULL);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_buffer_remaining(source, &r);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (r.length < 4) /* 2 bytes of flags, 1 proto, 1 alg */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer return (DST_R_INVALID_PUBLIC_KEY);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer flags = isc_buffer_getuint16(source);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer proto = isc_buffer_getuint8(source);
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer alg = isc_buffer_getuint8(source);
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson if (!dst_supported_algorithm(alg))
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson return (DST_R_UNSUPPORTED_ALG);
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson if (flags & NS_KEY_EXTENDED_FLAGS) {
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson isc_buffer_remaining(source, &r);
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson if (r.length < 2)
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer return (DST_R_INVALID_PUBLIC_KEY);
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson extflags = isc_buffer_getuint16(source);
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson flags |= (extflags << 16);
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson }
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer *keyp = get_key_struct(name, alg, flags, proto, 0, mctx);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (*keyp == NULL)
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer return(DST_R_NOMEMORY);
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson ret = (*keyp)->func->from_dns(*keyp, source, mctx);
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson if (ret != DST_R_SUCCESS)
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson dst_key_free((*keyp), mctx);
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson return (ret);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer}
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer/*
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * dst_key_frombuffer
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * Function to convert raw data into a public key. The raw data format
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington * is basically DNS KEY rdata format.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Parameters
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews * name The key name
4b30598fb908755c4fd04f51cf3ce1f550434bf3Mark Andrews * alg The algorithm
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * flags The key's flags
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * protocol The key's protocol
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * source A buffer containing the key
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * mctx The memory context used to allocate the key
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * keyp Returns the new key
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * Returns
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * DST_R_SUCCESS Success
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * !DST_R_SUCCESS Failure
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyerdst_result_t
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyerdst_key_frombuffer(const char *name, const int alg, const int flags,
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer const int protocol, isc_buffer_t *source, isc_mem_t *mctx,
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer dst_key_t **keyp)
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson{
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson dst_result_t ret;
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson REQUIRE(name != NULL);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson REQUIRE(source != NULL);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson REQUIRE(mctx != NULL);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson if (dst_supported_algorithm(alg) == ISC_FALSE)
f9521c63ea42fee93f7ae67512e4330ab13081c8Brian Wellington return (DST_R_UNSUPPORTED_ALG);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson *keyp = get_key_struct(name, alg, flags, protocol, 0, mctx);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson if (*keyp == NULL)
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson return (DST_R_NOMEMORY);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer ret = (*keyp)->func->from_dns((*keyp), source, mctx);
96eeb9496c09114c116132d5a493ef5eb88e3192Automatic Updater if (ret != DST_R_SUCCESS) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer dst_key_free((*keyp), mctx);
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt return (ret);
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt }
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt return (DST_R_SUCCESS);
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson}
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson/*
3f79a8b6f1b50f8e1bc2f274bcfe91d6af74f161Mark Andrews * dst_key_tobuffer
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * Function to convert a public key into raw data. The raw data format
96eeb9496c09114c116132d5a493ef5eb88e3192Automatic Updater * is basically DNS KEY rdata format.
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer * Parameters
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * key The key
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson * target The buffer to be written into.
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * Returns
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson * DST_R_SUCCESS Success
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * !DST_R_SUCCESS Failure
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer */
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyerdst_result_t
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyerdst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target) {
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson REQUIRE(VALID_KEY(key));
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer REQUIRE(target != NULL);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson if (dst_supported_algorithm(key->key_alg) == ISC_FALSE)
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson return (DST_R_UNSUPPORTED_ALG);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson return (key->func->to_dns(key, target));
aaa42824d2d1f5cc4add01f0191e3784ee0f7396Mark Andrews}
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson/*
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt * dst_key_generate
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt * Generate and store a public/private keypair.
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt * Keys will be stored in formatted files.
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * Parameters
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * name Name of the new key. Used to create key files
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson * K<name>+<alg>+<id>.public
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * K<name>+<alg>+<id>.private
7804502532d6e2eee80083431a22eb5d957f52a0Michael Sawyer * alg The algorithm to use
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer * bits Size of the new key in bits
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer * param Algorithm specific (currently RSA only)
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer * 0 use exponent 3
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer * !0 use Fermat4 (2^16 + 1)
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer * flags The default value of the DNS Key flags.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * protocol Default value of the DNS Key protocol field.
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * mctx The memory context used to allocate the key
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer * keyp Returns the new key
7804502532d6e2eee80083431a22eb5d957f52a0Michael Sawyer *
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * Return
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * DST_R_SUCCESS Success
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer * !DST_R_SUCCESS Failure
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerdst_result_t
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerdst_key_generate(const char *name, const int alg, const int bits,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer const int exp, const int flags, const int protocol,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_mem_t *mctx, dst_key_t **keyp)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer{
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer dst_result_t ret;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer REQUIRE(name != NULL);
7f658603910358db7ee27ffb9783096250afab62Tatuya JINMEI 神明達哉 REQUIRE(mctx != NULL);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence REQUIRE(keyp != NULL);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (dst_supported_algorithm(alg) == ISC_FALSE)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (DST_R_UNSUPPORTED_ALG);
46e349e515e0b992ecafdc9597f9d5ee0cf81b2aBrian Wellington
46e349e515e0b992ecafdc9597f9d5ee0cf81b2aBrian Wellington *keyp = get_key_struct(name, alg, flags, protocol, bits, mctx);
7f658603910358db7ee27ffb9783096250afab62Tatuya JINMEI 神明達哉 if (*keyp == NULL)
7f658603910358db7ee27ffb9783096250afab62Tatuya JINMEI 神明達哉 return (DST_R_NOMEMORY);
7f658603910358db7ee27ffb9783096250afab62Tatuya JINMEI 神明達哉
46e349e515e0b992ecafdc9597f9d5ee0cf81b2aBrian Wellington if (bits == 0) { /* NULL KEY */
46e349e515e0b992ecafdc9597f9d5ee0cf81b2aBrian Wellington (*keyp)->key_flags |= NS_KEY_TYPE_NO_KEY;
3f79a8b6f1b50f8e1bc2f274bcfe91d6af74f161Mark Andrews return (DST_R_SUCCESS);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ret = (*keyp)->func->generate(*keyp, exp, mctx);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (ret != DST_R_SUCCESS) {
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer dst_key_free(*keyp, mctx);
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer return (ret);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson }
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson return (DST_R_SUCCESS);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson}
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson/*
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson * dst_key_compare
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson * Compares two keys for equality.
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews * Parameters
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews * key1, key2 Two keys to be compared.
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson * Returns
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson * ISC_TRUE The keys are equal.
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson * ISC_FALSE The keys are not equal.
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson */
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafssonisc_boolean_t
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafssondst_key_compare(const dst_key_t *key1, const dst_key_t *key2) {
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson REQUIRE(VALID_KEY(key1));
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson REQUIRE(VALID_KEY(key2));
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (key1 == key2)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer return (ISC_TRUE);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer if (key1 == NULL || key2 == NULL)
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer return (ISC_FALSE);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer if (key1->key_alg == key2->key_alg &&
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer key1->key_id == key2->key_id &&
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer key1->func->compare(key1, key2) == ISC_TRUE)
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer return (ISC_TRUE);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer else
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer return (ISC_FALSE);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer}
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer/*
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer * dst_key_free
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer * Release all data structures pointed to by a key structure.
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer * Parameters
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer * key Key structure to be freed.
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer * mctx The memory context used to allocate the key
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer */
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyervoid
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyerdst_key_free(dst_key_t *key, isc_mem_t *mctx) {
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer REQUIRE(VALID_KEY(key));
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer REQUIRE(mctx != NULL);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer if (key->opaque != NULL)
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer key->func->destroy(key->opaque, mctx);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
96eeb9496c09114c116132d5a493ef5eb88e3192Automatic Updater isc_mem_free(mctx, key->key_name);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer memset(key, 0, sizeof(dst_key_t));
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer isc_mem_put(mctx, key, sizeof(dst_key_t));
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer}
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyerchar *
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyerdst_key_name(const dst_key_t *key) {
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer REQUIRE(VALID_KEY(key));
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson return key->key_name;
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson}
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssonint
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssondst_key_size(const dst_key_t *key) {
8ef03ac2743f73f7ce3c92d739c64d31387abb45Danny Mayer REQUIRE(VALID_KEY(key));
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson return key->key_size;
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson}
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssonint
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssondst_key_proto(const dst_key_t *key) {
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson REQUIRE(VALID_KEY(key));
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson return key->key_proto;
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson}
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssonint
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssondst_key_alg(const dst_key_t *key) {
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson REQUIRE(VALID_KEY(key));
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson return key->key_alg;
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson}
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssonisc_uint32_t
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssondst_key_flags(const dst_key_t *key) {
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer REQUIRE(VALID_KEY(key));
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews return key->key_flags;
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews}
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrewsisc_uint16_t
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrewsdst_key_id(const dst_key_t *key) {
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews REQUIRE(VALID_KEY(key));
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews return key->key_id;
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews}
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt/*
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt * dst_sig_size
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt * Computes the maximum size of a signature generated by the given key
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt * Parameters
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * key The DST key
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Returns
32e783c745299f838dd7cf8ffe2032775a80f1d0Andreas Gustafsson * n The number of bytes necessary to hold a signature with the key.
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerint
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyerdst_sig_size(const dst_key_t *key) {
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer REQUIRE(VALID_KEY(key));
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer REQUIRE(dst_supported_algorithm(key->key_alg) == ISC_TRUE);
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer switch (key->key_alg) {
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer case DST_ALG_RSA:
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews return key->key_size;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer case DST_ALG_DSA:
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer return (NS_DSA_SIG_SIZE);
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews case DST_ALG_HMAC_MD5:
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (16);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer case DST_ALG_HMAC_SHA1:
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews return (20);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer default:
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer REQUIRE(ISC_FALSE);
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews return (-1);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer}
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews
7f20fd8ebb0cabc8f935381d958f8371990c9212Mark Andrews/*
7f20fd8ebb0cabc8f935381d958f8371990c9212Mark Andrews * dst_random
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews * a random number generator that can generate different levels of
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * randomness
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews * Parameters
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson * mode selects the random number generator
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer * wanted the number of random bytes requested
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * target the buffer to store the random data
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews * Returns
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson * DST_R_SUCCESS Success
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer * !DST_R_SUCCESS Failure
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer */
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssondst_result_t
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerdst_random(const unsigned int wanted, isc_buffer_t *target) {
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson isc_region_t r;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer REQUIRE(target != NULL);
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson
96eeb9496c09114c116132d5a493ef5eb88e3192Automatic Updater isc_buffer_available(target, &r);
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer if (r.length < wanted)
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer return (DST_R_NOSPACE);
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer RUNTIME_CHECK(isc_mutex_lock((&random_lock)) == ISC_R_SUCCESS);
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson RAND_bytes(r.base, wanted);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer RUNTIME_CHECK(isc_mutex_unlock((&random_lock)) == ISC_R_SUCCESS);
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson isc_buffer_add(target, wanted);
96eeb9496c09114c116132d5a493ef5eb88e3192Automatic Updater return (DST_R_SUCCESS);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson/***
96eeb9496c09114c116132d5a493ef5eb88e3192Automatic Updater *** Static methods
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer ***/
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer/*
96eeb9496c09114c116132d5a493ef5eb88e3192Automatic Updater * initialize
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * This function initializes the Digital Signature Toolkit.
824cb6567555af556d0963d961798483d252eb5fMark Andrews * Parameters
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * none
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Returns
824cb6567555af556d0963d961798483d252eb5fMark Andrews * none
96eeb9496c09114c116132d5a493ef5eb88e3192Automatic Updater */
d821f1cd7e97552401296e880e7518c98c9ebea1Michael Sawyerstatic void
d821f1cd7e97552401296e880e7518c98c9ebea1Michael Sawyerinitialize() {
d821f1cd7e97552401296e880e7518c98c9ebea1Michael Sawyer memset(dst_t_func, 0, sizeof(dst_t_func));
9a762177e6a0aff3ea5c00f87568c8ae3cd0e6c7Andreas Gustafsson
aa2c453d3c6e416b56b29247bedd9a0af2721e93Mark Andrews RUNTIME_CHECK(isc_mem_create(0, 0, &dst_memory_pool) == ISC_R_SUCCESS);
9a762177e6a0aff3ea5c00f87568c8ae3cd0e6c7Andreas Gustafsson RUNTIME_CHECK(isc_mutex_init(&random_lock) == ISC_R_SUCCESS);
aa2c453d3c6e416b56b29247bedd9a0af2721e93Mark Andrews
9a762177e6a0aff3ea5c00f87568c8ae3cd0e6c7Andreas Gustafsson dst_result_register();
fb2c34419d268d36acb222e08e6f789b45a81306Mark Andrews
3d711f2f75cb9a9ddcbf1fca9b2de192e75340e6Mark Andrews dst_s_hmacmd5_init();
3d711f2f75cb9a9ddcbf1fca9b2de192e75340e6Mark Andrews#if defined(BSAFE) || defined(DNSSAFE)
3d711f2f75cb9a9ddcbf1fca9b2de192e75340e6Mark Andrews dst_s_bsafe_init();
3d711f2f75cb9a9ddcbf1fca9b2de192e75340e6Mark Andrews#endif
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews#ifdef OPENSSL
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews dst_s_openssl_init();
48674819ebf9176b5d5582ae851e485c324c1159Michael Sawyer#endif
96eeb9496c09114c116132d5a493ef5eb88e3192Automatic Updater#ifdef CYLINK_DSS
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer dst_s_cylink_init();
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#endif
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}
869f65bea49f8d65ab9bb14c4551215529bd7419David Lawrence
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer/*
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * get_key_struct
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer * This function allocates key structure and fills in some of the
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer * fields of the structure.
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer * Parameters:
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer * name the name of the key
5d20773abcdf9ff3afe14a349413174f94594188Michael Sawyer * alg the algorithm number
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * flags the dns flags of the key
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson * protocol the dns protocol of the key
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer * bits the size of the key
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer * mctx the memory context to allocate from
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer * Returns:
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer * NULL error
32e783c745299f838dd7cf8ffe2032775a80f1d0Andreas Gustafsson * valid pointer otherwise
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer */
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyerstatic dst_key_t *
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyerget_key_struct(const char *name, const int alg, const int flags,
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer const int protocol, const int bits, isc_mem_t *mctx)
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer{
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer dst_key_t *key;
32e783c745299f838dd7cf8ffe2032775a80f1d0Andreas Gustafsson
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer REQUIRE(dst_supported_algorithm(alg) != ISC_FALSE);
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer key = (dst_key_t *) isc_mem_get(mctx, sizeof(dst_key_t));
974f1d7b49442d65542ad3517dbd61146ee3b653Mark Andrews if (key == NULL)
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews return (NULL);
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer memset(key, 0, sizeof(dst_key_t));
cc7732e1299042bff430dfdfbc2624fb340047b2Michael Sawyer key->magic = KEY_MAGIC;
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer key->key_name = isc_mem_strdup(mctx, name);
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews key->key_alg = alg;
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer key->key_flags = flags;
cc7732e1299042bff430dfdfbc2624fb340047b2Michael Sawyer key->key_proto = protocol;
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer key->opaque = NULL;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer key->key_size = bits;
cc7732e1299042bff430dfdfbc2624fb340047b2Michael Sawyer key->func = dst_t_func[alg];
1d78c8d7cd619df2255ec52d69b0dd0d8850293fBrian Wellington return (key);
1d78c8d7cd619df2255ec52d69b0dd0d8850293fBrian Wellington}
1d78c8d7cd619df2255ec52d69b0dd0d8850293fBrian Wellington
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer/*
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * dst_read_public_key
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Read a public key from disk
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer * Parameters
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington * name The name
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * id The id
9fe3676b8490319aa65182f2072cbf5086097979Michael Sawyer * alg The algorithm
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * mctx The memory context used to allocate the key
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * keyp Returns the new key
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Returns
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * DST_R_SUCCESS Success
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * !DST_R_SUCCESS Failure
17747cd5ee98901b435cd2b3019c52937955a981Michael Sawyer */
3d711f2f75cb9a9ddcbf1fca9b2de192e75340e6Mark Andrews
3d711f2f75cb9a9ddcbf1fca9b2de192e75340e6Mark Andrewsstatic dst_result_t
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerread_public_key(const char *name, const isc_uint16_t id, int alg,
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer isc_mem_t *mctx, dst_key_t **keyp)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer{
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer char filename[PATH_MAX];
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer u_char rdatabuf[DST_MAX_KEY_SIZE];
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer isc_buffer_t b;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_lex_t *lex = NULL;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_token_t token;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_result_t iret;
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt dns_result_t dret, ret;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dns_rdata_t rdata;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer unsigned int opt = ISC_LEXOPT_DNSMULTILINE;
e412ae947df6de858883564b8676a9650df70d9aMark Andrews
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington if (dst_s_build_filename(filename, name, id, alg, PUBLIC_KEY,
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt PATH_MAX) != DST_R_SUCCESS)
e412ae947df6de858883564b8676a9650df70d9aMark Andrews return (DST_R_NAME_TOO_LONG);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer /*
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Open the file and read its formatted contents
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer * File format:
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer * domain.name [ttl] [IN] KEY <flags> <protocol> <algorithm> <key>
3f79a8b6f1b50f8e1bc2f274bcfe91d6af74f161Mark Andrews */
b31e7ab8857a62eb94d4a6a681243739f158374dDanny Mayer
715361d0c3800e5ad886e5df971936ce6cd1ca89Mark Andrews /* 540 should be large enough for a 1024 bit DSA key */
3f79a8b6f1b50f8e1bc2f274bcfe91d6af74f161Mark Andrews iret = isc_lex_create(mctx, 550, &lex);
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer if (iret != ISC_R_SUCCESS)
6e3a8e17fdf108f47bbba17474fd3d120d356b2fBrian Wellington return (DST_R_NOMEMORY);
6e3a8e17fdf108f47bbba17474fd3d120d356b2fBrian Wellington
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington iret = isc_lex_openfile(lex, filename);
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews if (iret != ISC_R_SUCCESS)
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington goto cleanup;
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington#define NEXTTOKEN(lex, opt, token) { \
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington iret = isc_lex_gettoken(lex, opt, token); \
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews if (iret != ISC_R_SUCCESS) \
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington goto cleanup; \
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt /* Read the domain name */
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt NEXTTOKEN(lex, opt, &token);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt /* Read the next word: either TTL, 'IN', or 'KEY' */
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt NEXTTOKEN(lex, opt, &token);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt /* If it's a TTL, read the next one */
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt if (token.type == isc_tokentype_number)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt NEXTTOKEN(lex, opt, &token);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt if (token.type != isc_tokentype_string)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt goto cleanup;
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt if (strcasecmp(token.value.as_pointer, "IN") == 0)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt NEXTTOKEN(lex, opt, &token);
1a4725bef25e60de161f77318f362c327b690d72Mark Andrews
1a4725bef25e60de161f77318f362c327b690d72Mark Andrews if (token.type != isc_tokentype_string)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt goto cleanup;
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt if (strcasecmp(token.value.as_pointer, "KEY") != 0)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt goto cleanup;
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt isc_buffer_init(&b, rdatabuf, sizeof(rdatabuf), ISC_BUFFERTYPE_BINARY);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt dret = dns_rdata_fromtext(&rdata, dns_rdataclass_in, dns_rdatatype_key,
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt lex, NULL, ISC_FALSE, &b, NULL);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt if (dret != DNS_R_SUCCESS)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt goto cleanup;
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt ret = dst_key_fromdns(name, &b, mctx, keyp);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt if (ret != DST_R_SUCCESS || (*keyp)->key_alg != alg)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt goto cleanup;
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt isc_lex_close(lex);
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews isc_lex_destroy(&lex);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (DST_R_SUCCESS);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyercleanup:
19c8df90f1f23c3df870c1771c89c1acdb15020eMichael Sawyer if (lex != NULL) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_lex_close(lex);
66921d6dd259c4486a07aba19eee2b8349698575Brian Wellington isc_lex_destroy(&lex);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson return (DST_R_INVALID_PUBLIC_KEY);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer}
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt/*
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt * write_public_key
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Write a key to disk in DNS format.
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt * Parameters
19c8df90f1f23c3df870c1771c89c1acdb15020eMichael Sawyer * key A DST key
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer * Returns
19c8df90f1f23c3df870c1771c89c1acdb15020eMichael Sawyer * DST_R_SUCCESS Success
19c8df90f1f23c3df870c1771c89c1acdb15020eMichael Sawyer * !DST_R_SUCCESS Failure
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer */
869f65bea49f8d65ab9bb14c4551215529bd7419David Lawrence
715361d0c3800e5ad886e5df971936ce6cd1ca89Mark Andrewsstatic dst_result_t
b31e7ab8857a62eb94d4a6a681243739f158374dDanny Mayerwrite_public_key(const dst_key_t *key) {
715361d0c3800e5ad886e5df971936ce6cd1ca89Mark Andrews FILE *fp;
715361d0c3800e5ad886e5df971936ce6cd1ca89Mark Andrews isc_buffer_t keyb, textb;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_region_t r;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer char filename[PATH_MAX];
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer unsigned char key_array[DST_MAX_KEY_SIZE];
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer char text_array[DST_MAX_KEY_SIZE];
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dst_result_t ret;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dns_result_t dnsret;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dns_rdata_t rdata;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer REQUIRE(VALID_KEY(key));
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_buffer_init(&keyb, key_array, sizeof(key_array),
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ISC_BUFFERTYPE_BINARY);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_buffer_init(&textb, text_array, sizeof(text_array),
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ISC_BUFFERTYPE_TEXT);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ret = dst_key_todns(key, &keyb);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (ret != DST_R_SUCCESS)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (ret);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_buffer_used(&keyb, &r);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dns_rdata_fromregion(&rdata, dns_rdataclass_in, dns_rdatatype_key, &r);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dnsret = dns_rdata_totext(&rdata, (dns_name_t *) NULL, &textb);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (dnsret != DNS_R_SUCCESS)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (DST_R_INVALID_PUBLIC_KEY);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dns_rdata_freestruct(&rdata);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_buffer_used(&textb, &r);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer /* Make the filename */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (dst_s_build_filename(filename, key->key_name, key->key_id,
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer key->key_alg, PUBLIC_KEY, PATH_MAX) < 0)
e715e011788a529446b8013239c33599542ece32Michael Sawyer return (DST_R_NAME_TOO_LONG);
e715e011788a529446b8013239c33599542ece32Michael Sawyer
e715e011788a529446b8013239c33599542ece32Michael Sawyer /* create public key file */
e715e011788a529446b8013239c33599542ece32Michael Sawyer if ((fp = fopen(filename, "w")) == NULL)
e715e011788a529446b8013239c33599542ece32Michael Sawyer return (DST_R_WRITE_ERROR);
e715e011788a529446b8013239c33599542ece32Michael Sawyer
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence fprintf(fp, "%s IN KEY ", key->key_name);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer fwrite(r.base, 1, r.length, fp);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer fputc('\n', fp);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer fclose(fp);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (DST_R_SUCCESS);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyervoid *
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerdst_mem_alloc(size_t size) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer INSIST(dst_memory_pool != NULL);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (isc_mem_allocate(dst_memory_pool, size));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellingtonvoid
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellingtondst_mem_free(void *ptr) {
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington INSIST(dst_memory_pool != NULL);
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington if (ptr != NULL)
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington isc_mem_free(dst_memory_pool, ptr);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer}
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyervoid *
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyerdst_mem_realloc(void *ptr, size_t size) {
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer void *p;
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer INSIST(dst_memory_pool != NULL);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer p = NULL;
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington if (size > 0) {
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington p = dst_mem_alloc(size);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (p != NULL && ptr != NULL)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer memcpy(p, ptr, size);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (ptr != NULL)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt dst_mem_free(ptr);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt return(p);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer