Coverage Report

Created: 2026-12-01 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hpn-ssh/sshsig.c
Line
Count
Source
1
/* $OpenBSD: sshsig.c,v 1.42 2026/10/30 02:18:03 djm Exp $ */
2
/*
3
 * Copyright (c) 2019 Google LLC
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
18
#include "includes.h"
19
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <stdarg.h>
23
#include <errno.h>
24
#include <string.h>
25
#include <unistd.h>
26
27
#include "authfd.h"
28
#include "authfile.h"
29
#include "log.h"
30
#include "misc.h"
31
#include "sshbuf.h"
32
#include "sshsig.h"
33
#include "ssherr.h"
34
#include "sshkey.h"
35
#include "match.h"
36
#include "digest.h"
37
#include "myproposal.h"
38
39
3.94k
#define SIG_VERSION   0x01
40
15.4k
#define MAGIC_PREAMBLE    "SSHSIG"
41
5.72k
#define MAGIC_PREAMBLE_LEN  (sizeof(MAGIC_PREAMBLE) - 1)
42
0
#define BEGIN_SIGNATURE   "-----BEGIN SSH SIGNATURE-----"
43
0
#define END_SIGNATURE   "-----END SSH SIGNATURE-----"
44
0
#define RSA_SIGN_ALG    "rsa-sha2-512"
45
48
#define RSA_SIGN_ALLOWED  "rsa-sha2-512,rsa-sha2-256"
46
0
#define HASHALG_DEFAULT   "sha512"
47
1.80k
#define HASHALG_ALLOWED   "sha256,sha512"
48
49
int
50
sshsig_armor(const struct sshbuf *blob, struct sshbuf **out)
51
0
{
52
0
  struct sshbuf *buf = NULL;
53
0
  int r = SSH_ERR_INTERNAL_ERROR;
54
55
0
  *out = NULL;
56
57
0
  if ((buf = sshbuf_new()) == NULL) {
58
0
    error_f("sshbuf_new failed");
59
0
    r = SSH_ERR_ALLOC_FAIL;
60
0
    goto out;
61
0
  }
62
63
0
  if ((r = sshbuf_putf(buf, "%s\n", BEGIN_SIGNATURE)) != 0) {
64
0
    error_fr(r, "sshbuf_putf");
65
0
    goto out;
66
0
  }
67
68
0
  if ((r = sshbuf_dtob64(blob, buf, 1)) != 0) {
69
0
    error_fr(r, "base64 encode signature");
70
0
    goto out;
71
0
  }
72
73
0
  if ((r = sshbuf_put(buf, END_SIGNATURE,
74
0
      sizeof(END_SIGNATURE)-1)) != 0 ||
75
0
      (r = sshbuf_put_u8(buf, '\n')) != 0) {
76
0
    error_fr(r, "sshbuf_put");
77
0
    goto out;
78
0
  }
79
  /* success */
80
0
  *out = buf;
81
0
  buf = NULL; /* transferred */
82
0
  r = 0;
83
0
 out:
84
0
  sshbuf_free(buf);
85
0
  return r;
86
0
}
87
88
int
89
sshsig_dearmor(struct sshbuf *sig, struct sshbuf **out)
90
0
{
91
0
  int r;
92
0
  size_t eoffset = 0;
93
0
  struct sshbuf *buf = NULL;
94
0
  struct sshbuf *sbuf = NULL;
95
0
  char *b64 = NULL;
96
97
0
  if ((sbuf = sshbuf_fromb(sig)) == NULL) {
98
0
    error_f("sshbuf_fromb failed");
99
0
    return SSH_ERR_ALLOC_FAIL;
100
0
  }
101
102
  /* Expect and consume preamble + lf/crlf */
103
0
  if ((r = sshbuf_cmp(sbuf, 0,
104
0
      BEGIN_SIGNATURE, sizeof(BEGIN_SIGNATURE)-1)) != 0) {
105
0
    error("Couldn't parse signature: missing header");
106
0
    goto done;
107
0
  }
108
0
  if ((r = sshbuf_consume(sbuf, sizeof(BEGIN_SIGNATURE)-1)) != 0) {
109
0
    error_fr(r, "consume");
110
0
    goto done;
111
0
  }
112
0
  if ((r = sshbuf_cmp(sbuf, 0, "\r\n", 2)) == 0)
113
0
    eoffset = 2;
114
0
  else if ((r = sshbuf_cmp(sbuf, 0, "\n", 1)) == 0)
115
0
    eoffset = 1;
116
0
  else {
117
0
    r = SSH_ERR_INVALID_FORMAT;
118
0
    error_f("no header eol");
119
0
    goto done;
120
0
  }
121
0
  if ((r = sshbuf_consume(sbuf, eoffset)) != 0) {
122
0
    error_fr(r, "consume eol");
123
0
    goto done;
124
0
  }
125
  /* Find and consume lf + suffix (any prior cr would be ignored) */
126
0
  if ((r = sshbuf_find(sbuf, 0, "\n" END_SIGNATURE,
127
0
      sizeof(END_SIGNATURE), &eoffset)) != 0) {
128
0
    error("Couldn't parse signature: missing footer");
129
0
    goto done;
130
0
  }
131
0
  if ((r = sshbuf_consume_end(sbuf, sshbuf_len(sbuf)-eoffset)) != 0) {
132
0
    error_fr(r, "consume");
133
0
    goto done;
134
0
  }
135
136
0
  if ((b64 = sshbuf_dup_string(sbuf)) == NULL) {
137
0
    error_f("sshbuf_dup_string failed");
138
0
    r = SSH_ERR_ALLOC_FAIL;
139
0
    goto done;
140
0
  }
141
142
0
  if ((buf = sshbuf_new()) == NULL) {
143
0
    error_f("sshbuf_new() failed");
144
0
    r = SSH_ERR_ALLOC_FAIL;
145
0
    goto done;
146
0
  }
147
148
0
  if ((r = sshbuf_b64tod(buf, b64)) != 0) {
149
0
    error_fr(r, "decode base64");
150
0
    goto done;
151
0
  }
152
153
  /* success */
154
0
  *out = buf;
155
0
  r = 0;
156
0
  buf = NULL; /* transferred */
157
0
done:
158
0
  sshbuf_free(buf);
159
0
  sshbuf_free(sbuf);
160
0
  free(b64);
161
0
  return r;
162
0
}
163
164
static int
165
sshsig_wrap_sign(struct sshkey *key, const char *hashalg,
166
    const char *sk_provider, const char *sk_pin, const struct sshbuf *h_message,
167
    const char *sig_namespace, struct sshbuf **out,
168
    sshsig_signer *signer, void *signer_ctx)
