summaryrefslogtreecommitdiffstats
path: root/crypto/heimdal/kadmin/random_password.c
blob: d56dd941f39f0f89b5b7b5dc6a5c62be7ed138f8 (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
/*
 * Copyright (c) 1998, 1999 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden). 
 * All rights reserved. 
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met: 
 *
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the distribution. 
 *
 * 3. Neither the name of the Institute nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 * SUCH DAMAGE. 
 */

#include "kadmin_locl.h"

RCSID("$Id: random_password.c 21745 2007-07-31 16:11:25Z lha $");

/* This file defines some a function that generates a random password,
   that can be used when creating a large amount of principals (such
   as for a batch of students). Since this is a political matter, you
   should think about how secure generated passwords has to be.
   
   Both methods defined here will give you at least 55 bits of
   entropy.
   */

/* If you want OTP-style passwords, define OTP_STYLE */

#ifdef OTP_STYLE
#include <otp.h>
#else
static void generate_password(char **pw, int num_classes, ...);
#endif

void
random_password(char *pw, size_t len)
{
#ifdef OTP_STYLE
    {
	OtpKey newkey;

	krb5_generate_random_block(&newkey, sizeof(newkey));
	otp_print_stddict (newkey, pw, len);
	strlwr(pw);
    }
#else
    char *pass;
    generate_password(&pass, 3, 
		      "abcdefghijklmnopqrstuvwxyz", 7, 
		      "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 2, 
		      "@$%&*()-+=:,/<>1234567890", 1);
    strlcpy(pw, pass, len);
    memset(pass, 0, strlen(pass));
    free(pass);
#endif
}

/* some helper functions */

#ifndef OTP_STYLE
/* return a random value in range 0-127 */
static int
RND(unsigned char *key, int keylen, int *left)
{
    if(*left == 0){
	krb5_generate_random_block(key, keylen);
	*left = keylen;
    }
    (*left)--;
    return ((unsigned char*)key)[*left];
}

/* This a helper function that generates a random password with a
   number of characters from a set of character classes.

   If there are n classes, and the size of each class is Pi, and the
   number of characters from each class is Ni, the number of possible
   passwords are (given that the character classes are disjoint):

     n             n
   -----        /  ----  \
   |   |  Ni    |  \     |
   |   | Pi     |   \  Ni| !
   |   | ---- * |   /    |
   |   | Ni!    |  /___  |
    i=1          \  i=1  /
   
    Since it uses the RND function above, neither the size of each
    class, nor the total length of the generated password should be
    larger than 127 (without fixing RND).
   
   */
static void
generate_password(char **pw, int num_classes, ...)
{
    struct {
	const char *str;
	int len;
	int freq;
    } *classes;
    va_list ap;
    int len, i;
    unsigned char rbuf[8]; /* random buffer */
    int rleft = 0;

    *pw = NULL;

    classes = malloc(num_classes * sizeof(*classes));
    if(classes == NULL)
	return;
    va_start(ap, num_classes);
    len = 0;
    for(i = 0; i < num_classes; i++){
	classes[i].str = va_arg(ap, const char*);
	classes[i].len = strlen(classes[i].str);
	classes[i].freq = va_arg(ap, int);
	len += classes[i].freq;
    }
    va_end(ap);
    *pw = malloc(len + 1);
    if(*pw == NULL) {
	free(classes);
	return;
    }
    for(i = 0; i < len; i++) {
	int j;
	int x = RND(rbuf, sizeof(rbuf), &rleft) % (len - i);
	int t = 0;
	for(j = 0; j < num_classes; j++) {
	    if(x < t + classes[j].freq) {
		(*pw)[i] = classes[j].str[RND(rbuf, sizeof(rbuf), &rleft)
					 % classes[j].len];
		classes[j].freq--;
		break;
	    }
	    t += classes[j].freq;
	}
    }
    (*pw)[len] = '\0';
    memset(rbuf, 0, sizeof(rbuf));
    free(classes);
}
#endif
OpenPOWER on IntegriCloud