Coverage Report

Created: 2026-06-07 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libssh/src/knownhosts.c
Line
Count
Source
1
/*
2
 * known_hosts: Host and public key verification.
3
 *
4
 * This file is part of the SSH Library
5
 *
6
 * Copyright (c) 2003-2009 by Aris Adamantiadis
7
 * Copyright (c) 2009-2017 by Andreas Schneider <asn@cryptomilk.org>
8
 *
9
 * The SSH Library is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU Lesser General Public License as published by
11
 * the Free Software Foundation; either version 2.1 of the License, or (at your
12
 * option) any later version.
13
 *
14
 * The SSH Library is distributed in the hope that it will be useful, but
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17
 * License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with the SSH Library; see the file COPYING.  If not, write to
21
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22
 * MA 02111-1307, USA.
23
 */
24
25
#include "config.h"
26
27
#include <ctype.h>
28
#include <errno.h>
29
#include <stdio.h>
30
#include <stdlib.h>
31
32
#ifndef _WIN32
33
#include <arpa/inet.h>
34
#include <netinet/in.h>
35
#endif
36
37
#include "libssh/priv.h"
38
#include "libssh/dh.h"
39
#include "libssh/session.h"
40
#include "libssh/options.h"
41
#include "libssh/misc.h"
42
#include "libssh/pki.h"
43
#include "libssh/dh.h"
44
#include "libssh/knownhosts.h"
45
#include "libssh/token.h"
46
47
#ifndef MAX_LINE_SIZE
48
#define MAX_LINE_SIZE 8192
49
#endif
50
51
/**
52
 * @addtogroup libssh_session
53
 *
54
 * @{
55
 */
56
57
static int hash_hostname(const char *name,
58
                         unsigned char *salt,
59
                         unsigned int salt_size,
60
                         unsigned char **hash,
61
                         size_t *hash_size)
62
0
{
63
0
    int rc;
64
0
    HMACCTX mac_ctx = NULL;
65
66
0
    mac_ctx = hmac_init(salt, salt_size, SSH_HMAC_SHA1);
67
0
    if (mac_ctx == NULL) {
68
0
        return SSH_ERROR;
69
0
    }
70
71
0
    rc = hmac_update(mac_ctx, name, strlen(name));
72
0
    if (rc != 1)
73
0
        return SSH_ERROR;
74
75
0
    rc = hmac_final(mac_ctx, *hash, hash_size);
76
0
    if (rc != 1)
77
0
        return SSH_ERROR;
78
79
0
    return SSH_OK;
80
0
}
81
82
static int match_hashed_hostname(const char *host, const char *hashed_host)
83
0
{
84
0
    char *hashed = NULL;
85
0
    char *b64_hash = NULL;
86
0
    ssh_buffer salt = NULL;
87
0
    ssh_buffer hash = NULL;
88
0
    unsigned char hashed_buf[256] = {0};
89
0
    unsigned char *hashed_buf_ptr = hashed_buf;
90
0
    size_t hashed_buf_size = sizeof(hashed_buf);
91
0
    int cmp;
92
0
    int rc;
93
0
    int match = 0;
94
95
0
    cmp = strncmp(hashed_host, "|1|", 3);
96
0
    if (cmp != 0) {
97
0
        return 0;
98
0
    }
99
100
0
    hashed = strdup(hashed_host + 3);
101
0
    if (hashed == NULL) {
102
0
        return 0;
103
0
    }
104
105
0
    b64_hash = strchr(hashed, '|');
106
0
    if (b64_hash == NULL) {
107
0
        goto error;
108
0
    }
109
0
    *b64_hash = '\0';
110
0
    b64_hash++;
111
112
0
    salt = base64_to_bin(hashed);
113
0
    if (salt == NULL) {
114
0
        goto error;
115
0
    }
116
117
0
    hash = base64_to_bin(b64_hash);
118
0
    if (hash == NULL) {
119
0
        goto error;
120
0
    }
121
122
0
    rc = hash_hostname(host,
123
0
                       ssh_buffer_get(salt),
124
0
                       ssh_buffer_get_len(salt),
125
0
                       &hashed_buf_ptr,
126
0
                       &hashed_buf_size);
127
0
    if (rc != SSH_OK) {
128
0
        goto error;
129
0
    }
130
131
0
    if (hashed_buf_size != ssh_buffer_get_len(hash)) {
132
0
        goto error;
133
0
    }
134
135
0
    cmp = memcmp(hashed_buf, ssh_buffer_get(hash), hashed_buf_size);
136
0
    if (cmp == 0) {
137
0
        match = 1;
138
0
    }
139
140
0
error:
141
0
    free(hashed);
142
0
    SSH_BUFFER_FREE(salt);
143
0
    SSH_BUFFER_FREE(hash);
144
145
0
    return match;
146
0
}
147
148
/**
149
 * @brief Free an allocated ssh_knownhosts_entry.
150
 *
151
 * Use SSH_KNOWNHOSTS_ENTRY_FREE() to set the pointer to NULL.
152
 *
153
 * @param[in]  entry     The entry to free.
154
 */
155
void ssh_knownhosts_entry_free(struct ssh_knownhosts_entry *entry)
156
0
{
157
0
    if (entry == NULL) {
158
0
        return;
159
0
    }
160
161
0
    SAFE_FREE(entry->hostname);
162
0
    SAFE_FREE(entry->unparsed);
163
0
    ssh_key_free(entry->publickey);
164
0
    SAFE_FREE(entry->comment);
165
0
    SAFE_FREE(entry);
166
0
}
167
168
static int known_hosts_read_line(FILE *fp,
169
                                 char *buf,
170
                                 size_t buf_size,
171
                                 size_t *buf_len,
172
                                 size_t *lineno)
173
0
{
174
0
    while (fgets(buf, (int)buf_size, fp) != NULL) {
175
0
        size_t len;
176
0
        if (buf[0] == '\0') {
177
0
            continue;
178
0
        }
179
180
0
        *lineno += 1;
181
0
        len = strlen(buf);
182
0
        if (buf_len != NULL) {
183
0
            *buf_len = len;
184
0
        }
185
0
        if (buf[len - 1] == '\n' || feof(fp)) {
186
0
            return 0;
187
0
        } else {
188
0
            errno = E2BIG;
189
0
            return -1;
190
0
        }
191
0
    }
192
193
0
    return -1;
194
0
}
195
196
static int
197
ssh_known_hosts_entries_compare(struct ssh_knownhosts_entry *k1,
198
                                struct ssh_knownhosts_entry *k2)
