You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

562 lines
17 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
23 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. /* Copyright (C) 2000-2006 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /* password checking routines */
  13. /*****************************************************************************
  14. The main idea is that no password are sent between client & server on
  15. connection and that no password are saved in mysql in a decodable form.
  16. On connection a random string is generated and sent to the client.
  17. The client generates a new string with a random generator inited with
  18. the hash values from the password and the sent string.
  19. This 'check' string is sent to the server where it is compared with
  20. a string generated from the stored hash_value of the password and the
  21. random string.
  22. The password is saved (in user.password) by using the PASSWORD() function in
  23. mysql.
  24. This is .c file because it's used in libmysqlclient, which is entirely in C.
  25. (we need it to be portable to a variety of systems).
  26. Example:
  27. update user set password=PASSWORD("hello") where user="test"
  28. This saves a hashed number as a string in the password field.
  29. The new authentication is performed in following manner:
  30. SERVER: public_seed=create_random_string()
  31. send(public_seed)
  32. CLIENT: recv(public_seed)
  33. hash_stage1=sha1("password")
  34. hash_stage2=sha1(hash_stage1)
  35. reply=xor(hash_stage1, sha1(public_seed,hash_stage2)
  36. // this three steps are done in scramble()
  37. send(reply)
  38. SERVER: recv(reply)
  39. hash_stage1=xor(reply, sha1(public_seed,hash_stage2))
  40. candidate_hash2=sha1(hash_stage1)
  41. check(candidate_hash2==hash_stage2)
  42. // this three steps are done in check_scramble()
  43. *****************************************************************************/
  44. #include <password.h>
  45. #include <my_global.h>
  46. #include <my_sys.h>
  47. #include <m_string.h>
  48. #include <sha1.h>
  49. #include "mysql.h"
  50. /************ MySQL 3.23-4.0 authentication routines: untouched ***********/
  51. /*
  52. New (MySQL 3.21+) random generation structure initialization
  53. SYNOPSIS
  54. randominit()
  55. rand_st OUT Structure to initialize
  56. seed1 IN First initialization parameter
  57. seed2 IN Second initialization parameter
  58. */
  59. void randominit(struct rand_struct *rand_st, ulong seed1, ulong seed2)
  60. { /* For mysql 3.21.# */
  61. #ifdef HAVE_purify
  62. bzero((char*) rand_st,sizeof(*rand_st)); /* Avoid UMC varnings */
  63. #endif
  64. rand_st->max_value= 0x3FFFFFFFL;
  65. rand_st->max_value_dbl=(double) rand_st->max_value;
  66. rand_st->seed1=seed1%rand_st->max_value ;
  67. rand_st->seed2=seed2%rand_st->max_value;
  68. }
  69. /*
  70. Generate random number.
  71. SYNOPSIS
  72. my_rnd()
  73. rand_st INOUT Structure used for number generation
  74. RETURN VALUE
  75. generated pseudo random number
  76. */
  77. double my_rnd(struct rand_struct *rand_st)
  78. {
  79. rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
  80. rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
  81. return (((double) rand_st->seed1)/rand_st->max_value_dbl);
  82. }
  83. /*
  84. Generate binary hash from raw text string
  85. Used for Pre-4.1 password handling
  86. SYNOPSIS
  87. hash_password()
  88. result OUT store hash in this location
  89. password IN plain text password to build hash
  90. password_len IN password length (password may be not null-terminated)
  91. */
  92. void hash_password(ulong *result, const char *password, uint password_len)
  93. {
  94. register ulong nr=1345345333L, add=7, nr2=0x12345671L;
  95. ulong tmp;
  96. const char *password_end= password + password_len;
  97. for (; password < password_end; password++)
  98. {
  99. if (*password == ' ' || *password == '\t')
  100. continue; /* skip space in password */
  101. tmp= (ulong) (uchar) *password;
  102. nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
  103. nr2+=(nr2 << 8) ^ nr;
  104. add+=tmp;
  105. }
  106. result[0]=nr & (((ulong) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
  107. result[1]=nr2 & (((ulong) 1L << 31) -1L);
  108. }
  109. /*
  110. Create password to be stored in user database from raw string
  111. Used for pre-4.1 password handling
  112. SYNOPSIS
  113. my_make_scrambled_password_323()
  114. to OUT store scrambled password here
  115. password IN user-supplied password
  116. pass_len IN length of password string
  117. */
  118. void my_make_scrambled_password_323(char *to, const char *password,
  119. size_t pass_len)
  120. {
  121. ulong hash_res[2];
  122. hash_password(hash_res, password, (uint) pass_len);
  123. sprintf(to, "%08lx%08lx", hash_res[0], hash_res[1]);
  124. }
  125. /*
  126. Wrapper around my_make_scrambled_password_323() to maintain client lib ABI
  127. compatibility.
  128. In server code usage of my_make_scrambled_password_323() is preferred to
  129. avoid strlen().
  130. SYNOPSIS
  131. make_scrambled_password_323()
  132. to OUT store scrambled password here
  133. password IN NULL-terminated string with user-supplied password
  134. */
  135. void make_scrambled_password_323(char *to, const char *password)
  136. {
  137. my_make_scrambled_password_323(to, password, strlen(password));
  138. }
  139. /*
  140. Scramble string with password.
  141. Used in pre 4.1 authentication phase.
  142. SYNOPSIS
  143. scramble_323()
  144. to OUT Store scrambled message here. Buffer must be at least
  145. SCRAMBLE_LENGTH_323+1 bytes long
  146. message IN Message to scramble. Message must be at least
  147. SRAMBLE_LENGTH_323 bytes long.
  148. password IN Password to use while scrambling
  149. */
  150. void scramble_323(char *to, const char *message, const char *password)
  151. {
  152. struct rand_struct rand_st;
  153. ulong hash_pass[2], hash_message[2];
  154. if (password && password[0])
  155. {
  156. char extra, *to_start=to;
  157. const char *message_end= message + SCRAMBLE_LENGTH_323;
  158. hash_password(hash_pass,password, (uint) strlen(password));
  159. hash_password(hash_message, message, SCRAMBLE_LENGTH_323);
  160. randominit(&rand_st,hash_pass[0] ^ hash_message[0],
  161. hash_pass[1] ^ hash_message[1]);
  162. for (; message < message_end; message++)
  163. *to++= (char) (floor(my_rnd(&rand_st)*31)+64);
  164. extra=(char) (floor(my_rnd(&rand_st)*31));
  165. while (to_start != to)
  166. *(to_start++)^=extra;
  167. }
  168. *to= 0;
  169. }
  170. /*
  171. Check scrambled message
  172. Used in pre 4.1 password handling
  173. SYNOPSIS
  174. check_scramble_323()
  175. scrambled scrambled message to check.
  176. message original random message which was used for scrambling; must
  177. be exactly SCRAMBLED_LENGTH_323 bytes long and
  178. NULL-terminated.
  179. hash_pass password which should be used for scrambling
  180. All params are IN.
  181. RETURN VALUE
  182. 0 - password correct
  183. !0 - password invalid
  184. */
  185. my_bool
  186. check_scramble_323(const unsigned char *scrambled, const char *message,
  187. ulong *hash_pass)
  188. {
  189. struct rand_struct rand_st;
  190. ulong hash_message[2];
  191. uchar buff[16],*to,extra; /* Big enough for check */
  192. const uchar *pos;
  193. hash_password(hash_message, message, SCRAMBLE_LENGTH_323);
  194. randominit(&rand_st,hash_pass[0] ^ hash_message[0],
  195. hash_pass[1] ^ hash_message[1]);
  196. to=buff;
  197. DBUG_ASSERT(sizeof(buff) > SCRAMBLE_LENGTH_323);
  198. for (pos=scrambled ; *pos && to < buff+sizeof(buff) ; pos++)
  199. *to++=(char) (floor(my_rnd(&rand_st)*31)+64);
  200. if (pos-scrambled != SCRAMBLE_LENGTH_323)
  201. return 1;
  202. extra=(char) (floor(my_rnd(&rand_st)*31));
  203. to=buff;
  204. while (*scrambled)
  205. {
  206. if (*scrambled++ != (uchar) (*to++ ^ extra))
  207. return 1; /* Wrong password */
  208. }
  209. return 0;
  210. }
  211. static inline uint8 char_val(uint8 X)
  212. {
  213. return (uint) (X >= '0' && X <= '9' ? X-'0' :
  214. X >= 'A' && X <= 'Z' ? X-'A'+10 : X-'a'+10);
  215. }
  216. /*
  217. Convert password from hex string (as stored in mysql.user) to binary form.
  218. SYNOPSIS
  219. get_salt_from_password_323()
  220. res OUT store salt here
  221. password IN password string as stored in mysql.user
  222. NOTE
  223. This function does not have length check for passwords. It will just crash
  224. Password hashes in old format must have length divisible by 8
  225. */
  226. void get_salt_from_password_323(ulong *res, const char *password)
  227. {
  228. res[0]= res[1]= 0;
  229. if (password)
  230. {
  231. while (*password)
  232. {
  233. ulong val=0;
  234. uint i;
  235. for (i=0 ; i < 8 ; i++)
  236. val=(val << 4)+char_val(*password++);
  237. *res++=val;
  238. }
  239. }
  240. }
  241. /*
  242. Convert scrambled password from binary form to asciiz hex string.
  243. SYNOPSIS
  244. make_password_from_salt_323()
  245. to OUT store resulting string password here, at least 17 bytes
  246. salt IN password in salt format, 2 ulongs
  247. */
  248. void make_password_from_salt_323(char *to, const ulong *salt)
  249. {
  250. sprintf(to,"%08lx%08lx", salt[0], salt[1]);
  251. }
  252. /*
  253. **************** MySQL 4.1.1 authentication routines *************
  254. */
  255. /*
  256. Generate string of printable random characters of requested length
  257. SYNOPSIS
  258. create_random_string()
  259. to OUT buffer for generation; must be at least length+1 bytes
  260. long; result string is always null-terminated
  261. length IN how many random characters to put in buffer
  262. rand_st INOUT structure used for number generation
  263. */
  264. void create_random_string(char *to, uint length, struct rand_struct *rand_st)
  265. {
  266. char *end= to + length;
  267. /* Use pointer arithmetics as it is faster way to do so. */
  268. for (; to < end; to++)
  269. *to= (char) (my_rnd(rand_st)*94+33);
  270. *to= '\0';
  271. }
  272. /* Character to use as version identifier for version 4.1 */
  273. #define PVERSION41_CHAR '*'
  274. /*
  275. Convert given octet sequence to asciiz string of hex characters;
  276. str..str+len and 'to' may not overlap.
  277. SYNOPSIS
  278. octet2hex()
  279. buf OUT output buffer. Must be at least 2*len+1 bytes
  280. str, len IN the beginning and the length of the input string
  281. RETURN
  282. buf+len*2
  283. */
  284. char *octet2hex(char *to, const char *str, uint len)
  285. {
  286. const char *str_end= str + len;
  287. for (; str != str_end; ++str)
  288. {
  289. *to++= _dig_vec_upper[((uchar) *str) >> 4];
  290. *to++= _dig_vec_upper[((uchar) *str) & 0x0F];
  291. }
  292. *to= '\0';
  293. return to;
  294. }
  295. /*
  296. Convert given asciiz string of hex (0..9 a..f) characters to octet
  297. sequence.
  298. SYNOPSIS
  299. hex2octet()
  300. to OUT buffer to place result; must be at least len/2 bytes
  301. str, len IN begin, length for character string; str and to may not
  302. overlap; len % 2 == 0
  303. */
  304. static void
  305. hex2octet(uint8 *to, const char *str, uint len)
  306. {
  307. const char *str_end= str + len;
  308. while (str < str_end)
  309. {
  310. register char tmp= char_val(*str++);
  311. *to++= (tmp << 4) | char_val(*str++);
  312. }
  313. }
  314. /*
  315. Encrypt/Decrypt function used for password encryption in authentication.
  316. Simple XOR is used here but it is OK as we crypt random strings. Note,
  317. that XOR(s1, XOR(s1, s2)) == s2, XOR(s1, s2) == XOR(s2, s1)
  318. SYNOPSIS
  319. my_crypt()
  320. to OUT buffer to hold crypted string; must be at least len bytes
  321. long; to and s1 (or s2) may be the same.
  322. s1, s2 IN input strings (of equal length)
  323. len IN length of s1 and s2
  324. */
  325. static void
  326. my_crypt(char *to, const uchar *s1, const uchar *s2, uint len)
  327. {
  328. const uint8 *s1_end= s1 + len;
  329. while (s1 < s1_end)
  330. *to++= *s1++ ^ *s2++;
  331. }
  332. /*
  333. MySQL 4.1.1 password hashing: SHA conversion (see RFC 2289, 3174) twice
  334. applied to the password string, and then produced octet sequence is
  335. converted to hex string.
  336. The result of this function is used as return value from PASSWORD() and
  337. is stored in the database.
  338. SYNOPSIS
  339. my_make_scrambled_password()
  340. buf OUT buffer of size 2*SHA1_HASH_SIZE + 2 to store hex string
  341. password IN password string
  342. pass_len IN length of password string
  343. */
  344. void my_make_scrambled_password(char *to, const char *password,
  345. size_t pass_len)
  346. {
  347. SHA1_CONTEXT sha1_context;
  348. uint8 hash_stage2[SHA1_HASH_SIZE];
  349. mysql_sha1_reset(&sha1_context);
  350. /* stage 1: hash password */
  351. mysql_sha1_input(&sha1_context, (uint8 *) password, (uint) pass_len);
  352. mysql_sha1_result(&sha1_context, (uint8 *) to);
  353. /* stage 2: hash stage1 output */
  354. mysql_sha1_reset(&sha1_context);
  355. mysql_sha1_input(&sha1_context, (uint8 *) to, SHA1_HASH_SIZE);
  356. /* separate buffer is used to pass 'to' in octet2hex */
  357. mysql_sha1_result(&sha1_context, hash_stage2);
  358. /* convert hash_stage2 to hex string */
  359. *to++= PVERSION41_CHAR;
  360. octet2hex(to, (const char*) hash_stage2, SHA1_HASH_SIZE);
  361. }
  362. /*
  363. Wrapper around my_make_scrambled_password() to maintain client lib ABI
  364. compatibility.
  365. In server code usage of my_make_scrambled_password() is preferred to
  366. avoid strlen().
  367. SYNOPSIS
  368. make_scrambled_password()
  369. buf OUT buffer of size 2*SHA1_HASH_SIZE + 2 to store hex string
  370. password IN NULL-terminated password string
  371. */
  372. void make_scrambled_password(char *to, const char *password)
  373. {
  374. my_make_scrambled_password(to, password, strlen(password));
  375. }
  376. /*
  377. Produce an obscure octet sequence from password and random
  378. string, recieved from the server. This sequence corresponds to the
  379. password, but password can not be easily restored from it. The sequence
  380. is then sent to the server for validation. Trailing zero is not stored
  381. in the buf as it is not needed.
  382. This function is used by client to create authenticated reply to the
  383. server's greeting.
  384. SYNOPSIS
  385. scramble()
  386. buf OUT store scrambled string here. The buf must be at least
  387. SHA1_HASH_SIZE bytes long.
  388. message IN random message, must be exactly SCRAMBLE_LENGTH long and
  389. NULL-terminated.
  390. password IN users' password
  391. */
  392. void
  393. scramble(char *to, const char *message, const char *password)
  394. {
  395. SHA1_CONTEXT sha1_context;
  396. uint8 hash_stage1[SHA1_HASH_SIZE];
  397. uint8 hash_stage2[SHA1_HASH_SIZE];
  398. mysql_sha1_reset(&sha1_context);
  399. /* stage 1: hash password */
  400. mysql_sha1_input(&sha1_context, (uint8 *) password, (uint) strlen(password));
  401. mysql_sha1_result(&sha1_context, hash_stage1);
  402. /* stage 2: hash stage 1; note that hash_stage2 is stored in the database */
  403. mysql_sha1_reset(&sha1_context);
  404. mysql_sha1_input(&sha1_context, hash_stage1, SHA1_HASH_SIZE);
  405. mysql_sha1_result(&sha1_context, hash_stage2);
  406. /* create crypt string as sha1(message, hash_stage2) */;
  407. mysql_sha1_reset(&sha1_context);
  408. mysql_sha1_input(&sha1_context, (const uint8 *) message, SCRAMBLE_LENGTH);
  409. mysql_sha1_input(&sha1_context, hash_stage2, SHA1_HASH_SIZE);
  410. /* xor allows 'from' and 'to' overlap: lets take advantage of it */
  411. mysql_sha1_result(&sha1_context, (uint8 *) to);
  412. my_crypt(to, (const uchar *) to, hash_stage1, SCRAMBLE_LENGTH);
  413. }
  414. /*
  415. Check that scrambled message corresponds to the password; the function
  416. is used by server to check that recieved reply is authentic.
  417. This function does not check lengths of given strings: message must be
  418. null-terminated, reply and hash_stage2 must be at least SHA1_HASH_SIZE
  419. long (if not, something fishy is going on).
  420. SYNOPSIS
  421. check_scramble()
  422. scramble clients' reply, presumably produced by scramble()
  423. message original random string, previously sent to client
  424. (presumably second argument of scramble()), must be
  425. exactly SCRAMBLE_LENGTH long and NULL-terminated.
  426. hash_stage2 hex2octet-decoded database entry
  427. All params are IN.
  428. RETURN VALUE
  429. 0 password is correct
  430. !0 password is invalid
  431. */
  432. my_bool
  433. check_scramble(const uchar *scramble_arg, const char *message,
  434. const uint8 *hash_stage2)
  435. {
  436. SHA1_CONTEXT sha1_context;
  437. uint8 buf[SHA1_HASH_SIZE];
  438. uint8 hash_stage2_reassured[SHA1_HASH_SIZE];
  439. mysql_sha1_reset(&sha1_context);
  440. /* create key to encrypt scramble */
  441. mysql_sha1_input(&sha1_context, (const uint8 *) message, SCRAMBLE_LENGTH);
  442. mysql_sha1_input(&sha1_context, hash_stage2, SHA1_HASH_SIZE);
  443. mysql_sha1_result(&sha1_context, buf);
  444. /* encrypt scramble */
  445. my_crypt((char *) buf, buf, scramble_arg, SCRAMBLE_LENGTH);
  446. /* now buf supposedly contains hash_stage1: so we can get hash_stage2 */
  447. mysql_sha1_reset(&sha1_context);
  448. mysql_sha1_input(&sha1_context, buf, SHA1_HASH_SIZE);
  449. mysql_sha1_result(&sha1_context, hash_stage2_reassured);
  450. return memcmp(hash_stage2, hash_stage2_reassured, SHA1_HASH_SIZE);
  451. }
  452. /*
  453. Convert scrambled password from asciiz hex string to binary form.
  454. SYNOPSIS
  455. get_salt_from_password()
  456. res OUT buf to hold password. Must be at least SHA1_HASH_SIZE
  457. bytes long.
  458. password IN 4.1.1 version value of user.password
  459. */
  460. void get_salt_from_password(uint8 *hash_stage2, const char *password)
  461. {
  462. hex2octet(hash_stage2, password+1 /* skip '*' */, SHA1_HASH_SIZE * 2);
  463. }
  464. /*
  465. Convert scrambled password from binary form to asciiz hex string.
  466. SYNOPSIS
  467. make_password_from_salt()
  468. to OUT store resulting string here, 2*SHA1_HASH_SIZE+2 bytes
  469. salt IN password in salt format
  470. */
  471. void make_password_from_salt(char *to, const uint8 *hash_stage2)
  472. {
  473. *to++= PVERSION41_CHAR;
  474. octet2hex(to, (const char*) hash_stage2, SHA1_HASH_SIZE);
  475. }