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

Annotation of src/usr.bin/ssh/ssh-ecdsa-sk.c, Revision 1.10

1.10    ! djm         1: /* $OpenBSD: ssh-ecdsa-sk.c,v 1.9 2022/10/28 00:35:40 djm Exp $ */
1.1       djm         2: /*
                      3:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
                      4:  * Copyright (c) 2010 Damien Miller.  All rights reserved.
                      5:  * Copyright (c) 2019 Google Inc.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     17:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     18:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     19:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     20:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     21:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     22:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     23:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     24:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     25:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     26:  */
                     27:
                     28: /* #define DEBUG_SK 1 */
                     29:
                     30: #include <sys/types.h>
                     31:
                     32: #include <openssl/bn.h>
                     33: #include <openssl/ec.h>
                     34: #include <openssl/ecdsa.h>
                     35: #include <openssl/evp.h>
                     36:
                     37: #include <string.h>
                     38: #include <stdio.h> /* needed for DEBUG_SK only */
                     39:
                     40: #include "sshbuf.h"
                     41: #include "ssherr.h"
                     42: #include "digest.h"
                     43: #define SSHKEY_INTERNAL
                     44: #include "sshkey.h"
                     45:
1.10    ! djm        46: /* Reuse some ECDSA internals */
        !            47: extern struct sshkey_impl_funcs sshkey_ecdsa_funcs;
        !            48:
1.9       djm        49: static void
                     50: ssh_ecdsa_sk_cleanup(struct sshkey *k)
                     51: {
1.10    ! djm        52:        sshkey_sk_cleanup(k);
        !            53:        sshkey_ecdsa_funcs.cleanup(k);
        !            54: }
        !            55:
        !            56: static int
        !            57: ssh_ecdsa_sk_equal(const struct sshkey *a, const struct sshkey *b)
        !            58: {
        !            59:        if (!sshkey_sk_fields_equal(a, b))
        !            60:                return 0;
        !            61:        if (!sshkey_ecdsa_funcs.equal(a, b))
        !            62:                return 0;
        !            63:        return 1;
1.9       djm        64: }
                     65:
1.7       djm        66: /*
                     67:  * Check FIDO/W3C webauthn signatures clientData field against the expected
                     68:  * format and prepare a hash of it for use in signature verification.
                     69:  *
                     70:  * webauthn signatures do not sign the hash of the message directly, but
                     71:  * instead sign a JSON-like "clientData" wrapper structure that contains the
                     72:  * message hash along with a other information.
                     73:  *
                     74:  * Fortunately this structure has a fixed format so it is possible to verify
                     75:  * that the hash of the signed message is present within the clientData
                     76:  * structure without needing to implement any JSON parsing.
                     77:  */
                     78: static int
                     79: webauthn_check_prepare_hash(const u_char *data, size_t datalen,
                     80:     const char *origin, const struct sshbuf *wrapper,
                     81:     uint8_t flags, const struct sshbuf *extensions,
                     82:     u_char *msghash, size_t msghashlen)
                     83: {
                     84:        int r = SSH_ERR_INTERNAL_ERROR;
                     85:        struct sshbuf *chall = NULL, *m = NULL;
                     86:
                     87:        if ((m = sshbuf_new()) == NULL ||
                     88:            (chall = sshbuf_from(data, datalen)) == NULL) {
                     89:                r = SSH_ERR_ALLOC_FAIL;
                     90:                goto out;
                     91:        }
                     92:        /*
                     93:         * Ensure origin contains no quote character and that the flags are
                     94:         * consistent with what we received
                     95:         */
                     96:        if (strchr(origin, '\"') != NULL ||
                     97:            (flags & 0x40) != 0 /* AD */ ||
                     98:            ((flags & 0x80) == 0 /* ED */) != (sshbuf_len(extensions) == 0)) {
                     99:                r = SSH_ERR_INVALID_FORMAT;
                    100:                goto out;
                    101:        }
1.8       djm       102:
                    103:        /*
                    104:         * Prepare the preamble to clientData that we expect, poking the
                    105:         * challenge and origin into their canonical positions in the
                    106:         * structure. The crossOrigin flag and any additional extension
                    107:         * fields present are ignored.
                    108:         */
1.7       djm       109: #define WEBAUTHN_0     "{\"type\":\"webauthn.get\",\"challenge\":\""
                    110: #define WEBAUTHN_1     "\",\"origin\":\""
                    111: #define WEBAUTHN_2     "\""
                    112:        if ((r = sshbuf_put(m, WEBAUTHN_0, sizeof(WEBAUTHN_0) - 1)) != 0 ||
                    113:            (r = sshbuf_dtourlb64(chall, m, 0)) != 0 ||
                    114:            (r = sshbuf_put(m, WEBAUTHN_1, sizeof(WEBAUTHN_1) - 1)) != 0 ||
                    115:            (r = sshbuf_put(m, origin, strlen(origin))) != 0 ||
                    116:            (r = sshbuf_put(m, WEBAUTHN_2, sizeof(WEBAUTHN_2) - 1)) != 0)
                    117:                goto out;
                    118: #ifdef DEBUG_SK
                    119:        fprintf(stderr, "%s: received origin: %s\n", __func__, origin);
                    120:        fprintf(stderr, "%s: received clientData:\n", __func__);
                    121:        sshbuf_dump(wrapper, stderr);
                    122:        fprintf(stderr, "%s: expected clientData premable:\n", __func__);
                    123:        sshbuf_dump(m, stderr);
                    124: #endif
1.8       djm       125:        /* Check that the supplied clientData has the preamble we expect */
1.7       djm       126:        if ((r = sshbuf_cmp(wrapper, 0, sshbuf_ptr(m), sshbuf_len(m))) != 0)
                    127:                goto out;
                    128:
                    129:        /* Prepare hash of clientData */
                    130:        if ((r = ssh_digest_buffer(SSH_DIGEST_SHA256, wrapper,
                    131:            msghash, msghashlen)) != 0)
                    132:                goto out;
                    133:
                    134:        /* success */
                    135:        r = 0;
                    136:  out:
                    137:        sshbuf_free(chall);
                    138:        sshbuf_free(m);
                    139:        return r;
                    140: }
                    141:
1.1       djm       142: /* ARGSUSED */
                    143: int
                    144: ssh_ecdsa_sk_verify(const struct sshkey *key,
                    145:     const u_char *signature, size_t signaturelen,
1.4       djm       146:     const u_char *data, size_t datalen, u_int compat,
                    147:     struct sshkey_sig_details **detailsp)
1.1       djm       148: {
                    149:        ECDSA_SIG *sig = NULL;
                    150:        BIGNUM *sig_r = NULL, *sig_s = NULL;
                    151:        u_char sig_flags;
                    152:        u_char msghash[32], apphash[32], sighash[32];
                    153:        u_int sig_counter;
1.7       djm       154:        int is_webauthn = 0, ret = SSH_ERR_INTERNAL_ERROR;
1.1       djm       155:        struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
1.7       djm       156:        struct sshbuf *webauthn_wrapper = NULL, *webauthn_exts = NULL;
                    157:        char *ktype = NULL, *webauthn_origin = NULL;
1.4       djm       158:        struct sshkey_sig_details *details = NULL;
1.1       djm       159: #ifdef DEBUG_SK
                    160:        char *tmp = NULL;
                    161: #endif
                    162:
1.4       djm       163:        if (detailsp != NULL)
                    164:                *detailsp = NULL;
1.1       djm       165:        if (key == NULL || key->ecdsa == NULL ||
                    166:            sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
                    167:            signature == NULL || signaturelen == 0)
                    168:                return SSH_ERR_INVALID_ARGUMENT;
                    169:
                    170:        if (key->ecdsa_nid != NID_X9_62_prime256v1)
                    171:                return SSH_ERR_INTERNAL_ERROR;
                    172:
                    173:        /* fetch signature */
                    174:        if ((b = sshbuf_from(signature, signaturelen)) == NULL)
                    175:                return SSH_ERR_ALLOC_FAIL;
1.6       djm       176:        if ((details = calloc(1, sizeof(*details))) == NULL) {
                    177:                ret = SSH_ERR_ALLOC_FAIL;
                    178:                goto out;
                    179:        }
                    180:        if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
                    181:                ret = SSH_ERR_INVALID_FORMAT;
                    182:                goto out;
                    183:        }
1.7       djm       184:        if (strcmp(ktype, "webauthn-sk-ecdsa-sha2-nistp256@openssh.com") == 0)
                    185:                is_webauthn = 1;
                    186:        else if (strcmp(ktype, "sk-ecdsa-sha2-nistp256@openssh.com") != 0) {
1.6       djm       187:                ret = SSH_ERR_INVALID_FORMAT;
                    188:                goto out;
                    189:        }
                    190:        if (sshbuf_froms(b, &sigbuf) != 0 ||
1.2       djm       191:            sshbuf_get_u8(b, &sig_flags) != 0 ||
                    192:            sshbuf_get_u32(b, &sig_counter) != 0) {
1.1       djm       193:                ret = SSH_ERR_INVALID_FORMAT;
                    194:                goto out;
                    195:        }