169
0
{
170
0
  int r;
171
0
  size_t slen = 0;
172
0
  u_char *sig = NULL;
173
0
  struct sshbuf *blob = NULL;
174
0
  struct sshbuf *tosign = NULL;
175
0
  const char *sign_alg = NULL;
176
177
0
  if ((tosign = sshbuf_new()) == NULL ||
178
0
      (blob = sshbuf_new()) == NULL) {
179
0
    error_f("sshbuf_new failed");
180
0
    r = SSH_ERR_ALLOC_FAIL;
181
0
    goto done;
182
0
  }
183
184
0
  if ((r = sshbuf_put(tosign, MAGIC_PREAMBLE, MAGIC_PREAMBLE_LEN)) != 0 ||
185
0
      (r = sshbuf_put_cstring(tosign, sig_namespace)) != 0 ||
186
0
      (r = sshbuf_put_string(tosign, NULL, 0)) != 0 || /* reserved */
187
0
      (r = sshbuf_put_cstring(tosign, hashalg)) != 0 ||
188
0
      (r = sshbuf_put_stringb(tosign, h_message)) != 0) {
189
0
    error_fr(r, "assemble message to sign");
190
0
    goto done;
191
0
  }
192
193
  /* If using RSA keys then default to a good signature algorithm */
194
0
  if (sshkey_type_plain(key->type) == KEY_RSA) {
195
0
    sign_alg = RSA_SIGN_ALG;
196
0
    if (strcmp(hashalg, "sha256") == 0)
197
0
      sign_alg = "rsa-sha2-256";
198
0
    else if (strcmp(hashalg, "sha512") == 0)
199
0
      sign_alg = "rsa-sha2-512";
200
0
  }
201
202
0
  if (signer != NULL) {
203
0
    if ((r = signer(key, &sig, &slen,
204
0
        sshbuf_ptr(tosign), sshbuf_len(tosign),
205
0
        sign_alg, sk_provider, sk_pin, 0, signer_ctx)) != 0) {
206
0
      error_r(r, "Couldn't sign message (signer)");
207
0
      goto done;
208
0
    }
209
0
  } else {
210
0
    if ((r = sshkey_sign(key, &sig, &slen,
211
0
        sshbuf_ptr(tosign), sshbuf_len(tosign),
212
0
        sign_alg, sk_provider, sk_pin, 0)) != 0) {
213
0
      error_r(r, "Couldn't sign message");
214
0
      goto done;
215
0
    }
216
0
  }
217
218
0
  if ((r = sshbuf_put(blob, MAGIC_PREAMBLE, MAGIC_PREAMBLE_LEN)) != 0 ||
219
0
      (r = sshbuf_put_u32(blob, SIG_VERSION)) != 0 ||
220
0
      (r = sshkey_puts(key, blob)) != 0 ||
221
0
      (r = sshbuf_put_cstring(blob, sig_namespace)) != 0 ||
222
0
      (r = sshbuf_put_string(blob, NULL, 0)) != 0 || /* reserved */
223
0
      (r = sshbuf_put_cstring(blob, hashalg)) != 0 ||
224
0
      (r = sshbuf_put_string(blob, sig, slen)) != 0) {
225
0
    error_fr(r, "assemble signature object");
226
0
    goto done;
227
0
  }
228
229
0
  if (out != NULL) {
230
0
    *out = blob;
231
0
    blob = NULL;
232
0
  }
233
0
  r = 0;
234
0
done:
235
0
  free(sig);
236
0
  sshbuf_free(blob);
237
0
  sshbuf_free(tosign);
238
0
  return r;
239
0
}
240
241
/* Check preamble and version. */
242
static int
243
sshsig_parse_preamble(struct sshbuf *buf)
244
3.96k
{
245
3.96k
  int r = SSH_ERR_INTERNAL_ERROR;
246
3.96k
  uint32_t sversion;
247
248
3.96k
  if ((r = sshbuf_cmp(buf, 0, MAGIC_PREAMBLE, MAGIC_PREAMBLE_LEN)) != 0 ||
249
3.94k
      (r = sshbuf_consume(buf, (sizeof(MAGIC_PREAMBLE)-1))) != 0 ||
250
3.94k
      (r = sshbuf_get_u32(buf, &sversion)) != 0) {
251
21
    error("Couldn't verify signature: invalid format");
252
21
    return r;
253
21
  }
254
255
3.94k
  if (sversion > SIG_VERSION) {
256
49
    error("Signature version %lu is larger than supported "
257
49
        "version %u", (unsigned long)sversion, SIG_VERSION);
258
49
    return SSH_ERR_INVALID_FORMAT;
259
49
  }
260
3.89k
  return 0;
261
3.94k
}
262
263
static int
264
sshsig_check_hashalg(const char *hashalg)
265
1.80k
{
266
1.80k
  if (hashalg == NULL ||
267
1.80k
      match_pattern_list(hashalg, HASHALG_ALLOWED, 0) == 1)
268
1.76k
    return 0;
269
39
  error_f("unsupported hash algorithm \"%.100s\"", hashalg);
270
39
  return SSH_ERR_SIGN_ALG_UNSUPPORTED;
271
1.80k
}
272
273
static int
274
sshsig_peek_hashalg(struct sshbuf *signature, char **hashalgp)
275
2.20k
{
276
2.20k
  struct sshbuf *buf = NULL;
277
2.20k
  char *hashalg = NULL;
278
2.20k
  int r = SSH_ERR_INTERNAL_ERROR;
279
280
2.20k
  if (hashalgp != NULL)
281
2.20k
    *hashalgp = NULL;
282
2.20k
  if ((buf = sshbuf_fromb(signature)) == NULL)
283
0
    return SSH_ERR_ALLOC_FAIL;
284
2.20k
  if ((r = sshsig_parse_preamble(buf)) != 0)
285
70
    goto done;
286
2.13k
  if ((r = sshbuf_get_string_direct(buf, NULL, NULL)) != 0 ||
287
2.06k
      (r = sshbuf_get_string_direct(buf, NULL, NULL)) != 0 ||
288
2.00k
      (r = sshbuf_get_string(buf, NULL, NULL)) != 0 ||
289
1.93k
      (r = sshbuf_get_cstring(buf, &hashalg, NULL)) != 0 ||
290
1.85k
      (r = sshbuf_get_string_direct(buf, NULL, NULL)) != 0) {
291
330
    error_fr(r, "parse signature object");
292
330
    goto done;
293
330
  }
294
295
  /* success */
296
1.80k
  r = 0;
297
1.80k
  *hashalgp = hashalg;
298
1.80k
  hashalg = NULL;
299
2.20k
 done:
300
2.20k
  free(hashalg);
301
2.20k
  sshbuf_free(buf);
302
2.20k
  return r;
303
1.80k
}
304
305
static int
306
sshsig_wrap_verify(struct sshbuf *signature, const char *hashalg,
307
    const struct sshbuf *h_message, const char *expect_namespace,
308
    struct sshkey **sign_keyp, struct sshkey_sig_details **sig_details)
