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