Coverage Report

Created: 2023-12-09 06:14

/src/dropbear/src/svr-kex.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Dropbear - a SSH2 server
3
 * 
4
 * Copyright (c) 2002,2003 Matt Johnston
5
 * Copyright (c) 2004 by Mihnea Stoenescu
6
 * All rights reserved.
7
 * 
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 * 
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 * 
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
 * SOFTWARE. */
25
26
#include "includes.h"
27
#include "dbutil.h"
28
#include "algo.h"
29
#include "buffer.h"
30
#include "session.h"
31
#include "kex.h"
32
#include "ssh.h"
33
#include "packet.h"
34
#include "bignum.h"
35
#include "dbrandom.h"
36
#include "runopts.h"
37
#include "ecc.h"
38
#include "gensignkey.h"
39
40
static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs);
41
#if DROPBEAR_EXT_INFO
42
static void send_msg_ext_info(void);
43
#endif
44
45
/* Handle a diffie-hellman key exchange initialisation. This involves
46
 * calculating a session key reply value, and corresponding hash. These
47
 * are carried out by send_msg_kexdh_reply(). recv_msg_kexdh_init() calls
48
 * that function, then brings the new keys into use */
49
38.0k
void recv_msg_kexdh_init() {
50
38.0k
  DEF_MP_INT(dh_e);
51
38.0k
  buffer *ecdh_qs = NULL;
52
53
38.0k
  TRACE(("enter recv_msg_kexdh_init"))
54
38.0k
  if (!ses.kexstate.recvkexinit) {
55
17
    dropbear_exit("Premature kexdh_init message received");
56
17
  }
57
58
37.9k
  switch (ses.newkeys->algo_kex->mode) {
59
0
#if DROPBEAR_NORMAL_DH
60
18.9k
    case DROPBEAR_KEX_NORMAL_DH:
61
18.9k
      m_mp_init(&dh_e);
62
18.9k
      if (buf_getmpint(ses.payload, &dh_e) != DROPBEAR_SUCCESS) {
63
131
        dropbear_exit("Bad kex value");
64
131
      }
65
18.8k
      break;
66
18.8k
#endif
67
18.8k
#if DROPBEAR_ECDH
68
18.8k
    case DROPBEAR_KEX_ECDH:
69
6.98k
#endif
70
6.98k
#if DROPBEAR_CURVE25519
71
19.0k
    case DROPBEAR_KEX_CURVE25519:
72
19.0k
#endif
73
19.0k
#if DROPBEAR_ECDH || DROPBEAR_CURVE25519
74
19.0k
      ecdh_qs = buf_getstringbuf(ses.payload);
75
19.0k
      break;
76
37.9k
#endif
77
37.9k
  }
78
37.6k
  if (ses.payload->pos != ses.payload->len) {
79
184
    dropbear_exit("Bad kex value");
80
184
  }
81
82
37.4k
  send_msg_kexdh_reply(&dh_e, ecdh_qs);
83
84
37.4k
  mp_clear(&dh_e);
85
37.4k
  if (ecdh_qs) {
86
18.5k
    buf_free(ecdh_qs);
87
18.5k
    ecdh_qs = NULL;
88
18.5k
  }
89
90
37.4k
  send_msg_newkeys();
91
92
37.4k
#if DROPBEAR_EXT_INFO
93
  /* Only send it following the first newkeys */
94
37.4k
  if (!ses.kexstate.donesecondkex && ses.allow_ext_info) {
95
201
    send_msg_ext_info();
96
201
  }
97
37.4k
#endif
98
99
37.4k
  ses.requirenext = SSH_MSG_NEWKEYS;
100
37.4k
  TRACE(("leave recv_msg_kexdh_init"))
101
37.4k
}
102
103
104
#if DROPBEAR_DELAY_HOSTKEY
105
106
0
static void svr_ensure_hostkey() {
107
108
0
  const char* fn = NULL;
109
0
  char *expand_fn = NULL;
110
0
  enum signkey_type type = ses.newkeys->algo_hostkey;
111
0
  void **hostkey = signkey_key_ptr(svr_opts.hostkey, type);
112
0
  int ret = DROPBEAR_FAILURE;
113
114
0
  if (hostkey && *hostkey) {
115
0
    return;
116
0
  }
117
118
0
  switch (type)
119
0
  {
120
0
#if DROPBEAR_RSA
121
0
    case DROPBEAR_SIGNKEY_RSA:
122
0
      fn = RSA_PRIV_FILENAME;
123
0
      break;
124
0
#endif
125
0
#if DROPBEAR_DSS
126
0
    case DROPBEAR_SIGNKEY_DSS:
127
0
      fn = DSS_PRIV_FILENAME;
128
0
      break;
129
0
#endif
130
0
#if DROPBEAR_ECDSA
131
0
    case DROPBEAR_SIGNKEY_ECDSA_NISTP256:
132
0
    case DROPBEAR_SIGNKEY_ECDSA_NISTP384:
133
0
    case DROPBEAR_SIGNKEY_ECDSA_NISTP521:
134
0
      fn = ECDSA_PRIV_FILENAME;
135
0
      break;
136
0
#endif
137
0
#if DROPBEAR_ED25519
138
0
    case DROPBEAR_SIGNKEY_ED25519:
139
0
      fn = ED25519_PRIV_FILENAME;
140
0
      break;
141
0
#endif
142
0
    default:
143
0
      dropbear_assert(0);
144
0
  }
145
146
0
  expand_fn = expand_homedir_path(fn);
147
148
0
  ret = readhostkey(expand_fn, svr_opts.hostkey, &type);
149
0
  if (ret == DROPBEAR_SUCCESS) {
150
0
    goto out;
151
0
  }
152
153
0
  if (signkey_generate(type, 0, expand_fn, 1) == DROPBEAR_FAILURE) {
154
0
    goto out;
155
0
  }
156
  
157
  /* Read what we just generated (or another process raced us) */
158
0
  ret = readhostkey(expand_fn, svr_opts.hostkey, &type);
159
160
0
  if (ret == DROPBEAR_SUCCESS) {
161
0
    char *fp = NULL;
162
0
    unsigned int len;
163
0
    buffer *key_buf = buf_new(MAX_PUBKEY_SIZE);
164
0
    buf_put_pub_key(key_buf, svr_opts.hostkey, type);
165
0
    buf_setpos(key_buf, 4);
166
0
    len = key_buf->len - key_buf->pos;
167
0
    fp = sign_key_fingerprint(buf_getptr(key_buf, len), len);
168
0
    dropbear_log(LOG_INFO, "Generated hostkey %s, fingerprint is %s",
169
0
      expand_fn, fp);
170
0
    m_free(fp);
171
0
    buf_free(key_buf);
172
0
  }
173
174
0
out:
175
0
  if (ret == DROPBEAR_FAILURE) {
176
0
    dropbear_exit("Couldn't read or generate hostkey %s", expand_fn);
177
0
  }
178
0
    m_free(expand_fn);
179
0
}
180
#endif
181
  