309
1.76k
{
310
1.76k
  int r = SSH_ERR_INTERNAL_ERROR;
311
1.76k
  struct sshbuf *buf = NULL, *toverify = NULL;
312
1.76k
  struct sshkey *key = NULL;
313
1.76k
  const u_char *sig;
314
1.76k
  char *got_namespace = NULL, *sigtype = NULL, *sig_hashalg = NULL;
315
1.76k
  size_t siglen;
316
317
1.76k
  debug_f("verify message length %zu", sshbuf_len(h_message));
318
1.76k
  if (sig_details != NULL)
319
1.76k
    *sig_details = NULL;
320
1.76k
  if (sign_keyp != NULL)
321
1.76k
    *sign_keyp = NULL;
322
323
1.76k
  if ((toverify = sshbuf_new()) == NULL) {
324
0
    error_f("sshbuf_new failed");
325
0
    r = SSH_ERR_ALLOC_FAIL;
326
0
    goto done;
327
0
  }
328
1.76k
  if ((r = sshbuf_put(toverify, MAGIC_PREAMBLE,
329
1.76k
      MAGIC_PREAMBLE_LEN)) != 0 ||
330
1.76k
      (r = sshbuf_put_cstring(toverify, expect_namespace)) != 0 ||
331
1.76k
      (r = sshbuf_put_string(toverify, NULL, 0)) != 0 || /* reserved */
332
1.76k
      (r = sshbuf_put_cstring(toverify, hashalg)) != 0 ||
333
1.76k
      (r = sshbuf_put_stringb(toverify, h_message)) != 0) {
334
0
    error_fr(r, "assemble message to verify");
335
0
    goto done;
336
0
  }
337
338
1.76k
  if ((r = sshsig_parse_preamble(signature)) != 0)
339
0
    goto done;
340
341
1.76k
  if ((r = sshkey_froms(signature, &key)) != 0 ||
342
545
      (r = sshbuf_get_cstring(signature, &got_namespace, NULL)) != 0 ||
343
544
      (r = sshbuf_get_string(signature, NULL, NULL)) != 0 ||
344
544
      (r = sshbuf_get_cstring(signature, &sig_hashalg, NULL)) != 0 ||
345
1.21k
      (r = sshbuf_get_string_direct(signature, &sig, &siglen)) != 0) {
346
1.21k
    error_fr(r, "parse signature object");
347
1.21k
    goto done;
348
1.21k
  }
349
350
544
  if (sshbuf_len(signature) != 0) {
351
29
    error("Signature contains trailing data");
352
29
    r = SSH_ERR_INVALID_FORMAT;
353
29
    goto done;
354
29
  }
355
356
515
  if (strcmp(expect_namespace, got_namespace) != 0) {
357
63
    error("Couldn't verify signature: namespace does not match");
358
63
    debug_f("expected namespace \"%s\" received \"%s\"",
359
63
        expect_namespace, got_namespace);
360
63
    r = SSH_ERR_SIGNATURE_INVALID;
361
63
    goto done;
362
63
  }
363
452
  if (strcmp(hashalg, sig_hashalg) != 0) {
364
0
    error("Couldn't verify signature: hash algorithm mismatch");
365
0
    debug_f("expected algorithm \"%s\" received \"%s\"",
366
0
        hashalg, sig_hashalg);
367
0
    r = SSH_ERR_SIGNATURE_INVALID;
368
0
    goto done;
369
0
  }
370
  /* Ensure that RSA keys use an acceptable signature algorithm */
371
452
  if (sshkey_type_plain(key->type) == KEY_RSA) {
372
50
    if ((r = sshkey_get_sigtype(sig, siglen, &sigtype)) != 0) {
373
2
      error_r(r, "Couldn't verify signature: unable to get "
374
2
          "signature type");
375
2
      goto done;
376
2
    }
377
48
    if (match_pattern_list(sigtype, RSA_SIGN_ALLOWED, 0) != 1) {
378
6
      error("Couldn't verify signature: unsupported RSA "
379
6
          "signature algorithm %s", sigtype);
380
6
      r = SSH_ERR_SIGN_ALG_UNSUPPORTED;
381
6
      goto done;
382
6
    }
383
48
  }
384
444
  if ((r = sshkey_verify(key, sig, siglen, sshbuf_ptr(toverify),
385
444
      sshbuf_len(toverify), NULL, 0, sig_details)) != 0) {
386
443
    error_r(r, "Signature verification failed");
387
443
    goto done;
388
443
  }
389
390
  /* success */
391
1
  r = 0;
392
1
  if (sign_keyp != NULL) {
393
1
    *sign_keyp = key;
394
1
    key = NULL; /* transferred */
395
1
  }
396
1.76k
done:
397
1.76k
  free(got_namespace);
398
1.76k
  free(sigtype);
399
1.76k
  free(sig_hashalg);
400
1.76k
  sshbuf_free(buf);
401
1.76k
  sshbuf_free(toverify);
402
1.76k
  sshkey_free(key);
403
1.76k
  return r;
404
1
}
405
406
static int
407
hash_buffer(const struct sshbuf *m, const char *hashalg, struct sshbuf **bp)
408
1.80k
{
409
1.80k
  char *hex, hash[SSH_DIGEST_MAX_LENGTH];
410
1.80k
  int alg, r = SSH_ERR_INTERNAL_ERROR;
411
1.80k
  struct sshbuf *b = NULL;
412
413
1.80k
  *bp = NULL;
414
1.80k
  memset(hash, 0, sizeof(hash));
415
416
1.80k
  if ((r = sshsig_check_hashalg(hashalg)) != 0)
417
39
    return r;
418
1.76k
  if ((alg = ssh_digest_alg_by_name(hashalg)) == -1) {
419
0
    error_f("can't look up hash algorithm %s", hashalg);
420
0
    return SSH_ERR_INTERNAL_ERROR;
421
0
  }
422
1.76k
  if ((r = ssh_digest_buffer(alg, m, hash, sizeof(hash))) != 0) {
423
0
    error_fr(r, "ssh_digest_buffer");
424
0
    return r;
425
0
  }
426
1.76k
  if ((hex = tohex(hash, ssh_digest_bytes(alg))) != NULL) {
427
1.76k
    debug3_f("final hash: %s", hex);
428
1.76k
    freezero(hex, strlen(hex));
429
1.76k
  }
430
1.76k
  if ((b = sshbuf_new()) == NULL) {
431
0
    r = SSH_ERR_ALLOC_FAIL;
432
0
    goto out;
433
0
  }
434
1.76k
  if ((r = sshbuf_put(b, hash, ssh_digest_bytes(alg))) != 0) {
435
0
    error_fr(r, "sshbuf_put");
436
0
    goto out;
437
0
  }
438
1.76k
  *bp = b;
439
1.76k
  b = NULL; /* transferred */
440
  /* success */
441
1.76k
  r = 0;
442
1.76k
 out:
443
1.76k
  sshbuf_free(b);
444
1.76k
  explicit_bzero(hash, sizeof(hash));
445
1.76k
  return r;
446
1.76k
}
447
448
int
449
sshsig_signb(struct sshkey *key, const char *hashalg,
450
    const char *sk_provider, const char *sk_pin,
451
    const struct sshbuf *message, const char *sig_namespace,
452
    struct sshbuf **out, sshsig_signer *signer, void *signer_ctx)
