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

Annotation of src/usr.bin/ssh/sshsig.c, Revision 1.7

1.1       djm         1: /*
                      2:  * Copyright (c) 2019 Google LLC
                      3:  *
                      4:  * Permission to use, copy, modify, and distribute this software for any
                      5:  * purpose with or without fee is hereby granted, provided that the above
                      6:  * copyright notice and this permission notice appear in all copies.
                      7:  *
                      8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                      9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     15:  */
                     16:
                     17: #include <stdio.h>
                     18: #include <stdlib.h>
                     19: #include <stdarg.h>
                     20: #include <errno.h>
                     21: #include <string.h>
                     22: #include <unistd.h>
                     23:
                     24: #include "authfd.h"
                     25: #include "authfile.h"
                     26: #include "log.h"
                     27: #include "misc.h"
                     28: #include "sshbuf.h"
                     29: #include "sshsig.h"
                     30: #include "ssherr.h"
                     31: #include "sshkey.h"
                     32: #include "match.h"
                     33: #include "digest.h"
                     34:
                     35: #define SIG_VERSION            0x01
                     36: #define MAGIC_PREAMBLE         "SSHSIG"
                     37: #define MAGIC_PREAMBLE_LEN     (sizeof(MAGIC_PREAMBLE) - 1)
                     38: #define BEGIN_SIGNATURE                "-----BEGIN SSH SIGNATURE-----\n"
                     39: #define END_SIGNATURE          "-----END SSH SIGNATURE-----"
                     40: #define RSA_SIGN_ALG           "rsa-sha2-512" /* XXX maybe make configurable */
                     41: #define RSA_SIGN_ALLOWED       "rsa-sha2-512,rsa-sha2-256"
                     42: #define HASHALG_DEFAULT                "sha512" /* XXX maybe make configurable */
                     43: #define HASHALG_ALLOWED                "sha256,sha512"
                     44:
                     45: int
                     46: sshsig_armor(const struct sshbuf *blob, struct sshbuf **out)
                     47: {
                     48:        struct sshbuf *buf = NULL;
                     49:        int r = SSH_ERR_INTERNAL_ERROR;
                     50:
                     51:        *out = NULL;
                     52:
                     53:        if ((buf = sshbuf_new()) == NULL) {
                     54:                error("%s: sshbuf_new failed", __func__);
                     55:                r = SSH_ERR_ALLOC_FAIL;
                     56:                goto out;
                     57:        }
                     58:
                     59:        if ((r = sshbuf_put(buf, BEGIN_SIGNATURE,
                     60:            sizeof(BEGIN_SIGNATURE)-1)) != 0) {
                     61:                error("%s: sshbuf_putf failed: %s", __func__, ssh_err(r));
                     62:                goto out;
                     63:        }
                     64:
                     65:        if ((r = sshbuf_dtob64(blob, buf, 1)) != 0) {
                     66:                error("%s: Couldn't base64 encode signature blob: %s",
                     67:                    __func__, ssh_err(r));
                     68:                goto out;
                     69:        }
                     70:
                     71:        if ((r = sshbuf_put(buf, END_SIGNATURE,
                     72:            sizeof(END_SIGNATURE)-1)) != 0 ||
                     73:            (r = sshbuf_put_u8(buf, '\n')) != 0) {
                     74:                error("%s: sshbuf_put failed: %s", __func__, ssh_err(r));
                     75:                goto out;
                     76:        }
                     77:        /* success */
                     78:        *out = buf;
                     79:        buf = NULL; /* transferred */
                     80:        r = 0;
                     81:  out:
                     82:        sshbuf_free(buf);
                     83:        return r;
                     84: }
                     85:
                     86: int
                     87: sshsig_dearmor(struct sshbuf *sig, struct sshbuf **out)
                     88: {
                     89:        int r;
                     90:        size_t eoffset = 0;
                     91:        struct sshbuf *buf = NULL;
                     92:        struct sshbuf *sbuf = NULL;
                     93:        char *b64 = NULL;
                     94:
                     95:        if ((sbuf = sshbuf_fromb(sig)) == NULL) {
                     96:                error("%s: sshbuf_fromb failed", __func__);
                     97:                return SSH_ERR_ALLOC_FAIL;
                     98:        }
                     99:
                    100:        if ((r = sshbuf_cmp(sbuf, 0,
                    101:            BEGIN_SIGNATURE, sizeof(BEGIN_SIGNATURE)-1)) != 0) {
                    102:                error("Couldn't parse signature: missing header");
                    103:                goto done;
                    104:        }
                    105:
                    106:        if ((r = sshbuf_consume(sbuf, sizeof(BEGIN_SIGNATURE)-1)) != 0) {
                    107:                error("%s: sshbuf_consume failed: %s", __func__, ssh_err(r));
                    108:                goto done;
                    109:        }
                    110:
                    111:        if ((r = sshbuf_find(sbuf, 0, "\n" END_SIGNATURE,
                    112:            sizeof("\n" END_SIGNATURE)-1, &eoffset)) != 0) {
                    113:                error("Couldn't parse signature: missing footer");
                    114:                goto done;
                    115:        }
                    116:
                    117:        if ((r = sshbuf_consume_end(sbuf, sshbuf_len(sbuf)-eoffset)) != 0) {
                    118:                error("%s: sshbuf_consume failed: %s", __func__, ssh_err(r));
                    119:                goto done;
                    120:        }
                    121:
                    122:        if ((b64 = sshbuf_dup_string(sbuf)) == NULL) {
                    123:                error("%s: sshbuf_dup_string failed", __func__);
                    124:                r = SSH_ERR_ALLOC_FAIL;
                    125:                goto done;
                    126:        }
                    127:
                    128:        if ((buf = sshbuf_new()) == NULL) {
                    129:                error("%s: sshbuf_new() failed", __func__);
                    130:                r = SSH_ERR_ALLOC_FAIL;
                    131:                goto done;
                    132:        }
                    133:
                    134:        if ((r = sshbuf_b64tod(buf, b64)) != 0) {
1.3       naddy     135:                error("Couldn't decode signature: %s", ssh_err(r));
1.1       djm       136:                goto done;
                    137:        }
                    138:
                    139:        /* success */
                    140:        *out = buf;
                    141:        r = 0;
                    142:        buf = NULL; /* transferred */
                    143: done:
                    144:        sshbuf_free(buf);
                    145:        sshbuf_free(sbuf);
                    146:        free(b64);
                    147:        return r;
                    148: }
                    149:
                    150: static int
                    151: sshsig_wrap_sign(struct sshkey *key, const char *hashalg,
1.7     ! djm       152:     const char *sk_provider, const struct sshbuf *h_message,
        !           153:     const char *sig_namespace, struct sshbuf **out,
        !           154:     sshsig_signer *signer, void *signer_ctx)
