1 /*        $NetBSD: hmac_link.c,v 1.1.1.2 2012/09/09 16:07:47 christos Exp $     */
2 
3 #ifdef HMAC_MD5
4 #ifndef LINT
5 static const char rcsid[] = "Header: /proj/cvs/prod/libbind/dst/hmac_link.c,v 1.8 2007/09/24 17:18:25 each Exp ";
6 #endif
7 /*
8  * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
9  *
10  * Permission to use, copy modify, and distribute this software for any
11  * purpose with or without fee is hereby granted, provided that the above
12  * copyright notice and this permission notice appear in all copies.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
15  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL
17  * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
18  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
19  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
21  * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
22  */
23 
24 /*%
25  * This file contains an implementation of the HMAC-MD5 algorithm.
26  */
27 #include "port_before.h"
28 
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <memory.h>
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #include <netinet/in.h>
37 #include <arpa/nameser.h>
38 #include <resolv.h>
39 
40 #include "dst_internal.h"
41 
42 #ifdef USE_MD5
43 # ifndef HAVE_MD5
44 #  include "md5.h"
45 # else
46 #  ifdef SOLARIS2
47 #   include <sys/md5.h>
48 #  endif
49 # endif
50 # ifndef _MD5_H_
51 #  define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */
52 # endif
53 #endif
54 
55 #include "port_after.h"
56 
57 
58 #define HMAC_LEN    64
59 #define HMAC_IPAD   0x36
60 #define HMAC_OPAD   0x5c
61 #define MD5_LEN               16
62 
63 
64 typedef struct hmackey {
65           u_char hk_ipad[64], hk_opad[64];
66 } HMAC_Key;
67 
68 
69 /**************************************************************************
70  * dst_hmac_md5_sign
71  *     Call HMAC signing functions to sign a block of data.
72  *     There are three steps to signing, INIT (initialize structures),
73  *     UPDATE (hash (more) data), FINAL (generate a signature).  This
74  *     routine performs one or more of these steps.
75  * Parameters
76  *     mode         SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
77  *     priv_key    key to use for signing.
78  *     context   the context to be used in this digest
79  *     data         data to be signed.
80  *     len           length in bytes of data.
81  *     signature   location to store signature.
82  *     sig_len     size of the signature location
83  * returns
84  *        N  Success on SIG_MODE_FINAL = returns signature length in bytes
85  *        0  Success on SIG_MODE_INIT  and UPDATE
86  *         <0  Failure
87  */
88 
89 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)90 dst_hmac_md5_sign(const int mode, DST_KEY *d_key, void **context,
91                       const u_char *data, const int len,
92                       u_char *signature, const int sig_len)
93 {
94           HMAC_Key *key;
95           int sign_len = 0;
96           MD5_CTX *ctx = NULL;
97 
98           if (d_key == NULL || d_key->dk_KEY_struct == NULL)
99                     return (-1);
100 
101           if (mode & SIG_MODE_INIT)
102                     ctx = (MD5_CTX *) malloc(sizeof(*ctx));
103           else if (context)
104                     ctx = (MD5_CTX *) *context;
105           if (ctx == NULL)
106                     return (-1);
107 
108           key = (HMAC_Key *) d_key->dk_KEY_struct;
109 
110           if (mode & SIG_MODE_INIT) {
111                     MD5Init(ctx);
112                     MD5Update(ctx, key->hk_ipad, HMAC_LEN);
113           }
114 
115           if ((mode & SIG_MODE_UPDATE) && (data && len > 0))
116                     MD5Update(ctx, data, len);
117 
118           if (mode & SIG_MODE_FINAL) {
119                     if (signature == NULL || sig_len < MD5_LEN)
120                               return (SIGN_FINAL_FAILURE);
121                     MD5Final(signature, ctx);
122 
123                     /* perform outer MD5 */
124                     MD5Init(ctx);
125                     MD5Update(ctx, key->hk_opad, HMAC_LEN);
126                     MD5Update(ctx, signature, MD5_LEN);
127                     MD5Final(signature, ctx);
128                     sign_len = MD5_LEN;
129                     SAFE_FREE(ctx);
130           }
131           else {
132                     if (context == NULL)
133                               return (-1);
134                     *context = (void *) ctx;
135           }
136           return (sign_len);
137 }
138 
139 
140 /**************************************************************************
141  * dst_hmac_md5_verify()
142  *     Calls HMAC verification routines.  There are three steps to
143  *     verification, INIT (initialize structures), UPDATE (hash (more) data),
144  *     FINAL (generate a signature).  This routine performs one or more of
145  *     these steps.
146  * Parameters
147  *     mode         SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
148  *     dkey         key to use for verify.
149  *     data         data signed.
150  *     len           length in bytes of data.
151  *     signature   signature.
152  *     sig_len     length in bytes of signature.
153  * returns
154  *     0  Success
155  *    <0  Failure
156  */
157 
158 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)159 dst_hmac_md5_verify(const int mode, DST_KEY *d_key, void **context,
160                     const u_char *data, const int len,
161                     const u_char *signature, const int sig_len)
162 {
163           HMAC_Key *key;
164           MD5_CTX *ctx = NULL;
165 
166           if (d_key == NULL || d_key->dk_KEY_struct == NULL)
167                     return (-1);
168 
169           if (mode & SIG_MODE_INIT)
170                     ctx = (MD5_CTX *) malloc(sizeof(*ctx));
171           else if (context)
172                     ctx = (MD5_CTX *) *context;
173           if (ctx == NULL)
174                     return (-1);
175 
176           key = (HMAC_Key *) d_key->dk_KEY_struct;
177           if (mode & SIG_MODE_INIT) {
178                     MD5Init(ctx);
179                     MD5Update(ctx, key->hk_ipad, HMAC_LEN);
180           }
181           if ((mode & SIG_MODE_UPDATE) && (data && len > 0))
182                     MD5Update(ctx, data, len);
183 
184           if (mode & SIG_MODE_FINAL) {
185                     u_char digest[MD5_LEN];
186                     if (signature == NULL || key == NULL || sig_len != MD5_LEN)
187                               return (VERIFY_FINAL_FAILURE);
188                     MD5Final(digest, ctx);
189 
190                     /* perform outer MD5 */
191                     MD5Init(ctx);
192                     MD5Update(ctx, key->hk_opad, HMAC_LEN);
193                     MD5Update(ctx, digest, MD5_LEN);
194                     MD5Final(digest, ctx);
195 
196                     SAFE_FREE(ctx);
197                     if (memcmp(digest, signature, MD5_LEN) != 0)
198                               return (VERIFY_FINAL_FAILURE);
199           }
200           else {
201                     if (context == NULL)
202                               return (-1);
203                     *context = (void *) ctx;
204           }
205           return (0);
206 }
207 
208 
209 /**************************************************************************
210  * dst_buffer_to_hmac_md5
211  *     Converts key from raw data to an HMAC Key
212  *     This function gets in a pointer to the data
213  * Parameters
214  *     hkey         the HMAC key to be filled in
215  *     key          the key in raw format
216  *     keylen       the length of the key
217  * Return
218  *        0         Success
219  *        <0        Failure
220  */
221 static int
dst_buffer_to_hmac_md5(DST_KEY * dkey,const u_char * key,const int keylen)222 dst_buffer_to_hmac_md5(DST_KEY *dkey, const u_char *key, const int keylen)
223 {
224           int i;
225           HMAC_Key *hkey = NULL;
226           MD5_CTX ctx;
227           int local_keylen = keylen;
228           u_char tk[MD5_LEN];
229 
230           if (dkey == NULL || key == NULL || keylen < 0)
231                     return (-1);
232 
233           if ((hkey = (HMAC_Key *) malloc(sizeof(HMAC_Key))) == NULL)
234                       return (-2);
235 
236           memset(hkey->hk_ipad, 0, sizeof(hkey->hk_ipad));
237           memset(hkey->hk_opad, 0, sizeof(hkey->hk_opad));
238 
239           /* if key is longer than HMAC_LEN bytes reset it to key=MD5(key) */
240           if (keylen > HMAC_LEN) {
241                     MD5Init(&ctx);
242                     MD5Update(&ctx, key, keylen);
243                     MD5Final(tk, &ctx);
244                     memset((void *) &ctx, 0, sizeof(ctx));
245                     key = tk;
246                     local_keylen = MD5_LEN;
247           }
248           /* start out by storing key in pads */
249           memcpy(hkey->hk_ipad, key, local_keylen);
250           memcpy(hkey->hk_opad, key, local_keylen);
251 
252           /* XOR key with hk_ipad and opad values */
253           for (i = 0; i < HMAC_LEN; i++) {
254                     hkey->hk_ipad[i] ^= HMAC_IPAD;
255                     hkey->hk_opad[i] ^= HMAC_OPAD;
256           }
257           dkey->dk_key_size = local_keylen;
258           dkey->dk_KEY_struct = (void *) hkey;
259           return (1);
260 }
261 
262 
263 /**************************************************************************
264  *  dst_hmac_md5_key_to_file_format
265  *        Encodes an HMAC Key into the portable file format.
266  *  Parameters
267  *        hkey      HMAC KEY structure
268  *        buff      output buffer
269  *        buff_len  size of output buffer
270  *  Return
271  *        0  Failure - null input hkey
272  *     -1  Failure - not enough space in output area
273  *        N  Success - Length of data returned in buff
274  */
275 
276 static int
dst_hmac_md5_key_to_file_format(const DST_KEY * dkey,char * buff,const int buff_len)277 dst_hmac_md5_key_to_file_format(const DST_KEY *dkey, char *buff,
278                                       const int buff_len)
279 {
280           char *bp;
281           int len, i, key_len;
282           u_char key[HMAC_LEN];
283           HMAC_Key *hkey;
284 
285           if (dkey == NULL || dkey->dk_KEY_struct == NULL)
286                     return (0);
287           /*
288            * Using snprintf() would be so much simpler here.
289            */
290           if (buff == NULL ||
291               buff_len <= (int)(strlen(key_file_fmt_str) +
292                                     strlen(KEY_FILE_FORMAT) + 4))
293                     return (-1);        /*%< no OR not enough space in output area */
294           hkey = (HMAC_Key *) dkey->dk_KEY_struct;
295           memset(buff, 0, buff_len);    /*%< just in case */
296           /* write file header */
297           sprintf(buff, key_file_fmt_str, KEY_FILE_FORMAT, KEY_HMAC_MD5, "HMAC");
298 
299           bp = buff + strlen(buff);
300 
301           memset(key, 0, HMAC_LEN);
302           for (i = 0; i < HMAC_LEN; i++)
303                     key[i] = hkey->hk_ipad[i] ^ HMAC_IPAD;
304           for (i = HMAC_LEN - 1; i >= 0; i--)
305                     if (key[i] != 0)
306                               break;
307           key_len = i + 1;
308 
309           if (buff_len - (bp - buff) < 6)
310                     return (-1);
311           strcat(bp, "Key: ");
312           bp += strlen("Key: ");
313 
314           len = b64_ntop(key, key_len, bp, buff_len - (bp - buff));
315           if (len < 0)
316                     return (-1);
317           bp += len;
318           if (buff_len - (bp - buff) < 2)
319                     return (-1);
320           *(bp++) = '\n';
321           *bp = '\0';
322 
323           return (bp - buff);
324 }
325 
326 
327 /**************************************************************************
328  * dst_hmac_md5_key_from_file_format
329  *     Converts contents of a key file into an HMAC key.
330  * Parameters
331  *     hkey    structure to put key into
332  *     buff       buffer containing the encoded key
333  *     buff_len   the length of the buffer
334  * Return
335  *     n >= 0 Foot print of the key converted
336  *     n <  0 Error in conversion
337  */
338 
339 static int
dst_hmac_md5_key_from_file_format(DST_KEY * dkey,const char * buff,const int buff_len)340 dst_hmac_md5_key_from_file_format(DST_KEY *dkey, const char *buff,
341                                     const int buff_len)
342 {
343           const char *p = buff, *eol;
344           u_char key[HMAC_LEN+1];       /* b64_pton needs more than 64 bytes do decode
345                                          * it should probably be fixed rather than doing
346                                          * this
347                                          */
348           u_char *tmp;
349           int key_len, len;
350 
351           if (dkey == NULL)
352                     return (-2);
353           if (buff == NULL || buff_len < 0)
354                     return (-1);
355 
356           memset(key, 0, sizeof(key));
357 
358           if (!dst_s_verify_str(&p, "Key: "))
359                     return (-3);
360 
361           eol = strchr(p, '\n');
362           if (eol == NULL)
363                     return (-4);
364           len = eol - p;
365           tmp = malloc(len + 2);
366           if (tmp == NULL)
367                     return (-5);
368           memcpy(tmp, p, len);
369           *(tmp + len) = 0x0;
370           key_len = b64_pton((char *)tmp, key, HMAC_LEN+1); /*%< see above */
371           SAFE_FREE2(tmp, len + 2);
372 
373           if (dst_buffer_to_hmac_md5(dkey, key, key_len) < 0) {
374                     return (-6);
375           }
376           return (0);
377 }
378 
379 /*%
380  * dst_hmac_md5_to_dns_key()
381  *         function to extract hmac key from DST_KEY structure
382  * intput:
383  *      in_key:  HMAC-MD5 key
384  * output:
385  *        out_str: buffer to write ot
386  *      out_len: size of output buffer
387  * returns:
388  *      number of bytes written to output buffer
389  */
390 static int
dst_hmac_md5_to_dns_key(const DST_KEY * in_key,u_char * out_str,const int out_len)391 dst_hmac_md5_to_dns_key(const DST_KEY *in_key, u_char *out_str,
392                               const int out_len)
393 {
394 
395           HMAC_Key *hkey;
396           int i;
397 
398           if (in_key == NULL || in_key->dk_KEY_struct == NULL ||
399               out_len <= in_key->dk_key_size || out_str == NULL)
400                     return (-1);
401 
402           hkey = (HMAC_Key *) in_key->dk_KEY_struct;
403           for (i = 0; i < in_key->dk_key_size; i++)
404                     out_str[i] = hkey->hk_ipad[i] ^ HMAC_IPAD;
405           return (i);
406 }
407 
408 /**************************************************************************
409  *  dst_hmac_md5_compare_keys
410  *        Compare two keys for equality.
411  *  Return
412  *        0           The keys are equal
413  *        NON-ZERO   The keys are not equal
414  */
415 
416 static int
dst_hmac_md5_compare_keys(const DST_KEY * key1,const DST_KEY * key2)417 dst_hmac_md5_compare_keys(const DST_KEY *key1, const DST_KEY *key2)
418 {
419           HMAC_Key *hkey1 = (HMAC_Key *) key1->dk_KEY_struct;
420           HMAC_Key *hkey2 = (HMAC_Key *) key2->dk_KEY_struct;
421           return memcmp(hkey1->hk_ipad, hkey2->hk_ipad, HMAC_LEN);
422 }
423 
424 /**************************************************************************
425  * dst_hmac_md5_free_key_structure
426  *     Frees all (none) dynamically allocated structures in hkey
427  */
428 
429 static void *
dst_hmac_md5_free_key_structure(void * key)430 dst_hmac_md5_free_key_structure(void *key)
431 {
432           HMAC_Key *hkey = key;
433           SAFE_FREE(hkey);
434           return (NULL);
435 }
436 
437 
438 /***************************************************************************
439  * dst_hmac_md5_generate_key
440  *     Creates a HMAC key of size size with a maximum size of 63 bytes
441  *     generating a HMAC key larger than 63 bytes makes no sense as that key
442  *     is digested before use.
443  */
444 
445 static int
dst_hmac_md5_generate_key(DST_KEY * key,const int nothing)446 dst_hmac_md5_generate_key(DST_KEY *key, const int nothing)
447 {
448           (void)key;
449           (void)nothing;
450           return (-1);
451 }
452 
453 /*%
454  * dst_hmac_md5_init()  Function to answer set up function pointers for HMAC
455  *           related functions
456  */
457 int
458 #ifdef    SUNW_LIBMD5
dst_md5_hmac_init()459 dst_md5_hmac_init()
460 #else
461 dst_hmac_md5_init()
462 #endif
463 {
464           if (dst_t_func[KEY_HMAC_MD5] != NULL)
465                     return (1);
466           dst_t_func[KEY_HMAC_MD5] = malloc(sizeof(struct dst_func));
467           if (dst_t_func[KEY_HMAC_MD5] == NULL)
468                     return (0);
469           memset(dst_t_func[KEY_HMAC_MD5], 0, sizeof(struct dst_func));
470           dst_t_func[KEY_HMAC_MD5]->sign = dst_hmac_md5_sign;
471           dst_t_func[KEY_HMAC_MD5]->verify = dst_hmac_md5_verify;
472           dst_t_func[KEY_HMAC_MD5]->compare = dst_hmac_md5_compare_keys;
473           dst_t_func[KEY_HMAC_MD5]->generate = dst_hmac_md5_generate_key;
474           dst_t_func[KEY_HMAC_MD5]->destroy = dst_hmac_md5_free_key_structure;
475           dst_t_func[KEY_HMAC_MD5]->to_dns_key = dst_hmac_md5_to_dns_key;
476           dst_t_func[KEY_HMAC_MD5]->from_dns_key = dst_buffer_to_hmac_md5;
477           dst_t_func[KEY_HMAC_MD5]->to_file_fmt = dst_hmac_md5_key_to_file_format;
478           dst_t_func[KEY_HMAC_MD5]->from_file_fmt = dst_hmac_md5_key_from_file_format;
479           return (1);
480 }
481 
482 #else
483 #define   dst_hmac_md5_init   __dst_hmac_md5_init
484 
485 int
dst_hmac_md5_init()486 dst_hmac_md5_init(){
487           return (0);
488 }
489 #endif
490 
491 /*! \file */
492