453
0
{
454
0
  struct sshbuf *b = NULL;
455
0
  int r = SSH_ERR_INTERNAL_ERROR;
456
457
0
  if (hashalg == NULL)
458
0
    hashalg = HASHALG_DEFAULT;
459
0
  if (out != NULL)
460
0
    *out = NULL;
461
0
  if ((r = hash_buffer(message, hashalg, &b)) != 0) {
462
0
    error_fr(r, "hash buffer");
463
0
    goto out;
464
0
  }
465
0
  if ((r = sshsig_wrap_sign(key, hashalg, sk_provider, sk_pin, b,
466
0
      sig_namespace, out, signer, signer_ctx)) != 0)
467
0
    goto out;
468
  /* success */
469
0
  r = 0;
470
0
 out:
471
0
  sshbuf_free(b);
472
0
  return r;
473
0
}
474
475
int
476
sshsig_verifyb(struct sshbuf *signature, const struct sshbuf *message,
477
    const char *expect_namespace, struct sshkey **sign_keyp,
478
    struct sshkey_sig_details **sig_details)
479
2.20k
{
480
2.20k
  struct sshbuf *b = NULL;
481
2.20k
  int r = SSH_ERR_INTERNAL_ERROR;
482
2.20k
  char *hashalg = NULL;
483
484
2.20k
  if (sig_details != NULL)
485
2.20k
    *sig_details = NULL;
486
2.20k
  if (sign_keyp != NULL)
487
2.20k
    *sign_keyp = NULL;
488
2.20k
  if ((r = sshsig_peek_hashalg(signature, &hashalg)) != 0)
489
400
    return r;
490
1.80k
  debug_f("signature made with hash \"%s\"", hashalg);
491
1.80k
  if ((r = hash_buffer(message, hashalg, &b)) != 0) {
492
39
    error_fr(r, "hash buffer");
493
39
    goto out;
494
39
  }
495
1.76k
  if ((r = sshsig_wrap_verify(signature, hashalg, b, expect_namespace,
496
1.76k
      sign_keyp, sig_details)) != 0)
497
1.76k
    goto out;
498
  /* success */
499
1
  r = 0;
500
1.80k
 out:
501
1.80k
  sshbuf_free(b);
502
1.80k
  free(hashalg);
503
1.80k
  return r;
504
1
}
505
506
static int
507
hash_file(int fd, const char *hashalg, struct sshbuf **bp)
508
0
{
509
0
  char *hex, rbuf[8192], hash[SSH_DIGEST_MAX_LENGTH];
510
0
  ssize_t n, total = 0;
511
0
  struct ssh_digest_ctx *ctx = NULL;
512
0
  int alg, oerrno, r = SSH_ERR_INTERNAL_ERROR;
513
0
  struct sshbuf *b = NULL;
514
515
0
  *bp = NULL;
516
0
  memset(hash, 0, sizeof(hash));
517
518
0
  if ((r = sshsig_check_hashalg(hashalg)) != 0)
519
0
    return r;
520
0
  if ((alg = ssh_digest_alg_by_name(hashalg)) == -1) {
521
0
    error_f("can't look up hash algorithm %s", hashalg);
522
0
    return SSH_ERR_INTERNAL_ERROR;
523
0
  }
524
0
  if ((ctx = ssh_digest_start(alg)) == NULL) {
525
0
    error_f("ssh_digest_start failed");
526
0
    return SSH_ERR_INTERNAL_ERROR;
527
0
  }
528
0
  for (;;) {
529
0
    if ((n = read(fd, rbuf, sizeof(rbuf))) == -1) {
530
0
      if (errno == EINTR || errno == EAGAIN)
531
0
        continue;
532
0
      oerrno = errno;
533
0
      error_f("read: %s", strerror(errno));
534
0
      errno = oerrno;
535
0
      r = SSH_ERR_SYSTEM_ERROR;
536
0
      goto out;
537
0
    } else if (n == 0) {
538
0
      debug2_f("hashed %zu bytes", total);
539
0
      break; /* EOF */
540
0
    }
541
0
    total += (size_t)n;
542
0
    if ((r = ssh_digest_update(ctx, rbuf, (size_t)n)) != 0) {
543
0
      error_fr(r, "ssh_digest_update");
544
0
      goto out;
545
0
    }
546
0
  }
547
0
  if ((r = ssh_digest_final(ctx, hash, sizeof(hash))) != 0) {
548
0
    error_fr(r, "ssh_digest_final");
549
0
    goto out;
550
0
  }
551
0
  if ((hex = tohex(hash, ssh_digest_bytes(alg))) != NULL) {
552
0
    debug3_f("final hash: %s", hex);
553
0
    freezero(hex, strlen(hex));
554
0
  }
555
0
  if ((b = sshbuf_new()) == NULL) {
556
0
    r = SSH_ERR_ALLOC_FAIL;
557
0
    goto out;
558
0
  }
559
0
  if ((r = sshbuf_put(b, hash, ssh_digest_bytes(alg))) != 0) {
560
0
    error_fr(r, "sshbuf_put");
561
0
    goto out;
562
0
  }
563
0
  *bp = b;
564
0
  b = NULL; /* transferred */
565
  /* success */
566
0
  r = 0;
567
0
 out:
568
0
  oerrno = errno;
569
0
  sshbuf_free(b);
570
0
  ssh_digest_free(ctx);
571
0
  explicit_bzero(hash, sizeof(hash));
572
0
  errno = oerrno;
573
0
  return r;
574
0
}
575
576
int
577
sshsig_sign_fd(struct sshkey *key, const char *hashalg,
578
    const char *sk_provider, const char *sk_pin,
579
    int fd, const char *sig_namespace, struct sshbuf **out,
580
    sshsig_signer *signer, void *signer_ctx)
