Line | Count | Source |
1 | | /* $OpenBSD: ssh_api.c,v 1.35 2026/07/27 12:28:52 markus Exp $ */ |
2 | | /* |
3 | | * Copyright (c) 2012 Markus Friedl. All rights reserved. |
4 | | * |
5 | | * Permission to use, copy, modify, and distribute this software for any |
6 | | * purpose with or without fee is hereby granted, provided that the above |
7 | | * copyright notice and this permission notice appear in all copies. |
8 | | * |
9 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
10 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
11 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
12 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
13 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
14 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
15 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
16 | | */ |
17 | | |
18 | | #include "includes.h" |
19 | | |
20 | | #include <sys/types.h> |
21 | | |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | |
25 | | #include "ssh_api.h" |
26 | | #include "compat.h" |
27 | | #include "log.h" |
28 | | #include "authfile.h" |
29 | | #include "sshkey.h" |
30 | | #include "dh.h" |
31 | | #include "misc.h" |
32 | | #include "ssh2.h" |
33 | | #include "version.h" |
34 | | #include "myproposal.h" |
35 | | #include "ssherr.h" |
36 | | #include "sshbuf.h" |
37 | | |
38 | | #include "openbsd-compat/openssl-compat.h" |
39 | | |
40 | | #include <string.h> |
41 | | |
42 | | int _ssh_exchange_banner(struct ssh *); |
43 | | int _ssh_send_banner(struct ssh *, struct sshbuf *); |
44 | | int _ssh_read_banner(struct ssh *, struct sshbuf *); |
45 | | int _ssh_order_hostkeyalgs(struct ssh *); |
46 | | int _ssh_verify_host_key(struct sshkey *, struct ssh *); |
47 | | struct sshkey *_ssh_host_public_key(int, int, struct ssh *); |
48 | | struct sshkey *_ssh_host_private_key(int, int, struct ssh *); |
49 | | int _ssh_host_key_sign(struct ssh *, struct sshkey *, struct sshkey *, |
50 | | u_char **, size_t *, const u_char *, size_t, const char *); |
51 | | |
52 | | /* |
53 | | * stubs for privsep calls in the server side implementation of kex. |
54 | | */ |
55 | | int mm_sshkey_sign(struct sshkey *, u_char **, u_int *, |
56 | | const u_char *, u_int, const char *, const char *, const char *, u_int); |
57 | | |
58 | | #ifdef WITH_OPENSSL |
59 | | DH *mm_choose_dh(int, int, int); |
60 | | #endif |
61 | | |
62 | | int |
63 | | mm_sshkey_sign(struct sshkey *key, u_char **sigp, u_int *lenp, |
64 | | const u_char *data, u_int datalen, const char *alg, |
65 | | const char *sk_provider, const char *sk_pin, u_int compat) |
66 | 0 | { |
67 | 0 | size_t slen = 0; |
68 | 0 | int ret; |
69 | |
|
70 | 0 | ret = sshkey_sign(key, sigp, &slen, data, datalen, alg, |
71 | 0 | sk_provider, sk_pin, compat); |
72 | 0 | *lenp = slen; |
73 | 0 | return ret; |
74 | 0 | } |
75 | | |
76 | | #ifdef WITH_OPENSSL |
77 | | DH * |
78 | | mm_choose_dh(int min, int nbits, int max) |
79 | 0 | { |
80 | 0 | return choose_dh(min, nbits, max); |
81 | 0 | } |
82 | | #endif |
83 | | |
84 | | /* API */ |
85 | | |
86 | | int |
87 | | ssh_init(struct ssh **sshp, int is_server, struct kex_params *kex_params) |
88 | 2 | { |
89 | 2 | char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT }; |
90 | 2 | char *populated[PROPOSAL_MAX]; |
91 | 2 | struct ssh *ssh; |
92 | 2 | char **proposal; |
93 | 2 | static int called; |
94 | 2 | int r; |
95 | | |
96 | 2 | if (!called) { |
97 | 1 | seed_rng(); |
98 | 1 | called = 1; |
99 | 1 | } |
100 | | |
101 | 2 | if ((ssh = ssh_packet_set_connection(NULL, -1, -1)) == NULL) |
102 | 0 | return SSH_ERR_ALLOC_FAIL; |
103 | 2 | if (is_server) |
104 | 1 | ssh_packet_set_server(ssh); |
105 | | |
106 | | /* Initialize key exchange */ |
107 | 2 | proposal = kex_params ? kex_params->proposal : myproposal; |
108 | 2 | kex_proposal_populate_entries(ssh, populated, |
109 | 2 | proposal[PROPOSAL_KEX_ALGS], |
110 | 2 | proposal[PROPOSAL_ENC_ALGS_CTOS], |
111 | 2 | proposal[PROPOSAL_MAC_ALGS_CTOS], |
112 | 2 | proposal[PROPOSAL_COMP_ALGS_CTOS], |
113 | 2 | proposal[PROPOSAL_SERVER_HOST_KEY_ALGS]); |
114 | 2 | r = kex_ready(ssh, populated); |
115 | 2 | kex_proposal_free_entries(populated); |
116 | 2 | if (r != 0) { |
117 | 0 | ssh_free(ssh); |
118 | 0 | return r; |
119 | 0 | } |
120 | | |
121 | 2 | ssh->kex->server = is_server; |
122 | 2 | if (is_server) { |
123 | 1 | #ifdef WITH_OPENSSL |
124 | 1 | ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server; |
125 | 1 | ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server; |
126 | 1 | ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server; |
127 | 1 | ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server; |
128 | 1 | ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server; |
129 | 1 | ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; |
130 | 1 | ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_server; |
131 | 1 | ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_server; |
132 | 1 | #endif /* WITH_OPENSSL */ |
133 | 1 | ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_server; |
134 | 1 | ssh->kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_server; |
135 | 1 | ssh->kex->kex[KEX_KEM_MLKEM768X25519_SHA256] = kex_gen_server; |
136 | 1 | ssh->kex->kex[KEX_KEM_MLKEM768ECDH_SHA256] = kex_gen_server; |
137 | 1 | ssh->kex->load_host_public_key=&_ssh_host_public_key; |
138 | 1 | ssh->kex->load_host_private_key=&_ssh_host_private_key; |
139 | 1 | ssh->kex->sign=&_ssh_host_key_sign; |
140 | 1 | } else { |
141 | 1 | #ifdef WITH_OPENSSL |
142 | 1 | ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client; |
143 | 1 | ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client; |
144 | 1 | ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client; |
145 | 1 | ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client; |
146 | 1 | ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client; |
147 | 1 | ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; |
148 | 1 | ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; |
149 | 1 | ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client; |
150 | 1 | #endif /* WITH_OPENSSL */ |
151 | 1 | ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client; |
152 | 1 | ssh->kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_client; |
153 | 1 | ssh->kex->kex[KEX_KEM_MLKEM768X25519_SHA256] = kex_gen_client; |
154 | 1 | ssh->kex->kex[KEX_KEM_MLKEM768ECDH_SHA256] = kex_gen_client; |
155 | 1 | ssh->kex->verify_host_key =&_ssh_verify_host_key; |
156 | 1 | } |
157 | 2 | *sshp = ssh; |
158 | 2 | return 0; |
159 | 2 | } |
160 | | |
161 | | void |
162 | | ssh_free(struct ssh *ssh) |
163 | 0 | { |
164 | 0 | struct key_entry *k; |
165 | |
|
166 | 0 | if (ssh == NULL) |
167 | 0 | return; |
168 | | |
169 | | /* |
170 | | * we've only created the public keys variants in case we |
171 | | * are a acting as a server. |
172 | | */ |
173 | 0 | while ((k = TAILQ_FIRST(&ssh->public_keys)) != NULL) { |
174 | 0 | TAILQ_REMOVE(&ssh->public_keys, k, next); |
175 | 0 | if (ssh->kex && ssh->kex->server) |
176 | 0 | sshkey_free(k->key); |
177 | 0 | free(k); |
178 | 0 | } |
179 | 0 | while ((k = TAILQ_FIRST(&ssh->private_keys)) != NULL) { |
180 | 0 | TAILQ_REMOVE(&ssh->private_keys, k, next); |
181 | 0 | free(k); |
182 | 0 | } |
183 | 0 | ssh_packet_close(ssh); |
184 | 0 | free(ssh); |
185 | 0 | } |
186 | | |
187 | | void |
188 | | ssh_set_app_data(struct ssh *ssh, void *app_data) |
189 | 0 | { |
190 | 0 | ssh->app_data = app_data; |
191 | 0 | } |
192 | | |
193 | | void * |
194 | | ssh_get_app_data(struct ssh *ssh) |
195 | 0 | { |
196 | 0 | return ssh->app_data; |
197 | 0 | } |
198 | | |
199 | | /* Returns < 0 on error, 0 otherwise */ |
200 | | int |
201 | | ssh_add_hostkey(struct ssh *ssh, struct sshkey *key) |
202 | 2 | { |
203 | 2 | struct sshkey *pubkey = NULL; |
204 | 2 | struct key_entry *k = NULL, *k_prv = NULL; |
205 | 2 | int r; |
206 | | |
207 | 2 | if (ssh->kex->server) { |
208 | 1 | if ((r = sshkey_from_private(key, &pubkey)) != 0) |
209 | 0 | return r; |
210 | 1 | if ((k = malloc(sizeof(*k))) == NULL || |
211 | 1 | (k_prv = malloc(sizeof(*k_prv))) == NULL) { |
212 | 0 | free(k); |
213 | 0 | sshkey_free(pubkey); |
214 | 0 | return SSH_ERR_ALLOC_FAIL; |
215 | 0 | } |
216 | 1 | k_prv->key = key; |
217 | 1 | TAILQ_INSERT_TAIL(&ssh->private_keys, k_prv, next); |
218 | | |
219 | | /* add the public key, too */ |
220 | 1 | k->key = pubkey; |
221 | 1 | TAILQ_INSERT_TAIL(&ssh->public_keys, k, next); |
222 | 1 | r = 0; |
223 | 1 | } else { |
224 | 1 | if ((k = malloc(sizeof(*k))) == NULL) |
225 | 0 | return SSH_ERR_ALLOC_FAIL; |
226 | 1 | k->key = key; |
227 | 1 | TAILQ_INSERT_TAIL(&ssh->public_keys, k, next); |
228 | 1 | r = 0; |
229 | 1 | } |
230 | | |
231 | 2 | return r; |
232 | 2 | } |
233 | | |
234 | | int |
235 | | ssh_set_verify_host_key_callback(struct ssh *ssh, |
236 | | int (*cb)(struct sshkey *, struct ssh *)) |
237 | 0 | { |
238 | 0 | if (cb == NULL || ssh->kex == NULL) |
239 | 0 | return SSH_ERR_INVALID_ARGUMENT; |
240 | | |
241 | 0 | ssh->kex->verify_host_key = cb; |
242 | |
|
243 | 0 | return 0; |
244 | 0 | } |
245 | | |
246 | | int |
247 | | ssh_input_append(struct ssh *ssh, const u_char *data, size_t len) |
248 | 1.35k | { |
249 | 1.35k | return sshbuf_put(ssh_packet_get_input(ssh), data, len); |
250 | 1.35k | } |
251 | | |
252 | | int |
253 | | ssh_packet_next(struct ssh *ssh, u_char *typep) |
254 | 1.35k | { |
255 | 1.35k | int r; |
256 | 1.35k | uint32_t seqnr; |
257 | 1.35k | u_char type; |
258 | | |
259 | | /* |
260 | | * Try to read a packet. Return SSH_MSG_NONE if no packet or not |
261 | | * enough data. |
262 | | */ |
263 | 1.35k | *typep = SSH_MSG_NONE; |
264 | 1.35k | if (sshbuf_len(ssh->kex->client_version) == 0 || |
265 | 1.35k | sshbuf_len(ssh->kex->server_version) == 0) |
266 | 3 | return _ssh_exchange_banner(ssh); |
267 | | /* |
268 | | * If we enough data and a dispatch function then |
269 | | * call the function and get the next packet. |
270 | | * Otherwise return the packet type to the caller so it |
271 | | * can decide how to go on. |
272 | | * |
273 | | * We will only call the dispatch function for: |
274 | | * 20-29 Algorithm negotiation |
275 | | * 30-49 Key exchange method specific (numbers can be reused for |
276 | | * different authentication methods) |
277 | | */ |
278 | 1.35k | for (;;) { |
279 | 1.35k | if ((r = ssh_packet_read_poll2(ssh, &type, &seqnr)) != 0) |
280 | 0 | return r; |
281 | 1.35k | if (type > 0 && type < DISPATCH_MAX && |
282 | 8 | type >= SSH2_MSG_KEXINIT && type <= SSH2_MSG_TRANSPORT_MAX && |
283 | 6 | ssh->dispatch[type] != NULL) { |
284 | 6 | if ((r = (*ssh->dispatch[type])(type, seqnr, ssh)) != 0) |
285 | 0 | return r; |
286 | 1.35k | } else { |
287 | 1.35k | *typep = type; |
288 | 1.35k | return 0; |
289 | 1.35k | } |
290 | 1.35k | } |
291 | 1.35k | } |
292 | | |
293 | | const u_char * |
294 | | ssh_packet_payload(struct ssh *ssh, size_t *lenp) |
295 | 0 | { |
296 | 0 | return sshpkt_ptr(ssh, lenp); |
297 | 0 | } |
298 | | |
299 | | int |
300 | | ssh_packet_put(struct ssh *ssh, int type, const u_char *data, size_t len) |
301 | 1.34k | { |
302 | 1.34k | int r; |
303 | | |
304 | 1.34k | if ((r = sshpkt_start(ssh, type)) != 0 || |
305 | 1.34k | (r = sshpkt_put(ssh, data, len)) != 0 || |
306 | 1.34k | (r = sshpkt_send(ssh)) != 0) |
307 | 0 | return r; |
308 | 1.34k | return 0; |
309 | 1.34k | } |
310 | | |
311 | | const u_char * |
312 | | ssh_output_ptr(struct ssh *ssh, size_t *len) |
313 | 1.35k | { |
314 | 1.35k | struct sshbuf *output = ssh_packet_get_output(ssh); |
315 | | |
316 | 1.35k | *len = sshbuf_len(output); |
317 | 1.35k | return sshbuf_ptr(output); |
318 | 1.35k | } |
319 | | |
320 | | int |
321 | | ssh_output_consume(struct ssh *ssh, size_t len) |
322 | 1.35k | { |
323 | 1.35k | return sshbuf_consume(ssh_packet_get_output(ssh), len); |
324 | 1.35k | } |
325 | | |
326 | | int |
327 | | ssh_output_space(struct ssh *ssh, size_t len) |
328 | 0 | { |
329 | 0 | return (0 == sshbuf_check_reserve(ssh_packet_get_output(ssh), len)); |
330 | 0 | } |
331 | | |
332 | | int |
333 | | ssh_input_space(struct ssh *ssh, size_t len) |
334 | 0 | { |
335 | 0 | return (0 == sshbuf_check_reserve(ssh_packet_get_input(ssh), len)); |
336 | 0 | } |
337 | | |
338 | | /* Read other side's version identification. */ |
339 | | int |
340 | | _ssh_read_banner(struct ssh *ssh, struct sshbuf *banner) |
341 | 3 | { |
342 | 3 | struct sshbuf *input = ssh_packet_get_input(ssh); |
343 | 3 | const char *mismatch = "Protocol mismatch.\r\n"; |
344 | 3 | const u_char *s = sshbuf_ptr(input); |
345 | 3 | u_char c; |
346 | 3 | char *cp = NULL, *remote_version = NULL; |
347 | 3 | int r = 0, remote_major, remote_minor, expect_nl; |
348 | 3 | size_t n, j; |
349 | | |
350 | 3 | for (j = n = 0;;) { |
351 | 3 | sshbuf_reset(banner); |
352 | 3 | expect_nl = 0; |
353 | 45 | for (;;) { |
354 | 45 | if (j >= sshbuf_len(input)) |
355 | 1 | return 0; /* insufficient data in input buf */ |
356 | 44 | c = s[j++]; |
357 | 44 | if (c == '\r') { |
358 | 2 | expect_nl = 1; |
359 | 2 | continue; |
360 | 2 | } |
361 | 42 | if (c == '\n') |
362 | 2 | break; |
363 | 40 | if (expect_nl) |
364 | 0 | goto bad; |
365 | 40 | if ((r = sshbuf_put_u8(banner, c)) != 0) |
366 | 0 | return r; |
367 | 40 | if (sshbuf_len(banner) > SSH_MAX_BANNER_LEN) |
368 | 0 | goto bad; |
369 | 40 | } |
370 | 2 | if (sshbuf_len(banner) >= 4 && |
371 | 2 | memcmp(sshbuf_ptr(banner), "SSH-", 4) == 0) |
372 | 2 | break; |
373 | 0 | debug_f("%.*s", (int)sshbuf_len(banner), |
374 | 0 | sshbuf_ptr(banner)); |
375 | | /* Accept lines before banner only on client */ |
376 | 0 | if (ssh->kex->server || ++n > SSH_MAX_PRE_BANNER_LINES) { |
377 | 0 | bad: |
378 | 0 | if ((r = sshbuf_put(ssh_packet_get_output(ssh), |
379 | 0 | mismatch, strlen(mismatch))) != 0) |
380 | 0 | return r; |
381 | 0 | return SSH_ERR_NO_PROTOCOL_VERSION; |
382 | 0 | } |
383 | 0 | } |
384 | 2 | if ((r = sshbuf_consume(input, j)) != 0) |
385 | 0 | return r; |
386 | | |
387 | | /* XXX remote version must be the same size as banner for sscanf */ |
388 | 2 | if ((cp = sshbuf_dup_string(banner)) == NULL || |
389 | 2 | (remote_version = calloc(1, sshbuf_len(banner))) == NULL) { |
390 | 0 | r = SSH_ERR_ALLOC_FAIL; |
391 | 0 | goto out; |
392 | 0 | } |
393 | | |
394 | | /* |
395 | | * Check that the versions match. In future this might accept |
396 | | * several versions and set appropriate flags to handle them. |
397 | | */ |
398 | 2 | if (sscanf(cp, "SSH-%d.%d-%[^\n]\n", |
399 | 2 | &remote_major, &remote_minor, remote_version) != 3) { |
400 | 0 | r = SSH_ERR_INVALID_FORMAT; |
401 | 0 | goto out; |
402 | 0 | } |
403 | 2 | debug("Remote protocol version %d.%d, remote software version %.100s", |
404 | 2 | remote_major, remote_minor, remote_version); |
405 | | |
406 | 2 | compat_banner(ssh, remote_version); |
407 | 2 | if (remote_major == 1 && remote_minor == 99) { |
408 | 0 | remote_major = 2; |
409 | 0 | remote_minor = 0; |
410 | 0 | } |
411 | 2 | if (remote_major != 2) |
412 | 0 | r = SSH_ERR_PROTOCOL_MISMATCH; |
413 | | |
414 | 2 | debug("Remote version string %.100s", cp); |
415 | 2 | out: |
416 | 2 | free(cp); |
417 | 2 | free(remote_version); |
418 | 2 | return r; |
419 | 2 | } |
420 | | |
421 | | /* Send our own protocol version identification. */ |
422 | | int |
423 | | _ssh_send_banner(struct ssh *ssh, struct sshbuf *banner) |
424 | 2 | { |
425 | 2 | char *cp; |
426 | 2 | int r; |
427 | | |
428 | 2 | if ((r = sshbuf_putf(banner, "SSH-2.0-%.100s\r\n", SSH_VERSION)) != 0) |
429 | 0 | return r; |
430 | 2 | if ((r = sshbuf_putb(ssh_packet_get_output(ssh), banner)) != 0) |
431 | 0 | return r; |
432 | | /* Remove trailing \r\n */ |
433 | 2 | if ((r = sshbuf_consume_end(banner, 2)) != 0) |
434 | 0 | return r; |
435 | 2 | if ((cp = sshbuf_dup_string(banner)) == NULL) |
436 | 0 | return SSH_ERR_ALLOC_FAIL; |
437 | 2 | debug("Local version string %.100s", cp); |
438 | 2 | free(cp); |
439 | 2 | return 0; |
440 | 2 | } |
441 | | |
442 | | int |
443 | | _ssh_exchange_banner(struct ssh *ssh) |
444 | 3 | { |
445 | 3 | struct kex *kex = ssh->kex; |
446 | 3 | int r; |
447 | | |
448 | | /* |
449 | | * if _ssh_read_banner() cannot parse a full version string |
450 | | * it will return NULL and we end up calling it again. |
451 | | */ |
452 | | |
453 | 3 | r = 0; |
454 | 3 | if (kex->server) { |
455 | 2 | if (sshbuf_len(ssh->kex->server_version) == 0) |
456 | 1 | r = _ssh_send_banner(ssh, ssh->kex->server_version); |
457 | 2 | if (r == 0 && |
458 | 2 | sshbuf_len(ssh->kex->server_version) != 0 && |
459 | 2 | sshbuf_len(ssh->kex->client_version) == 0) |
460 | 2 | r = _ssh_read_banner(ssh, ssh->kex->client_version); |
461 | 2 | } else { |
462 | 1 | if (sshbuf_len(ssh->kex->server_version) == 0) |
463 | 1 | r = _ssh_read_banner(ssh, ssh->kex->server_version); |
464 | 1 | if (r == 0 && |
465 | 1 | sshbuf_len(ssh->kex->server_version) != 0 && |
466 | 1 | sshbuf_len(ssh->kex->client_version) == 0) |
467 | 1 | r = _ssh_send_banner(ssh, ssh->kex->client_version); |
468 | 1 | } |
469 | 3 | if (r != 0) |
470 | 0 | return r; |
471 | | /* start initial kex as soon as we have exchanged the banners */ |
472 | 3 | if (sshbuf_len(ssh->kex->server_version) != 0 && |
473 | 3 | sshbuf_len(ssh->kex->client_version) != 0) { |
474 | 2 | if ((r = _ssh_order_hostkeyalgs(ssh)) != 0 || |
475 | 2 | (r = kex_send_kexinit(ssh)) != 0) |
476 | 0 | return r; |
477 | 2 | } |
478 | 3 | return 0; |
479 | 3 | } |
480 | | |
481 | | struct sshkey * |
482 | | _ssh_host_public_key(int type, int nid, struct ssh *ssh) |
483 | 1 | { |
484 | 1 | struct key_entry *k; |
485 | | |
486 | 1 | debug3_f("need %d", type); |
487 | 1 | TAILQ_FOREACH(k, &ssh->public_keys, next) { |
488 | 1 | debug3_f("check %s", sshkey_type(k->key)); |
489 | 1 | if (k->key->type == type && |
490 | 1 | (type != KEY_ECDSA || k->key->ecdsa_nid == nid)) |
491 | 1 | return (k->key); |
492 | 1 | } |
493 | 0 | return (NULL); |
494 | 1 | } |
495 | | |
496 | | struct sshkey * |
497 | | _ssh_host_private_key(int type, int nid, struct ssh *ssh) |
498 | 1 | { |
499 | 1 | struct key_entry *k; |
500 | | |
501 | 1 | debug3_f("need %d", type); |
502 | 1 | TAILQ_FOREACH(k, &ssh->private_keys, next) { |
503 | 1 | debug3_f("check %s", sshkey_type(k->key)); |
504 | 1 | if (k->key->type == type && |
505 | 1 | (type != KEY_ECDSA || k->key->ecdsa_nid == nid)) |
506 | 1 | return (k->key); |
507 | 1 | } |
508 | 0 | return (NULL); |
509 | 1 | } |
510 | | |
511 | | int |
512 | | _ssh_verify_host_key(struct sshkey *hostkey, struct ssh *ssh) |
513 | 1 | { |
514 | 1 | struct key_entry *k; |
515 | | |
516 | 1 | debug3_f("need %s", sshkey_type(hostkey)); |
517 | 1 | TAILQ_FOREACH(k, &ssh->public_keys, next) { |
518 | 1 | debug3_f("check %s", sshkey_type(k->key)); |
519 | 1 | if (sshkey_equal_public(hostkey, k->key)) |
520 | 1 | return (0); /* ok */ |
521 | 1 | } |
522 | 0 | return (-1); /* failed */ |
523 | 1 | } |
524 | | |
525 | | /* offer hostkey algorithms in kexinit depending on registered keys */ |
526 | | int |
527 | | _ssh_order_hostkeyalgs(struct ssh *ssh) |
528 | 2 | { |
529 | 2 | struct key_entry *k; |
530 | 2 | char *orig, *avail, *oavail = NULL, *alg, *replace = NULL; |
531 | 2 | char **proposal; |
532 | 2 | size_t maxlen; |
533 | 2 | int ktype, nid, r; |
534 | | |
535 | | /* XXX we de-serialize ssh->kex->my, modify it, and change it */ |
536 | 2 | if ((r = kex_buf2prop(ssh->kex->my, NULL, &proposal)) != 0) |
537 | 0 | return r; |
538 | 2 | orig = proposal[PROPOSAL_SERVER_HOST_KEY_ALGS]; |
539 | 2 | if ((oavail = avail = strdup(orig)) == NULL) { |
540 | 0 | r = SSH_ERR_ALLOC_FAIL; |
541 | 0 | goto out; |
542 | 0 | } |
543 | 2 | maxlen = strlen(avail) + 1; |
544 | 2 | if ((replace = calloc(1, maxlen)) == NULL) { |
545 | 0 | r = SSH_ERR_ALLOC_FAIL; |
546 | 0 | goto out; |
547 | 0 | } |
548 | 2 | *replace = '\0'; |
549 | 4 | while ((alg = strsep(&avail, ",")) && *alg != '\0') { |
550 | 2 | if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC) |
551 | 0 | continue; |
552 | 2 | nid = sshkey_ecdsa_nid_from_name(alg); |
553 | 2 | TAILQ_FOREACH(k, &ssh->public_keys, next) { |
554 | 2 | if (k->key->type != ktype && |
555 | 0 | (!sshkey_is_cert(k->key) || |
556 | 0 | k->key->type != sshkey_type_plain(ktype))) |
557 | 0 | continue; |
558 | 2 | if (sshkey_type_plain(k->key->type) == KEY_ECDSA && |
559 | 0 | k->key->ecdsa_nid != nid) |
560 | 0 | continue; |
561 | | /* Candidate */ |
562 | 2 | if (*replace != '\0') |
563 | 0 | strlcat(replace, ",", maxlen); |
564 | 2 | strlcat(replace, alg, maxlen); |
565 | 2 | break; |
566 | 2 | } |
567 | 2 | } |
568 | 2 | if (*replace != '\0') { |
569 | 2 | debug2_f("orig/%d %s", ssh->kex->server, orig); |
570 | 2 | debug2_f("replace/%d %s", ssh->kex->server, replace); |
571 | 2 | free(orig); |
572 | 2 | proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = replace; |
573 | 2 | replace = NULL; /* owned by proposal */ |
574 | 2 | r = kex_prop2buf(ssh->kex->my, proposal); |
575 | 2 | } |
576 | 2 | out: |
577 | 2 | free(oavail); |
578 | 2 | free(replace); |
579 | 2 | kex_prop_free(proposal); |
580 | 2 | return r; |
581 | 2 | } |
582 | | |
583 | | int |
584 | | _ssh_host_key_sign(struct ssh *ssh, struct sshkey *privkey, |
585 | | struct sshkey *pubkey, u_char **signature, size_t *slen, |
586 | | const u_char *data, size_t dlen, const char *alg) |
587 | 1 | { |
588 | 1 | return sshkey_sign(privkey, signature, slen, data, dlen, |
589 | 1 | alg, NULL, NULL, ssh->compat); |
590 | 1 | } |