199
0
{
200
0
    int cmp;
201
202
0
    if (k1 == NULL || k2 == NULL) {
203
0
        return 1;
204
0
    }
205
206
0
    cmp = strcmp(k1->hostname, k2->hostname);
207
0
    if (cmp != 0) {
208
0
        return cmp;
209
0
    }
210
211
0
    cmp = ssh_key_cmp(k1->publickey, k2->publickey, SSH_KEY_CMP_PUBLIC);
212
0
    if (cmp != 0) {
213
0
        return cmp;
214
0
    }
215
216
0
    return 0;
217
0
}
218
219
/**
220
 * @internal
221
 *
222
 * @brief Read entries from filename to provided list
223
 *
224
 * This method reads the known_hosts file referenced by the path
225
 * in  filename  argument, and entries matching the  match  argument
226
 * will be added to the list in  entries  argument.
227
 * If the  entries  list is NULL, it will allocate a new list. Caller
228
 * is responsible to free it even if an error occurs.
229
 *
230
 * @param match[in]       The host name (with port) to match against
231
 * @param filename[in]    The known hosts file to parse
232
 * @param entries[in,out] The list of entries to append matching ones
233
 * @return                `SSH_OK` on missing file or success parsing,
234
 *                        `SSH_ERROR` on error
235
 */
236
static int ssh_known_hosts_read_entries(const char *match,
237
                                        const char *filename,
238
                                        struct ssh_list **entries)
239
0
{
240
0
    char line[MAX_LINE_SIZE];
241
0
    size_t lineno = 0;
242
0
    size_t len = 0;
243
0
    FILE *fp = NULL;
244
0
    int rc;
245
246
0
    fp = ssh_strict_fopen(filename, SSH_MAX_CONFIG_FILE_SIZE);
247
0
    if (fp == NULL) {
248
0
        SSH_LOG_STRERROR(SSH_LOG_TRACE,
249
0
                         errno,
250
0
                         "Failed to open the known_hosts file '%s': %s",
251
0
                         filename);
252
        /* The missing file is not an error here */
253
0
        return SSH_OK;
254
0
    }
255
256
0
    if (*entries == NULL) {
257
0
        *entries = ssh_list_new();
258
0
        if (*entries == NULL) {
259
0
            fclose(fp);
260
0
            return SSH_ERROR;
261
0
        }
262
0
    }
263
264
0
    for (rc = known_hosts_read_line(fp, line, sizeof(line), &len, &lineno);
265
0
         rc == 0;
266
0
         rc = known_hosts_read_line(fp, line, sizeof(line), &len, &lineno)) {
267
0
        struct ssh_knownhosts_entry *entry = NULL;
268
0
        struct ssh_iterator *it = NULL;
269
0
        char *p = NULL;
270
271
0
        if (line[len] != '\n') {
272
0
            len = strcspn(line, "\n");
273
0
        }
274
0
        line[len] = '\0';
275
276
        /* Skip leading spaces */
277
0
        for (p = line; isspace((int)p[0]); p++);
278
279
        /* Skip comments and empty lines */
280
0
        if (p[0] == '\0' || p[0] == '#') {
281
0
            continue;
282
0
        }
283
284
        /* Skip lines starting with markers (@cert-authority, @revoked):
285
         * we do not completely support them anyway */
286
0
        if (p[0] == '@') {
287
0
            continue;
288
0
        }
289
290
0
        rc = ssh_known_hosts_parse_line(match,
291
0
                                        line,
292
0
                                        &entry);
293
0
        if (rc == SSH_AGAIN) {
294
0
            continue;
295
0
        } else if (rc != SSH_OK) {
296
0
            goto error;
297
0
        }
298
299
        /* Check for duplicates */
300
0
        for (it = ssh_list_get_iterator(*entries);
301
0
             it != NULL;
302
0
             it = it->next) {
303
0
            struct ssh_knownhosts_entry *entry2 = NULL;
304
0
            int cmp;
305
0
            entry2 = ssh_iterator_value(struct ssh_knownhosts_entry *, it);
306
0
            cmp = ssh_known_hosts_entries_compare(entry, entry2);
307
0
            if (cmp == 0) {
308
0
                ssh_knownhosts_entry_free(entry);
309
0
                entry = NULL;
310
0
                break;
311
0
            }
312
0
        }
313
0
        if (entry != NULL) {
314
0
            rc = ssh_list_append(*entries, entry);
315
0
            if (rc != SSH_OK) {
316
0
                ssh_knownhosts_entry_free(entry);
317
0
                goto error;
318
0
            }
319
0
        }
320
0
    }
321
322
0
    fclose(fp);
323
0
    return SSH_OK;
324
0
error:
325
0
    fclose(fp);
326
0
    return SSH_ERROR;
327
0
}
328
329
static char *ssh_session_get_host_port(ssh_session session)
330
0
{
331
0
    char *host_port = NULL;
332
0
    char *host = NULL;
333
334
0
    if (session->opts.host == NULL) {
335
0
        ssh_set_error(session,
336
0
                      SSH_FATAL,
337
0
                      "Can't verify server in known hosts if the host we "
338
0
                      "should connect to has not been set");
339
340
0
        return NULL;
341
0
    }
342
343
0
    host = ssh_lowercase(session->opts.host);
344
0
    if (host == NULL) {
345
0
        ssh_set_error_oom(session);
346
0
        return NULL;
347
0
    }
348
349
0
    if (session->opts.port == 0 || session->opts.port == 22) {
350
0
        host_port = host;
351
0
    } else {
352
0
        host_port = ssh_hostport(host, session->opts.port);
353
0
        SAFE_FREE(host);
354
0
        if (host_port == NULL) {
355
0
            ssh_set_error_oom(session);
356
0
            return NULL;
357
0
        }
358
0
    }
359
360
0
    return host_port;
361
0
}
362
363
/**
364
 * @internal
365
 *
366
 * @brief Free known hosts entries list
367
 *
368
 * @param[in] entry_list The list of ssh_knownhosts_entry items
369
 */
