2 * Copyright (c) 2014 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
37 #include <netinet/in.h>
44 chap_compute_md5(const char id, const char *secret,
45 const void *challenge, size_t challenge_len, void *response,
50 assert(response_len == CHAP_DIGEST_LEN);
53 MD5Update(&ctx, &id, sizeof(id));
54 MD5Update(&ctx, secret, strlen(secret));
55 MD5Update(&ctx, challenge, challenge_len);
56 MD5Final(response, &ctx);
60 chap_hex2int(const char hex)
107 chap_b642bin(const char *b64, void **binp, size_t *bin_lenp)
110 int b64_len, bin_len;
112 b64_len = strlen(b64);
113 bin_len = (b64_len + 3) / 4 * 3;
114 bin = calloc(bin_len, 1);
116 log_err(1, "calloc");
118 bin_len = b64_pton(b64, bin, bin_len);
120 log_warnx("malformed base64 variable");
130 * XXX: Review this _carefully_.
133 chap_hex2bin(const char *hex, void **binp, size_t *bin_lenp)
135 int i, hex_len, nibble;
136 bool lo = true; /* As opposed to 'hi'. */
138 size_t bin_off, bin_len;
140 if (strncasecmp(hex, "0b", strlen("0b")) == 0)
141 return (chap_b642bin(hex + 2, binp, bin_lenp));
143 if (strncasecmp(hex, "0x", strlen("0x")) != 0) {
144 log_warnx("malformed variable, should start with \"0x\""
150 hex_len = strlen(hex);
152 log_warnx("malformed variable; doesn't contain anything "
157 bin_len = hex_len / 2 + hex_len % 2;
158 bin = calloc(bin_len, 1);
160 log_err(1, "calloc");
162 bin_off = bin_len - 1;
163 for (i = hex_len - 1; i >= 0; i--) {
164 nibble = chap_hex2int(hex[i]);
166 log_warnx("malformed variable, invalid char \"%c\"",
172 assert(bin_off < bin_len);
174 bin[bin_off] = nibble;
177 bin[bin_off] |= nibble << 4;
190 chap_bin2hex(const char *bin, size_t bin_len)
192 unsigned char *b64, *tmp;
195 b64_len = (bin_len + 2) / 3 * 4 + 3; /* +2 for "0b", +1 for '\0'. */
196 b64 = malloc(b64_len);
198 log_err(1, "malloc");
201 tmp += sprintf(tmp, "0b");
202 b64_ntop(bin, bin_len, tmp, b64_len - 2);
208 chap_bin2hex(const char *bin, size_t bin_len)
210 unsigned char *hex, *tmp, ch;
214 hex_len = bin_len * 2 + 3; /* +2 for "0x", +1 for '\0'. */
215 hex = malloc(hex_len);
217 log_err(1, "malloc");
220 tmp += sprintf(tmp, "0x");
221 for (i = 0; i < bin_len; i++) {
223 tmp += sprintf(tmp, "%02x", ch);
228 #endif /* !USE_BASE64 */
235 chap = calloc(sizeof(*chap), 1);
237 log_err(1, "calloc");
240 * Generate the challenge.
242 arc4random_buf(chap->chap_challenge, sizeof(chap->chap_challenge));
243 arc4random_buf(&chap->chap_id, sizeof(chap->chap_id));
249 chap_get_id(const struct chap *chap)
254 ret = asprintf(&chap_i, "%d", chap->chap_id);
256 log_err(1, "asprintf");
262 chap_get_challenge(const struct chap *chap)
266 chap_c = chap_bin2hex(chap->chap_challenge,
267 sizeof(chap->chap_challenge));
273 chap_receive_bin(struct chap *chap, void *response, size_t response_len)
276 if (response_len != sizeof(chap->chap_response)) {
277 log_debugx("got CHAP response with invalid length; "
278 "got %zd, should be %zd",
279 response_len, sizeof(chap->chap_response));
283 memcpy(chap->chap_response, response, response_len);
288 chap_receive(struct chap *chap, const char *response)
291 size_t response_bin_len;
294 error = chap_hex2bin(response, &response_bin, &response_bin_len);
296 log_debugx("got incorrectly encoded CHAP response \"%s\"",
301 error = chap_receive_bin(chap, response_bin, response_bin_len);
308 chap_authenticate(struct chap *chap, const char *secret)
310 char expected_response[CHAP_DIGEST_LEN];
312 chap_compute_md5(chap->chap_id, secret,
313 chap->chap_challenge, sizeof(chap->chap_challenge),
314 expected_response, sizeof(expected_response));
316 if (memcmp(chap->chap_response,
317 expected_response, sizeof(expected_response)) != 0) {
325 chap_delete(struct chap *chap)
332 rchap_new(const char *secret)
336 rchap = calloc(sizeof(*rchap), 1);
338 log_err(1, "calloc");
340 rchap->rchap_secret = checked_strdup(secret);
346 rchap_receive_bin(struct rchap *rchap, const unsigned char id,
347 const void *challenge, size_t challenge_len)
350 rchap->rchap_id = id;
351 rchap->rchap_challenge = calloc(challenge_len, 1);
352 if (rchap->rchap_challenge == NULL)
353 log_err(1, "calloc");
354 memcpy(rchap->rchap_challenge, challenge, challenge_len);
355 rchap->rchap_challenge_len = challenge_len;
359 rchap_receive(struct rchap *rchap, const char *id, const char *challenge)
361 unsigned char id_bin;
363 size_t challenge_bin_len;
367 id_bin = strtoul(id, NULL, 10);
369 error = chap_hex2bin(challenge, &challenge_bin, &challenge_bin_len);
371 log_debugx("got incorrectly encoded CHAP challenge \"%s\"",
376 rchap_receive_bin(rchap, id_bin, challenge_bin, challenge_bin_len);
383 rchap_get_response_bin(struct rchap *rchap,
384 void **responsep, size_t *response_lenp)
387 size_t response_bin_len = CHAP_DIGEST_LEN;
389 response_bin = calloc(response_bin_len, 1);
390 if (response_bin == NULL)
391 log_err(1, "calloc");
393 chap_compute_md5(rchap->rchap_id, rchap->rchap_secret,
394 rchap->rchap_challenge, rchap->rchap_challenge_len,
395 response_bin, response_bin_len);
397 *responsep = response_bin;
398 *response_lenp = response_bin_len;
402 rchap_get_response(struct rchap *rchap)
408 rchap_get_response_bin(rchap, &response, &response_len);
409 chap_r = chap_bin2hex(response, response_len);
416 rchap_delete(struct rchap *rchap)
419 free(rchap->rchap_secret);
420 free(rchap->rchap_challenge);