blob: e63a36e1ec43c83986c061db3955b893045f044c [file] [log] [blame]
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +00001/*
2 * Copyright (c) 2019 Google LLC
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
Damien Miller1a72c0d2019-09-03 18:44:10 +100017#include "includes.h"
18
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +000019#include <stdio.h>
20#include <stdlib.h>
21#include <stdarg.h>
22#include <errno.h>
23#include <string.h>
24#include <unistd.h>
25
26#include "authfd.h"
27#include "authfile.h"
28#include "log.h"
29#include "misc.h"
30#include "sshbuf.h"
31#include "sshsig.h"
32#include "ssherr.h"
33#include "sshkey.h"
34#include "match.h"
35#include "digest.h"
36
37#define SIG_VERSION 0x01
38#define MAGIC_PREAMBLE "SSHSIG"
39#define MAGIC_PREAMBLE_LEN (sizeof(MAGIC_PREAMBLE) - 1)
40#define BEGIN_SIGNATURE "-----BEGIN SSH SIGNATURE-----\n"
41#define END_SIGNATURE "-----END SSH SIGNATURE-----"
42#define RSA_SIGN_ALG "rsa-sha2-512" /* XXX maybe make configurable */
43#define RSA_SIGN_ALLOWED "rsa-sha2-512,rsa-sha2-256"
44#define HASHALG_DEFAULT "sha512" /* XXX maybe make configurable */
45#define HASHALG_ALLOWED "sha256,sha512"
46
47int
48sshsig_armor(const struct sshbuf *blob, struct sshbuf **out)
49{
50 struct sshbuf *buf = NULL;
51 int r = SSH_ERR_INTERNAL_ERROR;
52
53 *out = NULL;
54
55 if ((buf = sshbuf_new()) == NULL) {
56 error("%s: sshbuf_new failed", __func__);
57 r = SSH_ERR_ALLOC_FAIL;
58 goto out;
59 }
60
61 if ((r = sshbuf_put(buf, BEGIN_SIGNATURE,
62 sizeof(BEGIN_SIGNATURE)-1)) != 0) {
63 error("%s: sshbuf_putf failed: %s", __func__, ssh_err(r));
64 goto out;
65 }
66
67 if ((r = sshbuf_dtob64(blob, buf, 1)) != 0) {
68 error("%s: Couldn't base64 encode signature blob: %s",
69 __func__, ssh_err(r));
70 goto out;
71 }
72
73 if ((r = sshbuf_put(buf, END_SIGNATURE,
74 sizeof(END_SIGNATURE)-1)) != 0 ||
75 (r = sshbuf_put_u8(buf, '\n')) != 0) {
76 error("%s: sshbuf_put failed: %s", __func__, ssh_err(r));
77 goto out;
78 }
79 /* success */
80 *out = buf;
81 buf = NULL; /* transferred */
82 r = 0;
83 out:
84 sshbuf_free(buf);
85 return r;
86}
87
88int
89sshsig_dearmor(struct sshbuf *sig, struct sshbuf **out)
90{
91 int r;
92 size_t eoffset = 0;
93 struct sshbuf *buf = NULL;
94 struct sshbuf *sbuf = NULL;
95 char *b64 = NULL;
96
97 if ((sbuf = sshbuf_fromb(sig)) == NULL) {
98 error("%s: sshbuf_fromb failed", __func__);
99 return SSH_ERR_ALLOC_FAIL;
100 }
101
102 if ((r = sshbuf_cmp(sbuf, 0,
103 BEGIN_SIGNATURE, sizeof(BEGIN_SIGNATURE)-1)) != 0) {
104 error("Couldn't parse signature: missing header");
105 goto done;
106 }
107
108 if ((r = sshbuf_consume(sbuf, sizeof(BEGIN_SIGNATURE)-1)) != 0) {
109 error("%s: sshbuf_consume failed: %s", __func__, ssh_err(r));
110 goto done;
111 }
112
113 if ((r = sshbuf_find(sbuf, 0, "\n" END_SIGNATURE,
114 sizeof("\n" END_SIGNATURE)-1, &eoffset)) != 0) {
115 error("Couldn't parse signature: missing footer");
116 goto done;
117 }
118
119 if ((r = sshbuf_consume_end(sbuf, sshbuf_len(sbuf)-eoffset)) != 0) {
120 error("%s: sshbuf_consume failed: %s", __func__, ssh_err(r));
121 goto done;
122 }
123
124 if ((b64 = sshbuf_dup_string(sbuf)) == NULL) {
125 error("%s: sshbuf_dup_string failed", __func__);
126 r = SSH_ERR_ALLOC_FAIL;
127 goto done;
128 }
129
130 if ((buf = sshbuf_new()) == NULL) {
131 error("%s: sshbuf_new() failed", __func__);
132 r = SSH_ERR_ALLOC_FAIL;
133 goto done;
134 }
135
136 if ((r = sshbuf_b64tod(buf, b64)) != 0) {
naddy@openbsd.org0f44e592019-09-03 20:51:49 +0000137 error("Couldn't decode signature: %s", ssh_err(r));
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000138 goto done;
139 }
140
141 /* success */
142 *out = buf;
143 r = 0;
144 buf = NULL; /* transferred */
145done:
146 sshbuf_free(buf);
147 sshbuf_free(sbuf);
148 free(b64);
149 return r;
150}
151
152static int
153sshsig_wrap_sign(struct sshkey *key, const char *hashalg,
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000154 const char *sk_provider, const struct sshbuf *h_message,
155 const char *sig_namespace, struct sshbuf **out,
156 sshsig_signer *signer, void *signer_ctx)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000157{
158 int r;
159 size_t slen = 0;
160 u_char *sig = NULL;
161 struct sshbuf *blob = NULL;
162 struct sshbuf *tosign = NULL;
163 const char *sign_alg = NULL;
164
165 if ((tosign = sshbuf_new()) == NULL ||
166 (blob = sshbuf_new()) == NULL) {
167 error("%s: sshbuf_new failed", __func__);
168 r = SSH_ERR_ALLOC_FAIL;
169 goto done;
170 }
171
172 if ((r = sshbuf_put(tosign, MAGIC_PREAMBLE, MAGIC_PREAMBLE_LEN)) != 0 ||
173 (r = sshbuf_put_cstring(tosign, sig_namespace)) != 0 ||
174 (r = sshbuf_put_string(tosign, NULL, 0)) != 0 || /* reserved */
175 (r = sshbuf_put_cstring(tosign, hashalg)) != 0 ||
djm@openbsd.orgb5a89ee2019-10-02 08:07:13 +0000176 (r = sshbuf_put_stringb(tosign, h_message)) != 0) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000177 error("Couldn't construct message to sign: %s", ssh_err(r));
178 goto done;
179 }
180
181 /* If using RSA keys then default to a good signature algorithm */
182 if (sshkey_type_plain(key->type) == KEY_RSA)
183 sign_alg = RSA_SIGN_ALG;
184
185 if (signer != NULL) {
186 if ((r = signer(key, &sig, &slen,
187 sshbuf_ptr(tosign), sshbuf_len(tosign),
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000188 sign_alg, sk_provider, 0, signer_ctx)) != 0) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000189 error("Couldn't sign message: %s", ssh_err(r));
190 goto done;
191 }
192 } else {
193 if ((r = sshkey_sign(key, &sig, &slen,
194 sshbuf_ptr(tosign), sshbuf_len(tosign),
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000195 sign_alg, sk_provider, 0)) != 0) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000196 error("Couldn't sign message: %s", ssh_err(r));
197 goto done;
198 }
199 }
200
201 if ((r = sshbuf_put(blob, MAGIC_PREAMBLE, MAGIC_PREAMBLE_LEN)) != 0 ||
202 (r = sshbuf_put_u32(blob, SIG_VERSION)) != 0 ||
203 (r = sshkey_puts(key, blob)) != 0 ||
204 (r = sshbuf_put_cstring(blob, sig_namespace)) != 0 ||
205 (r = sshbuf_put_string(blob, NULL, 0)) != 0 || /* reserved */
206 (r = sshbuf_put_cstring(blob, hashalg)) != 0 ||
207 (r = sshbuf_put_string(blob, sig, slen)) != 0) {
208 error("Couldn't populate blob: %s", ssh_err(r));
209 goto done;
210 }
211
212 *out = blob;
213 blob = NULL;
214 r = 0;
215done:
216 free(sig);
217 sshbuf_free(blob);
218 sshbuf_free(tosign);
219 return r;
220}
221
222/* Check preamble and version. */
223static int
224sshsig_parse_preamble(struct sshbuf *buf)
225{
226 int r = SSH_ERR_INTERNAL_ERROR;
227 uint32_t sversion;
228
229 if ((r = sshbuf_cmp(buf, 0, MAGIC_PREAMBLE, MAGIC_PREAMBLE_LEN)) != 0 ||
230 (r = sshbuf_consume(buf, (sizeof(MAGIC_PREAMBLE)-1))) != 0 ||
231 (r = sshbuf_get_u32(buf, &sversion)) != 0) {
232 error("Couldn't verify signature: invalid format");
233 return r;
234 }
235
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000236 if (sversion > SIG_VERSION) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000237 error("Signature version %lu is larger than supported "
238 "version %u", (unsigned long)sversion, SIG_VERSION);
239 return SSH_ERR_INVALID_FORMAT;
240 }
241 return 0;
242}
243
244static int
245sshsig_check_hashalg(const char *hashalg)
246{
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000247 if (hashalg == NULL ||
248 match_pattern_list(hashalg, HASHALG_ALLOWED, 0) == 1)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000249 return 0;
250 error("%s: unsupported hash algorithm \"%.100s\"", __func__, hashalg);
251 return SSH_ERR_SIGN_ALG_UNSUPPORTED;
252}
253
254static int
255sshsig_peek_hashalg(struct sshbuf *signature, char **hashalgp)
256{
257 struct sshbuf *buf = NULL;
258 char *hashalg = NULL;
259 int r = SSH_ERR_INTERNAL_ERROR;
260
261 if (hashalgp != NULL)
262 *hashalgp = NULL;
263 if ((buf = sshbuf_fromb(signature)) == NULL)
264 return SSH_ERR_ALLOC_FAIL;
265 if ((r = sshsig_parse_preamble(buf)) != 0)
266 goto done;
267 if ((r = sshbuf_get_string_direct(buf, NULL, NULL)) != 0 ||
268 (r = sshbuf_get_string_direct(buf, NULL, NULL)) != 0 ||
269 (r = sshbuf_get_string(buf, NULL, NULL)) != 0 ||
270 (r = sshbuf_get_cstring(buf, &hashalg, NULL)) != 0 ||
271 (r = sshbuf_get_string_direct(buf, NULL, NULL)) != 0) {
272 error("Couldn't parse signature blob: %s", ssh_err(r));
273 goto done;
274 }
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000275
276 /* success */
277 r = 0;
278 *hashalgp = hashalg;
279 hashalg = NULL;
280 done:
281 free(hashalg);
282 sshbuf_free(buf);
283 return r;
284}
285
286static int
287sshsig_wrap_verify(struct sshbuf *signature, const char *hashalg,
288 const struct sshbuf *h_message, const char *expect_namespace,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000289 struct sshkey **sign_keyp, struct sshkey_sig_details **sig_details)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000290{
291 int r = SSH_ERR_INTERNAL_ERROR;
292 struct sshbuf *buf = NULL, *toverify = NULL;
293 struct sshkey *key = NULL;
294 const u_char *sig;
295 char *got_namespace = NULL, *sigtype = NULL, *sig_hashalg = NULL;
296 size_t siglen;
297
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000298 debug("%s: verify message length %zu", __func__, sshbuf_len(h_message));
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000299 if (sig_details != NULL)
300 *sig_details = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000301 if (sign_keyp != NULL)
302 *sign_keyp = NULL;
303
304 if ((toverify = sshbuf_new()) == NULL) {
305 error("%s: sshbuf_new failed", __func__);
306 r = SSH_ERR_ALLOC_FAIL;
307 goto done;
308 }
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000309 if ((r = sshbuf_put(toverify, MAGIC_PREAMBLE,
310 MAGIC_PREAMBLE_LEN)) != 0 ||
311 (r = sshbuf_put_cstring(toverify, expect_namespace)) != 0 ||
312 (r = sshbuf_put_string(toverify, NULL, 0)) != 0 || /* reserved */
313 (r = sshbuf_put_cstring(toverify, hashalg)) != 0 ||
djm@openbsd.orgb5a89ee2019-10-02 08:07:13 +0000314 (r = sshbuf_put_stringb(toverify, h_message)) != 0) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000315 error("Couldn't construct message to verify: %s", ssh_err(r));
316 goto done;
317 }
318
319 if ((r = sshsig_parse_preamble(signature)) != 0)
320 goto done;
321
322 if ((r = sshkey_froms(signature, &key)) != 0 ||
323 (r = sshbuf_get_cstring(signature, &got_namespace, NULL)) != 0 ||
324 (r = sshbuf_get_string(signature, NULL, NULL)) != 0 ||
325 (r = sshbuf_get_cstring(signature, &sig_hashalg, NULL)) != 0 ||
326 (r = sshbuf_get_string_direct(signature, &sig, &siglen)) != 0) {
327 error("Couldn't parse signature blob: %s", ssh_err(r));
328 goto done;
329 }
330
331 if (sshbuf_len(signature) != 0) {
332 error("Signature contains trailing data");
333 r = SSH_ERR_INVALID_FORMAT;
334 goto done;
335 }
336
337 if (strcmp(expect_namespace, got_namespace) != 0) {
338 error("Couldn't verify signature: namespace does not match");
339 debug("%s: expected namespace \"%s\" received \"%s\"",
340 __func__, expect_namespace, got_namespace);
341 r = SSH_ERR_SIGNATURE_INVALID;
342 goto done;
343 }
344 if (strcmp(hashalg, sig_hashalg) != 0) {
345 error("Couldn't verify signature: hash algorithm mismatch");
346 debug("%s: expected algorithm \"%s\" received \"%s\"",
347 __func__, hashalg, sig_hashalg);
348 r = SSH_ERR_SIGNATURE_INVALID;
349 goto done;
350 }
351 /* Ensure that RSA keys use an acceptable signature algorithm */
352 if (sshkey_type_plain(key->type) == KEY_RSA) {
353 if ((r = sshkey_get_sigtype(sig, siglen, &sigtype)) != 0) {
354 error("Couldn't verify signature: unable to get "
355 "signature type: %s", ssh_err(r));
356 goto done;
357 }
358 if (match_pattern_list(sigtype, RSA_SIGN_ALLOWED, 0) != 1) {
359 error("Couldn't verify signature: unsupported RSA "
360 "signature algorithm %s", sigtype);
361 r = SSH_ERR_SIGN_ALG_UNSUPPORTED;
362 goto done;
363 }
364 }
365 if ((r = sshkey_verify(key, sig, siglen, sshbuf_ptr(toverify),
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000366 sshbuf_len(toverify), NULL, 0, sig_details)) != 0) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000367 error("Signature verification failed: %s", ssh_err(r));
368 goto done;
369 }
370
371 /* success */
372 r = 0;
373 if (sign_keyp != NULL) {
374 *sign_keyp = key;
375 key = NULL; /* transferred */
376 }
377done:
378 free(got_namespace);
379 free(sigtype);
380 free(sig_hashalg);
381 sshbuf_free(buf);
382 sshbuf_free(toverify);
383 sshkey_free(key);
384 return r;
385}
386
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000387static int
388hash_buffer(const struct sshbuf *m, const char *hashalg, struct sshbuf **bp)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000389{
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000390 char *hex, hash[SSH_DIGEST_MAX_LENGTH];
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000391 int alg, r = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000392 struct sshbuf *b = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000393
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000394 *bp = NULL;
395 memset(hash, 0, sizeof(hash));
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000396
397 if ((r = sshsig_check_hashalg(hashalg)) != 0)
398 return r;
399 if ((alg = ssh_digest_alg_by_name(hashalg)) == -1) {
400 error("%s: can't look up hash algorithm %s",
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000401 __func__, hashalg);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000402 return SSH_ERR_INTERNAL_ERROR;
403 }
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000404 if ((r = ssh_digest_buffer(alg, m, hash, sizeof(hash))) != 0) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000405 error("%s: ssh_digest_buffer failed: %s", __func__, ssh_err(r));
406 return r;
407 }
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000408 if ((hex = tohex(hash, ssh_digest_bytes(alg))) != NULL) {
409 debug3("%s: final hash: %s", __func__, hex);
410 freezero(hex, strlen(hex));
411 }
412 if ((b = sshbuf_new()) == NULL) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000413 r = SSH_ERR_ALLOC_FAIL;
414 goto out;
415 }
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000416 if ((r = sshbuf_put(b, hash, ssh_digest_bytes(alg))) != 0) {
417 error("%s: sshbuf_put: %s", __func__, ssh_err(r));
418 goto out;
419 }
420 *bp = b;
421 b = NULL; /* transferred */
422 /* success */
423 r = 0;
424 out:
425 sshbuf_free(b);
426 explicit_bzero(hash, sizeof(hash));
427 return 0;
428}
429
430int
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000431sshsig_signb(struct sshkey *key, const char *hashalg, const char *sk_provider,
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000432 const struct sshbuf *message, const char *sig_namespace,
433 struct sshbuf **out, sshsig_signer *signer, void *signer_ctx)
434{
435 struct sshbuf *b = NULL;
436 int r = SSH_ERR_INTERNAL_ERROR;
437
438 if (hashalg == NULL)
439 hashalg = HASHALG_DEFAULT;
440 if (out != NULL)
441 *out = NULL;
442 if ((r = hash_buffer(message, hashalg, &b)) != 0) {
443 error("%s: hash_buffer failed: %s", __func__, ssh_err(r));
444 goto out;
445 }
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000446 if ((r = sshsig_wrap_sign(key, hashalg, sk_provider, b,
447 sig_namespace, out, signer, signer_ctx)) != 0)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000448 goto out;
449 /* success */
450 r = 0;
451 out:
452 sshbuf_free(b);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000453 return r;
454}
455
456int
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000457sshsig_verifyb(struct sshbuf *signature, const struct sshbuf *message,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000458 const char *expect_namespace, struct sshkey **sign_keyp,
459 struct sshkey_sig_details **sig_details)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000460{
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000461 struct sshbuf *b = NULL;
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000462 int r = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000463 char *hashalg = NULL;
464
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000465 if (sig_details != NULL)
466 *sig_details = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000467 if (sign_keyp != NULL)
468 *sign_keyp = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000469 if ((r = sshsig_peek_hashalg(signature, &hashalg)) != 0)
470 return r;
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000471 debug("%s: signature made with hash \"%s\"", __func__, hashalg);
472 if ((r = hash_buffer(message, hashalg, &b)) != 0) {
473 error("%s: hash_buffer failed: %s", __func__, ssh_err(r));
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000474 goto out;
475 }
476 if ((r = sshsig_wrap_verify(signature, hashalg, b, expect_namespace,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000477 sign_keyp, sig_details)) != 0)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000478 goto out;
479 /* success */
480 r = 0;
481 out:
482 sshbuf_free(b);
483 free(hashalg);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000484 return r;
485}
486
487static int
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000488hash_file(int fd, const char *hashalg, struct sshbuf **bp)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000489{
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000490 char *hex, rbuf[8192], hash[SSH_DIGEST_MAX_LENGTH];
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000491 ssize_t n, total = 0;
492 struct ssh_digest_ctx *ctx;
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000493 int alg, oerrno, r = SSH_ERR_INTERNAL_ERROR;
494 struct sshbuf *b = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000495
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000496 *bp = NULL;
497 memset(hash, 0, sizeof(hash));
498
499 if ((r = sshsig_check_hashalg(hashalg)) != 0)
500 return r;
501 if ((alg = ssh_digest_alg_by_name(hashalg)) == -1) {
502 error("%s: can't look up hash algorithm %s",
503 __func__, hashalg);
504 return SSH_ERR_INTERNAL_ERROR;
505 }
506 if ((ctx = ssh_digest_start(alg)) == NULL) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000507 error("%s: ssh_digest_start failed", __func__);
508 return SSH_ERR_INTERNAL_ERROR;
509 }
510 for (;;) {
511 if ((n = read(fd, rbuf, sizeof(rbuf))) == -1) {
512 if (errno == EINTR || errno == EAGAIN)
513 continue;
514 oerrno = errno;
515 error("%s: read: %s", __func__, strerror(errno));
516 ssh_digest_free(ctx);
517 errno = oerrno;
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000518 r = SSH_ERR_SYSTEM_ERROR;
519 goto out;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000520 } else if (n == 0) {
521 debug2("%s: hashed %zu bytes", __func__, total);
522 break; /* EOF */
523 }
524 total += (size_t)n;
525 if ((r = ssh_digest_update(ctx, rbuf, (size_t)n)) != 0) {
526 error("%s: ssh_digest_update: %s",
527 __func__, ssh_err(r));
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000528 goto out;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000529 }
530 }
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000531 if ((r = ssh_digest_final(ctx, hash, sizeof(hash))) != 0) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000532 error("%s: ssh_digest_final: %s", __func__, ssh_err(r));
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000533 goto out;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000534 }
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000535 if ((hex = tohex(hash, ssh_digest_bytes(alg))) != NULL) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000536 debug3("%s: final hash: %s", __func__, hex);
537 freezero(hex, strlen(hex));
538 }
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000539 if ((b = sshbuf_new()) == NULL) {
540 r = SSH_ERR_ALLOC_FAIL;
541 goto out;
542 }
543 if ((r = sshbuf_put(b, hash, ssh_digest_bytes(alg))) != 0) {
544 error("%s: sshbuf_put: %s", __func__, ssh_err(r));
545 goto out;
546 }
547 *bp = b;
548 b = NULL; /* transferred */
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000549 /* success */
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000550 r = 0;
551 out:
552 sshbuf_free(b);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000553 ssh_digest_free(ctx);
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000554 explicit_bzero(hash, sizeof(hash));
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000555 return 0;
556}
557
558int
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000559sshsig_sign_fd(struct sshkey *key, const char *hashalg, const char *sk_provider,
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000560 int fd, const char *sig_namespace, struct sshbuf **out,
561 sshsig_signer *signer, void *signer_ctx)
562{
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000563 struct sshbuf *b = NULL;
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000564 int r = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000565
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000566 if (hashalg == NULL)
567 hashalg = HASHALG_DEFAULT;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000568 if (out != NULL)
569 *out = NULL;
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000570 if ((r = hash_file(fd, hashalg, &b)) != 0) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000571 error("%s: hash_file failed: %s", __func__, ssh_err(r));
572 return r;
573 }
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000574 if ((r = sshsig_wrap_sign(key, hashalg, sk_provider, b,
575 sig_namespace, out, signer, signer_ctx)) != 0)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000576 goto out;
577 /* success */
578 r = 0;
579 out:
580 sshbuf_free(b);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000581 return r;
582}
583
584int
585sshsig_verify_fd(struct sshbuf *signature, int fd,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000586 const char *expect_namespace, struct sshkey **sign_keyp,
587 struct sshkey_sig_details **sig_details)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000588{
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000589 struct sshbuf *b = NULL;
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000590 int r = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000591 char *hashalg = NULL;
592
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000593 if (sig_details != NULL)
594 *sig_details = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000595 if (sign_keyp != NULL)
596 *sign_keyp = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000597 if ((r = sshsig_peek_hashalg(signature, &hashalg)) != 0)
598 return r;
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000599 debug("%s: signature made with hash \"%s\"", __func__, hashalg);
600 if ((r = hash_file(fd, hashalg, &b)) != 0) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000601 error("%s: hash_file failed: %s", __func__, ssh_err(r));
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000602 goto out;
603 }
604 if ((r = sshsig_wrap_verify(signature, hashalg, b, expect_namespace,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +0000605 sign_keyp, sig_details)) != 0)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000606 goto out;
607 /* success */
608 r = 0;
609 out:
610 sshbuf_free(b);
611 free(hashalg);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000612 return r;
613}
614
djm@openbsd.orgbab6feb2019-09-05 04:55:32 +0000615struct sshsigopt {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000616 int ca;
617 char *namespaces;
618};
619
djm@openbsd.orgbab6feb2019-09-05 04:55:32 +0000620struct sshsigopt *
621sshsigopt_parse(const char *opts, const char *path, u_long linenum,
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000622 const char **errstrp)
623{
djm@openbsd.orgbab6feb2019-09-05 04:55:32 +0000624 struct sshsigopt *ret;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000625 int r;
626 const char *errstr = NULL;
627
628 if ((ret = calloc(1, sizeof(*ret))) == NULL)
629 return NULL;
630 if (opts == NULL || *opts == '\0')
631 return ret; /* Empty options yields empty options :) */
632
633 while (*opts && *opts != ' ' && *opts != '\t') {
634 /* flag options */
635 if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
636 ret->ca = 1;
637 } else if (opt_match(&opts, "namespaces")) {
638 if (ret->namespaces != NULL) {
639 errstr = "multiple \"namespaces\" clauses";
640 goto fail;
641 }
642 ret->namespaces = opt_dequote(&opts, &errstr);
643 if (ret->namespaces == NULL)
644 goto fail;
645 }
646 /*
647 * Skip the comma, and move to the next option
648 * (or break out if there are no more).
649 */
650 if (*opts == '\0' || *opts == ' ' || *opts == '\t')
651 break; /* End of options. */
652 /* Anything other than a comma is an unknown option */
653 if (*opts != ',') {
654 errstr = "unknown key option";
655 goto fail;
656 }
657 opts++;
658 if (*opts == '\0') {
659 errstr = "unexpected end-of-options";
660 goto fail;
661 }
662 }
663 /* success */
664 return ret;
665 fail:
666 if (errstrp != NULL)
667 *errstrp = errstr;
djm@openbsd.org69159af2019-09-05 05:42:59 +0000668 sshsigopt_free(ret);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000669 return NULL;
670}
671
djm@openbsd.orgbab6feb2019-09-05 04:55:32 +0000672void
673sshsigopt_free(struct sshsigopt *opts)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000674{
675 if (opts == NULL)
676 return;
677 free(opts->namespaces);
678 free(opts);
679}
680
681static int
djm@openbsd.orge2031b02020-01-22 02:25:21 +0000682parse_principals_key_and_options(const char *path, u_long linenum, char *line,
683 const char *required_principal, char **principalsp, struct sshkey **keyp,
684 struct sshsigopt **sigoptsp)
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000685{
djm@openbsd.orge2031b02020-01-22 02:25:21 +0000686 char *opts = NULL, *tmp, *cp, *principals = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000687 const char *reason = NULL;
djm@openbsd.orgbab6feb2019-09-05 04:55:32 +0000688 struct sshsigopt *sigopts = NULL;
djm@openbsd.orge2031b02020-01-22 02:25:21 +0000689 struct sshkey *key = NULL;
690 int r = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000691
djm@openbsd.orge2031b02020-01-22 02:25:21 +0000692 if (principalsp != NULL)
693 *principalsp = NULL;
694 if (sigoptsp != NULL)
695 *sigoptsp = NULL;
696 if (keyp != NULL)
697 *keyp = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000698
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000699 cp = line;
700 cp = cp + strspn(cp, " \t"); /* skip leading whitespace */
701 if (*cp == '#' || *cp == '\0')
djm@openbsd.orge2031b02020-01-22 02:25:21 +0000702 return SSH_ERR_KEY_NOT_FOUND; /* blank or all-comment line */
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000703
djm@openbsd.orge2031b02020-01-22 02:25:21 +0000704 /* format: identity[,identity...] [option[,option...]] key */
705 if ((tmp = strdelimw(&cp)) == NULL) {
706 error("%s:%lu: invalid line", path, linenum);
707 r = SSH_ERR_INVALID_FORMAT;
708 goto out;
709 }
710 if ((principals = strdup(tmp)) == NULL) {
711 error("%s: strdup failed", __func__);
712 r = SSH_ERR_ALLOC_FAIL;
713 goto out;
714 }
715 /*
716 * Bail out early if we're looking for a particular principal and this
717 * line does not list it.
718 */
719 if (required_principal != NULL) {
720 if (match_pattern_list(required_principal,
721 principals, 0) != 1) {
722 /* principal didn't match */
723 r = SSH_ERR_KEY_NOT_FOUND;
724 goto out;
725 }
726 debug("%s: %s:%lu: matched principal \"%s\"",
727 __func__, path, linenum, required_principal);
728 }
729
730 if ((key = sshkey_new(KEY_UNSPEC)) == NULL) {
731 error("%s: sshkey_new failed", __func__);
732 r = SSH_ERR_ALLOC_FAIL;
733 goto out;
734 }
735 if (sshkey_read(key, &cp) != 0) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000736 /* no key? Check for options */
737 opts = cp;
738 if (sshkey_advance_past_options(&cp) != 0) {
djm@openbsd.orge2031b02020-01-22 02:25:21 +0000739 error("%s:%lu: invalid options", path, linenum);
740 r = SSH_ERR_INVALID_FORMAT;
741 goto out;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000742 }
743 *cp++ = '\0';
744 skip_space(&cp);
djm@openbsd.orge2031b02020-01-22 02:25:21 +0000745 if (sshkey_read(key, &cp) != 0) {
746 error("%s:%lu: invalid key", path, linenum);
747 r = SSH_ERR_INVALID_FORMAT;
748 goto out;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000749 }
750 }
751 debug3("%s:%lu: options %s", path, linenum, opts == NULL ? "" : opts);
djm@openbsd.orgbab6feb2019-09-05 04:55:32 +0000752 if ((sigopts = sshsigopt_parse(opts, path, linenum, &reason)) == NULL) {
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000753 error("%s:%lu: bad options: %s", path, linenum, reason);
djm@openbsd.orge2031b02020-01-22 02:25:21 +0000754 r = SSH_ERR_INVALID_FORMAT;
755 goto out;
756 }
757 /* success */
758 if (principalsp != NULL) {
759 *principalsp = principals;
760 principals = NULL; /* transferred */
761 }
762 if (sigoptsp != NULL) {
763 *sigoptsp = sigopts;
764 sigopts = NULL; /* transferred */
765 }
766 if (keyp != NULL) {
767 *keyp = key;
768 key = NULL; /* transferred */
769 }
770 r = 0;
771 out:
772 free(principals);
773 sshsigopt_free(sigopts);
774 sshkey_free(key);
775 return r;
776}
777
778static int
779check_allowed_keys_line(const char *path, u_long linenum, char *line,
780 const struct sshkey *sign_key, const char *principal,
781 const char *sig_namespace)
782{
783 struct sshkey *found_key = NULL;
784 int r, found = 0;
785 const char *reason = NULL;
786 struct sshsigopt *sigopts = NULL;
787
788 /* Parse the line */
789 if ((r = parse_principals_key_and_options(path, linenum, line,
790 principal, NULL, &found_key, &sigopts)) != 0) {
791 /* error already logged */
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000792 goto done;
793 }
794
795 /* Check whether options preclude the use of this key */
796 if (sigopts->namespaces != NULL &&
797 match_pattern_list(sig_namespace, sigopts->namespaces, 0) != 1) {
798 error("%s:%lu: key is not permitted for use in signature "
799 "namespace \"%s\"", path, linenum, sig_namespace);
800 goto done;
801 }
802
803 if (!sigopts->ca && sshkey_equal(found_key, sign_key)) {
804 /* Exact match of key */
805 debug("%s:%lu: matched key and principal", path, linenum);
806 /* success */
807 found = 1;
808 } else if (sigopts->ca && sshkey_is_cert(sign_key) &&
809 sshkey_equal_public(sign_key->cert->signature_key, found_key)) {
810 /* Match of certificate's CA key */
811 if ((r = sshkey_cert_check_authority(sign_key, 0, 1,
812 principal, &reason)) != 0) {
813 error("%s:%lu: certificate not authorized: %s",
814 path, linenum, reason);
815 goto done;
816 }
817 debug("%s:%lu: matched certificate CA key", path, linenum);
818 /* success */
819 found = 1;
820 } else {
821 /* Principal matched but key didn't */
822 goto done;
823 }
824 done:
825 sshkey_free(found_key);
djm@openbsd.orgbab6feb2019-09-05 04:55:32 +0000826 sshsigopt_free(sigopts);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000827 return found ? 0 : SSH_ERR_KEY_NOT_FOUND;
828}
829
830int
831sshsig_check_allowed_keys(const char *path, const struct sshkey *sign_key,
832 const char *principal, const char *sig_namespace)
833{
834 FILE *f = NULL;
835 char *line = NULL;
836 size_t linesize = 0;
837 u_long linenum = 0;
838 int r, oerrno;
839
840 /* Check key and principal against file */
841 if ((f = fopen(path, "r")) == NULL) {
842 oerrno = errno;
843 error("Unable to open allowed keys file \"%s\": %s",
844 path, strerror(errno));
845 errno = oerrno;
846 return SSH_ERR_SYSTEM_ERROR;
847 }
848
849 while (getline(&line, &linesize, f) != -1) {
850 linenum++;
851 r = check_allowed_keys_line(path, linenum, line, sign_key,
852 principal, sig_namespace);
djm@openbsd.orgd637c4a2019-09-03 08:35:27 +0000853 free(line);
854 line = NULL;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000855 if (r == SSH_ERR_KEY_NOT_FOUND)
856 continue;
857 else if (r == 0) {
858 /* success */
859 fclose(f);
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000860 return 0;
djm@openbsd.org2a9c9f72019-09-03 08:34:19 +0000861 } else
862 break;
863 }
864 /* Either we hit an error parsing or we simply didn't find the key */
865 fclose(f);
866 free(line);
867 return r == 0 ? SSH_ERR_KEY_NOT_FOUND : r;
868}
djm@openbsd.org56cffcc2020-01-23 02:43:48 +0000869
870static int
djm@openbsd.org72a8bea2020-01-23 23:31:52 +0000871cert_filter_principals(const char *path, u_long linenum,
872 char **principalsp, const struct sshkey *cert)
873{
874 char *cp, *oprincipals, *principals;
875 const char *reason;
876 struct sshbuf *nprincipals;
877 int r = SSH_ERR_INTERNAL_ERROR, success = 0;
878
879 oprincipals = principals = *principalsp;
880 *principalsp = NULL;
881
882 if ((nprincipals = sshbuf_new()) == NULL)
883 return SSH_ERR_ALLOC_FAIL;
884
885 while ((cp = strsep(&principals, ",")) != NULL && *cp != '\0') {
886 if (strcspn(cp, "!?*") != strlen(cp)) {
887 debug("%s:%lu: principal \"%s\" not authorized: "
888 "contains wildcards", path, linenum, cp);
889 continue;
890 }
891 /* Check against principals list in certificate */
892 if ((r = sshkey_cert_check_authority(cert, 0, 1,
893 cp, &reason)) != 0) {
894 debug("%s:%lu: principal \"%s\" not authorized: %s",
895 path, linenum, cp, reason);
896 continue;
897 }
898 if ((r = sshbuf_putf(nprincipals, "%s%s",
899 sshbuf_len(nprincipals) != 0 ? "," : "", cp)) != 0) {
900 error("%s: buffer error", __func__);
901 goto out;
902 }
903 }
904 if (sshbuf_len(nprincipals) == 0) {
905 error("%s:%lu: no valid principals found", path, linenum);
906 r = SSH_ERR_KEY_CERT_INVALID;
907 goto out;
908 }
909 if ((principals = sshbuf_dup_string(nprincipals)) == NULL) {
910 error("%s: buffer error", __func__);
911 goto out;
912 }
913 /* success */
914 success = 1;
915 *principalsp = principals;
916 out:
917 sshbuf_free(nprincipals);
918 free(oprincipals);
919 return success ? 0 : r;
920}
921
922static int
923get_matching_principals_from_line(const char *path, u_long linenum, char *line,
djm@openbsd.org56cffcc2020-01-23 02:43:48 +0000924 const struct sshkey *sign_key, char **principalsp)
925{
926 struct sshkey *found_key = NULL;
927 char *principals = NULL;
928 int r, found = 0;
djm@openbsd.org56cffcc2020-01-23 02:43:48 +0000929 struct sshsigopt *sigopts = NULL;
930
931 if (principalsp != NULL)
932 *principalsp = NULL;
933
934 /* Parse the line */
935 if ((r = parse_principals_key_and_options(path, linenum, line,
936 NULL, &principals, &found_key, &sigopts)) != 0) {
937 /* error already logged */
938 goto done;
939 }
940
941 if (!sigopts->ca && sshkey_equal(found_key, sign_key)) {
942 /* Exact match of key */
943 debug("%s:%lu: matched key", path, linenum);
944 /* success */
945 found = 1;
946 } else if (sigopts->ca && sshkey_is_cert(sign_key) &&
947 sshkey_equal_public(sign_key->cert->signature_key, found_key)) {
djm@openbsd.org72a8bea2020-01-23 23:31:52 +0000948 /* Remove principals listed in file but not allowed by cert */
949 if ((r = cert_filter_principals(path, linenum,
950 &principals, sign_key)) != 0) {
951 /* error already displayed */
952 debug("%s:%lu: cert_filter_principals: %s",
953 path, linenum, ssh_err(r));
djm@openbsd.org56cffcc2020-01-23 02:43:48 +0000954 goto done;
955 }
956 debug("%s:%lu: matched certificate CA key", path, linenum);
957 /* success */
958 found = 1;
959 } else {
960 /* Key didn't match */
961 goto done;
962 }
963 done:
964 if (found) {
965 *principalsp = principals;
966 principals = NULL; /* transferred */
967 }
968 free(principals);
969 sshkey_free(found_key);
970 sshsigopt_free(sigopts);
971 return found ? 0 : SSH_ERR_KEY_NOT_FOUND;
972}
973
974int
djm@openbsd.org72a8bea2020-01-23 23:31:52 +0000975sshsig_find_principals(const char *path, const struct sshkey *sign_key,
976 char **principals)
djm@openbsd.org56cffcc2020-01-23 02:43:48 +0000977{
978 FILE *f = NULL;
979 char *line = NULL;
980 size_t linesize = 0;
981 u_long linenum = 0;
982 int r, oerrno;
983
984 if ((f = fopen(path, "r")) == NULL) {
985 oerrno = errno;
986 error("Unable to open allowed keys file \"%s\": %s",
987 path, strerror(errno));
988 errno = oerrno;
989 return SSH_ERR_SYSTEM_ERROR;
990 }
991
992 while (getline(&line, &linesize, f) != -1) {
993 linenum++;
djm@openbsd.org72a8bea2020-01-23 23:31:52 +0000994 r = get_matching_principals_from_line(path, linenum, line,
995 sign_key, principals);
djm@openbsd.org56cffcc2020-01-23 02:43:48 +0000996 free(line);
997 line = NULL;
998 if (r == SSH_ERR_KEY_NOT_FOUND)
999 continue;
1000 else if (r == 0) {
1001 /* success */
1002 fclose(f);
1003 return 0;
1004 } else
1005 break;
1006 }
1007 free(line);
1008 /* Either we hit an error parsing or we simply didn't find the key */
1009 if (ferror(f) != 0) {
1010 oerrno = errno;
1011 fclose(f);
1012 error("Unable to read allowed keys file \"%s\": %s",
1013 path, strerror(errno));
1014 errno = oerrno;
1015 return SSH_ERR_SYSTEM_ERROR;
1016 }
1017 fclose(f);
1018 return r == 0 ? SSH_ERR_KEY_NOT_FOUND : r;
1019}
1020
1021int
1022sshsig_get_pubkey(struct sshbuf *signature, struct sshkey **pubkey)
1023{
1024 struct sshkey *pk = NULL;
1025 int r = SSH_ERR_SIGNATURE_INVALID;
1026
1027 if (pubkey != NULL)
1028 *pubkey = NULL;
1029 if ((r = sshsig_parse_preamble(signature)) != 0)
1030 return r;
1031 if ((r = sshkey_froms(signature, &pk)) != 0)
1032 return r;
1033
1034 *pubkey = pk;
1035 pk = NULL;
1036 return 0;
1037}