370
static void ssh_knownhosts_entries_free(struct ssh_list *entry_list)
371
0
{
372
0
    struct ssh_iterator *it = NULL;
373
374
0
    if (entry_list == NULL) {
375
0
        return;
376
0
    }
377
378
0
    for (it = ssh_list_get_iterator(entry_list);
379
0
         it != NULL;
380
0
         it = ssh_list_get_iterator(entry_list)) {
381
0
        struct ssh_knownhosts_entry *entry = NULL;
382
383
0
        entry = ssh_iterator_value(struct ssh_knownhosts_entry *, it);
384
0
        ssh_knownhosts_entry_free(entry);
385
0
        ssh_list_remove(entry_list, it);
386
0
    }
387
0
    ssh_list_free(entry_list);
388
0
}
389
390
/**
391
 * @internal
392
 *
393
 * @brief Check which host keys should be preferred for the session.
394
 *
395
 * This checks the known_hosts file to find out which algorithms should be
396
 * preferred for the connection we are going to establish.
397
 *
398
 * @param[in]  session  The ssh session to use.
399
 *
400
 * @return A list of supported key types, NULL on error.
401
 */
402
struct ssh_list *ssh_known_hosts_get_algorithms(ssh_session session)
403
0
{
404
0
    struct ssh_list *entry_list = NULL;
405
0
    struct ssh_iterator *it = NULL;
406
0
    char *host_port = NULL;
407
0
    size_t count;
408
0
    struct ssh_list *list = NULL;
409
0
    int list_error = 0;
410
0
    int rc;
411
412
0
    if (session->opts.knownhosts == NULL ||
413
0
        session->opts.global_knownhosts == NULL) {
414
0
        if (ssh_options_apply(session) < 0) {
415
0
            ssh_set_error(session,
416
0
                          SSH_REQUEST_DENIED,
417
0
                          "Can't find a known_hosts file");
418
419
0
            return NULL;
420
0
        }
421
0
    }
422
423
0
    list = ssh_list_new();
424
0
    if (list == NULL) {
425
0
        ssh_set_error_oom(session);
426
0
        return NULL;
427
0
    }
428
429
0
    host_port = ssh_session_get_host_port(session);
430
0
    if (host_port == NULL) {
431
0
        goto error;
432
0
    }
433
434
0
    rc = ssh_known_hosts_read_entries(host_port,
435
0
                                      session->opts.knownhosts,
436
0
                                      &entry_list);
437
0
    if (rc != 0) {
438
0
        SAFE_FREE(host_port);
439
0
        goto error;
440
0
    }
441
442
0
    rc = ssh_known_hosts_read_entries(host_port,
443
0
                                      session->opts.global_knownhosts,
444
0
                                      &entry_list);
445
0
    SAFE_FREE(host_port);
446
0
    if (rc != 0) {
447
0
        goto error;
448
0
    }
449
450
0
    if (entry_list == NULL) {
451
0
        goto error;
452
0
    }
453
454
0
    count = ssh_list_count(entry_list);
455
0
    if (count == 0) {
456
0
        goto error;
457
0
    }
458
459
0
    for (it = ssh_list_get_iterator(entry_list);
460
0
         it != NULL;
461
0
         it = ssh_list_get_iterator(entry_list)) {
462
0
        struct ssh_iterator *it2 = NULL;
463
0
        struct ssh_knownhosts_entry *entry = NULL;
464
0
        const char *algo = NULL;
465
0
        bool present = false;
466
467
0
        entry = ssh_iterator_value(struct ssh_knownhosts_entry *, it);
468
0
        algo = entry->publickey->type_c;
469
470
        /* Check for duplicates */
471
0
        for (it2 = ssh_list_get_iterator(list);
472
0
             it2 != NULL;
473
0
             it2 = it2->next) {
474
0
            char *alg2 = ssh_iterator_value(char *, it2);
475
0
            int cmp = strcmp(alg2, algo);
476
0
            if (cmp == 0) {
477
0
                present = true;
478
0
                break;
479
0
            }
480
0
        }
481
482
        /* Add to the new list only if it is unique */
483
0
        if (!present) {
484
0
            rc = ssh_list_append(list, algo);
485
0
            if (rc != SSH_OK) {
486
0
               list_error = 1;
487
0
            }
488
0
        }
489
490
0
        ssh_knownhosts_entry_free(entry);
491
0
        ssh_list_remove(entry_list, it);
492
0
    }
493
0
    ssh_list_free(entry_list);
494
0
    if (list_error) {
495
0
        goto error;
496
0
    }
497
498
0
    return list;
499
0
error:
500
0
    ssh_knownhosts_entries_free(entry_list);
501
0
    ssh_list_free(list);
502
0
    return NULL;
503
0
}
504
505
/**
506
 * @internal
507
 *
508
 * @brief   Returns a static string containing a list of the signature types the
509
 * given key type can generate.
510
 *
511
 * @returns A static cstring containing the signature types the key is able to
512
 * generate separated by commas; NULL in case of error
513
 */
514
static const char *ssh_known_host_sigs_from_hostkey_type(enum ssh_keytypes_e type)
515
0
{
516
0
    switch (type) {
517
0
    case SSH_KEYTYPE_RSA:
518
0
        return "rsa-sha2-512,rsa-sha2-256,ssh-rsa";
519
0
    case SSH_KEYTYPE_ED25519:
520
0
        return "ssh-ed25519";
521
0
    case SSH_KEYTYPE_SK_ED25519:
522
0
        return "sk-ssh-ed25519@openssh.com";
523
0
#ifdef HAVE_ECC
524
0
    case SSH_KEYTYPE_ECDSA_P256:
525
0
        return "ecdsa-sha2-nistp256";
526
0
    case SSH_KEYTYPE_ECDSA_P384:
527
0
        return "ecdsa-sha2-nistp384";
528
0
    case SSH_KEYTYPE_ECDSA_P521:
529
0
        return "ecdsa-sha2-nistp521";
530
0
    case SSH_KEYTYPE_SK_ECDSA:
531
0
        return "sk-ecdsa-sha2-nistp256@openssh.com";
532
#else
533
    case SSH_KEYTYPE_ECDSA_P256:
534
    case SSH_KEYTYPE_ECDSA_P384:
535
    case SSH_KEYTYPE_ECDSA_P521:
536
        SSH_LOG(SSH_LOG_WARN, "ECDSA keys are not supported by this build");
537
        break;
538
#endif
539
0
    case SSH_KEYTYPE_UNKNOWN:
540
0
    default:
541
0
        SSH_LOG(SSH_LOG_TRACE,
542
0
                "The given type %d is not a base private key type "
543
0
                "or is unsupported",
544
0
                type);
545
0
    }
546
547
0
    return NULL;
548
0
}
549
550
/**
551
 * @internal
552
 *
553
 * @brief Get the host keys algorithms identifiers from the known_hosts files
554
 *
555
 * This expands the signatures types that can be generated from the keys types
556
 * present in the known_hosts files
557
 *
558
 * @param[in]  session  The ssh session to use.
559
 *
560
 * @return A newly allocated cstring containing a list of signature algorithms
561
 * that can be generated by the host using the keys listed in the known_hosts
562
 * files, NULL on error.
563
 */
