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