17c478bd9Sstevel@tonic-gate /*
29525b14bSRao Shoaib * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate
77c478bd9Sstevel@tonic-gate #ifdef HMAC_MD5
87c478bd9Sstevel@tonic-gate /*
97c478bd9Sstevel@tonic-gate * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
107c478bd9Sstevel@tonic-gate *
117c478bd9Sstevel@tonic-gate * Permission to use, copy modify, and distribute this software for any
127c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
137c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
147c478bd9Sstevel@tonic-gate *
157c478bd9Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
167c478bd9Sstevel@tonic-gate * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
177c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
187c478bd9Sstevel@tonic-gate * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
197c478bd9Sstevel@tonic-gate * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
207c478bd9Sstevel@tonic-gate * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
217c478bd9Sstevel@tonic-gate * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
227c478bd9Sstevel@tonic-gate * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
237c478bd9Sstevel@tonic-gate */
247c478bd9Sstevel@tonic-gate
259525b14bSRao Shoaib /*%
267c478bd9Sstevel@tonic-gate * This file contains an implementation of the HMAC-MD5 algorithm.
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate #include "port_before.h"
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <memory.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/time.h>
377c478bd9Sstevel@tonic-gate #include <netinet/in.h>
387c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
397c478bd9Sstevel@tonic-gate #include <resolv.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include "dst_internal.h"
429525b14bSRao Shoaib
437c478bd9Sstevel@tonic-gate #ifdef USE_MD5
449525b14bSRao Shoaib # ifndef HAVE_MD5
459525b14bSRao Shoaib # include "md5.h"
469525b14bSRao Shoaib # else
479525b14bSRao Shoaib # ifdef SOLARIS2
489525b14bSRao Shoaib # include <sys/md5.h>
499525b14bSRao Shoaib # endif
509525b14bSRao Shoaib # endif
517c478bd9Sstevel@tonic-gate # ifndef _MD5_H_
529525b14bSRao Shoaib # define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */
537c478bd9Sstevel@tonic-gate # endif
547c478bd9Sstevel@tonic-gate #endif
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate #include "port_after.h"
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate #define HMAC_LEN 64
607c478bd9Sstevel@tonic-gate #define HMAC_IPAD 0x36
617c478bd9Sstevel@tonic-gate #define HMAC_OPAD 0x5c
627c478bd9Sstevel@tonic-gate #define MD5_LEN 16
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate typedef struct hmackey {
667c478bd9Sstevel@tonic-gate u_char hk_ipad[64], hk_opad[64];
677c478bd9Sstevel@tonic-gate } HMAC_Key;
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate
70*55fea89dSDan Cross /**************************************************************************
717c478bd9Sstevel@tonic-gate * dst_hmac_md5_sign
727c478bd9Sstevel@tonic-gate * Call HMAC signing functions to sign a block of data.
73*55fea89dSDan Cross * There are three steps to signing, INIT (initialize structures),
747c478bd9Sstevel@tonic-gate * UPDATE (hash (more) data), FINAL (generate a signature). This
757c478bd9Sstevel@tonic-gate * routine performs one or more of these steps.
767c478bd9Sstevel@tonic-gate * Parameters
777c478bd9Sstevel@tonic-gate * mode SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
787c478bd9Sstevel@tonic-gate * priv_key key to use for signing.
797c478bd9Sstevel@tonic-gate * context the context to be used in this digest
807c478bd9Sstevel@tonic-gate * data data to be signed.
817c478bd9Sstevel@tonic-gate * len length in bytes of data.
827c478bd9Sstevel@tonic-gate * signature location to store signature.
837c478bd9Sstevel@tonic-gate * sig_len size of the signature location
84*55fea89dSDan Cross * returns
857c478bd9Sstevel@tonic-gate * N Success on SIG_MODE_FINAL = returns signature length in bytes
867c478bd9Sstevel@tonic-gate * 0 Success on SIG_MODE_INIT and UPDATE
877c478bd9Sstevel@tonic-gate * <0 Failure
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate static int
dst_hmac_md5_sign(const int mode,DST_KEY * d_key,void ** context,const u_char * data,const int len,u_char * signature,const int sig_len)91*55fea89dSDan Cross dst_hmac_md5_sign(const int mode, DST_KEY *d_key, void **context,
92*55fea89dSDan Cross const u_char *data, const int len,
937c478bd9Sstevel@tonic-gate u_char *signature, const int sig_len)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate HMAC_Key *key;
967c478bd9Sstevel@tonic-gate int sign_len = 0;
977c478bd9Sstevel@tonic-gate MD5_CTX *ctx = NULL;
987c478bd9Sstevel@tonic-gate
999525b14bSRao Shoaib if (d_key == NULL || d_key->dk_KEY_struct == NULL)
1009525b14bSRao Shoaib return (-1);
1019525b14bSRao Shoaib
102*55fea89dSDan Cross if (mode & SIG_MODE_INIT)
1037c478bd9Sstevel@tonic-gate ctx = (MD5_CTX *) malloc(sizeof(*ctx));
1047c478bd9Sstevel@tonic-gate else if (context)
1057c478bd9Sstevel@tonic-gate ctx = (MD5_CTX *) *context;
106*55fea89dSDan Cross if (ctx == NULL)
1077c478bd9Sstevel@tonic-gate return (-1);
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate key = (HMAC_Key *) d_key->dk_KEY_struct;
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate if (mode & SIG_MODE_INIT) {
1127c478bd9Sstevel@tonic-gate MD5Init(ctx);
1137c478bd9Sstevel@tonic-gate MD5Update(ctx, key->hk_ipad, HMAC_LEN);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate if ((mode & SIG_MODE_UPDATE) && (data && len > 0))
1177c478bd9Sstevel@tonic-gate MD5Update(ctx, data, len);
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate if (mode & SIG_MODE_FINAL) {
1207c478bd9Sstevel@tonic-gate if (signature == NULL || sig_len < MD5_LEN)
1217c478bd9Sstevel@tonic-gate return (SIGN_FINAL_FAILURE);
1227c478bd9Sstevel@tonic-gate MD5Final(signature, ctx);
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate /* perform outer MD5 */
1257c478bd9Sstevel@tonic-gate MD5Init(ctx);
1267c478bd9Sstevel@tonic-gate MD5Update(ctx, key->hk_opad, HMAC_LEN);
1277c478bd9Sstevel@tonic-gate MD5Update(ctx, signature, MD5_LEN);
1287c478bd9Sstevel@tonic-gate MD5Final(signature, ctx);
1297c478bd9Sstevel@tonic-gate sign_len = MD5_LEN;
1307c478bd9Sstevel@tonic-gate SAFE_FREE(ctx);
1317c478bd9Sstevel@tonic-gate }
132*55fea89dSDan Cross else {
133*55fea89dSDan Cross if (context == NULL)
1347c478bd9Sstevel@tonic-gate return (-1);
1357c478bd9Sstevel@tonic-gate *context = (void *) ctx;
136*55fea89dSDan Cross }
1377c478bd9Sstevel@tonic-gate return (sign_len);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate
141*55fea89dSDan Cross /**************************************************************************
142*55fea89dSDan Cross * dst_hmac_md5_verify()
143*55fea89dSDan Cross * Calls HMAC verification routines. There are three steps to
144*55fea89dSDan Cross * verification, INIT (initialize structures), UPDATE (hash (more) data),
145*55fea89dSDan Cross * FINAL (generate a signature). This routine performs one or more of
1467c478bd9Sstevel@tonic-gate * these steps.
1477c478bd9Sstevel@tonic-gate * Parameters
1487c478bd9Sstevel@tonic-gate * mode SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
1497c478bd9Sstevel@tonic-gate * dkey key to use for verify.
1507c478bd9Sstevel@tonic-gate * data data signed.
1517c478bd9Sstevel@tonic-gate * len length in bytes of data.
1527c478bd9Sstevel@tonic-gate * signature signature.
1537c478bd9Sstevel@tonic-gate * sig_len length in bytes of signature.
154*55fea89dSDan Cross * returns
155*55fea89dSDan Cross * 0 Success
1567c478bd9Sstevel@tonic-gate * <0 Failure
1577c478bd9Sstevel@tonic-gate */
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate static int
dst_hmac_md5_verify(const int mode,DST_KEY * d_key,void ** context,const u_char * data,const int len,const u_char * signature,const int sig_len)1607c478bd9Sstevel@tonic-gate dst_hmac_md5_verify(const int mode, DST_KEY *d_key, void **context,
1617c478bd9Sstevel@tonic-gate const u_char *data, const int len,
1627c478bd9Sstevel@tonic-gate const u_char *signature, const int sig_len)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate HMAC_Key *key;
1657c478bd9Sstevel@tonic-gate MD5_CTX *ctx = NULL;
1667c478bd9Sstevel@tonic-gate
1679525b14bSRao Shoaib if (d_key == NULL || d_key->dk_KEY_struct == NULL)
1689525b14bSRao Shoaib return (-1);
1699525b14bSRao Shoaib
170*55fea89dSDan Cross if (mode & SIG_MODE_INIT)
1717c478bd9Sstevel@tonic-gate ctx = (MD5_CTX *) malloc(sizeof(*ctx));
1727c478bd9Sstevel@tonic-gate else if (context)
1737c478bd9Sstevel@tonic-gate ctx = (MD5_CTX *) *context;
174*55fea89dSDan Cross if (ctx == NULL)
1757c478bd9Sstevel@tonic-gate return (-1);
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate key = (HMAC_Key *) d_key->dk_KEY_struct;
1787c478bd9Sstevel@tonic-gate if (mode & SIG_MODE_INIT) {
1797c478bd9Sstevel@tonic-gate MD5Init(ctx);
1807c478bd9Sstevel@tonic-gate MD5Update(ctx, key->hk_ipad, HMAC_LEN);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate if ((mode & SIG_MODE_UPDATE) && (data && len > 0))
1837c478bd9Sstevel@tonic-gate MD5Update(ctx, data, len);
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate if (mode & SIG_MODE_FINAL) {
1867c478bd9Sstevel@tonic-gate u_char digest[MD5_LEN];
1877c478bd9Sstevel@tonic-gate if (signature == NULL || key == NULL || sig_len != MD5_LEN)
1887c478bd9Sstevel@tonic-gate return (VERIFY_FINAL_FAILURE);
1897c478bd9Sstevel@tonic-gate MD5Final(digest, ctx);
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate /* perform outer MD5 */
1927c478bd9Sstevel@tonic-gate MD5Init(ctx);
1937c478bd9Sstevel@tonic-gate MD5Update(ctx, key->hk_opad, HMAC_LEN);
1947c478bd9Sstevel@tonic-gate MD5Update(ctx, digest, MD5_LEN);
1957c478bd9Sstevel@tonic-gate MD5Final(digest, ctx);
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate SAFE_FREE(ctx);
1987c478bd9Sstevel@tonic-gate if (memcmp(digest, signature, MD5_LEN) != 0)
1997c478bd9Sstevel@tonic-gate return (VERIFY_FINAL_FAILURE);
2007c478bd9Sstevel@tonic-gate }
201*55fea89dSDan Cross else {
202*55fea89dSDan Cross if (context == NULL)
2037c478bd9Sstevel@tonic-gate return (-1);
2047c478bd9Sstevel@tonic-gate *context = (void *) ctx;
205*55fea89dSDan Cross }
2067c478bd9Sstevel@tonic-gate return (0);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate
210*55fea89dSDan Cross /**************************************************************************
2117c478bd9Sstevel@tonic-gate * dst_buffer_to_hmac_md5
2127c478bd9Sstevel@tonic-gate * Converts key from raw data to an HMAC Key
2137c478bd9Sstevel@tonic-gate * This function gets in a pointer to the data
2147c478bd9Sstevel@tonic-gate * Parameters
2157c478bd9Sstevel@tonic-gate * hkey the HMAC key to be filled in
2167c478bd9Sstevel@tonic-gate * key the key in raw format
2177c478bd9Sstevel@tonic-gate * keylen the length of the key
2187c478bd9Sstevel@tonic-gate * Return
2197c478bd9Sstevel@tonic-gate * 0 Success
2207c478bd9Sstevel@tonic-gate * <0 Failure
2217c478bd9Sstevel@tonic-gate */
2227c478bd9Sstevel@tonic-gate static int
dst_buffer_to_hmac_md5(DST_KEY * dkey,const u_char * key,const int keylen)2237c478bd9Sstevel@tonic-gate dst_buffer_to_hmac_md5(DST_KEY *dkey, const u_char *key, const int keylen)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate int i;
2267c478bd9Sstevel@tonic-gate HMAC_Key *hkey = NULL;
2277c478bd9Sstevel@tonic-gate MD5_CTX ctx;
2287c478bd9Sstevel@tonic-gate int local_keylen = keylen;
2299525b14bSRao Shoaib u_char tk[MD5_LEN];
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate if (dkey == NULL || key == NULL || keylen < 0)
2327c478bd9Sstevel@tonic-gate return (-1);
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate if ((hkey = (HMAC_Key *) malloc(sizeof(HMAC_Key))) == NULL)
2357c478bd9Sstevel@tonic-gate return (-2);
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate memset(hkey->hk_ipad, 0, sizeof(hkey->hk_ipad));
2387c478bd9Sstevel@tonic-gate memset(hkey->hk_opad, 0, sizeof(hkey->hk_opad));
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate /* if key is longer than HMAC_LEN bytes reset it to key=MD5(key) */
2417c478bd9Sstevel@tonic-gate if (keylen > HMAC_LEN) {
2427c478bd9Sstevel@tonic-gate MD5Init(&ctx);
2437c478bd9Sstevel@tonic-gate MD5Update(&ctx, key, keylen);
2447c478bd9Sstevel@tonic-gate MD5Final(tk, &ctx);
2457c478bd9Sstevel@tonic-gate memset((void *) &ctx, 0, sizeof(ctx));
2467c478bd9Sstevel@tonic-gate key = tk;
2477c478bd9Sstevel@tonic-gate local_keylen = MD5_LEN;
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate /* start out by storing key in pads */
2507c478bd9Sstevel@tonic-gate memcpy(hkey->hk_ipad, key, local_keylen);
2517c478bd9Sstevel@tonic-gate memcpy(hkey->hk_opad, key, local_keylen);
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate /* XOR key with hk_ipad and opad values */
2547c478bd9Sstevel@tonic-gate for (i = 0; i < HMAC_LEN; i++) {
2557c478bd9Sstevel@tonic-gate hkey->hk_ipad[i] ^= HMAC_IPAD;
2567c478bd9Sstevel@tonic-gate hkey->hk_opad[i] ^= HMAC_OPAD;
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate dkey->dk_key_size = local_keylen;
2597c478bd9Sstevel@tonic-gate dkey->dk_KEY_struct = (void *) hkey;
2607c478bd9Sstevel@tonic-gate return (1);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate
264*55fea89dSDan Cross /**************************************************************************
2657c478bd9Sstevel@tonic-gate * dst_hmac_md5_key_to_file_format
2667c478bd9Sstevel@tonic-gate * Encodes an HMAC Key into the portable file format.
267*55fea89dSDan Cross * Parameters
268*55fea89dSDan Cross * hkey HMAC KEY structure
2697c478bd9Sstevel@tonic-gate * buff output buffer
270*55fea89dSDan Cross * buff_len size of output buffer
2717c478bd9Sstevel@tonic-gate * Return
2727c478bd9Sstevel@tonic-gate * 0 Failure - null input hkey
2737c478bd9Sstevel@tonic-gate * -1 Failure - not enough space in output area
2747c478bd9Sstevel@tonic-gate * N Success - Length of data returned in buff
2757c478bd9Sstevel@tonic-gate */
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate static int
dst_hmac_md5_key_to_file_format(const DST_KEY * dkey,char * buff,const int buff_len)2787c478bd9Sstevel@tonic-gate dst_hmac_md5_key_to_file_format(const DST_KEY *dkey, char *buff,
2799525b14bSRao Shoaib const int buff_len)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate char *bp;
2829525b14bSRao Shoaib int len, i, key_len;
2837c478bd9Sstevel@tonic-gate u_char key[HMAC_LEN];
2847c478bd9Sstevel@tonic-gate HMAC_Key *hkey;
2857c478bd9Sstevel@tonic-gate
286*55fea89dSDan Cross if (dkey == NULL || dkey->dk_KEY_struct == NULL)
2877c478bd9Sstevel@tonic-gate return (0);
2889525b14bSRao Shoaib /*
2899525b14bSRao Shoaib * Using snprintf() would be so much simpler here.
2909525b14bSRao Shoaib */
2919525b14bSRao Shoaib if (buff == NULL ||
2929525b14bSRao Shoaib buff_len <= (int)(strlen(key_file_fmt_str) +
2939525b14bSRao Shoaib strlen(KEY_FILE_FORMAT) + 4))
2949525b14bSRao Shoaib return (-1); /*%< no OR not enough space in output area */
2957c478bd9Sstevel@tonic-gate hkey = (HMAC_Key *) dkey->dk_KEY_struct;
2969525b14bSRao Shoaib memset(buff, 0, buff_len); /*%< just in case */
2977c478bd9Sstevel@tonic-gate /* write file header */
2987c478bd9Sstevel@tonic-gate sprintf(buff, key_file_fmt_str, KEY_FILE_FORMAT, KEY_HMAC_MD5, "HMAC");
2997c478bd9Sstevel@tonic-gate
3009525b14bSRao Shoaib bp = buff + strlen(buff);
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate memset(key, 0, HMAC_LEN);
3037c478bd9Sstevel@tonic-gate for (i = 0; i < HMAC_LEN; i++)
3047c478bd9Sstevel@tonic-gate key[i] = hkey->hk_ipad[i] ^ HMAC_IPAD;
3057c478bd9Sstevel@tonic-gate for (i = HMAC_LEN - 1; i >= 0; i--)
3067c478bd9Sstevel@tonic-gate if (key[i] != 0)
3077c478bd9Sstevel@tonic-gate break;
3087c478bd9Sstevel@tonic-gate key_len = i + 1;
3097c478bd9Sstevel@tonic-gate
3109525b14bSRao Shoaib if (buff_len - (bp - buff) < 6)
3119525b14bSRao Shoaib return (-1);
3127c478bd9Sstevel@tonic-gate strcat(bp, "Key: ");
3137c478bd9Sstevel@tonic-gate bp += strlen("Key: ");
3147c478bd9Sstevel@tonic-gate
3159525b14bSRao Shoaib len = b64_ntop(key, key_len, bp, buff_len - (bp - buff));
316*55fea89dSDan Cross if (len < 0)
3177c478bd9Sstevel@tonic-gate return (-1);
3187c478bd9Sstevel@tonic-gate bp += len;
3199525b14bSRao Shoaib if (buff_len - (bp - buff) < 2)
3209525b14bSRao Shoaib return (-1);
3217c478bd9Sstevel@tonic-gate *(bp++) = '\n';
3227c478bd9Sstevel@tonic-gate *bp = '\0';
3237c478bd9Sstevel@tonic-gate
3249525b14bSRao Shoaib return (bp - buff);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate
328*55fea89dSDan Cross /**************************************************************************
3297c478bd9Sstevel@tonic-gate * dst_hmac_md5_key_from_file_format
330*55fea89dSDan Cross * Converts contents of a key file into an HMAC key.
331*55fea89dSDan Cross * Parameters
332*55fea89dSDan Cross * hkey structure to put key into
333*55fea89dSDan Cross * buff buffer containing the encoded key
3347c478bd9Sstevel@tonic-gate * buff_len the length of the buffer
3357c478bd9Sstevel@tonic-gate * Return
336*55fea89dSDan Cross * n >= 0 Foot print of the key converted
337*55fea89dSDan Cross * n < 0 Error in conversion
3387c478bd9Sstevel@tonic-gate */
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate static int
dst_hmac_md5_key_from_file_format(DST_KEY * dkey,const char * buff,const int buff_len)3417c478bd9Sstevel@tonic-gate dst_hmac_md5_key_from_file_format(DST_KEY *dkey, const char *buff,
3427c478bd9Sstevel@tonic-gate const int buff_len)
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate const char *p = buff, *eol;
3457c478bd9Sstevel@tonic-gate u_char key[HMAC_LEN+1]; /* b64_pton needs more than 64 bytes do decode
3469525b14bSRao Shoaib * it should probably be fixed rather than doing
3479525b14bSRao Shoaib * this
3489525b14bSRao Shoaib */
3497c478bd9Sstevel@tonic-gate u_char *tmp;
3507c478bd9Sstevel@tonic-gate int key_len, len;
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate if (dkey == NULL)
3537c478bd9Sstevel@tonic-gate return (-2);
3547c478bd9Sstevel@tonic-gate if (buff == NULL || buff_len < 0)
3557c478bd9Sstevel@tonic-gate return (-1);
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate memset(key, 0, sizeof(key));
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate if (!dst_s_verify_str(&p, "Key: "))
3607c478bd9Sstevel@tonic-gate return (-3);
3617c478bd9Sstevel@tonic-gate
3627c478bd9Sstevel@tonic-gate eol = strchr(p, '\n');
3637c478bd9Sstevel@tonic-gate if (eol == NULL)
3647c478bd9Sstevel@tonic-gate return (-4);
3657c478bd9Sstevel@tonic-gate len = eol - p;
3667c478bd9Sstevel@tonic-gate tmp = malloc(len + 2);
3679525b14bSRao Shoaib if (tmp == NULL)
3689525b14bSRao Shoaib return (-5);
3697c478bd9Sstevel@tonic-gate memcpy(tmp, p, len);
3707c478bd9Sstevel@tonic-gate *(tmp + len) = 0x0;
3719525b14bSRao Shoaib key_len = b64_pton((char *)tmp, key, HMAC_LEN+1); /*%< see above */
3727c478bd9Sstevel@tonic-gate SAFE_FREE2(tmp, len + 2);
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate if (dst_buffer_to_hmac_md5(dkey, key, key_len) < 0) {
3757c478bd9Sstevel@tonic-gate return (-6);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate return (0);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate
3809525b14bSRao Shoaib /*%
381*55fea89dSDan Cross * dst_hmac_md5_to_dns_key()
382*55fea89dSDan Cross * function to extract hmac key from DST_KEY structure
383*55fea89dSDan Cross * intput:
384*55fea89dSDan Cross * in_key: HMAC-MD5 key
385*55fea89dSDan Cross * output:
3867c478bd9Sstevel@tonic-gate * out_str: buffer to write ot
387*55fea89dSDan Cross * out_len: size of output buffer
3887c478bd9Sstevel@tonic-gate * returns:
389*55fea89dSDan Cross * number of bytes written to output buffer
3907c478bd9Sstevel@tonic-gate */
3917c478bd9Sstevel@tonic-gate static int
dst_hmac_md5_to_dns_key(const DST_KEY * in_key,u_char * out_str,const int out_len)3927c478bd9Sstevel@tonic-gate dst_hmac_md5_to_dns_key(const DST_KEY *in_key, u_char *out_str,
3937c478bd9Sstevel@tonic-gate const int out_len)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate HMAC_Key *hkey;
3977c478bd9Sstevel@tonic-gate int i;
398*55fea89dSDan Cross
3997c478bd9Sstevel@tonic-gate if (in_key == NULL || in_key->dk_KEY_struct == NULL ||
4007c478bd9Sstevel@tonic-gate out_len <= in_key->dk_key_size || out_str == NULL)
4017c478bd9Sstevel@tonic-gate return (-1);
4027c478bd9Sstevel@tonic-gate
4037c478bd9Sstevel@tonic-gate hkey = (HMAC_Key *) in_key->dk_KEY_struct;
4047c478bd9Sstevel@tonic-gate for (i = 0; i < in_key->dk_key_size; i++)
4057c478bd9Sstevel@tonic-gate out_str[i] = hkey->hk_ipad[i] ^ HMAC_IPAD;
4067c478bd9Sstevel@tonic-gate return (i);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate
409*55fea89dSDan Cross /**************************************************************************
4107c478bd9Sstevel@tonic-gate * dst_hmac_md5_compare_keys
4117c478bd9Sstevel@tonic-gate * Compare two keys for equality.
4127c478bd9Sstevel@tonic-gate * Return
4137c478bd9Sstevel@tonic-gate * 0 The keys are equal
4147c478bd9Sstevel@tonic-gate * NON-ZERO The keys are not equal
4157c478bd9Sstevel@tonic-gate */
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate static int
dst_hmac_md5_compare_keys(const DST_KEY * key1,const DST_KEY * key2)4187c478bd9Sstevel@tonic-gate dst_hmac_md5_compare_keys(const DST_KEY *key1, const DST_KEY *key2)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate HMAC_Key *hkey1 = (HMAC_Key *) key1->dk_KEY_struct;
4217c478bd9Sstevel@tonic-gate HMAC_Key *hkey2 = (HMAC_Key *) key2->dk_KEY_struct;
4227c478bd9Sstevel@tonic-gate return memcmp(hkey1->hk_ipad, hkey2->hk_ipad, HMAC_LEN);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate
425*55fea89dSDan Cross /**************************************************************************
4267c478bd9Sstevel@tonic-gate * dst_hmac_md5_free_key_structure
4277c478bd9Sstevel@tonic-gate * Frees all (none) dynamically allocated structures in hkey
4287c478bd9Sstevel@tonic-gate */
4297c478bd9Sstevel@tonic-gate
4307c478bd9Sstevel@tonic-gate static void *
dst_hmac_md5_free_key_structure(void * key)4317c478bd9Sstevel@tonic-gate dst_hmac_md5_free_key_structure(void *key)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate HMAC_Key *hkey = key;
4347c478bd9Sstevel@tonic-gate SAFE_FREE(hkey);
4357c478bd9Sstevel@tonic-gate return (NULL);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate
439*55fea89dSDan Cross /***************************************************************************
4407c478bd9Sstevel@tonic-gate * dst_hmac_md5_generate_key
4417c478bd9Sstevel@tonic-gate * Creates a HMAC key of size size with a maximum size of 63 bytes
442*55fea89dSDan Cross * generating a HMAC key larger than 63 bytes makes no sense as that key
443*55fea89dSDan Cross * is digested before use.
4447c478bd9Sstevel@tonic-gate */
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate static int
dst_hmac_md5_generate_key(DST_KEY * key,const int nothing)4477c478bd9Sstevel@tonic-gate dst_hmac_md5_generate_key(DST_KEY *key, const int nothing)
4487c478bd9Sstevel@tonic-gate {
4499525b14bSRao Shoaib (void)key;
4509525b14bSRao Shoaib (void)nothing;
4519525b14bSRao Shoaib return (-1);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate
4549525b14bSRao Shoaib /*%
4557c478bd9Sstevel@tonic-gate * dst_hmac_md5_init() Function to answer set up function pointers for HMAC
456*55fea89dSDan Cross * related functions
4577c478bd9Sstevel@tonic-gate */
4587c478bd9Sstevel@tonic-gate int
dst_hmac_md5_init()4597c478bd9Sstevel@tonic-gate dst_hmac_md5_init()
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate if (dst_t_func[KEY_HMAC_MD5] != NULL)
4627c478bd9Sstevel@tonic-gate return (1);
4637c478bd9Sstevel@tonic-gate dst_t_func[KEY_HMAC_MD5] = malloc(sizeof(struct dst_func));
4647c478bd9Sstevel@tonic-gate if (dst_t_func[KEY_HMAC_MD5] == NULL)
4657c478bd9Sstevel@tonic-gate return (0);
4667c478bd9Sstevel@tonic-gate memset(dst_t_func[KEY_HMAC_MD5], 0, sizeof(struct dst_func));
4677c478bd9Sstevel@tonic-gate dst_t_func[KEY_HMAC_MD5]->sign = dst_hmac_md5_sign;
4687c478bd9Sstevel@tonic-gate dst_t_func[KEY_HMAC_MD5]->verify = dst_hmac_md5_verify;
4697c478bd9Sstevel@tonic-gate dst_t_func[KEY_HMAC_MD5]->compare = dst_hmac_md5_compare_keys;
4707c478bd9Sstevel@tonic-gate dst_t_func[KEY_HMAC_MD5]->generate = dst_hmac_md5_generate_key;
4717c478bd9Sstevel@tonic-gate dst_t_func[KEY_HMAC_MD5]->destroy = dst_hmac_md5_free_key_structure;
4727c478bd9Sstevel@tonic-gate dst_t_func[KEY_HMAC_MD5]->to_dns_key = dst_hmac_md5_to_dns_key;
4737c478bd9Sstevel@tonic-gate dst_t_func[KEY_HMAC_MD5]->from_dns_key = dst_buffer_to_hmac_md5;
4747c478bd9Sstevel@tonic-gate dst_t_func[KEY_HMAC_MD5]->to_file_fmt = dst_hmac_md5_key_to_file_format;
4757c478bd9Sstevel@tonic-gate dst_t_func[KEY_HMAC_MD5]->from_file_fmt = dst_hmac_md5_key_from_file_format;
4767c478bd9Sstevel@tonic-gate return (1);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate
479*55fea89dSDan Cross #else
4809525b14bSRao Shoaib #define dst_hmac_md5_init __dst_hmac_md5_init
4819525b14bSRao Shoaib
4827c478bd9Sstevel@tonic-gate int
dst_hmac_md5_init()4837c478bd9Sstevel@tonic-gate dst_hmac_md5_init(){
4847c478bd9Sstevel@tonic-gate return (0);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate #endif
4877c478bd9Sstevel@tonic-gate
4889525b14bSRao Shoaib /*! \file */
489