IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Cryptage Md5 pour un client PostgreSQL


Sujet :

C

  1. #1
    Membre averti Avatar de Fooshi
    Homme Profil pro
    ICD
    Inscrit en
    Juin 2002
    Messages
    508
    Points
    364
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : ICD
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 508
    Points : 364
    Par défaut Cryptage Md5 pour un client PostgreSQL
    Bonjour,
    je suis en train de développer un client postgreSQL en C (sans bibliothèque).
    je souhaite apres que j'ai recu un demande d'authentification en md5, crypter mon mot de passe en Md5. Postgresql ne demandant pas un hashage en une passe je me suis basé sur le code que fournis la libpq (lib C pour postgresql, j'en ai déterminé ce code qui me construis mon message d'envoi de password crypté en Md5 :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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);
    	}
    }
    Le probleme ? et bien le serveur postgreSQL ne me renvoie rien ! n'a pas non plus l'air de reconnaitre mon message.

    Si quelqu'un voudrais m'aider je suis preneur car la je bloque pas mal !
    Merci d'avance

  2. #2
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 961
    Points
    4 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 961
    Points : 4 391
    Par défaut
    Citation Envoyé par Fooshi Voir le message
    Bonjour,
    je suis en train de développer un client postgreSQL en C (sans bibliothèque).
    je souhaite apres que j'ai recu un demande d'authentification en md5, crypter mon mot de passe en Md5. Postgresql ne demandant pas un hashage en une passe je me suis basé sur le code que fournis la libpq (lib C pour postgresql, j'en ai déterminé ce code qui me construis mon message d'envoi de password crypté en Md5 :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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);
    	}
    }
    Le probleme ? et bien le serveur postgreSQL ne me renvoie rien ! n'a pas non plus l'air de reconnaitre mon message.

    Si quelqu'un voudrais m'aider je suis preneur car la je bloque pas mal !
    Merci d'avance
    hash sur la concatenation de PASSWORD et USERNAME (dans cet ordre…)
    la concaténation de "md5" et le résultat du premier hash est ce qui est stocké dans pg_shadow

  3. #3
    Membre averti Avatar de Fooshi
    Homme Profil pro
    ICD
    Inscrit en
    Juin 2002
    Messages
    508
    Points
    364
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : ICD
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 508
    Points : 364
    Par défaut
    oais je vois ... j'en deduis donc :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    /// Crypt the password to MD5.
    int
    sql_build_password_md5 ( PGconn * sql_conn )
    {
    	char * crypt_pwd;
     
            crypt_pwd  = malloc(MD5_PASSWORD_LEN + 1);
    	sql_md5_encrypt(sql_conn->pgpass, sql_conn->pguser, strlen(sql_conn->pguser), crypt_pwd);
     
    	// Ici j'ai aucune idée de la taille de ma chaine complète ! j'ai mis 19 qui est taille(password) + taille(user) + md5 a verifier
    	sql_send_msg(sql_conn, 'p', 19, 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;
    }
    Mais ca ne fonctionne pas non plus ! le serveur postegreSQL ne reconnais pas le message valide !
    la taille du hash se calcule comment ? ici j'ai mis 19 ( taille password + taille user + taille md5 sans hash )

  4. #4
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 961
    Points
    4 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 961
    Points : 4 391
    Par défaut
    Citation Envoyé par Fooshi Voir le message
    oais je vois ... j'en deduis donc :

    Mais ca ne fonctionne pas non plus ! le serveur postegreSQL ne reconnais pas le message valide !
    la taille du hash se calcule comment ? ici j'ai mis 19 ( taille password + taille user + taille md5 sans hash )
    un hash md5 fait toujours 32 caractères
    la string "md5" + le hash md5 fera donc toujours 35 caractères (+ le '\0' == 36 bytes)

  5. #5
    Membre averti Avatar de Fooshi
    Homme Profil pro
    ICD
    Inscrit en
    Juin 2002
    Messages
    508
    Points
    364
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : ICD
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 508
    Points : 364
    Par défaut
    Chez moi le hash md5 renvoie une chaine de 16 octets.
    de plus sur la doc :

    MD2(), MD4(), and MD5() compute the MD2, MD4, and MD5 message digest of the n bytes at d and place it in md (which must have space for MD2_DIGEST_LENGTH == MD4_DIGEST_LENGTH == MD5_DIGEST_LENGTH == 16 bytes of output). If md is NULL, the digest is placed in a static array.
    ci joins ma fonction de hash pour voir si elle est juste :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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);
    	}
    }
    EDIT : Le serveur me répond maintenant mais me renvoie une erreur :

    SFATAL C28000 Mpassword authentication failed

  6. #6
    Membre averti Avatar de Fooshi
    Homme Profil pro
    ICD
    Inscrit en
    Juin 2002
    Messages
    508
    Points
    364
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : ICD
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 508
    Points : 364
    Par défaut
    Problème résolu en utilisant les fonctions de hash MD5 de la libpq et non celle d'Open SSL !

  7. #7
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 961
    Points
    4 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 961
    Points : 4 391
    Par défaut
    Citation Envoyé par Fooshi Voir le message
    Chez moi le hash md5 renvoie une chaine de 16 octets.
    :
    oui, j'aurais dû être plus précis…
    16 octets représentés en hexadécimal par 32 caractères…

    si vous stockez sous forme de char() c'est bien de 32 caractères dont vous avez besoin… mais en bytea 16 octets suffisent…

Discussions similaires

  1. Algo pour Cryptage Md5
    Par ..::snake::.. dans le forum Algorithmes et structures de données
    Réponses: 15
    Dernier message: 25/05/2007, 01h46
  2. Pas de DNS pour les clients
    Par M.Dlb dans le forum Réseau
    Réponses: 2
    Dernier message: 06/07/2004, 00h06
  3. Choix port pour application client-serveur
    Par Tiaps dans le forum Développement
    Réponses: 7
    Dernier message: 15/03/2004, 10h49
  4. Réponses: 2
    Dernier message: 31/08/2002, 22h37
  5. Langage le mieux adapté pour application client serveur ?
    Par guenus dans le forum Débats sur le développement - Le Best Of
    Réponses: 4
    Dernier message: 17/06/2002, 16h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo