1999-07-12 20:08:42 +00:00
|
|
|
/*
|
|
|
|
* Portions Copyright (c) 1995-1998 by Network Associates, Inc.
|
2000-05-08 14:38:29 +00:00
|
|
|
* Portions Copyright (C) 1999, 2000 Internet Software Consortium.
|
1999-07-12 20:08:42 +00:00
|
|
|
*
|
2000-05-08 14:38:29 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
1999-07-12 20:08:42 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
2000-05-08 14:38:29 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM AND
|
|
|
|
* NETWORK ASSOCIATES DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
|
|
|
|
* SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE CONSORTIUM OR NETWORK
|
|
|
|
* ASSOCIATES BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
|
|
|
|
* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Principal Author: Brian Wellington
|
2000-06-01 02:33:26 +00:00
|
|
|
* $Id: hmac_link.c,v 1.28 2000/06/01 02:33:26 bwelling Exp $
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/string.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-05-02 03:54:17 +00:00
|
|
|
#include <dst/result.h>
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
#include "dst_internal.h"
|
|
|
|
#include "dst_parse.h"
|
|
|
|
|
2000-04-20 18:27:43 +00:00
|
|
|
#include <openssl/md5.h>
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
#define HMAC_LEN 64
|
|
|
|
#define HMAC_IPAD 0x36
|
|
|
|
#define HMAC_OPAD 0x5c
|
|
|
|
|
1999-10-29 05:25:57 +00:00
|
|
|
#define RETERR(x) do { \
|
1999-10-18 21:35:46 +00:00
|
|
|
ret = (x); \
|
|
|
|
if (ret != ISC_R_SUCCESS) \
|
|
|
|
return (ret); \
|
|
|
|
} while (0)
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
typedef struct hmackey {
|
|
|
|
unsigned char ipad[64], opad[64];
|
|
|
|
} HMAC_Key;
|
|
|
|
|
|
|
|
static struct dst_func hmacmd5_functions;
|
|
|
|
|
2000-05-02 03:54:17 +00:00
|
|
|
static isc_result_t
|
|
|
|
dst_hmacmd5_sign(const unsigned int mode, dst_key_t *key,
|
|
|
|
void **context, isc_region_t *data, isc_buffer_t *sig,
|
|
|
|
isc_mem_t *mctx);
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
dst_hmacmd5_verify(const unsigned int mode, dst_key_t *key, void **context,
|
|
|
|
isc_region_t *data, isc_region_t *sig, isc_mem_t *mctx);
|
|
|
|
|
|
|
|
static isc_boolean_t
|
|
|
|
dst_hmacmd5_compare(const dst_key_t *key1, const dst_key_t *key2);
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
dst_hmacmd5_generate(dst_key_t *key, int exp, isc_mem_t *mctx);
|
|
|
|
|
|
|
|
static isc_boolean_t
|
|
|
|
dst_hmacmd5_isprivate(const dst_key_t *key);
|
|
|
|
|
|
|
|
static void
|
|
|
|
dst_hmacmd5_destroy(void *key, isc_mem_t *mctx);
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
dst_hmacmd5_to_dns(const dst_key_t *in_key, isc_buffer_t *data);
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
dst_hmacmd5_from_dns(dst_key_t *key, isc_buffer_t *data, isc_mem_t *mctx);
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
dst_hmacmd5_to_file(const dst_key_t *key);
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
dst_hmacmd5_from_file(dst_key_t *key, const isc_uint16_t id, isc_mem_t *mctx);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* dst_s_hmacmd5_init()
|
|
|
|
* Sets up function pointers for HMAC-MD5 related functions
|
|
|
|
*/
|
|
|
|
void
|
2000-05-13 19:31:35 +00:00
|
|
|
dst_s_hmacmd5_init(void) {
|
1999-09-02 15:56:33 +00:00
|
|
|
REQUIRE(dst_t_func[DST_ALG_HMACMD5] == NULL);
|
|
|
|
dst_t_func[DST_ALG_HMACMD5] = &hmacmd5_functions;
|
1999-07-12 20:08:42 +00:00
|
|
|
memset(&hmacmd5_functions, 0, sizeof(struct dst_func));
|
|
|
|
hmacmd5_functions.sign = dst_hmacmd5_sign;
|
|
|
|
hmacmd5_functions.verify = dst_hmacmd5_verify;
|
1999-09-27 16:55:45 +00:00
|
|
|
hmacmd5_functions.computesecret = NULL;
|
1999-07-12 20:08:42 +00:00
|
|
|
hmacmd5_functions.compare = dst_hmacmd5_compare;
|
1999-09-27 16:55:45 +00:00
|
|
|
hmacmd5_functions.paramcompare = NULL;
|
1999-07-12 20:08:42 +00:00
|
|
|
hmacmd5_functions.generate = dst_hmacmd5_generate;
|
1999-09-23 20:54:38 +00:00
|
|
|
hmacmd5_functions.isprivate = dst_hmacmd5_isprivate;
|
1999-07-12 20:08:42 +00:00
|
|
|
hmacmd5_functions.destroy = dst_hmacmd5_destroy;
|
|
|
|
hmacmd5_functions.to_dns = dst_hmacmd5_to_dns;
|
|
|
|
hmacmd5_functions.from_dns = dst_hmacmd5_from_dns;
|
|
|
|
hmacmd5_functions.to_file = dst_hmacmd5_to_file;
|
|
|
|
hmacmd5_functions.from_file = dst_hmacmd5_from_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dst_hmacmd5_sign
|
|
|
|
* Call HMAC-MD5 signing functions to sign a block of data.
|
|
|
|
* There are three steps to signing, INIT (initialize structures),
|
|
|
|
* UPDATE (hash (more) data), FINAL (generate a signature). This
|
|
|
|
* routine performs one or more of these steps.
|
|
|
|
* Parameters
|
1999-08-26 20:41:54 +00:00
|
|
|
* mode DST_SIGMODE_{INIT_UPDATE_FINAL|ALL}
|
1999-07-12 20:08:42 +00:00
|
|
|
* key key to use for signing
|
|
|
|
* context the context to use for this computation
|
|
|
|
* data data to be signed
|
|
|
|
* signature buffer to store signature
|
|
|
|
* mctx memory context for temporary allocations
|
|
|
|
* Returns
|
1999-10-20 22:14:15 +00:00
|
|
|
* ISC_R_SUCCESS Success
|
|
|
|
* !ISC_R_SUCCESS Failure
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
1999-08-31 14:59:08 +00:00
|
|
|
dst_hmacmd5_sign(const unsigned int mode, dst_key_t *key, void **context,
|
1999-07-12 20:08:42 +00:00
|
|
|
isc_region_t *data, isc_buffer_t *sig, isc_mem_t *mctx)
|
|
|
|
{
|
|
|
|
isc_region_t r;
|
1999-10-18 21:35:46 +00:00
|
|
|
isc_result_t ret;
|
1999-10-26 19:43:25 +00:00
|
|
|
void *ctx;
|
|
|
|
|
1999-10-27 16:52:55 +00:00
|
|
|
if ((mode & DST_SIGMODE_ALL) != DST_SIGMODE_ALL)
|
1999-10-26 19:43:25 +00:00
|
|
|
REQUIRE(context != NULL);
|
|
|
|
else
|
|
|
|
context = &ctx;
|
|
|
|
|
1999-08-26 20:41:54 +00:00
|
|
|
if (mode & DST_SIGMODE_INIT) {
|
1999-07-12 20:08:42 +00:00
|
|
|
HMAC_Key *hkey = key->opaque;
|
|
|
|
|
1999-10-18 21:35:46 +00:00
|
|
|
r.base = hkey->ipad;
|
|
|
|
r.length = HMAC_LEN;
|
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_INIT, context, NULL, NULL, mctx));
|
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_UPDATE, context, &r, NULL, mctx));
|
|
|
|
}
|
|
|
|
if (mode & DST_SIGMODE_UPDATE) {
|
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_UPDATE, context, data, NULL,
|
|
|
|
mctx));
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
1999-08-26 20:41:54 +00:00
|
|
|
if (mode & DST_SIGMODE_FINAL) {
|
1999-07-12 20:08:42 +00:00
|
|
|
HMAC_Key *hkey = key->opaque;
|
1999-10-18 21:35:46 +00:00
|
|
|
unsigned char digest[MD5_DIGEST_LENGTH];
|
|
|
|
isc_buffer_t b;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_init(&b, digest, sizeof(digest));
|
1999-07-12 20:08:42 +00:00
|
|
|
|
1999-10-18 21:35:46 +00:00
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_FINAL, context, NULL, &b, mctx));
|
1999-07-12 20:08:42 +00:00
|
|
|
|
1999-10-18 21:35:46 +00:00
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_INIT, context, NULL, NULL, mctx));
|
|
|
|
r.base = hkey->opad;
|
|
|
|
r.length = HMAC_LEN;
|
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_UPDATE, context, &r, NULL, mctx));
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_usedregion(&b, &r);
|
1999-10-18 21:35:46 +00:00
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_UPDATE, context, &r, NULL, mctx));
|
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_FINAL, context, NULL, sig, mctx));
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dst_hmacmd5_verify
|
|
|
|
* Calls HMAC-MD5 verification routines. There are three steps to
|
|
|
|
* verification, INIT (initialize structures), UPDATE (hash (more) data),
|
|
|
|
* FINAL (generate a signature). This routine performs one or more of
|
|
|
|
* these steps.
|
|
|
|
* Parameters
|
1999-08-26 20:41:54 +00:00
|
|
|
* mode DST_SIGMODE_{INIT_UPDATE_FINAL|ALL}
|
1999-07-12 20:08:42 +00:00
|
|
|
* key key to use for verifying
|
|
|
|
* context the context to use for this computation
|
|
|
|
* data signed data
|
|
|
|
* signature signature
|
|
|
|
* mctx memory context for temporary allocations
|
|
|
|
* Returns
|
1999-10-20 22:14:15 +00:00
|
|
|
* ISC_R_SUCCESS Success
|
|
|
|
* !ISC_R_SUCCESS Failure
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
1999-08-31 14:59:08 +00:00
|
|
|
dst_hmacmd5_verify(const unsigned int mode, dst_key_t *key, void **context,
|
1999-07-12 20:08:42 +00:00
|
|
|
isc_region_t *data, isc_region_t *sig, isc_mem_t *mctx)
|
|
|
|
{
|
1999-10-18 21:35:46 +00:00
|
|
|
isc_region_t r;
|
|
|
|
isc_result_t ret;
|
1999-10-26 19:43:25 +00:00
|
|
|
void *ctx;
|
|
|
|
|
1999-10-27 16:52:55 +00:00
|
|
|
if ((mode & DST_SIGMODE_ALL) != DST_SIGMODE_ALL)
|
1999-10-26 19:43:25 +00:00
|
|
|
REQUIRE(context != NULL);
|
|
|
|
else
|
|
|
|
context = &ctx;
|
|
|
|
|
1999-08-26 20:41:54 +00:00
|
|
|
if (mode & DST_SIGMODE_INIT) {
|
1999-07-12 20:08:42 +00:00
|
|
|
HMAC_Key *hkey = key->opaque;
|
|
|
|
|
1999-10-18 21:35:46 +00:00
|
|
|
r.base = hkey->ipad;
|
|
|
|
r.length = HMAC_LEN;
|
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_INIT, context, NULL, NULL, mctx));
|
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_UPDATE, context, &r, NULL, mctx));
|
|
|
|
}
|
|
|
|
if (mode & DST_SIGMODE_UPDATE) {
|
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_UPDATE, context, data, NULL,
|
|
|
|
mctx));
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
1999-08-26 20:41:54 +00:00
|
|
|
if (mode & DST_SIGMODE_FINAL) {
|
1999-07-12 20:08:42 +00:00
|
|
|
HMAC_Key *hkey = key->opaque;
|
1999-10-18 21:35:46 +00:00
|
|
|
unsigned char digest[MD5_DIGEST_LENGTH];
|
|
|
|
isc_buffer_t b;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_init(&b, digest, sizeof(digest));
|
1999-07-12 20:08:42 +00:00
|
|
|
|
1999-10-18 21:35:46 +00:00
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_FINAL, context, NULL, &b, mctx));
|
1999-07-12 20:08:42 +00:00
|
|
|
|
1999-10-18 21:35:46 +00:00
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_INIT, context, NULL, NULL, mctx));
|
|
|
|
r.base = hkey->opad;
|
|
|
|
r.length = HMAC_LEN;
|
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_UPDATE, context, &r, NULL, mctx));
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_usedregion(&b, &r);
|
1999-10-18 21:35:46 +00:00
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_UPDATE, context, &r, NULL, mctx));
|
|
|
|
isc_buffer_clear(&b);
|
|
|
|
RETERR(dst_s_md5(DST_SIGMODE_FINAL, context, NULL, &b, mctx));
|
1999-07-12 20:08:42 +00:00
|
|
|
|
1999-10-18 21:35:46 +00:00
|
|
|
if (memcmp(digest, sig->base, MD5_DIGEST_LENGTH) != 0)
|
1999-09-01 18:56:19 +00:00
|
|
|
return (DST_R_VERIFYFINALFAILURE);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
1999-09-23 20:54:38 +00:00
|
|
|
/*
|
|
|
|
* dst_hmacmd5_isprivate
|
|
|
|
* Is this a private key? Yes
|
|
|
|
* Parameters
|
|
|
|
* key DST KEY structure
|
|
|
|
* Returns
|
|
|
|
* ISC_TRUE
|
|
|
|
*/
|
1999-09-27 16:55:45 +00:00
|
|
|
static isc_boolean_t
|
1999-09-23 20:54:38 +00:00
|
|
|
dst_hmacmd5_isprivate(const dst_key_t *key) {
|
2000-05-02 03:54:17 +00:00
|
|
|
UNUSED(key);
|
1999-09-27 16:55:45 +00:00
|
|
|
|
1999-09-23 20:54:38 +00:00
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
/*
|
|
|
|
* dst_hmacmd5_to_dns
|
|
|
|
* Converts key from HMAC to DNS rdata (raw bytes)
|
|
|
|
* Parameters
|
|
|
|
* key DST KEY structure
|
|
|
|
* data output data
|
|
|
|
* Returns
|
1999-10-20 22:14:15 +00:00
|
|
|
* ISC_R_SUCCESS Success
|
|
|
|
* !ISC_R_SUCCESS Failure
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
1999-07-12 20:08:42 +00:00
|
|
|
dst_hmacmd5_to_dns(const dst_key_t *key, isc_buffer_t *data) {
|
|
|
|
HMAC_Key *hkey;
|
|
|
|
isc_region_t r;
|
|
|
|
unsigned int bytes, i;
|
|
|
|
|
|
|
|
REQUIRE(key->opaque != NULL);
|
|
|
|
|
|
|
|
hkey = (HMAC_Key *) key->opaque;
|
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_availableregion(data, &r);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
bytes = (key->key_size + 7) / 8;
|
|
|
|
if (r.length < bytes)
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
for (i = 0; i < bytes; i++)
|
|
|
|
*r.base++ = hkey->ipad[i] ^ HMAC_IPAD;
|
|
|
|
|
|
|
|
isc_buffer_add(data, bytes);
|
|
|
|
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dst_hmacmd5_from_dns
|
|
|
|
* Converts from a DNS KEY RR format to an HMAC-MD5 KEY.
|
|
|
|
* Parameters
|
|
|
|
* key Partially filled key structure
|
|
|
|
* data Buffer containing key in DNS format
|
|
|
|
* Return
|
1999-10-20 22:14:15 +00:00
|
|
|
* ISC_R_SUCCESS Success
|
|
|
|
* !ISC_R_SUCCESS Failure
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
1999-07-12 20:08:42 +00:00
|
|
|
dst_hmacmd5_from_dns(dst_key_t *key, isc_buffer_t *data, isc_mem_t *mctx) {
|
|
|
|
HMAC_Key *hkey;
|
|
|
|
isc_region_t r;
|
|
|
|
int i, keylen;
|
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_remainingregion(data, &r);
|
1999-07-12 20:08:42 +00:00
|
|
|
if (r.length == 0)
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
hkey = (HMAC_Key *) isc_mem_get(mctx, sizeof(HMAC_Key));
|
|
|
|
if (hkey == NULL)
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
memset(hkey->ipad, 0, sizeof(hkey->ipad));
|
1999-07-29 17:21:23 +00:00
|
|
|
memset(hkey->opad, 0, sizeof(hkey->opad));
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
if (r.length > HMAC_LEN) {
|
|
|
|
MD5_CTX ctx;
|
1999-10-18 21:35:46 +00:00
|
|
|
unsigned char digest[MD5_DIGEST_LENGTH];
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2000-04-20 01:13:21 +00:00
|
|
|
MD5_Init(&ctx);
|
|
|
|
MD5_Update(&ctx, r.base, r.length);
|
|
|
|
MD5_Final(digest, &ctx);
|
1999-10-18 21:35:46 +00:00
|
|
|
memcpy(hkey->ipad, digest, MD5_DIGEST_LENGTH);
|
|
|
|
memcpy(hkey->opad, digest, MD5_DIGEST_LENGTH);
|
|
|
|
keylen = MD5_DIGEST_LENGTH;
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
memcpy(hkey->ipad, r.base, r.length);
|
1999-07-29 17:21:23 +00:00
|
|
|
memcpy(hkey->opad, r.base, r.length);
|
1999-07-12 20:08:42 +00:00
|
|
|
keylen = r.length;
|
|
|
|
}
|
|
|
|
|
2000-05-02 03:54:17 +00:00
|
|
|
/*
|
|
|
|
* XOR key with ipad and opad values.
|
|
|
|
*/
|
1999-07-12 20:08:42 +00:00
|
|
|
for (i = 0; i < HMAC_LEN; i++) {
|
|
|
|
hkey->ipad[i] ^= HMAC_IPAD;
|
|
|
|
hkey->opad[i] ^= HMAC_OPAD;
|
|
|
|
}
|
2000-06-01 02:33:26 +00:00
|
|
|
key->key_id = dst_s_id_calc(r.base, r.length);
|
1999-07-12 20:08:42 +00:00
|
|
|
key->key_size = keylen * 8;
|
|
|
|
key->opaque = hkey;
|
|
|
|
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dst_hmacmd5_to_file
|
|
|
|
* Encodes an HMAC-MD5 Key into the portable file format.
|
|
|
|
* Parameters
|
|
|
|
* key DST KEY structure
|
|
|
|
* Returns
|
1999-10-20 22:14:15 +00:00
|
|
|
* ISC_R_SUCCESS Success
|
|
|
|
* !ISC_R_SUCCESS Failure
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
1999-07-12 20:08:42 +00:00
|
|
|
dst_hmacmd5_to_file(const dst_key_t *key) {
|
|
|
|
int i, cnt = 0;
|
|
|
|
HMAC_Key *hkey;
|
|
|
|
dst_private_t priv;
|
|
|
|
unsigned char keydata[HMAC_LEN];
|
2000-03-15 18:52:23 +00:00
|
|
|
int bytes = (key->key_size + 7) / 8;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
if (key->opaque == NULL)
|
1999-09-01 18:56:19 +00:00
|
|
|
return (DST_R_NULLKEY);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
hkey = (HMAC_Key *) key->opaque;
|
2000-03-15 18:52:23 +00:00
|
|
|
memset(keydata, 0, HMAC_LEN);
|
|
|
|
for (i = 0; i < bytes; i++)
|
1999-07-12 20:08:42 +00:00
|
|
|
keydata[i] = hkey->ipad[i] ^ HMAC_IPAD;
|
|
|
|
|
|
|
|
priv.elements[cnt].tag = TAG_HMACMD5_KEY;
|
2000-03-15 18:52:23 +00:00
|
|
|
priv.elements[cnt].length = bytes;
|
1999-07-12 20:08:42 +00:00
|
|
|
priv.elements[cnt++].data = keydata;
|
|
|
|
|
|
|
|
priv.nelements = cnt;
|
2000-05-15 21:02:39 +00:00
|
|
|
return (dst_s_write_private_key_file(key, &priv));
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dst_hmacmd5_from_file
|
|
|
|
* Converts contents of a private key file into a private HMAC-MD5 key.
|
|
|
|
* Parameters
|
|
|
|
* key Partially filled HMAC-MD5 KEY structure
|
|
|
|
* id The key id
|
|
|
|
* path The directory that the file will be read from
|
|
|
|
* Return
|
1999-10-20 22:14:15 +00:00
|
|
|
* ISC_R_SUCCESS Success
|
|
|
|
* !ISC_R_SUCCESS Failure
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
1999-10-08 13:08:57 +00:00
|
|
|
dst_hmacmd5_from_file(dst_key_t *key, const isc_uint16_t id, isc_mem_t *mctx) {
|
1999-07-12 20:08:42 +00:00
|
|
|
dst_private_t priv;
|
2000-03-06 20:06:01 +00:00
|
|
|
isc_result_t ret;
|
1999-07-12 20:08:42 +00:00
|
|
|
isc_buffer_t b;
|
|
|
|
|
|
|
|
/* read private key file */
|
2000-05-15 21:30:45 +00:00
|
|
|
ret = dst_s_parse_private_key_file(key, id, &priv, mctx);
|
1999-10-20 22:14:15 +00:00
|
|
|
if (ret != ISC_R_SUCCESS)
|
1999-07-12 20:08:42 +00:00
|
|
|
return (ret);
|
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_init(&b, priv.elements[0].data, priv.elements[0].length);
|
2000-06-01 02:33:26 +00:00
|
|
|
isc_buffer_add(&b, priv.elements[0].length);
|
1999-07-12 20:08:42 +00:00
|
|
|
ret = dst_hmacmd5_from_dns(key, &b, mctx);
|
|
|
|
dst_s_free_private_structure_fields(&priv, mctx);
|
|
|
|
memset(&priv, 0, sizeof(priv));
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dst_hmacmd5_destroy
|
|
|
|
* Frees all dynamically allocated structures in key.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
dst_hmacmd5_destroy(void *key, isc_mem_t *mctx) {
|
|
|
|
HMAC_Key *hkey = (HMAC_Key *) key;
|
|
|
|
memset(hkey, 0, sizeof(HMAC_Key));
|
|
|
|
isc_mem_put(mctx, hkey, sizeof(HMAC_Key));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dst_hmacmd5_generate
|
|
|
|
* Creates an HMAC-MD5 key. If the specified size is more than 512
|
|
|
|
* bits, a key of size 512 is generated.
|
|
|
|
* Parameters
|
|
|
|
* key DST Key structure
|
|
|
|
* unused algorithm specific data, unused for HMAC-MD5.
|
|
|
|
* mctx memory context to allocate key
|
|
|
|
* Return
|
1999-10-20 22:14:15 +00:00
|
|
|
* ISC_R_SUCCESS Success
|
|
|
|
* !ISC_R_SUCCESS Failure
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
2000-03-06 20:06:01 +00:00
|
|
|
static isc_result_t
|
1999-07-12 20:08:42 +00:00
|
|
|
dst_hmacmd5_generate(dst_key_t *key, int unused, isc_mem_t *mctx) {
|
|
|
|
isc_buffer_t b;
|
2000-03-06 20:06:01 +00:00
|
|
|
isc_result_t ret;
|
1999-07-12 20:08:42 +00:00
|
|
|
int bytes;
|
|
|
|
unsigned char data[HMAC_LEN];
|
|
|
|
|
2000-05-02 03:54:17 +00:00
|
|
|
UNUSED(unused);
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
bytes = (key->key_size + 7) / 8;
|
|
|
|
if (bytes > 64) {
|
|
|
|
bytes = 64;
|
|
|
|
key->key_size = 512;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(data, 0, HMAC_LEN);
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_init(&b, data, sizeof(data));
|
1999-10-20 19:08:57 +00:00
|
|
|
ret = dst_random_get(bytes, &b);
|
1999-10-20 22:14:15 +00:00
|
|
|
if (ret != ISC_R_SUCCESS)
|
1999-07-12 20:08:42 +00:00
|
|
|
return (ret);
|
|
|
|
|
|
|
|
ret = dst_hmacmd5_from_dns(key, &b, mctx);
|
|
|
|
memset(data, 0, HMAC_LEN);
|
|
|
|
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ret);
|
1999-07-12 20:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
* dst_hmacmd5_compare
|
|
|
|
* Compare two keys for equality.
|
|
|
|
* Return
|
|
|
|
* ISC_TRUE The keys are equal
|
|
|
|
* ISC_FALSE The keys are not equal
|
|
|
|
*/
|
|
|
|
static isc_boolean_t
|
|
|
|
dst_hmacmd5_compare(const dst_key_t *key1, const dst_key_t *key2) {
|
|
|
|
HMAC_Key *hkey1, *hkey2;
|
|
|
|
|
2000-05-02 03:54:17 +00:00
|
|
|
hkey1 = (HMAC_Key *)key1->opaque;
|
|
|
|
hkey2 = (HMAC_Key *)key2->opaque;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
|
|
|
if (hkey1 == NULL && hkey2 == NULL)
|
1999-10-20 22:14:15 +00:00
|
|
|
return (ISC_TRUE);
|
1999-07-12 20:08:42 +00:00
|
|
|
else if (hkey1 == NULL || hkey2 == NULL)
|
|
|
|
return (ISC_FALSE);
|
|
|
|
|
|
|
|
if (memcmp(hkey1->ipad, hkey2->ipad, HMAC_LEN) == 0)
|
|
|
|
return (ISC_TRUE);
|
|
|
|
else
|
|
|
|
return (ISC_FALSE);
|
|
|
|
}
|