564
char *ssh_known_hosts_get_algorithms_names(ssh_session session)
565
0
{
566
0
    char methods_buffer[256 + 1] = {0};
567
0
    struct ssh_list *entry_list = NULL;
568
0
    struct ssh_iterator *it = NULL;
569
0
    char *host_port = NULL;
570
0
    size_t count;
571
0
    bool needcomma = false;
572
0
    char *names = NULL;
573
574
0
    int rc;
575
576
0
    if (session->opts.knownhosts == NULL ||
577
0
        session->opts.global_knownhosts == NULL) {
578
0
        if (ssh_options_apply(session) < 0) {
579
0
            ssh_set_error(session,
580
0
                          SSH_REQUEST_DENIED,
581
0
                          "Can't find a known_hosts file");
582
583
0
            return NULL;
584
0
        }
585
0
    }
586
587
0
    host_port = ssh_session_get_host_port(session);
588
0
    if (host_port == NULL) {
589
0
        return NULL;
590
0
    }
591
592
0
    rc = ssh_known_hosts_read_entries(host_port,
593
0
                                      session->opts.knownhosts,
594
0
                                      &entry_list);
595
0
    if (rc != 0) {
596
0
        SAFE_FREE(host_port);
597
0
        ssh_knownhosts_entries_free(entry_list);
598
0
        return NULL;
599
0
    }
600
601
0
    rc = ssh_known_hosts_read_entries(host_port,
602
0
                                      session->opts.global_knownhosts,
603
0
                                      &entry_list);
604
0
    SAFE_FREE(host_port);
605
0
    if (rc != 0) {
606
0
        ssh_knownhosts_entries_free(entry_list);
607
0
        return NULL;
608
0
    }
609
610
0
    if (entry_list == NULL) {
611
0
        return NULL;
612
0
    }
613
614
0
    count = ssh_list_count(entry_list);
615
0
    if (count == 0) {
616
0
        ssh_list_free(entry_list);
617
0
        return NULL;
618
0
    }
619
620
0
    for (it = ssh_list_get_iterator(entry_list);
621
0
         it != NULL;
622
0
         it = ssh_list_get_iterator(entry_list))
623
0
    {
624
0
        struct ssh_knownhosts_entry *entry = NULL;
625
0
        const char *algo = NULL;
626
627
0
        entry = ssh_iterator_value(struct ssh_knownhosts_entry *, it);
628
0
        algo = ssh_known_host_sigs_from_hostkey_type(entry->publickey->type);
629
0
        if (algo == NULL) {
630
0
            ssh_knownhosts_entry_free(entry);
631
0
            ssh_list_remove(entry_list, it);
632
0
            continue;
633
0
        }
634
635
0
        if (needcomma) {
636
0
            strlcat(methods_buffer, ",", sizeof(methods_buffer));
637
0
        }
638
639
0
        strlcat(methods_buffer, algo, sizeof(methods_buffer));
640
0
        needcomma = true;
641
642
0
        ssh_knownhosts_entry_free(entry);
643
0
        ssh_list_remove(entry_list, it);
644
0
    }
645
646
0
    ssh_list_free(entry_list);
647
648
0
    names = ssh_remove_duplicates(methods_buffer);
649
650
0
    return names;
651
0
}
652
653
/**
654
 * @brief Parse a line from a known_hosts entry into a structure
655
 *
656
 * This parses a known_hosts entry into a structure with the key in a libssh
657
 * consumeable form. You can use the PKI key function to further work with it.
658
 *
659
 * @param[in]  hostname     The hostname to match the line to
660
 *
661
 * @param[in]  line         The line to compare and parse if we have a hostname
662
 *                          match.
663
 *
664
 * @param[in]  entry        A pointer to store the allocated known_hosts
665
 *                          entry structure. The user needs to free the memory
666
 *                          using SSH_KNOWNHOSTS_ENTRY_FREE().
667
 *
668
 * @return SSH_OK on success, SSH_ERROR otherwise.
669
 */
670
int ssh_known_hosts_parse_line(const char *hostname,
671
                               const char *line,
672
                               struct ssh_knownhosts_entry **entry)
