summaryrefslogtreecommitdiffstats
path: root/lib/util/genrand_util.c
blob: 43005c5666600cd30bf1db00939c734740629ba3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
   Unix SMB/CIFS implementation.

   Functions to create reasonable random numbers for crypto use.

   Copyright (C) Jeremy Allison 2001

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"
#include "system/locale.h"
#include <tevent.h>
#include "lib/util/samba_util.h"
#include "lib/util/debug.h"

/**
 * @file
 * @brief Random number generation
 */

/**
  generate a single random uint32_t
**/
_PUBLIC_ uint32_t generate_random(void)
{
	uint8_t v[4];
	generate_random_buffer(v, 4);
	return IVAL(v, 0);
}

/**
  @brief generate a random uint64
**/
_PUBLIC_ uint64_t generate_random_u64(void)
{
	uint8_t v[8];
	generate_random_buffer(v, 8);
	return BVAL(v, 0);
}

/**
 * @brief Generate a random number in the given range.
 *
 * @param lower    The lower value of the range

 * @param upper    The upper value of the range
 *
 * @return A random number bigger than than lower and smaller than upper.
 */
_PUBLIC_ uint64_t generate_random_u64_range(uint64_t lower, uint64_t upper)
{
	return generate_random_u64() % (upper - lower) + lower;
}

_PUBLIC_ uint64_t generate_unique_u64(uint64_t veto_value)
{
	static struct generate_unique_u64_state {
		uint64_t next_value;
		int pid;
	} generate_unique_u64_state;

	int pid = tevent_cached_getpid();

	if (unlikely(pid != generate_unique_u64_state.pid)) {
		generate_unique_u64_state = (struct generate_unique_u64_state) {
			.pid = pid,
			.next_value = veto_value,
		};
	}

	while (unlikely(generate_unique_u64_state.next_value == veto_value)) {
		generate_nonce_buffer(
				(void *)&generate_unique_u64_state.next_value,
				sizeof(generate_unique_u64_state.next_value));
	}

	return generate_unique_u64_state.next_value++;
}

/**
  Microsoft composed the following rules (among others) for quality
  checks. This is an abridgment from
  http://msdn.microsoft.com/en-us/subscriptions/cc786468%28v=ws.10%29.aspx:

  Passwords must contain characters from three of the following five
  categories:

   - Uppercase characters of European languages (A through Z, with
     diacritic marks, Greek and Cyrillic characters)
   - Lowercase characters of European languages (a through z, sharp-s,
     with diacritic marks, Greek and Cyrillic characters)
   - Base 10 digits (0 through 9)
   - Nonalphanumeric characters: ~!@#$%^&*_-+=`|\(){}[]:;"'<>,.?/
   - Any Unicode character that is categorized as an alphabetic character
     but is not uppercase or lowercase. This includes Unicode characters
     from Asian languages.

 Note: for now do not check if the unicode category is
       alphabetic character
**/
_PUBLIC_ bool check_password_quality(const char *pwd)
{
	size_t ofs = 0;
	size_t num_digits = 0;
	size_t num_upper = 0;
	size_t num_lower = 0;
	size_t num_nonalpha = 0;
	size_t num_unicode = 0;
	size_t num_categories = 0;

	if (pwd == NULL) {
		return false;
	}

	while (true) {
		const char *s = &pwd[ofs];
		size_t len = 0;
		codepoint_t c;

		c = next_codepoint(s, &len);
		if (c == INVALID_CODEPOINT) {
			return false;
		} else if (c == 0) {
			break;
		}
		ofs += len;

		if (len == 1) {
			const char *na = "~!@#$%^&*_-+=`|\\(){}[]:;\"'<>,.?/";

			if (isdigit(c)) {
				num_digits += 1;
				continue;
			}

			if (isupper(c)) {
				num_upper += 1;
				continue;
			}

			if (islower(c)) {
				num_lower += 1;
				continue;
			}

			if (strchr(na, c)) {
				num_nonalpha += 1;
				continue;
			}

			/*
			 * the rest does not belong to
			 * a category.
			 */
			continue;
		}

		if (isupper_m(c)) {
			num_upper += 1;
			continue;
		}

		if (islower_m(c)) {
			num_lower += 1;
			continue;
		}

		/*
		 * Note: for now do not check if the unicode category is
		 *       alphabetic character
		 *
		 * We would have to import the details from
		 * ftp://ftp.unicode.org/Public/6.3.0/ucd/UnicodeData-6.3.0d1.txt
		 */
		num_unicode += 1;
		continue;
	}

	if (num_digits > 0) {
		num_categories += 1;
	}
	if (num_upper > 0) {
		num_categories += 1;
	}
	if (num_lower > 0) {
		num_categories += 1;
	}
	if (num_nonalpha > 0) {
		num_categories += 1;
	}
	if (num_unicode > 0) {
		num_categories += 1;
	}

	if (num_categories >= 3) {
		return true;
	}

	return false;
}