581
0
{
582
0
  struct sshbuf *b = NULL;
583
0
  int r = SSH_ERR_INTERNAL_ERROR;
584
585
0
  if (hashalg == NULL)
586
0
    hashalg = HASHALG_DEFAULT;
587
0
  if (out != NULL)
588
0
    *out = NULL;
589
0
  if ((r = hash_file(fd, hashalg, &b)) != 0) {
590
0
    error_fr(r, "hash_file");
591
0
    return r;
592
0
  }
593
0
  if ((r = sshsig_wrap_sign(key, hashalg, sk_provider, sk_pin, b,
594
0
      sig_namespace, out, signer, signer_ctx)) != 0)
595
0
    goto out;
596
  /* success */
597
0
  r = 0;
598
0
 out:
599
0
  sshbuf_free(b);
600
0
  return r;
601
0
}
602
603
int
604
sshsig_verify_fd(struct sshbuf *signature, int fd,
605
    const char *expect_namespace, struct sshkey **sign_keyp,
606
    struct sshkey_sig_details **sig_details)
607
0
{
608
0
  struct sshbuf *b = NULL;
609
0
  int r = SSH_ERR_INTERNAL_ERROR;
610
0
  char *hashalg = NULL;
611
612
0
  if (sig_details != NULL)
613
0
    *sig_details = NULL;
614
0
  if (sign_keyp != NULL)
615
0
    *sign_keyp = NULL;
616
0
  if ((r = sshsig_peek_hashalg(signature, &hashalg)) != 0)
617
0
    return r;
618
0
  debug_f("signature made with hash \"%s\"", hashalg);
619
0
  if ((r = hash_file(fd, hashalg, &b)) != 0) {
620
0
    error_fr(r, "hash_file");
621
0
    goto out;
622
0
  }
623
0
  if ((r = sshsig_wrap_verify(signature, hashalg, b, expect_namespace,
624
0
      sign_keyp, sig_details)) != 0)
625
0
    goto out;
626
  /* success */
627
0
  r = 0;
628
0
 out:
629
0
  sshbuf_free(b);
630
0
  free(hashalg);
631
0
  return r;
632
0
}
633
634
struct sshsigopt {
635
  int ca;
636
  char *namespaces;
637
  uint64_t valid_after, valid_before;
638
};
639
640
struct sshsigopt *
641
sshsigopt_parse(const char *opts, const char *path, u_long linenum,
642
    const char **errstrp)
643
1.02k
{
644
1.02k
  struct sshsigopt *ret;
645
1.02k
  int r;
646
1.02k
  char *opt;
647
1.02k
  const char *errstr = NULL;
648
649
1.02k
  if ((ret = calloc(1, sizeof(*ret))) == NULL)
650
0
    return NULL;
651
1.02k
  if (opts == NULL || *opts == '\0')
652
1
    return ret; /* Empty options yields empty options :) */
653
654
1.95k
  while (*opts && *opts != ' ' && *opts != '\t') {
655
    /* flag options */
656
1.95k
    if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
657
194
      ret->ca = 1;
658
1.76k
    } else if (opt_match(&opts, "namespaces")) {
659
76
      if (ret->namespaces != NULL) {
660
3
        errstr = "multiple \"namespaces\" clauses";
661
3
        goto fail;
662
3
      }
663
73
      ret->namespaces = opt_dequote(&opts, &errstr);
664
73
      if (ret->namespaces == NULL)
665
62
        goto fail;
666
1.68k
    } else if (opt_match(&opts, "valid-after")) {
667
580
      if (ret->valid_after != 0) {
668
81
        errstr = "multiple \"valid-after\" clauses";
669
81
        goto fail;
670
81
      }
671
499
      if ((opt = opt_dequote(&opts, &errstr)) == NULL)
672
2
        goto fail;
673
497
      if (parse_absolute_time(opt, &ret->valid_after) != 0 ||
674
401
          ret->valid_after == 0) {
675
98
        free(opt);
676
98
        errstr = "invalid \"valid-after\" time";
677
98
        goto fail;
678
98
      }
679
399
      free(opt);
680
1.10k
    } else if (opt_match(&opts, "valid-before")) {
681
395
      if (ret->valid_before != 0) {
682
82
        errstr = "multiple \"valid-before\" clauses";
683
82
        goto fail;
684
82
      }
685
313
      if ((opt = opt_dequote(&opts, &errstr)) == NULL)
686
2
        goto fail;
687
311
      if (parse_absolute_time(opt, &ret->valid_before) != 0 ||
688
305
          ret->valid_before == 0) {
689
7
        free(opt);
690
7
        errstr = "invalid \"valid-before\" time";
691
7
        goto fail;
692
7
      }
693
304
      free(opt);
694
304
    }
695
    /*
696
     * Skip the comma, and move to the next option
697
     * (or break out if there are no more).
698
     */
699
1.62k
    if (*opts == '\0' || *opts == ' ' || *opts == '\t')
700
403
      break;    /* End of options. */
701
    /* Anything other than a comma is an unknown option */
702
1.21k
    if (*opts != ',') {
703
276
      errstr = "unknown key option";
704
276
      goto fail;
705
276
    }
706
941
    opts++;
707
941
    if (*opts == '\0') {
708
5
      errstr = "unexpected end-of-options";
709
5
      goto fail;
710
5
    }
711
941
  }
712
  /* final consistency check */