1.1       djm       155: {
                    156:        int r;
                    157:        size_t slen = 0;
                    158:        u_char *sig = NULL;
                    159:        struct sshbuf *blob = NULL;
                    160:        struct sshbuf *tosign = NULL;
                    161:        const char *sign_alg = NULL;
                    162:
                    163:        if ((tosign = sshbuf_new()) == NULL ||
                    164:            (blob = sshbuf_new()) == NULL) {
                    165:                error("%s: sshbuf_new failed", __func__);
                    166:                r = SSH_ERR_ALLOC_FAIL;
                    167:                goto done;
                    168:        }
                    169:
                    170:        if ((r = sshbuf_put(tosign, MAGIC_PREAMBLE, MAGIC_PREAMBLE_LEN)) != 0 ||
                    171:            (r = sshbuf_put_cstring(tosign, sig_namespace)) != 0 ||
                    172:            (r = sshbuf_put_string(tosign, NULL, 0)) != 0 || /* reserved */
                    173:            (r = sshbuf_put_cstring(tosign, hashalg)) != 0 ||
1.6       djm       174:            (r = sshbuf_put_stringb(tosign, h_message)) != 0) {
1.1       djm       175:                error("Couldn't construct message to sign: %s", ssh_err(r));
                    176:                goto done;
                    177:        }
                    178:
                    179:        /* If using RSA keys then default to a good signature algorithm */
                    180:        if (sshkey_type_plain(key->type) == KEY_RSA)
                    181:                sign_alg = RSA_SIGN_ALG;
                    182:
                    183:        if (signer != NULL) {
                    184:                if ((r = signer(key, &sig, &slen,
                    185:                    sshbuf_ptr(tosign), sshbuf_len(tosign),
1.7     ! djm       186:                    sign_alg, sk_provider, 0, signer_ctx)) != 0) {
1.1       djm       187:                        error("Couldn't sign message: %s", ssh_err(r));
                    188:                        goto done;
                    189:                }
                    190:        } else {
                    191:                if ((r = sshkey_sign(key, &sig, &slen,
                    192:                    sshbuf_ptr(tosign), sshbuf_len(tosign),
1.7     ! djm       193:                    sign_alg, sk_provider, 0)) != 0) {
1.1       djm       194:                        error("Couldn't sign message: %s", ssh_err(r));
                    195:                        goto done;
                    196:                }
                    197:        }
                    198:
                    199:        if ((r = sshbuf_put(blob, MAGIC_PREAMBLE, MAGIC_PREAMBLE_LEN)) != 0 ||
                    200:            (r = sshbuf_put_u32(blob, SIG_VERSION)) != 0 ||
                    201:            (r = sshkey_puts(key, blob)) != 0 ||
                    202:            (r = sshbuf_put_cstring(blob, sig_namespace)) != 0 ||
                    203:            (r = sshbuf_put_string(blob, NULL, 0)) != 0 || /* reserved */
                    204:            (r = sshbuf_put_cstring(blob, hashalg)) != 0 ||
                    205:            (r = sshbuf_put_string(blob, sig, slen)) != 0) {
                    206:                error("Couldn't populate blob: %s", ssh_err(r));
                    207:                goto done;
                    208:        }
                    209:
                    210:        *out = blob;
                    211:        blob = NULL;
                    212:        r = 0;
                    213: done:
                    214:        free(sig);
                    215:        sshbuf_free(blob);
                    216:        sshbuf_free(tosign);
                    217:        return r;
                    218: }
                    219:
                    220: /* Check preamble and version. */
                    221: static int
                    222: sshsig_parse_preamble(struct sshbuf *buf)
                    223: {
                    224:        int r = SSH_ERR_INTERNAL_ERROR;
                    225:        uint32_t sversion;
                    226:
                    227:        if ((r = sshbuf_cmp(buf, 0, MAGIC_PREAMBLE, MAGIC_PREAMBLE_LEN)) != 0 ||
                    228:            (r = sshbuf_consume(buf, (sizeof(MAGIC_PREAMBLE)-1))) != 0 ||
                    229:            (r = sshbuf_get_u32(buf, &sversion)) != 0) {
                    230:                error("Couldn't verify signature: invalid format");
                    231:                return r;
                    232:        }
                    233:
1.2       djm       234:        if (sversion > SIG_VERSION) {
1.1       djm       235:                error("Signature version %lu is larger than supported "
                    236:                    "version %u", (unsigned long)sversion, SIG_VERSION);
                    237:                return SSH_ERR_INVALID_FORMAT;
                    238:        }
                    239:        return 0;
                    240: }
                    241:
                    242: static int
                    243: sshsig_check_hashalg(const char *hashalg)
                    244: {
1.2       djm       245:        if (hashalg == NULL ||
                    246:            match_pattern_list(hashalg, HASHALG_ALLOWED, 0) == 1)
1.1       djm       247:                return 0;
                    248:        error("%s: unsupported hash algorithm \"%.100s\"", __func__, hashalg);
                    249:        return SSH_ERR_SIGN_ALG_UNSUPPORTED;
                    250: }
                    251:
                    252: static int
                    253: sshsig_peek_hashalg(struct sshbuf *signature, char **hashalgp)
                    254: {
                    255:        struct sshbuf *buf = NULL;
                    256:        char *hashalg = NULL;
                    257:        int r = SSH_ERR_INTERNAL_ERROR;
                    258:
                    259:        if (hashalgp != NULL)
                    260:                *hashalgp = NULL;
                    261:        if ((buf = sshbuf_fromb(signature)) == NULL)
                    262:                return SSH_ERR_ALLOC_FAIL;
                    263:        if ((r = sshsig_parse_preamble(buf)) != 0)
                    264:                goto done;
                    265:        if ((r = sshbuf_get_string_direct(buf, NULL, NULL)) != 0 ||
                    266:            (r = sshbuf_get_string_direct(buf, NULL, NULL)) != 0 ||
                    267:            (r = sshbuf_get_string(buf, NULL, NULL)) != 0 ||
                    268:            (r = sshbuf_get_cstring(buf, &hashalg, NULL)) != 0 ||
                    269:            (r = sshbuf_get_string_direct(buf, NULL, NULL)) != 0) {
                    270:                error("Couldn't parse signature blob: %s", ssh_err(r));
                    271:                goto done;
                    272:        }
                    273:
                    274:        /* success */
                    275:        r = 0;
                    276:        *hashalgp = hashalg;
                    277:        hashalg = NULL;
                    278:  done:
                    279:        free(hashalg);
                    280:        sshbuf_free(buf);
                    281:        return r;
                    282: }
                    283:
                    284: static int
                    285: sshsig_wrap_verify(struct sshbuf *signature, const char *hashalg,
                    286:     const struct sshbuf *h_message, const char *expect_namespace,
                    287:     struct sshkey **sign_keyp)
                    288: {
                    289:        int r = SSH_ERR_INTERNAL_ERROR;
                    290:        struct sshbuf *buf = NULL, *toverify = NULL;
                    291:        struct sshkey *key = NULL;
                    292:        const u_char *sig;
                    293:        char *got_namespace = NULL, *sigtype = NULL, *sig_hashalg = NULL;
                    294:        size_t siglen;
                    295:
1.2       djm       296:        debug("%s: verify message length %zu", __func__, sshbuf_len(h_message));
1.1       djm       297:        if (sign_keyp != NULL)
                    298:                *sign_keyp = NULL;
                    299:
                    300:        if ((toverify = sshbuf_new()) == NULL) {
                    301:                error("%s: sshbuf_new failed", __func__);
                    302:                r = SSH_ERR_ALLOC_FAIL;
                    303:                goto done;
                    304:        }
                    305:        if ((r = sshbuf_put(toverify, MAGIC_PREAMBLE,
                    306:            MAGIC_PREAMBLE_LEN)) != 0 ||
                    307:            (r = sshbuf_put_cstring(toverify, expect_namespace)) != 0 ||
                    308:            (r = sshbuf_put_string(toverify, NULL, 0)) != 0 || /* reserved */
                    309:            (r = sshbuf_put_cstring(toverify, hashalg)) != 0 ||
1.6       djm       310:            (r = sshbuf_put_stringb(toverify, h_message)) != 0) {
1.1       djm       311:                error("Couldn't construct message to verify: %s", ssh_err(r));
                    312:                goto done;
                    313:        }
                    314:
                    315:        if ((r = sshsig_parse_preamble(signature)) != 0)
                    316:                goto done;
                    317:
                    318:        if ((r = sshkey_froms(signature, &key)) != 0 ||
                    319:            (r = sshbuf_get_cstring(signature, &got_namespace, NULL)) != 0 ||
                    320:            (r = sshbuf_get_string(signature, NULL, NULL)) != 0 ||
                    321:            (r = sshbuf_get_cstring(signature, &sig_hashalg, NULL)) != 0 ||
                    322:            (r = sshbuf_get_string_direct(signature, &sig, &siglen)) != 0) {
                    323:                error("Couldn't parse signature blob: %s", ssh_err(r));
                    324:                goto done;
                    325:        }
                    326:
                    327:        if (sshbuf_len(signature) != 0) {
                    328:                error("Signature contains trailing data");
                    329:                r = SSH_ERR_INVALID_FORMAT;
                    330:                goto done;
                    331:        }
                    332:
                    333:        if (strcmp(expect_namespace, got_namespace) != 0) {
                    334:                error("Couldn't verify signature: namespace does not match");
                    335:                debug("%s: expected namespace \"%s\" received \"%s\"",
                    336:                    __func__, expect_namespace, got_namespace);
                    337:                r = SSH_ERR_SIGNATURE_INVALID;
                    338:                goto done;
                    339:        }
                    340:        if (strcmp(hashalg, sig_hashalg) != 0) {
                    341:                error("Couldn't verify signature: hash algorithm mismatch");
                    342:                debug("%s: expected algorithm \"%s\" received \"%s\"",
                    343:                    __func__, hashalg, sig_hashalg);
                    344:                r = SSH_ERR_SIGNATURE_INVALID;
                    345:                goto done;
                    346:        }
                    347:        /* Ensure that RSA keys use an acceptable signature algorithm */
                    348:        if (sshkey_type_plain(key->type) == KEY_RSA) {
                    349:                if ((r = sshkey_get_sigtype(sig, siglen, &sigtype)) != 0) {
                    350:                        error("Couldn't verify signature: unable to get "
                    351:                            "signature type: %s", ssh_err(r));
                    352:                        goto done;
                    353:                }
                    354:                if (match_pattern_list(sigtype, RSA_SIGN_ALLOWED, 0) != 1) {
                    355:                        error("Couldn't verify signature: unsupported RSA "
                    356:                            "signature algorithm %s", sigtype);
                    357:                        r = SSH_ERR_SIGN_ALG_UNSUPPORTED;
                    358:                        goto done;
                    359:                }
                    360:        }
                    361:        if ((r = sshkey_verify(key, sig, siglen, sshbuf_ptr(toverify),
                    362:            sshbuf_len(toverify), NULL, 0)) != 0) {
                    363:                error("Signature verification failed: %s", ssh_err(r));
                    364:                goto done;
                    365:        }
                    366:
                    367:        /* success */
                    368:        r = 0;
                    369:        if (sign_keyp != NULL) {
                    370:                *sign_keyp = key;
                    371:                key = NULL; /* transferred */
                    372:        }
                    373: done:
                    374:        free(got_namespace);
                    375:        free(sigtype);
                    376:        free(sig_hashalg);
                    377:        sshbuf_free(buf);
                    378:        sshbuf_free(toverify);
                    379:        sshkey_free(key);
                    380:        return r;
                    381: }
                    382:
1.2       djm       383: static int
                    384: hash_buffer(const struct sshbuf *m, const char *hashalg, struct sshbuf **bp)
1.1       djm       385: {
1.2       djm       386:        char *hex, hash[SSH_DIGEST_MAX_LENGTH];
                    387:        int alg, r = SSH_ERR_INTERNAL_ERROR;
1.1       djm       388:        struct sshbuf *b = NULL;
                    389:
1.2       djm       390:        *bp = NULL;
                    391:        memset(hash, 0, sizeof(hash));
1.1       djm       392:
                    393:        if ((r = sshsig_check_hashalg(hashalg)) != 0)
                    394:                return r;
                    395:        if ((alg = ssh_digest_alg_by_name(hashalg)) == -1) {
                    396:                error("%s: can't look up hash algorithm %s",
1.2       djm       397:                    __func__, hashalg);
1.1       djm       398:                return SSH_ERR_INTERNAL_ERROR;
                    399:        }
1.2       djm       400:        if ((r = ssh_digest_buffer(alg, m, hash, sizeof(hash))) != 0) {
1.1       djm       401:                error("%s: ssh_digest_buffer failed: %s", __func__, ssh_err(r));
                    402:                return r;
                    403:        }
1.2       djm       404:        if ((hex = tohex(hash, ssh_digest_bytes(alg))) != NULL) {
                    405:                debug3("%s: final hash: %s", __func__, hex);
                    406:                freezero(hex, strlen(hex));
                    407:        }
                    408:        if ((b = sshbuf_new()) == NULL) {
1.1       djm       409:                r = SSH_ERR_ALLOC_FAIL;
                    410:                goto out;
                    411:        }
1.2       djm       412:        if ((r = sshbuf_put(b, hash, ssh_digest_bytes(alg))) != 0) {
                    413:                error("%s: sshbuf_put: %s", __func__, ssh_err(r));
                    414:                goto out;
                    415:        }
                    416:        *bp = b;
                    417:        b = NULL; /* transferred */
                    418:        /* success */
                    419:        r = 0;
                    420:  out:
                    421:        sshbuf_free(b);
                    422:        explicit_bzero(hash, sizeof(hash));
                    423:        return 0;
                    424: }
                    425:
                    426: int
