Coverage Report

Created: 2025-12-14 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libssh/src/pki.c
Line
Count
Source
1
/*
2
 * pki.c
3
 * This file is part of the SSH Library
4
 *
5
 * Copyright (c) 2010 by Aris Adamantiadis
6
 * Copyright (c) 2011-2013 Andreas Schneider <asn@cryptomilk.org>
7
 * Copyright (c) 2019      Sahana Prasad     <sahana@redhat.com>
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
/**
26
 * @defgroup libssh_pki The SSH Public Key Infrastructure
27
 * @ingroup libssh
28
 *
29
 * Functions for the creation, importation and manipulation of public and
30
 * private keys in the context of the SSH protocol
31
 *
32
 * @{
33
 */
34
35
#include "config.h"
36
#include "libssh/wrapper.h"
37
38
#include <errno.h>
39
#include <ctype.h>
40
#include <stdint.h>
41
#include <stdio.h>
42
#include <fcntl.h>
43
#include <sys/stat.h>
44
#include <sys/types.h>
45
46
#include "libssh/agent.h"
47
#include "libssh/buffer.h"
48
#include "libssh/keys.h"
49
#include "libssh/libssh.h"
50
#include "libssh/misc.h"
51
#include "libssh/pki.h"
52
#include "libssh/pki_context.h"
53
#include "libssh/pki_priv.h"
54
#include "libssh/pki_sk.h"
55
#include "libssh/priv.h"
56
#include "libssh/session.h"
57
#include "libssh/sk_common.h" /* For SK_NOT_SUPPORTED_MSG */
58
59
#ifndef MAX_LINE_SIZE
60
#define MAX_LINE_SIZE 4096
61
#endif /* NOT MAX_LINE_SIZE */
62
63
#define PKCS11_URI "pkcs11:"
64
65
enum ssh_keytypes_e pki_privatekey_type_from_string(const char *privkey)
66
0
{
67
0
    char *start = NULL;
68
69
0
    start = strstr(privkey, RSA_HEADER_BEGIN);
70
0
    if (start != NULL) {
71
0
        return SSH_KEYTYPE_RSA;
72
0
    }
73
74
0
    start = strstr(privkey, ECDSA_HEADER_BEGIN);
75
0
    if (start != 0) {
76
        /* We don't know what the curve is at this point, so we don't actually
77
         * know the type. We figure out the actual curve and fix things up in
78
         * pki_private_key_from_base64 */
79
0
        return SSH_KEYTYPE_ECDSA_P256;
80
0
    }
81
82
0
    return SSH_KEYTYPE_UNKNOWN;
83
0
}
84
85
/**
86
 * @brief returns the ECDSA key name ("ecdsa-sha2-nistp256" for example)
87
 *
88
 * @param[in] key the ssh_key whose ECDSA name to get
89
 *
90
 * @returns the ECDSA key name ("ecdsa-sha2-nistp256" for example)
91
 *
92
 * @returns "unknown" if the ECDSA key name is not known
93
 */
94
const char *ssh_pki_key_ecdsa_name(const ssh_key key)
95
0
{
96
0
    if (key == NULL) {
97
0
        return NULL;
98
0
    }
99
100
0
#ifdef HAVE_ECC /* FIXME Better ECC check needed */
101
0
    return pki_key_ecdsa_nid_to_name(key->ecdsa_nid);
102
#else
103
    return NULL;
104
#endif /* HAVE_ECC */
105
0
}
106
107
/**
108
 * @brief creates a new empty SSH key
109
 *
110
 * @returns an empty ssh_key handle, or NULL on error.
111
 */
112
ssh_key ssh_key_new (void)
113
6
{
114
6
    ssh_key ptr = malloc (sizeof (struct ssh_key_struct));
115
6
    if (ptr == NULL) {
116
0
        return NULL;
117
0
    }
118
6
    ZERO_STRUCTP(ptr);
119
6
    return ptr;
120
6
}
121
122
/**
123
 * @internal
124
 *
125
 * @brief Initialize a new SSH key by duplicating common fields from an existing
126
 * key.
127
 *
128
 * This function creates a new SSH key and copies the common fields from the
129
 * source key, including the key type, type string, flags, and security key
130
 * fields if applicable. This is a helper function used by key duplication
131
 * routines.
132
 *
133
 * @param[in] key     The source ssh_key to copy common fields from.
134
 * @param[in] demote  Whether to demote the new key to public only. If non-zero,
135
 *                    only the public fields will be copied and the flags will
136
 *                    be set accordingly.
137
 *
138
 * @return            A new ssh_key with common fields initialized, or NULL on
139
 * error.
140
 *
141
 * @note The caller is responsible for freeing the returned key with
142
 * ssh_key_free().
143
 */
144
ssh_key pki_key_dup_common_init(const ssh_key key, int demote)
145
0
{
146
0
    ssh_key new = NULL;
147
148
0
    if (key == NULL) {
149
0
        return NULL;
150
0
    }
151
152
0
    new = ssh_key_new();
153
0
    if (new == NULL) {
154
0
        return NULL;
155
0
    }
156
157
0
    new->type = key->type;
158
0
    new->type_c = key->type_c;
159
0
    if (demote) {
160
0
        new->flags = SSH_KEY_FLAG_PUBLIC;
161
0
    } else {
162
0
        new->flags = key->flags;
163
0
    }
164
165
    /* Copy security key fields if present */
166
0
    if (is_sk_key_type(key->type)) {
167
0
        new->sk_application = ssh_string_copy(key->sk_application);
168
0
        if (new->sk_application == NULL) {
169
0
            goto fail;
170
0
        }
171
172
0
        if (key->sk_user_id != NULL) {
173
0
            new->sk_user_id = ssh_string_copy(key->sk_user_id);
174
0
            if (new->sk_user_id == NULL) {
175
0
                goto fail;
176
0
            }
177
0
        }
178
179
0
        if (!demote) {
180
0
            new->sk_flags = key->sk_flags;
181
182
0
            new->sk_key_handle = ssh_string_copy(key->sk_key_handle);
183
0
            if (new->sk_key_handle == NULL) {
184
0
                goto fail;
185
0
            }
186
187
0
            new->sk_reserved = ssh_string_copy(key->sk_reserved);
188
0
            if (new->sk_reserved == NULL) {
189
0
                goto fail;
190
0
            }
191
0
        }
192
0
    }
193
194
0
    return new;
195
196
0
fail:
197
0
    SSH_KEY_FREE(new);
198
0
    return NULL;
199
0
}
200
201
/**
202
 * @brief duplicates the key
203
 *
204
 * @param key An ssh_key to duplicate
205
 *
206
 * @return A duplicated ssh_key key
207
 */
208
ssh_key ssh_key_dup(const ssh_key key)
209
0
{
210
0
    if (key == NULL) {
211
0
        return NULL;
212
0
    }
213
214
0
    return pki_key_dup(key, 0);
215
0
}
216
217
/**
218
 * @brief clean up the key and deallocate all existing keys
219
 * @param[in] key ssh_key to clean
220
 */
221
void ssh_key_clean (ssh_key key)
222
6
{
223
6
    if (key == NULL)
224
0
        return;
225
226
6
    pki_key_clean(key);
227
228
#ifndef HAVE_LIBCRYPTO
229
    if (key->ed25519_privkey != NULL) {
230
        ssh_burn(key->ed25519_privkey, sizeof(ed25519_privkey));
231
        SAFE_FREE(key->ed25519_privkey);
232
    }
233
    SAFE_FREE(key->ed25519_pubkey);
234
#endif /* HAVE_LIBCRYPTO */
235
6
    if (key->cert != NULL) {
236
0
        SSH_BUFFER_FREE(key->cert);
237
0
    }
238
6
    if (is_sk_key_type(key->type)) {
239
3
        ssh_string_burn(key->sk_application);
240
3
        ssh_string_free(key->sk_application);
241
3
        ssh_string_burn(key->sk_key_handle);
242
3
        ssh_string_free(key->sk_key_handle);
243
3
        ssh_string_burn(key->sk_reserved);
244
3
        ssh_string_free(key->sk_reserved);
245
3
        ssh_string_burn(key->sk_user_id);
246
3
        ssh_string_free(key->sk_user_id);
247
3
        key->sk_flags = 0;
248
3
    }
249
6
    key->cert_type = SSH_KEYTYPE_UNKNOWN;
250
6
    key->flags = SSH_KEY_FLAG_EMPTY;
251
6
    key->type = SSH_KEYTYPE_UNKNOWN;
252
6
    key->ecdsa_nid = 0;
253
6
    key->type_c = NULL;
254
6
}
255
256
/**
257
 * @brief deallocate a SSH key
258
 * @param[in] key ssh_key handle to free
259
 */
260
void ssh_key_free (ssh_key key)
261
10
{
262
10
    if (key) {
263
6
        ssh_key_clean(key);
264
6
        SAFE_FREE(key);
265
6
    }
266
10
}
267
268
/**
269
 * @brief returns the type of a ssh key
270
 * @param[in] key the ssh_key handle
271
 * @returns one of SSH_KEYTYPE_RSA,
272
 *          SSH_KEYTYPE_ECDSA_P256, SSH_KEYTYPE_ECDSA_P384,
273
 *          SSH_KEYTYPE_ECDSA_P521, SSH_KEYTYPE_ED25519,
274
 *          SSH_KEYTYPE_RSA_CERT01, SSH_KEYTYPE_ECDSA_P256_CERT01,
275
 *          SSH_KEYTYPE_ECDSA_P384_CERT01, SSH_KEYTYPE_ECDSA_P521_CERT01, or
276
 *          SSH_KEYTYPE_ED25519_CERT01.
277
 * @returns SSH_KEYTYPE_UNKNOWN if the type is unknown
278
 */
279
enum ssh_keytypes_e ssh_key_type(const ssh_key key)
280
0
{
281
0
    if (key == NULL) {
282
0
        return SSH_KEYTYPE_UNKNOWN;
283
0
    }
284
0
    return key->type;
285
0
}
286
287
/**
288
 * @brief Get security key (FIDO2) flags for a security key backed ssh_key.
289
 *
290
 * The returned value contains a bitmask of SSH_SK_* flags (e.g.
291
 * SSH_SK_USER_PRESENCE_REQD, SSH_SK_USER_VERIFICATION_REQD, etc.).
292
 * If NULL is passed, then 0 is returned.
293
 *
294
 * @param[in] key  The ssh_key handle.
295
 *
296
 * @return Bitmask of security key flags, or 0 if not applicable.
297
 */
298
uint32_t ssh_key_get_sk_flags(const ssh_key key)
299
0
{
300
0
    if (key == NULL) {
301
0
        return 0;
302
0
    }
303
0
    return key->sk_flags;
304
0
}
305
306
/**
307
 * @brief Get the application (RP ID) associated with a security key.
308
 *
309
 * This function returns a freshly allocated ssh_string containing a copy of the
310
 * application (RP ID). The caller owns the returned ssh_string and must free it
311
 * with SSH_STRING_FREE() when no longer needed.
312
 *
313
 * Returns NULL if the key is NULL, not a security key type or if the field is
314
 * not set.
315
 *
316
 * @param[in] key  The ssh_key handle.
317
 *
318
 * @return ssh_string copy of the application (RP ID) or NULL if not available.
319
 */
320
ssh_string ssh_key_get_sk_application(const ssh_key key)
321
0
{
322
0
    if (key == NULL || key->sk_application == NULL) {
323
0
        return NULL;
324
0
    }
325
326
0
    return ssh_string_copy(key->sk_application);
327
0
}
328
329
/**
330
 * @brief Get a copy of the user ID associated with a resident security key
331
 * credential.
332
 *
333
 * For resident (discoverable) credentials, authenticators may provide a user
334
 * id which can be arbitrary binary data to allow for storing multiple keys for
335
 * the same Relying Party. This function returns a freshly allocated ssh_string
336
 * containing a copy of that user id. The caller owns the returned ssh_string
337
 * and must free it with SSH_STRING_FREE() when no longer needed.
338
 *
339
 * @note This function will only return useful information if the ssh_key
340
 * passed represents a resident key loaded using the ssh_sk_resident_keys_load()
341
 * function.
342
 *
343
 * @param[in] key  The ssh_key handle.
344
 *
345
 * @return ssh_string copy of user id or NULL if not available.
346
 */
347
ssh_string ssh_key_get_sk_user_id(const ssh_key key)
348
0
{
349
0
    if (key == NULL) {
350
0
        return NULL;
351
0
    }
352
0
    return ssh_string_copy(key->sk_user_id);
353
0
}
354
355
/**
356
 * @brief Convert a signature type to a string.
357
 *
358
 * @param[in]  type     The algorithm type to convert.
359
 *
360
 * @param[in] hash_type The hash type to convert
361
 *
362
 * @return              A string for the keytype or NULL if unknown.
363
 */
364
const char *
365
ssh_key_signature_to_char(enum ssh_keytypes_e type,
366
                          enum ssh_digest_e hash_type)
367
0
{
368
0
    switch (type) {
369
0
    case SSH_KEYTYPE_RSA:
370
0
        switch (hash_type) {
371
0
        case SSH_DIGEST_SHA256:
372
0
            return "rsa-sha2-256";
373
0
        case SSH_DIGEST_SHA512:
374
0
            return "rsa-sha2-512";
375
0
        case SSH_DIGEST_SHA1:
376
0
        case SSH_DIGEST_AUTO:
377
0
            return "ssh-rsa";
378
0
        default:
379
0
            return NULL;
380
0
        }
381
0
        break;
382
0
    case SSH_KEYTYPE_RSA_CERT01:
383
0
        switch (hash_type) {
384
0
        case SSH_DIGEST_SHA256:
385
0
            return "rsa-sha2-256-cert-v01@openssh.com";
386
0
        case SSH_DIGEST_SHA512:
387
0
            return "rsa-sha2-512-cert-v01@openssh.com";
388
0
        case SSH_DIGEST_SHA1:
389
0
        case SSH_DIGEST_AUTO:
390
0
            return "ssh-rsa-cert-v01@openssh.com";
391
0
        default:
392
0
            return NULL;
393
0
        }
394
0
        break;
395
0
    default:
396
0
        return ssh_key_type_to_char(type);
397
0
    }
398
399
    /* We should never reach this */
400
0
    return NULL;
401
0
}
402
403
/**
404
 * @brief Convert a key type to a string.
405
 *
406
 * @param[in]  type     The type to convert.
407
 *
408
 * @return              A string for the keytype or NULL if unknown.
409
 */
410
10
const char *ssh_key_type_to_char(enum ssh_keytypes_e type) {
411
10
  switch (type) {
412
0
    case SSH_KEYTYPE_RSA:
413
0
      return "ssh-rsa";
414
0
    case SSH_KEYTYPE_ECDSA:
415
0
      return "ssh-ecdsa"; /* deprecated. invalid value */
416
0
    case SSH_KEYTYPE_ECDSA_P256:
417
0
      return "ecdsa-sha2-nistp256";
418
0
    case SSH_KEYTYPE_ECDSA_P384:
419
0
      return "ecdsa-sha2-nistp384";
420
0
    case SSH_KEYTYPE_ECDSA_P521:
421
0
      return "ecdsa-sha2-nistp521";
422
3
    case SSH_KEYTYPE_ED25519:
423
3
      return "ssh-ed25519";
424
0
    case SSH_KEYTYPE_RSA_CERT01:
425
0
      return "ssh-rsa-cert-v01@openssh.com";
426
0
    case SSH_KEYTYPE_ECDSA_P256_CERT01:
427
0
      return "ecdsa-sha2-nistp256-cert-v01@openssh.com";
428
0
    case SSH_KEYTYPE_ECDSA_P384_CERT01:
429
0
      return "ecdsa-sha2-nistp384-cert-v01@openssh.com";
430
0
    case SSH_KEYTYPE_ECDSA_P521_CERT01:
431
0
      return "ecdsa-sha2-nistp521-cert-v01@openssh.com";
432
0
    case SSH_KEYTYPE_ED25519_CERT01:
433
0
      return "ssh-ed25519-cert-v01@openssh.com";
434
3
    case SSH_KEYTYPE_SK_ECDSA:
435
3
      return "sk-ecdsa-sha2-nistp256@openssh.com";
436
0
    case SSH_KEYTYPE_SK_ED25519:
437
0
      return "sk-ssh-ed25519@openssh.com";
438
4
    case SSH_KEYTYPE_SK_ECDSA_CERT01:
439
4
      return "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com";
440
0
    case SSH_KEYTYPE_SK_ED25519_CERT01:
441
0
      return "sk-ssh-ed25519-cert-v01@openssh.com";
442
0
    case SSH_KEYTYPE_DSS:   /* deprecated */
443
0
    case SSH_KEYTYPE_RSA1:
444
0
    case SSH_KEYTYPE_DSS_CERT01:    /* deprecated */
445
0
    case SSH_KEYTYPE_UNKNOWN:
446
0
      return NULL;
447
10
  }
448
449
  /* We should never reach this */
450
0
  return NULL;
451
10
}
452
453
enum ssh_digest_e ssh_key_hash_from_name(const char *name)
454
0
{
455
0
    if (name == NULL) {
456
        /* TODO we should rather fail */
457
0
        return SSH_DIGEST_AUTO;
458
0
    }
459
460
0
    if (strcmp(name, "ssh-rsa") == 0) {
461
0
        return SSH_DIGEST_SHA1;
462
0
    } else if (strcmp(name, "rsa-sha2-256") == 0) {
463
0
        return SSH_DIGEST_SHA256;
464
0
    } else if (strcmp(name, "rsa-sha2-512") == 0) {
465
0
        return SSH_DIGEST_SHA512;
466
0
    } else if (strcmp(name, "ecdsa-sha2-nistp256") == 0) {
467
0
        return SSH_DIGEST_SHA256;
468
0
    } else if (strcmp(name, "ecdsa-sha2-nistp384") == 0) {
469
0
        return SSH_DIGEST_SHA384;
470
0
    } else if (strcmp(name, "ecdsa-sha2-nistp521") == 0) {
471
0
        return SSH_DIGEST_SHA512;
472
0
    } else if (strcmp(name, "ssh-ed25519") == 0) {
473
0
        return SSH_DIGEST_AUTO;
474
0
    } else if (strcmp(name, "sk-ecdsa-sha2-nistp256@openssh.com") == 0) {
475
0
        return SSH_DIGEST_SHA256;
476
0
    } else if (strcmp(name, "sk-ssh-ed25519@openssh.com") == 0) {
477
0
        return SSH_DIGEST_AUTO;
478
0
    }
479
480
0
    SSH_LOG(SSH_LOG_TRACE, "Unknown signature name %s", name);
481
482
    /* TODO we should rather fail */
483
0
    return SSH_DIGEST_AUTO;
484
0
}
485
486
/**
487
 * @brief Checks the given key against the configured allowed
488
 * public key algorithm types
489
 *
490
 * @param[in] session The SSH session
491
 * @param[in] type    The key algorithm to check
492
 * @returns           1 if the key algorithm is allowed, 0 otherwise
493
 */
494
int ssh_key_algorithm_allowed(ssh_session session, const char *type)
495
0
{
496
0
    const char *allowed_list = NULL;
497
498
0
    if (session->client) {
499
0
        allowed_list = session->opts.pubkey_accepted_types;
500
0
        if (allowed_list == NULL) {
501
0
            if (ssh_fips_mode()) {
502
0
                allowed_list = ssh_kex_get_fips_methods(SSH_HOSTKEYS);
503
0
            } else {
504
0
                allowed_list = ssh_kex_get_default_methods(SSH_HOSTKEYS);
505
0
            }
506
0
        }
507
0
    }
508
0
#ifdef WITH_SERVER
509
0
    else if (session->server) {
510
0
        allowed_list = session->opts.wanted_methods[SSH_HOSTKEYS];
511
0
        if (allowed_list == NULL) {
512
0
            SSH_LOG(SSH_LOG_TRACE, "Session invalid: no host key available");
513
0
            return 0;
514
0
        }
515
0
    }
516
0
#endif /* WITH_SERVER */
517
0
    else {
518
0
        SSH_LOG(SSH_LOG_TRACE, "Session invalid: not set as client nor server");
519
0
        return 0;
520
0
    }
521
522
0
    SSH_LOG(SSH_LOG_DEBUG, "Checking %s with list <%s>", type, allowed_list);
523
0
    return match_group(allowed_list, type);
524
0
}
525
526
bool ssh_key_size_allowed_rsa(int min_size, ssh_key key)
527
0
{
528
0
    int key_size = ssh_key_size(key);
529
530
0
    if (min_size < RSA_MIN_KEY_SIZE) {
531
0
        if (ssh_fips_mode()) {
532
0
            min_size = RSA_MIN_FIPS_KEY_SIZE;
533
0
        } else {
534
0
            min_size = RSA_MIN_KEY_SIZE;
535
0
        }
536
0
    }
537
0
    return (key_size >= min_size);
538
0
}
539
540
/**
541
 * @brief Check the given key is acceptable in regards to the key size policy
542
 * specified by the configuration
543
 *
544
 * @param[in] session The SSH session
545
 * @param[in] key     The SSH key
546
 * @returns           true if the key is allowed, false otherwise
547
 */
