kernel - CAM cleanup 3/N - Remove unnecessary mplocks
[dragonfly.git] / crypto / openssh / authfile.c
blobf46b4e37f938aa1071de4ceecbefc3526f40c76a
1 /* $OpenBSD: authfile.c,v 1.121 2016/04/09 12:39:30 djm Exp $ */
2 /*
3 * Copyright (c) 2000, 2013 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "includes.h"
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/uio.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <limits.h>
41 #include "cipher.h"
42 #include "ssh.h"
43 #include "log.h"
44 #include "authfile.h"
45 #include "rsa.h"
46 #include "misc.h"
47 #include "atomicio.h"
48 #include "sshkey.h"
49 #include "sshbuf.h"
50 #include "ssherr.h"
51 #include "krl.h"
53 #define MAX_KEY_FILE_SIZE (1024 * 1024)
55 /* Save a key blob to a file */
56 static int
57 sshkey_save_private_blob(struct sshbuf *keybuf, const char *filename)
59 int fd, oerrno;
61 if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0)
62 return SSH_ERR_SYSTEM_ERROR;
63 if (atomicio(vwrite, fd, (u_char *)sshbuf_ptr(keybuf),
64 sshbuf_len(keybuf)) != sshbuf_len(keybuf)) {
65 oerrno = errno;
66 close(fd);
67 unlink(filename);
68 errno = oerrno;
69 return SSH_ERR_SYSTEM_ERROR;
71 close(fd);
72 return 0;
75 int
76 sshkey_save_private(struct sshkey *key, const char *filename,
77 const char *passphrase, const char *comment,
78 int force_new_format, const char *new_format_cipher, int new_format_rounds)
80 struct sshbuf *keyblob = NULL;
81 int r;
83 if ((keyblob = sshbuf_new()) == NULL)
84 return SSH_ERR_ALLOC_FAIL;
85 if ((r = sshkey_private_to_fileblob(key, keyblob, passphrase, comment,
86 force_new_format, new_format_cipher, new_format_rounds)) != 0)
87 goto out;
88 if ((r = sshkey_save_private_blob(keyblob, filename)) != 0)
89 goto out;
90 r = 0;
91 out:
92 sshbuf_free(keyblob);
93 return r;
96 /* Load a key from a fd into a buffer */
97 int
98 sshkey_load_file(int fd, struct sshbuf *blob)
100 u_char buf[1024];
101 size_t len;
102 struct stat st;
103 int r;
105 if (fstat(fd, &st) < 0)
106 return SSH_ERR_SYSTEM_ERROR;
107 if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
108 st.st_size > MAX_KEY_FILE_SIZE)
109 return SSH_ERR_INVALID_FORMAT;
110 for (;;) {
111 if ((len = atomicio(read, fd, buf, sizeof(buf))) == 0) {
112 if (errno == EPIPE)
113 break;
114 r = SSH_ERR_SYSTEM_ERROR;
115 goto out;
117 if ((r = sshbuf_put(blob, buf, len)) != 0)
118 goto out;
119 if (sshbuf_len(blob) > MAX_KEY_FILE_SIZE) {
120 r = SSH_ERR_INVALID_FORMAT;
121 goto out;
124 if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
125 st.st_size != (off_t)sshbuf_len(blob)) {
126 r = SSH_ERR_FILE_CHANGED;
127 goto out;
129 r = 0;
131 out:
132 explicit_bzero(buf, sizeof(buf));
133 if (r != 0)
134 sshbuf_reset(blob);
135 return r;
138 #ifdef WITH_SSH1
140 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
141 * encountered (the file does not exist or is not readable), and the key
142 * otherwise.
144 static int
145 sshkey_load_public_rsa1(int fd, struct sshkey **keyp, char **commentp)
147 struct sshbuf *b = NULL;
148 int r;
150 if (keyp != NULL)
151 *keyp = NULL;
152 if (commentp != NULL)
153 *commentp = NULL;
155 if ((b = sshbuf_new()) == NULL)
156 return SSH_ERR_ALLOC_FAIL;
157 if ((r = sshkey_load_file(fd, b)) != 0)
158 goto out;
159 if ((r = sshkey_parse_public_rsa1_fileblob(b, keyp, commentp)) != 0)
160 goto out;
161 r = 0;
162 out:
163 sshbuf_free(b);
164 return r;
166 #endif /* WITH_SSH1 */
168 /* XXX remove error() calls from here? */
170 sshkey_perm_ok(int fd, const char *filename)
172 struct stat st;
174 if (fstat(fd, &st) < 0)
175 return SSH_ERR_SYSTEM_ERROR;
177 * if a key owned by the user is accessed, then we check the
178 * permissions of the file. if the key owned by a different user,
179 * then we don't care.
181 #ifdef HAVE_CYGWIN
182 if (check_ntsec(filename))
183 #endif
184 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
185 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
186 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
187 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
188 error("Permissions 0%3.3o for '%s' are too open.",
189 (u_int)st.st_mode & 0777, filename);
190 error("It is required that your private key files are NOT accessible by others.");
191 error("This private key will be ignored.");
192 return SSH_ERR_KEY_BAD_PERMISSIONS;
194 return 0;
197 /* XXX kill perm_ok now that we have SSH_ERR_KEY_BAD_PERMISSIONS? */
199 sshkey_load_private_type(int type, const char *filename, const char *passphrase,
200 struct sshkey **keyp, char **commentp, int *perm_ok)
202 int fd, r;
204 if (keyp != NULL)
205 *keyp = NULL;
206 if (commentp != NULL)
207 *commentp = NULL;
209 if ((fd = open(filename, O_RDONLY)) < 0) {
210 if (perm_ok != NULL)
211 *perm_ok = 0;
212 return SSH_ERR_SYSTEM_ERROR;
214 if (sshkey_perm_ok(fd, filename) != 0) {
215 if (perm_ok != NULL)
216 *perm_ok = 0;
217 r = SSH_ERR_KEY_BAD_PERMISSIONS;
218 goto out;
220 if (perm_ok != NULL)
221 *perm_ok = 1;
223 r = sshkey_load_private_type_fd(fd, type, passphrase, keyp, commentp);
224 out:
225 close(fd);
226 return r;
230 sshkey_load_private_type_fd(int fd, int type, const char *passphrase,
231 struct sshkey **keyp, char **commentp)
233 struct sshbuf *buffer = NULL;
234 int r;
236 if (keyp != NULL)
237 *keyp = NULL;
238 if ((buffer = sshbuf_new()) == NULL) {
239 r = SSH_ERR_ALLOC_FAIL;
240 goto out;
242 if ((r = sshkey_load_file(fd, buffer)) != 0 ||
243 (r = sshkey_parse_private_fileblob_type(buffer, type,
244 passphrase, keyp, commentp)) != 0)
245 goto out;
247 /* success */
248 r = 0;
249 out:
250 sshbuf_free(buffer);
251 return r;
254 /* XXX this is almost identical to sshkey_load_private_type() */
256 sshkey_load_private(const char *filename, const char *passphrase,
257 struct sshkey **keyp, char **commentp)
259 struct sshbuf *buffer = NULL;
260 int r, fd;
262 if (keyp != NULL)
263 *keyp = NULL;
264 if (commentp != NULL)
265 *commentp = NULL;
267 if ((fd = open(filename, O_RDONLY)) < 0)
268 return SSH_ERR_SYSTEM_ERROR;
269 if (sshkey_perm_ok(fd, filename) != 0) {
270 r = SSH_ERR_KEY_BAD_PERMISSIONS;
271 goto out;
274 if ((buffer = sshbuf_new()) == NULL) {
275 r = SSH_ERR_ALLOC_FAIL;
276 goto out;
278 if ((r = sshkey_load_file(fd, buffer)) != 0 ||
279 (r = sshkey_parse_private_fileblob(buffer, passphrase, keyp,
280 commentp)) != 0)
281 goto out;
282 r = 0;
283 out:
284 close(fd);
285 sshbuf_free(buffer);
286 return r;
289 static int
290 sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp)
292 FILE *f;
293 char line[SSH_MAX_PUBKEY_BYTES];
294 char *cp;
295 u_long linenum = 0;
296 int r;
298 if (commentp != NULL)
299 *commentp = NULL;
300 if ((f = fopen(filename, "r")) == NULL)
301 return SSH_ERR_SYSTEM_ERROR;
302 while (read_keyfile_line(f, filename, line, sizeof(line),
303 &linenum) != -1) {
304 cp = line;
305 switch (*cp) {
306 case '#':
307 case '\n':
308 case '\0':
309 continue;
311 /* Abort loading if this looks like a private key */
312 if (strncmp(cp, "-----BEGIN", 10) == 0 ||
313 strcmp(cp, "SSH PRIVATE KEY FILE") == 0)
314 break;
315 /* Skip leading whitespace. */
316 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
318 if (*cp) {
319 if ((r = sshkey_read(k, &cp)) == 0) {
320 cp[strcspn(cp, "\r\n")] = '\0';
321 if (commentp) {
322 *commentp = strdup(*cp ?
323 cp : filename);
324 if (*commentp == NULL)
325 r = SSH_ERR_ALLOC_FAIL;
327 fclose(f);
328 return r;
332 fclose(f);
333 return SSH_ERR_INVALID_FORMAT;
336 /* load public key from ssh v1 private or any pubkey file */
338 sshkey_load_public(const char *filename, struct sshkey **keyp, char **commentp)
340 struct sshkey *pub = NULL;
341 char file[PATH_MAX];
342 int r, fd;
344 if (keyp != NULL)
345 *keyp = NULL;
346 if (commentp != NULL)
347 *commentp = NULL;
349 /* XXX should load file once and attempt to parse each format */
351 if ((fd = open(filename, O_RDONLY)) < 0)
352 goto skip;
353 #ifdef WITH_SSH1
354 /* try rsa1 private key */
355 r = sshkey_load_public_rsa1(fd, keyp, commentp);
356 close(fd);
357 switch (r) {
358 case SSH_ERR_INTERNAL_ERROR:
359 case SSH_ERR_ALLOC_FAIL:
360 case SSH_ERR_INVALID_ARGUMENT:
361 case SSH_ERR_SYSTEM_ERROR:
362 case 0:
363 return r;
365 #else /* WITH_SSH1 */
366 close(fd);
367 #endif /* WITH_SSH1 */
369 /* try ssh2 public key */
370 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
371 return SSH_ERR_ALLOC_FAIL;
372 if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
373 if (keyp != NULL)
374 *keyp = pub;
375 return 0;
377 sshkey_free(pub);
379 #ifdef WITH_SSH1
380 /* try rsa1 public key */
381 if ((pub = sshkey_new(KEY_RSA1)) == NULL)
382 return SSH_ERR_ALLOC_FAIL;
383 if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
384 if (keyp != NULL)
385 *keyp = pub;
386 return 0;
388 sshkey_free(pub);
389 #endif /* WITH_SSH1 */
391 skip:
392 /* try .pub suffix */
393 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
394 return SSH_ERR_ALLOC_FAIL;
395 r = SSH_ERR_ALLOC_FAIL; /* in case strlcpy or strlcat fail */
396 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
397 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
398 (r = sshkey_try_load_public(pub, file, commentp)) == 0) {
399 if (keyp != NULL)
400 *keyp = pub;
401 return 0;
403 sshkey_free(pub);
405 return r;
408 /* Load the certificate associated with the named private key */
410 sshkey_load_cert(const char *filename, struct sshkey **keyp)
412 struct sshkey *pub = NULL;
413 char *file = NULL;
414 int r = SSH_ERR_INTERNAL_ERROR;
416 if (keyp != NULL)
417 *keyp = NULL;
419 if (asprintf(&file, "%s-cert.pub", filename) == -1)
420 return SSH_ERR_ALLOC_FAIL;
422 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
423 goto out;
425 if ((r = sshkey_try_load_public(pub, file, NULL)) != 0)
426 goto out;
427 /* success */
428 if (keyp != NULL) {
429 *keyp = pub;
430 pub = NULL;
432 r = 0;
433 out:
434 free(file);
435 sshkey_free(pub);
436 return r;
439 /* Load private key and certificate */
441 sshkey_load_private_cert(int type, const char *filename, const char *passphrase,
442 struct sshkey **keyp, int *perm_ok)
444 struct sshkey *key = NULL, *cert = NULL;
445 int r;
447 if (keyp != NULL)
448 *keyp = NULL;
450 switch (type) {
451 #ifdef WITH_OPENSSL
452 case KEY_RSA:
453 case KEY_DSA:
454 case KEY_ECDSA:
455 #endif /* WITH_OPENSSL */
456 case KEY_ED25519:
457 case KEY_UNSPEC:
458 break;
459 default:
460 return SSH_ERR_KEY_TYPE_UNKNOWN;
463 if ((r = sshkey_load_private_type(type, filename,
464 passphrase, &key, NULL, perm_ok)) != 0 ||
465 (r = sshkey_load_cert(filename, &cert)) != 0)
466 goto out;
468 /* Make sure the private key matches the certificate */
469 if (sshkey_equal_public(key, cert) == 0) {
470 r = SSH_ERR_KEY_CERT_MISMATCH;
471 goto out;
474 if ((r = sshkey_to_certified(key)) != 0 ||
475 (r = sshkey_cert_copy(cert, key)) != 0)
476 goto out;
477 r = 0;
478 if (keyp != NULL) {
479 *keyp = key;
480 key = NULL;
482 out:
483 sshkey_free(key);
484 sshkey_free(cert);
485 return r;
489 * Returns success if the specified "key" is listed in the file "filename",
490 * SSH_ERR_KEY_NOT_FOUND: if the key is not listed or another error.
491 * If "strict_type" is set then the key type must match exactly,
492 * otherwise a comparison that ignores certficiate data is performed.
493 * If "check_ca" is set and "key" is a certificate, then its CA key is
494 * also checked and sshkey_in_file() will return success if either is found.
497 sshkey_in_file(struct sshkey *key, const char *filename, int strict_type,
498 int check_ca)
500 FILE *f;
501 char line[SSH_MAX_PUBKEY_BYTES];
502 char *cp;
503 u_long linenum = 0;
504 int r = 0;
505 struct sshkey *pub = NULL;
506 int (*sshkey_compare)(const struct sshkey *, const struct sshkey *) =
507 strict_type ? sshkey_equal : sshkey_equal_public;
509 if ((f = fopen(filename, "r")) == NULL)
510 return SSH_ERR_SYSTEM_ERROR;
512 while (read_keyfile_line(f, filename, line, sizeof(line),
513 &linenum) != -1) {
514 cp = line;
516 /* Skip leading whitespace. */
517 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
520 /* Skip comments and empty lines */
521 switch (*cp) {
522 case '#':
523 case '\n':
524 case '\0':
525 continue;
528 if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
529 r = SSH_ERR_ALLOC_FAIL;
530 goto out;
532 if ((r = sshkey_read(pub, &cp)) != 0)
533 goto out;
534 if (sshkey_compare(key, pub) ||
535 (check_ca && sshkey_is_cert(key) &&
536 sshkey_compare(key->cert->signature_key, pub))) {
537 r = 0;
538 goto out;
540 sshkey_free(pub);
541 pub = NULL;
543 r = SSH_ERR_KEY_NOT_FOUND;
544 out:
545 sshkey_free(pub);
546 fclose(f);
547 return r;
551 * Checks whether the specified key is revoked, returning 0 if not,
552 * SSH_ERR_KEY_REVOKED if it is or another error code if something
553 * unexpected happened.
554 * This will check both the key and, if it is a certificate, its CA key too.
555 * "revoked_keys_file" may be a KRL or a one-per-line list of public keys.
558 sshkey_check_revoked(struct sshkey *key, const char *revoked_keys_file)
560 int r;
562 r = ssh_krl_file_contains_key(revoked_keys_file, key);
563 /* If this was not a KRL to begin with then continue below */
564 if (r != SSH_ERR_KRL_BAD_MAGIC)
565 return r;
568 * If the file is not a KRL or we can't handle KRLs then attempt to
569 * parse the file as a flat list of keys.
571 switch ((r = sshkey_in_file(key, revoked_keys_file, 0, 1))) {
572 case 0:
573 /* Key found => revoked */
574 return SSH_ERR_KEY_REVOKED;
575 case SSH_ERR_KEY_NOT_FOUND:
576 /* Key not found => not revoked */
577 return 0;
578 default:
579 /* Some other error occurred */
580 return r;