[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.124

1.124   ! djm         1: /* $OpenBSD: authfile.c,v 1.123 2017/03/26 00:18:52 deraadt 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.29      markus    314: /* load public key from ssh v1 private or 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.110     deraadt   319:        char file[PATH_MAX];
1.107     djm       320:        int r, fd;
1.18      markus    321:
1.107     djm       322:        if (keyp != NULL)
                    323:                *keyp = NULL;
                    324:        if (commentp != NULL)
                    325:                *commentp = NULL;
                    326:
1.111     djm       327:        /* XXX should load file once and attempt to parse each format */
                    328:
1.107     djm       329:        if ((fd = open(filename, O_RDONLY)) < 0)
                    330:                goto skip;
1.113     djm       331:        close(fd);
1.107     djm       332:
                    333:        /* try ssh2 public key */
                    334:        if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
                    335:                return SSH_ERR_ALLOC_FAIL;
                    336:        if ((r = sshkey_try_load_public(pub, filename, commentp)) == 0) {
                    337:                if (keyp != NULL)
                    338:                        *keyp = pub;
                    339:                return 0;
                    340:        }
                    341:        sshkey_free(pub);
1.53      markus    342:
                    343:
1.107     djm       344:  skip:
                    345:        /* try .pub suffix */
                    346:        if ((pub = sshkey_new(KEY_UNSPEC)) == NULL)
                    347:                return SSH_ERR_ALLOC_FAIL;
                    348:        r = SSH_ERR_ALLOC_FAIL; /* in case strlcpy or strlcat fail */
1.29      markus    349:        if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
                    350:            (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
1.107     djm       351:            (r = sshkey_try_load_public(pub, file, commentp)) == 0) {
                    352:                if (keyp != NULL)
                    353:                        *keyp = pub;
                    354:                return 0;
                    355:        }
                    356:        sshkey_free(pub);
1.111     djm       357:
1.107     djm       358:        return r;
1.81      djm       359: }
                    360:
                    361: /* Load the certificate associated with the named private key */
1.107     djm       362: int
                    363: sshkey_load_cert(const char *filename, struct sshkey **keyp)
1.81      djm       364: {
1.107     djm       365:        struct sshkey *pub = NULL;
                    366:        char *file = NULL;
                    367:        int r = SSH_ERR_INTERNAL_ERROR;
                    368:
1.121     djm       369:        if (keyp != NULL)
                    370:                *keyp = NULL;
1.81      djm       371:
1.107     djm       372:        if (asprintf(&file, "%s-cert.pub", filename) == -1)
                    373:                return SSH_ERR_ALLOC_FAIL;
                    374:
                    375:        if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
                    376:                goto out;
                    377:        }
                    378:        if ((r = sshkey_try_load_public(pub, file, NULL)) != 0)
                    379:                goto out;
1.121     djm       380:        /* success */
                    381:        if (keyp != NULL) {
                    382:                *keyp = pub;
                    383:                pub = NULL;
                    384:        }
1.107     djm       385:        r = 0;
                    386:  out:
1.118     mmcc      387:        free(file);
1.119     mmcc      388:        sshkey_free(pub);
1.107     djm       389:        return r;
1.81      djm       390: }
                    391:
                    392: /* Load private key and certificate */
1.107     djm       393: int
                    394: sshkey_load_private_cert(int type, const char *filename, const char *passphrase,
                    395:     struct sshkey **keyp, int *perm_ok)
1.81      djm       396: {
1.107     djm       397:        struct sshkey *key = NULL, *cert = NULL;
                    398:        int r;
                    399:
1.121     djm       400:        if (keyp != NULL)
                    401:                *keyp = NULL;
1.81      djm       402:
                    403:        switch (type) {
1.106     markus    404: #ifdef WITH_OPENSSL
1.81      djm       405:        case KEY_RSA:
                    406:        case KEY_DSA:
1.83      djm       407:        case KEY_ECDSA:
1.116     markus    408: #endif /* WITH_OPENSSL */
1.101     djm       409:        case KEY_ED25519:
1.107     djm       410:        case KEY_UNSPEC:
1.81      djm       411:                break;
                    412:        default:
1.107     djm       413:                return SSH_ERR_KEY_TYPE_UNKNOWN;
1.81      djm       414:        }
                    415:
1.107     djm       416:        if ((r = sshkey_load_private_type(type, filename,
                    417:            passphrase, &key, NULL, perm_ok)) != 0 ||
                    418:            (r = sshkey_load_cert(filename, &cert)) != 0)
                    419:                goto out;
1.81      djm       420:
                    421:        /* Make sure the private key matches the certificate */
1.107     djm       422:        if (sshkey_equal_public(key, cert) == 0) {
                    423:                r = SSH_ERR_KEY_CERT_MISMATCH;
                    424:                goto out;
1.81      djm       425:        }
                    426:
1.115     djm       427:        if ((r = sshkey_to_certified(key)) != 0 ||
1.107     djm       428:            (r = sshkey_cert_copy(cert, key)) != 0)
                    429:                goto out;
                    430:        r = 0;
1.121     djm       431:        if (keyp != NULL) {
                    432:                *keyp = key;
                    433:                key = NULL;
                    434:        }
1.107     djm       435:  out:
1.119     mmcc      436:        sshkey_free(key);
                    437:        sshkey_free(cert);
1.107     djm       438:        return r;
1.1       deraadt   439: }
1.80      djm       440:
                    441: /*
1.107     djm       442:  * Returns success if the specified "key" is listed in the file "filename",
                    443:  * SSH_ERR_KEY_NOT_FOUND: if the key is not listed or another error.
1.108     djm       444:  * If "strict_type" is set then the key type must match exactly,
1.80      djm       445:  * otherwise a comparison that ignores certficiate data is performed.
1.108     djm       446:  * If "check_ca" is set and "key" is a certificate, then its CA key is
                    447:  * also checked and sshkey_in_file() will return success if either is found.
1.80      djm       448:  */
                    449: int
