1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-08-07 08:02:55 +03:00

dh: Add support for diffie-hellman-group14-sha1.

This commit is contained in:
Dmitriy Kuznetsov
2012-09-04 16:16:15 +02:00
committed by Andreas Schneider
parent 977951e96a
commit e689375e45
7 changed files with 92 additions and 22 deletions

View File

@@ -49,6 +49,8 @@
enum ssh_key_exchange_e { enum ssh_key_exchange_e {
/* diffie-hellman-group1-sha1 */ /* diffie-hellman-group1-sha1 */
SSH_KEX_DH_GROUP1_SHA1=1, SSH_KEX_DH_GROUP1_SHA1=1,
/* diffie-hellman-group14-sha1 */
SSH_KEX_DH_GROUP14_SHA1,
/* ecdh-sha2-nistp256 */ /* ecdh-sha2-nistp256 */
SSH_KEX_ECDH_SHA2_NISTP256 SSH_KEX_ECDH_SHA2_NISTP256
}; };

View File

@@ -194,6 +194,7 @@ static int dh_handshake(ssh_session session) {
case DH_STATE_INIT: case DH_STATE_INIT:
switch(session->next_crypto->kex_type){ switch(session->next_crypto->kex_type){
case SSH_KEX_DH_GROUP1_SHA1: case SSH_KEX_DH_GROUP1_SHA1:
case SSH_KEX_DH_GROUP14_SHA1:
rc = ssh_client_dh_init(session); rc = ssh_client_dh_init(session);
break; break;
#ifdef HAVE_ECDH #ifdef HAVE_ECDH

101
src/dh.c
View File

@@ -5,6 +5,7 @@
* *
* Copyright (c) 2003-2008 by Aris Adamantiadis * Copyright (c) 2003-2008 by Aris Adamantiadis
* Copyright (c) 2009 by Andreas Schneider <mail@cynapses.org> * Copyright (c) 2009 by Andreas Schneider <mail@cynapses.org>
* Copyright (c) 2012 by Dmitriy Kuznetsov <dk@yandex.ru>
* *
* The SSH Library is free software; you can redistribute it and/or modify * The SSH Library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
@@ -67,7 +68,7 @@
#include <openssl/err.h> #include <openssl/err.h>
#endif #endif
static unsigned char p_value[] = { static unsigned char p_group1_value[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2,
0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, 0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6, 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6,
@@ -79,13 +80,45 @@ static unsigned char p_value[] = {
0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5, 0xAE, 0x9F, 0x24, 0x11, 0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5, 0xAE, 0x9F, 0x24, 0x11,
0x7C, 0x4B, 0x1F, 0xE6, 0x49, 0x28, 0x66, 0x51, 0xEC, 0xE6, 0x53, 0x81, 0x7C, 0x4B, 0x1F, 0xE6, 0x49, 0x28, 0x66, 0x51, 0xEC, 0xE6, 0x53, 0x81,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
#define P_LEN 128 /* Size in bytes of the p number */ #define P_GROUP1_LEN 128 /* Size in bytes of the p number */
static unsigned char p_group14_value[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2,
0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
0x29, 0x02, 0x4E, 0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0xF0, 0x6F, 0x4C, 0x52, 0xC9,
0xDE, 0x2B, 0xCB, 0xF6, 0x95, 0x58, 0x17, 0x18, 0x39, 0x95, 0x49, 0x7C,
0xEA, 0x95, 0x6A, 0xE5, 0x15, 0xD2, 0x26, 0x18, 0x98, 0xFA, 0x05, 0x10,
0x15, 0x72, 0x8E, 0x5A, 0x8A, 0xAC, 0xAA, 0x68, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF};
#define P_GROUP14_LEN 256 /* Size in bytes of the p number for group 14 */
static unsigned long g_int = 2 ; /* G is defined as 2 by the ssh2 standards */ static unsigned long g_int = 2 ; /* G is defined as 2 by the ssh2 standards */
static bignum g; static bignum g;
static bignum p; static bignum p_group1;
static bignum p_group14;
static int ssh_crypto_initialized; static int ssh_crypto_initialized;
static bignum select_p(enum ssh_key_exchange_e type) {
return type == SSH_KEX_DH_GROUP14_SHA1 ? p_group14 : p_group1;
}
int ssh_get_random(void *where, int len, int strong){ int ssh_get_random(void *where, int len, int strong){
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
@@ -107,6 +140,7 @@ int ssh_get_random(void *where, int len, int strong){
return 1; return 1;
} }
/* /*
* This inits the values g and p which are used for DH key agreement * This inits the values g and p which are used for DH key agreement
* FIXME: Make the function thread safe by adding a semaphore or mutex. * FIXME: Make the function thread safe by adding a semaphore or mutex.
@@ -128,20 +162,40 @@ int ssh_crypto_init(void) {
bignum_set_word(g,g_int); bignum_set_word(g,g_int);
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
bignum_bin2bn(p_value, P_LEN, &p); bignum_bin2bn(p_group1_value, P_GROUP1_LEN, &p_group1);
if (p == NULL) { if (p_group1 == NULL) {
bignum_free(g); bignum_free(g);
g = NULL; g = NULL;
return -1; return -1;
} }
bignum_bin2bn(p_group14_value, P_GROUP14_LEN, &p_group14);
if (p_group1 == NULL) {
bignum_free(g);
bignum_free(p_group1);
g = NULL;
p_group1 = NULL;
return -1;
}
#elif defined HAVE_LIBCRYPTO #elif defined HAVE_LIBCRYPTO
p = bignum_new(); p_group1 = bignum_new();
if (p == NULL) { if (p_group1 == NULL) {
bignum_free(g); bignum_free(g);
g = NULL; g = NULL;
return -1; return -1;
} }
bignum_bin2bn(p_value, P_LEN, p); bignum_bin2bn(p_group1_value, P_GROUP1_LEN, p_group1);
p_group14 = bignum_new();
if (p_group14 == NULL) {
bignum_free(g);
bignum_free(p_group1);
g = NULL;
p_group1 = NULL;
return -1;
}
bignum_bin2bn(p_group14_value, P_GROUP14_LEN, p_group14);
OpenSSL_add_all_algorithms(); OpenSSL_add_all_algorithms();
#endif #endif
@@ -156,8 +210,10 @@ void ssh_crypto_finalize(void) {
if (ssh_crypto_initialized) { if (ssh_crypto_initialized) {
bignum_free(g); bignum_free(g);
g = NULL; g = NULL;
bignum_free(p); bignum_free(p_group1);
p = NULL; p_group1 = NULL;
bignum_free(p_group14);
p_group14 = NULL;
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
gcry_control(GCRYCTL_TERM_SECMEM); gcry_control(GCRYCTL_TERM_SECMEM);
#elif defined HAVE_LIBCRYPTO #elif defined HAVE_LIBCRYPTO
@@ -295,9 +351,11 @@ int dh_generate_e(ssh_session session) {
} }
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
bignum_mod_exp(session->next_crypto->e, g, session->next_crypto->x, p); bignum_mod_exp(session->next_crypto->e, g, session->next_crypto->x,
select_p(type));
#elif defined HAVE_LIBCRYPTO #elif defined HAVE_LIBCRYPTO
bignum_mod_exp(session->next_crypto->e, g, session->next_crypto->x, p, ctx); bignum_mod_exp(session->next_crypto->e, g, session->next_crypto->x,
select_p(session->next_crypto->kex_type), ctx);
#endif #endif
#ifdef DEBUG_CRYPTO #ifdef DEBUG_CRYPTO
@@ -328,9 +386,11 @@ int dh_generate_f(ssh_session session) {
} }
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
bignum_mod_exp(session->next_crypto->f, g, session->next_crypto->y, p); bignum_mod_exp(session->next_crypto->f, g, session->next_crypto->y,
select_p(session->next_crypto->kex_type));
#elif defined HAVE_LIBCRYPTO #elif defined HAVE_LIBCRYPTO
bignum_mod_exp(session->next_crypto->f, g, session->next_crypto->y, p, ctx); bignum_mod_exp(session->next_crypto->f, g, session->next_crypto->y,
select_p(session->next_crypto->kex_type), ctx);
#endif #endif
#ifdef DEBUG_CRYPTO #ifdef DEBUG_CRYPTO
@@ -460,18 +520,18 @@ int dh_build_k(ssh_session session) {
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
if(session->client) { if(session->client) {
bignum_mod_exp(session->next_crypto->k, session->next_crypto->f, bignum_mod_exp(session->next_crypto->k, session->next_crypto->f,
session->next_crypto->x, p); session->next_crypto->x, select_p(session->next_crypto->kex_type));
} else { } else {
bignum_mod_exp(session->next_crypto->k, session->next_crypto->e, bignum_mod_exp(session->next_crypto->k, session->next_crypto->e,
session->next_crypto->y, p); session->next_crypto->y, select_p(session->next_crypto->kex_type));
} }
#elif defined HAVE_LIBCRYPTO #elif defined HAVE_LIBCRYPTO
if (session->client) { if (session->client) {
bignum_mod_exp(session->next_crypto->k, session->next_crypto->f, bignum_mod_exp(session->next_crypto->k, session->next_crypto->f,
session->next_crypto->x, p, ctx); session->next_crypto->x, select_p(session->next_crypto->kex_type), ctx);
} else { } else {
bignum_mod_exp(session->next_crypto->k, session->next_crypto->e, bignum_mod_exp(session->next_crypto->k, session->next_crypto->e,
session->next_crypto->y, p, ctx); session->next_crypto->y, select_p(session->next_crypto->kex_type), ctx);
} }
#endif #endif
@@ -668,7 +728,9 @@ int make_sessionid(ssh_session session) {
if (buffer_add_data(buf, session->next_crypto->server_pubkey, len) < 0) { if (buffer_add_data(buf, session->next_crypto->server_pubkey, len) < 0) {
goto error; goto error;
} }
if(session->next_crypto->kex_type == SSH_KEX_DH_GROUP1_SHA1){ if(session->next_crypto->kex_type == SSH_KEX_DH_GROUP1_SHA1 ||
session->next_crypto->kex_type == SSH_KEX_DH_GROUP14_SHA1) {
num = make_bignum_string(session->next_crypto->e); num = make_bignum_string(session->next_crypto->e);
if (num == NULL) { if (num == NULL) {
goto error; goto error;
@@ -718,6 +780,7 @@ int make_sessionid(ssh_session session) {
switch(session->next_crypto->kex_type){ switch(session->next_crypto->kex_type){
case SSH_KEX_DH_GROUP1_SHA1: case SSH_KEX_DH_GROUP1_SHA1:
case SSH_KEX_DH_GROUP14_SHA1:
session->next_crypto->digest_len = SHA_DIGEST_LENGTH; session->next_crypto->digest_len = SHA_DIGEST_LENGTH;
session->next_crypto->mac_type = SSH_MAC_SHA1; session->next_crypto->mac_type = SSH_MAC_SHA1;
session->next_crypto->session_id = malloc(session->next_crypto->digest_len); session->next_crypto->session_id = malloc(session->next_crypto->digest_len);

View File

@@ -65,10 +65,10 @@
#endif #endif
#ifdef HAVE_ECDH #ifdef HAVE_ECDH
#define KEY_EXCHANGE "ecdh-sha2-nistp256,diffie-hellman-group1-sha1" #define KEY_EXCHANGE "ecdh-sha2-nistp256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1"
#define HOSTKEYS "ecdsa-sha2-nistp256,ssh-rsa,ssh-dss" #define HOSTKEYS "ecdsa-sha2-nistp256,ssh-rsa,ssh-dss"
#else #else
#define KEY_EXCHANGE "diffie-hellman-group1-sha1" #define KEY_EXCHANGE "diffie-hellman-group14-sha1,diffie-hellman-group1-sha1"
#define HOSTKEYS "ssh-rsa,ssh-dss" #define HOSTKEYS "ssh-rsa,ssh-dss"
#endif #endif
@@ -393,6 +393,8 @@ int ssh_kex_select_methods (ssh_session session){
} }
if(strcmp(session->next_crypto->kex_methods[SSH_KEX], "diffie-hellman-group1-sha1") == 0){ if(strcmp(session->next_crypto->kex_methods[SSH_KEX], "diffie-hellman-group1-sha1") == 0){
session->next_crypto->kex_type=SSH_KEX_DH_GROUP1_SHA1; session->next_crypto->kex_type=SSH_KEX_DH_GROUP1_SHA1;
} else if(strcmp(session->next_crypto->kex_methods[SSH_KEX], "diffie-hellman-group14-sha1") == 0){
session->next_crypto->kex_type=SSH_KEX_DH_GROUP14_SHA1;
} else if(strcmp(session->next_crypto->kex_methods[SSH_KEX], "ecdh-sha2-nistp256") == 0){ } else if(strcmp(session->next_crypto->kex_methods[SSH_KEX], "ecdh-sha2-nistp256") == 0){
session->next_crypto->kex_type=SSH_KEX_ECDH_SHA2_NISTP256; session->next_crypto->kex_type=SSH_KEX_ECDH_SHA2_NISTP256;
} }

View File

@@ -332,7 +332,7 @@ int ssh_options_set_algo(ssh_session session, int algo,
* - SSH_OPTIONS_KEY_EXCHANGE: * - SSH_OPTIONS_KEY_EXCHANGE:
* Set the key exchange method to be used (const char *, * Set the key exchange method to be used (const char *,
* comma-separated list). ex: * comma-separated list). ex:
* "ecdh-sha2-nistp256,diffie-hellman-group1-sha1" * "ecdh-sha2-nistp256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1"
* *
* - SSH_OPTIONS_HOSTKEYS: * - SSH_OPTIONS_HOSTKEYS:
* Set the preferred server host key types (const char *, * Set the preferred server host key types (const char *,

View File

@@ -94,6 +94,7 @@ SSH_PACKET_CALLBACK(ssh_packet_dh_reply){
} }
switch(session->next_crypto->kex_type){ switch(session->next_crypto->kex_type){
case SSH_KEX_DH_GROUP1_SHA1: case SSH_KEX_DH_GROUP1_SHA1:
case SSH_KEX_DH_GROUP14_SHA1:
rc=ssh_client_dh_reply(session, packet); rc=ssh_client_dh_reply(session, packet);
break; break;
#ifdef HAVE_ECDH #ifdef HAVE_ECDH

View File

@@ -177,6 +177,7 @@ SSH_PACKET_CALLBACK(ssh_packet_kexdh_init){
} }
switch(session->next_crypto->kex_type){ switch(session->next_crypto->kex_type){
case SSH_KEX_DH_GROUP1_SHA1: case SSH_KEX_DH_GROUP1_SHA1:
case SSH_KEX_DH_GROUP14_SHA1:
rc=ssh_server_kexdh_init(session, packet); rc=ssh_server_kexdh_init(session, packet);
break; break;
#ifdef HAVE_ECDH #ifdef HAVE_ECDH