673
0
{
674
0
    struct ssh_knownhosts_entry *e = NULL;
675
0
    char *known_host = NULL;
676
0
    char *p = NULL;
677
0
    const char *cp = NULL;
678
0
    char *save_tok = NULL;
679
0
    enum ssh_keytypes_e key_type;
680
0
    int match = 0;
681
0
    int rc = SSH_OK;
682
683
0
    known_host = strdup(line);
684
0
    if (known_host == NULL) {
685
0
        return SSH_ERROR;
686
0
    }
687
688
    /* match pattern for hostname or hashed hostname */
689
0
    p = strtok_r(known_host, " ", &save_tok);
690
0
    if (p == NULL ) {
691
0
        free(known_host);
692
0
        return SSH_ERROR;
693
0
    }
694
695
0
    e = calloc(1, sizeof(struct ssh_knownhosts_entry));
696
0
    if (e == NULL) {
697
0
        free(known_host);
698
0
        return SSH_ERROR;
699
0
    }
700
701
0
    if (hostname != NULL) {
702
0
        char *host_port = NULL;
703
0
        char *q = NULL;
704
705
        /* Hashed */
706
0
        if (p[0] == '|') {
707
0
            match = match_hashed_hostname(hostname, p);
708
0
        }
709
710
0
        save_tok = NULL;
711
712
0
        for (q = strtok_r(p, ",", &save_tok);
713
0
             q != NULL;
714
0
             q = strtok_r(NULL, ",", &save_tok)) {
715
0
            int cmp;
716
717
0
            if (q[0] == '[' && hostname[0] != '[') {
718
                /* Corner case: We have standard port so we do not have
719
                 * hostname in square braces. But the pattern is enclosed
720
                 * in braces with, possibly standard or wildcard, port.
721
                 * We need to test against [host]:port pair here.
722
                 */
723
0
                if (host_port == NULL) {
724
0
                    host_port = ssh_hostport(hostname, 22);
725
0
                    if (host_port == NULL) {
726
0
                        rc = SSH_ERROR;
727
0
                        goto out;
728
0
                    }
729
0
                }
730
731
0
                cmp = match_hostname(host_port, q, strlen(q));
732
0
            } else {
733
0
                cmp = match_hostname(hostname, q, strlen(q));
734
0
            }
735
0
            if (cmp == 1) {
736
0
                match = 1;
737
0
                break;
738
0
            }
739
0
        }
740
0
        free(host_port);
741
742
0
        if (match == 0) {
743
0
            rc = SSH_AGAIN;
744
0
            goto out;
745
0
        }
746
747
0
        e->hostname = strdup(hostname);
748
0
        if (e->hostname == NULL) {
749
0
            rc = SSH_ERROR;
750
0
            goto out;
751
0
        }
752
0
    }
753
754
    /* Restart parsing */
755
0
    SAFE_FREE(known_host);
756
0
    known_host = strdup(line);
757
0
    if (known_host == NULL) {
758
0
        rc = SSH_ERROR;
759
0
        goto out;
760
0
    }
761
762
0
    save_tok = NULL;
763
764
0
    p = strtok_r(known_host, " ", &save_tok);
765
0
    if (p == NULL ) {
766
0
        rc = SSH_ERROR;
767
0
        goto out;
768
0
    }
769
770
0
    e->unparsed = strdup(p);
771
0
    if (e->unparsed == NULL) {
772
0
        rc = SSH_ERROR;
773
0
        goto out;
774
0
    }
775
776
    /* pubkey type */
777
0
    p = strtok_r(NULL, " ", &save_tok);
778
0
    if (p == NULL) {
779
0
        rc = SSH_ERROR;
780
0
        goto out;
781
0
    }
782
783
0
    key_type = ssh_key_type_from_name(p);
784
0
    if (key_type == SSH_KEYTYPE_UNKNOWN) {
785
0
        SSH_LOG(SSH_LOG_TRACE, "key type '%s' unknown!", p);
786
0
        rc = SSH_ERROR;
787
0
        goto out;
788
0
    }
789
790
    /* public key */
791
0
    p = strtok_r(NULL, " ", &save_tok);
792
0
    if (p == NULL) {
793
0
        rc = SSH_ERROR;
794
0
        goto out;
795
0
    }
796
797
0
    rc = ssh_pki_import_pubkey_base64(p,
798
0
                                      key_type,
799
0
                                      &e->publickey);
800
0
    if (rc != SSH_OK) {
801
0
        SSH_LOG(SSH_LOG_TRACE,
802
0
                "Failed to parse %s key for entry: %s!",
803
0
                ssh_key_type_to_char(key_type),
804
0
                e->unparsed);
805
0
        goto out;
806
0
    }
807
808
    /* comment */
809
0
    p = strtok_r(NULL, " ", &save_tok);
810
0
    if (p != NULL) {
811
0
        cp = strstr(line, p);
812
0
        if (cp != NULL) {
813
0
            e->comment = strdup(cp);
814
0
            if (e->comment == NULL) {
815
0
                rc = SSH_ERROR;
816
0
                goto out;
817
0
            }
818
0
        }
819
0
    }
820
821
0
    *entry = e;
822
0
    SAFE_FREE(known_host);
823
824
0
    return SSH_OK;
825
0
out:
826
0
    SAFE_FREE(known_host);
827
0
    ssh_knownhosts_entry_free(e);
828
0
    return rc;
829
0
}
830
831
/**
832
 * @brief Check if the set hostname and port match an entry in known_hosts.
833
 *
834
 * This check if the set hostname and port have an entry in the known_hosts file.
835
 * You need to set at least the hostname using ssh_options_set().
836
 *
837
 * @param[in]  session  The session with the values set to check.
838
 *
839
 * @return A ssh_known_hosts_e return value.
840
 */
841
enum ssh_known_hosts_e ssh_session_has_known_hosts_entry(ssh_session session)
842
0
{
843
0
    struct ssh_list *entry_list = NULL;
844
0
    char *host_port = NULL;
845
0
    bool global_known_hosts_found = false;
846
0
    bool known_hosts_found = false;
847
0
    int rc;
848
849
0
    if (session->opts.knownhosts == NULL) {
850
0
        if (ssh_options_apply(session) < 0) {
851
0
            ssh_set_error(session,
852
0
                          SSH_REQUEST_DENIED,
853
0
                          "Cannot find a known_hosts file");
854
855
0
            return SSH_KNOWN_HOSTS_NOT_FOUND;
856
0
        }
857
0
    }
858
859
0
    if (session->opts.knownhosts == NULL &&
860
0
        session->opts.global_knownhosts == NULL) {
861
0
            ssh_set_error(session,
862
0
                          SSH_REQUEST_DENIED,
863
0
                          "No path set for a known_hosts file");
864
865
0
            return SSH_KNOWN_HOSTS_NOT_FOUND;
866
0
    }
867
868
0
    if (session->opts.knownhosts != NULL) {
869
0
        known_hosts_found = ssh_file_readaccess_ok(session->opts.knownhosts);
870
0
        if (!known_hosts_found) {
871
0
            SSH_LOG(SSH_LOG_TRACE, "Cannot access file %s",
872
0
                    session->opts.knownhosts);
873
0
        }
874
0
    }
875
876
0
    if (session->opts.global_knownhosts != NULL) {
877
0
        global_known_hosts_found =
878
0
                ssh_file_readaccess_ok(session->opts.global_knownhosts);
879
0
        if (!global_known_hosts_found) {
880
0
            SSH_LOG(SSH_LOG_TRACE, "Cannot access file %s",
881
0
                    session->opts.global_knownhosts);
882
0
        }
883
0
    }
884
885
0
    if ((!known_hosts_found) && (!global_known_hosts_found)) {
886
0
        ssh_set_error(session,
887
0
                      SSH_REQUEST_DENIED,
888
0
                      "Cannot find a known_hosts file");
889
890
0
        return SSH_KNOWN_HOSTS_NOT_FOUND;
891
0
    }
892
893
0
    host_port = ssh_session_get_host_port(session);
894
0
    if (host_port == NULL) {
895
0
        return SSH_KNOWN_HOSTS_ERROR;
896
0
    }
897
898
0
    if (known_hosts_found) {
899
0
        rc = ssh_known_hosts_read_entries(host_port,
900
0
                                          session->opts.knownhosts,
901
0
                                          &entry_list);
902
0
        if (rc != 0) {
903
0
            SAFE_FREE(host_port);
904
0
            ssh_knownhosts_entries_free(entry_list);
905
0
            return SSH_KNOWN_HOSTS_ERROR;
906
0
        }
907
0
    }
908
909
0
    if (global_known_hosts_found) {
910
0
        rc = ssh_known_hosts_read_entries(host_port,
911
0
                                          session->opts.global_knownhosts,
912
0
                                          &entry_list);
913
0
        if (rc != 0) {
914
0
            SAFE_FREE(host_port);
915
0
            ssh_knownhosts_entries_free(entry_list);
916
0
            return SSH_KNOWN_HOSTS_ERROR;
917
0
        }
918
0
    }
919
920
0
    SAFE_FREE(host_port);
921
922
0
    if (ssh_list_count(entry_list) == 0) {
923
0
        ssh_list_free(entry_list);
924
0
        return SSH_KNOWN_HOSTS_UNKNOWN;
925
0
    }
926
927
0
    ssh_knownhosts_entries_free(entry_list);
928
929
0
    return SSH_KNOWN_HOSTS_OK;
930
0
}
931
932
/**
933
 * @brief Export the current session information to a known_hosts string.
934
 *
935
 * This exports the current information of a session which is connected so a
936
 * ssh server into an entry line which can be added to a known_hosts file.
937
 *
938
 * @param[in]  session  The session with information to export.
939
 *
940
 * @param[in]  pentry_string A pointer to a string to store the allocated
941
 *                           line of the entry. The user must free it using
942
 *                           ssh_string_free_char().
943
 *
944
 * @return SSH_OK on success, SSH_ERROR otherwise.
945
 */
