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

1.15    ! djm         1: /* $OpenBSD: ssh-ecdsa-sk.c,v 1.14 2022/10/28 00:41:52 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.11      djm        66: static int
                     67: ssh_ecdsa_sk_serialize_public(const struct sshkey *key, struct sshbuf *b,
1.14      djm        68:     enum sshkey_serialize_rep opts)
1.11      djm        69: {
                     70:        int r;
                     71:
1.14      djm        72:        if ((r = sshkey_ecdsa_funcs.serialize_public(key, b, opts)) != 0)
1.11      djm        73:                return r;
                     74:        if ((r = sshkey_serialize_sk(key, b)) != 0)
                     75:                return r;
                     76:
                     77:        return 0;
                     78: }
                     79:
1.13      djm        80: static int
                     81: ssh_ecdsa_sk_copy_public(const struct sshkey *from, struct sshkey *to)
                     82: {
                     83:        int r;
                     84:
                     85:        if ((r = sshkey_ecdsa_funcs.copy_public(from, to)) != 0)
                     86:                return r;
                     87:        if ((r = sshkey_copy_public_sk(from, to)) != 0)
                     88:                return r;
                     89:        return 0;
                     90: }
                     91:
1.14      djm        92: static int
                     93: ssh_ecdsa_sk_deserialize_public(const char *ktype, struct sshbuf *b,
                     94:     struct sshkey *key)
                     95: {
                     96:        int r;
                     97:
                     98:        if ((r = sshkey_ecdsa_funcs.deserialize_public(ktype, b, key)) != 0)
                     99:                return r;
                    100:        if ((r = sshkey_deserialize_sk(b, key)) != 0)
                    101:                return r;
                    102:        return 0;
                    103: }
                    104:
1.7       djm       105: /*
                    106:  * Check FIDO/W3C webauthn signatures clientData field against the expected
                    107:  * format and prepare a hash of it for use in signature verification.
                    108:  *
                    109:  * webauthn signatures do not sign the hash of the message directly, but
                    110:  * instead sign a JSON-like "clientData" wrapper structure that contains the
                    111:  * message hash along with a other information.
                    112:  *
                    113:  * Fortunately this structure has a fixed format so it is possible to verify
                    114:  * that the hash of the signed message is present within the clientData
                    115:  * structure without needing to implement any JSON parsing.
                    116:  */
                    117: static int
                    118: webauthn_check_prepare_hash(const u_char *data, size_t datalen,
                    119:     const char *origin, const struct sshbuf *wrapper,
                    120:     uint8_t flags, const struct sshbuf *extensions,
                    121:     u_char *msghash, size_t msghashlen)
                    122: {
                    123:        int r = SSH_ERR_INTERNAL_ERROR;
                    124:        struct sshbuf *chall = NULL, *m = NULL;
                    125:
                    126:        if ((m = sshbuf_new()) == NULL ||
                    127:            (chall = sshbuf_from(data, datalen)) == NULL) {
                    128:                r = SSH_ERR_ALLOC_FAIL;
                    129:                goto out;
                    130:        }
                    131:        /*
                    132:         * Ensure origin contains no quote character and that the flags are
                    133:         * consistent with what we received
                    134:         */
                    135:        if (strchr(origin, '\"') != NULL ||
                    136:            (flags & 0x40) != 0 /* AD */ ||
                    137:            ((flags & 0x80) == 0 /* ED */) != (sshbuf_len(extensions) == 0)) {
                    138:                r = SSH_ERR_INVALID_FORMAT;
                    139:                goto out;
                    140:        }
1.8       djm       141:
                    142:        /*
                    143:         * Prepare the preamble to clientData that we expect, poking the
                    144:         * challenge and origin into their canonical positions in the
                    145:         * structure. The crossOrigin flag and any additional extension
                    146:         * fields present are ignored.
                    147:         */
1.7       djm       148: #define WEBAUTHN_0     "{\"type\":\"webauthn.get\",\"challenge\":\""
                    149: #define WEBAUTHN_1     "\",\"origin\":\""
                    150: #define WEBAUTHN_2     "\""
                    151:        if ((r = sshbuf_put(m, WEBAUTHN_0, sizeof(WEBAUTHN_0) - 1)) != 0 ||
                    152:            (r = sshbuf_dtourlb64(chall, m, 0)) != 0 ||
                    153:            (r = sshbuf_put(m, WEBAUTHN_1, sizeof(WEBAUTHN_1) - 1)) != 0 ||
                    154:            (r = sshbuf_put(m, origin, strlen(origin))) != 0 ||
                    155:            (r = sshbuf_put(m, WEBAUTHN_2, sizeof(WEBAUTHN_2) - 1)) != 0)
                    156:                goto out;
                    157: #ifdef DEBUG_SK
                    158:        fprintf(stderr, "%s: received origin: %s\n", __func__, origin);
                    159:        fprintf(stderr, "%s: received clientData:\n", __func__);
                    160:        sshbuf_dump(wrapper, stderr);
                    161:        fprintf(stderr, "%s: expected clientData premable:\n", __func__);
                    162:        sshbuf_dump(m, stderr);
                    163: #endif
1.8       djm       164:        /* Check that the supplied clientData has the preamble we expect */
1.7       djm       165:        if ((r = sshbuf_cmp(wrapper, 0, sshbuf_ptr(m), sshbuf_len(m))) != 0)
                    166:                goto out;
                    167:
                    168:        /* Prepare hash of clientData */
                    169:        if ((r = ssh_digest_buffer(SSH_DIGEST_SHA256, wrapper,
                    170:            msghash, msghashlen)) != 0)
                    171:                goto out;
                    172:
                    173:        /* success */
                    174:        r = 0;
                    175:  out:
                    176:        sshbuf_free(chall);
                    177:        sshbuf_free(m);
                    178:        return r;
                    179: }
                    180:
