PageRenderTime 31ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/uClinux-dist/user/bind/lib/bind/dst/hmac_link.c

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