/**
 Use the random number generator to generate a random string.
**/

_PUBLIC_ char *generate_random_str_list(TALLOC_CTX *mem_ctx, size_t len, const char *list)
{
	size_t i;
	size_t list_len = strlen(list);

	char *retstr = talloc_array(mem_ctx, char, len + 1);
	if (!retstr) return NULL;

	generate_secret_buffer((uint8_t *)retstr, len);
	for (i = 0; i < len; i++) {
		retstr[i] = list[retstr[i] % list_len];
	}
	retstr[i] = '\0';

	return retstr;
}

/**
 * Generate a random text string consisting of the specified length.
 * The returned string will be allocated.
 *
 * Characters used are: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_-#.,
 */

_PUBLIC_ char *generate_random_str(TALLOC_CTX *mem_ctx, size_t len)
{
	char *retstr;
	const char *c_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_-#.,";

again:
	retstr = generate_random_str_list(mem_ctx, len, c_list);
	if (!retstr) return NULL;

	/* we need to make sure the random string passes basic quality tests
	   or it might be rejected by windows as a password */
	if (len >= 7 && !check_password_quality(retstr)) {
		talloc_free(retstr);
		goto again;
	}

	return retstr;
}

/**
 * Generate a random text password (based on printable ascii characters).
 */

_PUBLIC_ char *generate_random_password(TALLOC_CTX *mem_ctx, size_t min, size_t max)
{
	char *retstr;
	/* This list does not include { or } because they cause
	 * problems for our provision (it can create a substring
	 * ${...}, and for Fedora DS (which treats {...} at the start
	 * of a stored password as special
	 *  -- Andrew Bartlett 2010-03-11
	 */
	const char *c_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_-#.,@$%&!?:;<=>()[]~";
	size_t len = max;
	size_t diff;

	if (min > max) {
		errno = EINVAL;
		return NULL;
	}

	diff = max - min;

	if (diff > 0 ) {
		size_t tmp;

		generate_secret_buffer((uint8_t *)&tmp, sizeof(tmp));

		tmp %= diff;

		len = min + tmp;
	}

again:
	retstr = generate_random_str_list(mem_ctx, len, c_list);
	if (!retstr) return NULL;

	/* we need to make sure the random string passes basic quality tests
	   or it might be rejected by windows as a password */
	if (len >= 7 && !check_password_quality(retstr)) {
		talloc_free(retstr);
		goto again;
	}

	return retstr;
}

/**
 * Generate a random machine password (based on random utf16 characters,
 * converted to utf8). min must be at least 14, max must be at most 255.
 *
 * If 'unix charset' is not utf8, the password consist of random ascii
 * values!
 *
 * The return value is a talloc string with destructor talloc_keep_secret() set.
 * The content will be overwritten by zeros when the mem_ctx is destroyed.
 */