1.1       djm       181: /* ARGSUSED */
1.15    ! djm       182: static int
1.1       djm       183: ssh_ecdsa_sk_verify(const struct sshkey *key,
1.15    ! djm       184:     const u_char *sig, size_t siglen,
        !           185:     const u_char *data, size_t dlen, const char *alg, u_int compat,
1.4       djm       186:     struct sshkey_sig_details **detailsp)
1.1       djm       187: {
1.15    ! djm       188:        ECDSA_SIG *esig = NULL;
1.1       djm       189:        BIGNUM *sig_r = NULL, *sig_s = NULL;
                    190:        u_char sig_flags;
                    191:        u_char msghash[32], apphash[32], sighash[32];
                    192:        u_int sig_counter;
1.7       djm       193:        int is_webauthn = 0, ret = SSH_ERR_INTERNAL_ERROR;
1.1       djm       194:        struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
1.7       djm       195:        struct sshbuf *webauthn_wrapper = NULL, *webauthn_exts = NULL;
                    196:        char *ktype = NULL, *webauthn_origin = NULL;
1.4       djm       197:        struct sshkey_sig_details *details = NULL;
1.1       djm       198: #ifdef DEBUG_SK
                    199:        char *tmp = NULL;
                    200: #endif
                    201:
1.4       djm       202:        if (detailsp != NULL)
                    203:                *detailsp = NULL;
1.1       djm       204:        if (key == NULL || key->ecdsa == NULL ||
                    205:            sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
1.15    ! djm       206:            sig == NULL || siglen == 0)
1.1       djm       207:                return SSH_ERR_INVALID_ARGUMENT;
                    208:
                    209:        if (key->ecdsa_nid != NID_X9_62_prime256v1)
                    210:                return SSH_ERR_INTERNAL_ERROR;
                    211:
                    212:        /* fetch signature */
1.15    ! djm       213:        if ((b = sshbuf_from(sig, siglen)) == NULL)
1.1       djm       214:                return SSH_ERR_ALLOC_FAIL;
1.6       djm       215:        if ((details = calloc(1, sizeof(*details))) == NULL) {
                    216:                ret = SSH_ERR_ALLOC_FAIL;
                    217:                goto out;
                    218:        }
                    219:        if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
                    220:                ret = SSH_ERR_INVALID_FORMAT;
                    221:                goto out;
                    222:        }
1.7       djm       223:        if (strcmp(ktype, "webauthn-sk-ecdsa-sha2-nistp256@openssh.com") == 0)
                    224:                is_webauthn = 1;
                    225:        else if (strcmp(ktype, "sk-ecdsa-sha2-nistp256@openssh.com") != 0) {
1.6       djm       226:                ret = SSH_ERR_INVALID_FORMAT;
                    227:                goto out;
                    228:        }
                    229:        if (sshbuf_froms(b, &sigbuf) != 0 ||
1.2       djm       230:            sshbuf_get_u8(b, &sig_flags) != 0 ||
                    231:            sshbuf_get_u32(b, &sig_counter) != 0) {
1.1       djm       232:                ret = SSH_ERR_INVALID_FORMAT;
                    233:                goto out;
                    234:        }