1.7     ! djm       427: sshsig_signb(struct sshkey *key, const char *hashalg, const char *sk_provider,
1.2       djm       428:     const struct sshbuf *message, const char *sig_namespace,
                    429:     struct sshbuf **out, sshsig_signer *signer, void *signer_ctx)
                    430: {
                    431:        struct sshbuf *b = NULL;
                    432:        int r = SSH_ERR_INTERNAL_ERROR;
                    433:
                    434:        if (hashalg == NULL)
                    435:                hashalg = HASHALG_DEFAULT;
                    436:        if (out != NULL)
                    437:                *out = NULL;
                    438:        if ((r = hash_buffer(message, hashalg, &b)) != 0) {
                    439:                error("%s: hash_buffer failed: %s", __func__, ssh_err(r));
                    440:                goto out;
                    441:        }
1.7     ! djm       442:        if ((r = sshsig_wrap_sign(key, hashalg, sk_provider, b,
        !           443:            sig_namespace, out, signer, signer_ctx)) != 0)
1.1       djm       444:                goto out;
                    445:        /* success */
                    446:        r = 0;
                    447:  out:
                    448:        sshbuf_free(b);
                    449:        return r;
                    450: }
                    451:
                    452: int
1.2       djm       453: sshsig_verifyb(struct sshbuf *signature, const struct sshbuf *message,
1.1       djm       454:     const char *expect_namespace, struct sshkey **sign_keyp)
                    455: {
                    456:        struct sshbuf *b = NULL;
1.2       djm       457:        int r = SSH_ERR_INTERNAL_ERROR;
1.1       djm       458:        char *hashalg = NULL;
                    459:
                    460:        if (sign_keyp != NULL)
                    461:                *sign_keyp = NULL;
                    462:
                    463:        if ((r = sshsig_peek_hashalg(signature, &hashalg)) != 0)
                    464:                return r;
1.2       djm       465:        debug("%s: signature made with hash \"%s\"", __func__, hashalg);
                    466:        if ((r = hash_buffer(message, hashalg, &b)) != 0) {
                    467:                error("%s: hash_buffer failed: %s", __func__, ssh_err(r));
1.1       djm       468:                goto out;
                    469:        }
                    470:        if ((r = sshsig_wrap_verify(signature, hashalg, b, expect_namespace,
                    471:            sign_keyp)) != 0)
                    472:                goto out;
                    473:        /* success */
                    474:        r = 0;
                    475:  out:
                    476:        sshbuf_free(b);
                    477:        free(hashalg);
                    478:        return r;
                    479: }
                    480:
                    481: static int