182
/* Generate our side of the diffie-hellman key exchange value (dh_f), and
183
 * calculate the session key using the diffie-hellman algorithm. Following
184
 * that, the session hash is calculated, and signed with RSA or DSS. The
185
 * result is sent to the client. 
186
 *
187
 * See the transport RFC4253 section 8 for details
188
 * or RFC5656 section 4 for elliptic curve variant. */
189
37.4k
static void send_msg_kexdh_reply(mp_int *dh_e, buffer *ecdh_qs) {
190
37.4k
  TRACE(("enter send_msg_kexdh_reply"))
191
192
  /* we can start creating the kexdh_reply packet */
193
37.4k
  CHECKCLEARTOWRITE();
194
195
37.4k
#if DROPBEAR_DELAY_HOSTKEY
196
37.4k
  if (svr_opts.delay_hostkey)
197
0
  {
198
0
    svr_ensure_hostkey();
199
0
  }
200
37.4k
#endif
201
202
37.4k
#if DROPBEAR_FUZZ
203
37.4k
  if (fuzz.fuzzing && fuzz.skip_kexmaths) {
204
19.0k
    fuzz_fake_send_kexdh_reply();
205
19.0k
    return;
206
19.0k
  }
207
18.4k
#endif
208
209
18.4k
  buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_REPLY);