1.7       djm       235:        if (is_webauthn) {
                    236:                if (sshbuf_get_cstring(b, &webauthn_origin, NULL) != 0 ||
                    237:                    sshbuf_froms(b, &webauthn_wrapper) != 0 ||
                    238:                    sshbuf_froms(b, &webauthn_exts) != 0) {
                    239:                        ret = SSH_ERR_INVALID_FORMAT;
                    240:                        goto out;
                    241:                }
                    242:        }
1.1       djm       243:        if (sshbuf_len(b) != 0) {
                    244:                ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
                    245:                goto out;
                    246:        }
                    247:
                    248:        /* parse signature */
                    249:        if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
1.2       djm       250:            sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
1.1       djm       251:                ret = SSH_ERR_INVALID_FORMAT;
                    252:                goto out;
                    253:        }
1.6       djm       254:        if (sshbuf_len(sigbuf) != 0) {
                    255:                ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
1.1       djm       256:                goto out;
                    257:        }
1.7       djm       258:
1.1       djm       259: #ifdef DEBUG_SK
1.5       djm       260:        fprintf(stderr, "%s: data: (len %zu)\n", __func__, datalen);
                    261:        /* sshbuf_dump_data(data, datalen, stderr); */
1.1       djm       262:        fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
                    263:        free(tmp);
                    264:        fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
                    265:        free(tmp);
                    266:        fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
                    267:            __func__, sig_flags, sig_counter);
1.7       djm       268:        if (is_webauthn) {
                    269:                fprintf(stderr, "%s: webauthn origin: %s\n", __func__,
                    270:                    webauthn_origin);
                    271:                fprintf(stderr, "%s: webauthn_wrapper:\n", __func__);
                    272:                sshbuf_dump(webauthn_wrapper, stderr);
                    273:        }
1.1       djm       274: #endif
1.15    ! djm       275:        if ((esig = ECDSA_SIG_new()) == NULL) {
1.6       djm       276:                ret = SSH_ERR_ALLOC_FAIL;
                    277:                goto out;
                    278:        }
1.15    ! djm       279:        if (!ECDSA_SIG_set0(esig, sig_r, sig_s)) {
1.6       djm       280:                ret = SSH_ERR_LIBCRYPTO_ERROR;
1.1       djm       281:                goto out;
                    282:        }
1.6       djm       283:        sig_r = sig_s = NULL; /* transferred */
1.1       djm       284:
                    285:        /* Reconstruct data that was supposedly signed */
1.3       djm       286:        if ((original_signed = sshbuf_new()) == NULL) {
                    287:                ret = SSH_ERR_ALLOC_FAIL;
                    288:                goto out;
                    289:        }
1.7       djm       290:        if (is_webauthn) {
1.15    ! djm       291:                if ((ret = webauthn_check_prepare_hash(data, dlen,
1.7       djm       292:                    webauthn_origin, webauthn_wrapper, sig_flags, webauthn_exts,
                    293:                    msghash, sizeof(msghash))) != 0)
                    294:                        goto out;
1.15    ! djm       295:        } else if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, dlen,
1.1       djm       296:            msghash, sizeof(msghash))) != 0)
                    297:                goto out;
                    298:        /* Application value is hashed before signature */
                    299:        if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
                    300:            strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
                    301:                goto out;
                    302: #ifdef DEBUG_SK
1.5       djm       303:        fprintf(stderr, "%s: hashed application:\n", __func__);
                    304:        sshbuf_dump_data(apphash, sizeof(apphash), stderr);
