[BACK]Return to authfile.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/authfile.c, Revision 1.121

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