summaryrefslogtreecommitdiffstats
path: root/src/auth/password-scheme-crypt.c
blob: 34febad8845a98260ddc14f5e901c81834a014dc (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
/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "mycrypt.h"
#include "password-scheme.h"
#include "crypt-blowfish.h"
#include "randgen.h"

/* Lengths and limits for some crypt() algorithms. */
#define CRYPT_BLF_ROUNDS_DEFAULT 5
#define CRYPT_BLF_ROUNDS_MIN 4
#define CRYPT_BLF_ROUNDS_MAX 31
#define CRYPT_BLF_SALT_LEN 16 /* raw salt */
#define CRYPT_BLF_PREFIX_LEN (7+22+1) /* $2.$nn$ + salt */
#define CRYPT_BLF_BUFFER_LEN 128
#define CRYPT_BLF_PREFIX "$2y"
#define CRYPT_SHA2_ROUNDS_DEFAULT 5000
#define CRYPT_SHA2_ROUNDS_MIN 1000
#define CRYPT_SHA2_ROUNDS_MAX 999999999
#define CRYPT_SHA2_SALT_LEN 16

static void
crypt_generate_des(const char *plaintext, const struct password_generate_params *params ATTR_UNUSED,
		   const unsigned char **raw_password_r, size_t *size_r)
{
#define CRYPT_SALT_LEN 2
	const char *password, *salt;

	salt = password_generate_salt(CRYPT_SALT_LEN);
	password = t_strdup(mycrypt(plaintext, salt));
	*raw_password_r = (const unsigned char *)password;
	*size_r = strlen(password);
}

static void
crypt_generate_blowfish(const char *plaintext, const struct password_generate_params *params,
			 const unsigned char **raw_password_r, size_t *size_r)
{
	char salt[CRYPT_BLF_SALT_LEN];
	char password[CRYPT_BLF_BUFFER_LEN];
	char magic_salt[CRYPT_BLF_PREFIX_LEN];
	unsigned int rounds = params->rounds;

	if (rounds == 0)
		rounds = CRYPT_BLF_ROUNDS_DEFAULT;
	else if (rounds < CRYPT_BLF_ROUNDS_MIN)
		rounds = CRYPT_BLF_ROUNDS_MIN;
	else if (rounds > CRYPT_BLF_ROUNDS_MAX)
		rounds = CRYPT_BLF_ROUNDS_MAX;

	random_fill(salt, CRYPT_BLF_SALT_LEN);
	if (crypt_gensalt_blowfish_rn(CRYPT_BLF_PREFIX, rounds,
				      salt, CRYPT_BLF_SALT_LEN,
				      magic_salt, CRYPT_BLF_PREFIX_LEN) == NULL)
		i_fatal("crypt_gensalt_blowfish_rn failed: %m");

	if (crypt_blowfish_rn(plaintext, magic_salt, password,
			       CRYPT_BLF_BUFFER_LEN) == NULL)
		i_fatal("crypt_blowfish_rn failed: %m");

	*raw_password_r = (const unsigned char *)t_strdup(password);
	*size_r = strlen(password);
}

static int
crypt_verify_blowfish(const char *plaintext, const struct password_generate_params *params ATTR_UNUSED,
		      const unsigned char *raw_password, size_t size,
		      const char **error_r)
{
	const char *password;
	const char *salt;
	char crypted[CRYPT_BLF_BUFFER_LEN];

	if (size == 0) {
		/* the default mycrypt() handler would return match */
		return 0;
	}
	password = t_strndup(raw_password, size);

	if (size < CRYPT_BLF_PREFIX_LEN ||
	    !str_begins(password, "$2") ||
	    password[2] < 'a' || password[2] > 'z' ||
	    password[3] != '$') {
		*error_r = "Password is not blowfish password";
		return -1;
	}

	salt = t_strndup(password, CRYPT_BLF_PREFIX_LEN);
	if (crypt_blowfish_rn(plaintext, salt, crypted, CRYPT_BLF_BUFFER_LEN) == NULL) {
		/* really shouldn't happen unless the system is broken */
		*error_r = t_strdup_printf("crypt_blowfish_rn failed: %m");
		return -1;
	}

	return strcmp(crypted, password) == 0 ? 1 : 0;
}

static void
crypt_generate_sha256(const char *plaintext, const struct password_generate_params *params,
		      const unsigned char **raw_password_r, size_t *size_r)
{
	const char *password, *salt, *magic_salt;
	unsigned int rounds = params->rounds;

	if (rounds == 0)
		rounds = CRYPT_SHA2_ROUNDS_DEFAULT;
	else if (rounds < CRYPT_SHA2_ROUNDS_MIN)
		rounds = CRYPT_SHA2_ROUNDS_MIN;
	else if (rounds > CRYPT_SHA2_ROUNDS_MAX)
		rounds = CRYPT_SHA2_ROUNDS_MAX;

	salt = password_generate_salt(CRYPT_SHA2_SALT_LEN);
	if (rounds == CRYPT_SHA2_ROUNDS_DEFAULT)
		magic_salt = t_strdup_printf("$5$%s", salt);
	else
		magic_salt = t_strdup_printf("$5$rounds=%u$%s", rounds, salt);
	password = t_strdup(mycrypt(plaintext, magic_salt));
	*raw_password_r = (const unsigned char *)password;
	*size_r = strlen(password);
}

static void
crypt_generate_sha512(const char *plaintext, const struct password_generate_params *params,
		      const unsigned char **raw_password_r, size_t *size_r)
{
	const char *password, *salt, *magic_salt;
	unsigned int rounds = params->rounds;

	if (rounds == 0)
		rounds = CRYPT_SHA2_ROUNDS_DEFAULT;
	else if (rounds < CRYPT_SHA2_ROUNDS_MIN)
		rounds = CRYPT_SHA2_ROUNDS_MIN;
	else if (rounds > CRYPT_SHA2_ROUNDS_MAX)
		rounds = CRYPT_SHA2_ROUNDS_MAX;

	salt = password_generate_salt(CRYPT_SHA2_SALT_LEN);
	if (rounds == CRYPT_SHA2_ROUNDS_DEFAULT)
		magic_salt = t_strdup_printf("$6$%s", salt);
	else
		magic_salt = t_strdup_printf("$6$rounds=%u$%s", rounds, salt);
	password = t_strdup(mycrypt(plaintext, magic_salt));
	*raw_password_r = (const unsigned char *)password;
	*size_r = strlen(password);
}

/* keep in sync with the crypt_schemes struct below */
static const struct {
	const char *key;
	const char *salt;
	const char *expected;
} sample[] = {
	{ "08/15!test~4711", "JB", "JBOZ0DgmtucwE" },
	{ "08/15!test~4711", "$5$rounds=1000$0123456789abcdef",
	  "$5$rounds=1000$0123456789abcdef$K/DksR0DT01hGc8g/kt"
	  "9McEgrbFMKi9qrb1jehe7hn4" },
	{ "08/15!test~4711", "$6$rounds=1000$0123456789abcdef",
	  "$6$rounds=1000$0123456789abcdef$ZIAd5WqfyLkpvsVCVUU1GrvqaZTq"
	  "vhJoouxdSqJO71l9Ld3tVrfOatEjarhghvEYADkq//LpDnTeO90tcbtHR1" }
};

/* keep in sync with the sample struct above */
static const struct password_scheme crypt_schemes[] = {
	{ "DES-CRYPT", PW_ENCODING_NONE, 0, crypt_verify,
	  crypt_generate_des },
	{ "SHA256-CRYPT", PW_ENCODING_NONE, 0, crypt_verify,
	  crypt_generate_sha256 },
	{ "SHA512-CRYPT", PW_ENCODING_NONE, 0, crypt_verify,
	  crypt_generate_sha512 }
};

static const struct password_scheme blf_crypt_scheme = {
	"BLF-CRYPT", PW_ENCODING_NONE, 0, crypt_verify_blowfish,
		crypt_generate_blowfish
};

static const struct password_scheme default_crypt_scheme = {
	"CRYPT", PW_ENCODING_NONE, 0, crypt_verify,
		crypt_generate_blowfish
};

void password_scheme_register_crypt(void)
{
	unsigned int i;
	const char *crypted;

	i_assert(N_ELEMENTS(crypt_schemes) == N_ELEMENTS(sample));

	for (i = 0; i < N_ELEMENTS(crypt_schemes); i++) {
		crypted = mycrypt(sample[i].key, sample[i].salt);
		if (crypted != NULL &&
		   (strcmp(crypted, sample[i].expected) == 0))
			password_scheme_register(&crypt_schemes[i]);
	}
	password_scheme_register(&blf_crypt_scheme);
	password_scheme_register(&default_crypt_scheme);
}