1.2       djm       482: hash_file(int fd, const char *hashalg, struct sshbuf **bp)
1.1       djm       483: {
1.2       djm       484:        char *hex, rbuf[8192], hash[SSH_DIGEST_MAX_LENGTH];
1.1       djm       485:        ssize_t n, total = 0;
                    486:        struct ssh_digest_ctx *ctx;
1.2       djm       487:        int alg, oerrno, r = SSH_ERR_INTERNAL_ERROR;
                    488:        struct sshbuf *b = NULL;
                    489:
                    490:        *bp = NULL;
                    491:        memset(hash, 0, sizeof(hash));
1.1       djm       492:
1.2       djm       493:        if ((r = sshsig_check_hashalg(hashalg)) != 0)
                    494:                return r;
                    495:        if ((alg = ssh_digest_alg_by_name(hashalg)) == -1) {
                    496:                error("%s: can't look up hash algorithm %s",
                    497:                    __func__, hashalg);
                    498:                return SSH_ERR_INTERNAL_ERROR;
                    499:        }
                    500:        if ((ctx = ssh_digest_start(alg)) == NULL) {
1.1       djm       501:                error("%s: ssh_digest_start failed", __func__);
                    502:                return SSH_ERR_INTERNAL_ERROR;
                    503:        }
                    504:        for (;;) {
                    505:                if ((n = read(fd, rbuf, sizeof(rbuf))) == -1) {
                    506:                        if (errno == EINTR || errno == EAGAIN)
                    507:                                continue;
                    508:                        oerrno = errno;
                    509:                        error("%s: read: %s", __func__, strerror(errno));
                    510:                        ssh_digest_free(ctx);
                    511:                        errno = oerrno;
1.2       djm       512:                        r = SSH_ERR_SYSTEM_ERROR;
                    513:                        goto out;
1.1       djm       514:                } else if (n == 0) {
                    515:                        debug2("%s: hashed %zu bytes", __func__, total);
                    516:                        break; /* EOF */
                    517:                }
                    518:                total += (size_t)n;
                    519:                if ((r = ssh_digest_update(ctx, rbuf, (size_t)n)) != 0) {
                    520:                        error("%s: ssh_digest_update: %s",
                    521:                            __func__, ssh_err(r));
1.2       djm       522:                        goto out;
1.1       djm       523:                }
                    524:        }
1.2       djm       525:        if ((r = ssh_digest_final(ctx, hash, sizeof(hash))) != 0) {
1.1       djm       526:                error("%s: ssh_digest_final: %s", __func__, ssh_err(r));
1.2       djm       527:                goto out;
1.1       djm       528:        }
1.2       djm       529:        if ((hex = tohex(hash, ssh_digest_bytes(alg))) != NULL) {
1.1       djm       530:                debug3("%s: final hash: %s", __func__, hex);
                    531:                freezero(hex, strlen(hex));
                    532:        }
1.2       djm       533:        if ((b = sshbuf_new()) == NULL) {
                    534:                r = SSH_ERR_ALLOC_FAIL;
                    535:                goto out;
                    536:        }
                    537:        if ((r = sshbuf_put(b, hash, ssh_digest_bytes(alg))) != 0) {
                    538:                error("%s: sshbuf_put: %s", __func__, ssh_err(r));
                    539:                goto out;
                    540:        }
                    541:        *bp = b;
                    542:        b = NULL; /* transferred */
1.1       djm       543:        /* success */
1.2       djm       544:        r = 0;
                    545:  out:
                    546:        sshbuf_free(b);
1.1       djm       547:        ssh_digest_free(ctx);
1.2       djm       548:        explicit_bzero(hash, sizeof(hash));
1.1       djm       549:        return 0;
                    550: }
                    551:
                    552: int
