Print this page
@@ -1,15 +1,14 @@
/*
- * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
-#pragma ident "%Z%%M% %I% %E% SMI"
#ifdef HMAC_MD5
#ifndef LINT
-static const char rcsid[] = "$Header: /proj/cvs/isc/bind8/src/lib/dst/hmac_link.c,v 1.9 2001/05/29 05:48:10 marka Exp $";
+static const char rcsid[] = "$Header: /proj/cvs/prod/libbind/dst/hmac_link.c,v 1.8 2007/09/24 17:18:25 each Exp $";
#endif
/*
* Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
*
* Permission to use, copy modify, and distribute this software for any
@@ -24,11 +23,11 @@
* 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 THE SOFTWARE.
*/
-/*
+/*%
* This file contains an implementation of the HMAC-MD5 algorithm.
*/
#include "port_before.h"
#include <stdio.h>
@@ -41,18 +40,21 @@
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include "dst_internal.h"
+
#ifdef USE_MD5
-#ifndef SUNW_LIBMD5
+# ifndef HAVE_MD5
# include "md5.h"
-#else
-#include <sys/md5.h>
-#endif
+# else
+# ifdef SOLARIS2
+# include <sys/md5.h>
+# endif
+# endif
# ifndef _MD5_H_
-# define _MD5_H_ 1 /* make sure we do not include rsaref md5.h file */
+# define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */
# endif
#endif
#include "port_after.h"
@@ -95,19 +97,20 @@
{
HMAC_Key *key;
int sign_len = 0;
MD5_CTX *ctx = NULL;
+ if (d_key == NULL || d_key->dk_KEY_struct == NULL)
+ return (-1);
+
if (mode & SIG_MODE_INIT)
ctx = (MD5_CTX *) malloc(sizeof(*ctx));
else if (context)
ctx = (MD5_CTX *) *context;
if (ctx == NULL)
return (-1);
- if (d_key == NULL || d_key->dk_KEY_struct == NULL)
- return (-1);
key = (HMAC_Key *) d_key->dk_KEY_struct;
if (mode & SIG_MODE_INIT) {
MD5Init(ctx);
MD5Update(ctx, key->hk_ipad, HMAC_LEN);
@@ -162,20 +165,20 @@
const u_char *signature, const int sig_len)
{
HMAC_Key *key;
MD5_CTX *ctx = NULL;
+ if (d_key == NULL || d_key->dk_KEY_struct == NULL)
+ return (-1);
+
if (mode & SIG_MODE_INIT)
ctx = (MD5_CTX *) malloc(sizeof(*ctx));
else if (context)
ctx = (MD5_CTX *) *context;
if (ctx == NULL)
return (-1);
- if (d_key == NULL || d_key->dk_KEY_struct == NULL)
- return (-1);
-
key = (HMAC_Key *) d_key->dk_KEY_struct;
if (mode & SIG_MODE_INIT) {
MD5Init(ctx);
MD5Update(ctx, key->hk_ipad, HMAC_LEN);
}
@@ -224,10 +227,11 @@
{
int i;
HMAC_Key *hkey = NULL;
MD5_CTX ctx;
int local_keylen = keylen;
+ u_char tk[MD5_LEN];
if (dkey == NULL || key == NULL || keylen < 0)
return (-1);
if ((hkey = (HMAC_Key *) malloc(sizeof(HMAC_Key))) == NULL)
@@ -236,11 +240,10 @@
memset(hkey->hk_ipad, 0, sizeof(hkey->hk_ipad));
memset(hkey->hk_opad, 0, sizeof(hkey->hk_opad));
/* if key is longer than HMAC_LEN bytes reset it to key=MD5(key) */
if (keylen > HMAC_LEN) {
- u_char tk[MD5_LEN];
MD5Init(&ctx);
MD5Update(&ctx, key, keylen);
MD5Final(tk, &ctx);
memset((void *) &ctx, 0, sizeof(ctx));
key = tk;
@@ -277,48 +280,53 @@
static int
dst_hmac_md5_key_to_file_format(const DST_KEY *dkey, char *buff,
const int buff_len)
{
char *bp;
- int len, b_len, i, key_len;
+ int len, i, key_len;
u_char key[HMAC_LEN];
HMAC_Key *hkey;
if (dkey == NULL || dkey->dk_KEY_struct == NULL)
return (0);
- if (buff == NULL || buff_len <= (int) strlen(key_file_fmt_str))
- return (-1); /* no OR not enough space in output area */
-
+ /*
+ * Using snprintf() would be so much simpler here.
+ */
+ if (buff == NULL ||
+ buff_len <= (int)(strlen(key_file_fmt_str) +
+ strlen(KEY_FILE_FORMAT) + 4))
+ return (-1); /*%< no OR not enough space in output area */
hkey = (HMAC_Key *) dkey->dk_KEY_struct;
- memset(buff, 0, buff_len); /* just in case */
+ memset(buff, 0, buff_len); /*%< just in case */
/* write file header */
sprintf(buff, key_file_fmt_str, KEY_FILE_FORMAT, KEY_HMAC_MD5, "HMAC");
- bp = (char *) strchr(buff, '\0');
- b_len = buff_len - (bp - buff);
+ bp = buff + strlen(buff);
memset(key, 0, HMAC_LEN);
for (i = 0; i < HMAC_LEN; i++)
key[i] = hkey->hk_ipad[i] ^ HMAC_IPAD;
for (i = HMAC_LEN - 1; i >= 0; i--)
if (key[i] != 0)
break;
key_len = i + 1;
+ if (buff_len - (bp - buff) < 6)
+ return (-1);
strcat(bp, "Key: ");
bp += strlen("Key: ");
- b_len = buff_len - (bp - buff);
- len = b64_ntop(key, key_len, bp, b_len);
+ len = b64_ntop(key, key_len, bp, buff_len - (bp - buff));
if (len < 0)
return (-1);
bp += len;
+ if (buff_len - (bp - buff) < 2)
+ return (-1);
*(bp++) = '\n';
*bp = '\0';
- b_len = buff_len - (bp - buff);
- return (buff_len - b_len);
+ return (bp - buff);
}
/**************************************************************************
* dst_hmac_md5_key_from_file_format
@@ -357,22 +365,24 @@
eol = strchr(p, '\n');
if (eol == NULL)
return (-4);
len = eol - p;
tmp = malloc(len + 2);
+ if (tmp == NULL)
+ return (-5);
memcpy(tmp, p, len);
*(tmp + len) = 0x0;
- key_len = b64_pton((char *)tmp, key, HMAC_LEN+1); /* see above */
+ key_len = b64_pton((char *)tmp, key, HMAC_LEN+1); /*%< see above */
SAFE_FREE2(tmp, len + 2);
if (dst_buffer_to_hmac_md5(dkey, key, key_len) < 0) {
return (-6);
}
return (0);
}
-/*
+/*%
* dst_hmac_md5_to_dns_key()
* function to extract hmac key from DST_KEY structure
* intput:
* in_key: HMAC-MD5 key
* output:
@@ -437,47 +447,21 @@
*/
static int
dst_hmac_md5_generate_key(DST_KEY *key, const int nothing)
{
- u_char *buff;
- int i, n, size;
-
- i = nothing;
-
- if (key == NULL || key->dk_alg != KEY_HMAC_MD5)
- return (0);
- size = (key->dk_key_size + 7) / 8; /* convert to bytes */
- if (size <= 0)
- return(0);
-
- i = size > 64 ? 64 : size;
- buff = malloc(i+8);
-
- n = dst_random(DST_RAND_SEMI, i, buff);
- n += dst_random(DST_RAND_KEY, i, buff);
- if (n <= i) { /* failed getting anything */
- SAFE_FREE2(buff, i);
+ (void)key;
+ (void)nothing;
return (-1);
- }
- n = dst_buffer_to_hmac_md5(key, buff, i);
- SAFE_FREE2(buff, i);
- if (n <= 0)
- return (n);
- return (1);
}
-/*
+/*%
* dst_hmac_md5_init() Function to answer set up function pointers for HMAC
* related functions
*/
int
-#ifdef ORIGINAL_ISC_CODE
dst_hmac_md5_init()
-#else
-dst_md5_hmac_init()
-#endif
{
if (dst_t_func[KEY_HMAC_MD5] != NULL)
return (1);
dst_t_func[KEY_HMAC_MD5] = malloc(sizeof(struct dst_func));
if (dst_t_func[KEY_HMAC_MD5] == NULL)
@@ -494,17 +478,14 @@
dst_t_func[KEY_HMAC_MD5]->from_file_fmt = dst_hmac_md5_key_from_file_format;
return (1);
}
#else
+#define dst_hmac_md5_init __dst_hmac_md5_init
+
int
dst_hmac_md5_init(){
return (0);
}
#endif
-
-
-
-
-
-
+/*! \file */