1
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
* Drizzle Client & Protocol Library
5
* Copyright (C) 2008 Eric Day (eday@oddments.org)
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions are
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above
16
* copyright notice, this list of conditions and the following disclaimer
17
* in the documentation and/or other materials provided with the
20
* * The names of its contributors may not be used to endorse or
21
* promote products derived from this software without specific prior
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
* @brief Packing definitions
43
#include <libdrizzle/common.h>
46
* Private declarations
50
* @addtogroup drizzle_pack_private Private Packing Functions
51
* @ingroup drizzle_pack
56
* Compute hash from password and scramble.
58
static drizzle_return_t _pack_scramble_hash(drizzle_con_st *con,
67
uint8_t *drizzle_pack_length(uint64_t number, uint8_t *ptr)
71
ptr[0]= (uint8_t)number;
74
else if (number < 65536)
78
drizzle_set_byte2(ptr, number);
81
else if (number < 16777216)
85
drizzle_set_byte3(ptr, number);
92
drizzle_set_byte8(ptr, number);
99
uint64_t drizzle_unpack_length(drizzle_con_st *con, drizzle_return_t *ret_ptr)
104
drizzle_return_t unused_ret;
107
ret_ptr= &unused_ret;
112
*ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
116
if (con->buffer_ptr[0] < 251)
118
length= (uint64_t)(con->buffer_ptr[0]);
121
else if (con->buffer_ptr[0] == 251)
127
*ret_ptr= DRIZZLE_RETURN_NULL_SIZE;
130
else if (con->buffer_ptr[0] == 252 && con->buffer_size > 2)
132
length= drizzle_get_byte2(con->buffer_ptr + 1);
135
else if (con->buffer_ptr[0] == 253 && con->buffer_size > 3)
137
length= drizzle_get_byte3(con->buffer_ptr + 1);
140
else if (con->buffer_size > 8)
142
length= drizzle_get_byte8(con->buffer_ptr + 1);
147
*ret_ptr= DRIZZLE_RETURN_IO_WAIT;
151
con->buffer_ptr+= bytes;
152
con->buffer_size-= bytes;
153
con->packet_size-= bytes;
155
*ret_ptr= DRIZZLE_RETURN_OK;
159
uint8_t *drizzle_pack_string(char *string, uint8_t *ptr)
166
uint64_t size= strlen(string);
168
ptr= drizzle_pack_length(size, ptr);
171
memcpy(ptr, string, (size_t)size);
178
drizzle_return_t drizzle_unpack_string(drizzle_con_st *con, char *buffer,
181
drizzle_return_t ret= DRIZZLE_RETURN_OK;
185
return DRIZZLE_RETURN_INVALID_ARGUMENT;
188
uint64_t length= drizzle_unpack_length(con, &ret);
189
if (ret != DRIZZLE_RETURN_OK)
191
if (ret == DRIZZLE_RETURN_NULL_SIZE)
193
drizzle_set_error(con->drizzle, "drizzle_unpack_string",
194
"unexpected NULL length");
200
if (length < max_length)
203
memcpy(buffer, con->buffer_ptr, (size_t)length);
209
memcpy(buffer, con->buffer_ptr, (size_t)(max_length - 1));
210
buffer[max_length - 1]= 0;
213
con->buffer_ptr+= length;
214
con->buffer_size-= (size_t)length;
215
con->packet_size-= (size_t)length;
217
return DRIZZLE_RETURN_OK;
220
uint8_t *drizzle_pack_auth(drizzle_con_st *con, uint8_t *ptr,
221
drizzle_return_t *ret_ptr)
223
drizzle_return_t unused_ret;
226
ret_ptr= &unused_ret;
231
*ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
235
if (con->user[0] != 0)
237
memcpy(ptr, con->user, strlen(con->user));
238
ptr+= strlen(con->user);
244
if (con->options & DRIZZLE_CON_RAW_SCRAMBLE && con->scramble != NULL)
246
ptr[0]= DRIZZLE_MAX_SCRAMBLE_SIZE;
249
memcpy(ptr, con->scramble, DRIZZLE_MAX_SCRAMBLE_SIZE);
250
ptr+= DRIZZLE_MAX_SCRAMBLE_SIZE;
252
else if (con->password[0] == 0)
256
con->packet_size-= DRIZZLE_MAX_SCRAMBLE_SIZE;
260
ptr[0]= DRIZZLE_MAX_SCRAMBLE_SIZE;
263
if (con->options & DRIZZLE_CON_MYSQL && con->options & DRIZZLE_CON_AUTH_PLUGIN)
265
snprintf((char *)ptr, DRIZZLE_MAX_SCRAMBLE_SIZE, "%s", con->password);
266
ptr[DRIZZLE_MAX_SCRAMBLE_SIZE-1]= 0;
268
else if (con->options & DRIZZLE_CON_MYSQL)
270
*ret_ptr= _pack_scramble_hash(con, ptr);
271
if (*ret_ptr != DRIZZLE_RETURN_OK)
274
else // We assume Drizzle
276
snprintf((char *)ptr, DRIZZLE_MAX_SCRAMBLE_SIZE, "%s", con->password);
277
ptr[DRIZZLE_MAX_SCRAMBLE_SIZE-1]= 0;
280
ptr+= DRIZZLE_MAX_SCRAMBLE_SIZE;
285
memcpy(ptr, con->db, strlen(con->db));
286
ptr+= strlen(con->db);
292
*ret_ptr= DRIZZLE_RETURN_OK;
297
* Private definitions
300
static drizzle_return_t _pack_scramble_hash(drizzle_con_st *con,
304
uint8_t hash_tmp1[SHA1_DIGEST_LENGTH];
305
uint8_t hash_tmp2[SHA1_DIGEST_LENGTH];
307
if (SHA1_DIGEST_LENGTH != DRIZZLE_MAX_SCRAMBLE_SIZE)
309
drizzle_set_error(con->drizzle, "_pack_scramble_hash",
310
"SHA1 hash size mismatch:%u:%u", SHA1_DIGEST_LENGTH,
311
DRIZZLE_MAX_SCRAMBLE_SIZE);
312
return DRIZZLE_RETURN_INTERNAL_ERROR;
315
if (con->scramble == NULL)
317
drizzle_set_error(con->drizzle, "_pack_scramble_hash",
318
"no scramble buffer");
319
return DRIZZLE_RETURN_NO_SCRAMBLE;
322
/* First hash the password. */
324
SHA1Update(&ctx, (uint8_t *)(con->password), strlen(con->password));
325
SHA1Final(hash_tmp1, &ctx);
327
/* Second, hash the password hash. */
329
SHA1Update(&ctx, hash_tmp1, SHA1_DIGEST_LENGTH);
330
SHA1Final(hash_tmp2, &ctx);
332
/* Third, hash the scramble and the double password hash. */
334
SHA1Update(&ctx, con->scramble, SHA1_DIGEST_LENGTH);
335
SHA1Update(&ctx, hash_tmp2, SHA1_DIGEST_LENGTH);
336
SHA1Final(buffer, &ctx);
338
/* Fourth, xor the last hash against the first password hash. */
340
for (x= 0; x < SHA1_DIGEST_LENGTH; x++)
342
buffer[x]= buffer[x] ^ hash_tmp1[x];
345
return DRIZZLE_RETURN_OK;