1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* password checking routines */
17
/*****************************************************************************
18
The main idea is that no password are sent between client & server on
19
connection and that no password are saved in mysql in a decodable form.
21
On connection a random string is generated and sent to the client.
22
The client generates a new string with a random generator inited with
23
the hash values from the password and the sent string.
24
This 'check' string is sent to the server where it is compared with
25
a string generated from the stored hash_value of the password and the
28
The password is saved (in user.password) by using the PASSWORD() function in
31
This is .c file because it's used in libmysqlclient, which is entirely in C.
32
(we need it to be portable to a variety of systems).
34
update user set password=PASSWORD("hello") where user="test"
35
This saves a hashed number as a string in the password field.
37
The new authentication is performed in following manner:
39
SERVER: public_seed=create_random_string()
42
CLIENT: recv(public_seed)
43
hash_stage1=sha1("password")
44
hash_stage2=sha1(hash_stage1)
45
reply=xor(hash_stage1, sha1(public_seed,hash_stage2)
47
// this three steps are done in scramble()
53
hash_stage1=xor(reply, sha1(public_seed,hash_stage2))
54
candidate_hash2=sha1(hash_stage1)
55
check(candidate_hash2==hash_stage2)
57
// this three steps are done in check_scramble()
59
*****************************************************************************/
61
#include <my_global.h>
67
/************ MySQL 3.23-4.0 authentication routines: untouched ***********/
70
New (MySQL 3.21+) random generation structure initialization
73
rand_st OUT Structure to initialize
74
seed1 IN First initialization parameter
75
seed2 IN Second initialization parameter
78
void randominit(struct rand_struct *rand_st, uint32_t seed1, uint32_t seed2)
79
{ /* For mysql 3.21.# */
81
bzero((char*) rand_st,sizeof(*rand_st)); /* Avoid UMC varnings */
83
rand_st->max_value= 0x3FFFFFFFL;
84
rand_st->max_value_dbl=(double) rand_st->max_value;
85
rand_st->seed1=seed1%rand_st->max_value ;
86
rand_st->seed2=seed2%rand_st->max_value;
91
Generate random number.
94
rand_st INOUT Structure used for number generation
96
generated pseudo random number
99
double my_rnd(struct rand_struct *rand_st)
101
rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
102
rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
103
return (((double) rand_st->seed1)/rand_st->max_value_dbl);
108
Generate binary hash from raw text string
109
Used for Pre-4.1 password handling
112
result OUT store hash in this location
113
password IN plain text password to build hash
114
password_len IN password length (password may be not null-terminated)
117
void hash_password(uint32_t *result, const char *password, uint32_t password_len)
119
register ulong nr=1345345333L, add=7, nr2=0x12345671L;
121
const char *password_end= password + password_len;
122
for (; password < password_end; password++)
124
if (*password == ' ' || *password == '\t')
125
continue; /* skip space in password */
126
tmp= (uint32_t) (uchar) *password;
127
nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
128
nr2+=(nr2 << 8) ^ nr;
131
result[0]=nr & (((uint32_t) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
132
result[1]=nr2 & (((uint32_t) 1L << 31) -1L);
135
static inline uint8 char_val(uint8 X)
137
return (uint) (X >= '0' && X <= '9' ? X-'0' :
138
X >= 'A' && X <= 'Z' ? X-'A'+10 : X-'a'+10);
143
**************** MySQL 4.1.1 authentication routines *************
147
Generate string of printable random characters of requested length
149
create_random_string()
150
to OUT buffer for generation; must be at least length+1 bytes
151
long; result string is always null-terminated
152
length IN how many random characters to put in buffer
153
rand_st INOUT structure used for number generation
156
void create_random_string(char *to, uint length, struct rand_struct *rand_st)
158
char *end= to + length;
159
/* Use pointer arithmetics as it is faster way to do so. */
160
for (; to < end; to++)
161
*to= (char) (my_rnd(rand_st)*94+33);
166
/* Character to use as version identifier for version 4.1 */
168
#define PVERSION41_CHAR '*'
172
Convert given octet sequence to asciiz string of hex characters;
173
str..str+len and 'to' may not overlap.
176
buf OUT output buffer. Must be at least 2*len+1 bytes
177
str, len IN the beginning and the length of the input string
183
char *octet2hex(char *to, const char *str, uint len)
185
const char *str_end= str + len;
186
for (; str != str_end; ++str)
188
*to++= _dig_vec_upper[((uchar) *str) >> 4];
189
*to++= _dig_vec_upper[((uchar) *str) & 0x0F];
197
Convert given asciiz string of hex (0..9 a..f) characters to octet
201
to OUT buffer to place result; must be at least len/2 bytes
202
str, len IN begin, length for character string; str and to may not
203
overlap; len % 2 == 0
207
hex2octet(uint8 *to, const char *str, uint len)
209
const char *str_end= str + len;
210
while (str < str_end)
212
register char tmp= char_val(*str++);
213
*to++= (tmp << 4) | char_val(*str++);
219
Encrypt/Decrypt function used for password encryption in authentication.
220
Simple XOR is used here but it is OK as we crypt random strings. Note,
221
that XOR(s1, XOR(s1, s2)) == s2, XOR(s1, s2) == XOR(s2, s1)
224
to OUT buffer to hold crypted string; must be at least len bytes
225
long; to and s1 (or s2) may be the same.
226
s1, s2 IN input strings (of equal length)
227
len IN length of s1 and s2
231
my_crypt(char *to, const uchar *s1, const uchar *s2, uint len)
233
const uint8 *s1_end= s1 + len;
235
*to++= *s1++ ^ *s2++;
240
MySQL 4.1.1 password hashing: SHA conversion (see RFC 2289, 3174) twice
241
applied to the password string, and then produced octet sequence is
242
converted to hex string.
243
The result of this function is used as return value from PASSWORD() and
244
is stored in the database.
246
make_scrambled_password()
247
buf OUT buffer of size 2*SHA1_HASH_SIZE + 2 to store hex string
248
password IN NULL-terminated password string
252
make_scrambled_password(char *to, const char *password)
254
SHA1_CONTEXT sha1_context;
255
uint8 hash_stage2[SHA1_HASH_SIZE];
257
mysql_sha1_reset(&sha1_context);
258
/* stage 1: hash password */
259
mysql_sha1_input(&sha1_context, (uint8 *) password, (uint) strlen(password));
260
mysql_sha1_result(&sha1_context, (uint8 *) to);
261
/* stage 2: hash stage1 output */
262
mysql_sha1_reset(&sha1_context);
263
mysql_sha1_input(&sha1_context, (uint8 *) to, SHA1_HASH_SIZE);
264
/* separate buffer is used to pass 'to' in octet2hex */
265
mysql_sha1_result(&sha1_context, hash_stage2);
266
/* convert hash_stage2 to hex string */
267
*to++= PVERSION41_CHAR;
268
octet2hex(to, (const char*) hash_stage2, SHA1_HASH_SIZE);
273
Produce an obscure octet sequence from password and random
274
string, recieved from the server. This sequence corresponds to the
275
password, but password can not be easily restored from it. The sequence
276
is then sent to the server for validation. Trailing zero is not stored
277
in the buf as it is not needed.
278
This function is used by client to create authenticated reply to the
282
buf OUT store scrambled string here. The buf must be at least
283
SHA1_HASH_SIZE bytes long.
284
message IN random message, must be exactly SCRAMBLE_LENGTH long and
286
password IN users' password
290
scramble(char *to, const char *message, const char *password)
292
SHA1_CONTEXT sha1_context;
293
uint8 hash_stage1[SHA1_HASH_SIZE];
294
uint8 hash_stage2[SHA1_HASH_SIZE];
296
mysql_sha1_reset(&sha1_context);
297
/* stage 1: hash password */
298
mysql_sha1_input(&sha1_context, (uint8 *) password, (uint) strlen(password));
299
mysql_sha1_result(&sha1_context, hash_stage1);
300
/* stage 2: hash stage 1; note that hash_stage2 is stored in the database */
301
mysql_sha1_reset(&sha1_context);
302
mysql_sha1_input(&sha1_context, hash_stage1, SHA1_HASH_SIZE);
303
mysql_sha1_result(&sha1_context, hash_stage2);
304
/* create crypt string as sha1(message, hash_stage2) */;
305
mysql_sha1_reset(&sha1_context);
306
mysql_sha1_input(&sha1_context, (const uint8 *) message, SCRAMBLE_LENGTH);
307
mysql_sha1_input(&sha1_context, hash_stage2, SHA1_HASH_SIZE);
308
/* xor allows 'from' and 'to' overlap: lets take advantage of it */
309
mysql_sha1_result(&sha1_context, (uint8 *) to);
310
my_crypt(to, (const uchar *) to, hash_stage1, SCRAMBLE_LENGTH);
315
Check that scrambled message corresponds to the password; the function
316
is used by server to check that recieved reply is authentic.
317
This function does not check lengths of given strings: message must be
318
null-terminated, reply and hash_stage2 must be at least SHA1_HASH_SIZE
319
long (if not, something fishy is going on).
322
scramble clients' reply, presumably produced by scramble()
323
message original random string, previously sent to client
324
(presumably second argument of scramble()), must be
325
exactly SCRAMBLE_LENGTH long and NULL-terminated.
326
hash_stage2 hex2octet-decoded database entry
330
0 password is correct
331
!0 password is invalid
335
check_scramble(const char *scramble_arg, const char *message,
336
const uint8 *hash_stage2)
338
SHA1_CONTEXT sha1_context;
339
uint8 buf[SHA1_HASH_SIZE];
340
uint8 hash_stage2_reassured[SHA1_HASH_SIZE];
342
mysql_sha1_reset(&sha1_context);
343
/* create key to encrypt scramble */
344
mysql_sha1_input(&sha1_context, (const uint8 *) message, SCRAMBLE_LENGTH);
345
mysql_sha1_input(&sha1_context, hash_stage2, SHA1_HASH_SIZE);
346
mysql_sha1_result(&sha1_context, buf);
347
/* encrypt scramble */
348
my_crypt((char *) buf, buf, (const uchar *) scramble_arg, SCRAMBLE_LENGTH);
349
/* now buf supposedly contains hash_stage1: so we can get hash_stage2 */
350
mysql_sha1_reset(&sha1_context);
351
mysql_sha1_input(&sha1_context, buf, SHA1_HASH_SIZE);
352
mysql_sha1_result(&sha1_context, hash_stage2_reassured);
353
return memcmp(hash_stage2, hash_stage2_reassured, SHA1_HASH_SIZE);
358
Convert scrambled password from asciiz hex string to binary form.
361
get_salt_from_password()
362
res OUT buf to hold password. Must be at least SHA1_HASH_SIZE
364
password IN 4.1.1 version value of user.password
367
void get_salt_from_password(uint8 *hash_stage2, const char *password)
369
hex2octet(hash_stage2, password+1 /* skip '*' */, SHA1_HASH_SIZE * 2);
373
Convert scrambled password from binary form to asciiz hex string.
375
make_password_from_salt()
376
to OUT store resulting string here, 2*SHA1_HASH_SIZE+2 bytes
377
salt IN password in salt format
380
void make_password_from_salt(char *to, const uint8 *hash_stage2)
382
*to++= PVERSION41_CHAR;
383
octet2hex(to, (const char*) hash_stage2, SHA1_HASH_SIZE);