1 /* $OpenBSD: sshsig.c,v 1.33 2023/09/06 23:18:15 djm Exp $ */
3 * Copyright (c) 2019 Google LLC
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.
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.
38 #define SIG_VERSION 0x01
39 #define MAGIC_PREAMBLE "SSHSIG"
40 #define MAGIC_PREAMBLE_LEN (sizeof(MAGIC_PREAMBLE) - 1)
41 #define BEGIN_SIGNATURE "-----BEGIN SSH SIGNATURE-----"
42 #define END_SIGNATURE "-----END SSH SIGNATURE-----"
43 #define RSA_SIGN_ALG "rsa-sha2-512" /* XXX maybe make configurable */
44 #define RSA_SIGN_ALLOWED "rsa-sha2-512,rsa-sha2-256"
45 #define HASHALG_DEFAULT "sha512" /* XXX maybe make configurable */
46 #define HASHALG_ALLOWED "sha256,sha512"
49 sshsig_armor(const struct sshbuf *blob, struct sshbuf **out)
51 struct sshbuf *buf = NULL;
52 int r = SSH_ERR_INTERNAL_ERROR;
56 if ((buf = sshbuf_new()) == NULL) {
57 error_f("sshbuf_new failed");
58 r = SSH_ERR_ALLOC_FAIL;
62 if ((r = sshbuf_putf(buf, "%s\n", BEGIN_SIGNATURE)) != 0) {
63 error_fr(r, "sshbuf_putf");
67 if ((r = sshbuf_dtob64(blob, buf, 1)) != 0) {
68 error_fr(r, "base64 encode signature");
72 if ((r = sshbuf_put(buf, END_SIGNATURE,
73 sizeof(END_SIGNATURE)-1)) != 0 ||
74 (r = sshbuf_put_u8(buf, '\n')) != 0) {
75 error_fr(r, "sshbuf_put");
80 buf = NULL; /* transferred */
88 sshsig_dearmor(struct sshbuf *sig, struct sshbuf **out)
92 struct sshbuf *buf = NULL;
93 struct sshbuf *sbuf = NULL;
96 if ((sbuf = sshbuf_fromb(sig)) == NULL) {
97 error_f("sshbuf_fromb failed");
98 return SSH_ERR_ALLOC_FAIL;
101 /* Expect and consume preamble + lf/crlf */
102 if ((r = sshbuf_cmp(sbuf, 0,
103 BEGIN_SIGNATURE, sizeof(BEGIN_SIGNATURE)-1)) != 0) {
104 error("Couldn't parse signature: missing header");
107 if ((r = sshbuf_consume(sbuf, sizeof(BEGIN_SIGNATURE)-1)) != 0) {
108 error_fr(r, "consume");
111 if ((r = sshbuf_cmp(sbuf, 0, "\r\n", 2)) == 0)
113 else if ((r = sshbuf_cmp(sbuf, 0, "\n", 1)) == 0)
116 r = SSH_ERR_INVALID_FORMAT;
117 error_f("no header eol");
120 if ((r = sshbuf_consume(sbuf, eoffset)) != 0) {
121 error_fr(r, "consume eol");
124 /* Find and consume lf + suffix (any prior cr would be ignored) */
125 if ((r = sshbuf_find(sbuf, 0, "\n" END_SIGNATURE,
126 sizeof(END_SIGNATURE), &eoffset)) != 0) {
127 error("Couldn't parse signature: missing footer");
130 if ((r = sshbuf_consume_end(sbuf, sshbuf_len(sbuf)-eoffset)) != 0) {
131 error_fr(r, "consume");
135 if ((b64 = sshbuf_dup_string(sbuf)) == NULL) {
136 error_f("sshbuf_dup_string failed");
137 r = SSH_ERR_ALLOC_FAIL;
141 if ((buf = sshbuf_new()) == NULL) {
142 error_f("sshbuf_new() failed");
143 r = SSH_ERR_ALLOC_FAIL;
147 if ((r = sshbuf_b64tod(buf, b64)) != 0) {
148 error_fr(r, "decode base64");
155 buf = NULL; /* transferred */
164 sshsig_wrap_sign(struct sshkey *key, const char *hashalg,
165 const char *sk_provider, const char *sk_pin, const struct sshbuf *h_message,
166 const char *sig_namespace, struct sshbuf **out,
167 sshsig_signer *signer, void *signer_ctx)
172 struct sshbuf *blob = NULL;
173 struct sshbuf *tosign = NULL;
174 const char *sign_alg = NULL;
176 if ((tosign = sshbuf_new()) == NULL ||
177 (blob = sshbuf_new()) == NULL) {
178 error_f("sshbuf_new failed");
179 r = SSH_ERR_ALLOC_FAIL;
183 if ((r = sshbuf_put(tosign, MAGIC_PREAMBLE, MAGIC_PREAMBLE_LEN)) != 0 ||
184 (r = sshbuf_put_cstring(tosign, sig_namespace)) != 0 ||
185 (r = sshbuf_put_string(tosign, NULL, 0)) != 0 || /* reserved */
186 (r = sshbuf_put_cstring(tosign, hashalg)) != 0 ||
187 (r = sshbuf_put_stringb(tosign, h_message)) != 0) {
188 error_fr(r, "assemble message to sign");
192 /* If using RSA keys then default to a good signature algorithm */
193 if (sshkey_type_plain(key->type) == KEY_RSA)
194 sign_alg = RSA_SIGN_ALG;
196 if (signer != NULL) {
197 if ((r = signer(key, &sig, &slen,
198 sshbuf_ptr(tosign), sshbuf_len(tosign),
199 sign_alg, sk_provider, sk_pin, 0, signer_ctx)) != 0) {
200 error_r(r, "Couldn't sign message (signer)");
204 if ((r = sshkey_sign(key, &sig, &slen,
205 sshbuf_ptr(tosign), sshbuf_len(tosign),
206 sign_alg, sk_provider, sk_pin, 0)) != 0) {
207 error_r(r, "Couldn't sign message");
212 if ((r = sshbuf_put(blob, MAGIC_PREAMBLE, MAGIC_PREAMBLE_LEN)) != 0 ||
213 (r = sshbuf_put_u32(blob, SIG_VERSION)) != 0 ||
214 (r = sshkey_puts(key, blob)) != 0 ||
215 (r = sshbuf_put_cstring(blob, sig_namespace)) != 0 ||
216 (r = sshbuf_put_string(blob, NULL, 0)) != 0 || /* reserved */
217 (r = sshbuf_put_cstring(blob, hashalg)) != 0 ||
218 (r = sshbuf_put_string(blob, sig, slen)) != 0) {
219 error_fr(r, "assemble signature object");
235 /* Check preamble and version. */
237 sshsig_parse_preamble(struct sshbuf *buf)
239 int r = SSH_ERR_INTERNAL_ERROR;
242 if ((r = sshbuf_cmp(buf, 0, MAGIC_PREAMBLE, MAGIC_PREAMBLE_LEN)) != 0 ||
243 (r = sshbuf_consume(buf, (sizeof(MAGIC_PREAMBLE)-1))) != 0 ||
244 (r = sshbuf_get_u32(buf, &sversion)) != 0) {
245 error("Couldn't verify signature: invalid format");
249 if (sversion > SIG_VERSION) {
250 error("Signature version %lu is larger than supported "
251 "version %u", (unsigned long)sversion, SIG_VERSION);
252 return SSH_ERR_INVALID_FORMAT;
258 sshsig_check_hashalg(const char *hashalg)
260 if (hashalg == NULL ||
261 match_pattern_list(hashalg, HASHALG_ALLOWED, 0) == 1)
263 error_f("unsupported hash algorithm \"%.100s\"", hashalg);
264 return SSH_ERR_SIGN_ALG_UNSUPPORTED;
268 sshsig_peek_hashalg(struct sshbuf *signature, char **hashalgp)
270 struct sshbuf *buf = NULL;
271 char *hashalg = NULL;
272 int r = SSH_ERR_INTERNAL_ERROR;
274 if (hashalgp != NULL)
276 if ((buf = sshbuf_fromb(signature)) == NULL)
277 return SSH_ERR_ALLOC_FAIL;
278 if ((r = sshsig_parse_preamble(buf)) != 0)
280 if ((r = sshbuf_get_string_direct(buf, NULL, NULL)) != 0 ||
281 (r = sshbuf_get_string_direct(buf, NULL, NULL)) != 0 ||
282 (r = sshbuf_get_string(buf, NULL, NULL)) != 0 ||
283 (r = sshbuf_get_cstring(buf, &hashalg, NULL)) != 0 ||
284 (r = sshbuf_get_string_direct(buf, NULL, NULL)) != 0) {
285 error_fr(r, "parse signature object");
300 sshsig_wrap_verify(struct sshbuf *signature, const char *hashalg,
301 const struct sshbuf *h_message, const char *expect_namespace,
302 struct sshkey **sign_keyp, struct sshkey_sig_details **sig_details)
304 int r = SSH_ERR_INTERNAL_ERROR;
305 struct sshbuf *buf = NULL, *toverify = NULL;
306 struct sshkey *key = NULL;
308 char *got_namespace = NULL, *sigtype = NULL, *sig_hashalg = NULL;
311 debug_f("verify message length %zu", sshbuf_len(h_message));
312 if (sig_details != NULL)
314 if (sign_keyp != NULL)
317 if ((toverify = sshbuf_new()) == NULL) {
318 error_f("sshbuf_new failed");
319 r = SSH_ERR_ALLOC_FAIL;
322 if ((r = sshbuf_put(toverify, MAGIC_PREAMBLE,
323 MAGIC_PREAMBLE_LEN)) != 0 ||
324 (r = sshbuf_put_cstring(toverify, expect_namespace)) != 0 ||
325 (r = sshbuf_put_string(toverify, NULL, 0)) != 0 || /* reserved */
326 (r = sshbuf_put_cstring(toverify, hashalg)) != 0 ||
327 (r = sshbuf_put_stringb(toverify, h_message)) != 0) {
328 error_fr(r, "assemble message to verify");
332 if ((r = sshsig_parse_preamble(signature)) != 0)
335 if ((r = sshkey_froms(signature, &key)) != 0 ||
336 (r = sshbuf_get_cstring(signature, &got_namespace, NULL)) != 0 ||
337 (r = sshbuf_get_string(signature, NULL, NULL)) != 0 ||
338 (r = sshbuf_get_cstring(signature, &sig_hashalg, NULL)) != 0 ||
339 (r = sshbuf_get_string_direct(signature, &sig, &siglen)) != 0) {
340 error_fr(r, "parse signature object");
344 if (sshbuf_len(signature) != 0) {
345 error("Signature contains trailing data");
346 r = SSH_ERR_INVALID_FORMAT;
350 if (strcmp(expect_namespace, got_namespace) != 0) {
351 error("Couldn't verify signature: namespace does not match");
352 debug_f("expected namespace \"%s\" received \"%s\"",
353 expect_namespace, got_namespace);
354 r = SSH_ERR_SIGNATURE_INVALID;
357 if (strcmp(hashalg, sig_hashalg) != 0) {
358 error("Couldn't verify signature: hash algorithm mismatch");
359 debug_f("expected algorithm \"%s\" received \"%s\"",
360 hashalg, sig_hashalg);
361 r = SSH_ERR_SIGNATURE_INVALID;
364 /* Ensure that RSA keys use an acceptable signature algorithm */
365 if (sshkey_type_plain(key->type) == KEY_RSA) {
366 if ((r = sshkey_get_sigtype(sig, siglen, &sigtype)) != 0) {
367 error_r(r, "Couldn't verify signature: unable to get "
371 if (match_pattern_list(sigtype, RSA_SIGN_ALLOWED, 0) != 1) {
372 error("Couldn't verify signature: unsupported RSA "
373 "signature algorithm %s", sigtype);
374 r = SSH_ERR_SIGN_ALG_UNSUPPORTED;
378 if ((r = sshkey_verify(key, sig, siglen, sshbuf_ptr(toverify),
379 sshbuf_len(toverify), NULL, 0, sig_details)) != 0) {
380 error_r(r, "Signature verification failed");
386 if (sign_keyp != NULL) {
388 key = NULL; /* transferred */
395 sshbuf_free(toverify);
401 hash_buffer(const struct sshbuf *m, const char *hashalg, struct sshbuf **bp)
403 char *hex, hash[SSH_DIGEST_MAX_LENGTH];
404 int alg, r = SSH_ERR_INTERNAL_ERROR;
405 struct sshbuf *b = NULL;
408 memset(hash, 0, sizeof(hash));
410 if ((r = sshsig_check_hashalg(hashalg)) != 0)
412 if ((alg = ssh_digest_alg_by_name(hashalg)) == -1) {
413 error_f("can't look up hash algorithm %s", hashalg);
414 return SSH_ERR_INTERNAL_ERROR;
416 if ((r = ssh_digest_buffer(alg, m, hash, sizeof(hash))) != 0) {
417 error_fr(r, "ssh_digest_buffer");
420 if ((hex = tohex(hash, ssh_digest_bytes(alg))) != NULL) {
421 debug3_f("final hash: %s", hex);
422 freezero(hex, strlen(hex));
424 if ((b = sshbuf_new()) == NULL) {
425 r = SSH_ERR_ALLOC_FAIL;
428 if ((r = sshbuf_put(b, hash, ssh_digest_bytes(alg))) != 0) {
429 error_fr(r, "sshbuf_put");
433 b = NULL; /* transferred */
438 explicit_bzero(hash, sizeof(hash));
443 sshsig_signb(struct sshkey *key, const char *hashalg,
444 const char *sk_provider, const char *sk_pin,
445 const struct sshbuf *message, const char *sig_namespace,
446 struct sshbuf **out, sshsig_signer *signer, void *signer_ctx)
448 struct sshbuf *b = NULL;
449 int r = SSH_ERR_INTERNAL_ERROR;
452 hashalg = HASHALG_DEFAULT;
455 if ((r = hash_buffer(message, hashalg, &b)) != 0) {
456 error_fr(r, "hash buffer");
459 if ((r = sshsig_wrap_sign(key, hashalg, sk_provider, sk_pin, b,
460 sig_namespace, out, signer, signer_ctx)) != 0)
470 sshsig_verifyb(struct sshbuf *signature, const struct sshbuf *message,
471 const char *expect_namespace, struct sshkey **sign_keyp,
472 struct sshkey_sig_details **sig_details)
474 struct sshbuf *b = NULL;
475 int r = SSH_ERR_INTERNAL_ERROR;
476 char *hashalg = NULL;
478 if (sig_details != NULL)
480 if (sign_keyp != NULL)
482 if ((r = sshsig_peek_hashalg(signature, &hashalg)) != 0)
484 debug_f("signature made with hash \"%s\"", hashalg);
485 if ((r = hash_buffer(message, hashalg, &b)) != 0) {
486 error_fr(r, "hash buffer");
489 if ((r = sshsig_wrap_verify(signature, hashalg, b, expect_namespace,
490 sign_keyp, sig_details)) != 0)
501 hash_file(int fd, const char *hashalg, struct sshbuf **bp)
503 char *hex, rbuf[8192], hash[SSH_DIGEST_MAX_LENGTH];
504 ssize_t n, total = 0;
505 struct ssh_digest_ctx *ctx = NULL;
506 int alg, oerrno, r = SSH_ERR_INTERNAL_ERROR;
507 struct sshbuf *b = NULL;
510 memset(hash, 0, sizeof(hash));
512 if ((r = sshsig_check_hashalg(hashalg)) != 0)
514 if ((alg = ssh_digest_alg_by_name(hashalg)) == -1) {
515 error_f("can't look up hash algorithm %s", hashalg);
516 return SSH_ERR_INTERNAL_ERROR;
518 if ((ctx = ssh_digest_start(alg)) == NULL) {
519 error_f("ssh_digest_start failed");
520 return SSH_ERR_INTERNAL_ERROR;
523 if ((n = read(fd, rbuf, sizeof(rbuf))) == -1) {
524 if (errno == EINTR || errno == EAGAIN)
527 error_f("read: %s", strerror(errno));
529 r = SSH_ERR_SYSTEM_ERROR;
532 debug2_f("hashed %zu bytes", total);
536 if ((r = ssh_digest_update(ctx, rbuf, (size_t)n)) != 0) {
537 error_fr(r, "ssh_digest_update");
541 if ((r = ssh_digest_final(ctx, hash, sizeof(hash))) != 0) {
542 error_fr(r, "ssh_digest_final");
545 if ((hex = tohex(hash, ssh_digest_bytes(alg))) != NULL) {
546 debug3_f("final hash: %s", hex);
547 freezero(hex, strlen(hex));
549 if ((b = sshbuf_new()) == NULL) {
550 r = SSH_ERR_ALLOC_FAIL;
553 if ((r = sshbuf_put(b, hash, ssh_digest_bytes(alg))) != 0) {
554 error_fr(r, "sshbuf_put");
558 b = NULL; /* transferred */
564 ssh_digest_free(ctx);
565 explicit_bzero(hash, sizeof(hash));
571 sshsig_sign_fd(struct sshkey *key, const char *hashalg,
572 const char *sk_provider, const char *sk_pin,
573 int fd, const char *sig_namespace, struct sshbuf **out,
574 sshsig_signer *signer, void *signer_ctx)
576 struct sshbuf *b = NULL;
577 int r = SSH_ERR_INTERNAL_ERROR;
580 hashalg = HASHALG_DEFAULT;
583 if ((r = hash_file(fd, hashalg, &b)) != 0) {
584 error_fr(r, "hash_file");
587 if ((r = sshsig_wrap_sign(key, hashalg, sk_provider, sk_pin, b,
588 sig_namespace, out, signer, signer_ctx)) != 0)
598 sshsig_verify_fd(struct sshbuf *signature, int fd,
599 const char *expect_namespace, struct sshkey **sign_keyp,
600 struct sshkey_sig_details **sig_details)
602 struct sshbuf *b = NULL;
603 int r = SSH_ERR_INTERNAL_ERROR;
604 char *hashalg = NULL;
606 if (sig_details != NULL)
608 if (sign_keyp != NULL)
610 if ((r = sshsig_peek_hashalg(signature, &hashalg)) != 0)
612 debug_f("signature made with hash \"%s\"", hashalg);
613 if ((r = hash_file(fd, hashalg, &b)) != 0) {
614 error_fr(r, "hash_file");
617 if ((r = sshsig_wrap_verify(signature, hashalg, b, expect_namespace,
618 sign_keyp, sig_details)) != 0)
631 uint64_t valid_after, valid_before;
635 sshsigopt_parse(const char *opts, const char *path, u_long linenum,
636 const char **errstrp)
638 struct sshsigopt *ret;
641 const char *errstr = NULL;
643 if ((ret = calloc(1, sizeof(*ret))) == NULL)
645 if (opts == NULL || *opts == '\0')
646 return ret; /* Empty options yields empty options :) */
648 while (*opts && *opts != ' ' && *opts != '\t') {
650 if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
652 } else if (opt_match(&opts, "namespaces")) {
653 if (ret->namespaces != NULL) {
654 errstr = "multiple \"namespaces\" clauses";
657 ret->namespaces = opt_dequote(&opts, &errstr);
658 if (ret->namespaces == NULL)
660 } else if (opt_match(&opts, "valid-after")) {
661 if (ret->valid_after != 0) {
662 errstr = "multiple \"valid-after\" clauses";
665 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
667 if (parse_absolute_time(opt, &ret->valid_after) != 0 ||
668 ret->valid_after == 0) {
670 errstr = "invalid \"valid-after\" time";
674 } else if (opt_match(&opts, "valid-before")) {
675 if (ret->valid_before != 0) {
676 errstr = "multiple \"valid-before\" clauses";
679 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
681 if (parse_absolute_time(opt, &ret->valid_before) != 0 ||
682 ret->valid_before == 0) {
684 errstr = "invalid \"valid-before\" time";
690 * Skip the comma, and move to the next option
691 * (or break out if there are no more).
693 if (*opts == '\0' || *opts == ' ' || *opts == '\t')
694 break; /* End of options. */
695 /* Anything other than a comma is an unknown option */
697 errstr = "unknown key option";
702 errstr = "unexpected end-of-options";
706 /* final consistency check */
707 if (ret->valid_after != 0 && ret->valid_before != 0 &&
708 ret->valid_before <= ret->valid_after) {
709 errstr = "\"valid-before\" time is before \"valid-after\"";
722 sshsigopt_free(struct sshsigopt *opts)
726 free(opts->namespaces);
731 parse_principals_key_and_options(const char *path, u_long linenum, char *line,
732 const char *required_principal, char **principalsp, struct sshkey **keyp,
733 struct sshsigopt **sigoptsp)
735 char *opts = NULL, *tmp, *cp, *principals = NULL;
736 const char *reason = NULL;
737 struct sshsigopt *sigopts = NULL;
738 struct sshkey *key = NULL;
739 int r = SSH_ERR_INTERNAL_ERROR;
741 if (principalsp != NULL)
743 if (sigoptsp != NULL)
749 cp = cp + strspn(cp, " \t"); /* skip leading whitespace */
750 if (*cp == '#' || *cp == '\0')
751 return SSH_ERR_KEY_NOT_FOUND; /* blank or all-comment line */
753 /* format: identity[,identity...] [option[,option...]] key */
754 if ((tmp = strdelimw(&cp)) == NULL || cp == NULL) {
755 error("%s:%lu: invalid line", path, linenum);
756 r = SSH_ERR_INVALID_FORMAT;
759 if ((principals = strdup(tmp)) == NULL) {
760 error_f("strdup failed");
761 r = SSH_ERR_ALLOC_FAIL;
765 * Bail out early if we're looking for a particular principal and this
766 * line does not list it.
768 if (required_principal != NULL) {
769 if (match_pattern_list(required_principal,
770 principals, 0) != 1) {
771 /* principal didn't match */
772 r = SSH_ERR_KEY_NOT_FOUND;
775 debug_f("%s:%lu: matched principal \"%s\"",
776 path, linenum, required_principal);
779 if ((key = sshkey_new(KEY_UNSPEC)) == NULL) {
780 error_f("sshkey_new failed");
781 r = SSH_ERR_ALLOC_FAIL;
784 if (sshkey_read(key, &cp) != 0) {
785 /* no key? Check for options */
787 if (sshkey_advance_past_options(&cp) != 0) {
788 error("%s:%lu: invalid options", path, linenum);
789 r = SSH_ERR_INVALID_FORMAT;
792 if (cp == NULL || *cp == '\0') {
793 error("%s:%lu: missing key", path, linenum);
794 r = SSH_ERR_INVALID_FORMAT;
799 if (sshkey_read(key, &cp) != 0) {
800 error("%s:%lu: invalid key", path, linenum);
801 r = SSH_ERR_INVALID_FORMAT;
805 debug3("%s:%lu: options %s", path, linenum, opts == NULL ? "" : opts);
806 if ((sigopts = sshsigopt_parse(opts, path, linenum, &reason)) == NULL) {
807 error("%s:%lu: bad options: %s", path, linenum, reason);
808 r = SSH_ERR_INVALID_FORMAT;
812 if (principalsp != NULL) {
813 *principalsp = principals;
814 principals = NULL; /* transferred */
816 if (sigoptsp != NULL) {
818 sigopts = NULL; /* transferred */
822 key = NULL; /* transferred */
827 sshsigopt_free(sigopts);
833 cert_filter_principals(const char *path, u_long linenum,
834 char **principalsp, const struct sshkey *cert, uint64_t verify_time)
836 char *cp, *oprincipals, *principals;
838 struct sshbuf *nprincipals;
839 int r = SSH_ERR_INTERNAL_ERROR, success = 0;
842 oprincipals = principals = *principalsp;
845 if ((nprincipals = sshbuf_new()) == NULL) {
846 r = SSH_ERR_ALLOC_FAIL;
850 while ((cp = strsep(&principals, ",")) != NULL && *cp != '\0') {
851 /* Check certificate validity */
852 if ((r = sshkey_cert_check_authority(cert, 0, 1, 0,
853 verify_time, NULL, &reason)) != 0) {
854 debug("%s:%lu: principal \"%s\" not authorized: %s",
855 path, linenum, cp, reason);
858 /* Return all matching principal names from the cert */
859 for (i = 0; i < cert->cert->nprincipals; i++) {
860 if (match_pattern(cert->cert->principals[i], cp)) {
861 if ((r = sshbuf_putf(nprincipals, "%s%s",
862 sshbuf_len(nprincipals) != 0 ? "," : "",
863 cert->cert->principals[i])) != 0) {
864 error_f("buffer error");
870 if (sshbuf_len(nprincipals) == 0) {
871 error("%s:%lu: no valid principals found", path, linenum);
872 r = SSH_ERR_KEY_CERT_INVALID;
875 if ((principals = sshbuf_dup_string(nprincipals)) == NULL) {
876 error_f("buffer error");
881 *principalsp = principals;
883 sshbuf_free(nprincipals);
885 return success ? 0 : r;
889 check_allowed_keys_line(const char *path, u_long linenum, char *line,
890 const struct sshkey *sign_key, const char *principal,
891 const char *sig_namespace, uint64_t verify_time, char **principalsp)
893 struct sshkey *found_key = NULL;
894 char *principals = NULL;
896 const char *reason = NULL;
897 struct sshsigopt *sigopts = NULL;
898 char tvalid[64], tverify[64];
900 if (principalsp != NULL)
904 if ((r = parse_principals_key_and_options(path, linenum, line,
905 principal, &principals, &found_key, &sigopts)) != 0) {
906 /* error already logged */
910 if (!sigopts->ca && sshkey_equal(found_key, sign_key)) {
911 /* Exact match of key */
912 debug("%s:%lu: matched key", path, linenum);
913 } else if (sigopts->ca && sshkey_is_cert(sign_key) &&
914 sshkey_equal_public(sign_key->cert->signature_key, found_key)) {
916 /* Match certificate CA key with specified principal */
917 if ((r = sshkey_cert_check_authority(sign_key, 0, 1, 0,
918 verify_time, principal, &reason)) != 0) {
919 error("%s:%lu: certificate not authorized: %s",
920 path, linenum, reason);
923 debug("%s:%lu: matched certificate CA key",
926 /* No principal specified - find all matching ones */
927 if ((r = cert_filter_principals(path, linenum,
928 &principals, sign_key, verify_time)) != 0) {
929 /* error already displayed */
930 debug_r(r, "%s:%lu: cert_filter_principals",
934 debug("%s:%lu: matched certificate CA key",
938 /* Didn't match key */
942 /* Check whether options preclude the use of this key */
943 if (sigopts->namespaces != NULL && sig_namespace != NULL &&
944 match_pattern_list(sig_namespace, sigopts->namespaces, 0) != 1) {
945 error("%s:%lu: key is not permitted for use in signature "
946 "namespace \"%s\"", path, linenum, sig_namespace);
950 /* check key time validity */
951 format_absolute_time((uint64_t)verify_time, tverify, sizeof(tverify));
952 if (sigopts->valid_after != 0 &&
953 (uint64_t)verify_time < sigopts->valid_after) {
954 format_absolute_time(sigopts->valid_after,
955 tvalid, sizeof(tvalid));
956 error("%s:%lu: key is not yet valid: "
957 "verify time %s < valid-after %s", path, linenum,
961 if (sigopts->valid_before != 0 &&
962 (uint64_t)verify_time > sigopts->valid_before) {
963 format_absolute_time(sigopts->valid_before,
964 tvalid, sizeof(tvalid));
965 error("%s:%lu: key has expired: "
966 "verify time %s > valid-before %s", path, linenum,
973 if (success && principalsp != NULL) {
974 *principalsp = principals;
975 principals = NULL; /* transferred */
978 sshkey_free(found_key);
979 sshsigopt_free(sigopts);
980 return success ? 0 : SSH_ERR_KEY_NOT_FOUND;
984 sshsig_check_allowed_keys(const char *path, const struct sshkey *sign_key,
985 const char *principal, const char *sig_namespace, uint64_t verify_time)
991 int r = SSH_ERR_KEY_NOT_FOUND, oerrno;
993 /* Check key and principal against file */
994 if ((f = fopen(path, "r")) == NULL) {
996 error("Unable to open allowed keys file \"%s\": %s",
997 path, strerror(errno));
999 return SSH_ERR_SYSTEM_ERROR;
1002 while (getline(&line, &linesize, f) != -1) {
1004 r = check_allowed_keys_line(path, linenum, line, sign_key,
1005 principal, sig_namespace, verify_time, NULL);
1009 if (r == SSH_ERR_KEY_NOT_FOUND)
1018 /* Either we hit an error parsing or we simply didn't find the key */
1025 sshsig_find_principals(const char *path, const struct sshkey *sign_key,
1026 uint64_t verify_time, char **principals)
1030 size_t linesize = 0;
1032 int r = SSH_ERR_KEY_NOT_FOUND, oerrno;
1034 if ((f = fopen(path, "r")) == NULL) {
1036 error("Unable to open allowed keys file \"%s\": %s",
1037 path, strerror(errno));
1039 return SSH_ERR_SYSTEM_ERROR;
1042 while (getline(&line, &linesize, f) != -1) {
1044 r = check_allowed_keys_line(path, linenum, line,
1045 sign_key, NULL, NULL, verify_time, principals);
1049 if (r == SSH_ERR_KEY_NOT_FOUND)
1059 /* Either we hit an error parsing or we simply didn't find the key */
1060 if (ferror(f) != 0) {
1063 error("Unable to read allowed keys file \"%s\": %s",
1064 path, strerror(errno));
1066 return SSH_ERR_SYSTEM_ERROR;
1073 sshsig_match_principals(const char *path, const char *principal,
1074 char ***principalsp, size_t *nprincipalsp)
1077 char *found, *line = NULL, **principals = NULL, **tmp;
1078 size_t i, nprincipals = 0, linesize = 0;
1080 int oerrno = 0, r, ret = 0;
1082 if (principalsp != NULL)
1083 *principalsp = NULL;
1084 if (nprincipalsp != NULL)
1087 /* Check key and principal against file */
1088 if ((f = fopen(path, "r")) == NULL) {
1090 error("Unable to open allowed keys file \"%s\": %s",
1091 path, strerror(errno));
1093 return SSH_ERR_SYSTEM_ERROR;
1096 while (getline(&line, &linesize, f) != -1) {
1098 /* Parse the line */
1099 if ((r = parse_principals_key_and_options(path, linenum, line,
1100 principal, &found, NULL, NULL)) != 0) {
1101 if (r == SSH_ERR_KEY_NOT_FOUND)
1105 break; /* unexpected error */
1107 if ((tmp = recallocarray(principals, nprincipals,
1108 nprincipals + 1, sizeof(*principals))) == NULL) {
1109 ret = SSH_ERR_ALLOC_FAIL;
1114 principals[nprincipals++] = found; /* transferred */
1122 if (nprincipals == 0)
1123 ret = SSH_ERR_KEY_NOT_FOUND;
1124 if (principalsp != NULL) {
1125 *principalsp = principals;
1126 principals = NULL; /* transferred */
1128 if (nprincipalsp != 0) {
1129 *nprincipalsp = nprincipals;
1134 for (i = 0; i < nprincipals; i++)
1135 free(principals[i]);
1143 sshsig_get_pubkey(struct sshbuf *signature, struct sshkey **pubkey)
1145 struct sshkey *pk = NULL;
1146 int r = SSH_ERR_SIGNATURE_INVALID;
1149 return SSH_ERR_INTERNAL_ERROR;
1150 if ((r = sshsig_parse_preamble(signature)) != 0)
1152 if ((r = sshkey_froms(signature, &pk)) != 0)