946
int ssh_session_export_known_hosts_entry(ssh_session session,
947
                                         char **pentry_string)
948
0
{
949
0
    ssh_key server_pubkey = NULL;
950
0
    char *host = NULL;
951
0
    char entry_buf[MAX_LINE_SIZE] = {0};
952
0
    char *b64_key = NULL;
953
0
    int rc;
954
955
0
    if (pentry_string == NULL) {
956
0
        ssh_set_error_invalid(session);
957
0
        return SSH_ERROR;
958
0
    }
959
960
0
    if (session->opts.host == NULL) {
961
0
        ssh_set_error(session, SSH_FATAL,
962
0
                      "Can't create known_hosts entry - hostname unknown");
963
0
        return SSH_ERROR;
964
0
    }
965
966
0
    host = ssh_session_get_host_port(session);
967
0
    if (host == NULL) {
968
0
        return SSH_ERROR;
969
0
    }
970
971
0
    if (session->current_crypto == NULL) {
972
0
        ssh_set_error(session, SSH_FATAL,
973
0
                      "No current crypto context, please connect first");
974
0
        SAFE_FREE(host);
975
0
        return SSH_ERROR;
976
0
    }
977
978
0
    server_pubkey = ssh_dh_get_current_server_publickey(session);
979
0
    if (server_pubkey == NULL){
980
0
        ssh_set_error(session, SSH_FATAL, "No public key present");
981
0
        SAFE_FREE(host);
982
0
        return SSH_ERROR;
983
0
    }
984
985
0
    rc = ssh_pki_export_pubkey_base64(server_pubkey, &b64_key);
986
0
    if (rc < 0) {
987
0
        SAFE_FREE(host);
988
0
        return SSH_ERROR;
989
0
    }
990
991
0
    snprintf(entry_buf, sizeof(entry_buf),
992
0
                "%s %s %s\n",
993
0
                host,
994
0
                server_pubkey->type_c,
995
0
                b64_key);
996
997
0
    SAFE_FREE(host);
998
0
    SAFE_FREE(b64_key);
999
1000
0
    *pentry_string = strdup(entry_buf);
1001
0
    if (*pentry_string == NULL) {
1002
0
        return SSH_ERROR;
1003
0
    }
1004
1005
0
    return SSH_OK;
1006
0
}
1007
1008
/**
1009
 * @brief Adds the currently connected server to the user known_hosts file.
1010
 *
1011
 * This adds the currently connected server to the known_hosts file by
1012
 * appending a new line at the end. The global known_hosts file is considered
1013
 * read-only so it is not touched by this function.
1014
 *
1015
 * @param[in]  session  The session to use to write the entry.
1016
 *
1017
 * @return SSH_OK on success, SSH_ERROR otherwise.
1018
 */