_PUBLIC_ char *generate_random_machine_password(TALLOC_CTX *mem_ctx, size_t min, size_t max)
{
	TALLOC_CTX *frame = NULL;
	struct generate_random_machine_password_state {
		uint8_t password_buffer[256 * 2];
		uint8_t tmp;
	} *state;
	char *new_pw = NULL;
	size_t len = max;
	char *utf8_pw = NULL;
	size_t utf8_len = 0;
	char *unix_pw = NULL;
	size_t unix_len = 0;
	size_t diff;
	size_t i;
	bool ok;
	int cmp;

	if (max > 255) {
		errno = EINVAL;
		return NULL;
	}

	if (min < 14) {
		errno = EINVAL;
		return NULL;
	}

	if (min > max) {
		errno = EINVAL;
		return NULL;
	}

	frame = talloc_stackframe_pool(2048);
	state = talloc_zero(frame, struct generate_random_machine_password_state);
	talloc_keep_secret(state);

	diff = max - min;

	if (diff > 0) {
		size_t tmp;

		generate_secret_buffer((uint8_t *)&tmp, sizeof(tmp));

		tmp %= diff;

		len = min + tmp;
	}

	/*
	 * Create a random machine account password
	 * We create a random buffer and convert that to utf8.
	 * This is similar to what windows is doing.
	 *
	 * In future we may store the raw random buffer,
	 * but for now we need to pass the password as
	 * char pointer through some layers.
	 *
	 * As most kerberos keys are derived from the
	 * utf8 password we need to fallback to
	 * ASCII passwords if "unix charset" is not utf8.
	 */
	generate_secret_buffer(state->password_buffer, len * 2);
	for (i = 0; i < len; i++) {
		size_t idx = i*2;
		uint16_t c;

		/*
		 * both MIT krb5 and HEIMDAL only
		 * handle codepoints up to 0xffff.
		 *
		 * It means we need to avoid
		 * 0xD800 - 0xDBFF (high surrogate)
		 * and
		 * 0xDC00 - 0xDFFF (low surrogate)
		 * in the random utf16 data.
		 *
		 * 55296 0xD800 0154000 0b1101100000000000
		 * 57343 0xDFFF 0157777 0b1101111111111111
		 * 8192  0x2000  020000   0b10000000000000
		 *
		 * The above values show that we can check
		 * for 0xD800 and just add 0x2000 to avoid
		 * the surrogate ranges.
		 *
		 * The rest will be handled by CH_UTF16MUNGED
		 * see utf16_munged_pull().
		 */
		c = SVAL(state->password_buffer, idx);
		if (c & 0xD800) {
			c |= 0x2000;
		}
		SSVAL(state->password_buffer, idx, c);
	}
	ok = convert_string_talloc(frame,
				   CH_UTF16MUNGED, CH_UTF8,
				   state->password_buffer, len * 2,
				   (void *)&utf8_pw, &utf8_len);
	if (!ok) {
		DEBUG(0, ("%s: convert_string_talloc() failed\n",
			  __func__));
		TALLOC_FREE(frame);
		return NULL;
	}
	talloc_keep_secret(utf8_pw);

	ok = convert_string_talloc(frame,
				   CH_UTF16MUNGED, CH_UNIX,
				   state->password_buffer, len * 2,
				   (void *)&unix_pw, &unix_len);
	if (!ok) {
		goto ascii_fallback;
	}
	talloc_keep_secret(unix_pw);

	if (utf8_len != unix_len) {
		goto ascii_fallback;
	}

	cmp = memcmp((const uint8_t *)utf8_pw,
		     (const uint8_t *)unix_pw,
		     utf8_len);
	if (cmp != 0) {
		goto ascii_fallback;
	}

	new_pw = talloc_strdup(mem_ctx, utf8_pw);
	if (new_pw == NULL) {
		TALLOC_FREE(frame);
		return NULL;
	}
	talloc_keep_secret(new_pw);
	talloc_set_name_const(new_pw, __func__);
	TALLOC_FREE(frame);
	return new_pw;

ascii_fallback:
	for (i = 0; i < len; i++) {
		/*
		 * truncate to ascii
		 */
		state->tmp = state->password_buffer[i] & 0x7f;
		if (state->tmp == 0) {
			state->tmp = state->password_buffer[i] >> 1;
		}
		if (state->tmp == 0) {
			state->tmp = 0x01;
		}
		state->password_buffer[i] = state->tmp;
	}
	state->password_buffer[i] = '\0';

	new_pw = talloc_strdup(mem_ctx, (const char *)state->password_buffer);
	if (new_pw == NULL) {
		TALLOC_FREE(frame);
		return NULL;
	}
	talloc_keep_secret(new_pw);
	talloc_set_name_const(new_pw, __func__);
	TALLOC_FREE(frame);
	return new_pw;
}

/**
 * Generate an array of unique text strings all of the same length.
 * The returned string will be allocated.
 * Returns NULL if the number of unique combinations cannot be created.
 *
 * Characters used are: abcdefghijklmnopqrstuvwxyz0123456789+_-#.,
 */
_PUBLIC_ char** generate_unique_strs(TALLOC_CTX *mem_ctx, size_t len,
				     uint32_t num)
{
	const char *c_list = "abcdefghijklmnopqrstuvwxyz0123456789+_-#.,";
	const unsigned c_size = 42;
	size_t i, j;
	unsigned rem;
	char ** strs = NULL;

	if (num == 0 || len == 0)
		return NULL;

	strs = talloc_array(mem_ctx, char *, num);
	if (strs == NULL) return NULL;

	for (i = 0; i < num; i++) {
		char *retstr = (char *)talloc_size(strs, len + 1);
		if (retstr == NULL) {
			talloc_free(strs);
			return NULL;
		}
		rem = i;
		for (j = 0; j < len; j++) {
			retstr[j] = c_list[rem % c_size];
			rem = rem / c_size;
		}
		retstr[j] = 0;
		strs[i] = retstr;
		if (rem != 0) {
			/* we were not able to fit the number of
			 * combinations asked for in the length
			 * specified */
			DEBUG(0,(__location__ ": Too many combinations %u for length %u\n",
				 num, (unsigned)len));

			talloc_free(strs);
			return NULL;
		}
	}

	return strs;
}