MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
password.c
1 /*
2  Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16 
17 /* password checking routines */
18 /*****************************************************************************
19  The main idea is that no password are sent between client & server on
20  connection and that no password are saved in mysql in a decodable form.
21 
22  On connection a random string is generated and sent to the client.
23  The client generates a new string with a random generator inited with
24  the hash values from the password and the sent string.
25  This 'check' string is sent to the server where it is compared with
26  a string generated from the stored hash_value of the password and the
27  random string.
28 
29  The password is saved (in user.password) by using the PASSWORD() function in
30  mysql.
31 
32  This is .c file because it's used in libmysqlclient, which is entirely in C.
33  (we need it to be portable to a variety of systems).
34  Example:
35  update user set password=PASSWORD("hello") where user="test"
36  This saves a hashed number as a string in the password field.
37 
38  The new authentication is performed in following manner:
39 
40  SERVER: public_seed=create_random_string()
41  send(public_seed)
42 
43  CLIENT: recv(public_seed)
44  hash_stage1=sha1("password")
45  hash_stage2=sha1(hash_stage1)
46  reply=xor(hash_stage1, sha1(public_seed,hash_stage2)
47 
48  // this three steps are done in scramble()
49 
50  send(reply)
51 
52 
53  SERVER: recv(reply)
54  hash_stage1=xor(reply, sha1(public_seed,hash_stage2))
55  candidate_hash2=sha1(hash_stage1)
56  check(candidate_hash2==hash_stage2)
57 
58  // this three steps are done in check_scramble()
59 
60 *****************************************************************************/
61 
62 #include <password.h>
63 #include <my_global.h>
64 #include <my_sys.h>
65 #include <m_string.h>
66 #include <sha1.h>
67 #include <my_rnd.h>
68 #include "mysql.h"
69 #include "crypt_genhash_impl.h"
70 
71 /************ MySQL 3.23-4.0 authentication routines: untouched ***********/
72 
73 /*
74  New (MySQL 3.21+) random generation structure initialization
75  SYNOPSIS
76  randominit()
77  rand_st OUT Structure to initialize
78  seed1 IN First initialization parameter
79  seed2 IN Second initialization parameter
80 */
81 
82 void randominit(struct rand_struct *rand_st, ulong seed1, ulong seed2)
83 { /* For mysql 3.21.# */
84 #ifdef HAVE_purify
85  memset(rand_st, 0, sizeof(*rand_st)); /* Avoid UMC varnings */
86 #endif
87  rand_st->max_value= 0x3FFFFFFFL;
88  rand_st->max_value_dbl=(double) rand_st->max_value;
89  rand_st->seed1=seed1%rand_st->max_value ;
90  rand_st->seed2=seed2%rand_st->max_value;
91 }
92 
93 /*
94  Generate binary hash from raw text string
95  Used for Pre-4.1 password handling
96  SYNOPSIS
97  hash_password()
98  result OUT store hash in this location
99  password IN plain text password to build hash
100  password_len IN password length (password may be not null-terminated)
101 */
102 
103 void hash_password(ulong *result, const char *password, uint password_len)
104 {
105  register ulong nr=1345345333L, add=7, nr2=0x12345671L;
106  ulong tmp;
107  const char *password_end= password + password_len;
108  for (; password < password_end; password++)
109  {
110  if (*password == ' ' || *password == '\t')
111  continue; /* skip space in password */
112  tmp= (ulong) (uchar) *password;
113  nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
114  nr2+=(nr2 << 8) ^ nr;
115  add+=tmp;
116  }
117  result[0]=nr & (((ulong) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
118  result[1]=nr2 & (((ulong) 1L << 31) -1L);
119 }
120 
121 
122 /*
123  Create password to be stored in user database from raw string
124  Used for pre-4.1 password handling
125  SYNOPSIS
126  my_make_scrambled_password_323()
127  to OUT store scrambled password here
128  password IN user-supplied password
129  pass_len IN length of password string
130 */
131 
132 void my_make_scrambled_password_323(char *to, const char *password,
133  size_t pass_len)
134 {
135  ulong hash_res[2];
136  hash_password(hash_res, password, (uint) pass_len);
137  sprintf(to, "%08lx%08lx", hash_res[0], hash_res[1]);
138 }
139 
140 
141 /*
142  Wrapper around my_make_scrambled_password_323() to maintain client lib ABI
143  compatibility.
144  In server code usage of my_make_scrambled_password_323() is preferred to
145  avoid strlen().
146  SYNOPSIS
147  make_scrambled_password_323()
148  to OUT store scrambled password here
149  password IN NULL-terminated string with user-supplied password
150 */
151 
152 void make_scrambled_password_323(char *to, const char *password)
153 {
154  my_make_scrambled_password_323(to, password, strlen(password));
155 }
156 
157 
158 /*
159  Scramble string with password.
160  Used in pre 4.1 authentication phase.
161  SYNOPSIS
162  scramble_323()
163  to OUT Store scrambled message here. Buffer must be at least
164  SCRAMBLE_LENGTH_323+1 bytes long
165  message IN Message to scramble. Message must be at least
166  SRAMBLE_LENGTH_323 bytes long.
167  password IN Password to use while scrambling
168 */
169 
170 void scramble_323(char *to, const char *message, const char *password)
171 {
172  struct rand_struct rand_st;
173  ulong hash_pass[2], hash_message[2];
174 
175  if (password && password[0])
176  {
177  char extra, *to_start=to;
178  const char *message_end= message + SCRAMBLE_LENGTH_323;
179  hash_password(hash_pass,password, (uint) strlen(password));
180  hash_password(hash_message, message, SCRAMBLE_LENGTH_323);
181  randominit(&rand_st,hash_pass[0] ^ hash_message[0],
182  hash_pass[1] ^ hash_message[1]);
183  for (; message < message_end; message++)
184  *to++= (char) (floor(my_rnd(&rand_st)*31)+64);
185  extra=(char) (floor(my_rnd(&rand_st)*31));
186  while (to_start != to)
187  *(to_start++)^=extra;
188  }
189  *to= 0;
190 }
191 
192 
205 my_bool
206 check_scramble_323(const unsigned char *scrambled, const char *message,
207  ulong *hash_pass)
208 {
209  struct rand_struct rand_st;
210  ulong hash_message[2];
211  /* Big enough for checks. */
212  uchar buff[16], scrambled_buff[SCRAMBLE_LENGTH_323 + 1];
213  uchar *to, extra;
214  const uchar *pos;
215 
216  /* Ensure that the scrambled message is null-terminated. */
217  memcpy(scrambled_buff, scrambled, SCRAMBLE_LENGTH_323);
218  scrambled_buff[SCRAMBLE_LENGTH_323]= '\0';
219  scrambled= scrambled_buff;
220 
221  hash_password(hash_message, message, SCRAMBLE_LENGTH_323);
222  randominit(&rand_st,hash_pass[0] ^ hash_message[0],
223  hash_pass[1] ^ hash_message[1]);
224  to=buff;
225  DBUG_ASSERT(sizeof(buff) > SCRAMBLE_LENGTH_323);
226  for (pos=scrambled ; *pos && to < buff+sizeof(buff) ; pos++)
227  *to++=(char) (floor(my_rnd(&rand_st)*31)+64);
228  if (pos-scrambled != SCRAMBLE_LENGTH_323)
229  return 1;
230  extra=(char) (floor(my_rnd(&rand_st)*31));
231  to=buff;
232  while (*scrambled)
233  {
234  if (*scrambled++ != (uchar) (*to++ ^ extra))
235  return 1; /* Wrong password */
236  }
237  return 0;
238 }
239 
240 static inline uint8 char_val(uint8 X)
241 {
242  return (uint) (X >= '0' && X <= '9' ? X-'0' :
243  X >= 'A' && X <= 'Z' ? X-'A'+10 : X-'a'+10);
244 }
245 
246 
247 /*
248  Convert password from hex string (as stored in mysql.user) to binary form.
249  SYNOPSIS
250  get_salt_from_password_323()
251  res OUT store salt here
252  password IN password string as stored in mysql.user
253  NOTE
254  This function does not have length check for passwords. It will just crash
255  Password hashes in old format must have length divisible by 8
256 */
257 
258 void get_salt_from_password_323(ulong *res, const char *password)
259 {
260  res[0]= res[1]= 0;
261  if (password)
262  {
263  while (*password)
264  {
265  ulong val=0;
266  uint i;
267  for (i=0 ; i < 8 ; i++)
268  val=(val << 4)+char_val(*password++);
269  *res++=val;
270  }
271  }
272 }
273 
274 
275 /*
276  Convert scrambled password from binary form to asciiz hex string.
277  SYNOPSIS
278  make_password_from_salt_323()
279  to OUT store resulting string password here, at least 17 bytes
280  salt IN password in salt format, 2 ulongs
281 */
282 
283 void make_password_from_salt_323(char *to, const ulong *salt)
284 {
285  sprintf(to,"%08lx%08lx", salt[0], salt[1]);
286 }
287 
288 
289 /*
290  **************** MySQL 4.1.1 authentication routines *************
291 */
292 
308 void create_random_string(char *to, uint length, struct rand_struct *rand_st)
309 {
310  char *end= to + length;
311  /*
312  Warning: my_rnd() is a fast prng, but it doesn't necessarily have a uniform
313  distribution.
314  */
315  for (; to < end; to++)
316  *to= (char) (my_rnd(rand_st) * 94 + 33);
317  *to= '\0';
318 }
319 
320 
321 /* Character to use as version identifier for version 4.1 */
322 
323 #define PVERSION41_CHAR '*'
324 
325 
326 /*
327  Convert given octet sequence to asciiz string of hex characters;
328  str..str+len and 'to' may not overlap.
329  SYNOPSIS
330  octet2hex()
331  buf OUT output buffer. Must be at least 2*len+1 bytes
332  str, len IN the beginning and the length of the input string
333 
334  RETURN
335  buf+len*2
336 */
337 
338 char *octet2hex(char *to, const char *str, uint len)
339 {
340  const char *str_end= str + len;
341  for (; str != str_end; ++str)
342  {
343  *to++= _dig_vec_upper[((uchar) *str) >> 4];
344  *to++= _dig_vec_upper[((uchar) *str) & 0x0F];
345  }
346  *to= '\0';
347  return to;
348 }
349 
350 
351 /*
352  Convert given asciiz string of hex (0..9 a..f) characters to octet
353  sequence.
354  SYNOPSIS
355  hex2octet()
356  to OUT buffer to place result; must be at least len/2 bytes
357  str, len IN begin, length for character string; str and to may not
358  overlap; len % 2 == 0
359 */
360 
361 static void
362 hex2octet(uint8 *to, const char *str, uint len)
363 {
364  const char *str_end= str + len;
365  while (str < str_end)
366  {
367  register char tmp= char_val(*str++);
368  *to++= (tmp << 4) | char_val(*str++);
369  }
370 }
371 
372 
373 /*
374  Encrypt/Decrypt function used for password encryption in authentication.
375  Simple XOR is used here but it is OK as we crypt random strings. Note,
376  that XOR(s1, XOR(s1, s2)) == s2, XOR(s1, s2) == XOR(s2, s1)
377  SYNOPSIS
378  my_crypt()
379  to OUT buffer to hold crypted string; must be at least len bytes
380  long; to and s1 (or s2) may be the same.
381  s1, s2 IN input strings (of equal length)
382  len IN length of s1 and s2
383 */
384 
385 static void
386 my_crypt(char *to, const uchar *s1, const uchar *s2, uint len)
387 {
388  const uint8 *s1_end= s1 + len;
389  while (s1 < s1_end)
390  *to++= *s1++ ^ *s2++;
391 }
392 
393 #if defined(HAVE_OPENSSL)
394 void my_make_scrambled_password(char *to, const char *password,
395  size_t pass_len)
396 {
397 
398  char salt[CRYPT_SALT_LENGTH + 1];
399 
400  generate_user_salt(salt, CRYPT_SALT_LENGTH + 1);
401  my_crypt_genhash(to,
402  CRYPT_MAX_PASSWORD_SIZE,
403  password,
404  pass_len,
405  salt,
406  0);
407 
408 }
409 #endif
410 
422 inline static
423 void compute_two_stage_sha1_hash(const char *password, size_t pass_len,
424  uint8 *hash_stage1, uint8 *hash_stage2)
425 {
426  /* Stage 1: hash password */
427  compute_sha1_hash(hash_stage1, password, pass_len);
428 
429  /* Stage 2 : hash first stage's output. */
430  compute_sha1_hash(hash_stage2, (const char *) hash_stage1, SHA1_HASH_SIZE);
431 }
432 
433 
434 /*
435  MySQL 4.1.1 password hashing: SHA conversion (see RFC 2289, 3174) twice
436  applied to the password string, and then produced octet sequence is
437  converted to hex string.
438  The result of this function is used as return value from PASSWORD() and
439  is stored in the database.
440  SYNOPSIS
441  my_make_scrambled_password_sha1()
442  buf OUT buffer of size 2*SHA1_HASH_SIZE + 2 to store hex string
443  password IN password string
444  pass_len IN length of password string
445 */
446 
447 void my_make_scrambled_password_sha1(char *to, const char *password,
448  size_t pass_len)
449 {
450  uint8 hash_stage2[SHA1_HASH_SIZE];
451 
452  /* Two stage SHA1 hash of the password. */
453  compute_two_stage_sha1_hash(password, pass_len, (uint8 *) to, hash_stage2);
454 
455  /* convert hash_stage2 to hex string */
456  *to++= PVERSION41_CHAR;
457  octet2hex(to, (const char*) hash_stage2, SHA1_HASH_SIZE);
458 }
459 
460 
461 /*
462  Wrapper around my_make_scrambled_password() to maintain client lib ABI
463  compatibility.
464  In server code usage of my_make_scrambled_password() is preferred to
465  avoid strlen().
466  SYNOPSIS
467  make_scrambled_password()
468  buf OUT buffer of size 2*SHA1_HASH_SIZE + 2 to store hex string
469  password IN NULL-terminated password string
470 */
471 
472 void make_scrambled_password(char *to, const char *password)
473 {
474  my_make_scrambled_password_sha1(to, password, strlen(password));
475 }
476 
477 
478 /*
479  Produce an obscure octet sequence from password and random
480  string, recieved from the server. This sequence corresponds to the
481  password, but password can not be easily restored from it. The sequence
482  is then sent to the server for validation. Trailing zero is not stored
483  in the buf as it is not needed.
484  This function is used by client to create authenticated reply to the
485  server's greeting.
486  SYNOPSIS
487  scramble()
488  buf OUT store scrambled string here. The buf must be at least
489  SHA1_HASH_SIZE bytes long.
490  message IN random message, must be exactly SCRAMBLE_LENGTH long and
491  NULL-terminated.
492  password IN users' password
493 */
494 
495 void
496 scramble(char *to, const char *message, const char *password)
497 {
498  uint8 hash_stage1[SHA1_HASH_SIZE];
499  uint8 hash_stage2[SHA1_HASH_SIZE];
500 
501  /* Two stage SHA1 hash of the password. */
502  compute_two_stage_sha1_hash(password, strlen(password), hash_stage1,
503  hash_stage2);
504 
505  /* create crypt string as sha1(message, hash_stage2) */;
506  compute_sha1_hash_multi((uint8 *) to, message, SCRAMBLE_LENGTH,
507  (const char *) hash_stage2, SHA1_HASH_SIZE);
508  my_crypt(to, (const uchar *) to, hash_stage1, SCRAMBLE_LENGTH);
509 }
510 
511 
512 /*
513  Check that scrambled message corresponds to the password; the function
514  is used by server to check that recieved reply is authentic.
515  This function does not check lengths of given strings: message must be
516  null-terminated, reply and hash_stage2 must be at least SHA1_HASH_SIZE
517  long (if not, something fishy is going on).
518  SYNOPSIS
519  check_scramble_sha1()
520  scramble clients' reply, presumably produced by scramble()
521  message original random string, previously sent to client
522  (presumably second argument of scramble()), must be
523  exactly SCRAMBLE_LENGTH long and NULL-terminated.
524  hash_stage2 hex2octet-decoded database entry
525  All params are IN.
526 
527  RETURN VALUE
528  0 password is correct
529  !0 password is invalid
530 */
531 
532 my_bool
533 check_scramble_sha1(const uchar *scramble_arg, const char *message,
534  const uint8 *hash_stage2)
535 {
536  uint8 buf[SHA1_HASH_SIZE];
537  uint8 hash_stage2_reassured[SHA1_HASH_SIZE];
538 
539  /* create key to encrypt scramble */
540  compute_sha1_hash_multi(buf, message, SCRAMBLE_LENGTH,
541  (const char *) hash_stage2, SHA1_HASH_SIZE);
542  /* encrypt scramble */
543  my_crypt((char *) buf, buf, scramble_arg, SCRAMBLE_LENGTH);
544 
545  /* now buf supposedly contains hash_stage1: so we can get hash_stage2 */
546  compute_sha1_hash(hash_stage2_reassured, (const char *) buf, SHA1_HASH_SIZE);
547 
548  return test(memcmp(hash_stage2, hash_stage2_reassured, SHA1_HASH_SIZE));
549 }
550 
551 my_bool
552 check_scramble(const uchar *scramble_arg, const char *message,
553  const uint8 *hash_stage2)
554 {
555  return check_scramble_sha1(scramble_arg, message, hash_stage2);
556 }
557 
558 /*
559  Convert scrambled password from asciiz hex string to binary form.
560 
561  SYNOPSIS
562  get_salt_from_password()
563  res OUT buf to hold password. Must be at least SHA1_HASH_SIZE
564  bytes long.
565  password IN 4.1.1 version value of user.password
566 */
567 
568 void get_salt_from_password(uint8 *hash_stage2, const char *password)
569 {
570  hex2octet(hash_stage2, password+1 /* skip '*' */, SHA1_HASH_SIZE * 2);
571 }
572 
573 /*
574  Convert scrambled password from binary form to asciiz hex string.
575  SYNOPSIS
576  make_password_from_salt()
577  to OUT store resulting string here, 2*SHA1_HASH_SIZE+2 bytes
578  salt IN password in salt format
579 */
580 
581 void make_password_from_salt(char *to, const uint8 *hash_stage2)
582 {
583  *to++= PVERSION41_CHAR;
584  octet2hex(to, (const char*) hash_stage2, SHA1_HASH_SIZE);
585 }
586