Debian Codegraph @

lib/crypt.c

raw
1 /* High-level libcrypt interfaces.
2
3 Copyright 2007-2017 Thorsten Kukuk and Zack Weinberg
4 Copyright 2018-2025 Björn Esser
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License
8 as published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20 #include "crypt-port.h"
21
22 #include <errno.h>
23 #include <stdlib.h>
24
25 /* The internal storage area within struct crypt_data is used as
26 follows. We don't know what alignment the algorithm modules will
27 need for their scratch data, so give it the maximum natural
28 alignment. Note that the C11 alignas() specifier can't be applied
29 directly to a struct type, but it can be applied to the first field
30 of a struct, which effectively forces alignment of the entire
31 struct, since the first field must always have offset 0. */
32 struct crypt_internal
33 {
34 /* Alignment critical data members. */
35 char alignas (alignof (max_align_t)) alg_specific[ALG_SPECIFIC_SIZE];
36 /* Not alignment critical data members. */
37 char output[CRYPT_OUTPUT_SIZE];
38 };
39
40 static_assert(sizeof (struct crypt_internal) + alignof (struct crypt_internal)
41 <= CRYPT_DATA_INTERNAL_SIZE,
42 "crypt_data.internal is too small for crypt_internal");
43
44 /* struct crypt_data is allocated by application code and contains
45 only char-typed fields, so its 'internal' field may not be
46 sufficiently aligned. */
47 static inline struct crypt_internal *
48 get_internal (struct crypt_data *data)
49 {
50 uintptr_t internalp = (uintptr_t) data->internal;
51 const uintptr_t align = alignof (struct crypt_internal);
52 internalp = (internalp + align - 1) & ~(align - 1);
53 return (struct crypt_internal *)internalp;
54 }
55
56 typedef void (*crypt_fn) (const char *phrase, size_t phr_size,
57 const char *setting, size_t set_size,
58 uint8_t *output, size_t out_size,
59 void *scratch, size_t scr_size);
60
61 typedef void (*gensalt_fn) (unsigned long count,
62 const uint8_t *rbytes, size_t nrbytes,
63 uint8_t *output, size_t output_size);
64
65 struct hashfn
66 {
67 const char *prefix;
68 size_t plen;
69 crypt_fn crypt;
70 gensalt_fn gensalt;
71 /* The type of this field is unsigned char to ensure that it cannot
72 be set larger than the size of an internal buffer in crypt_gensalt_rn. */
73 unsigned char nrbytes;
74 unsigned char is_strong;
75 };
76
77 static const struct hashfn hash_algorithms[] =
78 {
79 HASH_ALGORITHM_TABLE_ENTRIES
80 };
81
82 #if INCLUDE_descrypt || INCLUDE_bigcrypt
83 static int
84 is_des_salt_char (char c)
85 {
86 return ((c >= 'a' && c <= 'z') ||
87 (c >= 'A' && c <= 'Z') ||
88 (c >= '0' && c <= '9') ||
89 c == '.' || c == '/');
90 }
91 #endif
92
93 static const struct hashfn *
94 get_hashfn (const char *setting)
95 {
96 const struct hashfn *h;
97 for (h = hash_algorithms; h->prefix; h++)
98 {
99 if (h->plen > 0)
100 {
101 if (!strncmp (setting, h->prefix, h->plen))
102 return h;
103 }
104 #if INCLUDE_descrypt || INCLUDE_bigcrypt
105 else
106 {
107 if (setting[0] == '\0' ||
108 (is_des_salt_char (setting[0]) && is_des_salt_char (setting[1])))
109 return h;
110 }
111 #endif
112 }
113 return 0;
114 }
115
116 /* Check a setting string for generic validity, according to the rule
117 stated in crypt(5):
118
119 "Hashed passphrases are always entirely printable ASCII, and do
120 not contain any whitespace or the characters ':', ';', '*', '!',
121 or '\\'. (These characters are used as delimiters and special
122 markers in the passwd(5) and shadow(5) files.)"
123
124 There is a precautionary case for rejecting additional ASCII
125 punctuation, particularly other characters often given syntactic
126 significance in configuration files, such as " ' and #. However,
127 this check didn't used to exist at all, and some of the hash
128 function implementations don't restrict the set of byte values they
129 they will accept in their setting strings (particularly in the salt
130 component) either. Thus, to maintain compatibility with the widest
131 variety of existing hashed passphrases, we are only enforcing the
132 documented rule for now.
133
134 See <https://github.com/besser82/libxcrypt/issues/135> for
135 additional discussion. */
136 static int
137 check_badsalt_chars (const char *setting)
138 {
139 size_t i;
140
141 for (i = 0; setting[i] != '\0'; i++)
142 if ((unsigned char) setting[i] <= 0x20 ||
143 (unsigned char) setting[i] >= 0x7f)
144 return 1;
145
146 return strcspn (setting, "!*:;\\") != i;
147 }
148
149 static void
150 do_crypt (const char *phrase, const char *setting, struct crypt_data *data)
151 {
152 struct crypt_internal *cint = get_internal (data);
153 memset (cint->output, 0, sizeof cint->output);
154 make_failure_token (setting, cint->output, sizeof cint->output);
155
156 if (!phrase || !setting)
157 {
158 errno = EINVAL;
159 goto out;
160 }
161 /* Do these strlen() calls before reading prefixes of either
162 'phrase' or 'setting', so we get a predictable crash if they are
163 not valid strings. */
164 size_t phr_size = strlen (phrase);
165 size_t set_size = strlen (setting);
166 if (phr_size >= CRYPT_MAX_PASSPHRASE_SIZE)
167 {
168 errno = ERANGE;
169 goto out;
170 }
171 if (check_badsalt_chars (setting))
172 {
173 errno = EINVAL;
174 goto out;
175 }
176
177 const struct hashfn *h = get_hashfn (setting);
178 if (!h)
179 {
180 /* Unrecognized hash algorithm */
181 errno = EINVAL;
182 goto out;
183 }
184
185 h->crypt (phrase, phr_size, setting, set_size,
186 (unsigned char *) cint->output, sizeof cint->output,
187 cint->alg_specific, sizeof cint->alg_specific);
188
189 out:
190 strcpy_or_abort (data->output, sizeof data->output, cint->output);
191 explicit_bzero (data->internal, sizeof data->internal);
192 explicit_bzero (data->reserved, sizeof data->reserved);
193 data->initialized = 0;
194 }
195
196 #if INCLUDE_crypt_rn
197 char *
198 crypt_rn (const char *phrase, const char *setting, void *data, int size)
199 {
200 if (size < (int) sizeof (struct crypt_data))
201 {
202 errno = ERANGE;
203 make_failure_token (setting, data, size);
204 return 0;
205 }
206
207 struct crypt_data *p = data;
208 do_crypt (phrase, setting, p);
209 return p->output[0] == '*' ? 0 : p->output;
210 }
211 SYMVER_crypt_rn;
212 #endif
213
214 #if INCLUDE_crypt_ra
215 char *
216 crypt_ra (const char *phrase, const char *setting, void **data, int *size)
217 {
218 struct crypt_data *p = NULL;
219
220 /* Short-circuit, if possible. */
221 if (*data && *size >= (int) sizeof (struct crypt_data))
222 {
223 p = *data;
224 do_crypt (phrase, setting, p);
225 return p->output[0] == '*' ? 0 : p->output;
226 }
227
228 /* Allocate new memory for struct crypt_data. */
229 p = malloc (sizeof (struct crypt_data));
230 if (!p)
231 {
232 /* ERRNO is set by malloc. */
233 if (*data)
234 make_failure_token (setting, *data, *size);
235 return 0;
236 }
237 memset (p, 0, sizeof (struct crypt_data));
238
239 do_crypt (phrase, setting, p);
240
241 /* Zeroize memory, if needed, before assigning
242 the new memory location to the data pointer. */
243 if (*data && *size > 0)
244 explicit_bzero (*data, (size_t) *size);
245 free (*data);
246 *data = p;
247 *size = sizeof (struct crypt_data);
248
249 return p->output[0] == '*' ? 0 : p->output;
250 }
251 SYMVER_crypt_ra;
252 #endif
253
254 #if INCLUDE_crypt_r
255 char *
256 crypt_r (const char *phrase, const char *setting, struct crypt_data *data)
257 {
258 do_crypt (phrase, setting, data);
259 #if ENABLE_FAILURE_TOKENS
260 return data->output;
261 #else
262 return data->output[0] == '*' ? 0 : data->output;
263 #endif
264 }
265 SYMVER_crypt_r;
266 #endif
267
268 /* For code compatibility with older versions (v3.1.1 and earlier). */
269 #if INCLUDE_crypt_r && INCLUDE_xcrypt_r
270 strong_alias (crypt_r, xcrypt_r);
271 SYMVER_xcrypt_r;
272 #endif
273
274 #if INCLUDE_crypt_gensalt_rn
275 char *
276 crypt_gensalt_rn (const char *prefix, unsigned long count,
277 const char *rbytes, int nrbytes, char *output,
278 int output_size)
279 {
280 /* Individual gensalt functions will check for adequate space for
281 their own breed of setting, but the shortest possible one is
282 three bytes (DES two-character salt + NUL terminator) and we
283 also want to rule out negative numbers early. */
284 if (output_size < 3)
285 {
286 errno = ERANGE;
287 make_failure_token (prefix, output, output_size);
288 return 0;
289 }
290
291 char outbuf[CRYPT_GENSALT_OUTPUT_SIZE];
292 unsigned char internal_nrbytes = 0;
293 memset (outbuf, 0, sizeof outbuf);
294 make_failure_token (prefix, outbuf, sizeof outbuf);
295
296 /* If the prefix is 0, that means to use the current best default.
297 Note that this is different from the behavior when the prefix is
298 "", which selects DES. HASH_ALGORITHM_DEFAULT is not defined when
299 the current default algorithm was disabled at configure time. */
300 if (!prefix)
301 {
302 #if defined HASH_ALGORITHM_DEFAULT
303 prefix = HASH_ALGORITHM_DEFAULT;
304 #else
305 errno = EINVAL;
306 goto out;
307 #endif
308 }
309
310 const struct hashfn *h = get_hashfn (prefix);
311 if (!h)
312 {
313 errno = EINVAL;
314 goto out;
315 }
316
317 char internal_rbytes[UCHAR_MAX] = "\0";
318 /* typeof (internal_nrbytes) == typeof (h->nrbytes). */
319
320 /* If rbytes is 0, read random bytes from the operating system if
321 possible. */
322 if (!rbytes)
323 {
324 if (!get_random_bytes (internal_rbytes, h->nrbytes))
325 goto out;
326
327 rbytes = internal_rbytes;
328 nrbytes = internal_nrbytes = h->nrbytes;
329 }
330
331 h->gensalt (count,
332 (const unsigned char *) rbytes, (size_t) nrbytes,
333 (unsigned char *) outbuf,
334 MIN ((size_t) output_size, sizeof outbuf));
335
336 out:
337 strcpy_or_abort (output, (size_t) output_size, outbuf);
338 explicit_bzero (outbuf, sizeof outbuf);
339
340 if (internal_nrbytes)
341 explicit_bzero (internal_rbytes, internal_nrbytes);
342
343 return output[0] == '*' ? 0 : output;
344 }
345 SYMVER_crypt_gensalt_rn;
346 #endif
347
348 /* For code compatibility with older versions (v3.1.1 and earlier). */
349 #if INCLUDE_crypt_gensalt_rn && INCLUDE_crypt_gensalt_r
350 strong_alias (crypt_gensalt_rn, crypt_gensalt_r);
351 SYMVER_crypt_gensalt_r;
352 #endif
353
354 /* For code compatibility with older versions (v3.1.1 and earlier). */
355 #if INCLUDE_crypt_gensalt_rn && INCLUDE_xcrypt_gensalt_r
356 strong_alias (crypt_gensalt_rn, xcrypt_gensalt_r);
357 SYMVER_xcrypt_gensalt_r;
358 #endif
359
360 #if INCLUDE_crypt_gensalt_ra
361 char *
362 crypt_gensalt_ra (const char *prefix, unsigned long count,
363 const char *rbytes, int nrbytes)
364 {
365 char *output = malloc (CRYPT_GENSALT_OUTPUT_SIZE);
366 if (!output)
367 return 0;
368
369 char *result = crypt_gensalt_rn (prefix, count, rbytes, nrbytes, output,
370 CRYPT_GENSALT_OUTPUT_SIZE);
371 if (result == 0)
372 free (output);
373 return result;
374 }
375 SYMVER_crypt_gensalt_ra;
376 #endif
377
378 #if INCLUDE_crypt_checksalt
379 static_assert(CRYPT_SALT_OK == 0, "CRYPT_SALT_OK does not equal zero");
380
381 int
382 crypt_checksalt (const char *setting)
383 {
384 int retval = CRYPT_SALT_INVALID;
385
386 if (!setting || /* NULL string */
387 setting[0] == '\0' || /* empty passphrase */
388 check_badsalt_chars (setting)) /* bad salt chars */
389 goto end;
390
391 const struct hashfn *h = get_hashfn (setting);
392
393 if (h)
394 {
395 retval = CRYPT_SALT_OK;
396
397 if (h->is_strong == 0)
398 {
399 retval = CRYPT_SALT_METHOD_LEGACY;
400 goto end;
401 }
402 }
403
404 end:
405 return retval;
406 }
407 SYMVER_crypt_checksalt;
408 #endif
409
410 #if INCLUDE_crypt_preferred_method
411 const char *
412 crypt_preferred_method (void)
413 {
414 #if defined HASH_ALGORITHM_DEFAULT
415 return HASH_ALGORITHM_DEFAULT;
416 #else
417 return NULL;
418 #endif
419 }
420 SYMVER_crypt_preferred_method;
421 #endif