/src/freeradius-server/src/lib/util/chap.c
Line | Count | Source |
1 | | /* |
2 | | * This program is free software; you can redistribute it and/or modify |
3 | | * it under the terms of the GNU General Public License, version 2 of the |
4 | | * License as published by the Free Software Foundation. |
5 | | * |
6 | | * This program is distributed in the hope that it will be useful, |
7 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9 | | * GNU General Public License for more details. |
10 | | * |
11 | | * You should have received a copy of the GNU General Public License |
12 | | * along with this program; if not, write to the Free Software |
13 | | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
14 | | */ |
15 | | |
16 | | /** Functions for parsing raw network packets |
17 | | * |
18 | | * @file src/lib/util/chap.c |
19 | | * |
20 | | * @author Alan DeKok (aland@networkradius.com) |
21 | | * @copyright 2023 Network RADIUS SAS (legal@networkradius.com) |
22 | | */ |
23 | | #include <freeradius-devel/util/chap.h> |
24 | | |
25 | | /** Encode a CHAP password |
26 | | * |
27 | | * @param[out] out An output buffer of 17 bytes (id + MD5 digest). |
28 | | * @param[in] id CHAP ID, a random ID for request/response matching. |
29 | | * @param[in] challenge the CHAP challenge |
30 | | * @param[in] challenge_len Length of the challenge. |
31 | | * @param[in] password Input password to hash. |
32 | | * @param[in] password_len Length of input password. |
33 | | */ |
34 | | void fr_chap_encode(uint8_t out[static 1 + FR_CHAP_CHALLENGE_LENGTH], |
35 | | uint8_t id, uint8_t const *challenge, size_t challenge_len, |
36 | | char const *password, size_t password_len) |
37 | 0 | { |
38 | 0 | fr_md5_ctx_t *md5_ctx; |
39 | |
|
40 | 0 | md5_ctx = fr_md5_ctx_alloc_from_list(); |
41 | | |
42 | | /* |
43 | | * First ingest the ID and the password. |
44 | | */ |
45 | 0 | fr_md5_update(md5_ctx, (uint8_t const *)&id, 1); |
46 | 0 | fr_md5_update(md5_ctx, (uint8_t const *)password, password_len); |
47 | |
|
48 | 0 | fr_md5_update(md5_ctx, challenge, challenge_len); |
49 | 0 | out[0] = id; |
50 | 0 | fr_md5_final(out + 1, md5_ctx); |
51 | 0 | fr_md5_ctx_free_from_list(&md5_ctx); |
52 | 0 | } |