210
18.4k
  buf_put_pub_key(ses.writepayload, svr_opts.hostkey,
211
18.4k
      ses.newkeys->algo_hostkey);
212
213
18.4k
  switch (ses.newkeys->algo_kex->mode) {
214
0
#if DROPBEAR_NORMAL_DH
215
10.2k
    case DROPBEAR_KEX_NORMAL_DH:
216
10.2k
      {
217
10.2k
      struct kex_dh_param * dh_param = gen_kexdh_param();
218
10.2k
      kexdh_comb_key(dh_param, dh_e, svr_opts.hostkey);
219
220
      /* put f */
221
10.2k
      buf_putmpint(ses.writepayload, &dh_param->pub);
222
10.2k
      free_kexdh_param(dh_param);
223
10.2k
      }
224
10.2k
      break;
225
0
#endif
226
0
#if DROPBEAR_ECDH
227
308
    case DROPBEAR_KEX_ECDH:
228
308
      {
229
308
      struct kex_ecdh_param *ecdh_param = gen_kexecdh_param();
230
308
      kexecdh_comb_key(ecdh_param, ecdh_qs, svr_opts.hostkey);
231
232
308
      buf_put_ecc_raw_pubkey_string(ses.writepayload, &ecdh_param->key);
233
308
      free_kexecdh_param(ecdh_param);
234
308
      }
235
308
      break;
236
0
#endif
237
0
#if DROPBEAR_CURVE25519
238
7.87k
    case DROPBEAR_KEX_CURVE25519:
239
7.87k
      {
240
7.87k
      struct kex_curve25519_param *param = gen_kexcurve25519_param();
241
7.87k
      kexcurve25519_comb_key(param, ecdh_qs, svr_opts.hostkey);
242
243
7.87k
      buf_putstring(ses.writepayload, param->pub, CURVE25519_LEN);
244
7.87k
      free_kexcurve25519_param(param);
245
7.87k
      }
246
7.87k
      break;
247
18.4k
#endif
248
18.4k
  }
249
250
  /* calc the signature */
251
18.0k
  buf_put_sign(ses.writepayload, svr_opts.hostkey, 
252
18.0k
      ses.newkeys->algo_signature, ses.hash);
253
254
  /* the SSH_MSG_KEXDH_REPLY is done */
255
18.0k
  encrypt_packet();
256
257
18.0k
  TRACE(("leave send_msg_kexdh_reply"))
258
18.0k
}
259
260
#if DROPBEAR_EXT_INFO
261
/* Only used for server-sig-algs on the server side */
262
201
static void send_msg_ext_info(void) {
263
201
  TRACE(("enter send_msg_ext_info"))
264
265
201
  buf_putbyte(ses.writepayload, SSH_MSG_EXT_INFO);
266
  /* nr-extensions */
267
201
  buf_putint(ses.writepayload, 1);
268
269
201
  buf_putstring(ses.writepayload, SSH_SERVER_SIG_ALGS, strlen(SSH_SERVER_SIG_ALGS));
270
201
  buf_put_algolist_all(ses.writepayload, sigalgs, 1);
271
  
272
201
  encrypt_packet();
273
274
201
  TRACE(("leave send_msg_ext_info"))
275
201
}
276
#endif