713
405
  if (ret->valid_after != 0 && ret->valid_before != 0 &&
714
138
      ret->valid_before <= ret->valid_after) {
715
42
    errstr = "\"valid-before\" time is before \"valid-after\"";
716
42
    goto fail;
717
42
  }
718
  /* success */
719
363
  return ret;
720
660
 fail:
721
660
  if (errstrp != NULL)
722
0
    *errstrp = errstr;
723
660
  sshsigopt_free(ret);
724
660
  return NULL;
725
405
}
726
727
void
728
sshsigopt_free(struct sshsigopt *opts)
729
1.68k
{
730
1.68k
  if (opts == NULL)
731
660
    return;
732
1.02k
  free(opts->namespaces);
733
1.02k
  free(opts);
734
1.02k
}
735
736
static int
737
parse_principals_key_and_options(const char *path, u_long linenum, char *line,
738
    const char *required_principal, char **principalsp, struct sshkey **keyp,
739
    struct sshsigopt **sigoptsp)
740
0
{
741
0
  char *opts = NULL, *tmp, *cp, *principals = NULL;
742
0
  const char *reason = NULL;
743
0
  struct sshsigopt *sigopts = NULL;
744
0
  struct sshkey *key = NULL;
745
0
  int r = SSH_ERR_INTERNAL_ERROR;
746
747
0
  if (principalsp != NULL)
748
0
    *principalsp = NULL;
749
0
  if (sigoptsp != NULL)
750
0
    *sigoptsp = NULL;
751
0
  if (keyp != NULL)
752
0
    *keyp = NULL;
753
754
0
  cp = line;
755
0
  cp = cp + strspn(cp, " \t\n\r"); /* skip leading whitespace */
756
0
  if (*cp == '#' || *cp == '\0')
757
0
    return SSH_ERR_KEY_NOT_FOUND; /* blank or all-comment line */
758
759
  /* format: identity[,identity...] [option[,option...]] key */
760
0
  if ((tmp = strdelimw(&cp)) == NULL || cp == NULL) {
761
0
    error("%s:%lu: invalid line", path, linenum);
762
0
    r = SSH_ERR_INVALID_FORMAT;
763
0
    goto out;
764
0
  }
765
0
  if ((principals = strdup(tmp)) == NULL) {
766
0
    error_f("strdup failed");
767
0
    r = SSH_ERR_ALLOC_FAIL;
768
0
    goto out;
769
0
  }
770
  /*
771
   * Bail out early if we're looking for a particular principal and this
772
   * line does not list it.
773
   */
774
0
  if (required_principal != NULL) {
775
0
    if (match_pattern_list(required_principal,
776
0
        principals, 0) != 1) {
777
      /* principal didn't match */
778
0
      r = SSH_ERR_KEY_NOT_FOUND;
779
0
      goto out;
780
0
    }
781
0
    debug_f("%s:%lu: matched principal \"%s\"",
782
0
        path, linenum, required_principal);
783
0
  }
784
785
0
  if ((key = sshkey_new(KEY_UNSPEC)) == NULL) {
786
0
    error_f("sshkey_new failed");
787
0
    r = SSH_ERR_ALLOC_FAIL;
788
0
    goto out;
789
0
  }
790
0
  if (sshkey_read(key, &cp) != 0) {
791
    /* no key? Check for options */
792
0
    opts = cp;
793
0
    if (sshkey_advance_past_options(&cp) != 0) {
794
0
      error("%s:%lu: invalid options", path, linenum);
795
0
      r = SSH_ERR_INVALID_FORMAT;
796
0
      goto out;
797
0
    }
798
0
    if (cp == NULL || *cp == '\0') {
799
0
      error("%s:%lu: missing key", path, linenum);
800
0
      r = SSH_ERR_INVALID_FORMAT;
801
0
      goto out;
802
0
    }
803
0
    *cp++ = '\0';
804
0
    skip_space(&cp);
805
0
    if (sshkey_read(key, &cp) != 0) {
806
0
      error("%s:%lu: invalid key", path, linenum);
807
0
      r = SSH_ERR_INVALID_FORMAT;
808
0
      goto out;
809
0
    }
810
0
  }
811
0
  debug3("%s:%lu: options %s", path, linenum, opts == NULL ? "" : opts);
812
0
  if ((sigopts = sshsigopt_parse(opts, path, linenum, &reason)) == NULL) {
813
0
    error("%s:%lu: bad options: %s", path, linenum, reason);
814
0
    r = SSH_ERR_INVALID_FORMAT;
815
0
    goto out;
816
0
  }
817
  /* success */
818
0
  if (principalsp != NULL) {
819
0
    *principalsp = principals;
820
0
    principals = NULL; /* transferred */
821
0
  }
822
0
  if (sigoptsp != NULL) {
823
0
    *sigoptsp = sigopts;
824
0
    sigopts = NULL; /* transferred */
825
0
  }
826
0
  if (keyp != NULL) {
827
0
    *keyp = key;
828
0
    key = NULL; /* transferred */
829
0
  }
830
0
  r = 0;
831
0
 out:
832
0
  free(principals);
833
0
  sshsigopt_free(sigopts);
834
0
  sshkey_free(key);
835
0
  return r;
836
0
}
837
838
static int
839
cert_filter_principals(const char *path, u_long linenum,
840
    char **principalsp, const struct sshkey *cert, uint64_t verify_time)
