Coverage Report

Created: 2025-11-16 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libssh/src/ecdh.c
Line
Count
Source
1
/*
2
 * This file is part of the SSH Library
3
 *
4
 * Copyright (c) 2011-2013 by Aris Adamantiadis
5
 *
6
 * The SSH Library is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation; either version 2.1 of the License, or (at your
9
 * option) any later version.
10
 *
11
 * The SSH Library is distributed in the hope that it will be useful, but
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14
 * License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with the SSH Library; see the file COPYING.  If not, write to
18
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19
 * MA 02111-1307, USA.
20
 */
21
22
#include "config.h"
23
#include "libssh/session.h"
24
#include "libssh/ecdh.h"
25
#include "libssh/dh.h"
26
#include "libssh/buffer.h"
27
#include "libssh/ssh2.h"
28
#include "libssh/pki.h"
29
#include "libssh/bignum.h"
30
31
#ifdef HAVE_ECDH
32
33
static SSH_PACKET_CALLBACK(ssh_packet_client_ecdh_reply);
34
35
static ssh_packet_callback ecdh_client_callbacks[]= {
36
    ssh_packet_client_ecdh_reply
37
};
38
39
struct ssh_packet_callbacks_struct ssh_ecdh_client_callbacks = {
40
    .start = SSH2_MSG_KEX_ECDH_REPLY,
41
    .n_callbacks = 1,
42
    .callbacks = ecdh_client_callbacks,
43
    .user = NULL
44
};
45
46
void ssh_client_ecdh_remove_callbacks(ssh_session session)
47
2
{
48
2
    ssh_packet_remove_callbacks(session, &ssh_ecdh_client_callbacks);
49
2
}
50
51
/** @internal
52
 * @brief parses a SSH_MSG_KEX_ECDH_REPLY packet and sends back
53
 * a SSH_MSG_NEWKEYS
54
 */
55
2
SSH_PACKET_CALLBACK(ssh_packet_client_ecdh_reply){
56
2
  ssh_string q_s_string = NULL;
57
2
  ssh_string pubkey_blob = NULL;
58
2
  ssh_string signature = NULL;
59
2
  int rc;
60
2
  (void)type;
61
2
  (void)user;
62
63
2
  ssh_client_ecdh_remove_callbacks(session);
64
2
  pubkey_blob = ssh_buffer_get_ssh_string(packet);
65
2
  if (pubkey_blob == NULL) {
66
1
    ssh_set_error(session,SSH_FATAL, "No public key in packet");
67
1
    goto error;
68
1
  }
69
70
1
  rc = ssh_dh_import_next_pubkey_blob(session, pubkey_blob);
71
1
  SSH_STRING_FREE(pubkey_blob);
72
1
  if (rc != 0) {
73
1
      goto error;
74
1
  }
75
76
0
  q_s_string = ssh_buffer_get_ssh_string(packet);
77
0
  if (q_s_string == NULL) {
78
0
    ssh_set_error(session,SSH_FATAL, "No Q_S ECC point in packet");
79
0
    goto error;
80
0
  }
81
0
  session->next_crypto->ecdh_server_pubkey = q_s_string;
82
0
  signature = ssh_buffer_get_ssh_string(packet);
83
0
  if (signature == NULL) {
84
0
    ssh_set_error(session, SSH_FATAL, "No signature in packet");
85
0
    goto error;
86
0
  }
87
0
  session->next_crypto->dh_server_signature = signature;
88
0
  signature=NULL; /* ownership changed */
89
  /* TODO: verify signature now instead of waiting for NEWKEYS */
90
0
  if (ecdh_build_k(session) < 0) {
91
0
    ssh_set_error(session, SSH_FATAL, "Cannot build k number");
92
0
    goto error;
93
0
  }
94
95
  /* Send the MSG_NEWKEYS */
96
0
  rc = ssh_packet_send_newkeys(session);
97
0
  if (rc == SSH_ERROR) {
98
0
    goto error;
99
0
  }
100
0
  session->dh_handshake_state = DH_STATE_NEWKEYS_SENT;
101
102
0
  return SSH_PACKET_USED;
103
104
2
error:
105
2
  session->session_state=SSH_SESSION_STATE_ERROR;
106
2
  return SSH_PACKET_USED;
107
0
}
108
109
#ifdef WITH_SERVER
110
111
static ssh_packet_callback ecdh_server_callbacks[] = {
112
    ssh_packet_server_ecdh_init
113
};
114
115
struct ssh_packet_callbacks_struct ssh_ecdh_server_callbacks = {
116
    .start = SSH2_MSG_KEX_ECDH_INIT,
117
    .n_callbacks = 1,
118
    .callbacks = ecdh_server_callbacks,
119
    .user = NULL
120
};
121
122
/** @internal
123
 * @brief sets up the ecdh kex callbacks
124
 */
125
0
void ssh_server_ecdh_init(ssh_session session){
126
0
    ssh_packet_set_callbacks(session, &ssh_ecdh_server_callbacks);
127
0
}
128
129
#endif /* WITH_SERVER */
130
#endif /* HAVE_ECDH */