548
bool ssh_key_size_allowed(ssh_session session, ssh_key key)
549
0
{
550
0
    int min_size = 0;
551
552
0
    switch (ssh_key_type(key)) {
553
0
    case SSH_KEYTYPE_RSA:
554
0
    case SSH_KEYTYPE_RSA_CERT01:
555
0
        min_size = session->opts.rsa_min_size;
556
0
        return ssh_key_size_allowed_rsa(min_size, key);
557
0
    default:
558
0
        return true;
559
0
    }
560
0
}
561
562
/**
563
 * @brief Helper function to convert a key type to a hash type.
564
 *
565
 * @param[in]  type     The type to convert.
566
 *
567
 * @return              A hash type to be used.
568
 *
569
 * @warning This helper function is available for use without session (for
570
 *          example for signing commits) and might cause interoperability issues
571
 *          when used within session! It is recommended to use
572
 *          ssh_key_type_to_hash() instead of this helper directly when a
573
 *          session is available.
574
 *
575
 * @note    In order to follow current security best practises for RSA, defaults
576
 *          to SHA-2 with SHA-512 digest (RFC8332) instead of the default for
577
 *          the SSH protocol (SHA1 with RSA ; RFC 4253).
578
 *
579
 * @see     ssh_key_type_to_hash()
580
 */
581
static enum ssh_digest_e key_type_to_hash(enum ssh_keytypes_e type)
582
0
{
583
0
    switch (type) {
584
0
    case SSH_KEYTYPE_RSA_CERT01:
585
0
    case SSH_KEYTYPE_RSA:
586
0
        return SSH_DIGEST_SHA512;
587
0
    case SSH_KEYTYPE_ECDSA_P256_CERT01:
588
0
    case SSH_KEYTYPE_ECDSA_P256:
589
0
    case SSH_KEYTYPE_SK_ECDSA:
590
0
        return SSH_DIGEST_SHA256;
591
0
    case SSH_KEYTYPE_ECDSA_P384_CERT01:
592
0
    case SSH_KEYTYPE_ECDSA_P384:
593
0
        return SSH_DIGEST_SHA384;
594
0
    case SSH_KEYTYPE_ECDSA_P521_CERT01:
595
0
    case SSH_KEYTYPE_ECDSA_P521:
596
0
        return SSH_DIGEST_SHA512;
597
0
    case SSH_KEYTYPE_ED25519_CERT01:
598
0
    case SSH_KEYTYPE_ED25519:
599
0
    case SSH_KEYTYPE_SK_ED25519:
600
0
        return SSH_DIGEST_AUTO;
601
0
    case SSH_KEYTYPE_RSA1:
602
0
    case SSH_KEYTYPE_DSS:        /* deprecated */
603
0
    case SSH_KEYTYPE_DSS_CERT01: /* deprecated */
604
0
    case SSH_KEYTYPE_ECDSA:
605
0
    case SSH_KEYTYPE_UNKNOWN:
606
0
    default:
607
0
        SSH_LOG(SSH_LOG_WARN,
608
0
                "Digest algorithm to be used with key type %u "
609
0
                "is not defined",
610
0
                type);
611
0
    }
612
613
    /* We should never reach this */
614
0
    return SSH_DIGEST_AUTO;
615
0
}
616
617
/**
618
 * @brief Convert a key type to a hash type. This is usually unambiguous
619
 * for all the key types, unless the SHA2 extension (RFC 8332) is
620
 * negotiated during key exchange.
621
 *
622
 * @param[in]  session  SSH Session.
623
 *
624
 * @param[in]  type     The type to convert.
625
 *
626
 * @return              A hash type to be used.
627
 */
628
enum ssh_digest_e ssh_key_type_to_hash(ssh_session session,
629
                                       enum ssh_keytypes_e type)
630
0
{
631
0
    switch (type) {
632
0
    case SSH_KEYTYPE_RSA_CERT01:
633
        /* If we are talking to an old OpenSSH version which does not support
634
         * SHA2 in certificates */
635
0
        if ((session->openssh > 0) &&
636
0
            (session->openssh < SSH_VERSION_INT(7, 2, 0)))
637
0
        {
638
0
            SSH_LOG(SSH_LOG_DEBUG,
639
0
                    "We are talking to an old OpenSSH (%x); "
640
0
                    "returning SSH_DIGEST_SHA1",
641
0
                    session->openssh);
642
643
0
            return SSH_DIGEST_SHA1;
644
0
        }
645
0
        FALL_THROUGH;
646
0
    case SSH_KEYTYPE_RSA:
647
0
        if (ssh_key_algorithm_allowed(session, "rsa-sha2-512") &&
648
0
            (session->extensions & SSH_EXT_SIG_RSA_SHA512)) {
649
0
            return SSH_DIGEST_SHA512;
650
0
        }
651
652
0
        if (ssh_key_algorithm_allowed(session, "rsa-sha2-256") &&
653
0
            (session->extensions & SSH_EXT_SIG_RSA_SHA256)) {
654
0
            return SSH_DIGEST_SHA256;
655
0
        }
656
657
        /* Default algorithm for RSA is SHA1 */
658
0
        return SSH_DIGEST_SHA1;
659
660
0
    default:
661
0
        return key_type_to_hash(type);
662
0
    }
663
664
    /* We should never reach this */
665
0
    return SSH_DIGEST_AUTO;
666
0
}
667
668
/**
669
 * @brief Gets signature algorithm name to be used with the given
670
 *        key type.
671
 *
672
 * @param[in]  session  SSH session.
673
 * @param[in]  type     The algorithm type to convert.
674
 *
675
 * @return              A string for the keytype or NULL if unknown.
676
 */
677
const char *
678
ssh_key_get_signature_algorithm(ssh_session session,
679
                                enum ssh_keytypes_e type)
680
0
{
681
0
    enum ssh_digest_e hash_type;
682
683
0
    if (type == SSH_KEYTYPE_RSA_CERT01) {
684
        /* If we are talking to an old OpenSSH version which does not support
685
         * rsa-sha2-{256,512}-cert-v01@openssh.com */
686
0
        if ((session->openssh > 0) &&
687
0
            (session->openssh < SSH_VERSION_INT(7, 8, 0)))
688
0
        {
689
0
            SSH_LOG(SSH_LOG_DEBUG,
690
0
                    "We are talking to an old OpenSSH (%x); "
691
0
                    "using old cert format",
692
0
                    session->openssh);
693
694
0
            return "ssh-rsa-cert-v01@openssh.com";
695
0
        }
696
0
    }
697
698
0
    hash_type = ssh_key_type_to_hash(session, type);
699
700
0
    return ssh_key_signature_to_char(type, hash_type);
701
0
}
702
703
/**
704
 * @brief Convert a ssh key algorithm name to a ssh key algorithm type.
705
 *
706
 * @param[in] name      The name to convert.
707
 *
708
 * @return              The enum ssh key algorithm type.
709
 */
710
0
enum ssh_keytypes_e ssh_key_type_from_signature_name(const char *name) {
711
0
    if (name == NULL) {
712
0
        return SSH_KEYTYPE_UNKNOWN;
713
0
    }
714
715
0
    if ((strcmp(name, "rsa-sha2-256") == 0) ||
716
0
        (strcmp(name, "rsa-sha2-512") == 0)) {
717
0
        return SSH_KEYTYPE_RSA;
718
0
    }
719
720
    /* Otherwise the key type matches the signature type */
721
0
    return ssh_key_type_from_name(name);
722
0
}
723
724
/**
725
 * @brief Convert a ssh key name to a ssh key type.
726
 *
727
 * @param[in] name      The name to convert.
728
 *
729
 * @return              The enum ssh key type.
730
 */
731
enum ssh_keytypes_e ssh_key_type_from_name(const char *name)
732
212
{
733
212
    if (name == NULL) {
734
0
        return SSH_KEYTYPE_UNKNOWN;
735
0
    }
736
737
212
    if (strcmp(name, "rsa") == 0) {
738
2
        return SSH_KEYTYPE_RSA;
739
210
    } else if (strcmp(name, "ssh-rsa") == 0) {
740
0
        return SSH_KEYTYPE_RSA;
741
210
    } else if (strcmp(name, "ssh-ecdsa") == 0
742
208
            || strcmp(name, "ecdsa") == 0
743
205
            || strcmp(name, "ecdsa-sha2-nistp256") == 0) {
744
5
        return SSH_KEYTYPE_ECDSA_P256;
745
205
    } else if (strcmp(name, "ecdsa-sha2-nistp384") == 0) {
746
2
        return SSH_KEYTYPE_ECDSA_P384;
747
203
    } else if (strcmp(name, "ecdsa-sha2-nistp521") == 0) {
748
0
        return SSH_KEYTYPE_ECDSA_P521;
749
203
    } else if (strcmp(name, "ssh-ed25519") == 0){
750
5
        return SSH_KEYTYPE_ED25519;
751
198
    } else if (strcmp(name, "ssh-rsa-cert-v01@openssh.com") == 0) {
752
3
        return SSH_KEYTYPE_RSA_CERT01;
753
195
    } else if (strcmp(name, "ecdsa-sha2-nistp256-cert-v01@openssh.com") == 0) {
754
0
        return SSH_KEYTYPE_ECDSA_P256_CERT01;
755
195
    } else if (strcmp(name, "ecdsa-sha2-nistp384-cert-v01@openssh.com") == 0) {
756
1
        return SSH_KEYTYPE_ECDSA_P384_CERT01;
757
194
    } else if (strcmp(name, "ecdsa-sha2-nistp521-cert-v01@openssh.com") == 0) {
758
0
        return SSH_KEYTYPE_ECDSA_P521_CERT01;
759
194
    } else if (strcmp(name, "ssh-ed25519-cert-v01@openssh.com") == 0) {
760
1
        return SSH_KEYTYPE_ED25519_CERT01;
761
193
    } else if(strcmp(name, "sk-ecdsa-sha2-nistp256@openssh.com") == 0) {
762
0
        return SSH_KEYTYPE_SK_ECDSA;
763
193
    } else if(strcmp(name, "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com") == 0) {
764
6
        return SSH_KEYTYPE_SK_ECDSA_CERT01;
765
187
    } else if(strcmp(name, "sk-ssh-ed25519@openssh.com") == 0) {
766
0
        return SSH_KEYTYPE_SK_ED25519;
767
187
    } else if(strcmp(name, "sk-ssh-ed25519-cert-v01@openssh.com") == 0) {
768
1
        return SSH_KEYTYPE_SK_ED25519_CERT01;
769
1
    }
770
771
186
    return SSH_KEYTYPE_UNKNOWN;
772
212
}
773
774
/**
775
 * @brief Get the public key type corresponding to a certificate type.
776
 *
777
 * @param[in] type   The certificate or public key type.
778
 *
779
 * @return           The matching public key type.
780
 */
781
enum ssh_keytypes_e ssh_key_type_plain(enum ssh_keytypes_e type)
782
0
{
783
0
    switch (type) {
784
0
        case SSH_KEYTYPE_RSA_CERT01:
785
0
            return SSH_KEYTYPE_RSA;
786
0
        case SSH_KEYTYPE_ECDSA_P256_CERT01:
787
0
            return SSH_KEYTYPE_ECDSA_P256;
788
0
        case SSH_KEYTYPE_ECDSA_P384_CERT01:
789
0
            return SSH_KEYTYPE_ECDSA_P384;
790
0
        case SSH_KEYTYPE_ECDSA_P521_CERT01:
791
0
            return SSH_KEYTYPE_ECDSA_P521;
792
0
        case SSH_KEYTYPE_ED25519_CERT01:
793
0
            return SSH_KEYTYPE_ED25519;
794
0
        case SSH_KEYTYPE_SK_ECDSA_CERT01:
795
0
            return SSH_KEYTYPE_SK_ECDSA;
796
0
        case SSH_KEYTYPE_SK_ED25519_CERT01:
797
0
            return SSH_KEYTYPE_SK_ED25519;
798
0
        default:
799
0
            return type;
800
0
    }
801
0
}
802
803
/**
804
 * @brief Check if the key has/is a public key.
805
 *
806
 * @param[in] k         The key to check.
807
 *
808
 * @return              1 if it is a public key, 0 if not.
809
 */
810
int ssh_key_is_public(const ssh_key k)
811
0
{
812
0
    if (k == NULL) {
813
0
        return 0;
814
0
    }
815
816
0
    return (k->flags & SSH_KEY_FLAG_PUBLIC) == SSH_KEY_FLAG_PUBLIC;
817
0
}
818
819
/**
820
 * @brief Check if the key is a private key.
821
 *
822
 * @param[in] k         The key to check.
823
 *
824
 * @return              1 if it is a private key, 0 if not.
825
 */
826
0
int ssh_key_is_private(const ssh_key k) {
827
0
    if (k == NULL) {
828
0
        return 0;
829
0
    }
830
831
0
    return (k->flags & SSH_KEY_FLAG_PRIVATE) == SSH_KEY_FLAG_PRIVATE;
832
0
}
833
834
/**
835
 * @brief Compare keys if they are equal.
836
 *
837
 * @param[in] k1        The first key to compare.
838
 *
839
 * @param[in] k2        The second key to compare.
840
 *
841
 * @param[in] what      What part or type of the key do you want to compare.
842
 *
843
 * @return              0 if equal, 1 if not.
844
 */
845
int ssh_key_cmp(const ssh_key k1,
846
                const ssh_key k2,
847
                enum ssh_keycmp_e what)
848
0
{
849
0
    if (k1 == NULL || k2 == NULL) {
850
0
        return 1;
851
0
    }
852
853
0
    if (ssh_key_type_plain(k1->type) != ssh_key_type_plain(k2->type)) {
854
0
        SSH_LOG(SSH_LOG_DEBUG, "key types don't match!");
855
0
        return 1;
856
0
    }
857
858
0
    if (what == SSH_KEY_CMP_PRIVATE) {
859
0
        if (!ssh_key_is_private(k1) ||
860
0
            !ssh_key_is_private(k2)) {
861
0
            return 1;
862
0
        }
863
0
    }
864
865
0
    if (is_sk_key_type(k1->type)) {
866
0
        if (ssh_string_cmp(k1->sk_application, k2->sk_application) != 0) {
867
0
            return 1;
868
0
        }
869
870
0
        if (ssh_string_cmp(k1->sk_user_id, k2->sk_user_id) != 0) {
871
0
            return 1;
872
0
        }
873
874
0
        if (what == SSH_KEY_CMP_PRIVATE) {
875
0
            if (k1->sk_flags != k2->sk_flags) {
876
0
                return 1;
877
0
            }
878
879
0
            if (ssh_string_cmp(k1->sk_key_handle, k2->sk_key_handle) != 0) {
880
0
                return 1;
881
0
            }
882
883
0
            if (ssh_string_cmp(k1->sk_reserved, k2->sk_reserved) != 0) {
884
0
                return 1;
885
0
            }
886
0
        }
887
0
    }
888
889
0
    if (what == SSH_KEY_CMP_CERTIFICATE) {
890
0
        if (!is_cert_type(k1->type) ||
891
0
            !is_cert_type(k2->type)) {
892
0
            return 1;
893
0
        }
894
0
        if (k1->cert == NULL || k2->cert == NULL) {
895
0
            return 1;
896
0
        }
897
0
        if (ssh_buffer_get_len(k1->cert) != ssh_buffer_get_len(k2->cert)) {
898
0
            return 1;
899
0
        }
900
0
        return memcmp(ssh_buffer_get(k1->cert),
901
0
                      ssh_buffer_get(k2->cert),
902
0
                      ssh_buffer_get_len(k1->cert));
903
0
    }
904
905
#ifndef HAVE_LIBCRYPTO
906
    if (k1->type == SSH_KEYTYPE_ED25519) {
907
        return pki_ed25519_key_cmp(k1, k2, what);
908
    } else if (k1->type == SSH_KEYTYPE_SK_ED25519) {
909
        return pki_ed25519_key_cmp(k1, k2, SSH_KEY_CMP_PUBLIC);
910
    }
911
#endif
912
913
0
    return pki_key_compare(k1, k2, what);
914
0
}
915
916
ssh_signature ssh_signature_new(void)
917
0
{
918
0
    struct ssh_signature_struct *sig = NULL;
919
920
0
    sig = calloc(1, sizeof(struct ssh_signature_struct));
921
0
    if (sig == NULL) {
922
0
        return NULL;
923
0
    }
924
925
0
    return sig;
926
0
}
927
928
void ssh_signature_free(ssh_signature sig)
929
0
{
930
0
    if (sig == NULL) {
931
0
        return;
932
0
    }
933
934
0
    switch(sig->type) {
935
0
        case SSH_KEYTYPE_RSA:
936
#ifdef HAVE_LIBGCRYPT
937
            gcry_sexp_release(sig->rsa_sig);
938
#elif defined HAVE_LIBMBEDCRYPTO
939
            SAFE_FREE(sig->rsa_sig);
940
#endif /* HAVE_LIBGCRYPT */
941
0
            break;
942
0
        case SSH_KEYTYPE_ECDSA_P256:
943
0
        case SSH_KEYTYPE_ECDSA_P384:
944
0
        case SSH_KEYTYPE_ECDSA_P521:
945
0
        case SSH_KEYTYPE_SK_ECDSA:
946
#ifdef HAVE_GCRYPT_ECC
947
            gcry_sexp_release(sig->ecdsa_sig);
948
#elif defined HAVE_LIBMBEDCRYPTO
949
            bignum_safe_free(sig->ecdsa_sig.r);
950
            bignum_safe_free(sig->ecdsa_sig.s);
951
#endif /* HAVE_GCRYPT_ECC */
952
0
            break;
953
0
        case SSH_KEYTYPE_ED25519:
954
0
        case SSH_KEYTYPE_SK_ED25519:
955
#ifndef HAVE_LIBCRYPTO
956
            /* When using OpenSSL, the signature is stored in sig->raw_sig */
957
            SAFE_FREE(sig->ed25519_sig);
958
#endif /* HAVE_LIBCRYPTO */
959
0
            break;
960
0
        case SSH_KEYTYPE_DSS:   /* deprecated */
961
0
        case SSH_KEYTYPE_DSS_CERT01:    /* deprecated */
962
0
        case SSH_KEYTYPE_RSA_CERT01:
963
0
        case SSH_KEYTYPE_ECDSA_P256_CERT01:
964
0
        case SSH_KEYTYPE_ECDSA_P384_CERT01:
965
0
        case SSH_KEYTYPE_ECDSA_P521_CERT01:
966
0
        case SSH_KEYTYPE_ED25519_CERT01:
967
0
        case SSH_KEYTYPE_SK_ECDSA_CERT01:
968
0
        case SSH_KEYTYPE_SK_ED25519_CERT01:
969
0
        case SSH_KEYTYPE_RSA1:
970
0
        case SSH_KEYTYPE_ECDSA:
971
0
        case SSH_KEYTYPE_UNKNOWN:
972
0
            break;
973
0
    }
974
975
    /* Explicitly zero the signature content before free */
976
0
    ssh_string_burn(sig->raw_sig);
977
0
    SSH_STRING_FREE(sig->raw_sig);
978
0
    SAFE_FREE(sig);
979
0
}
980
981
/**
982
 * @brief import a base64 formatted key from a memory c-string
983
 *
984
 * @param[in]  b64_key  The c-string holding the base64 encoded key
985
 *
986
 * @param[in]  passphrase The passphrase to decrypt the key, or NULL
987
 *
988
 * @param[in]  auth_fn  An auth function you may want to use or NULL.
989
 *
990
 * @param[in]  auth_data Private data passed to the auth function.
991
 *
992
 * @param[out] pkey     A pointer where the allocated key can be stored. You
993
 *                      need to free the memory using ssh_key_free()
994
 *
995
 * @return  SSH_ERROR in case of error, SSH_OK otherwise.
996
 *
997
 * @see ssh_key_free()
998
 */
999
int ssh_pki_import_privkey_base64(const char *b64_key,
1000
                                  const char *passphrase,
1001
                                  ssh_auth_callback auth_fn,
1002
                                  void *auth_data,
1003
                                  ssh_key *pkey)