1.7     ! djm       553: sshsig_sign_fd(struct sshkey *key, const char *hashalg, const char *sk_provider,
1.1       djm       554:     int fd, const char *sig_namespace, struct sshbuf **out,
                    555:     sshsig_signer *signer, void *signer_ctx)
                    556: {
                    557:        struct sshbuf *b = NULL;
1.2       djm       558:        int r = SSH_ERR_INTERNAL_ERROR;
1.1       djm       559:
1.2       djm       560:        if (hashalg == NULL)
                    561:                hashalg = HASHALG_DEFAULT;
1.1       djm       562:        if (out != NULL)
                    563:                *out = NULL;
1.2       djm       564:        if ((r = hash_file(fd, hashalg, &b)) != 0) {
1.1       djm       565:                error("%s: hash_file failed: %s", __func__, ssh_err(r));
                    566:                return r;
                    567:        }
1.7     ! djm       568:        if ((r = sshsig_wrap_sign(key, hashalg, sk_provider, b,
        !           569:            sig_namespace, out, signer, signer_ctx)) != 0)
1.1       djm       570:                goto out;
                    571:        /* success */
                    572:        r = 0;
                    573:  out:
                    574:        sshbuf_free(b);
                    575:        return r;
                    576: }
                    577:
                    578: int
                    579: sshsig_verify_fd(struct sshbuf *signature, int fd,
                    580:     const char *expect_namespace, struct sshkey **sign_keyp)
                    581: {
                    582:        struct sshbuf *b = NULL;
1.2       djm       583:        int r = SSH_ERR_INTERNAL_ERROR;
1.1       djm       584:        char *hashalg = NULL;
                    585:
                    586:        if (sign_keyp != NULL)
                    587:                *sign_keyp = NULL;
                    588:
                    589:        if ((r = sshsig_peek_hashalg(signature, &hashalg)) != 0)
                    590:                return r;
1.2       djm       591:        debug("%s: signature made with hash \"%s\"", __func__, hashalg);
                    592:        if ((r = hash_file(fd, hashalg, &b)) != 0) {
1.1       djm       593:                error("%s: hash_file failed: %s", __func__, ssh_err(r));
                    594:                goto out;
                    595:        }
                    596:        if ((r = sshsig_wrap_verify(signature, hashalg, b, expect_namespace,
                    597:            sign_keyp)) != 0)
                    598:                goto out;
                    599:        /* success */
                    600:        r = 0;
                    601:  out:
                    602:        sshbuf_free(b);
                    603:        free(hashalg);
                    604:        return r;
                    605: }
                    606:
1.4       djm       607: struct sshsigopt {
1.1       djm       608:        int ca;
                    609:        char *namespaces;
                    610: };
                    611:
1.4       djm       612: struct sshsigopt *
                    613: sshsigopt_parse(const char *opts, const char *path, u_long linenum,
1.1       djm       614:     const char **errstrp)
                    615: {
1.4       djm       616:        struct sshsigopt *ret;
1.1       djm       617:        int r;
                    618:        const char *errstr = NULL;
                    619:
                    620:        if ((ret = calloc(1, sizeof(*ret))) == NULL)
                    621:                return NULL;
                    622:        if (opts == NULL || *opts == '\0')
                    623:                return ret; /* Empty options yields empty options :) */
                    624:
                    625:        while (*opts && *opts != ' ' && *opts != '\t') {
                    626:                /* flag options */
                    627:                if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
                    628:                        ret->ca = 1;
                    629:                } else if (opt_match(&opts, "namespaces")) {
                    630:                        if (ret->namespaces != NULL) {
                    631:                                errstr = "multiple \"namespaces\" clauses";
                    632:                                goto fail;
                    633:                        }
                    634:                        ret->namespaces = opt_dequote(&opts, &errstr);
                    635:                        if (ret->namespaces == NULL)
                    636:                                goto fail;
                    637:                }
                    638:                /*
                    639:                 * Skip the comma, and move to the next option
                    640:                 * (or break out if there are no more).
                    641:                 */
                    642:                if (*opts == '\0' || *opts == ' ' || *opts == '\t')
                    643:                        break;          /* End of options. */
                    644:                /* Anything other than a comma is an unknown option */
                    645:                if (*opts != ',') {
                    646:                        errstr = "unknown key option";
                    647:                        goto fail;
                    648:                }
                    649:                opts++;
                    650:                if (*opts == '\0') {
                    651:                        errstr = "unexpected end-of-options";
                    652:                        goto fail;
                    653:                }
                    654:        }
                    655:        /* success */
                    656:        return ret;
                    657:  fail:
                    658:        if (errstrp != NULL)
                    659:                *errstrp = errstr;
1.5       djm       660:        sshsigopt_free(ret);
1.1       djm       661:        return NULL;
                    662: }
                    663:
1.4       djm       664: void
                    665: sshsigopt_free(struct sshsigopt *opts)