1.7       djm       196:        if (is_webauthn) {
                    197:                if (sshbuf_get_cstring(b, &webauthn_origin, NULL) != 0 ||
                    198:                    sshbuf_froms(b, &webauthn_wrapper) != 0 ||
                    199:                    sshbuf_froms(b, &webauthn_exts) != 0) {
                    200:                        ret = SSH_ERR_INVALID_FORMAT;
                    201:                        goto out;
                    202:                }
                    203:        }
1.1       djm       204:        if (sshbuf_len(b) != 0) {
                    205:                ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
                    206:                goto out;
                    207:        }
                    208:
                    209:        /* parse signature */
                    210:        if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
1.2       djm       211:            sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
1.1       djm       212:                ret = SSH_ERR_INVALID_FORMAT;
                    213:                goto out;
                    214:        }
1.6       djm       215:        if (sshbuf_len(sigbuf) != 0) {
                    216:                ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
1.1       djm       217:                goto out;
                    218:        }
1.7       djm       219:
1.1       djm       220: #ifdef DEBUG_SK
1.5       djm       221:        fprintf(stderr, "%s: data: (len %zu)\n", __func__, datalen);
                    222:        /* sshbuf_dump_data(data, datalen, stderr); */
1.1       djm       223:        fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
                    224:        free(tmp);
                    225:        fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
                    226:        free(tmp);
                    227:        fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
                    228:            __func__, sig_flags, sig_counter);
1.7       djm       229:        if (is_webauthn) {
                    230:                fprintf(stderr, "%s: webauthn origin: %s\n", __func__,
                    231:                    webauthn_origin);
                    232:                fprintf(stderr, "%s: webauthn_wrapper:\n", __func__);
                    233:                sshbuf_dump(webauthn_wrapper, stderr);
                    234:        }
1.1       djm       235: #endif
1.6       djm       236:        if ((sig = ECDSA_SIG_new()) == NULL) {
                    237:                ret = SSH_ERR_ALLOC_FAIL;
                    238:                goto out;
                    239:        }
                    240:        if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
                    241:                ret = SSH_ERR_LIBCRYPTO_ERROR;
1.1       djm       242:                goto out;
                    243:        }
1.6       djm       244:        sig_r = sig_s = NULL; /* transferred */
1.1       djm       245:
                    246:        /* Reconstruct data that was supposedly signed */
1.3       djm       247:        if ((original_signed = sshbuf_new()) == NULL) {
                    248:                ret = SSH_ERR_ALLOC_FAIL;
                    249:                goto out;
                    250:        }
1.7       djm       251:        if (is_webauthn) {
                    252:                if ((ret = webauthn_check_prepare_hash(data, datalen,
                    253:                    webauthn_origin, webauthn_wrapper, sig_flags, webauthn_exts,
                    254:                    msghash, sizeof(msghash))) != 0)
                    255:                        goto out;
                    256:        } else if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, datalen,
1.1       djm       257:            msghash, sizeof(msghash))) != 0)
                    258:                goto out;
                    259:        /* Application value is hashed before signature */
                    260:        if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
                    261:            strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
                    262:                goto out;
                    263: #ifdef DEBUG_SK
1.5       djm       264:        fprintf(stderr, "%s: hashed application:\n", __func__);
                    265:        sshbuf_dump_data(apphash, sizeof(apphash), stderr);