1004
0
{
1005
0
    ssh_key key = NULL;
1006
0
    char *openssh_header = NULL;
1007
1008
0
    if (b64_key == NULL || pkey == NULL) {
1009
0
        return SSH_ERROR;
1010
0
    }
1011
1012
0
    if (b64_key == NULL || !*b64_key) {
1013
0
        return SSH_ERROR;
1014
0
    }
1015
1016
0
    SSH_LOG(SSH_LOG_DEBUG,
1017
0
            "Trying to decode privkey passphrase=%s",
1018
0
            passphrase ? "true" : "false");
1019
1020
    /* Test for OpenSSH key format first */
1021
0
    openssh_header = strstr(b64_key, OPENSSH_HEADER_BEGIN);
1022
0
    if (openssh_header != NULL) {
1023
0
        key = ssh_pki_openssh_privkey_import(openssh_header,
1024
0
                                             passphrase,
1025
0
                                             auth_fn,
1026
0
                                             auth_data);
1027
0
    } else {
1028
        /* fallback on PEM decoder */
1029
0
        key = pki_private_key_from_base64(b64_key,
1030
0
                                          passphrase,
1031
0
                                          auth_fn,
1032
0
                                          auth_data);
1033
0
    }
1034
0
    if (key == NULL) {
1035
0
        return SSH_ERROR;
1036
0
    }
1037
1038
0
    *pkey = key;
1039
1040
0
    return SSH_OK;
1041
0
}
1042
1043
1044
 /**
1045
 * @brief Convert a private key to a base64 encoded key in given format
1046
 *
1047
 * @param[in]  privkey  The private key to export.
1048
 *
1049
 * @param[in]  passphrase The passphrase to use to encrypt the key with or
1050
 *             NULL. An empty string means no passphrase.
1051
 *
1052
 * @param[in]  auth_fn  An auth function you may want to use or NULL.
1053
 *
1054
 * @param[in]  auth_data Private data passed to the auth function.
1055
 *
1056
 * @param[out] b64_key  A pointer to store the allocated base64 encoded key. You
1057
 *                      need to free the buffer using ssh_string_from_char().
1058
 *
1059
 * @param[in]  format   The file format (OpenSSH, PEM, or default)
1060
 *
1061
 * @return     SSH_OK on success, SSH_ERROR on error.
1062
 *
1063
 * @see ssh_string_free_char()
1064
 */
1065
int
1066
ssh_pki_export_privkey_base64_format(const ssh_key privkey,
1067
                                     const char *passphrase,
1068
                                     ssh_auth_callback auth_fn,
1069
                                     void *auth_data,
1070
                                     char **b64_key,
1071
                                     enum ssh_file_format_e format)
1072
0
{
1073
0
    ssh_string blob = NULL;
1074
0
    char *b64 = NULL;
1075
1076
0
    if (privkey == NULL || !ssh_key_is_private(privkey)) {
1077
0
        return SSH_ERROR;
1078
0
    }
1079
1080
    /*
1081
     * For historic reasons, the Ed25519 keys are exported in OpenSSH file
1082
     * format by default also when built with OpenSSL.
1083
     *
1084
     * The FIDO2/U2F security keys are an extension to the SSH protocol
1085
     * proposed by OpenSSH, and do not have any representation in PEM format.
1086
     * So, they are always exported in the OpenSSH file format.
1087
     */
1088
0
#ifdef HAVE_LIBCRYPTO
1089
0
    if (format == SSH_FILE_FORMAT_DEFAULT &&
1090
0
        privkey->type != SSH_KEYTYPE_ED25519 &&
1091
0
        !is_sk_key_type(privkey->type)) {
1092
0
        format = SSH_FILE_FORMAT_PEM;
1093
0
    }
1094
0
#endif /* HAVE_LIBCRYPTO */
1095
1096
0
    switch (format) {
1097
0
    case SSH_FILE_FORMAT_PEM:
1098
0
        blob = pki_private_key_to_pem(privkey,
1099
0
                                      passphrase,
1100
0
                                      auth_fn,
1101
0
                                      auth_data);
1102
0
        break;
1103
0
    case SSH_FILE_FORMAT_DEFAULT:
1104
        /* default except (OpenSSL && !ED25519) handled above */
1105
0
    case SSH_FILE_FORMAT_OPENSSH:
1106
0
        blob = ssh_pki_openssh_privkey_export(privkey,
1107
0
                                              passphrase,
1108
0
                                              auth_fn,
1109
0
                                              auth_data);
1110
0
        break;
1111
0
    }
1112
0
    if (blob == NULL) {
1113
0
        return SSH_ERROR;
1114
0
    }
1115
1116
0
    b64 = strndup(ssh_string_data(blob), ssh_string_len(blob));
1117
0
    SSH_STRING_FREE(blob);
1118
0
    if (b64 == NULL) {
1119
0
        return SSH_ERROR;
1120
0
    }
1121
1122
0
    *b64_key = b64;
1123
1124
0
    return SSH_OK;
1125
0
}
1126
1127
 /**
1128
 * @brief Convert a private key to a pem base64 encoded key, or OpenSSH format for
1129
 *        keytype ssh-ed25519
1130
 *
1131
 * @param[in]  privkey  The private key to export.
1132
 *
1133
 * @param[in]  passphrase The passphrase to use to encrypt the key with or
1134
 *             NULL. An empty string means no passphrase.
1135
 *
1136
 * @param[in]  auth_fn  An auth function you may want to use or NULL.
1137
 *
1138
 * @param[in]  auth_data Private data passed to the auth function.
1139
 *
1140
 * @param[out] b64_key  A pointer to store the allocated base64 encoded key. You
1141
 *                      need to free the buffer using ssh_string_from_char().
1142
 *
1143
 * @return     SSH_OK on success, SSH_ERROR on error.
1144
 *
1145
 * @see ssh_string_free_char()
1146
 */
1147
int ssh_pki_export_privkey_base64(const ssh_key privkey,
1148
                                  const char *passphrase,
1149
                                  ssh_auth_callback auth_fn,
1150
                                  void *auth_data,
1151
                                  char **b64_key)
1152
0
{
1153
0
    return ssh_pki_export_privkey_base64_format(privkey,
1154
0
                                                passphrase,
1155
0
                                                auth_fn,
1156
0
                                                auth_data,
1157
0
                                                b64_key,
1158
0
                                                SSH_FILE_FORMAT_DEFAULT);
1159
0
}
1160
1161
1162
1163
/**
1164
 * @brief Import a private key from a file or a PKCS #11 device.
1165
 *
1166
 * @param[in]  filename The filename of the private key or the
1167
 *                      PKCS #11 URI corresponding to the private key.
1168
 *
1169
 * @param[in]  passphrase The passphrase to decrypt the private key. Set to NULL
1170
 *                        if none is needed or it is unknown.
1171
 *
1172
 * @param[in]  auth_fn  An auth function you may want to use or NULL.
1173
 *
1174
 * @param[in]  auth_data Private data passed to the auth function.
1175
 *
1176
 * @param[out] pkey     A pointer to store the allocated ssh_key. You need to
1177
 *                      free the key using ssh_key_free().
1178
 *
1179
 * @returns SSH_OK on success, SSH_EOF if the file doesn't exist or permission
1180
 *          denied, SSH_ERROR otherwise.
1181
 *
1182
 * @see ssh_key_free()
1183
 **/
1184
int ssh_pki_import_privkey_file(const char *filename,
1185
                                const char *passphrase,
1186
                                ssh_auth_callback auth_fn,
1187
                                void *auth_data,
1188
0
                                ssh_key *pkey) {
1189
0
    struct stat sb;
1190
0
    char *key_buf = NULL;
1191
0
    FILE *file = NULL;
1192
0
    off_t size;
1193
0
    int rc;
1194
0
    char err_msg[SSH_ERRNO_MSG_MAX] = {0};
1195
1196
0
    if (pkey == NULL || filename == NULL || *filename == '\0') {
1197
0
        return SSH_ERROR;
1198
0
    }
1199
1200
#ifdef WITH_PKCS11_URI
1201
    if (ssh_pki_is_uri(filename)) {
1202
        rc = pki_uri_import(filename, pkey, SSH_KEY_PRIVATE);
1203
        return rc;
1204
    }
1205
#endif /* WITH_PKCS11_URI */
1206
1207
0
    file = fopen(filename, "rb");
1208
0
    if (file == NULL) {
1209
0
        SSH_LOG(SSH_LOG_TRACE,
1210
0
                "Error opening %s: %s",
1211
0
                filename,
1212
0
                ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
1213
0
        return SSH_EOF;
1214
0
    }
1215
1216
0
    rc = fstat(fileno(file), &sb);
1217
0
    if (rc < 0) {
1218
0
        fclose(file);
1219
0
        SSH_LOG(SSH_LOG_TRACE,
1220
0
                "Error getting stat of %s: %s",
1221
0
                filename,
1222
0
                ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
1223
0
        switch (errno) {
1224
0
            case ENOENT:
1225
0
            case EACCES:
1226
0
                return SSH_EOF;
1227
0
        }
1228
1229
0
        return SSH_ERROR;
1230
0
    }
1231
1232
0
    if (sb.st_size > MAX_PRIVKEY_SIZE) {
1233
0
        SSH_LOG(SSH_LOG_TRACE,
1234
0
                "Private key is bigger than 4M.");
1235
0
        fclose(file);
1236
0
        return SSH_ERROR;
1237
0
    }
1238
1239
0
    key_buf = malloc(sb.st_size + 1);
1240
0
    if (key_buf == NULL) {
1241
0
        fclose(file);
1242
0
        SSH_LOG(SSH_LOG_TRACE, "Out of memory!");
1243
0
        return SSH_ERROR;
1244
0
    }
1245
1246
0
    size = fread(key_buf, 1, sb.st_size, file);
1247
0
    fclose(file);
1248
1249
0
    if (size != sb.st_size) {
1250
0
        SAFE_FREE(key_buf);
1251
0
        SSH_LOG(SSH_LOG_TRACE,
1252
0
                "Error reading %s: %s",
1253
0
                filename,
1254
0
                ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
1255
0
        return SSH_ERROR;
1256
0
    }
1257
0
    key_buf[size] = 0;
1258
1259
0
    rc = ssh_pki_import_privkey_base64(key_buf,
1260
0
                                       passphrase,
1261
0
                                       auth_fn,
1262
0
                                       auth_data,
1263
0
                                       pkey);
1264
1265
0
    SAFE_FREE(key_buf);
1266
0
    return rc;
1267
0
}
1268
1269
/**
1270
 * @brief Export a private key to a file in format specified in the argument
1271
 *
1272
 * @param[in]  privkey  The private key to export.
1273
 *
1274
 * @param[in]  passphrase The passphrase to use to encrypt the key with or
1275
 *             NULL. An empty string means no passphrase.
1276
 *
1277
 * @param[in]  auth_fn  An auth function you may want to use or NULL.
1278
 *
1279
 * @param[in]  auth_data Private data passed to the auth function.
1280
 *
1281
 * @param[in]  filename  The path where to store the pem file.
1282
 *
1283
 * @param[in]  format    The file format (OpenSSH, PEM, or default)
1284
 *
1285
 * @return     SSH_OK on success, SSH_ERROR on error.
1286
 */
1287
1288
int
1289
ssh_pki_export_privkey_file_format(const ssh_key privkey,
1290
                                   const char *passphrase,
1291
                                   ssh_auth_callback auth_fn,
1292
                                   void *auth_data,
1293
                                   const char *filename,
1294
                                   enum ssh_file_format_e format)
1295
0
{
1296
0
    ssh_string blob = NULL;
1297
0
    FILE *fp = NULL;
1298
0
    int rc;
1299
1300
0
    if (privkey == NULL || !ssh_key_is_private(privkey)) {
1301
0
        return SSH_ERROR;
1302
0
    }
1303
1304
0
    fp = fopen(filename, "wb");
1305
0
    if (fp == NULL) {
1306
0
        char err_msg[SSH_ERRNO_MSG_MAX] = {0};
1307
0
        SSH_LOG(SSH_LOG_FUNCTIONS, "Error opening %s: %s",
1308
0
                filename, ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
1309
0
        return SSH_EOF;
1310
0
    }
1311
1312
    /*
1313
     * For historic reasons, the Ed25519 keys are exported in OpenSSH file
1314
     * format by default also when built with OpenSSL.
1315
     *
1316
     * The FIDO2/U2F security keys are an extension to the SSH protocol
1317
     * proposed by OpenSSH, and do not have any representation in PEM format.
1318
     * So, they are always exported in the OpenSSH file format.
1319
     */
1320
0
#ifdef HAVE_LIBCRYPTO
1321
0
    if (format == SSH_FILE_FORMAT_DEFAULT &&
1322
0
        privkey->type != SSH_KEYTYPE_ED25519 &&
1323
0
        !is_sk_key_type(privkey->type)) {
1324
1325
0
        format = SSH_FILE_FORMAT_PEM;
1326
0
    }
1327
0
#endif /* HAVE_LIBCRYPTO */
1328
1329
0
    switch (format) {
1330
0
    case SSH_FILE_FORMAT_PEM:
1331
0
        blob = pki_private_key_to_pem(privkey,
1332
0
                                      passphrase,
1333
0
                                      auth_fn,
1334
0
                                      auth_data);
1335
0
        break;
1336
0
    case SSH_FILE_FORMAT_DEFAULT:
1337
        /* default except (OpenSSL && !ED25519) handled above */
1338
0
    case SSH_FILE_FORMAT_OPENSSH:
1339
0
        blob = ssh_pki_openssh_privkey_export(privkey,
1340
0
                                              passphrase,
1341
0
                                              auth_fn,
1342
0
                                              auth_data);
1343
0
        break;
1344
0
    }
1345
0
    if (blob == NULL) {
1346
0
        fclose(fp);
1347
0
        return -1;
1348
0
    }
1349
1350
0
    rc = fwrite(ssh_string_data(blob), ssh_string_len(blob), 1, fp);
1351
0
    SSH_STRING_FREE(blob);
1352
0
    if (rc != 1 || ferror(fp)) {
1353
0
        fclose(fp);
1354
0
        unlink(filename);
1355
0
        return SSH_ERROR;
1356
0
    }
1357
0
    fclose(fp);
1358
1359
0
    return SSH_OK;
1360
0
}
1361
1362
/**
1363
 * @brief Export a private key to a pem file on disk, or OpenSSH format for
1364
 *        keytype ssh-ed25519
1365
 *
1366
 * @param[in]  privkey  The private key to export.
1367
 *
1368
 * @param[in]  passphrase The passphrase to use to encrypt the key with or
1369
 *             NULL. An empty string means no passphrase.
1370
 *
1371
 * @param[in]  auth_fn  An auth function you may want to use or NULL.
1372
 *
1373
 * @param[in]  auth_data Private data passed to the auth function.
1374
 *
1375
 * @param[in]  filename  The path where to store the pem file.
1376
 *
1377
 * @return     SSH_OK on success, SSH_ERROR on error.
1378
 */
1379
int
1380
ssh_pki_export_privkey_file(const ssh_key privkey,
1381
                            const char *passphrase,
1382
                            ssh_auth_callback auth_fn,
1383
                            void *auth_data,
1384
                            const char *filename)
1385
0
{
1386
0
    return ssh_pki_export_privkey_file_format(privkey,
1387
0
                                              passphrase,
1388
0
                                              auth_fn,
1389
0
                                              auth_data,
1390
0
                                              filename,
1391
0
                                              SSH_FILE_FORMAT_DEFAULT);
1392
0
}
1393
1394
/* temporary function to migrate seamlessly to ssh_key */
1395
ssh_public_key ssh_pki_convert_key_to_publickey(const ssh_key key)
1396
0
{
1397
0
    ssh_public_key pub = NULL;
1398
0
    ssh_key tmp = NULL;
1399
1400
0
    if (key == NULL) {
1401
0
        return NULL;
1402
0
    }
1403
1404
0
    tmp = ssh_key_dup(key);
1405
0
    if (tmp == NULL) {
1406
0
        return NULL;
1407
0
    }
1408
1409
0
    pub = calloc(1, sizeof(struct ssh_public_key_struct));
1410
0
    if (pub == NULL) {
1411
0
        ssh_key_free(tmp);
1412
0
        return NULL;
1413
0
    }
1414
1415
0
    pub->type = tmp->type;
1416
0
    pub->type_c = tmp->type_c;
1417
1418
#if defined(HAVE_LIBMBEDCRYPTO)
1419
    pub->rsa_pub = tmp->pk;
1420
    tmp->pk = NULL;
1421
#elif defined(HAVE_LIBCRYPTO)
1422
    pub->key_pub = tmp->key;
1423
0
    tmp->key = NULL;
1424
#else
1425
    pub->rsa_pub = tmp->rsa;
1426
    tmp->rsa = NULL;
1427
#endif /* HAVE_LIBCRYPTO */
1428
1429
0
    ssh_key_free(tmp);
1430
1431
0
    return pub;
1432
0
}
1433
1434
ssh_private_key ssh_pki_convert_key_to_privatekey(const ssh_key key)
1435
0
{
1436
0
    ssh_private_key privkey = NULL;
1437
1438
0
    privkey = calloc(1, sizeof(struct ssh_private_key_struct));
1439
0
    if (privkey == NULL) {
1440
0
        ssh_key_free(key);
1441
0
        return NULL;
1442
0
    }
1443
1444
0
    privkey->type = key->type;
1445
#if defined(HAVE_LIBMBEDCRYPTO)
1446
    privkey->rsa_priv = key->pk;
1447
#elif defined(HAVE_LIBCRYPTO)
1448
    privkey->key_priv = key->key;
1449
#else
1450
    privkey->rsa_priv = key->rsa;
1451
#endif /* HAVE_LIBCRYPTO */
1452
1453
0
    return privkey;
1454
0
}
1455
1456
int pki_import_privkey_buffer(enum ssh_keytypes_e type,
1457
                              ssh_buffer buffer,
1458
                              ssh_key *pkey)
1459
0
{
1460
0
    ssh_key key = NULL;
1461
0
    int rc;
1462
1463
0
    key = ssh_key_new();
1464
0
    if (key == NULL) {
1465
0
        return SSH_ERROR;
1466
0
    }
1467
1468
0
    key->type = type;
1469
0
    key->type_c = ssh_key_type_to_char(type);
1470
0
    key->flags = SSH_KEY_FLAG_PRIVATE | SSH_KEY_FLAG_PUBLIC;
1471
1472
0
    switch (type) {
1473
0
    case SSH_KEYTYPE_RSA: {
1474
0
        ssh_string n = NULL;
1475
0
        ssh_string e = NULL;
1476
0
        ssh_string d = NULL;
1477
0
        ssh_string iqmp = NULL;
1478
0
        ssh_string p = NULL;
1479
0
        ssh_string q = NULL;
1480
1481
0
        rc = ssh_buffer_unpack(buffer, "SSSSSS", &n, &e, &d, &iqmp, &p, &q);
1482
0
        if (rc != SSH_OK) {
1483
0
            SSH_LOG(SSH_LOG_TRACE, "Unpack error");
1484
0
            goto fail;
1485
0
        }
1486
1487
0
        rc = pki_privkey_build_rsa(key, n, e, d, iqmp, p, q);
1488
#ifdef DEBUG_CRYPTO
1489
        ssh_log_hexdump("n", ssh_string_data(n), ssh_string_len(n));
1490
        ssh_log_hexdump("e", ssh_string_data(e), ssh_string_len(e));
1491
        ssh_log_hexdump("d", ssh_string_data(d), ssh_string_len(d));
1492
        ssh_log_hexdump("iqmp", ssh_string_data(iqmp), ssh_string_len(iqmp));
1493
        ssh_log_hexdump("p", ssh_string_data(p), ssh_string_len(p));
1494
        ssh_log_hexdump("q", ssh_string_data(q), ssh_string_len(q));
1495
#endif /* DEBUG_CRYPTO */
1496
0
        ssh_string_burn(n);
1497
0
        SSH_STRING_FREE(n);
1498
0
        ssh_string_burn(e);
1499
0
        SSH_STRING_FREE(e);
1500
0
        ssh_string_burn(d);
1501
0
        SSH_STRING_FREE(d);
1502
0
        ssh_string_burn(iqmp);
1503
0
        SSH_STRING_FREE(iqmp);
1504
0
        ssh_string_burn(p);
1505
0
        SSH_STRING_FREE(p);
1506
0
        ssh_string_burn(q);
1507
0
        SSH_STRING_FREE(q);
1508
0
        if (rc == SSH_ERROR) {
1509
0
            SSH_LOG(SSH_LOG_TRACE, "Failed to build RSA private key");
1510
0
            goto fail;
1511
0
        }
1512
0
        break;
1513
0
    }
1514
0
#ifdef HAVE_ECC
1515
0
    case SSH_KEYTYPE_ECDSA_P256:
1516
0
    case SSH_KEYTYPE_ECDSA_P384:
1517
0
    case SSH_KEYTYPE_ECDSA_P521: {
1518
0
        ssh_string e = NULL;
1519
0
        ssh_string exp = NULL;
1520
0
        ssh_string i = NULL;
1521
0
        int nid;
1522
1523
0
        rc = ssh_buffer_unpack(buffer, "SSS", &i, &e, &exp);
1524
0
        if (rc != SSH_OK) {
1525
0
            SSH_LOG(SSH_LOG_TRACE, "Unpack error");
1526
0
            goto fail;
1527
0
        }
1528
1529
0
        nid = pki_key_ecdsa_nid_from_name(ssh_string_get_char(i));
1530
0
        SSH_STRING_FREE(i);
1531
0
        if (nid == -1) {
1532
0
            ssh_string_burn(e);
1533
0
            SSH_STRING_FREE(e);
1534
0
            ssh_string_burn(exp);
1535
0
            SSH_STRING_FREE(exp);
1536
0
            goto fail;
1537
0
        }
1538
1539
0
        rc = pki_privkey_build_ecdsa(key, nid, e, exp);
1540
0
        ssh_string_burn(e);
1541
0
        SSH_STRING_FREE(e);
1542
0
        ssh_string_burn(exp);
1543
0
        SSH_STRING_FREE(exp);
1544
0
        if (rc < 0) {
1545
0
            SSH_LOG(SSH_LOG_TRACE, "Failed to build ECDSA private key");
1546
0
            goto fail;
1547
0
        }
1548
0
        break;
1549
0
    }
1550
0
    case SSH_KEYTYPE_SK_ECDSA: {
1551
0
        ssh_string type_str = NULL;
1552
0
        ssh_string pubkey = NULL;
1553
0
        int nid;
1554
1555
0
        rc = ssh_buffer_unpack(buffer, "SS", &type_str, &pubkey);
1556
0
        if (rc != SSH_OK) {
1557
0
            goto fail;
1558
0
        }
1559
1560
0
        rc = pki_buffer_unpack_sk_priv_data(buffer, key);
1561
0
        if (rc != SSH_OK) {
1562
0
            SSH_STRING_FREE(type_str);
1563
0
            SSH_STRING_FREE(pubkey);
1564
0
            goto fail;
1565
0
        }
1566
1567
0
        nid = pki_key_ecdsa_nid_from_name(ssh_string_get_char(type_str));
1568
0
        SSH_STRING_FREE(type_str);
1569
1570
0
        if (nid == -1) {
1571
0
            SSH_STRING_FREE(pubkey);
1572
0
            goto fail;
1573
0
        }
1574
1575
0
        rc = pki_pubkey_build_ecdsa(key, nid, pubkey);
1576
0
        SSH_STRING_FREE(pubkey);
1577
0
        if (rc != SSH_OK) {
1578
0
            goto fail;
1579
0
        }
1580
0
        break;
1581
0
    }
1582
0
#endif /* HAVE_ECC */
1583
0
    case SSH_KEYTYPE_ED25519: {
1584
0
        ssh_string pubkey = NULL, privkey = NULL;
1585
1586
0
        if (ssh_fips_mode()) {
1587
0
            SSH_LOG(SSH_LOG_TRACE, "Ed25519 keys not supported in FIPS mode");
1588
0
            goto fail;
1589
0
        }
1590
1591
0
        rc = ssh_buffer_unpack(buffer, "SS", &pubkey, &privkey);
1592
0
        if (rc != SSH_OK) {
1593
0
            SSH_LOG(SSH_LOG_TRACE, "Unpack error");
1594
0
            goto fail;
1595
0
        }
1596
1597
0
        rc = pki_privkey_build_ed25519(key, pubkey, privkey);
1598
0
        ssh_string_burn(privkey);
1599
0
        SSH_STRING_FREE(privkey);
1600
0
        SSH_STRING_FREE(pubkey);
1601
0
        if (rc != SSH_OK) {
1602
0
            SSH_LOG(SSH_LOG_TRACE, "Failed to build ed25519 key");
1603
0
            goto fail;
1604
0
        }
1605
0
        break;
1606
0
    }
1607
0
    case SSH_KEYTYPE_SK_ED25519: {
1608
0
        ssh_string pubkey = NULL;
1609
1610
0
        if (ssh_fips_mode()) {
1611
0
            SSH_LOG(SSH_LOG_TRACE, "Ed25519 keys not supported in FIPS mode");
1612
0
            goto fail;
1613
0
        }
1614
1615
0
        rc = ssh_buffer_unpack(buffer, "S", &pubkey);
1616
0
        if (rc != SSH_OK) {
1617
0
            goto fail;
1618
0
        }
1619
1620
0
        rc = pki_buffer_unpack_sk_priv_data(buffer, key);
1621
0
        if (rc != SSH_OK) {
1622
0
            SSH_STRING_FREE(pubkey);
1623
0
            goto fail;
1624
0
        }
1625
1626
0
        rc = pki_pubkey_build_ed25519(key, pubkey);
1627
0
        SSH_STRING_FREE(pubkey);
1628
0
        if (rc != SSH_OK) {
1629
0
            goto fail;
1630
0
        }
1631
0
        break;
1632
0
    }
1633
0
    case SSH_KEYTYPE_RSA_CERT01:
1634
0
    case SSH_KEYTYPE_ECDSA_P256_CERT01:
1635
0
    case SSH_KEYTYPE_ECDSA_P384_CERT01:
1636
0
    case SSH_KEYTYPE_ECDSA_P521_CERT01:
1637
0
    case SSH_KEYTYPE_ED25519_CERT01:
1638
0
    case SSH_KEYTYPE_SK_ECDSA_CERT01:
1639
0
    case SSH_KEYTYPE_SK_ED25519_CERT01:
1640
0
    case SSH_KEYTYPE_RSA1:
1641
0
    case SSH_KEYTYPE_UNKNOWN:
1642
0
    default:
1643
0
        SSH_LOG(SSH_LOG_TRACE, "Unknown private key type (%d)", type);
1644
0
        goto fail;
1645
0
    }
1646
1647
0
    *pkey = key;
1648
0
    return SSH_OK;
1649
0
fail:
1650
0
    ssh_key_free(key);
1651
1652
0
    return SSH_ERROR;
1653
0
}
1654
1655
static int pki_import_pubkey_buffer(ssh_buffer buffer,
1656
                                    enum ssh_keytypes_e type,
1657
                                    ssh_key *pkey)
1658
6
{
1659
6
    ssh_key key = NULL;
1660
6
    int rc;
1661
1662
6
    key = ssh_key_new();
1663
6
    if (key == NULL) {
1664
0
        return SSH_ERROR;
1665
0
    }
1666
1667
6
    key->type = type;
1668
6
    key->type_c = ssh_key_type_to_char(type);
1669
6
    key->flags = SSH_KEY_FLAG_PUBLIC;
1670
1671
6
    switch (type) {
1672
0
        case SSH_KEYTYPE_RSA:
1673
0
            {
1674
0
                ssh_string e = NULL;
1675
0
                ssh_string n = NULL;
1676
1677
0
                rc = ssh_buffer_unpack(buffer, "SS", &e, &n);
1678
0
                if (rc != SSH_OK) {
1679
0
                    SSH_LOG(SSH_LOG_TRACE, "Unpack error");
1680
0
                    goto fail;
1681
0
                }
1682
1683
0
                rc = pki_pubkey_build_rsa(key, e, n);
1684
#ifdef DEBUG_CRYPTO
1685
                ssh_log_hexdump("e", ssh_string_data(e), ssh_string_len(e));
1686
                ssh_log_hexdump("n", ssh_string_data(n), ssh_string_len(n));
1687
#endif /* DEBUG_CRYPTO */
1688
0
                ssh_string_burn(e);
1689
0
                SSH_STRING_FREE(e);
1690
0
                ssh_string_burn(n);
1691
0
                SSH_STRING_FREE(n);
1692
0
                if (rc == SSH_ERROR) {
1693
0
                    SSH_LOG(SSH_LOG_TRACE, "Failed to build RSA public key");
1694
0
                    goto fail;
1695
0
                }
1696
0
            }
1697
0
            break;
1698
0
#ifdef HAVE_ECC
1699
0
        case SSH_KEYTYPE_ECDSA: /* deprecated */
1700
0
        case SSH_KEYTYPE_ECDSA_P256:
1701
0
        case SSH_KEYTYPE_ECDSA_P384:
1702
0
        case SSH_KEYTYPE_ECDSA_P521:
1703
3
        case SSH_KEYTYPE_SK_ECDSA:
1704
3
            {
1705
3
                ssh_string e = NULL;
1706
3
                ssh_string i = NULL;
1707
3
                int nid;
1708
1709
3
                rc = ssh_buffer_unpack(buffer, "SS", &i, &e);
1710
3
                if (rc != SSH_OK) {
1711
3
                    SSH_LOG(SSH_LOG_TRACE, "Unpack error");
1712
3
                    goto fail;
1713
3
                }
1714
1715
0
                nid = pki_key_ecdsa_nid_from_name(ssh_string_get_char(i));
1716
0
                SSH_STRING_FREE(i);
1717
0
                if (nid == -1) {
1718
0
                    ssh_string_burn(e);
1719
0
                    SSH_STRING_FREE(e);
1720
0
                    goto fail;
1721
0
                }
1722
1723
0
                rc = pki_pubkey_build_ecdsa(key, nid, e);
1724
0
                ssh_string_burn(e);
1725
0
                SSH_STRING_FREE(e);
1726
0
                if (rc < 0) {
1727
0
                    SSH_LOG(SSH_LOG_TRACE, "Failed to build ECDSA public key");
1728
0
                    goto fail;
1729
0
                }
1730
1731
                /* Unpack SK specific parameters */
1732
0
                if (type == SSH_KEYTYPE_SK_ECDSA) {
1733
0
                    ssh_string application = ssh_buffer_get_ssh_string(buffer);
1734
0
                    if (application == NULL) {
1735
0
                        SSH_LOG(SSH_LOG_TRACE, "SK Unpack error");
1736
0
                        goto fail;
1737
0
                    }
1738
0
                    key->sk_application = application;
1739
0
                    key->type_c = ssh_key_type_to_char(key->type);
1740
0
                }
1741
0
            }
1742
0
            break;
1743
0
#endif /* HAVE_ECC */
1744
3
        case SSH_KEYTYPE_ED25519:
1745
3
        case SSH_KEYTYPE_SK_ED25519:
1746
3
        {
1747
3
            ssh_string pubkey = ssh_buffer_get_ssh_string(buffer);
1748
1749
3
            if (ssh_string_len(pubkey) != ED25519_KEY_LEN) {
1750
2
                SSH_LOG(SSH_LOG_TRACE, "Invalid public key length");
1751
2
                ssh_string_burn(pubkey);
1752
2
                SSH_STRING_FREE(pubkey);
1753
2
                goto fail;
1754
2
            }
1755
1756
1
            rc = pki_pubkey_build_ed25519(key, pubkey);
1757
1
            ssh_string_burn(pubkey);
1758
1
            SSH_STRING_FREE(pubkey);
1759
1
            if (rc < 0) {
1760
0
                SSH_LOG(SSH_LOG_TRACE, "Failed to build ED25519 public key");
1761
0
                goto fail;
1762
0
            }
1763
1764
1
            if (type == SSH_KEYTYPE_SK_ED25519) {
1765
0
                ssh_string application = ssh_buffer_get_ssh_string(buffer);
1766
0
                if (application == NULL) {
1767
0
                    SSH_LOG(SSH_LOG_TRACE, "SK Unpack error");
1768
0
                    goto fail;
1769
0
                }
1770
0
                key->sk_application = application;
1771
0
            }
1772
1
        }
1773
1
        break;
1774
1
        case SSH_KEYTYPE_RSA_CERT01:
1775
0
        case SSH_KEYTYPE_ECDSA_P256_CERT01:
1776
0
        case SSH_KEYTYPE_ECDSA_P384_CERT01:
1777
0
        case SSH_KEYTYPE_ECDSA_P521_CERT01:
1778
0
        case SSH_KEYTYPE_SK_ECDSA_CERT01:
1779
0
        case SSH_KEYTYPE_ED25519_CERT01:
1780
0
        case SSH_KEYTYPE_SK_ED25519_CERT01:
1781
0
        case SSH_KEYTYPE_RSA1:
1782
0
        case SSH_KEYTYPE_UNKNOWN:
1783
0
        default:
1784
0
            SSH_LOG(SSH_LOG_TRACE, "Unknown public key type %d", type);
1785
0
            goto fail;
1786
6
    }
1787
1788
1
    *pkey = key;
1789
1
    return SSH_OK;
1790
5
fail:
1791
5
    ssh_key_free(key);
1792
1793
5
    return SSH_ERROR;
1794
6
}
1795
1796
static int pki_import_cert_buffer(ssh_buffer buffer,
1797
                                  enum ssh_keytypes_e type,
1798
                                  ssh_key *pkey)
1799
4
{
1800
4
    ssh_buffer cert = NULL;
1801
4
    ssh_string tmp_s = NULL;
1802
4
    const char *type_c = NULL;
1803
4
    ssh_key key = NULL;
1804
4
    int rc;
1805
1806
    /*
1807
     * The cert blob starts with the key type as an ssh_string, but this
1808
     * string has been read out of the buffer to identify the key type.
1809
     * Simply add it again as first element before copying the rest.
1810
     */
1811
4
    cert = ssh_buffer_new();
1812
4
    if (cert == NULL) {
1813
0
        goto fail;
1814
0
    }
1815
4
    type_c = ssh_key_type_to_char(type);
1816
4
    tmp_s = ssh_string_from_char(type_c);
1817
4
    if (tmp_s == NULL) {
1818
0
        goto fail;
1819
0
    }
1820
4
    rc = ssh_buffer_add_ssh_string(cert, tmp_s);
1821
4
    SSH_STRING_FREE(tmp_s);
1822
4
    if (rc != 0) {
1823
0
        goto fail;
1824
0
    }
1825
4
    rc = ssh_buffer_add_buffer(cert, buffer);
1826
4
    if (rc != 0) {
1827
0
        goto fail;
1828
0
    }
1829
1830
    /*
1831
     * After the key type, comes an ssh_string nonce. Just after this comes the
1832
     * cert public key, which can be parsed out of the buffer.
1833
     */
1834
4
    tmp_s = ssh_buffer_get_ssh_string(buffer);
1835
4
    if (tmp_s == NULL) {
1836
1
        goto fail;
1837
1
    }
1838
3
    SSH_STRING_FREE(tmp_s);
1839
1840
3
    switch (type) {
1841
0
        case SSH_KEYTYPE_RSA_CERT01:
1842
0
            rc = pki_import_pubkey_buffer(buffer, SSH_KEYTYPE_RSA, &key);
1843
0
            break;
1844
0
        case SSH_KEYTYPE_ECDSA_P256_CERT01:
1845
0
            rc = pki_import_pubkey_buffer(buffer, SSH_KEYTYPE_ECDSA_P256, &key);
1846
0
            break;
1847
0
        case SSH_KEYTYPE_ECDSA_P384_CERT01:
1848
0
            rc = pki_import_pubkey_buffer(buffer, SSH_KEYTYPE_ECDSA_P384, &key);
1849
0
            break;
1850
0
        case SSH_KEYTYPE_ECDSA_P521_CERT01:
1851
0
            rc = pki_import_pubkey_buffer(buffer, SSH_KEYTYPE_ECDSA_P521, &key);
1852
0
            break;
1853
0
        case SSH_KEYTYPE_ED25519_CERT01:
1854
0
            rc = pki_import_pubkey_buffer(buffer, SSH_KEYTYPE_ED25519, &key);
1855
0
            break;
1856
3
        case SSH_KEYTYPE_SK_ECDSA_CERT01:
1857
3
            rc = pki_import_pubkey_buffer(buffer, SSH_KEYTYPE_SK_ECDSA, &key);
1858
3
            break;
1859
0
        case SSH_KEYTYPE_SK_ED25519_CERT01:
1860
0
            rc = pki_import_pubkey_buffer(buffer, SSH_KEYTYPE_SK_ED25519, &key);
1861
0
            break;
1862
0
        default:
1863
0
            key = ssh_key_new();
1864
3
    }
1865
3
    if (rc != 0 || key == NULL) {
1866
3
        goto fail;
1867
3
    }
1868
1869
0
    key->type = type;
1870
0
    key->type_c = type_c;
1871
0
    key->cert = cert;
1872
1873
0
    *pkey = key;
1874
0
    return SSH_OK;
1875
1876
4
fail:
1877
4
    ssh_key_free(key);
1878
4
    SSH_BUFFER_FREE(cert);
1879
4
    return SSH_ERROR;
1880
3
}
1881
1882
/**
1883
 * @brief Import a base64 formatted public key from a memory c-string.
1884
 *
1885
 * @param[in]  b64_key  The base64 key to format.
1886
 *
1887
 * @param[in]  type     The type of the key to format.
1888
 *
1889
 * @param[out] pkey     A pointer where the allocated key can be stored. You
1890
 *                      need to free the memory using ssh_key_free().
1891
 *
1892
 * @return              SSH_OK on success, SSH_ERROR on error.
1893
 *
1894
 * @see ssh_key_free()
1895
 */
1896
int ssh_pki_import_pubkey_base64(const char *b64_key,
1897
                                 enum ssh_keytypes_e type,
1898
                                 ssh_key *pkey)
1899
23
{
1900
23
    ssh_buffer buffer = NULL;
1901
23
    ssh_string type_s = NULL;
1902
23
    int rc;
1903
1904
23
    if (b64_key == NULL || pkey == NULL) {
1905
0
        return SSH_ERROR;
1906
0
    }
1907
1908
23
    buffer = base64_to_bin(b64_key);
1909
23
    if (buffer == NULL) {
1910
15
        return SSH_ERROR;
1911
15
    }
1912
1913
8
    type_s = ssh_buffer_get_ssh_string(buffer);
1914
8
    if (type_s == NULL) {
1915
1
        SSH_BUFFER_FREE(buffer);
1916
1
        return SSH_ERROR;
1917
1
    }
1918
7
    SSH_STRING_FREE(type_s);
1919
1920
7
    if (is_cert_type(type)) {
1921
4
        rc = pki_import_cert_buffer(buffer, type, pkey);
1922
4
    } else {
1923
3
        rc = pki_import_pubkey_buffer(buffer, type, pkey);
1924
3
    }
1925
7
    SSH_BUFFER_FREE(buffer);
1926
1927
7
    return rc;
1928
8
}
1929
1930
/**
1931
 * @internal
1932
 *
1933
 * @brief Import a public key from a ssh string.
1934
 *
1935
 * @param[in]  key_blob The key blob to import as specified in RFC 4253 section
1936
 *                      6.6 "Public Key Algorithms".
1937
 *
1938
 * @param[out] pkey     A pointer where the allocated key can be stored. You
1939
 *                      need to free the memory using ssh_key_free().
1940
 *
1941
 * @return              SSH_OK on success, SSH_ERROR on error.
1942
 *
1943
 * @see ssh_key_free()
1944
 */
1945
int ssh_pki_import_pubkey_blob(const ssh_string key_blob,
1946
                               ssh_key *pkey)
1947
0
{
1948
0
    ssh_buffer buffer = NULL;
1949
0
    ssh_string type_s = NULL;
1950
0
    enum ssh_keytypes_e type;
1951
0
    int rc;
1952
1953
0
    if (key_blob == NULL || pkey == NULL) {
1954
0
        return SSH_ERROR;
1955
0
    }
1956
1957
0
    buffer = ssh_buffer_new();
1958
0
    if (buffer == NULL) {
1959
0
        SSH_LOG(SSH_LOG_TRACE, "Out of memory!");
1960
0
        return SSH_ERROR;
1961
0
    }
1962
1963
0
    rc = ssh_buffer_add_data(buffer,
1964
0
                             ssh_string_data(key_blob),
1965
0
                             (uint32_t)ssh_string_len(key_blob));
1966
0
    if (rc < 0) {
1967
0
        SSH_LOG(SSH_LOG_TRACE, "Out of memory!");
1968
0
        goto fail;
1969
0
    }
1970
1971
0
    type_s = ssh_buffer_get_ssh_string(buffer);
1972
0
    if (type_s == NULL) {
1973
0
        SSH_LOG(SSH_LOG_TRACE, "Out of memory!");
1974
0
        goto fail;
1975
0
    }
1976
1977
0
    type = ssh_key_type_from_name(ssh_string_get_char(type_s));
1978
0
    if (type == SSH_KEYTYPE_UNKNOWN) {
1979
0
        SSH_LOG(SSH_LOG_TRACE, "Unknown key type found!");
1980
0
        goto fail;
1981
0
    }
1982
0
    SSH_STRING_FREE(type_s);
1983
1984
0
    if (is_cert_type(type)) {
1985
0
        rc = pki_import_cert_buffer(buffer, type, pkey);
1986
0
    } else {
1987
0
        rc = pki_import_pubkey_buffer(buffer, type, pkey);
1988
0
    }
1989
1990
0
    SSH_BUFFER_FREE(buffer);
1991
1992
0
    return rc;
1993
0
fail:
1994
0
    SSH_BUFFER_FREE(buffer);
1995
0
    SSH_STRING_FREE(type_s);
1996
1997
0
    return SSH_ERROR;
1998
0
}
1999
2000
#ifdef WITH_PKCS11_URI
2001
/**
2002
 *@brief Detect if the pathname in cmp is a PKCS #11 URI.
2003
 *
2004
 * @param[in] cmp The path to the public/private key
2005
 *                     or a private/public PKCS #11 URI.
2006
 *
2007
 * @returns true if filename is a URI starting with "pkcs11:"
2008
 *          false otherwise.
2009
 */
2010
bool ssh_pki_is_uri(const char *cmp)
2011
{
2012
    int rc;
2013
2014
    rc = strncmp(cmp, PKCS11_URI, strlen(PKCS11_URI));
2015
    if (rc == 0) {
2016
        return true;
2017
    }
2018
2019
    return false;
2020
}
2021
2022
/**
2023
 *@brief export a Public PKCS #11 URI from a Private PKCS #11 URI
2024
 *       by replacing "type=private" to "type=public".
2025
 *       TODO: Improve the parser
2026
 *
2027
 * @param[in] priv_uri Private PKCS #11 URI.
2028
 *
2029
 * @returns pointer to the public PKCS #11 URI. You need to free
2030
 *          the memory using ssh_string_free_char().
2031
 *
2032
 * @see ssh_string_free_char().
2033
 */
2034
char *ssh_pki_export_pub_uri_from_priv_uri(const char *priv_uri)
2035
{
2036
    char *pub_uri_temp = NULL;
2037
2038
    pub_uri_temp = ssh_strreplace(priv_uri,
2039
                                  "type=private",
2040
                                  "type=public");
2041
2042
    return pub_uri_temp;
2043
}
2044
#endif /* WITH_PKCS11_URI */
2045
2046
/**
2047
 * @brief Import a public key from a file or a PKCS #11 device.
2048
 *
2049
 * @param[in]  filename The filename of the public key or the
2050
 *                      PKCS #11 URI corresponding to the public key.
2051
 *
2052
 * @param[out] pkey     A pointer to store the allocated public key. You need to
2053
 *                      free the memory using ssh_key_free().
2054
 *
2055
 * @returns SSH_OK on success, SSH_EOF if the file doesn't exist or permission
2056
 *          denied, SSH_ERROR otherwise.
2057
 *
2058
 * @see ssh_key_free()
2059
 */
2060
int ssh_pki_import_pubkey_file(const char *filename, ssh_key *pkey)
2061
307
{
2062
307
    enum ssh_keytypes_e type;
2063
307
    struct stat sb;
2064
307
    char *key_buf = NULL, *p = NULL;
2065
307
    size_t buflen, i;
2066
307
    const char *q = NULL;
2067
307
    FILE *file = NULL;
2068
307
    off_t size;
2069
307
    int rc, cmp;
2070
307
    char err_msg[SSH_ERRNO_MSG_MAX] = {0};
2071
307
    ssh_key priv_key = NULL;
2072
2073
307
    if (pkey == NULL || filename == NULL || *filename == '\0') {
2074
0
        return SSH_ERROR;
2075
0
    }
2076
2077
#ifdef WITH_PKCS11_URI
2078
    if (ssh_pki_is_uri(filename)) {
2079
        rc = pki_uri_import(filename, pkey, SSH_KEY_PUBLIC);
2080
        return rc;
2081
    }
2082
#endif /* WITH_PKCS11_URI */
2083
2084
307
    file = fopen(filename, "rb");
2085
307
    if (file == NULL) {
2086
53
        SSH_LOG(SSH_LOG_TRACE, "Error opening %s: %s",
2087
53
                    filename, ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
2088
53
        return SSH_EOF;
2089
53
    }
2090
2091
254
    rc = fstat(fileno(file), &sb);
2092
254
    if (rc < 0) {
2093
0
        fclose(file);
2094
0
        SSH_LOG(SSH_LOG_TRACE, "Error gettint stat of %s: %s",
2095
0
                    filename, ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
2096
0
        switch (errno) {
2097
0
            case ENOENT:
2098
0
            case EACCES:
2099
0
                return SSH_EOF;
2100
0
        }
2101
0
        return SSH_ERROR;
2102
0
    }
2103
2104
254
    if (sb.st_size > MAX_PUBKEY_SIZE) {
2105
0
        fclose(file);
2106
0
        return SSH_ERROR;
2107
0
    }
2108
2109
254
    key_buf = malloc(sb.st_size + 1);
2110
254
    if (key_buf == NULL) {
2111
31
        fclose(file);
2112
31
        SSH_LOG(SSH_LOG_TRACE, "Out of memory!");
2113
31
        return SSH_ERROR;
2114
31
    }
2115
2116
223
    size = fread(key_buf, 1, sb.st_size, file);
2117
223
    fclose(file);
2118
2119
223
    if (size != sb.st_size) {
2120
0
        SAFE_FREE(key_buf);
2121
0
        SSH_LOG(SSH_LOG_TRACE, "Error reading %s: %s",
2122
0
                filename, ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
2123
0
        return SSH_ERROR;
2124
0
    }
2125
223
    key_buf[size] = '\0';
2126
223
    buflen = strlen(key_buf);
2127
2128
    /* Test for new OpenSSH key format first */
2129
223
    cmp = strncmp(key_buf, OPENSSH_HEADER_BEGIN, strlen(OPENSSH_HEADER_BEGIN));
2130
223
    if (cmp == 0) {
2131
11
        *pkey = ssh_pki_openssh_pubkey_import(key_buf);
2132
11
        SAFE_FREE(key_buf);
2133
11
        if (*pkey == NULL) {
2134
11
            SSH_LOG(SSH_LOG_TRACE, "Failed to import public key from OpenSSH"
2135
11
                                  " private key file");
2136
11
            return SSH_ERROR;
2137
11
        }
2138
0
        return SSH_OK;
2139
11
    }
2140
2141
    /*
2142
     * Try to parse key as PEM. Set empty passphrase, so user won't be prompted
2143
     * for passphrase. Don't try to decrypt encrypted private key.
2144
     */
2145
212
    priv_key = pki_private_key_from_base64(key_buf, "", NULL, NULL);
2146
212
    if (priv_key) {
2147
0
        rc = ssh_pki_export_privkey_to_pubkey(priv_key, pkey);
2148
0
        ssh_key_free(priv_key);
2149
0
        SAFE_FREE(key_buf);
2150
0
        if (rc != SSH_OK) {
2151
0
            SSH_LOG(SSH_LOG_WARN, "Failed to import public key from PEM"
2152
0
                                  " private key file");
2153
0
            return SSH_ERROR;
2154
0
        }
2155
0
        return SSH_OK;
2156
0
    }
2157
2158
    /* This the old one-line public key format */
2159
212
    q = p = key_buf;
2160
2.16M
    for (i = 0; i < buflen; i++) {
2161
2.16M
        if (isspace((int)p[i])) {
2162
76
            p[i] = '\0';
2163
76
            break;
2164
76
        }
2165
2.16M
    }
2166
2167
212
    type = ssh_key_type_from_name(q);
2168
212
    if (type == SSH_KEYTYPE_UNKNOWN) {
2169
186
        SAFE_FREE(key_buf);
2170
186
        return SSH_ERROR;
2171
186
    }
2172
2173
26
    if (i >= buflen) {
2174
3
        SAFE_FREE(key_buf);
2175
3
        return SSH_ERROR;
2176
3
    }
2177
23
    q = &p[i + 1];
2178
1.87M
    for (; i < buflen; i++) {
2179
1.87M
        if (isspace((int)p[i])) {
2180
9
            p[i] = '\0';
2181
9
            break;
2182
9
        }
2183
1.87M
    }
2184
2185
23
    rc = ssh_pki_import_pubkey_base64(q, type, pkey);
2186
23
    SAFE_FREE(key_buf);
2187
2188
23
    return rc;
2189
26
}
2190
2191
/**
2192
 * @brief Import a base64 formatted certificate from a memory c-string.
2193
 *
2194
 * @param[in]  b64_cert  The base64 cert to format.
2195
 *
2196
 * @param[in]  type     The type of the cert to format.
2197
 *
2198
 * @param[out] pkey     A pointer where the allocated key can be stored. You
2199
 *                      need to free the memory using ssh_key_free().
2200
 *
2201
 * @return              SSH_OK on success, SSH_ERROR on error.
2202
 *
2203
 * @see ssh_key_free()
2204
 */
2205
int ssh_pki_import_cert_base64(const char *b64_cert,
2206
                               enum ssh_keytypes_e type,
2207
                               ssh_key *pkey)
2208
0
{
2209
0
    return ssh_pki_import_pubkey_base64(b64_cert, type, pkey);
2210
0
}
2211
2212
/**
2213
 * @internal
2214
 *
2215
 * @brief Import a certificate from a ssh string.
2216
 *
2217
 * @param[in]  cert_blob The cert blob to import as specified in RFC 4253 section
2218
 *                      6.6 "Public Key Algorithms".
2219
 *
2220
 * @param[out] pkey     A pointer where the allocated key can be stored. You
2221
 *                      need to free the memory using ssh_key_free().
2222
 *
2223
 * @return              SSH_OK on success, SSH_ERROR on error.
2224
 *
2225
 * @see ssh_key_free()
2226
 */
2227
int ssh_pki_import_cert_blob(const ssh_string cert_blob,
2228
                             ssh_key *pkey)
2229
0
{
2230
0
    return ssh_pki_import_pubkey_blob(cert_blob, pkey);
2231
0
}
2232
2233
/**
2234
 * @brief Import a certificate from the given filename.
2235
 *
2236
 * @param[in]  filename The path to the certificate.
2237
 *
2238
 * @param[out] pkey     A pointer to store the allocated certificate. You need to
2239
 *                      free the memory using ssh_key_free().
2240
 *
2241
 * @returns SSH_OK on success, SSH_EOF if the file doesn't exist or permission
2242
 *          denied, SSH_ERROR otherwise.
2243
 *
2244
 * @see ssh_key_free()
2245
 */
2246
int ssh_pki_import_cert_file(const char *filename, ssh_key *pkey)
2247
0
{
2248
0
    int rc;
2249
2250
0
    rc = ssh_pki_import_pubkey_file(filename, pkey);
2251
0
    if (rc == SSH_OK) {
2252
        /* check the key is a cert type. */
2253
0
        if (!is_cert_type((*pkey)->type)) {
2254
0
            SSH_KEY_FREE(*pkey);
2255
0
            return SSH_ERROR;
2256
0
        }
2257
0
    }
2258
2259
0
    return rc;
2260
0
}
2261
2262
/**
2263
 * @internal
2264
 *
2265
 * @brief Internal function to generate a key pair.
2266
 *
2267
 * @param[in] type      Type of key to create
2268
 *
2269
 * @param[in] parameter Parameter to the creation of key:
2270
 *                      rsa : length of the key in bits (e.g. 1024, 2048, 4096)
2271
 *                      If parameter is 0, then the default size will be used.
2272
 * @param[out] pkey     A pointer to store the allocated private key. You need
2273
 *                      to free the memory using ssh_key_free().
2274
 *
2275
 * @return              SSH_OK on success, SSH_ERROR on error.
2276
 */
2277
static int pki_generate_key_internal(enum ssh_keytypes_e type,
2278
                                     int parameter,
2279
                                     ssh_key *pkey)
2280
0
{
2281
0
    int rc;
2282
0
    ssh_key key = NULL;
2283
2284
0
    if (pkey == NULL) {
2285
0
        return SSH_ERROR;
2286
0
    }
2287
2288
0
    key = ssh_key_new();
2289
0
    if (key == NULL) {
2290
0
        return SSH_ERROR;
2291
0
    }
2292
2293
0
    key->type = type;
2294
0
    key->type_c = ssh_key_type_to_char(type);
2295
0
    key->flags = SSH_KEY_FLAG_PRIVATE | SSH_KEY_FLAG_PUBLIC;
2296
2297
0
    switch(type){
2298
0
        case SSH_KEYTYPE_RSA:
2299
0
            if (parameter != 0 && parameter < RSA_MIN_KEY_SIZE) {
2300
0
                SSH_LOG(
2301
0
                    SSH_LOG_WARN,
2302
0
                    "RSA key size parameter (%d) is below minimum allowed (%d)",
2303
0
                    parameter,
2304
0
                    RSA_MIN_KEY_SIZE);
2305
0
                goto error;
2306
0
            }
2307
2308
0
            rc = pki_key_generate_rsa(key, parameter);
2309
0
            if(rc == SSH_ERROR)
2310
0
                goto error;
2311
0
            break;
2312
0
#ifdef HAVE_ECC
2313
0
        case SSH_KEYTYPE_ECDSA: /* deprecated */
2314
0
            rc = pki_key_generate_ecdsa(key, parameter);
2315
0
            if (rc == SSH_ERROR) {
2316
0
                goto error;
2317
0
            }
2318
2319
            /* Update key type */
2320
0
            key->type_c = ssh_pki_key_ecdsa_name(key);
2321
0
            break;
2322
0
        case SSH_KEYTYPE_ECDSA_P256:
2323
0
            rc = pki_key_generate_ecdsa(key, 256);
2324
0
            if (rc == SSH_ERROR) {
2325
0
                goto error;
2326
0
            }
2327
0
            break;
2328
0
        case SSH_KEYTYPE_ECDSA_P384:
2329
0
            rc = pki_key_generate_ecdsa(key, 384);
2330
0
            if (rc == SSH_ERROR) {
2331
0
                goto error;
2332
0
            }
2333
0
            break;
2334
0
        case SSH_KEYTYPE_ECDSA_P521:
2335
0
            rc = pki_key_generate_ecdsa(key, 521);
2336
0
            if (rc == SSH_ERROR) {
2337
0
                goto error;
2338
0
            }
2339
0
            break;
2340
0
#endif /* HAVE_ECC */
2341
0
        case SSH_KEYTYPE_ED25519:
2342
0
            rc = pki_key_generate_ed25519(key);
2343
0
            if (rc == SSH_ERROR) {
2344
0
                goto error;
2345
0
            }
2346
0
            break;
2347
0
        case SSH_KEYTYPE_RSA_CERT01:
2348
0
        case SSH_KEYTYPE_ECDSA_P256_CERT01:
2349
0
        case SSH_KEYTYPE_ECDSA_P384_CERT01:
2350
0
        case SSH_KEYTYPE_ECDSA_P521_CERT01:
2351
0
        case SSH_KEYTYPE_ED25519_CERT01:
2352
0
        case SSH_KEYTYPE_SK_ECDSA:
2353
0
        case SSH_KEYTYPE_SK_ECDSA_CERT01:
2354
0
        case SSH_KEYTYPE_SK_ED25519:
2355
0
        case SSH_KEYTYPE_SK_ED25519_CERT01:
2356
0
        case SSH_KEYTYPE_RSA1:
2357
0
        case SSH_KEYTYPE_UNKNOWN:
2358
0
        default:
2359
0
            goto error;
2360
0
    }
2361
2362
0
    *pkey = key;
2363
0
    return SSH_OK;
2364
0
error:
2365
0
    ssh_key_free(key);
2366
0
    return SSH_ERROR;
2367
0
}
2368
2369
/**
2370
 * @brief Generates a key pair.
2371
 *
2372
 * @param[in] type      Type of key to create
2373
 *
2374
 * @param[in] parameter Parameter to the creation of key:
2375
 *                      rsa : length of the key in bits (e.g. 1024, 2048, 4096)
2376
 *                      If parameter is 0, then the default size will be used.
2377
 * @param[out] pkey     A pointer to store the allocated private key. You need
2378
 *                      to free the memory using ssh_key_free().
2379
 *
2380
 * @return              SSH_OK on success, SSH_ERROR on error.
2381
 *
2382
 * @warning             Generating a key pair may take some time.
2383
 *
2384
 * @see ssh_key_free()
2385
 */
2386
int ssh_pki_generate(enum ssh_keytypes_e type, int parameter, ssh_key *pkey)
2387
0
{
2388
0
    return pki_generate_key_internal(type, parameter, pkey);
2389
0
}
2390
2391
/**
2392
 * @brief Generates a key pair.
2393
 *
2394
 * @param[in] type        Type of key to create
2395
 *
2396
 * @param[in] pki_context PKI context containing various configuration
2397
 *                        parameters and sub-contexts. Can be NULL for
2398
 *                        standard SSH key types (RSA, ECDSA, ED25519) where
2399
 *                        defaults will be used. Can also be NULL for security
2400
 *                        key types (SK_*), in which case default callbacks and
2401
 *                        settings will be used automatically.
2402
 *
2403
 * @param[out] pkey       A pointer to store the allocated private key. You need
2404
 *                        to free the memory using ssh_key_free().
2405
 *
2406
 * @return              SSH_OK on success, SSH_ERROR on error.
2407
 *
2408
 * @see ssh_pki_ctx_new()
2409
 * @see ssh_key_free()
2410
 */
2411
int ssh_pki_generate_key(enum ssh_keytypes_e type,
2412
                         ssh_pki_ctx pki_context,
2413
                         ssh_key *pkey)
2414
0
{
2415
2416
    /* Handle Security Key types with the specialized function */
2417
0
    if (is_sk_key_type(type)) {
2418
#ifdef WITH_FIDO2
2419
        ssh_pki_ctx temp_ctx = NULL;
2420
        ssh_pki_ctx ctx_to_use = pki_context;
2421
        int rc;
2422
2423
        /* If no context provided, create a temporary default one */
2424
        if (pki_context == NULL) {
2425
            SSH_LOG(SSH_LOG_INFO,
2426
                    "No PKI context provided, using the default one");
2427
2428
            temp_ctx = ssh_pki_ctx_new();
2429
            if (temp_ctx == NULL) {
2430
                SSH_LOG(SSH_LOG_WARN, "Failed to create temporary PKI context");
2431
                return SSH_ERROR;
2432
            }
2433
            ctx_to_use = temp_ctx;
2434
        }
2435
2436
        /* Verify that we have valid SK callbacks */
2437
        if (ctx_to_use->sk_callbacks == NULL) {
2438
            SSH_LOG(SSH_LOG_WARN, "Missing SK callbacks in PKI context");
2439
            if (temp_ctx != NULL) {
2440
                SSH_PKI_CTX_FREE(temp_ctx);
2441
            }
2442
            return SSH_ERROR;
2443
        }
2444
2445
        rc = pki_sk_enroll_key(ctx_to_use, type, pkey);
2446
2447
        /* Clean up temporary context if we created one */
2448
        if (temp_ctx != NULL) {
2449
            SSH_PKI_CTX_FREE(temp_ctx);
2450
        }
2451
2452
        return rc;
2453
#else  /* WITH_FIDO2 */
2454
0
        SSH_LOG(SSH_LOG_WARN, SK_NOT_SUPPORTED_MSG);
2455
0
        return SSH_ERROR;
2456
0
#endif /* WITH_FIDO2 */
2457
0
    } else {
2458
0
        int parameter = 0;
2459
2460
0
        if (type == SSH_KEYTYPE_RSA && pki_context != NULL) {
2461
0
            parameter = pki_context->rsa_key_size;
2462
0
        }
2463
2464
0
        return pki_generate_key_internal(type, parameter, pkey);
2465
0
    }
2466
0
}
2467
2468
/**
2469
 * @brief Create a public key from a private key.
2470
 *
2471
 * @param[in]  privkey  The private key to get the public key from.
2472
 *
2473
 * @param[out] pkey     A pointer to store the newly allocated public key. You
2474
 *                      NEED to free the key using ssh_key_free().
2475
 *
2476
 * @return              SSH_OK on success, SSH_ERROR on error.
2477
 *
2478
 * @see ssh_key_free()
2479
 */
2480
int ssh_pki_export_privkey_to_pubkey(const ssh_key privkey,
2481
                                     ssh_key *pkey)
2482
0
{
2483
0
    ssh_key pubkey = NULL;
2484
2485
0
    if (privkey == NULL || !ssh_key_is_private(privkey)) {
2486
0
        return SSH_ERROR;
2487
0
    }
2488
2489
0
    pubkey = pki_key_dup(privkey, 1);
2490
0
    if (pubkey == NULL) {
2491
0
        return SSH_ERROR;
2492
0
    }
2493
2494
0
    *pkey = pubkey;
2495
0
    return SSH_OK;
2496
0
}
2497
2498
/**
2499
 * @internal
2500
 *
2501
 * @brief Pack security key private data into a buffer.
2502
 *
2503
 * This function packs the common security key fields (application, flags,
2504
 * key handle, and reserved data) into a buffer.
2505
 * This is used for both ECDSA and Ed25519 security keys when exporting
2506
 * private key data.
2507
 *
2508
 * @param[in] buffer    The buffer to pack the security key data into.
2509
 *
2510
 * @param[in] key       The security key containing the data to pack.
2511
 *                      Must be a security key type (SK_ECDSA or SK_ED25519).
2512
 *
2513
 * @return              SSH_OK on success, SSH_ERROR on error.
2514
 *
2515
 * @see ssh_buffer_pack()
2516
 */
2517
int pki_buffer_pack_sk_priv_data(ssh_buffer buffer, ssh_key key)
2518
0
{
2519
0
    return ssh_buffer_pack(buffer,
2520
0
                           "SbSS",
2521
0
                           key->sk_application,
2522
0
                           key->sk_flags,
2523
0
                           key->sk_key_handle,
2524
0
                           key->sk_reserved);
2525
0
}
2526
2527
/**
2528
 * @internal
2529
 *
2530
 * @brief Unpack security key private data from a buffer.
2531
 *
2532
 * This function unpacks the common security key fields (application, flags,
2533
 * key handle, and reserved data) from a buffer.
2534
 * This is used for both ECDSA and Ed25519 security keys when importing
2535
 * private key data.
2536
 *
2537
 * @param[in] buffer    The buffer to unpack the security key data from.
2538
 *
2539
 * @param[in] key       The security key to store the unpacked data into.
2540
 *                      Must be a security key type (SK_ECDSA or SK_ED25519).
2541
 *
2542
 * @return              SSH_OK on success, SSH_ERROR on error.
2543
 *
2544
 * @see ssh_buffer_unpack()
2545
 */
2546
int pki_buffer_unpack_sk_priv_data(ssh_buffer buffer, ssh_key key)
2547
0
{
2548
0
    return ssh_buffer_unpack(buffer,
2549
0
                             "SbSS",
2550
0
                             &key->sk_application,
2551
0
                             &key->sk_flags,
2552
0
                             &key->sk_key_handle,
2553
0
                             &key->sk_reserved);
2554
0
}
2555
2556
/**
2557
 * @internal
2558
 *
2559
 * @brief Create a key_blob from a public key.
2560
 *
2561
 * The "key_blob" is encoded as per RFC 4253 section 6.6 "Public Key
2562
 * Algorithms" for any of the supported protocol 2 key types.
2563
 * Encoding of EC keys is described in RFC 5656 section 3.1 "Key
2564
 * Format".
2565
 *
2566
 * @param[in]  key      A public or private key to create the public ssh_string
2567
 *                      from.
2568
 *
2569
 * @param[out] pblob    A pointer to store the newly allocated key blob. You
2570
 *                      need to free it using ssh_string_free().
2571
 *
2572
 * @return              SSH_OK on success, SSH_ERROR otherwise.
2573
 *
2574
 * @see ssh_string_free()
2575
 */
2576
int ssh_pki_export_pubkey_blob(const ssh_key key,
2577
                               ssh_string *pblob)
2578
0
{
2579
0
    ssh_string blob = NULL;
2580
2581
0
    if (key == NULL) {
2582
0
        return SSH_OK;
2583
0
    }
2584
2585
0
    blob = pki_key_to_blob(key, SSH_KEY_PUBLIC);
2586
0
    if (blob == NULL) {
2587
0
        return SSH_ERROR;
2588
0
    }
2589
2590
0
    *pblob = blob;
2591
0
    return SSH_OK;
2592
0
}
2593
2594
/**
2595
 * @internal
2596
 *
2597
 * @brief Create a key_blob from a private key.
2598
 *
2599
 * The "key_blob" is encoded as per draft-miller-ssh-agent-08 section 4.2
2600
 * "Adding keys to the agent" for any of the supported key types.
2601
 *
2602
 * @param[in]  key      A private key to create the private ssh_string from.
2603
 *
2604
 * @param[out] pblob    A pointer to store the newly allocated key blob. You
2605
 *                      need to free it using ssh_string_free().
2606
 *
2607
 * @return              SSH_OK on success, SSH_ERROR otherwise.
2608
 *
2609
 * @see ssh_string_free()
2610
 */
2611
int ssh_pki_export_privkey_blob(const ssh_key key,
2612
                                ssh_string *pblob)
2613
0
{
2614
0
    ssh_string blob = NULL;
2615
2616
0
    if (key == NULL) {
2617
0
        return SSH_OK;
2618
0
    }
2619
2620
0
    blob = pki_key_to_blob(key, SSH_KEY_PRIVATE);
2621
0
    if (blob == NULL) {
2622
0
        return SSH_ERROR;
2623
0
    }
2624
2625
0
    *pblob = blob;
2626
0
    return SSH_OK;
2627
0
}
2628
2629
/**
2630
 * @brief Convert a public key to a base64 encoded key.
2631
 *
2632
 * @param[in] key       The key to hash
2633
 *
2634
 * @param[out] b64_key  A pointer to store the allocated base64 encoded key. You
2635
 *                      need to free the buffer using ssh_string_free_char()
2636
 *
2637
 * @return              SSH_OK on success, SSH_ERROR on error.
2638
 *
2639
 * @see ssh_string_free_char()
2640
 */
2641
int ssh_pki_export_pubkey_base64(const ssh_key key,
2642
                                 char **b64_key)
2643
0
{
2644
0
    ssh_string key_blob = NULL;
2645
0
    unsigned char *b64 = NULL;
2646
2647
0
    if (key == NULL || b64_key == NULL) {
2648
0
        return SSH_ERROR;
2649
0
    }
2650
2651
0
    key_blob = pki_key_to_blob(key, SSH_KEY_PUBLIC);
2652
0
    if (key_blob == NULL) {
2653
0
        return SSH_ERROR;
2654
0
    }
2655
2656
0
    b64 = bin_to_base64(ssh_string_data(key_blob), ssh_string_len(key_blob));
2657
0
    SSH_STRING_FREE(key_blob);
2658
0
    if (b64 == NULL) {
2659
0
        return SSH_ERROR;
2660
0
    }
2661
2662
0
    *b64_key = (char *)b64;
2663
2664
0
    return SSH_OK;
2665
0
}
2666
2667
/**
2668
 * @brief Export public key to file
2669
 *
2670
 * Exports the public key in AuthorizedKeysFile acceptable format.
2671
 * For more information see `man sshd`
2672
 *
2673
 * @param key A key to export
2674
 *
2675
 * @param filename The name of the output file
2676
 *
2677
 * @returns SSH_OK on success, SSH_ERROR otherwise.
2678
 */
2679
int ssh_pki_export_pubkey_file(const ssh_key key,
2680
                               const char *filename)
2681
0
{
2682
0
    char key_buf[MAX_LINE_SIZE];
2683
0
    char host[256];
2684
0
    char *b64_key = NULL;
2685
0
    char *user = NULL;
2686
0
    FILE *fp = NULL;
2687
0
    int rc;
2688
2689
0
    if (key == NULL || filename == NULL || *filename == '\0') {
2690
0
        return SSH_ERROR;
2691
0
    }
2692
2693
0
    user = ssh_get_local_username();
2694
0
    if (user == NULL) {
2695
0
        return SSH_ERROR;
2696
0
    }
2697
2698
0
    rc = gethostname(host, sizeof(host));
2699
0
    if (rc < 0) {
2700
0
        free(user);
2701
0
        return SSH_ERROR;
2702
0
    }
2703
2704
0
    rc = ssh_pki_export_pubkey_base64(key, &b64_key);
2705
0
    if (rc < 0) {
2706
0
        free(user);
2707
0
        return SSH_ERROR;
2708
0
    }
2709
2710
0
    rc = snprintf(key_buf, sizeof(key_buf),
2711
0
                  "%s %s %s@%s\n",
2712
0
                  key->type_c,
2713
0
                  b64_key,
2714
0
                  user,
2715
0
                  host);
2716
0
    free(user);
2717
0
    free(b64_key);
2718
0
    if (rc < 0) {
2719
0
        return SSH_ERROR;
2720
0
    }
2721
2722
0
    fp = fopen(filename, "wb+");
2723
0
    if (fp == NULL) {
2724
0
        return SSH_ERROR;
2725
0
    }
2726
0
    rc = fwrite(key_buf, strlen(key_buf), 1, fp);
2727
0
    if (rc != 1 || ferror(fp)) {
2728
0
        fclose(fp);
2729
0
        unlink(filename);
2730
0
        return SSH_ERROR;
2731
0
    }
2732
0
    fclose(fp);
2733
2734
0
    return SSH_OK;
2735
0
}
2736
2737
/**
2738
 * @brief Copy the certificate part of a public key into a private key.
2739
 *
2740
 * @param[in]  certkey  The certificate key.
2741
 *
2742
 * @param[in]  privkey  The target private key to copy the certificate to.
2743
 *
2744
 * @returns SSH_OK on success, SSH_ERROR otherwise.
2745
 **/
2746
int ssh_pki_copy_cert_to_privkey(const ssh_key certkey, ssh_key privkey)
2747
0
{
2748
0
    ssh_buffer cert_buffer = NULL;
2749
0
    int rc, cmp;
2750
2751
0
    if (certkey == NULL || privkey == NULL) {
2752
0
        return SSH_ERROR;
2753
0
    }
2754
2755
0
    if (privkey->cert != NULL) {
2756
0
        return SSH_ERROR;
2757
0
    }
2758
2759
0
    if (certkey->cert == NULL) {
2760
0
        return SSH_ERROR;
2761
0
    }
2762
2763
    /* make sure the public keys match */
2764
0
    cmp = ssh_key_cmp(certkey, privkey, SSH_KEY_CMP_PUBLIC);
2765
0
    if (cmp != 0) {
2766
0
        return SSH_ERROR;
2767
0
    }
2768
2769
0
    cert_buffer = ssh_buffer_new();
2770
0
    if (cert_buffer == NULL) {
2771
0
        return SSH_ERROR;
2772
0
    }
2773
2774
0
    rc = ssh_buffer_add_buffer(cert_buffer, certkey->cert);
2775
0
    if (rc != 0) {
2776
0
        SSH_BUFFER_FREE(cert_buffer);
2777
0
        return SSH_ERROR;
2778
0
    }
2779
2780
0
    privkey->cert = cert_buffer;
2781
0
    privkey->cert_type = certkey->type;
2782
0
    return SSH_OK;
2783
0
}
2784
2785
int ssh_pki_export_signature_blob(const ssh_signature sig,
2786
                                  ssh_string *sig_blob)
2787
0
{
2788
0
    ssh_buffer buf = NULL;
2789
0
    ssh_string str = NULL;
2790
0
    int rc;
2791
2792
0
    if (sig == NULL || sig_blob == NULL) {
2793
0
        return SSH_ERROR;
2794
0
    }
2795
2796
0
    buf = ssh_buffer_new();
2797
0
    if (buf == NULL) {
2798
0
        return SSH_ERROR;
2799
0
    }
2800
2801
0
    str = ssh_string_from_char(sig->type_c);
2802
0
    if (str == NULL) {
2803
0
        SSH_BUFFER_FREE(buf);
2804
0
        return SSH_ERROR;
2805
0
    }
2806
2807
0
    rc = ssh_buffer_add_ssh_string(buf, str);
2808
0
    SSH_STRING_FREE(str);
2809
0
    if (rc < 0) {
2810
0
        SSH_BUFFER_FREE(buf);
2811
0
        return SSH_ERROR;
2812
0
    }
2813
2814
0
    str = pki_signature_to_blob(sig);
2815
0
    if (str == NULL) {
2816
0
        SSH_BUFFER_FREE(buf);
2817
0
        return SSH_ERROR;
2818
0
    }
2819
2820
0
    rc = ssh_buffer_add_ssh_string(buf, str);
2821
0
    SSH_STRING_FREE(str);
2822
0
    if (rc < 0) {
2823
0
        SSH_BUFFER_FREE(buf);
2824
0
        return SSH_ERROR;
2825
0
    }
2826
2827
0
    if (is_sk_key_type(sig->type)) {
2828
        /* Add flags and counter for SK keys */
2829
0
        rc = ssh_buffer_pack(buf, "bd", sig->sk_flags, sig->sk_counter);
2830
0
        if (rc < 0) {
2831
0
            SSH_BUFFER_FREE(buf);
2832
0
            return SSH_ERROR;
2833
0
        }
2834
0
    }
2835
2836
0
    str = ssh_string_new(ssh_buffer_get_len(buf));
2837
0
    if (str == NULL) {
2838
0
        SSH_BUFFER_FREE(buf);
2839
0
        return SSH_ERROR;
2840
0
    }
2841
2842
0
    rc = ssh_string_fill(str, ssh_buffer_get(buf), ssh_buffer_get_len(buf));
2843
0
    SSH_BUFFER_FREE(buf);
2844
0
    if (rc < 0) {
2845
0
        SSH_STRING_FREE(str);
2846
0
        return SSH_ERROR;
2847
0
    }
2848
2849
0
    *sig_blob = str;
2850
2851
0
    return SSH_OK;
2852
0
}
2853
2854
int ssh_pki_import_signature_blob(const ssh_string sig_blob,
2855
                                  const ssh_key pubkey,
2856
                                  ssh_signature *psig)
2857
0
{
2858
0
    ssh_signature sig = NULL;
2859
0
    enum ssh_keytypes_e type;
2860
0
    enum ssh_digest_e hash_type;
2861
0
    ssh_string algorithm = NULL, blob = NULL;
2862
0
    ssh_buffer buf = NULL;
2863
0
    const char *alg = NULL;
2864
0
    uint8_t flags = 0;
2865
0
    uint32_t counter = 0;
2866
0
    int rc;
2867
2868
0
    if (sig_blob == NULL || psig == NULL) {
2869
0
        return SSH_ERROR;
2870
0
    }
2871
2872
0
    buf = ssh_buffer_new();
2873
0
    if (buf == NULL) {
2874
0
        return SSH_ERROR;
2875
0
    }
2876
2877
0
    rc = ssh_buffer_add_data(buf,
2878
0
                             ssh_string_data(sig_blob),
2879
0
                             (uint32_t)ssh_string_len(sig_blob));
2880
0
    if (rc < 0) {
2881
0
        SSH_BUFFER_FREE(buf);
2882
0
        return SSH_ERROR;
2883
0
    }
2884
2885
0
    algorithm = ssh_buffer_get_ssh_string(buf);
2886
0
    if (algorithm == NULL) {
2887
0
        SSH_BUFFER_FREE(buf);
2888
0
        return SSH_ERROR;
2889
0
    }
2890
2891
0
    alg = ssh_string_get_char(algorithm);
2892
0
    type = ssh_key_type_from_signature_name(alg);
2893
0
    hash_type = ssh_key_hash_from_name(alg);
2894
0
    SSH_STRING_FREE(algorithm);
2895
2896
0
    blob = ssh_buffer_get_ssh_string(buf);
2897
0
    if (blob == NULL) {
2898
0
        SSH_BUFFER_FREE(buf);
2899
0
        return SSH_ERROR;
2900
0
    }
2901
2902
0
    if (type == SSH_KEYTYPE_SK_ECDSA ||
2903
0
        type == SSH_KEYTYPE_SK_ED25519) {
2904
0
        rc = ssh_buffer_unpack(buf, "bd", &flags, &counter);
2905
0
        if (rc < 0) {
2906
0
            SSH_BUFFER_FREE(buf);
2907
0
            SSH_STRING_FREE(blob);
2908
0
            return SSH_ERROR;
2909
0
        }
2910
0
    }
2911
0
    SSH_BUFFER_FREE(buf);
2912
2913
0
    sig = pki_signature_from_blob(pubkey, blob, type, hash_type);
2914
0
    SSH_STRING_FREE(blob);
2915
0
    if (sig == NULL) {
2916
0
        return SSH_ERROR;
2917
0
    }
2918
2919
    /* Set SK specific values */
2920
0
    sig->sk_flags = flags;
2921
0
    sig->sk_counter = counter;
2922
2923
0
    *psig = sig;
2924
0
    return SSH_OK;
2925
0
}
2926
2927
/**
2928
 * @internal
2929
 *
2930
 * @brief Check if the provided key can be used with the provided hash type for
2931
 * data signing or signature verification.
2932
 *
2933
 * @param[in]   key         The key to be checked.
2934
 * @param[in]   hash_type   The digest algorithm to be checked.
2935
 *
2936
 * @return  SSH_OK if compatible; SSH_ERROR otherwise
2937
 */
2938
int pki_key_check_hash_compatible(ssh_key key,
2939
                                  enum ssh_digest_e hash_type)
2940
0
{
2941
0
    if (key == NULL) {
2942
0
        SSH_LOG(SSH_LOG_TRACE, "Null pointer provided as key to "
2943
0
                               "pki_key_check_hash_compatible()");
2944
0
        return SSH_ERROR;
2945
0
    }
2946
2947
0
    switch(key->type) {
2948
0
    case SSH_KEYTYPE_RSA_CERT01:
2949
0
    case SSH_KEYTYPE_RSA:
2950
0
        if (hash_type == SSH_DIGEST_SHA1) {
2951
0
            if (ssh_fips_mode()) {
2952
0
                SSH_LOG(SSH_LOG_TRACE, "SHA1 is not allowed in FIPS mode");
2953
0
                return SSH_ERROR;
2954
0
            } else {
2955
0
                return SSH_OK;
2956
0
            }
2957
0
        }
2958
2959
0
        if (hash_type == SSH_DIGEST_SHA256 ||
2960
0
            hash_type == SSH_DIGEST_SHA512)
2961
0
        {
2962
0
            return SSH_OK;
2963
0
        }
2964
0
        break;
2965
0
    case SSH_KEYTYPE_ECDSA_P256_CERT01:
2966
0
    case SSH_KEYTYPE_ECDSA_P256:
2967
0
    case SSH_KEYTYPE_SK_ECDSA_CERT01:
2968
0
    case SSH_KEYTYPE_SK_ECDSA:
2969
0
        if (hash_type == SSH_DIGEST_SHA256) {
2970
0
            return SSH_OK;
2971
0
        }
2972
0
        break;
2973
0
    case SSH_KEYTYPE_ECDSA_P384_CERT01:
2974
0
    case SSH_KEYTYPE_ECDSA_P384:
2975
0
        if (hash_type == SSH_DIGEST_SHA384) {
2976
0
            return SSH_OK;
2977
0
        }
2978
0
        break;
2979
0
    case SSH_KEYTYPE_ECDSA_P521_CERT01:
2980
0
    case SSH_KEYTYPE_ECDSA_P521:
2981
0
        if (hash_type == SSH_DIGEST_SHA512) {
2982
0
            return SSH_OK;
2983
0
        }
2984
0
        break;
2985
0
    case SSH_KEYTYPE_ED25519_CERT01:
2986
0
    case SSH_KEYTYPE_ED25519:
2987
0
    case SSH_KEYTYPE_SK_ED25519_CERT01:
2988
0
    case SSH_KEYTYPE_SK_ED25519:
2989
0
        if (hash_type == SSH_DIGEST_AUTO) {
2990
0
            return SSH_OK;
2991
0
        }
2992
0
        break;
2993
0
    case SSH_KEYTYPE_DSS:   /* deprecated */
2994
0
    case SSH_KEYTYPE_DSS_CERT01:    /* deprecated */
2995
0
    case SSH_KEYTYPE_RSA1:
2996
0
    case SSH_KEYTYPE_ECDSA:
2997
0
    case SSH_KEYTYPE_UNKNOWN:
2998
0
        SSH_LOG(SSH_LOG_TRACE, "Unknown key type %d", key->type);
2999
0
        return SSH_ERROR;
3000
0
    }
3001
3002
0
    SSH_LOG(SSH_LOG_TRACE, "Key type %d incompatible with hash type  %d",
3003
0
            key->type, hash_type);
3004
3005
0
    return SSH_ERROR;
3006
0
}
3007
3008
/**
3009
 * @brief Prepare buffer for FIDO2/U2F security key signature verification
3010
 *
3011
 * This function creates a buffer containing the application hash, flags,
3012
 * counter, and input hash for FIDO/U2F key signature verification.
3013
 *
3014
 * @param key          The SSH key containing sk_application
3015
 * @param sig          The signature containing sk_flags and sk_counter
3016
 * @param input        The input data to hash
3017
 * @param input_len    Length of the input data
3018
 * @param sk_buffer_out Pointer to store the created buffer
3019
 *
3020
 * @return SSH_OK on success, SSH_ERROR on error
3021
 */
3022
int pki_sk_signature_buffer_prepare(const ssh_key key,
3023
                                    const ssh_signature sig,
3024
                                    const unsigned char *input,
3025
                                    size_t input_len,
3026
                                    ssh_buffer *sk_buffer_out)
3027
0
{
3028
0
    ssh_buffer sk_buffer = NULL;
3029
0
    SHA256CTX ctx = NULL;
3030
0
    unsigned char application_hash[SHA256_DIGEST_LEN] = {0};
3031
0
    unsigned char input_hash[SHA256_DIGEST_LEN] = {0};
3032
0
    int rc, ret = SSH_ERROR;
3033
3034
0
    if (key == NULL || sig == NULL || input == NULL || sk_buffer_out == NULL) {
3035
0
        SSH_LOG(SSH_LOG_TRACE, "Bad parameter(s) provided to %s()", __func__);
3036
0
        return SSH_ERROR;
3037
0
    }
3038
3039
0
    *sk_buffer_out = NULL;
3040
3041
    /* Calculate application hash */
3042
0
    ctx = sha256_init();
3043
0
    if (ctx == NULL) {
3044
0
        SSH_LOG(SSH_LOG_TRACE, "Can not create SHA256CTX for application hash");
3045
0
        return SSH_ERROR;
3046
0
    }
3047
0
    sha256_update(ctx,
3048
0
                  ssh_string_data(key->sk_application),
3049
0
                  ssh_string_len(key->sk_application));
3050
0
    sha256_final(application_hash, ctx);
3051
3052
    /* Calculate input hash */
3053
0
    ctx = sha256_init();
3054
0
    if (ctx == NULL) {
3055
0
        SSH_LOG(SSH_LOG_TRACE, "Can not create SHA256CTX for input hash");
3056
0
        goto out;
3057
0
    }
3058
0
    sha256_update(ctx, input, input_len);
3059
0
    sha256_final(input_hash, ctx);
3060
3061
    /* Create and pack the sk_buffer */
3062
0
    sk_buffer = ssh_buffer_new();
3063
0
    if (sk_buffer == NULL) {
3064
0
        goto out;
3065
0
    }
3066
3067
0
    rc = ssh_buffer_pack(sk_buffer,
3068
0
                         "PbdP",
3069
0
                         (size_t)SHA256_DIGEST_LEN,
3070
0
                         application_hash,
3071
0
                         sig->sk_flags,
3072
0
                         sig->sk_counter,
3073
0
                         (size_t)SHA256_DIGEST_LEN,
3074
0
                         input_hash);
3075
0
    if (rc != SSH_OK) {
3076
0
        goto out;
3077
0
    }
3078
3079
0
    *sk_buffer_out = sk_buffer;
3080
0
    sk_buffer = NULL;
3081
0
    ret = SSH_OK;
3082
3083
0
out:
3084
0
    SSH_BUFFER_FREE(sk_buffer);
3085
0
    ssh_burn(application_hash, SHA256_DIGEST_LEN);
3086
0
    ssh_burn(input_hash, SHA256_DIGEST_LEN);
3087
3088
0
    return ret;
3089
0
}
3090
3091
int ssh_pki_signature_verify(ssh_session session,
3092
                             ssh_signature sig,
3093
                             const ssh_key key,
3094
                             const unsigned char *input,
3095
                             size_t input_len)
3096
0
{
3097
0
    int rc;
3098
0
    bool allowed;
3099
0
    enum ssh_keytypes_e key_type;
3100
3101
0
    if (session == NULL || sig == NULL || key == NULL || input == NULL) {
3102
0
        SSH_LOG(SSH_LOG_TRACE, "Bad parameter(s) provided to %s()", __func__);
3103
0
        return SSH_ERROR;
3104
0
    }
3105
0
    key_type = ssh_key_type_plain(key->type);
3106
3107
0
    SSH_LOG(SSH_LOG_FUNCTIONS,
3108
0
            "Going to verify a %s type signature",
3109
0
            sig->type_c);
3110
3111
0
    if (key_type != sig->type) {
3112
0
        SSH_LOG(SSH_LOG_TRACE,
3113
0
                "Can not verify %s signature with %s key",
3114
0
                sig->type_c, key->type_c);
3115
0
        return SSH_ERROR;
3116
0
    }
3117
3118
0
    allowed = ssh_key_size_allowed(session, key);
3119
0
    if (!allowed) {
3120
0
        ssh_set_error(session,
3121
0
                      SSH_FATAL,
3122
0
                      "The '%s' key of size %d is not allowed by RSA_MIN_SIZE",
3123
0
                      key->type_c,
3124
0
                      ssh_key_size(key));
3125
0
        return SSH_ERROR;
3126
0
    }
3127
3128
    /* Check if public key and hash type are compatible */
3129
0
    rc = pki_key_check_hash_compatible(key, sig->hash_type);
3130
0
    if (rc != SSH_OK) {
3131
0
        return SSH_ERROR;
3132
0
    }
3133
3134
0
    if (is_sk_key_type(key->type)) {
3135
0
        ssh_buffer sk_buffer = NULL;
3136
3137
0
        rc = pki_sk_signature_buffer_prepare(key,
3138
0
                                             sig,
3139
0
                                             input,
3140
0
                                             input_len,
3141
0
                                             &sk_buffer);
3142
0
        if (rc != SSH_OK) {
3143
0
            return SSH_ERROR;
3144
0
        }
3145
3146
0
        rc = pki_verify_data_signature(sig,
3147
0
                                       key,
3148
0
                                       ssh_buffer_get(sk_buffer),
3149
0
                                       ssh_buffer_get_len(sk_buffer));
3150
0
        SSH_BUFFER_FREE(sk_buffer);
3151
0
        return rc;
3152
0
    }
3153
3154
0
    return pki_verify_data_signature(sig, key, input, input_len);
3155
0
}
3156
3157
ssh_signature pki_do_sign(const ssh_key privkey,
3158
                          const unsigned char *input,
3159
                          size_t input_len,
3160
                          enum ssh_digest_e hash_type)
3161
0
{
3162
0
    int rc;
3163
3164
0
    if (privkey == NULL || input == NULL) {
3165
0
        SSH_LOG(SSH_LOG_TRACE, "Bad parameter provided to "
3166
0
                               "pki_do_sign()");
3167
0
        return NULL;
3168
0
    }
3169
3170
    /* Check if public key and hash type are compatible */
3171
0
    rc = pki_key_check_hash_compatible(privkey, hash_type);
3172
0
    if (rc != SSH_OK) {
3173
0
        return NULL;
3174
0
    }
3175
3176
0
    return pki_sign_data(privkey, hash_type, input, input_len);
3177
0
}
3178
3179
/**
3180
 * @brief Encodes a binary signature blob as an sshsig armored signature
3181
 *
3182
 * @param blob       The binary signature blob to encode
3183
 * @param out_str    Pointer to store the allocated base64 encoded string
3184
 *                   Must be freed with ssh_string_free_char()
3185
 *
3186
 * @return SSH_OK on success, SSH_ERROR on error
3187
 */
3188
static int sshsig_armor(ssh_buffer blob, char **out_str)
3189
0
{
3190
0
    char *b64_data = NULL;
3191
0
    char *armored = NULL;
3192
0
    const unsigned char *data = NULL;
3193
0
    size_t len, b64_len, armored_len, num_lines;
3194
0
    size_t i, j;
3195
3196
0
    if (blob == NULL || out_str == NULL) {
3197
0
        SSH_LOG(SSH_LOG_TRACE, "Invalid input parameters");
3198
0
        return SSH_ERROR;
3199
0
    }
3200
3201
0
    *out_str = NULL;
3202
3203
0
    data = ssh_buffer_get(blob);
3204
0
    len = ssh_buffer_get_len(blob);
3205
3206
0
    b64_data = (char *)bin_to_base64(data, len);
3207
0
    if (b64_data == NULL) {
3208
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to base64 encode signature blob");
3209
0
        return SSH_ERROR;
3210
0
    }
3211
3212
0
    b64_len = strlen(b64_data);
3213
3214
    /* Calculate space needed: header + data with line breaks + footer */
3215
0
    num_lines = (b64_len + SSHSIG_LINE_LENGTH - 1) /
3216
0
                SSHSIG_LINE_LENGTH;                    /* Round up division */
3217
0
    armored_len = strlen(SSHSIG_BEGIN_SIGNATURE) + 1 + /* header + \n */
3218
0
                  b64_len + num_lines +                /* data + line breaks */
3219
0
                  strlen(SSHSIG_END_SIGNATURE) + 1;    /* footer + \0 */
3220
3221
0
    armored = calloc(armored_len, 1);
3222
0
    if (armored == NULL) {
3223
0
        SSH_LOG(SSH_LOG_TRACE,
3224
0
                "Failed to allocate %zu bytes for armored signature",
3225
0
                armored_len);
3226
0
        SAFE_FREE(b64_data);
3227
0
        return SSH_ERROR;
3228
0
    }
3229
3230
0
    j = snprintf(armored, armored_len, SSHSIG_BEGIN_SIGNATURE "\n");
3231
0
    for (i = 0; i < b64_len; i++) {
3232
0
        if (i > 0 && i % SSHSIG_LINE_LENGTH == 0) {
3233
0
            armored[j++] = '\n';
3234
0
        }
3235
0
        armored[j++] = b64_data[i];
3236
0
    }
3237
0
    armored[j++] = '\n';
3238
0
    snprintf(armored + j, armored_len - j, SSHSIG_END_SIGNATURE);
3239
3240
0
    SAFE_FREE(b64_data);
3241
3242
0
    *out_str = armored;
3243
0
    return SSH_OK;
3244
0
}
3245
3246
/**
3247
 * @brief Dearmor an sshsig signature from ASCII armored format to binary
3248
 *
3249
 * @param[in]  signature    The armored sshsig signature string
3250
 * @param[out] out          Pointer to store the allocated binary buffer
3251
 *
3252
 * @return SSH_OK on success, SSH_ERROR on error
3253
 */
3254
static int sshsig_dearmor(const char *signature, ssh_buffer *out)
3255
0
{
3256
0
    const char *begin = NULL;
3257
0
    const char *end = NULL;
3258
0
    char *clean_b64 = NULL;
3259
0
    ssh_buffer decoded_buffer = NULL;
3260
0
    int i, j;
3261
0
    int rc = SSH_ERROR;
3262
3263
0
    if (signature == NULL || out == NULL) {
3264
0
        SSH_LOG(SSH_LOG_TRACE, "Invalid input parameters");
3265
0
        return SSH_ERROR;
3266
0
    }
3267
3268
0
    *out = NULL;
3269
3270
0
    rc = strncmp(signature,
3271
0
                 SSHSIG_BEGIN_SIGNATURE,
3272
0
                 strlen(SSHSIG_BEGIN_SIGNATURE));
3273
0
    if (rc != SSH_OK) {
3274
0
        SSH_LOG(SSH_LOG_TRACE, "Signature does not start with expected header");
3275
0
        return SSH_ERROR;
3276
0
    }
3277
3278
0
    begin = signature + strlen(SSHSIG_BEGIN_SIGNATURE);
3279
0
    while (isspace(*begin)) {
3280
0
        begin++;
3281
0
    }
3282
3283
0
    end = strstr(begin, SSHSIG_END_SIGNATURE);
3284
0
    if (end == NULL) {
3285
0
        SSH_LOG(SSH_LOG_TRACE, "Signature end marker not found");
3286
0
        return SSH_ERROR;
3287
0
    }
3288
3289
    /* Backtrack to find the real end of data */
3290
0
    while (end > begin && (isspace(*(end - 1)))) {
3291
0
        end--;
3292
0
    }
3293
3294
0
    clean_b64 = calloc(end - begin + 1, 1);
3295
0
    if (clean_b64 == NULL) {
3296
0
        SSH_LOG(SSH_LOG_TRACE,
3297
0
                "Failed to allocate %td bytes for clean base64 data",
3298
0
                end - begin + 1);
3299
0
        return SSH_ERROR;
3300
0
    }
3301
3302
0
    for (i = 0, j = 0; begin + i < end; i++) {
3303
0
        if (!isspace(begin[i])) {
3304
0
            clean_b64[j++] = begin[i];
3305
0
        }
3306
0
    }
3307
0
    clean_b64[j] = '\0';
3308
3309
0
    decoded_buffer = base64_to_bin(clean_b64);
3310
0
    SAFE_FREE(clean_b64);
3311
3312
0
    if (decoded_buffer == NULL) {
3313
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to decode base64 signature data");
3314
0
        return SSH_ERROR;
3315
0
    }
3316
3317
0
    *out = decoded_buffer;
3318
0
    return SSH_OK;
3319
0
}
3320
3321
/**
3322
 * @internal
3323
 * @brief Common helper function to prepare the data in sshsig format
3324
 *
3325
 * This function handles the common logic to prepare the sshsig format:
3326
 * 1. Hash the input data using the specified algorithm
3327
 * 2. Build the data buffer to sign
3328
 *
3329
 * @param data           The raw data to process
3330
 * @param data_length    The length of the data
3331
 * @param hash_alg       The hash algorithm to use (sha256 or sha512)
3332
 * @param sig_namespace  The signature namespace
3333
 * @param tosign_buf     Pointer to store the allocated to-sign buffer
3334
 *
3335
 * @return SSH_OK on success, SSH_ERROR on error
3336
 */
3337
static int sshsig_prepare_data(const void *data,
3338
                               size_t data_length,
3339
                               const char *hash_alg,
3340
                               const char *sig_namespace,
3341
                               ssh_buffer *tosign_buf)
3342
0
{
3343
0
    ssh_buffer tosign = NULL;
3344
0
    ssh_string hash_string = NULL;
3345
0
    char hash[SHA512_DIGEST_LEN];
3346
0
    size_t hash_len;
3347
0
    int rc = SSH_ERROR;
3348
3349
0
    if (data == NULL || hash_alg == NULL || sig_namespace == NULL ||
3350
0
        tosign_buf == NULL) {
3351
0
        SSH_LOG(SSH_LOG_TRACE, "Invalid input parameters");
3352
0
        return SSH_ERROR;
3353
0
    }
3354
3355
0
    *tosign_buf = NULL;
3356
3357
0
    if (strcmp(hash_alg, "sha256") == 0) {
3358
0
        hash_len = SHA256_DIGEST_LEN;
3359
0
        rc = sha256(data, data_length, (unsigned char *)hash);
3360
0
    } else if (strcmp(hash_alg, "sha512") == 0) {
3361
0
        hash_len = SHA512_DIGEST_LEN;
3362
0
        rc = sha512(data, data_length, (unsigned char *)hash);
3363
0
    } else {
3364
0
        SSH_LOG(SSH_LOG_TRACE, "Unsupported hash algorithm: %s", hash_alg);
3365
0
        goto cleanup;
3366
0
    }
3367
0
    if (rc != SSH_OK) {
3368
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to compute %s hash of data", hash_alg);
3369
0
        goto cleanup;
3370
0
    }
3371
3372
0
    hash_string = ssh_string_new(hash_len);
3373
0
    if (hash_string == NULL) {
3374
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to allocate ssh_string for hash");
3375
0
        goto cleanup;
3376
0
    }
3377
3378
0
    rc = ssh_string_fill(hash_string, hash, hash_len);
3379
0
    if (rc != SSH_OK) {
3380
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to fill ssh_string with hash data");
3381
0
        goto cleanup;
3382
0
    }
3383
3384
0
    tosign = ssh_buffer_new();
3385
0
    if (tosign == NULL) {
3386
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to allocate buffer for signing data");
3387
0
        goto cleanup;
3388
0
    }
3389
3390
0
    rc = ssh_buffer_pack(tosign,
3391
0
                         "tsssS",
3392
0
                         SSHSIG_MAGIC_PREAMBLE,
3393
0
                         sig_namespace,
3394
0
                         "",
3395
0
                         hash_alg,
3396
0
                         hash_string);
3397
3398
0
    if (rc == SSH_OK) {
3399
0
        *tosign_buf = tosign;
3400
0
        tosign = NULL;
3401
0
    } else {
3402
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to pack signing data into buffer");
3403
0
    }
3404
3405
0
cleanup:
3406
0
    SSH_BUFFER_FREE(tosign);
3407
0
    SSH_STRING_FREE(hash_string);
3408
3409
0
    return rc;
3410
0
}
3411
3412
/**
3413
 * @brief Signs data in sshsig compatible format
3414
 *
3415
 * @param data            The data to sign
3416
 * @param data_length     The length of the data
3417
 * @param privkey         The private key to sign with
3418
 * @param pki_context     The PKI context. For non-SK keys, this parameter is
3419
 *                        ignored and can be NULL. For SK keys, can be NULL in
3420
 *                        which case a default context with default callbacks
3421
 *                        will be used. If provided, the context must have
3422
 *                        sk_callbacks set with a valid sign callback
3423
 *                        implementation. See ssh_pki_ctx_set_sk_callbacks().
3424
 * @param sig_namespace   The signature namespace (e.g. "file", "email", etc.)
3425
 * @param hash_alg        The hash algorithm to use (SSHSIG_DIGEST_SHA2_256 or
3426
 *                        SSHSIG_DIGEST_SHA2_512)
3427
 * @param signature       Pointer to store the allocated signature string in the
3428
 *                        armored format. Must be freed with
3429
 *                        ssh_string_free_char()
3430
 *
3431
 * @return SSH_OK on success, SSH_ERROR on error
3432
 */
3433
int sshsig_sign(const void *data,
3434
                size_t data_length,
3435
                ssh_key privkey,
3436
                ssh_pki_ctx pki_context,
3437
                const char *sig_namespace,
3438
                enum sshsig_digest_e hash_alg,
3439
                char **signature)
3440
0
{
3441
0
    ssh_buffer tosign = NULL;
3442
0
    ssh_buffer signature_blob = NULL;
3443
0
    ssh_signature sig = NULL;
3444
0
    ssh_string sig_string = NULL;
3445
0
    ssh_string pub_blob = NULL;
3446
0
    ssh_pki_ctx temp_ctx = NULL;
3447
0
    ssh_pki_ctx ctx_to_use = NULL;
3448
0
    enum ssh_digest_e digest_type;
3449
0
    const char *hash_alg_str = NULL;
3450
0
    int rc = SSH_ERROR;
3451
3452
0
    if (privkey == NULL || data == NULL || sig_namespace == NULL ||
3453
0
        signature == NULL) {
3454
0
        SSH_LOG(SSH_LOG_TRACE, "Invalid parameters provided to sshsig_sign");
3455
0
        return SSH_ERROR;
3456
0
    }
3457
3458
0
    if (strlen(sig_namespace) == 0) {
3459
0
        SSH_LOG(SSH_LOG_TRACE,
3460
0
                "Invalid parameters provided to sshsig_sign: empty namespace "
3461
0
                "string");
3462
0
        return SSH_ERROR;
3463
0
    }
3464
3465
    /* Check if this is an SK key that requires a PKI context */
3466
0
    if (is_sk_key_type(privkey->type)) {
3467
        /* If no context provided, create a temporary default one */
3468
0
        if (pki_context == NULL) {
3469
0
            SSH_LOG(SSH_LOG_INFO,
3470
0
                    "No PKI context provided, using the default one");
3471
3472
0
            temp_ctx = ssh_pki_ctx_new();
3473
0
            if (temp_ctx == NULL) {
3474
0
                SSH_LOG(SSH_LOG_WARN, "Failed to create temporary PKI context");
3475
0
                return SSH_ERROR;
3476
0
            }
3477
0
            ctx_to_use = temp_ctx;
3478
0
        } else {
3479
0
            ctx_to_use = pki_context;
3480
0
        }
3481
3482
        /* Verify that we have valid SK callbacks */
3483
0
        if (ctx_to_use->sk_callbacks == NULL) {
3484
0
            SSH_LOG(SSH_LOG_WARN,
3485
0
                    "Security Key callbacks not configured in PKI context");
3486
0
            goto cleanup;
3487
0
        }
3488
0
    }
3489
3490
0
    *signature = NULL;
3491
3492
0
    if (hash_alg == SSHSIG_DIGEST_SHA2_256) {
3493
0
        hash_alg_str = "sha256";
3494
0
    } else if (hash_alg == SSHSIG_DIGEST_SHA2_512) {
3495
0
        hash_alg_str = "sha512";
3496
0
    } else {
3497
0
        SSH_LOG(SSH_LOG_TRACE, "Invalid hash algorithm %d", hash_alg);
3498
0
        return SSH_ERROR;
3499
0
    }
3500
3501
0
    rc = sshsig_prepare_data(data,
3502
0
                             data_length,
3503
0
                             hash_alg_str,
3504
0
                             sig_namespace,
3505
0
                             &tosign);
3506
0
    if (rc != SSH_OK) {
3507
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to prepare data for sshsig signing");
3508
0
        goto cleanup;
3509
0
    }
3510
3511
    /* Use appropriate signing method based on key type */
3512
0
    if (is_sk_key_type(privkey->type)) {
3513
#ifdef WITH_FIDO2
3514
        sig = pki_sk_do_sign(ctx_to_use,
3515
                             privkey,
3516
                             ssh_buffer_get(tosign),
3517
                             ssh_buffer_get_len(tosign));
3518
#else
3519
0
        SSH_LOG(SSH_LOG_WARN, SK_NOT_SUPPORTED_MSG);
3520
0
        goto cleanup;
3521
0
#endif
3522
0
    } else {
3523
0
        digest_type = key_type_to_hash(ssh_key_type_plain(privkey->type));
3524
0
        sig = pki_sign_data(privkey,
3525
0
                            digest_type,
3526
0
                            ssh_buffer_get(tosign),
3527
0
                            ssh_buffer_get_len(tosign));
3528
0
    }
3529
0
    if (sig == NULL) {
3530
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to sign data with private key");
3531
0
        goto cleanup;
3532
0
    }
3533
3534
0
    rc = ssh_pki_export_pubkey_blob(privkey, &pub_blob);
3535
0
    if (rc != SSH_OK || pub_blob == NULL) {
3536
0
        SSH_LOG(SSH_LOG_TRACE,
3537
0
                "Failed to export public key blob from private key");
3538
0
        goto cleanup;
3539
0
    }
3540
3541
0
    rc = ssh_pki_export_signature_blob(sig, &sig_string);
3542
0
    if (rc != SSH_OK) {
3543
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to export signature blob");
3544
0
        goto cleanup;
3545
0
    }
3546
3547
0
    signature_blob = ssh_buffer_new();
3548
0
    if (signature_blob == NULL) {
3549
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to allocate signature buffer");
3550
0
        goto cleanup;
3551
0
    }
3552
3553
0
    rc = ssh_buffer_pack(signature_blob,
3554
0
                         "tdSsssS",
3555
0
                         SSHSIG_MAGIC_PREAMBLE,
3556
0
                         SSHSIG_VERSION,
3557
0
                         pub_blob,
3558
0
                         sig_namespace,
3559
0
                         "",
3560
0
                         hash_alg_str,
3561
0
                         sig_string);
3562
0
    if (rc != SSH_OK) {
3563
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to pack signature blob");
3564
0
        goto cleanup;
3565
0
    }
3566
3567
0
    rc = sshsig_armor(signature_blob, signature);
3568
0
    if (rc != SSH_OK) {
3569
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to armor signature blob");
3570
0
        goto cleanup;
3571
0
    }
3572
3573
0
cleanup:
3574
0
    SSH_BUFFER_FREE(tosign);
3575
0
    SSH_BUFFER_FREE(signature_blob);
3576
0
    SSH_SIGNATURE_FREE(sig);
3577
0
    SSH_STRING_FREE(sig_string);
3578
0
    SSH_STRING_FREE(pub_blob);
3579
3580
    /* Clean up temporary context if we created one */
3581
0
    if (temp_ctx != NULL) {
3582
0
        SSH_PKI_CTX_FREE(temp_ctx);
3583
0
    }
3584
3585
0
    return rc;
3586
0
}
3587
3588
/**
3589
 * @brief Verifies an sshsig formatted signature against data
3590
 *
3591
 * @param data           The data to verify
3592
 * @param data_length    The length of the data
3593
 * @param signature      The armored sshsig signature
3594
 * @param sig_namespace  The expected signature namespace
3595
 * @param sign_key       If not NULL, returns the allocated public key that was
3596
 *                       used for signing this data. Must be freed with
3597
 *                       ssh_key_free(). Note that this is an output parameter
3598
 *                       and is not checked against "allowed signers". The
3599
 *                       caller needs to compare it with expected signer key
3600
 *                       using ssh_key_cmp().
3601
 *
3602
 * @return SSH_OK on success, SSH_ERROR on verification failure
3603
 */
3604
int sshsig_verify(const void *data,
3605
                  size_t data_length,
3606
                  const char *signature,
3607
                  const char *sig_namespace,
3608
                  ssh_key *sign_key)
3609
0
{
3610
0
    ssh_buffer sig_buf = NULL;
3611
0
    ssh_buffer tosign = NULL;
3612
0
    ssh_key key = NULL;
3613
0
    char *hash_alg_str = NULL;
3614
0
    ssh_string sig_data = NULL;
3615
0
    ssh_string sig_namespace_str = NULL;
3616
0
    ssh_string reserved_str = NULL;
3617
0
    ssh_string pubkey_blob = NULL;
3618
0
    int rc = SSH_ERROR;
3619
0
    ssh_signature signature_obj = NULL;
3620
0
    uint32_t sig_version;
3621
3622
0
    if (sign_key != NULL) {
3623
0
        *sign_key = NULL;
3624
0
    }
3625
3626
0
    if (signature == NULL || data == NULL || sig_namespace == NULL) {
3627
0
        SSH_LOG(SSH_LOG_TRACE, "Invalid parameters provided to sshsig_verify");
3628
0
        return SSH_ERROR;
3629
0
    }
3630
3631
0
    if (strlen(sig_namespace) == 0) {
3632
0
        SSH_LOG(SSH_LOG_TRACE,
3633
0
                "Invalid parameters provided to sshsig_verify: empty namespace "
3634
0
                "string");
3635
0
        return SSH_ERROR;
3636
0
    }
3637
3638
0
    rc = sshsig_dearmor(signature, &sig_buf);
3639
0
    if (rc != SSH_OK) {
3640
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to dearmor signature");
3641
0
        return SSH_ERROR;
3642
0
    }
3643
3644
0
    if (ssh_buffer_get_len(sig_buf) < SSHSIG_MAGIC_PREAMBLE_LEN ||
3645
0
        memcmp(ssh_buffer_get(sig_buf),
3646
0
               SSHSIG_MAGIC_PREAMBLE,
3647
0
               SSHSIG_MAGIC_PREAMBLE_LEN) != 0) {
3648
0
        SSH_LOG(SSH_LOG_TRACE, "Invalid signature magic preamble");
3649
0
        SSH_BUFFER_FREE(sig_buf);
3650
0
        return SSH_ERROR;
3651
0
    }
3652
3653
0
    ssh_buffer_pass_bytes(sig_buf, SSHSIG_MAGIC_PREAMBLE_LEN);
3654
0
    rc = ssh_buffer_unpack(sig_buf,
3655
0
                           "dSSSsS",
3656
0
                           &sig_version,
3657
0
                           &pubkey_blob,
3658
0
                           &sig_namespace_str,
3659
0
                           &reserved_str,
3660
0
                           &hash_alg_str,
3661
0
                           &sig_data);
3662
3663
0
    if (rc != SSH_OK) {
3664
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to unpack signature buffer");
3665
0
        SSH_BUFFER_FREE(sig_buf);
3666
0
        return SSH_ERROR;
3667
0
    }
3668
3669
0
    if (sig_version != SSHSIG_VERSION) {
3670
0
        SSH_LOG(SSH_LOG_TRACE,
3671
0
                "Unsupported signature version %u, expected %u",
3672
0
                sig_version,
3673
0
                SSHSIG_VERSION);
3674
0
        rc = SSH_ERROR;
3675
0
        goto cleanup;
3676
0
    }
3677
3678
0
    rc = ssh_pki_import_pubkey_blob(pubkey_blob, &key);
3679
0
    if (rc != SSH_OK) {
3680
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to import public key from signature");
3681
0
        goto cleanup;
3682
0
    }
3683
3684
0
    if (ssh_string_len(sig_namespace_str) != strlen(sig_namespace) ||
3685
0
        memcmp(ssh_string_data(sig_namespace_str),
3686
0
               sig_namespace,
3687
0
               strlen(sig_namespace)) != 0) {
3688
0
        SSH_LOG(SSH_LOG_TRACE,
3689
0
                "Signature namespace mismatch: expected '%s', got '%s'",
3690
0
                sig_namespace,
3691
0
                ssh_string_get_char(sig_namespace_str));
3692
0
        rc = SSH_ERROR;
3693
0
        goto cleanup;
3694
0
    }
3695
3696
0
    if (strcmp(hash_alg_str, "sha256") != 0 &&
3697
0
        strcmp(hash_alg_str, "sha512") != 0) {
3698
0
        SSH_LOG(SSH_LOG_TRACE, "Unsupported hash algorithm '%s'", hash_alg_str);
3699
0
        rc = SSH_ERROR;
3700
0
        goto cleanup;
3701
0
    }
3702
3703
0
    rc = sshsig_prepare_data(data,
3704
0
                             data_length,
3705
0
                             hash_alg_str,
3706
0
                             sig_namespace,
3707
0
                             &tosign);
3708
0
    if (rc != SSH_OK) {
3709
0
        SSH_LOG(SSH_LOG_TRACE,
3710
0
                "Failed to prepare data for sshsig verification");
3711
0
        goto cleanup;
3712
0
    }
3713
3714
0
    rc = ssh_pki_import_signature_blob(sig_data, key, &signature_obj);
3715
0
    if (rc != SSH_OK) {
3716
0
        SSH_LOG(SSH_LOG_TRACE, "Failed to import signature blob");
3717
0
        goto cleanup;
3718
0
    }
3719
3720
0
    if (is_sk_key_type(key->type)) {
3721
0
        ssh_buffer sk_buffer = NULL;
3722
0
        rc = pki_sk_signature_buffer_prepare(key,
3723
0
                                             signature_obj,
3724
0
                                             ssh_buffer_get(tosign),
3725
0
                                             ssh_buffer_get_len(tosign),
3726
0
                                             &sk_buffer);
3727
0
        if (rc != SSH_OK) {
3728
0
            SSH_LOG(SSH_LOG_TRACE, "Failed to prepare sk signature buffer");
3729
0
            goto cleanup;
3730
0
        }
3731
3732
0
        rc = pki_verify_data_signature(signature_obj,
3733
0
                                       key,
3734
0
                                       ssh_buffer_get(sk_buffer),
3735
0
                                       ssh_buffer_get_len(sk_buffer));
3736
0
        SSH_BUFFER_FREE(sk_buffer);
3737
0
    } else {
3738
0
        rc = pki_verify_data_signature(signature_obj,
3739
0
                                       key,
3740
0
                                       ssh_buffer_get(tosign),
3741
0
                                       ssh_buffer_get_len(tosign));
3742
0
    }
3743
0
    if (rc != SSH_OK) {
3744
0
        SSH_LOG(SSH_LOG_TRACE, "Signature verification failed");
3745
0
        goto cleanup;
3746
0
    }
3747
3748
0
    if (sign_key != NULL) {
3749
0
        *sign_key = key;
3750
0
        key = NULL; /* Transferred ownership */
3751
0
    }
3752
3753
0
cleanup:
3754
0
    SSH_STRING_FREE(pubkey_blob);
3755
0
    SSH_STRING_FREE(sig_namespace_str);
3756
0
    SSH_STRING_FREE(reserved_str);
3757
0
    SSH_STRING_FREE(sig_data);
3758
0
    SSH_BUFFER_FREE(tosign);
3759
0
    SSH_BUFFER_FREE(sig_buf);
3760
0
    SSH_KEY_FREE(key);
3761
0
    SAFE_FREE(hash_alg_str);
3762
0
    SSH_SIGNATURE_FREE(signature_obj);
3763
3764
0
    return rc;
3765
0
}
3766
3767
/*
3768
 * This function signs the session id as a string then
3769
 * the content of sigbuf */
3770
ssh_string ssh_pki_do_sign(ssh_session session,
3771
                           ssh_buffer sigbuf,
3772
                           const ssh_key privkey,
3773
                           enum ssh_digest_e hash_type)
3774
0
{
3775
0
    struct ssh_crypto_struct *crypto = NULL;
3776
3777
0
    ssh_signature sig = NULL;
3778
0
    ssh_string sig_blob = NULL;
3779
3780
0
    ssh_string session_id = NULL;
3781
0
    ssh_buffer sign_input = NULL;
3782
3783
0
    int rc;
3784
3785
0
    if (session == NULL || sigbuf == NULL || privkey == NULL ||
3786
0
        !ssh_key_is_private(privkey))
3787
0
    {
3788
0
        SSH_LOG(SSH_LOG_TRACE, "Bad parameter provided to "
3789
0
                               "ssh_pki_do_sign()");
3790
0
        return NULL;
3791
0
    }
3792
3793
0
    crypto = ssh_packet_get_current_crypto(session, SSH_DIRECTION_BOTH);
3794
0
    if (crypto == NULL) {
3795
0
        return NULL;
3796
0
    }
3797
3798
    /* Get the session ID */
3799
0
    session_id = ssh_string_new(crypto->session_id_len);
3800
0
    if (session_id == NULL) {
3801
0
        return NULL;
3802
0
    }
3803
0
    rc = ssh_string_fill(session_id, crypto->session_id, crypto->session_id_len);
3804
0
    if (rc < 0) {
3805
0
        goto end;
3806
0
    }
3807
3808
    /* Fill the input */
3809
0
    sign_input = ssh_buffer_new();
3810
0
    if (sign_input == NULL) {
3811
0
        goto end;
3812
0
    }
3813
0
    ssh_buffer_set_secure(sign_input);
3814
3815
0
    rc = ssh_buffer_pack(sign_input,
3816
0
                         "SP",
3817
0
                         session_id,
3818
0
                         (size_t)ssh_buffer_get_len(sigbuf),
3819
0
                         ssh_buffer_get(sigbuf));
3820
0
    if (rc != SSH_OK) {
3821
0
        goto end;
3822
0
    }
3823
3824
    /* Generate the signature */
3825
0
    if (is_sk_key_type(privkey->type)) {
3826
#ifdef WITH_FIDO2
3827
        if (session->pki_context == NULL ||
3828
            session->pki_context->sk_callbacks == NULL) {
3829
            SSH_LOG(SSH_LOG_WARN, "Missing PKI context or SK callbacks");
3830
            goto end;
3831
        }
3832
3833
        rc = pki_key_check_hash_compatible(privkey, hash_type);
3834
        if (rc != SSH_OK) {
3835
            SSH_LOG(SSH_LOG_WARN,
3836
                    "Incompatible hash type %d for sk key type %d",
3837
                    hash_type,
3838
                    privkey->type);
3839
            goto end;
3840
        }
3841
3842
        sig = pki_sk_do_sign(session->pki_context,
3843
                             privkey,
3844
                             ssh_buffer_get(sign_input),
3845
                             ssh_buffer_get_len(sign_input));
3846
#else
3847
0
        SSH_LOG(SSH_LOG_WARN, SK_NOT_SUPPORTED_MSG);
3848
0
        goto end;
3849
0
#endif /* WITH_FIDO2 */
3850
0
    } else {
3851
0
        sig = pki_do_sign(privkey,
3852
0
                          ssh_buffer_get(sign_input),
3853
0
                          ssh_buffer_get_len(sign_input),
3854
0
                          hash_type);
3855
0
    }
3856
3857
0
    if (sig == NULL) {
3858
0
        goto end;
3859
0
    }
3860
3861
    /* Convert the signature to blob */
3862
0
    rc = ssh_pki_export_signature_blob(sig, &sig_blob);
3863
0
    if (rc < 0) {
3864
0
        sig_blob = NULL;
3865
0
    }
3866
3867
0
end:
3868
0
    ssh_signature_free(sig);
3869
0
    SSH_BUFFER_FREE(sign_input);
3870
0
    SSH_STRING_FREE(session_id);
3871
3872
0
    return sig_blob;
3873
0
}
3874
3875
ssh_string ssh_pki_do_sign_agent(ssh_session session,
3876
                                 struct ssh_buffer_struct *buf,
3877
                                 const ssh_key pubkey)
3878
0
{
3879
0
    struct ssh_crypto_struct *crypto = NULL;
3880
0
    ssh_string session_id = NULL;
3881
0
    ssh_string sig_blob = NULL;
3882
0
    ssh_buffer sig_buf = NULL;
3883
0
    int rc;
3884
3885
0
    crypto = ssh_packet_get_current_crypto(session, SSH_DIRECTION_BOTH);
3886
0
    if (crypto == NULL) {
3887
0
        return NULL;
3888
0
    }
3889
3890
    /* prepend session identifier */
3891
0
    session_id = ssh_string_new(crypto->session_id_len);
3892
0
    if (session_id == NULL) {
3893
0
        return NULL;
3894
0
    }
3895
0
    rc = ssh_string_fill(session_id, crypto->session_id, crypto->session_id_len);
3896
0
    if (rc < 0) {
3897
0
        SSH_STRING_FREE(session_id);
3898
0
        return NULL;
3899
0
    }
3900
3901
0
    sig_buf = ssh_buffer_new();
3902
0
    if (sig_buf == NULL) {
3903
0
        SSH_STRING_FREE(session_id);
3904
0
        return NULL;
3905
0
    }
3906
3907
0
    rc = ssh_buffer_add_ssh_string(sig_buf, session_id);
3908
0
    if (rc < 0) {
3909
0
        SSH_STRING_FREE(session_id);
3910
0
        SSH_BUFFER_FREE(sig_buf);
3911
0
        return NULL;
3912
0
    }
3913
0
    SSH_STRING_FREE(session_id);
3914
3915
    /* append out buffer */
3916
0
    if (ssh_buffer_add_buffer(sig_buf, buf) < 0) {
3917
0
        SSH_BUFFER_FREE(sig_buf);
3918
0
        return NULL;
3919
0
    }
3920
3921
    /* create signature */
3922
0
    sig_blob = ssh_agent_sign_data(session, pubkey, sig_buf);
3923
3924
0
    SSH_BUFFER_FREE(sig_buf);
3925
3926
0
    return sig_blob;
3927
0
}
3928
3929
#ifdef WITH_SERVER
3930
ssh_string ssh_srv_pki_do_sign_sessionid(ssh_session session,
3931
                                         const ssh_key privkey,
3932
                                         const enum ssh_digest_e digest)
3933
0
{
3934
0
    struct ssh_crypto_struct *crypto = NULL;
3935
0
    bool allowed;
3936
0
    ssh_signature sig = NULL;
3937
0
    ssh_string sig_blob = NULL;
3938
3939
0
    ssh_buffer sign_input = NULL;
3940
3941
0
    int rc;
3942
3943
0
    if (session == NULL || privkey == NULL || !ssh_key_is_private(privkey)) {
3944
0
        return NULL;
3945
0
    }
3946
3947
0
    allowed = ssh_key_size_allowed(session, privkey);
3948
0
    if (!allowed) {
3949
0
        ssh_set_error(session, SSH_FATAL, "The hostkey size too small");
3950
0
        return NULL;
3951
0
    }
3952
3953
0
    crypto = session->next_crypto ? session->next_crypto :
3954
0
                                    session->current_crypto;
3955
3956
0
    if (crypto->secret_hash == NULL){
3957
0
        ssh_set_error(session, SSH_FATAL, "Missing secret_hash");
3958
0
        return NULL;
3959
0
    }
3960
3961
    /* Fill the input */
3962
0
    sign_input = ssh_buffer_new();
3963
0
    if (sign_input == NULL) {
3964
0
        goto end;
3965
0
    }
3966
0
    ssh_buffer_set_secure(sign_input);
3967
3968
0
    rc = ssh_buffer_pack(sign_input,
3969
0
                         "P",
3970
0
                         crypto->digest_len,
3971
0
                         crypto->secret_hash);
3972
0
    if (rc != SSH_OK) {
3973
0
        goto end;
3974
0
    }
3975
3976
    /* Generate the signature */
3977
0
    sig = pki_do_sign(privkey,
3978
0
                      ssh_buffer_get(sign_input),
3979
0
                      ssh_buffer_get_len(sign_input),
3980
0
                      digest);
3981
0
    if (sig == NULL) {
3982
0
        goto end;
3983
0
    }
3984
3985
    /* Convert the signature to blob */
3986
0
    rc = ssh_pki_export_signature_blob(sig, &sig_blob);
3987
0
    if (rc < 0) {
3988
0
        sig_blob = NULL;
3989
0
    }
3990
3991
0
end:
3992
0
    ssh_signature_free(sig);
3993
0
    SSH_BUFFER_FREE(sign_input);
3994
3995
0
    return sig_blob;
3996
0
}
3997
#endif /* WITH_SERVER */
3998
3999
/**
4000
 * @}
4001
 */