1.1       djm       666: {
                    667:        if (opts == NULL)
                    668:                return;
                    669:        free(opts->namespaces);
                    670:        free(opts);
                    671: }
                    672:
                    673: static int
                    674: check_allowed_keys_line(const char *path, u_long linenum, char *line,
                    675:     const struct sshkey *sign_key, const char *principal,
                    676:     const char *sig_namespace)
                    677: {
                    678:        struct sshkey *found_key = NULL;
                    679:        char *cp, *opts = NULL, *identities = NULL;
                    680:        int r, found = 0;
                    681:        const char *reason = NULL;
1.4       djm       682:        struct sshsigopt *sigopts = NULL;
1.1       djm       683:
                    684:        if ((found_key = sshkey_new(KEY_UNSPEC)) == NULL) {
                    685:                error("%s: sshkey_new failed", __func__);
                    686:                return SSH_ERR_ALLOC_FAIL;
                    687:        }
                    688:
                    689:        /* format: identity[,identity...] [option[,option...]] key */
                    690:        cp = line;
                    691:        cp = cp + strspn(cp, " \t"); /* skip leading whitespace */
                    692:        if (*cp == '#' || *cp == '\0')
                    693:                goto done;
                    694:        if ((identities = strdelimw(&cp)) == NULL) {
                    695:                error("%s:%lu: invalid line", path, linenum);
                    696:                goto done;
                    697:        }
                    698:        if (match_pattern_list(principal, identities, 0) != 1) {
                    699:                /* principal didn't match */
                    700:                goto done;
                    701:        }
                    702:        debug("%s: %s:%lu: matched principal \"%s\"",
                    703:            __func__, path, linenum, principal);
                    704:
                    705:        if (sshkey_read(found_key, &cp) != 0) {
                    706:                /* no key? Check for options */
                    707:                opts = cp;
                    708:                if (sshkey_advance_past_options(&cp) != 0) {
                    709:                        error("%s:%lu: invalid options",
                    710:                            path, linenum);
                    711:                        goto done;
                    712:                }
                    713:                *cp++ = '\0';
                    714:                skip_space(&cp);
                    715:                if (sshkey_read(found_key, &cp) != 0) {
                    716:                        error("%s:%lu: invalid key", path,
                    717:                            linenum);
                    718:                        goto done;
                    719:                }
                    720:        }
                    721:        debug3("%s:%lu: options %s", path, linenum, opts == NULL ? "" : opts);
1.4       djm       722:        if ((sigopts = sshsigopt_parse(opts, path, linenum, &reason)) == NULL) {
1.1       djm       723:                error("%s:%lu: bad options: %s", path, linenum, reason);
                    724:                goto done;
                    725:        }
                    726:
                    727:        /* Check whether options preclude the use of this key */
                    728:        if (sigopts->namespaces != NULL &&
                    729:            match_pattern_list(sig_namespace, sigopts->namespaces, 0) != 1) {
                    730:                error("%s:%lu: key is not permitted for use in signature "
                    731:                    "namespace \"%s\"", path, linenum, sig_namespace);
                    732:                goto done;
                    733:        }
                    734:
                    735:        if (!sigopts->ca && sshkey_equal(found_key, sign_key)) {
                    736:                /* Exact match of key */
                    737:                debug("%s:%lu: matched key and principal", path, linenum);
                    738:                /* success */
                    739:                found = 1;
                    740:        } else if (sigopts->ca && sshkey_is_cert(sign_key) &&
                    741:            sshkey_equal_public(sign_key->cert->signature_key, found_key)) {
                    742:                /* Match of certificate's CA key */
                    743:                if ((r = sshkey_cert_check_authority(sign_key, 0, 1,
                    744:                    principal, &reason)) != 0) {
                    745:                        error("%s:%lu: certificate not authorized: %s",
                    746:                            path, linenum, reason);
                    747:                        goto done;
                    748:                }
                    749:                debug("%s:%lu: matched certificate CA key", path, linenum);
                    750:                /* success */
                    751:                found = 1;
                    752:        } else {
                    753:                /* Principal matched but key didn't */
                    754:                goto done;
                    755:        }
                    756:  done:
                    757:        sshkey_free(found_key);
1.4       djm       758:        sshsigopt_free(sigopts);
1.1       djm       759:        return found ? 0 : SSH_ERR_KEY_NOT_FOUND;
                    760: }
                    761:
                    762: int
                    763: sshsig_check_allowed_keys(const char *path, const struct sshkey *sign_key,
                    764:     const char *principal, const char *sig_namespace)
                    765: {
                    766:        FILE *f = NULL;
                    767:        char *line = NULL;
                    768:        size_t linesize = 0;
                    769:        u_long linenum = 0;
                    770:        int r, oerrno;
                    771:
                    772:        /* Check key and principal against file */
                    773:        if ((f = fopen(path, "r")) == NULL) {
                    774:                oerrno = errno;
                    775:                error("Unable to open allowed keys file \"%s\": %s",
                    776:                    path, strerror(errno));
                    777:                errno = oerrno;
                    778:                return SSH_ERR_SYSTEM_ERROR;
                    779:        }
                    780:
                    781:        while (getline(&line, &linesize, f) != -1) {
                    782:                linenum++;
                    783:                r = check_allowed_keys_line(path, linenum, line, sign_key,
                    784:                    principal, sig_namespace);
1.2       djm       785:                free(line);
                    786:                line = NULL;
1.1       djm       787:                if (r == SSH_ERR_KEY_NOT_FOUND)
                    788:                        continue;
                    789:                else if (r == 0) {
                    790:                        /* success */
                    791:                        fclose(f);
                    792:                        return 0;
                    793:                } else
                    794:                        break;
                    795:        }
                    796:        /* Either we hit an error parsing or we simply didn't find the key */
                    797:        fclose(f);
                    798:        free(line);
                    799:        return r == 0 ? SSH_ERR_KEY_NOT_FOUND : r;
                    800: }