841
0
{
842
0
  char *cp, *oprincipals, *principals;
843
0
  const char *reason;
844
0
  struct sshbuf *nprincipals;
845
0
  int r = SSH_ERR_INTERNAL_ERROR, success = 0;
846
0
  u_int i;
847
848
0
  oprincipals = principals = *principalsp;
849
0
  *principalsp = NULL;
850
851
0
  if ((nprincipals = sshbuf_new()) == NULL) {
852
0
    r = SSH_ERR_ALLOC_FAIL;
853
0
    goto out;
854
0
  }
855
856
0
  while ((cp = strsep(&principals, ",")) != NULL && *cp != '\0') {
857
    /* Check certificate validity */
858
0
    if ((r = sshkey_cert_check_authority(cert, 0, 0, verify_time,
859
0
        NULL, &reason)) != 0) {
860
0
      debug("%s:%lu: principal \"%s\" not authorized: %s",
861
0
          path, linenum, cp, reason);
862
0
      continue;
863
0
    }
864
    /* Return all matching principal names from the cert */
865
0
    for (i = 0; i < cert->cert->nprincipals; i++) {
866
0
      if (match_pattern(cert->cert->principals[i], cp)) {
867
0
        if ((r = sshbuf_putf(nprincipals, "%s%s",
868
0
          sshbuf_len(nprincipals) != 0 ? "," : "",
869
0
            cert->cert->principals[i])) != 0) {
870
0
          error_f("buffer error");
871
0
          goto out;
872
0
        }
873
0
      }
874
0
    }
875
0
  }
876
0
  if (sshbuf_len(nprincipals) == 0) {
877
0
    error("%s:%lu: no valid principals found", path, linenum);
878
0
    r = SSH_ERR_KEY_CERT_INVALID;
879
0
    goto out;
880
0
  }
881
0
  if ((principals = sshbuf_dup_string(nprincipals)) == NULL) {
882
0
    error_f("buffer error");
883
0
    r = SSH_ERR_ALLOC_FAIL;
884
0
    goto out;
885
0
  }
886
  /* success */
887
0
  success = 1;
888
0
  *principalsp = principals;
889
0
 out:
890
0
  sshbuf_free(nprincipals);
891
0
  free(oprincipals);
892
0
  return success ? 0 : r;
893
0
}
894
895
static int
896
check_allowed_keys_line(const char *path, u_long linenum, char *line,
897
    const struct sshkey *sign_key, const char *principal,
898
    const char *sig_namespace, uint64_t verify_time, char **principalsp)
899
0
{
900
0
  struct sshkey *found_key = NULL;
901
0
  char *principals = NULL;
902
0
  int r, success = 0;
903
0
  const char *reason = NULL;
904
0
  struct sshsigopt *sigopts = NULL;
905
0
  char tvalid[64], tverify[64];
906
907
0
  if (principalsp != NULL)
908
0
    *principalsp = NULL;
909
910
  /* Parse the line */
911
0
  if ((r = parse_principals_key_and_options(path, linenum, line,
912
0
      principal, &principals, &found_key, &sigopts)) != 0) {
913
    /* error already logged */
914
0
    goto done;
915
0
  }
916
917
0
  if (!sigopts->ca && sshkey_equal(found_key, sign_key)) {
918
    /* Exact match of key */
919
0
    debug("%s:%lu: matched key", path, linenum);
920
0
  } else if (sigopts->ca && sshkey_is_cert(sign_key) &&
921
0
      sshkey_equal_public(sign_key->cert->signature_key, found_key)) {
922
    /* Ensure that the CA used an acceptable signature algorithm */
923
0
    if ((r = sshkey_check_cert_sigtype(sign_key,
924
0
        SSH_ALLOWED_CA_SIGALGS)) != 0) {
925
0
      error("%s:%lu: certificate signed with disallowed "
926
0
          "algorithm %s", path, linenum,
927
0
          sign_key->cert->signature_type);
928
0
      goto done;
929
0
    }
930
0
    if (principal) {
931
      /* Match certificate CA key with specified principal */
932
0
      if ((r = sshkey_cert_check_authority(sign_key, 0, 0,
933
0
          verify_time, principal, &reason)) != 0) {
934
0
        error("%s:%lu: certificate not authorized: %s",
935
0
            path, linenum, reason);
936
0
        goto done;
937
0
      }
938
0
      debug("%s:%lu: matched certificate CA key",
939
0
          path, linenum);
940
0
    } else {
941
      /* No principal specified - find all matching ones */
942
0
      if ((r = cert_filter_principals(path, linenum,
943
0
          &principals, sign_key, verify_time)) != 0) {
944
        /* error already displayed */
945
0
        debug_r(r, "%s:%lu: cert_filter_principals",
946
0
            path, linenum);
947
0
        goto done;
948
0
      }
949
0
      debug("%s:%lu: matched certificate CA key",
950
0
          path, linenum);
951
0
    }
952
0
  } else {
953
    /* Didn't match key */
954
0
    goto done;
955
0
  }
956
957
  /* Check whether options preclude the use of this key */
958
0
  if (sigopts->namespaces != NULL && sig_namespace != NULL &&
959
0
      match_pattern_list(sig_namespace, sigopts->namespaces, 0) != 1) {
960
0
    error("%s:%lu: key is not permitted for use in signature "
961
0
        "namespace \"%s\"", path, linenum, sig_namespace);
962
0
    goto done;
963
0
  }
964
965
  /* check key time validity */
966
0
  format_absolute_time((uint64_t)verify_time, tverify, sizeof(tverify));
967
0
  if (sigopts->valid_after != 0 &&
968
0
      (uint64_t)verify_time < sigopts->valid_after) {
969
0
    format_absolute_time(sigopts->valid_after,
970
0
        tvalid, sizeof(tvalid));
971
0
    error("%s:%lu: key is not yet valid: "
972
0
        "verify time %s < valid-after %s", path, linenum,
973
0
        tverify, tvalid);
974
0
    goto done;
975
0
  }
976
0
  if (sigopts->valid_before != 0 &&
977
0
      (uint64_t)verify_time > sigopts->valid_before) {
978
0
    format_absolute_time(sigopts->valid_before,
979
0
        tvalid, sizeof(tvalid));
980
0
    error("%s:%lu: key has expired: "
981
0
        "verify time %s > valid-before %s", path, linenum,
982
0
        tverify, tvalid);
983
0
    goto done;
984
0
  }
985
0
  success = 1;
986
987
0
 done:
988
0
  if (success && principalsp != NULL) {
989
0
    *principalsp = principals;
990
0
    principals = NULL; /* transferred */
991
0
  }
992
0
  free(principals);
993
0
  sshkey_free(found_key);
994
0
  sshsigopt_free(sigopts);
995
0
  return success ? 0 : SSH_ERR_KEY_NOT_FOUND;
996
0
}
997
998
int
999
sshsig_check_allowed_keys(const char *path, const struct sshkey *sign_key,
1000
    const char *principal, const char *sig_namespace, uint64_t verify_time)
