2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
38 #include <netinet/in.h>
45 chap_compute_md5(const char id, const char *secret,
46 const void *challenge, size_t challenge_len, void *response,
51 assert(response_len == CHAP_DIGEST_LEN);
54 MD5Update(&ctx, &id, sizeof(id));
55 MD5Update(&ctx, secret, strlen(secret));
56 MD5Update(&ctx, challenge, challenge_len);
57 MD5Final(response, &ctx);
61 chap_hex2int(const char hex)
108 chap_b642bin(const char *b64, void **binp, size_t *bin_lenp)
111 int b64_len, bin_len;
113 b64_len = strlen(b64);
114 bin_len = (b64_len + 3) / 4 * 3;
115 bin = calloc(bin_len, 1);
117 log_err(1, "calloc");
119 bin_len = b64_pton(b64, bin, bin_len);
121 log_warnx("malformed base64 variable");
131 * XXX: Review this _carefully_.
134 chap_hex2bin(const char *hex, void **binp, size_t *bin_lenp)
136 int i, hex_len, nibble;
137 bool lo = true; /* As opposed to 'hi'. */
139 size_t bin_off, bin_len;
141 if (strncasecmp(hex, "0b", strlen("0b")) == 0)
142 return (chap_b642bin(hex + 2, binp, bin_lenp));
144 if (strncasecmp(hex, "0x", strlen("0x")) != 0) {
145 log_warnx("malformed variable, should start with \"0x\""
151 hex_len = strlen(hex);
153 log_warnx("malformed variable; doesn't contain anything "
158 bin_len = hex_len / 2 + hex_len % 2;
159 bin = calloc(bin_len, 1);
161 log_err(1, "calloc");
163 bin_off = bin_len - 1;
164 for (i = hex_len - 1; i >= 0; i--) {
165 nibble = chap_hex2int(hex[i]);
167 log_warnx("malformed variable, invalid char \"%c\"",
173 assert(bin_off < bin_len);
175 bin[bin_off] = nibble;
178 bin[bin_off] |= nibble << 4;
191 chap_bin2hex(const char *bin, size_t bin_len)
193 unsigned char *b64, *tmp;
196 b64_len = (bin_len + 2) / 3 * 4 + 3; /* +2 for "0b", +1 for '\0'. */
197 b64 = malloc(b64_len);
199 log_err(1, "malloc");
202 tmp += sprintf(tmp, "0b");
203 b64_ntop(bin, bin_len, tmp, b64_len - 2);
209 chap_bin2hex(const char *bin, size_t bin_len)
211 unsigned char *hex, *tmp, ch;
215 hex_len = bin_len * 2 + 3; /* +2 for "0x", +1 for '\0'. */
216 hex = malloc(hex_len);
218 log_err(1, "malloc");
221 tmp += sprintf(tmp, "0x");
222 for (i = 0; i < bin_len; i++) {
224 tmp += sprintf(tmp, "%02x", ch);
229 #endif /* !USE_BASE64 */
236 chap = calloc(1, sizeof(*chap));
238 log_err(1, "calloc");
241 * Generate the challenge.
243 arc4random_buf(chap->chap_challenge, sizeof(chap->chap_challenge));
244 arc4random_buf(&chap->chap_id, sizeof(chap->chap_id));
250 chap_get_id(const struct chap *chap)
255 ret = asprintf(&chap_i, "%d", chap->chap_id);
257 log_err(1, "asprintf");
263 chap_get_challenge(const struct chap *chap)
267 chap_c = chap_bin2hex(chap->chap_challenge,
268 sizeof(chap->chap_challenge));
274 chap_receive_bin(struct chap *chap, void *response, size_t response_len)
277 if (response_len != sizeof(chap->chap_response)) {
278 log_debugx("got CHAP response with invalid length; "
279 "got %zd, should be %zd",
280 response_len, sizeof(chap->chap_response));
284 memcpy(chap->chap_response, response, response_len);
289 chap_receive(struct chap *chap, const char *response)
292 size_t response_bin_len;
295 error = chap_hex2bin(response, &response_bin, &response_bin_len);
297 log_debugx("got incorrectly encoded CHAP response \"%s\"",
302 error = chap_receive_bin(chap, response_bin, response_bin_len);
309 chap_authenticate(struct chap *chap, const char *secret)
311 char expected_response[CHAP_DIGEST_LEN];
313 chap_compute_md5(chap->chap_id, secret,
314 chap->chap_challenge, sizeof(chap->chap_challenge),
315 expected_response, sizeof(expected_response));
317 if (memcmp(chap->chap_response,
318 expected_response, sizeof(expected_response)) != 0) {
326 chap_delete(struct chap *chap)
333 rchap_new(const char *secret)
337 rchap = calloc(1, sizeof(*rchap));
339 log_err(1, "calloc");
341 rchap->rchap_secret = checked_strdup(secret);
347 rchap_receive_bin(struct rchap *rchap, const unsigned char id,
348 const void *challenge, size_t challenge_len)
351 rchap->rchap_id = id;
352 rchap->rchap_challenge = calloc(challenge_len, 1);
353 if (rchap->rchap_challenge == NULL)
354 log_err(1, "calloc");
355 memcpy(rchap->rchap_challenge, challenge, challenge_len);
356 rchap->rchap_challenge_len = challenge_len;
360 rchap_receive(struct rchap *rchap, const char *id, const char *challenge)
362 unsigned char id_bin;
364 size_t challenge_bin_len;
368 id_bin = strtoul(id, NULL, 10);
370 error = chap_hex2bin(challenge, &challenge_bin, &challenge_bin_len);
372 log_debugx("got incorrectly encoded CHAP challenge \"%s\"",
377 rchap_receive_bin(rchap, id_bin, challenge_bin, challenge_bin_len);
384 rchap_get_response_bin(struct rchap *rchap,
385 void **responsep, size_t *response_lenp)
388 size_t response_bin_len = CHAP_DIGEST_LEN;
390 response_bin = calloc(response_bin_len, 1);
391 if (response_bin == NULL)
392 log_err(1, "calloc");
394 chap_compute_md5(rchap->rchap_id, rchap->rchap_secret,
395 rchap->rchap_challenge, rchap->rchap_challenge_len,
396 response_bin, response_bin_len);
398 *responsep = response_bin;
399 *response_lenp = response_bin_len;
403 rchap_get_response(struct rchap *rchap)
409 rchap_get_response_bin(rchap, &response, &response_len);
410 chap_r = chap_bin2hex(response, response_len);
417 rchap_delete(struct rchap *rchap)
420 free(rchap->rchap_secret);
421 free(rchap->rchap_challenge);