1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| /// Crypt the password to MD5.
int
sql_build_password_md5 ( PGconn * sql_conn )
{
char * crypt_pwd;
char * crypt_pwd2;
crypt_pwd = malloc(2 * (MD5_PASSWORD_LEN + 1)); // Allocate enough space for two md5 hashes
if (!crypt_pwd)
{
trace_err("Can't alloc crypt_pwd");
return -1;
}
crypt_pwd2 = crypt_pwd + MD5_PASSWORD_LEN + 1;
if (sql_md5_encrypt(sql_conn->pgpass, sql_conn->pguser, strlen(sql_conn->pguser), crypt_pwd2) == -1) // First hash
{
free(crypt_pwd);
return -1;
}
if (sql_md5_encrypt(crypt_pwd2 + strlen("md5"), sql_conn->md5Salt, sizeof(sql_conn->md5Salt), crypt_pwd) == -1) // Second hash
{
free(crypt_pwd);
return -1;
}
sql_send_msg(sql_conn, 'p', strlen(crypt_pwd) + 1, crypt_pwd);
free(crypt_pwd);
return 0;
}
/// Md5 Hash.
int
sql_md5_encrypt (const char * password, const char * salt, size_t salt_len, char * buf )
{
size_t password_len = strlen(password);
char * crypt_buffer = malloc(password_len + salt_len + 1); // +1 here is just to avoid risk of unportable malloc(0)
if (!crypt_buffer)
{
free(crypt_buffer);
return -1;
}
/* Place salt at the end because it may be known by users trying to crack the MD5 output */
memcpy(crypt_buffer, password, password_len);
memcpy(crypt_buffer + password_len, salt, salt_len);
strcpy(buf, "md5");
sys_crypto_md5_create(crypt_buffer, password_len + salt_len, buf+3);
free(crypt_buffer);
return 0;
}
/// Add the msg_type and the message length at the beginning of the message.
void
sql_add_start_msg( PGconn * sql_conn, byte1 msg_type, int32 msg_len )
{
if (msg_type)
{
msg_len += sizeof(msg_type);
block_list_write_buffer(&sql_conn->send, &msg_type, sizeof(msg_type)); // Add the message type.
}
msg_len += sizeof(msg_len);
msg_len = htonl(msg_len);
block_list_write_buffer(&sql_conn->send, &msg_len, sizeof(msg_len)); // Add the message size.
}
/// Send a message.
int
sql_send_msg( PGconn * sql_conn, byte1 msg_type, int32 msg_len, char * msg )
{
char buffer[256];
unsigned int len = 256;
unsigned int size_read;
sql_add_start_msg(sql_conn, msg_type, msg_len); // We add the lenght message
block_list_write_buffer(&sql_conn->send, msg, msg_len); // The message is saved in the block list
size_read = block_list_read_buffer(&sql_conn->send, &buffer, len);
if (net_socket_send_tcp(sql_conn->socket, &buffer, size_read) != NET_SOCKET_OK)
{
trace_err("Can't send message");
sys_events_set_callback(sql_conn->sql_client->sys_events, SYS_EVENT_WRITE, sql_conn->socket, &sql_conn->write_callback);
return -1;
}
return 0;
}
/* MD5 */
void
sys_crypto_md5_create ( const unsigned char * source, unsigned long size, unsigned char * destination )
{
if (check(source != NULL, "Argument source is NULL") &&
check(destination != NULL, "Argument destination is NULL") &&
check(size > 0, "Argument size is invalid"))
{
MD5(source,size,destination);
}
} |
Partager