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