1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
/* password checking routines */
21
/*****************************************************************************
22
The main idea is that no password are sent between client & server on
23
connection and that no password are saved in mysql in a decodable form.
25
On connection a random string is generated and sent to the client.
26
The client generates a new string with a random generator inited with
27
the hash values from the password and the sent string.
28
This 'check' string is sent to the server where it is compared with
29
a string generated from the stored hash_value of the password and the
32
The password is saved (in user.password) by using the PASSWORD() function in
35
This is .c file because it's used in libmysqlclient, which is entirely in C.
36
(we need it to be portable to a variety of systems).
38
update user set password=PASSWORD("hello") where user="test"
39
This saves a hashed number as a string in the password field.
41
The new authentication is performed in following manner:
43
SERVER: public_seed=create_random_string()
46
CLIENT: recv(public_seed)
47
hash_stage1=sha1("password")
48
hash_stage2=sha1(hash_stage1)
49
reply=xor(hash_stage1, sha1(public_seed,hash_stage2)
51
// this three steps are done in scramble()
57
hash_stage1=xor(reply, sha1(public_seed,hash_stage2))
58
candidate_hash2=sha1(hash_stage1)
59
check(candidate_hash2==hash_stage2)
61
// this three steps are done in check_scramble()
63
*****************************************************************************/
65
#include "libdrizzle.h"
66
#include "libdrizzle_priv.h"
70
/************ MySQL 3.23-4.0 authentication routines: untouched ***********/
73
New (MySQL 3.21+) random generation structure initialization
76
rand_st OUT Structure to initialize
77
seed1 IN First initialization parameter
78
seed2 IN Second initialization parameter
81
void randominit(struct rand_struct *rand_st, uint32_t seed1, uint32_t seed2)
82
{ /* For mysql 3.21.# */
83
memset(rand_st, 0, sizeof(*rand_st)); /* Avoid UMC varnings */
84
rand_st->max_value= 0x3FFFFFFFL;
85
rand_st->max_value_dbl=(double) rand_st->max_value;
86
rand_st->seed1=seed1%rand_st->max_value ;
87
rand_st->seed2=seed2%rand_st->max_value;
92
Generate random number.
95
rand_st INOUT Structure used for number generation
97
generated pseudo random number
100
double my_rnd(struct rand_struct *rand_st)
102
rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
103
rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
104
return (((double) rand_st->seed1)/rand_st->max_value_dbl);
109
Generate binary hash from raw text string
110
Used for Pre-4.1 password handling
113
result OUT store hash in this location
114
password IN plain text password to build hash
115
password_len IN password length (password may be not null-terminated)
118
void hash_password(uint32_t *result, const char *password, uint32_t password_len)
120
register uint32_t nr=1345345333L, add=7, nr2=0x12345671L;
122
const char *password_end= password + password_len;
123
for (; password < password_end; password++)
125
if (*password == ' ' || *password == '\t')
126
continue; /* skip space in password */
127
tmp= (uint32_t) (unsigned char) *password;
128
nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
129
nr2+=(nr2 << 8) ^ nr;
132
result[0]=nr & (((uint32_t) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
133
result[1]=nr2 & (((uint32_t) 1L << 31) -1L);
136
static inline uint8_t char_val(uint8_t X)
138
return (uint32_t) (X >= '0' && X <= '9' ? X-'0' :
139
X >= 'A' && X <= 'Z' ? X-'A'+10 : X-'a'+10);
144
**************** MySQL 4.1.1 authentication routines *************
148
Generate string of printable random characters of requested length
150
create_random_string()
151
to OUT buffer for generation; must be at least length+1 bytes
152
long; result string is always null-terminated
153
length IN how many random characters to put in buffer
154
rand_st INOUT structure used for number generation
157
void create_random_string(char *to, uint32_t length, struct rand_struct *rand_st)
159
char *end= to + length;
160
/* Use pointer arithmetics as it is faster way to do so. */
161
for (; to < end; to++)
162
*to= (char) (my_rnd(rand_st)*94+33);
167
/* Character to use as version identifier for version 4.1 */
169
#define PVERSION41_CHAR '*'
173
Convert given octet sequence to asciiz string of hex characters;
174
str..str+len and 'to' may not overlap.
177
buf OUT output buffer. Must be at least 2*len+1 bytes
178
str, len IN the beginning and the length of the input string
184
char *octet2hex(char *to, const char *str, uint32_t len)
186
const char *str_end= str + len;
187
for (; str != str_end; ++str)
189
*to++= _dig_vec_upper[((unsigned char) *str) >> 4];
190
*to++= _dig_vec_upper[((unsigned char) *str) & 0x0F];