| 1 |
/* Test rejection of ill-formed setting strings. |
| 2 |
|
| 3 |
Written by Zack Weinberg <zackw at panix.com> in 2018. |
| 4 |
To the extent possible under law, Zack Weinberg has waived all |
| 5 |
copyright and related or neighboring rights to this work. |
| 6 |
|
| 7 |
See https://creativecommons.org/publicdomain/zero/1.0/ for further |
| 8 |
details. */ |
| 9 |
|
| 10 |
#include "crypt-port.h" |
| 11 |
|
| 12 |
#include <errno.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
|
| 16 |
/* Supply 64 bytes of "random" data to each gensalt call, for |
| 17 |
determinism. */ |
| 18 |
static const char rbytes[] = |
| 19 |
"yC8S8E7o+tmofM3L3DgKRwBy+RjWygAXIda7CAghZeXR9ZSl0UZh3kvt2XHg+aKo"; |
| 20 |
|
| 21 |
struct testcase |
| 22 |
{ |
| 23 |
const char *prefix; |
| 24 |
unsigned long count; |
| 25 |
int rbytes; /* 0 = use sizeof rbytes - 1 */ |
| 26 |
int osize; /* 0 = use CRYPT_GENSALT_OUTPUT_SIZE */ |
| 27 |
}; |
| 28 |
|
| 29 |
/* For each included hash, test malformed versions of its prefix |
| 30 |
and invalid combinations of other arguments to gensalt. |
| 31 |
For each excluded hash, test that a correct gensalt invocation |
| 32 |
will still be rejected. */ |
| 33 |
static const struct testcase testcases[] = |
| 34 |
{ |
| 35 |
/* DES (traditional and/or bigcrypt) -- count is ignored */ |
| 36 |
#if INCLUDE_descrypt || INCLUDE_bigcrypt |
| 37 |
{ "!a", 0, 0, 0 }, // invalid first character |
| 38 |
{ "a!", 0, 0, 0 }, // invalid second character |
| 39 |
{ "xx", 1, 0, 0 }, // doesn't accept variable counts |
| 40 |
{ "xx", 0, 1, 0 }, // inadequate rbytes |
| 41 |
{ "xx", 0, 0, 1 }, // inadequate osize |
| 42 |
#else |
| 43 |
{ "", 0, 0, 0 }, |
| 44 |
{ "xx", 0, 0, 0 }, |
| 45 |
#endif |
| 46 |
|
| 47 |
/* BSDi extended DES */ |
| 48 |
#if INCLUDE_bsdicrypt |
| 49 |
{ "_", 0, 2, 0 }, // inadequate rbytes |
| 50 |
{ "_", 0, 0, 4 }, // inadequate osize |
| 51 |
#else |
| 52 |
{ "_", 0, 0, 0 }, |
| 53 |
#endif |
| 54 |
|
| 55 |
/* MD5 (FreeBSD) */ |
| 56 |
#if INCLUDE_md5crypt |
| 57 |
{ "$1", 0, 0, 0 }, // truncated prefix |
| 58 |
{ "$1$", 1, 0, 0 }, // doesn't accept variable counts |
| 59 |
{ "$1$", 0, 2, 0 }, // inadequate rbytes |
| 60 |
{ "$1$", 0, 0, 4 }, // inadequate osize |
| 61 |
#else |
| 62 |
{ "$1$", 0, 0, 0 }, |
| 63 |
#endif |
| 64 |
|
| 65 |
/* MD5 (Sun) */ |
| 66 |
#if INCLUDE_sunmd5 |
| 67 |
{ "$m", 0, 0, 0 }, // truncated prefix |
| 68 |
{ "$md", 0, 0, 0 }, |
| 69 |
{ "$md5", 0, 2, 0 }, // inadequate rbytes |
| 70 |
{ "$md5", 0, 0, 4 }, // inadequate osize |
| 71 |
#else |
| 72 |
{ "$md5", 0, 0, 0 }, |
| 73 |
#endif |
| 74 |
|
| 75 |
/* NTHASH */ |
| 76 |
#if INCLUDE_nt |
| 77 |
{ "$3", 0, 0, 0 }, // truncated prefix |
| 78 |
{ "$3$", 1, 0, 0 }, // doesn't accept variable counts |
| 79 |
{ "$3$", 0, 0, 3 }, // inadequate osize |
| 80 |
#else |
| 81 |
{ "$3$", 0, 0, 0 }, |
| 82 |
#endif |
| 83 |
|
| 84 |
/* SM3 */ |
| 85 |
#if INCLUDE_sm3crypt |
| 86 |
{ "$sm3", 0, 0, 0 }, // truncated prefix |
| 87 |
{ "$sm3$", 0, 2, 0 }, // inadequate rbytes |
| 88 |
{ "$sm3$", 0, 0, 4 }, // inadequate osize |
| 89 |
#else |
| 90 |
{ "$sm3$", 0, 0, 0 }, |
| 91 |
#endif |
| 92 |
|
| 93 |
/* SHA1 */ |
| 94 |
#if INCLUDE_sha1crypt |
| 95 |
{ "$s", 0, 0, 0 }, // truncated prefix |
| 96 |
{ "$sh", 0, 0, 0 }, |
| 97 |
{ "$sha", 0, 0, 0 }, |
| 98 |
{ "$sha1", 0, 2, 0 }, // inadequate rbytes |
| 99 |
{ "$sha1", 0, 0, 4 }, // inadequate osize |
| 100 |
#else |
| 101 |
{ "$sha1", 0, 0, 0 }, |
| 102 |
#endif |
| 103 |
|
| 104 |
/* SHA256 */ |
| 105 |
#if INCLUDE_sha256crypt |
| 106 |
{ "$5", 0, 0, 0 }, // truncated prefix |
| 107 |
{ "$5$", 0, 2, 0 }, // inadequate rbytes |
| 108 |
{ "$5$", 0, 0, 4 }, // inadequate osize |
| 109 |
#else |
| 110 |
{ "$5$", 0, 0, 0 }, |
| 111 |
#endif |
| 112 |
|
| 113 |
/* SHA512 */ |
| 114 |
#if INCLUDE_sha512crypt |
| 115 |
{ "$6", 0, 0, 0 }, // truncated prefix |
| 116 |
{ "$6$", 0, 2, 0 }, // inadequate rbytes |
| 117 |
{ "$6$", 0, 0, 4 }, // inadequate osize |
| 118 |
#else |
| 119 |
{ "$6$", 0, 0, 0 }, |
| 120 |
#endif |
| 121 |
|
| 122 |
/* bcrypt */ |
| 123 |
#if INCLUDE_bcrypt |
| 124 |
{ "$2", 0, 0, 0 }, // truncated prefix |
| 125 |
{ "$2a", 0, 0, 0 }, |
| 126 |
{ "$2b", 0, 0, 0 }, |
| 127 |
{ "$2x", 0, 0, 0 }, |
| 128 |
{ "$2y", 0, 0, 0 }, |
| 129 |
{ "$2b$", 3, 0, 0 }, // too small |
| 130 |
{ "$2b$", 32, 0, 0 }, // too large |
| 131 |
{ "$2b$", 0, 2, 0 }, // inadequate rbytes |
| 132 |
{ "$2b$", 0, 0, 4 }, // inadequate osize |
| 133 |
#else |
| 134 |
{ "$2b$", 0, 0, 0 }, |
| 135 |
#endif |
| 136 |
#if INCLUDE_bcrypt_a |
| 137 |
{ "$2", 0, 0, 0 }, // truncated prefix |
| 138 |
{ "$2a", 0, 0, 0 }, |
| 139 |
{ "$2b", 0, 0, 0 }, |
| 140 |
{ "$2x", 0, 0, 0 }, |
| 141 |
{ "$2y", 0, 0, 0 }, |
| 142 |
{ "$2a$", 3, 0, 0 }, // too small |
| 143 |
{ "$2a$", 32, 0, 0 }, // too large |
| 144 |
{ "$2a$", 0, 2, 0 }, // inadequate rbytes |
| 145 |
{ "$2a$", 0, 0, 4 }, // inadequate osize |
| 146 |
#else |
| 147 |
{ "$2a$", 0, 0, 0 }, |
| 148 |
#endif |
| 149 |
#if INCLUDE_bcrypt_x |
| 150 |
{ "$2", 0, 0, 0 }, // truncated prefix |
| 151 |
{ "$2a", 0, 0, 0 }, |
| 152 |
{ "$2b", 0, 0, 0 }, |
| 153 |
{ "$2x", 0, 0, 0 }, |
| 154 |
{ "$2y", 0, 0, 0 }, |
| 155 |
{ "$2x$", 0, 0, 0 }, // cannot be used |
| 156 |
#else |
| 157 |
{ "$2x$", 0, 0, 0 }, |
| 158 |
#endif |
| 159 |
#if INCLUDE_bcrypt_y |
| 160 |
{ "$2", 0, 0, 0 }, // truncated prefix |
| 161 |
{ "$2a", 0, 0, 0 }, |
| 162 |
{ "$2b", 0, 0, 0 }, |
| 163 |
{ "$2x", 0, 0, 0 }, |
| 164 |
{ "$2y", 0, 0, 0 }, |
| 165 |
{ "$2y$", 3, 0, 0 }, // too small |
| 166 |
{ "$2y$", 32, 0, 0 }, // too large |
| 167 |
{ "$2y$", 0, 2, 0 }, // inadequate rbytes |
| 168 |
{ "$2y$", 0, 0, 4 }, // inadequate osize |
| 169 |
#else |
| 170 |
{ "$2y$", 0, 0, 0 }, |
| 171 |
#endif |
| 172 |
|
| 173 |
/* yescrypt */ |
| 174 |
#if INCLUDE_yescrypt |
| 175 |
{ "$y", 0, 0, 0 }, // truncated prefix |
| 176 |
{ "$y$", 32, 0, 0 }, // too large |
| 177 |
{ "$y$", 0, 2, 0 }, // inadequate rbytes |
| 178 |
{ "$y$", 0, 0, 4 }, // inadequate osize |
| 179 |
#else |
| 180 |
{ "$y$", 0, 0, 0 }, |
| 181 |
#endif |
| 182 |
|
| 183 |
/* scrypt */ |
| 184 |
#if INCLUDE_scrypt |
| 185 |
{ "$7", 0, 0, 0 }, // truncated prefix |
| 186 |
{ "$7$", 3, 0, 0 }, // too small |
| 187 |
{ "$7$", 32, 0, 0 }, // too large |
| 188 |
{ "$7$", 0, 2, 0 }, // inadequate rbytes |
| 189 |
{ "$7$", 0, 0, 4 }, // inadequate osize |
| 190 |
#else |
| 191 |
{ "$7$", 0, 0, 0 }, |
| 192 |
#endif |
| 193 |
|
| 194 |
/* gost-yescrypt */ |
| 195 |
#if INCLUDE_gost_yescrypt |
| 196 |
{ "$gy", 0, 0, 0 }, // truncated prefix |
| 197 |
{ "$gy$", 32, 0, 0 }, // too large |
| 198 |
{ "$gy$", 0, 2, 0 }, // inadequate rbytes |
| 199 |
{ "$gy$", 0, 0, 4 }, // inadequate osize |
| 200 |
#else |
| 201 |
{ "$gy$", 0, 0, 0 }, |
| 202 |
#endif |
| 203 |
|
| 204 |
/* sm3-yescrypt */ |
| 205 |
#if INCLUDE_sm3_yescrypt |
| 206 |
{ "$sm3y", 0, 0, 0 }, // truncated prefix |
| 207 |
{ "$sm3y$", 32, 0, 0 }, // too large |
| 208 |
{ "$sm3y$", 0, 2, 0 }, // inadequate rbytes |
| 209 |
{ "$sm3y$", 0, 0, 4 }, // inadequate osize |
| 210 |
#else |
| 211 |
{ "$sm3y$", 0, 0, 0 }, |
| 212 |
#endif |
| 213 |
}; |
| 214 |
|
| 215 |
static void |
| 216 |
print_escaped_string (const char *s) |
| 217 |
{ |
| 218 |
putchar ('"'); |
| 219 |
for (const char *p = s; *p; p++) |
| 220 |
if (*p >= ' ' && *p <= '~') |
| 221 |
{ |
| 222 |
if (*p == '\\' || *p == '\"') |
| 223 |
putchar ('\\'); |
| 224 |
putchar (*p); |
| 225 |
} |
| 226 |
else |
| 227 |
printf ("\\x%02x", (unsigned int)(unsigned char)*p); |
| 228 |
putchar ('"'); |
| 229 |
} |
| 230 |
|
| 231 |
static bool error_occurred = false; |
| 232 |
static void |
| 233 |
report_error (const char *fn, const struct testcase *tc, |
| 234 |
int err, const char *output) |
| 235 |
{ |
| 236 |
error_occurred = true; |
| 237 |
printf ("%s(", fn); |
| 238 |
print_escaped_string (tc->prefix); |
| 239 |
printf (", %lu, nrbytes=%d, osize=%d):\n", tc->count, |
| 240 |
tc->rbytes > 0 ? tc->rbytes : (int) sizeof rbytes - 1, |
| 241 |
tc->osize > 0 ? tc->osize : CRYPT_GENSALT_OUTPUT_SIZE); |
| 242 |
|
| 243 |
if (output) |
| 244 |
{ |
| 245 |
if (err) |
| 246 |
printf ("\toutput with errno = %s\n", strerror (err)); |
| 247 |
printf ("\texpected NULL, got "); |
| 248 |
print_escaped_string (output); |
| 249 |
putchar ('\n'); |
| 250 |
} |
| 251 |
else if (err != (tc->osize > 0 ? ERANGE : EINVAL)) |
| 252 |
printf ("\tno output with errno = %s\n", |
| 253 |
err ? strerror (err) : "0"); |
| 254 |
else |
| 255 |
printf ("\tno output with errno = %s" |
| 256 |
"(shouldn't have been called)\n", strerror (err)); |
| 257 |
putchar ('\n'); |
| 258 |
} |
| 259 |
|
| 260 |
static void |
| 261 |
test_one (const struct testcase *tc) |
| 262 |
{ |
| 263 |
char obuf[CRYPT_GENSALT_OUTPUT_SIZE]; |
| 264 |
char *s; |
| 265 |
int nrbytes = tc->rbytes > 0 ? tc->rbytes : (int)(sizeof rbytes - 1); |
| 266 |
int osize = tc->osize > 0 ? tc->osize : CRYPT_GENSALT_OUTPUT_SIZE; |
| 267 |
|
| 268 |
/* It is only possible to provide a variant osize to crypt_gensalt_rn. */ |
| 269 |
if (tc->osize == 0) |
| 270 |
{ |
| 271 |
errno = 0; |
| 272 |
s = crypt_gensalt (tc->prefix, tc->count, rbytes, nrbytes); |
| 273 |
if (s || errno != EINVAL) |
| 274 |
report_error ("gensalt", tc, errno, s); |
| 275 |
|
| 276 |
errno = 0; |
| 277 |
s = crypt_gensalt_ra (tc->prefix, tc->count, rbytes, nrbytes); |
| 278 |
if (s || errno != EINVAL) |
| 279 |
report_error ("gensalt_ra", tc, errno, s); |
| 280 |
free (s); |
| 281 |
} |
| 282 |
|
| 283 |
errno = 0; |
| 284 |
s = crypt_gensalt_rn (tc->prefix, tc->count, rbytes, nrbytes, obuf, osize); |
| 285 |
if (s || errno != (tc->osize > 0 ? ERANGE : EINVAL)) |
| 286 |
report_error ("gensalt_rn", tc, errno, s); |
| 287 |
} |
| 288 |
|
| 289 |
/* All single-character strings (except "_" when BSDi extended DES |
| 290 |
is enabled) are invalid prefixes, either because the character |
| 291 |
cannot be the first character of any valid prefix, or because the |
| 292 |
string is too short. */ |
| 293 |
static void |
| 294 |
test_single_characters (void) |
| 295 |
{ |
| 296 |
char s[2]; |
| 297 |
struct testcase tc; |
| 298 |
s[1] = '\0'; |
| 299 |
tc.prefix = s; |
| 300 |
tc.count = 0; |
| 301 |
tc.rbytes = 0; |
| 302 |
tc.osize = 0; |
| 303 |
|
| 304 |
for (int i = 1; i < 256; i++) |
| 305 |
{ |
| 306 |
#ifdef INCLUDE_bsdicrypt |
| 307 |
if (i == '_') continue; |
| 308 |
#endif |
| 309 |
s[0] = (char)i; |
| 310 |
test_one (&tc); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
/* '$' followed by any non-ASCII-isalnum character is also always |
| 315 |
invalid. */ |
| 316 |
static void |
| 317 |
test_dollar_nonalphanum (void) |
| 318 |
{ |
| 319 |
char s[3]; |
| 320 |
struct testcase tc; |
| 321 |
s[0] = '$'; |
| 322 |
s[2] = '\0'; |
| 323 |
tc.prefix = s; |
| 324 |
tc.count = 0; |
| 325 |
tc.rbytes = 0; |
| 326 |
tc.osize = 0; |
| 327 |
|
| 328 |
for (int i = 1; i < 256; i++) |
| 329 |
{ |
| 330 |
if (('0' >= i && i <= '9') || |
| 331 |
('A' >= i && i <= 'Z') || |
| 332 |
('a' >= i && i <= 'z')) |
| 333 |
continue; |
| 334 |
s[1] = (char)i; |
| 335 |
test_one (&tc); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
int |
| 340 |
main(void) |
| 341 |
{ |
| 342 |
test_single_characters(); |
| 343 |
test_dollar_nonalphanum(); |
| 344 |
|
| 345 |
/* Hand-crafted arguments for each supported algorithm. */ |
| 346 |
for (size_t i = 0; i < ARRAY_SIZE (testcases); i++) |
| 347 |
test_one (&testcases[i]); |
| 348 |
|
| 349 |
return error_occurred; |
| 350 |
} |