1019
int ssh_session_update_known_hosts(ssh_session session)
1020
0
{
1021
0
    FILE *fp = NULL;
1022
0
    char *entry = NULL;
1023
0
    char *dir = NULL;
1024
0
    size_t nwritten;
1025
0
    size_t len;
1026
0
    int rc;
1027
0
    char err_msg[SSH_ERRNO_MSG_MAX] = {0};
1028
1029
0
    if (session->opts.knownhosts == NULL) {
1030
0
        rc = ssh_options_apply(session);
1031
0
        if (rc != SSH_OK) {
1032
0
            ssh_set_error(session, SSH_FATAL, "Can't find a known_hosts file");
1033
0
            return SSH_ERROR;
1034
0
        }
1035
0
    }
1036
1037
0
    errno = 0;
1038
0
    fp = fopen(session->opts.knownhosts, "a");
1039
0
    if (fp == NULL) {
1040
0
        if (errno == ENOENT) {
1041
0
            dir = ssh_dirname(session->opts.knownhosts);
1042
0
            if (dir == NULL) {
1043
0
                ssh_set_error(session, SSH_FATAL, "%s",
1044
0
                              ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
1045
0
                return SSH_ERROR;
1046
0
            }
1047
1048
0
            rc = ssh_mkdirs(dir, 0700);
1049
0
            if (rc < 0) {
1050
0
                ssh_set_error(session, SSH_FATAL,
1051
0
                              "Cannot create %s directory: %s",
1052
0
                              dir,
1053
0
                              ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
1054
0
                SAFE_FREE(dir);
1055
0
                return SSH_ERROR;
1056
0
            }
1057
0
            SAFE_FREE(dir);
1058
1059
0
            errno = 0;
1060
0
            fp = fopen(session->opts.knownhosts, "a");
1061
0
            if (fp == NULL) {
1062
0
                ssh_set_error(session, SSH_FATAL,
1063
0
                              "Couldn't open known_hosts file %s"
1064
0
                              " for appending: %s",
1065
0
                              session->opts.knownhosts,
1066
0
                              ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
1067
0
                return SSH_ERROR;
1068
0
            }
1069
0
        } else {
1070
0
            ssh_set_error(session,
1071
0
                          SSH_FATAL,
1072
0
                          "Couldn't open known_hosts file %s for appending: %s",
1073
0
                          session->opts.knownhosts,
1074
0
                          ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
1075
0
            return SSH_ERROR;
1076
0
        }
1077
0
    }
1078
1079
0
    rc = ssh_session_export_known_hosts_entry(session, &entry);
1080
0
    if (rc != SSH_OK) {
1081
0
        fclose(fp);
1082
0
        return rc;
1083
0
    }
1084
1085
0
    len = strlen(entry);
1086
0
    nwritten = fwrite(entry, sizeof(char), len, fp);
1087
0
    SAFE_FREE(entry);
1088
0
    if (nwritten != len || ferror(fp)) {
1089
0
        ssh_set_error(session, SSH_FATAL,
1090
0
                      "Couldn't append to known_hosts file %s: %s",
1091
0
                      session->opts.knownhosts,
1092
0
                      ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
1093
0
        fclose(fp);
1094
0
        return SSH_ERROR;
1095
0
    }
1096
1097
0
    fclose(fp);
1098
0
    return SSH_OK;
1099
0
}
1100
1101
static enum ssh_known_hosts_e
1102
ssh_known_hosts_check_server_key(const char *hosts_entry,
1103
                                 const char *filename,
1104
                                 ssh_key server_key,
1105
                                 struct ssh_knownhosts_entry **pentry)
1106
0
{
1107
0
    struct ssh_list *entry_list = NULL;
1108
0
    struct ssh_iterator *it = NULL;
1109
0
    enum ssh_known_hosts_e found = SSH_KNOWN_HOSTS_UNKNOWN;
1110
0
    int rc;
1111
1112
0
    rc = ssh_known_hosts_read_entries(hosts_entry,
1113
0
                                      filename,
1114
0
                                      &entry_list);
1115
0
    if (rc != 0) {
1116
0
        ssh_knownhosts_entries_free(entry_list);
1117
0
        return SSH_KNOWN_HOSTS_UNKNOWN;
1118
0
    }
1119
1120
0
    it = ssh_list_get_iterator(entry_list);
1121
0
    if (it == NULL) {
1122
0
        ssh_knownhosts_entries_free(entry_list);
1123
0
        return SSH_KNOWN_HOSTS_UNKNOWN;
1124
0
    }
1125
1126
0
    for (;it != NULL; it = it->next) {
1127
0
        struct ssh_knownhosts_entry *entry = NULL;
1128
0
        int cmp;
1129
1130
0
        entry = ssh_iterator_value(struct ssh_knownhosts_entry *, it);
1131
1132
0
        cmp = ssh_key_cmp(server_key, entry->publickey, SSH_KEY_CMP_PUBLIC);
1133
0
        if (cmp == 0) {
1134
0
            found = SSH_KNOWN_HOSTS_OK;
1135
0
            if (pentry != NULL) {
1136
0
                *pentry = entry;
1137
0
                ssh_list_remove(entry_list, it);
1138
0
            }
1139
0
            break;
1140
0
        }
1141
1142
0
        if (ssh_key_type(server_key) == ssh_key_type(entry->publickey)) {
1143
0
            found = SSH_KNOWN_HOSTS_CHANGED;
1144
0
            continue;
1145
0
        }
1146
1147
0
        if (found != SSH_KNOWN_HOSTS_CHANGED) {
1148
0
            found = SSH_KNOWN_HOSTS_OTHER;
1149
0
        }
1150
0
    }
1151
1152
0
    ssh_knownhosts_entries_free(entry_list);
1153
1154
0
    return found;
1155
0
}
1156
1157
static bool ssh_known_hosts_should_continue_unsafe(ssh_session session,
1158
                                                   enum ssh_known_hosts_e rv)
1159
0
{
1160
0
    return session->opts.StrictHostKeyChecking == SSH_STRICT_HOSTKEY_OFF &&
1161
0
           (rv == SSH_KNOWN_HOSTS_CHANGED || rv == SSH_KNOWN_HOSTS_OTHER);
1162
0
}
1163
1164
/**
1165
 * @brief Get the known_hosts entry for the currently connected session.
1166
 *
1167
 * @param[in]  session  The session to validate.
1168
 *
1169
 * @param[in]  pentry   A pointer to store the allocated known hosts entry.
1170
 *
1171
 * @returns SSH_KNOWN_HOSTS_OK:        The server is known and has not changed.\n
1172
 *          SSH_KNOWN_HOSTS_CHANGED:   The server key has changed. Either you
1173
 *                                     are under attack or the administrator
1174
 *                                     changed the key. You HAVE to warn the
1175
 *                                     user about a possible attack.\n
1176
 *          SSH_KNOWN_HOSTS_OTHER:     The server gave use a key of a type while
1177
 *                                     we had an other type recorded. It is a
1178
 *                                     possible attack.\n
1179
 *          SSH_KNOWN_HOSTS_UNKNOWN:   The server is unknown. User should
1180
 *                                     confirm the public key hash is correct.\n
1181
 *          SSH_KNOWN_HOSTS_NOT_FOUND: The known host file does not exist. The
1182
 *                                     host is thus unknown. File will be
1183
 *                                     created if host key is accepted.\n
1184
 *          SSH_KNOWN_HOSTS_ERROR:     There had been an error checking the host.
1185
 *
1186
 * @see ssh_knownhosts_entry_free()
1187
 */
1188
enum ssh_known_hosts_e
1189
ssh_session_get_known_hosts_entry(ssh_session session,
1190
                                  struct ssh_knownhosts_entry **pentry)
1191
0
{
1192
0
    enum ssh_known_hosts_e old_rv, rv = SSH_KNOWN_HOSTS_UNKNOWN;
1193
0
    int rc;
1194
1195
0
    if (pentry != NULL) {
1196
0
        *pentry = NULL;
1197
0
    }
1198
1199
0
    if (session->opts.knownhosts == NULL) {
1200
0
        if (ssh_options_apply(session) < 0) {
1201
0
            ssh_set_error(session,
1202
0
                          SSH_REQUEST_DENIED,
1203
0
                          "Can't find a known_hosts file");
1204
1205
0
            return SSH_KNOWN_HOSTS_NOT_FOUND;
1206
0
        }
1207
0
    }
1208
1209
0
    rv = ssh_session_get_known_hosts_entry_file(session,
1210
0
                                                session->opts.knownhosts,
1211
0
                                                pentry);
1212
0
    if (rv == SSH_KNOWN_HOSTS_OK) {
1213
        /* We already found a match in the first file: return */
1214
0
        return rv;
1215
0
    }
1216
1217
0
    old_rv = rv;
1218
0
    rv = ssh_session_get_known_hosts_entry_file(session,
1219
0
                                                session->opts.global_knownhosts,
1220
0
                                                pentry);
1221
1222
0
    if (ssh_known_hosts_should_continue_unsafe(session, rv)) {
1223
0
        ssh_known_hosts_continue_unsafe(session);
1224
0
        return SSH_KNOWN_HOSTS_OK;
1225
0
    }
1226
1227
    /* If the global file did not help, report the result from the user file. */
1228
0
    if (rv == SSH_KNOWN_HOSTS_UNKNOWN || rv == SSH_KNOWN_HOSTS_NOT_FOUND) {
1229
0
        if (ssh_known_hosts_should_continue_unsafe(session, old_rv)) {
1230
0
            ssh_known_hosts_continue_unsafe(session);
1231
0
            return SSH_KNOWN_HOSTS_OK;
1232
0
        }
1233
1234
0
        if ((old_rv == SSH_KNOWN_HOSTS_UNKNOWN ||
1235
0
             old_rv == SSH_KNOWN_HOSTS_NOT_FOUND) &&
1236
0
            (session->opts.StrictHostKeyChecking == SSH_STRICT_HOSTKEY_OFF ||
1237
0
             session->opts.StrictHostKeyChecking ==
1238
0
                 SSH_STRICT_HOSTKEY_ACCEPT_NEW)) {
1239
0
            rc = ssh_session_update_known_hosts(session);
1240
0
            if (rc != SSH_OK) {
1241
0
                return SSH_KNOWN_HOSTS_ERROR;
1242
0
            }
1243
1244
0
            rv =
1245
0
                ssh_session_get_known_hosts_entry_file(session,
1246
0
                                                       session->opts.knownhosts,
1247
0
                                                       pentry);
1248
0
            if (rv == SSH_KNOWN_HOSTS_UNKNOWN ||
1249
0
                rv == SSH_KNOWN_HOSTS_NOT_FOUND) {
1250
0
                return SSH_KNOWN_HOSTS_OK;
1251
0
            }
1252
1253
0
            return rv;
1254
0
        }
1255
1256
0
        return old_rv;
1257
0
    }
1258
1259
    /* We found some match: return it */
1260
0
    return rv;
1261
1262
0
}
1263
1264
/**
1265
 * @internal
1266
 *
1267
 * @brief Get the known_hosts entry for the current connected session
1268
 *        from the given known_hosts file.
1269
 *
1270
 * @param[in]  session  The session to validate.
1271
 *
1272
 * @param[in]  filename The filename to parse.
1273
 *
1274
 * @param[in]  pentry   A pointer to store the allocated known hosts entry.
1275
 *
1276
 * @returns SSH_KNOWN_HOSTS_OK:        The server is known and has not changed.\n
1277
 *          SSH_KNOWN_HOSTS_CHANGED:   The server key has changed. Either you
1278
 *                                     are under attack or the administrator
1279
 *                                     changed the key. You HAVE to warn the
1280
 *                                     user about a possible attack.\n
1281
 *          SSH_KNOWN_HOSTS_OTHER:     The server gave use a key of a type while
1282
 *                                     we had an other type recorded. It is a
1283
 *                                     possible attack.\n
1284
 *          SSH_KNOWN_HOSTS_UNKNOWN:   The server is unknown. User should
1285
 *                                     confirm the public key hash is correct.\n
1286
 *          SSH_KNOWN_HOSTS_NOT_FOUND: The known host file does not exist. The
1287
 *                                     host is thus unknown. File will be
1288
 *                                     created if host key is accepted.\n
1289
 *          SSH_KNOWN_HOSTS_ERROR:     There had been an error checking the host.
1290
 *
1291
 * @see ssh_knownhosts_entry_free()
1292
 */
1293
enum ssh_known_hosts_e
1294
ssh_session_get_known_hosts_entry_file(ssh_session session,
1295
                                       const char *filename,
1296
                                       struct ssh_knownhosts_entry **pentry)
1297
0
{
1298
0
    ssh_key server_pubkey = NULL;
1299
0
    char *host_port = NULL;
1300
0
    enum ssh_known_hosts_e found = SSH_KNOWN_HOSTS_UNKNOWN;
1301
1302
0
    server_pubkey = ssh_dh_get_current_server_publickey(session);
1303
0
    if (server_pubkey == NULL) {
1304
0
        ssh_set_error(session,
1305
0
                      SSH_FATAL,
1306
0
                      "ssh_session_is_known_host called without a "
1307
0
                      "server_key!");
1308
1309
0
        return SSH_KNOWN_HOSTS_ERROR;
1310
0
    }
1311
1312
0
    host_port = ssh_session_get_host_port(session);
1313
0
    if (host_port == NULL) {
1314
0
        return SSH_KNOWN_HOSTS_ERROR;
1315
0
    }
1316
1317
0
    found = ssh_known_hosts_check_server_key(host_port,
1318
0
                                             filename,
1319
0
                                             server_pubkey,
1320
0
                                             pentry);
1321
0
    SAFE_FREE(host_port);
1322
1323
0
    return found;
1324
0
}
1325
1326
/**
1327
 * @brief Check if the servers public key for the connected session is known.
1328
 *
1329
 * This checks if we already know the public key of the server we want to
1330
 * connect to. This allows to detect if there is a MITM attach going on
1331
 * of if there have been changes on the server we don't know about.
1332
 *
1333
 * @param[in]  session  The SSH to validate.
1334
 *
1335
 * @returns SSH_KNOWN_HOSTS_OK:        The server is known and has not changed.\n
1336
 *          SSH_KNOWN_HOSTS_CHANGED:   The server key has changed. Either you
1337
 *                                     are under attack or the administrator
1338
 *                                     changed the key. You HAVE to warn the
1339
 *                                     user about a possible attack.\n
1340
 *          SSH_KNOWN_HOSTS_OTHER:     The server gave use a key of a type while
1341
 *                                     we had an other type recorded. It is a
1342
 *                                     possible attack.\n
1343
 *          SSH_KNOWN_HOSTS_UNKNOWN:   The server is unknown. User should
1344
 *                                     confirm the public key hash is correct.\n
1345
 *          SSH_KNOWN_HOSTS_NOT_FOUND: The known host file does not exist. The
1346
 *                                     host is thus unknown. File will be
1347
 *                                     created if host key is accepted.\n
1348
 *          SSH_KNOWN_HOSTS_ERROR:     There had been an error checking the host.
1349
 */
1350
enum ssh_known_hosts_e ssh_session_is_known_server(ssh_session session)
1351
0
{
1352
    return ssh_session_get_known_hosts_entry(session, NULL);
1353
0
}
1354
1355
/** @} */