1001
0
{
1002
0
  FILE *f = NULL;
1003
0
  char *line = NULL;
1004
0
  size_t linesize = 0;
1005
0
  u_long linenum = 0;
1006
0
  int r = SSH_ERR_KEY_NOT_FOUND, oerrno;
1007
1008
  /* Check key and principal against file */
1009
0
  if ((f = fopen(path, "r")) == NULL) {
1010
0
    oerrno = errno;
1011
0
    error("Unable to open allowed keys file \"%s\": %s",
1012
0
        path, strerror(errno));
1013
0
    errno = oerrno;
1014
0
    return SSH_ERR_SYSTEM_ERROR;
1015
0
  }
1016
1017
0
  while (getline(&line, &linesize, f) != -1) {
1018
0
    linenum++;
1019
0
    r = check_allowed_keys_line(path, linenum, line, sign_key,
1020
0
        principal, sig_namespace, verify_time, NULL);
1021
0
    free(line);
1022
0
    line = NULL;
1023
0
    linesize = 0;
1024
0
    if (r == SSH_ERR_KEY_NOT_FOUND)
1025
0
      continue;
1026
0
    else if (r == 0) {
1027
      /* success */
1028
0
      fclose(f);
1029
0
      return 0;
1030
0
    } else
1031
0
      break;
1032
0
  }
1033
  /* Either we hit an error parsing or we simply didn't find the key */
1034
0
  fclose(f);
1035
0
  free(line);
1036
0
  return r;
1037
0
}
1038
1039
int
1040
sshsig_find_principals(const char *path, const struct sshkey *sign_key,
1041
    uint64_t verify_time, char **principals)
1042
0
{
1043
0
  FILE *f = NULL;
1044
0
  char *line = NULL;
1045
0
  size_t linesize = 0;
1046
0
  u_long linenum = 0;
1047
0
  int r = SSH_ERR_KEY_NOT_FOUND, oerrno;
1048
1049
0
  if ((f = fopen(path, "r")) == NULL) {
1050
0
    oerrno = errno;
1051
0
    error("Unable to open allowed keys file \"%s\": %s",
1052
0
        path, strerror(errno));
1053
0
    errno = oerrno;
1054
0
    return SSH_ERR_SYSTEM_ERROR;
1055
0
  }
1056
1057
0
  while (getline(&line, &linesize, f) != -1) {
1058
0
    linenum++;
1059
0
    r = check_allowed_keys_line(path, linenum, line,
1060
0
        sign_key, NULL, NULL, verify_time, principals);
1061
0
    free(line);
1062
0
    line = NULL;
1063
0
    linesize = 0;
1064
0
    if (r == SSH_ERR_KEY_NOT_FOUND)
1065
0
      continue;
1066
0
    else if (r == 0) {
1067
      /* success */
1068
0
      fclose(f);
1069
0
      return 0;
1070
0
    } else
1071
0
      break;
1072
0
  }
1073
0
  free(line);
1074
  /* Either we hit an error parsing or we simply didn't find the key */
1075
0
  if (ferror(f) != 0) {
1076
0
    oerrno = errno;
1077
0
    fclose(f);
1078
0
    error("Unable to read allowed keys file \"%s\": %s",
1079
0
        path, strerror(errno));
1080
0
    errno = oerrno;
1081
0
    return SSH_ERR_SYSTEM_ERROR;
1082
0
  }
1083
0
  fclose(f);
1084
0
  return r;
1085
0
}
1086
1087
int
1088
sshsig_match_principals(const char *path, const char *principal,
1089
    char ***principalsp, size_t *nprincipalsp)
1090
0
{
1091
0
  FILE *f = NULL;
1092
0
  char *found, *line = NULL, **principals = NULL, **tmp;
1093
0
  size_t i, nprincipals = 0, linesize = 0;
1094
0
  u_long linenum = 0;
1095
0
  int oerrno = 0, r, ret = 0;
1096
1097
0
  if (principalsp != NULL)
1098
0
    *principalsp = NULL;
1099
0
  if (nprincipalsp != NULL)
1100
0
    *nprincipalsp = 0;
1101
1102
  /* Check key and principal against file */
1103
0
  if ((f = fopen(path, "r")) == NULL) {
1104
0
    oerrno = errno;
1105
0
    error("Unable to open allowed keys file \"%s\": %s",
1106
0
        path, strerror(errno));
1107
0
    errno = oerrno;
1108
0
    return SSH_ERR_SYSTEM_ERROR;
1109
0
  }
1110
1111
0
  while (getline(&line, &linesize, f) != -1) {
1112
0
    linenum++;
1113
    /* Parse the line */
1114
0
    if ((r = parse_principals_key_and_options(path, linenum, line,
1115
0
        principal, &found, NULL, NULL)) != 0) {
1116
0
      if (r == SSH_ERR_KEY_NOT_FOUND)
1117
0
        continue;
1118
0
      ret = r;
1119
0
      oerrno = errno;
1120
0
      break; /* unexpected error */
1121
0
    }
1122
0
    if ((tmp = recallocarray(principals, nprincipals,
1123
0
        nprincipals + 1, sizeof(*principals))) == NULL) {
1124
0
      ret = SSH_ERR_ALLOC_FAIL;
1125
0
      free(found);
1126
0
      break;
1127
0
    }
1128
0
    principals = tmp;
1129
0
    principals[nprincipals++] = found; /* transferred */
1130
0
    free(line);
1131
0
    line = NULL;
1132
0
    linesize = 0;
1133
0
  }
1134
0
  fclose(f);
1135
0
  free(line);
1136
1137
0
  if (ret == 0) {
1138
0
    if (nprincipals == 0)
1139
0
      ret = SSH_ERR_KEY_NOT_FOUND;
1140
0
    if (nprincipalsp != NULL)
1141
0
      *nprincipalsp = nprincipals;
1142
0
    if (principalsp != NULL) {
1143
0
      *principalsp = principals;
1144
0
      principals = NULL; /* transferred */
1145
0
      nprincipals = 0;
1146
0
    }
1147
0
  }
1148
1149
0
  for (i = 0; i < nprincipals; i++)
1150
0
    free(principals[i]);
1151
0
  free(principals);
1152
1153
0
  errno = oerrno;
1154
0
  return ret;
1155
0
}
1156
1157
int
1158
sshsig_get_pubkey(struct sshbuf *signature, struct sshkey **pubkey)
1159
0
{
1160
0
  struct sshkey *pk = NULL;
1161
0
  int r = SSH_ERR_SIGNATURE_INVALID;
1162
1163
0
  if (pubkey == NULL)
1164
0
    return SSH_ERR_INTERNAL_ERROR;
1165
0
  if ((r = sshsig_parse_preamble(signature)) != 0)
1166
0
    return r;
1167
0
  if ((r = sshkey_froms(signature, &pk)) != 0)
1168
0
    return r;
1169
1170
0
  *pubkey = pk;
1171
  pk = NULL;
1172
0
  return 0;
1173
0
}