1.108     djm       450: sshkey_in_file(struct sshkey *key, const char *filename, int strict_type,
                    451:     int check_ca)
1.80      djm       452: {
                    453:        FILE *f;
                    454:        char line[SSH_MAX_PUBKEY_BYTES];
                    455:        char *cp;
                    456:        u_long linenum = 0;
1.107     djm       457:        int r = 0;
                    458:        struct sshkey *pub = NULL;
                    459:        int (*sshkey_compare)(const struct sshkey *, const struct sshkey *) =
                    460:            strict_type ?  sshkey_equal : sshkey_equal_public;
1.80      djm       461:
1.108     djm       462:        if ((f = fopen(filename, "r")) == NULL)
                    463:                return SSH_ERR_SYSTEM_ERROR;
1.80      djm       464:
                    465:        while (read_keyfile_line(f, filename, line, sizeof(line),
1.107     djm       466:            &linenum) != -1) {
1.80      djm       467:                cp = line;
                    468:
                    469:                /* Skip leading whitespace. */
                    470:                for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
                    471:                        ;
                    472:
                    473:                /* Skip comments and empty lines */
                    474:                switch (*cp) {
                    475:                case '#':
                    476:                case '\n':
                    477:                case '\0':
                    478:                        continue;
                    479:                }
                    480:
1.107     djm       481:                if ((pub = sshkey_new(KEY_UNSPEC)) == NULL) {
                    482:                        r = SSH_ERR_ALLOC_FAIL;
                    483:                        goto out;
1.80      djm       484:                }
1.107     djm       485:                if ((r = sshkey_read(pub, &cp)) != 0)
                    486:                        goto out;
1.108     djm       487:                if (sshkey_compare(key, pub) ||
                    488:                    (check_ca && sshkey_is_cert(key) &&
                    489:                    sshkey_compare(key->cert->signature_key, pub))) {
1.107     djm       490:                        r = 0;
                    491:                        goto out;
1.80      djm       492:                }
1.107     djm       493:                sshkey_free(pub);
                    494:                pub = NULL;
1.80      djm       495:        }
1.107     djm       496:        r = SSH_ERR_KEY_NOT_FOUND;
                    497:  out:
1.119     mmcc      498:        sshkey_free(pub);
1.80      djm       499:        fclose(f);
1.107     djm       500:        return r;
1.108     djm       501: }
                    502:
                    503: /*
                    504:  * Checks whether the specified key is revoked, returning 0 if not,
                    505:  * SSH_ERR_KEY_REVOKED if it is or another error code if something
                    506:  * unexpected happened.
                    507:  * This will check both the key and, if it is a certificate, its CA key too.
                    508:  * "revoked_keys_file" may be a KRL or a one-per-line list of public keys.
                    509:  */
                    510: int
                    511: sshkey_check_revoked(struct sshkey *key, const char *revoked_keys_file)
                    512: {
                    513:        int r;
                    514:
                    515:        r = ssh_krl_file_contains_key(revoked_keys_file, key);
                    516:        /* If this was not a KRL to begin with then continue below */
                    517:        if (r != SSH_ERR_KRL_BAD_MAGIC)
                    518:                return r;
                    519:
                    520:        /*
                    521:         * If the file is not a KRL or we can't handle KRLs then attempt to
                    522:         * parse the file as a flat list of keys.
                    523:         */
                    524:        switch ((r = sshkey_in_file(key, revoked_keys_file, 0, 1))) {
                    525:        case 0:
                    526:                /* Key found => revoked */
                    527:                return SSH_ERR_KEY_REVOKED;
                    528:        case SSH_ERR_KEY_NOT_FOUND:
                    529:                /* Key not found => not revoked */
                    530:                return 0;
                    531:        default:
                    532:                /* Some other error occurred */
                    533:                return r;
                    534:        }
1.80      djm       535: }
1.107     djm       536: