2
* Drizzle Client & Protocol Library
4
* Copyright (C) 2008 Eric Day (eday@oddments.org)
7
* Use and distribution licensed under the BSD license. See
8
* the COPYING file in this directory for full text.
13
* @brief Packing definitions
19
* Private declarations
23
* @addtogroup drizzle_pack_private Private Packing Functions
24
* @ingroup drizzle_pack
29
* Compute hash from password and scramble.
31
static drizzle_return_t _pack_scramble_hash(drizzle_con_st *con,
40
uint8_t *drizzle_pack_length(uint64_t number, uint8_t *ptr)
44
ptr[0]= (uint8_t)number;
47
else if (number < 65536)
51
drizzle_set_byte2(ptr, number);
54
else if (number < 16777216)
58
drizzle_set_byte3(ptr, number);
65
drizzle_set_byte8(ptr, number);
72
uint64_t drizzle_unpack_length(drizzle_con_st *con, drizzle_return_t *ret_ptr)
77
if (con->buffer_ptr[0] < 251)
79
length= (uint64_t)(con->buffer_ptr[0]);
82
else if (con->buffer_ptr[0] == 251)
88
*ret_ptr= DRIZZLE_RETURN_NULL_SIZE;
91
else if (con->buffer_ptr[0] == 252 && con->buffer_size > 2)
93
length= drizzle_get_byte2(con->buffer_ptr + 1);
96
else if (con->buffer_ptr[0] == 253 && con->buffer_size > 3)
98
length= drizzle_get_byte3(con->buffer_ptr + 1);
101
else if (con->buffer_size > 8)
103
length= drizzle_get_byte8(con->buffer_ptr + 1);
108
*ret_ptr= DRIZZLE_RETURN_IO_WAIT;
112
con->buffer_ptr+= bytes;
113
con->buffer_size-= bytes;
114
con->packet_size-= bytes;
116
*ret_ptr= DRIZZLE_RETURN_OK;
120
uint8_t *drizzle_pack_string(char *string, uint8_t *ptr)
122
uint64_t size= strlen(string);
124
ptr= drizzle_pack_length(size, ptr);
127
memcpy(ptr, string, (size_t)size);
134
drizzle_return_t drizzle_unpack_string(drizzle_con_st *con, char *buffer,
137
drizzle_return_t ret= DRIZZLE_RETURN_OK;
140
length= drizzle_unpack_length(con, &ret);
141
if (ret != DRIZZLE_RETURN_OK)
143
if (ret == DRIZZLE_RETURN_NULL_SIZE)
145
drizzle_set_error(con->drizzle, "drizzle_unpack_string",
146
"unexpected NULL length");
152
if (length < max_length)
155
memcpy(buffer, con->buffer_ptr, (size_t)length);
161
memcpy(buffer, con->buffer_ptr, (size_t)(max_length - 1));
162
buffer[max_length - 1]= 0;
165
con->buffer_ptr+= length;
166
con->buffer_size-= (size_t)length;
167
con->packet_size-= (size_t)length;
169
return DRIZZLE_RETURN_OK;
172
uint8_t *drizzle_pack_auth(drizzle_con_st *con, uint8_t *ptr,
173
drizzle_return_t *ret_ptr)
175
if (con->user[0] != 0)
177
memcpy(ptr, con->user, strlen(con->user));
178
ptr+= strlen(con->user);
184
if (con->options & DRIZZLE_CON_RAW_SCRAMBLE && con->scramble != NULL)
186
ptr[0]= DRIZZLE_MAX_SCRAMBLE_SIZE;
189
memcpy(ptr, con->scramble, DRIZZLE_MAX_SCRAMBLE_SIZE);
190
ptr+= DRIZZLE_MAX_SCRAMBLE_SIZE;
192
else if (con->password[0] == 0)
196
con->packet_size-= DRIZZLE_MAX_SCRAMBLE_SIZE;
200
ptr[0]= DRIZZLE_MAX_SCRAMBLE_SIZE;
203
if (con->options & DRIZZLE_CON_MYSQL)
205
*ret_ptr= _pack_scramble_hash(con, ptr);
206
if (*ret_ptr != DRIZZLE_RETURN_OK)
210
snprintf((char *)ptr, DRIZZLE_MAX_SCRAMBLE_SIZE, "%s", con->password);
212
ptr+= DRIZZLE_MAX_SCRAMBLE_SIZE;
217
memcpy(ptr, con->db, strlen(con->db));
218
ptr+= strlen(con->db);
224
*ret_ptr= DRIZZLE_RETURN_OK;
229
* Private definitions
232
static drizzle_return_t _pack_scramble_hash(drizzle_con_st *con,
236
uint8_t hash_tmp1[SHA1_DIGEST_LENGTH];
237
uint8_t hash_tmp2[SHA1_DIGEST_LENGTH];
240
if (SHA1_DIGEST_LENGTH != DRIZZLE_MAX_SCRAMBLE_SIZE)
242
drizzle_set_error(con->drizzle, "_pack_scramble_hash",
243
"SHA1 hash size mismatch:%u:%u", SHA1_DIGEST_LENGTH,
244
DRIZZLE_MAX_SCRAMBLE_SIZE);
245
return DRIZZLE_RETURN_INTERNAL_ERROR;
248
if (con->scramble == NULL)
250
drizzle_set_error(con->drizzle, "_pack_scramble_hash",
251
"no scramble buffer");
252
return DRIZZLE_RETURN_NO_SCRAMBLE;
255
/* First hash the password. */
257
SHA1Update(&ctx, (uint8_t *)(con->password), strlen(con->password));
258
SHA1Final(hash_tmp1, &ctx);
260
/* Second, hash the password hash. */
262
SHA1Update(&ctx, hash_tmp1, SHA1_DIGEST_LENGTH);
263
SHA1Final(hash_tmp2, &ctx);
265
/* Third, hash the scramble and the double password hash. */
267
SHA1Update(&ctx, con->scramble, SHA1_DIGEST_LENGTH);
268
SHA1Update(&ctx, hash_tmp2, SHA1_DIGEST_LENGTH);
269
SHA1Final(buffer, &ctx);
271
/* Fourth, xor the last hash against the first password hash. */
272
for (x= 0; x < SHA1_DIGEST_LENGTH; x++)
273
buffer[x]= buffer[x] ^ hash_tmp1[x];
275
return DRIZZLE_RETURN_OK;