1.1       djm       305:        fprintf(stderr, "%s: hashed message:\n", __func__);
                    306:        sshbuf_dump_data(msghash, sizeof(msghash), stderr);
                    307: #endif
                    308:        if ((ret = sshbuf_put(original_signed,
                    309:            apphash, sizeof(apphash))) != 0 ||
                    310:            (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
                    311:            (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
1.7       djm       312:            (ret = sshbuf_putb(original_signed, webauthn_exts)) != 0 ||
1.1       djm       313:            (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
                    314:                goto out;
                    315:        /* Signature is over H(original_signed) */
                    316:        if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed,
                    317:            sighash, sizeof(sighash))) != 0)
                    318:                goto out;
1.4       djm       319:        details->sk_counter = sig_counter;
                    320:        details->sk_flags = sig_flags;
1.1       djm       321: #ifdef DEBUG_SK
                    322:        fprintf(stderr, "%s: signed buf:\n", __func__);
                    323:        sshbuf_dump(original_signed, stderr);
                    324:        fprintf(stderr, "%s: signed hash:\n", __func__);
                    325:        sshbuf_dump_data(sighash, sizeof(sighash), stderr);
                    326: #endif
                    327:
                    328:        /* Verify it */
1.15    ! djm       329:        switch (ECDSA_do_verify(sighash, sizeof(sighash), esig, key->ecdsa)) {
1.1       djm       330:        case 1:
                    331:                ret = 0;
                    332:                break;
                    333:        case 0:
                    334:                ret = SSH_ERR_SIGNATURE_INVALID;
                    335:                goto out;
                    336:        default:
                    337:                ret = SSH_ERR_LIBCRYPTO_ERROR;
                    338:                goto out;
                    339:        }
1.4       djm       340:        /* success */
                    341:        if (detailsp != NULL) {
                    342:                *detailsp = details;
                    343:                details = NULL;
                    344:        }
1.1       djm       345:  out:
                    346:        explicit_bzero(&sig_flags, sizeof(sig_flags));
                    347:        explicit_bzero(&sig_counter, sizeof(sig_counter));
                    348:        explicit_bzero(msghash, sizeof(msghash));
                    349:        explicit_bzero(sighash, sizeof(msghash));
                    350:        explicit_bzero(apphash, sizeof(apphash));
1.4       djm       351:        sshkey_sig_details_free(details);
1.7       djm       352:        sshbuf_free(webauthn_wrapper);
                    353:        sshbuf_free(webauthn_exts);
                    354:        free(webauthn_origin);
1.1       djm       355:        sshbuf_free(original_signed);
                    356:        sshbuf_free(sigbuf);
                    357:        sshbuf_free(b);
1.15    ! djm       358:        ECDSA_SIG_free(esig);
1.1       djm       359:        BN_clear_free(sig_r);
                    360:        BN_clear_free(sig_s);
                    361:        free(ktype);
                    362:        return ret;
                    363: }
1.9       djm       364:
                    365: static const struct sshkey_impl_funcs sshkey_ecdsa_sk_funcs = {
                    366:        /* .size = */           NULL,
                    367:        /* .alloc = */          NULL,
                    368:        /* .cleanup = */        ssh_ecdsa_sk_cleanup,
1.10      djm       369:        /* .equal = */          ssh_ecdsa_sk_equal,
1.11      djm       370:        /* .ssh_serialize_public = */ ssh_ecdsa_sk_serialize_public,
1.14      djm       371:        /* .ssh_deserialize_public = */ ssh_ecdsa_sk_deserialize_public,
1.12      djm       372:        /* .generate = */       NULL,
1.13      djm       373:        /* .copy_public = */    ssh_ecdsa_sk_copy_public,
1.15    ! djm       374:        /* .sign = */           NULL,
        !           375:        /* .verify = */         ssh_ecdsa_sk_verify,
1.9       djm       376: };
                    377:
                    378: const struct sshkey_impl sshkey_ecdsa_sk_impl = {
                    379:        /* .name = */           "sk-ecdsa-sha2-nistp256@openssh.com",
                    380:        /* .shortname = */      "ECDSA-SK",
                    381:        /* .sigalg = */         NULL,
                    382:        /* .type = */           KEY_ECDSA_SK,
                    383:        /* .nid = */            NID_X9_62_prime256v1,
                    384:        /* .cert = */           0,
                    385:        /* .sigonly = */        0,
                    386:        /* .keybits = */        256,
                    387:        /* .funcs = */          &sshkey_ecdsa_sk_funcs,
                    388: };
                    389:
                    390: const struct sshkey_impl sshkey_ecdsa_sk_cert_impl = {
                    391:        /* .name = */           "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com",
                    392:        /* .shortname = */      "ECDSA-SK-CERT",
                    393:        /* .sigalg = */         NULL,
                    394:        /* .type = */           KEY_ECDSA_SK_CERT,
                    395:        /* .nid = */            NID_X9_62_prime256v1,
                    396:        /* .cert = */           1,
                    397:        /* .sigonly = */        0,
                    398:        /* .keybits = */        256,
                    399:        /* .funcs = */          &sshkey_ecdsa_sk_funcs,
                    400: };
                    401:
                    402: const struct sshkey_impl sshkey_ecdsa_sk_webauthn_impl = {
                    403:        /* .name = */           "webauthn-sk-ecdsa-sha2-nistp256@openssh.com",
                    404:        /* .shortname = */      "ECDSA-SK",
                    405:        /* .sigalg = */         NULL,
                    406:        /* .type = */           KEY_ECDSA_SK,
                    407:        /* .nid = */            NID_X9_62_prime256v1,
                    408:        /* .cert = */           0,
                    409:        /* .sigonly = */        1,
                    410:        /* .keybits = */        256,
                    411:        /* .funcs = */          &sshkey_ecdsa_sk_funcs,
                    412: };