1.1       djm       266:        fprintf(stderr, "%s: hashed message:\n", __func__);
                    267:        sshbuf_dump_data(msghash, sizeof(msghash), stderr);
                    268: #endif
                    269:        if ((ret = sshbuf_put(original_signed,
                    270:            apphash, sizeof(apphash))) != 0 ||
                    271:            (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
                    272:            (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
1.7       djm       273:            (ret = sshbuf_putb(original_signed, webauthn_exts)) != 0 ||
1.1       djm       274:            (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
                    275:                goto out;
                    276:        /* Signature is over H(original_signed) */
                    277:        if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed,
                    278:            sighash, sizeof(sighash))) != 0)
                    279:                goto out;
1.4       djm       280:        details->sk_counter = sig_counter;
                    281:        details->sk_flags = sig_flags;
1.1       djm       282: #ifdef DEBUG_SK
                    283:        fprintf(stderr, "%s: signed buf:\n", __func__);
                    284:        sshbuf_dump(original_signed, stderr);
                    285:        fprintf(stderr, "%s: signed hash:\n", __func__);
                    286:        sshbuf_dump_data(sighash, sizeof(sighash), stderr);
                    287: #endif
                    288:
                    289:        /* Verify it */
                    290:        switch (ECDSA_do_verify(sighash, sizeof(sighash), sig, key->ecdsa)) {
                    291:        case 1:
                    292:                ret = 0;
                    293:                break;
                    294:        case 0:
                    295:                ret = SSH_ERR_SIGNATURE_INVALID;
                    296:                goto out;
                    297:        default:
                    298:                ret = SSH_ERR_LIBCRYPTO_ERROR;
                    299:                goto out;
                    300:        }
1.4       djm       301:        /* success */
                    302:        if (detailsp != NULL) {
                    303:                *detailsp = details;
                    304:                details = NULL;
                    305:        }
1.1       djm       306:  out:
                    307:        explicit_bzero(&sig_flags, sizeof(sig_flags));
                    308:        explicit_bzero(&sig_counter, sizeof(sig_counter));
                    309:        explicit_bzero(msghash, sizeof(msghash));
                    310:        explicit_bzero(sighash, sizeof(msghash));
                    311:        explicit_bzero(apphash, sizeof(apphash));
1.4       djm       312:        sshkey_sig_details_free(details);
1.7       djm       313:        sshbuf_free(webauthn_wrapper);
                    314:        sshbuf_free(webauthn_exts);
                    315:        free(webauthn_origin);
1.1       djm       316:        sshbuf_free(original_signed);
                    317:        sshbuf_free(sigbuf);
                    318:        sshbuf_free(b);
                    319:        ECDSA_SIG_free(sig);
                    320:        BN_clear_free(sig_r);
                    321:        BN_clear_free(sig_s);
                    322:        free(ktype);
                    323:        return ret;
                    324: }
1.9       djm       325:
                    326: static const struct sshkey_impl_funcs sshkey_ecdsa_sk_funcs = {
                    327:        /* .size = */           NULL,
                    328:        /* .alloc = */          NULL,
                    329:        /* .cleanup = */        ssh_ecdsa_sk_cleanup,
1.10    ! djm       330:        /* .equal = */          ssh_ecdsa_sk_equal,
1.9       djm       331: };
                    332:
                    333: const struct sshkey_impl sshkey_ecdsa_sk_impl = {
                    334:        /* .name = */           "sk-ecdsa-sha2-nistp256@openssh.com",
                    335:        /* .shortname = */      "ECDSA-SK",
                    336:        /* .sigalg = */         NULL,
                    337:        /* .type = */           KEY_ECDSA_SK,
                    338:        /* .nid = */            NID_X9_62_prime256v1,
                    339:        /* .cert = */           0,
                    340:        /* .sigonly = */        0,
                    341:        /* .keybits = */        256,
                    342:        /* .funcs = */          &sshkey_ecdsa_sk_funcs,
                    343: };
                    344:
                    345: const struct sshkey_impl sshkey_ecdsa_sk_cert_impl = {
                    346:        /* .name = */           "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com",
                    347:        /* .shortname = */      "ECDSA-SK-CERT",
                    348:        /* .sigalg = */         NULL,
                    349:        /* .type = */           KEY_ECDSA_SK_CERT,
                    350:        /* .nid = */            NID_X9_62_prime256v1,
                    351:        /* .cert = */           1,
                    352:        /* .sigonly = */        0,
                    353:        /* .keybits = */        256,
                    354:        /* .funcs = */          &sshkey_ecdsa_sk_funcs,
                    355: };
                    356:
                    357: const struct sshkey_impl sshkey_ecdsa_sk_webauthn_impl = {
                    358:        /* .name = */           "webauthn-sk-ecdsa-sha2-nistp256@openssh.com",
                    359:        /* .shortname = */      "ECDSA-SK",
                    360:        /* .sigalg = */         NULL,
                    361:        /* .type = */           KEY_ECDSA_SK,
                    362:        /* .nid = */            NID_X9_62_prime256v1,
                    363:        /* .cert = */           0,
                    364:        /* .sigonly = */        1,
                    365:        /* .keybits = */        256,
                    366:        /* .funcs = */          &sshkey_ecdsa_sk_funcs,
                    367: };