chap.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2014 The FreeBSD Foundation
  5. *
  6. * This software was developed by Edward Tomasz Napierala under sponsorship
  7. * from the FreeBSD Foundation.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <assert.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <netinet/in.h>
  34. #include <resolv.h>
  35. #include <md5.h>
  36. #include "libiscsiutil.h"
  37. static void
  38. chap_compute_md5(const char id, const char *secret,
  39. const void *challenge, size_t challenge_len, void *response,
  40. size_t response_len)
  41. {
  42. MD5_CTX ctx;
  43. assert(response_len == CHAP_DIGEST_LEN);
  44. MD5Init(&ctx);
  45. MD5Update(&ctx, &id, sizeof(id));
  46. MD5Update(&ctx, secret, strlen(secret));
  47. MD5Update(&ctx, challenge, challenge_len);
  48. MD5Final(response, &ctx);
  49. }
  50. static int
  51. chap_hex2int(const char hex)
  52. {
  53. switch (hex) {
  54. case '0':
  55. return (0x00);
  56. case '1':
  57. return (0x01);
  58. case '2':
  59. return (0x02);
  60. case '3':
  61. return (0x03);
  62. case '4':
  63. return (0x04);
  64. case '5':
  65. return (0x05);
  66. case '6':
  67. return (0x06);
  68. case '7':
  69. return (0x07);
  70. case '8':
  71. return (0x08);
  72. case '9':
  73. return (0x09);
  74. case 'a':
  75. case 'A':
  76. return (0x0a);
  77. case 'b':
  78. case 'B':
  79. return (0x0b);
  80. case 'c':
  81. case 'C':
  82. return (0x0c);
  83. case 'd':
  84. case 'D':
  85. return (0x0d);
  86. case 'e':
  87. case 'E':
  88. return (0x0e);
  89. case 'f':
  90. case 'F':
  91. return (0x0f);
  92. default:
  93. return (-1);
  94. }
  95. }
  96. static int
  97. chap_b642bin(const char *b64, void **binp, size_t *bin_lenp)
  98. {
  99. char *bin;
  100. int b64_len, bin_len;
  101. b64_len = strlen(b64);
  102. bin_len = (b64_len + 3) / 4 * 3;
  103. bin = calloc(bin_len, 1);
  104. if (bin == NULL)
  105. log_err(1, "calloc");
  106. bin_len = b64_pton(b64, bin, bin_len);
  107. if (bin_len < 0) {
  108. log_warnx("malformed base64 variable");
  109. free(bin);
  110. return (-1);
  111. }
  112. *binp = bin;
  113. *bin_lenp = bin_len;
  114. return (0);
  115. }
  116. /*
  117. * XXX: Review this _carefully_.
  118. */
  119. static int
  120. chap_hex2bin(const char *hex, void **binp, size_t *bin_lenp)
  121. {
  122. int i, hex_len, nibble;
  123. bool lo = true; /* As opposed to 'hi'. */
  124. char *bin;
  125. size_t bin_off, bin_len;
  126. if (strncasecmp(hex, "0b", strlen("0b")) == 0)
  127. return (chap_b642bin(hex + 2, binp, bin_lenp));
  128. if (strncasecmp(hex, "0x", strlen("0x")) != 0) {
  129. log_warnx("malformed variable, should start with \"0x\""
  130. " or \"0b\"");
  131. return (-1);
  132. }
  133. hex += strlen("0x");
  134. hex_len = strlen(hex);
  135. if (hex_len < 1) {
  136. log_warnx("malformed variable; doesn't contain anything "
  137. "but \"0x\"");
  138. return (-1);
  139. }
  140. bin_len = hex_len / 2 + hex_len % 2;
  141. bin = calloc(bin_len, 1);
  142. if (bin == NULL)
  143. log_err(1, "calloc");
  144. bin_off = bin_len - 1;
  145. for (i = hex_len - 1; i >= 0; i--) {
  146. nibble = chap_hex2int(hex[i]);
  147. if (nibble < 0) {
  148. log_warnx("malformed variable, invalid char \"%c\"",
  149. hex[i]);
  150. free(bin);
  151. return (-1);
  152. }
  153. assert(bin_off < bin_len);
  154. if (lo) {
  155. bin[bin_off] = nibble;
  156. lo = false;
  157. } else {
  158. bin[bin_off] |= nibble << 4;
  159. bin_off--;
  160. lo = true;
  161. }
  162. }
  163. *binp = bin;
  164. *bin_lenp = bin_len;
  165. return (0);
  166. }
  167. #ifdef USE_BASE64
  168. static char *
  169. chap_bin2hex(const char *bin, size_t bin_len)
  170. {
  171. unsigned char *b64, *tmp;
  172. size_t b64_len;
  173. b64_len = (bin_len + 2) / 3 * 4 + 3; /* +2 for "0b", +1 for '\0'. */
  174. b64 = malloc(b64_len);
  175. if (b64 == NULL)
  176. log_err(1, "malloc");
  177. tmp = b64;
  178. tmp += sprintf(tmp, "0b");
  179. b64_ntop(bin, bin_len, tmp, b64_len - 2);
  180. return (b64);
  181. }
  182. #else
  183. static char *
  184. chap_bin2hex(const char *bin, size_t bin_len)
  185. {
  186. unsigned char *hex, *tmp, ch;
  187. size_t hex_len;
  188. size_t i;
  189. hex_len = bin_len * 2 + 3; /* +2 for "0x", +1 for '\0'. */
  190. hex = malloc(hex_len);
  191. if (hex == NULL)
  192. log_err(1, "malloc");
  193. tmp = hex;
  194. tmp += sprintf(tmp, "0x");
  195. for (i = 0; i < bin_len; i++) {
  196. ch = bin[i];
  197. tmp += sprintf(tmp, "%02x", ch);
  198. }
  199. return (hex);
  200. }
  201. #endif /* !USE_BASE64 */
  202. struct chap *
  203. chap_new(void)
  204. {
  205. struct chap *chap;
  206. chap = calloc(1, sizeof(*chap));
  207. if (chap == NULL)
  208. log_err(1, "calloc");
  209. /*
  210. * Generate the challenge.
  211. */
  212. arc4random_buf(chap->chap_challenge, sizeof(chap->chap_challenge));
  213. arc4random_buf(&chap->chap_id, sizeof(chap->chap_id));
  214. return (chap);
  215. }
  216. char *
  217. chap_get_id(const struct chap *chap)
  218. {
  219. char *chap_i;
  220. int ret;
  221. ret = asprintf(&chap_i, "%d", chap->chap_id);
  222. if (ret < 0)
  223. log_err(1, "asprintf");
  224. return (chap_i);
  225. }
  226. char *
  227. chap_get_challenge(const struct chap *chap)
  228. {
  229. char *chap_c;
  230. chap_c = chap_bin2hex(chap->chap_challenge,
  231. sizeof(chap->chap_challenge));
  232. return (chap_c);
  233. }
  234. static int
  235. chap_receive_bin(struct chap *chap, void *response, size_t response_len)
  236. {
  237. if (response_len != sizeof(chap->chap_response)) {
  238. log_debugx("got CHAP response with invalid length; "
  239. "got %zd, should be %zd",
  240. response_len, sizeof(chap->chap_response));
  241. return (1);
  242. }
  243. memcpy(chap->chap_response, response, response_len);
  244. return (0);
  245. }
  246. int
  247. chap_receive(struct chap *chap, const char *response)
  248. {
  249. void *response_bin;
  250. size_t response_bin_len;
  251. int error;
  252. error = chap_hex2bin(response, &response_bin, &response_bin_len);
  253. if (error != 0) {
  254. log_debugx("got incorrectly encoded CHAP response \"%s\"",
  255. response);
  256. return (1);
  257. }
  258. error = chap_receive_bin(chap, response_bin, response_bin_len);
  259. free(response_bin);
  260. return (error);
  261. }
  262. int
  263. chap_authenticate(struct chap *chap, const char *secret)
  264. {
  265. char expected_response[CHAP_DIGEST_LEN];
  266. chap_compute_md5(chap->chap_id, secret,
  267. chap->chap_challenge, sizeof(chap->chap_challenge),
  268. expected_response, sizeof(expected_response));
  269. if (memcmp(chap->chap_response,
  270. expected_response, sizeof(expected_response)) != 0) {
  271. return (-1);
  272. }
  273. return (0);
  274. }
  275. void
  276. chap_delete(struct chap *chap)
  277. {
  278. free(chap);
  279. }
  280. struct rchap *
  281. rchap_new(const char *secret)
  282. {
  283. struct rchap *rchap;
  284. rchap = calloc(1, sizeof(*rchap));
  285. if (rchap == NULL)
  286. log_err(1, "calloc");
  287. rchap->rchap_secret = checked_strdup(secret);
  288. return (rchap);
  289. }
  290. static void
  291. rchap_receive_bin(struct rchap *rchap, const unsigned char id,
  292. const void *challenge, size_t challenge_len)
  293. {
  294. rchap->rchap_id = id;
  295. rchap->rchap_challenge = calloc(challenge_len, 1);
  296. if (rchap->rchap_challenge == NULL)
  297. log_err(1, "calloc");
  298. memcpy(rchap->rchap_challenge, challenge, challenge_len);
  299. rchap->rchap_challenge_len = challenge_len;
  300. }
  301. int
  302. rchap_receive(struct rchap *rchap, const char *id, const char *challenge)
  303. {
  304. unsigned char id_bin;
  305. void *challenge_bin;
  306. size_t challenge_bin_len;
  307. int error;
  308. id_bin = strtoul(id, NULL, 10);
  309. error = chap_hex2bin(challenge, &challenge_bin, &challenge_bin_len);
  310. if (error != 0) {
  311. log_debugx("got incorrectly encoded CHAP challenge \"%s\"",
  312. challenge);
  313. return (1);
  314. }
  315. rchap_receive_bin(rchap, id_bin, challenge_bin, challenge_bin_len);
  316. free(challenge_bin);
  317. return (0);
  318. }
  319. static void
  320. rchap_get_response_bin(struct rchap *rchap,
  321. void **responsep, size_t *response_lenp)
  322. {
  323. void *response_bin;
  324. size_t response_bin_len = CHAP_DIGEST_LEN;
  325. response_bin = calloc(response_bin_len, 1);
  326. if (response_bin == NULL)
  327. log_err(1, "calloc");
  328. chap_compute_md5(rchap->rchap_id, rchap->rchap_secret,
  329. rchap->rchap_challenge, rchap->rchap_challenge_len,
  330. response_bin, response_bin_len);
  331. *responsep = response_bin;
  332. *response_lenp = response_bin_len;
  333. }
  334. char *
  335. rchap_get_response(struct rchap *rchap)
  336. {
  337. void *response;
  338. size_t response_len;
  339. char *chap_r;
  340. rchap_get_response_bin(rchap, &response, &response_len);
  341. chap_r = chap_bin2hex(response, response_len);
  342. free(response);
  343. return (chap_r);
  344. }
  345. void
  346. rchap_delete(struct rchap *rchap)
  347. {
  348. free(rchap->rchap_secret);
  349. free(rchap->rchap_challenge);
  350. free(rchap);
  351. }