Annotation of src/usr.bin/ssh/authfile.c, Revision 1.125
1.125 ! markus 1: /* $OpenBSD: authfile.c,v 1.124 2017/04/30 23:10:43 djm Exp $ */
1.1 deraadt 2: /*
1.99 markus 3: * Copyright (c) 2000, 2013 Markus Friedl. All rights reserved.
1.19 deraadt 4: *
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.
13: *
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.
1.9 deraadt 24: */
1.1 deraadt 25:
1.62 stevesk 26:
27: #include <sys/types.h>
28: #include <sys/stat.h>
1.76 deraadt 29: #include <sys/uio.h>
1.1 deraadt 30:
1.69 stevesk 31: #include <errno.h>
1.68 stevesk 32: #include <fcntl.h>
1.74 stevesk 33: #include <stdio.h>
1.73 stevesk 34: #include <stdlib.h>
1.71 stevesk 35: #include <string.h>
1.70 stevesk 36: #include <unistd.h>
1.110 deraadt 37: #include <limits.h>
1.15 markus 38:
1.25 markus 39: #include "cipher.h"
1.1 deraadt 40: #include "ssh.h"
1.25 markus 41: #include "log.h"
1.27 itojun 42: #include "authfile.h"
1.44 markus 43: #include "rsa.h"
1.60 dtucker 44: #include "misc.h"
1.61 djm 45: #include "atomicio.h"
1.116 markus 46: #include "sshkey.h"
1.107 djm 47: #include "sshbuf.h"
48: #include "ssherr.h"
1.108 djm 49: #include "krl.h"
1.1 deraadt 50:
1.88 djm 51: #define MAX_KEY_FILE_SIZE (1024 * 1024)
52:
1.86 djm 53: /* Save a key blob to a file */
54: static int
1.107 djm 55: sshkey_save_private_blob(struct sshbuf *keybuf, const char *filename)
1.86 djm 56: {
1.107 djm 57: int fd, oerrno;
1.86 djm 58:
1.107 djm 59: if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0)
60: return SSH_ERR_SYSTEM_ERROR;
61: if (atomicio(vwrite, fd, (u_char *)sshbuf_ptr(keybuf),
62: sshbuf_len(keybuf)) != sshbuf_len(keybuf)) {
63: oerrno = errno;
1.86 djm 64: close(fd);
65: unlink(filename);
1.107 djm 66: errno = oerrno;
67: return SSH_ERR_SYSTEM_ERROR;
1.86 djm 68: }
69: close(fd);
1.107 djm 70: return 0;
1.86 djm 71: }
72:
73: int
1.107 djm 74: sshkey_save_private(struct sshkey *key, const char *filename,
75: const char *passphrase, const char *comment,
76: int force_new_format, const char *new_format_cipher, int new_format_rounds)
1.86 djm 77: {
1.107 djm 78: struct sshbuf *keyblob = NULL;
79: int r;
1.86 djm 80:
1.107 djm 81: if ((keyblob = sshbuf_new()) == NULL)
82: return SSH_ERR_ALLOC_FAIL;
83: if ((r = sshkey_private_to_fileblob(key, keyblob, passphrase, comment,
84: force_new_format, new_format_cipher, new_format_rounds)) != 0)
1.86 djm 85: goto out;
1.107 djm 86: if ((r = sshkey_save_private_blob(keyblob, filename)) != 0)
1.86 djm 87: goto out;
1.107 djm 88: r = 0;
1.86 djm 89: out:
1.107 djm 90: sshbuf_free(keyblob);
91: return r;
1.86 djm 92: }
93:
1.88 djm 94: /* Load a key from a fd into a buffer */
95: int
1.109 djm 96: sshkey_load_file(int fd, struct sshbuf *blob)
1.86 djm 97: {
1.88 djm 98: u_char buf[1024];
1.86 djm 99: size_t len;
1.51 fgsch 100: struct stat st;
1.122 djm 101: int r, dontmax = 0;
1.8 markus 102:
1.107 djm 103: if (fstat(fd, &st) < 0)
104: return SSH_ERR_SYSTEM_ERROR;
1.88 djm 105: if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
1.107 djm 106: st.st_size > MAX_KEY_FILE_SIZE)
107: return SSH_ERR_INVALID_FORMAT;
1.122 djm 108: /*
109: * Pre-allocate the buffer used for the key contents and clamp its
110: * maximum size. This ensures that key contents are never leaked via
111: * implicit realloc() in the sshbuf code.
112: */
113: if ((st.st_mode & S_IFREG) == 0 || st.st_size <= 0) {
1.123 deraadt 114: st.st_size = 64*1024; /* 64k ought to be enough for anybody. :) */
1.122 djm 115: dontmax = 1;
116: }
117: if ((r = sshbuf_allocate(blob, st.st_size)) != 0 ||
118: (dontmax && (r = sshbuf_set_max_size(blob, st.st_size)) != 0))
119: return r;
1.88 djm 120: for (;;) {
121: if ((len = atomicio(read, fd, buf, sizeof(buf))) == 0) {
122: if (errno == EPIPE)
123: break;
1.107 djm 124: r = SSH_ERR_SYSTEM_ERROR;
125: goto out;
1.88 djm 126: }
1.107 djm 127: if ((r = sshbuf_put(blob, buf, len)) != 0)
128: goto out;
129: if (sshbuf_len(blob) > MAX_KEY_FILE_SIZE) {
130: r = SSH_ERR_INVALID_FORMAT;
131: goto out;
1.88 djm 132: }
133: }
134: if ((st.st_mode & (S_IFSOCK|S_IFCHR|S_IFIFO)) == 0 &&
1.107 djm 135: st.st_size != (off_t)sshbuf_len(blob)) {
136: r = SSH_ERR_FILE_CHANGED;
137: goto out;
1.8 markus 138: }
1.107 djm 139: r = 0;
1.88 djm 140:
1.107 djm 141: out:
142: explicit_bzero(buf, sizeof(buf));
143: if (r != 0)
144: sshbuf_reset(blob);
145: return r;
1.86 djm 146: }
1.8 markus 147:
1.1 deraadt 148:
1.107 djm 149: /* XXX remove error() calls from here? */
1.63 dtucker 150: int
1.107 djm 151: sshkey_perm_ok(int fd, const char *filename)
1.15 markus 152: {
153: struct stat st;
154:
1.38 markus 155: if (fstat(fd, &st) < 0)
1.107 djm 156: return SSH_ERR_SYSTEM_ERROR;
1.38 markus 157: /*
158: * if a key owned by the user is accessed, then we check the
159: * permissions of the file. if the key owned by a different user,
160: * then we don't care.
161: */
162: if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
1.15 markus 163: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
164: error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
165: error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
1.38 markus 166: error("Permissions 0%3.3o for '%s' are too open.",
1.54 djm 167: (u_int)st.st_mode & 0777, filename);
1.114 djm 168: error("It is required that your private key files are NOT accessible by others.");
1.29 markus 169: error("This private key will be ignored.");
1.107 djm 170: return SSH_ERR_KEY_BAD_PERMISSIONS;
1.15 markus 171: }
1.107 djm 172: return 0;
1.29 markus 173: }
174:
1.107 djm 175: /* XXX kill perm_ok now that we have SSH_ERR_KEY_BAD_PERMISSIONS? */
176: int
177: sshkey_load_private_type(int type, const char *filename, const char *passphrase,
178: struct sshkey **keyp, char **commentp, int *perm_ok)
1.86 djm 179: {
1.107 djm 180: int fd, r;
1.99 markus 181:
1.121 djm 182: if (keyp != NULL)
183: *keyp = NULL;
1.107 djm 184: if (commentp != NULL)
185: *commentp = NULL;
1.86 djm 186:
1.107 djm 187: if ((fd = open(filename, O_RDONLY)) < 0) {
1.78 dtucker 188: if (perm_ok != NULL)
189: *perm_ok = 0;
1.107 djm 190: return SSH_ERR_SYSTEM_ERROR;
1.78 dtucker 191: }
1.107 djm 192: if (sshkey_perm_ok(fd, filename) != 0) {
1.67 dtucker 193: if (perm_ok != NULL)
194: *perm_ok = 0;
1.107 djm 195: r = SSH_ERR_KEY_BAD_PERMISSIONS;
196: goto out;
1.29 markus 197: }
1.67 dtucker 198: if (perm_ok != NULL)
199: *perm_ok = 1;
1.86 djm 200:
1.109 djm 201: r = sshkey_load_private_type_fd(fd, type, passphrase, keyp, commentp);
202: out:
203: close(fd);
204: return r;
205: }
206:
207: int
208: sshkey_load_private_type_fd(int fd, int type, const char *passphrase,
209: struct sshkey **keyp, char **commentp)
210: {
211: struct sshbuf *buffer = NULL;
212: int r;
213:
1.121 djm 214: if (keyp != NULL)
215: *keyp = NULL;
1.107 djm 216: if ((buffer = sshbuf_new()) == NULL) {
217: r = SSH_ERR_ALLOC_FAIL;
218: goto out;
1.15 markus 219: }
1.109 djm 220: if ((r = sshkey_load_file(fd, buffer)) != 0 ||
221: (r = sshkey_parse_private_fileblob_type(buffer, type,
222: passphrase, keyp, commentp)) != 0)
1.107 djm 223: goto out;
1.109 djm 224:
225: /* success */
1.107 djm 226: r = 0;
227: out:
1.120 mmcc 228: sshbuf_free(buffer);
1.107 djm 229: return r;
1.29 markus 230: }
231:
1.107 djm 232: /* XXX this is almost identical to sshkey_load_private_type() */
233: int
234: sshkey_load_private(const char *filename, const char *passphrase,
235: struct sshkey **keyp, char **commentp)
1.88 djm 236: {
1.107 djm 237: struct sshbuf *buffer = NULL;
238: int r, fd;
1.88 djm 239:
1.121 djm 240: if (keyp != NULL)
241: *keyp = NULL;
1.107 djm 242: if (commentp != NULL)
243: *commentp = NULL;
1.88 djm 244:
1.107 djm 245: if ((fd = open(filename, O_RDONLY)) < 0)
246: return SSH_ERR_SYSTEM_ERROR;
247: if (sshkey_perm_ok(fd, filename) != 0) {
248: r = SSH_ERR_KEY_BAD_PERMISSIONS;
249: goto out;
1.29 markus 250: }
1.86 djm 251:
1.107 djm 252: if ((buffer = sshbuf_new()) == NULL) {
253: r = SSH_ERR_ALLOC_FAIL;
254: goto out;
1.86 djm 255: }
1.109 djm 256: if ((r = sshkey_load_file(fd, buffer)) != 0 ||
1.117 tim 257: (r = sshkey_parse_private_fileblob(buffer, passphrase, keyp,
258: commentp)) != 0)
1.107 djm 259: goto out;
260: r = 0;
261: out:
1.86 djm 262: close(fd);
1.120 mmcc 263: sshbuf_free(buffer);
1.107 djm 264: return r;
1.18 markus 265: }
266:
1.37 itojun 267: static int
1.107 djm 268: sshkey_try_load_public(struct sshkey *k, const char *filename, char **commentp)
1.18 markus 269: {
270: FILE *f;
1.59 dtucker 271: char line[SSH_MAX_PUBKEY_BYTES];
1.18 markus 272: char *cp;
1.60 dtucker 273: u_long linenum = 0;
1.107 djm 274: int r;
1.18 markus 275:
1.107 djm 276: if (commentp != NULL)
277: *commentp = NULL;
278: if ((f = fopen(filename, "r")) == NULL)
279: return SSH_ERR_SYSTEM_ERROR;
280: while (read_keyfile_line(f, filename, line, sizeof(line),
281: &linenum) != -1) {
282: cp = line;
283: switch (*cp) {
284: case '#':
285: case '\n':
286: case '\0':
287: continue;
288: }
289: /* Abort loading if this looks like a private key */
290: if (strncmp(cp, "-----BEGIN", 10) == 0 ||
291: strcmp(cp, "SSH PRIVATE KEY FILE") == 0)
292: break;
293: /* Skip leading whitespace. */
294: for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
295: ;
296: if (*cp) {
297: if ((r = sshkey_read(k, &cp)) == 0) {
298: cp[strcspn(cp, "\r\n")] = '\0';
299: if (commentp) {
300: *commentp = strdup(*cp ?
301: cp : filename);
302: if (*commentp == NULL)
303: r = SSH_ERR_ALLOC_FAIL;
1.18 markus 304: }
1.107 djm 305: fclose(f);
306: return r;
1.18 markus 307: }
308: }
309: }
1.107 djm 310: fclose(f);
311: return SSH_ERR_INVALID_FORMAT;
1.18 markus 312: }
313:
1.125 ! markus 314: /* load public key from any pubkey file */
1.107 djm 315: int
316: sshkey_load_public(const char *filename, struct sshkey **keyp, char **commentp)
1.18 markus 317: {
1.107 djm 318: struct sshkey *pub = NULL;
1.125 ! markus 319: char *file = NULL;
! 320: int r;
1.18 markus 321:
1.107 djm 322: if (keyp != NULL)
323: *keyp = NULL;
324: if (commentp != NULL)
325: *commentp = NULL;
326:
327: if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
328: return SSH_ERR_ALLOC_FAIL;
329: if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
1.125 ! markus 330: if (keyp != NULL) {
1.107 djm 331: *keyp = pub;
1.125 ! markus 332: pub = NULL;
! 333: }
! 334: r = 0;
! 335: goto out;
1.107 djm 336: }
337: sshkey_free(pub);
1.53 markus 338:
1.107 djm 339: /* try .pub suffix */
1.125 ! markus 340: if (asprintf(&file, "%s.pub", filename) == -1)
1.107 djm 341: return SSH_ERR_ALLOC_FAIL;
1.125 ! markus 342: if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
! 343: r = SSH_ERR_ALLOC_FAIL;
! 344: goto out;
! 345: }
! 346: if ((r = sshkey_try_load_public(pub, file, commentp)) == 0) {
! 347: if (keyp != NULL) {
1.107 djm 348: *keyp = pub;
1.125 ! markus 349: pub = NULL;
! 350: }
! 351: r = 0;
1.107 djm 352: }
1.125 ! markus 353: out:
! 354: free(file);
1.107 djm 355: sshkey_free(pub);
356: return r;
1.81 djm 357: }
358:
359: /* Load the certificate associated with the named private key */
1.107 djm 360: int
361: sshkey_load_cert(const char *filename, struct sshkey **keyp)
1.81 djm 362: {
1.107 djm 363: struct sshkey *pub = NULL;
364: char *file = NULL;
365: int r = SSH_ERR_INTERNAL_ERROR;
366:
1.121 djm 367: if (keyp != NULL)
368: *keyp = NULL;
1.81 djm 369:
1.107 djm 370: if (asprintf(&file, "%s-cert.pub", filename) == -1)
371: return SSH_ERR_ALLOC_FAIL;
372:
373: if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
374: goto out;
375: }
376: if ((r = sshkey_try_load_public(pub, file, NULL)) != 0)
377: goto out;
1.121 djm 378: /* success */
379: if (keyp != NULL) {
380: *keyp = pub;
381: pub = NULL;
382: }
1.107 djm 383: r = 0;
384: out:
1.118 mmcc 385: free(file);
1.119 mmcc 386: sshkey_free(pub);
1.107 djm 387: return r;
1.81 djm 388: }
389:
390: /* Load private key and certificate */
1.107 djm 391: int
392: sshkey_load_private_cert(int type, const char *filename, const char *passphrase,
393: struct sshkey **keyp, int *perm_ok)
1.81 djm 394: {
1.107 djm 395: struct sshkey *key = NULL, *cert = NULL;
396: int r;
397:
1.121 djm 398: if (keyp != NULL)
399: *keyp = NULL;
1.81 djm 400:
401: switch (type) {
1.106 markus 402: #ifdef WITH_OPENSSL
1.81 djm 403: case KEY_RSA:
404: case KEY_DSA:
1.83 djm 405: case KEY_ECDSA:
1.116 markus 406: #endif /* WITH_OPENSSL */
1.101 djm 407: case KEY_ED25519:
1.107 djm 408: case KEY_UNSPEC:
1.81 djm 409: break;
410: default:
1.107 djm 411: return SSH_ERR_KEY_TYPE_UNKNOWN;
1.81 djm 412: }
413:
1.107 djm 414: if ((r = sshkey_load_private_type(type, filename,
415: passphrase, &key, NULL, perm_ok)) != 0 ||
416: (r = sshkey_load_cert(filename, &cert)) != 0)
417: goto out;
1.81 djm 418:
419: /* Make sure the private key matches the certificate */
1.107 djm 420: if (sshkey_equal_public(key, cert) == 0) {
421: r = SSH_ERR_KEY_CERT_MISMATCH;
422: goto out;
1.81 djm 423: }
424:
1.115 djm 425: if ((r = sshkey_to_certified(key)) != 0 ||
1.107 djm 426: (r = sshkey_cert_copy(cert, key)) != 0)
427: goto out;
428: r = 0;
1.121 djm 429: if (keyp != NULL) {
430: *keyp = key;
431: key = NULL;
432: }
1.107 djm 433: out:
1.119 mmcc 434: sshkey_free(key);
435: sshkey_free(cert);
1.107 djm 436: return r;
1.1 deraadt 437: }
1.80 djm 438:
439: /*
1.107 djm 440: * Returns success if the specified "key" is listed in the file "filename",
441: * SSH_ERR_KEY_NOT_FOUND: if the key is not listed or another error.
1.108 djm 442: * If "strict_type" is set then the key type must match exactly,
1.80 djm 443: * otherwise a comparison that ignores certficiate data is performed.
1.108 djm 444: * If "check_ca" is set and "key" is a certificate, then its CA key is
445: * also checked and sshkey_in_file() will return success if either is found.
1.80 djm 446: */
447: int
1.108 djm 448: sshkey_in_file(struct sshkey *key, const char *filename, int strict_type,
449: int check_ca)
1.80 djm 450: {
451: FILE *f;
452: char line[SSH_MAX_PUBKEY_BYTES];
453: char *cp;
454: u_long linenum = 0;
1.107 djm 455: int r = 0;
456: struct sshkey *pub = NULL;
457: int (*sshkey_compare)(const struct sshkey *, const struct sshkey *) =
458: strict_type ? sshkey_equal : sshkey_equal_public;
1.80 djm 459:
1.108 djm 460: if ((f = fopen(filename, "r")) == NULL)
461: return SSH_ERR_SYSTEM_ERROR;
1.80 djm 462:
463: while (read_keyfile_line(f, filename, line, sizeof(line),
1.107 djm 464: &linenum) != -1) {
1.80 djm 465: cp = line;
466:
467: /* Skip leading whitespace. */
468: for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
469: ;
470:
471: /* Skip comments and empty lines */
472: switch (*cp) {
473: case '#':
474: case '\n':
475: case '\0':
476: continue;
477: }
478:
1.107 djm 479: if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
480: r = SSH_ERR_ALLOC_FAIL;
481: goto out;
1.80 djm 482: }
1.107 djm 483: if ((r = sshkey_read(pub, &cp)) != 0)
484: goto out;
1.108 djm 485: if (sshkey_compare(key, pub) ||
486: (check_ca && sshkey_is_cert(key) &&
487: sshkey_compare(key->cert->signature_key, pub))) {
1.107 djm 488: r = 0;
489: goto out;
1.80 djm 490: }
1.107 djm 491: sshkey_free(pub);
492: pub = NULL;
1.80 djm 493: }
1.107 djm 494: r = SSH_ERR_KEY_NOT_FOUND;
495: out:
1.119 mmcc 496: sshkey_free(pub);
1.80 djm 497: fclose(f);
1.107 djm 498: return r;
1.108 djm 499: }
500:
501: /*
502: * Checks whether the specified key is revoked, returning 0 if not,
503: * SSH_ERR_KEY_REVOKED if it is or another error code if something
504: * unexpected happened.
505: * This will check both the key and, if it is a certificate, its CA key too.
506: * "revoked_keys_file" may be a KRL or a one-per-line list of public keys.
507: */
508: int
509: sshkey_check_revoked(struct sshkey *key, const char *revoked_keys_file)
510: {
511: int r;
512:
513: r = ssh_krl_file_contains_key(revoked_keys_file, key);
514: /* If this was not a KRL to begin with then continue below */
515: if (r != SSH_ERR_KRL_BAD_MAGIC)
516: return r;
517:
518: /*
519: * If the file is not a KRL or we can't handle KRLs then attempt to
520: * parse the file as a flat list of keys.
521: */
522: switch ((r = sshkey_in_file(key, revoked_keys_file, 0, 1))) {
523: case 0:
524: /* Key found => revoked */
525: return SSH_ERR_KEY_REVOKED;
526: case SSH_ERR_KEY_NOT_FOUND:
527: /* Key not found => not revoked */
528: return 0;
529: default:
530: /* Some other error occurred */
531: return r;
532: }
1.80 djm 533: }
1.107 djm 534: