Path: blob/main/contrib/libfido2/tools/cred_verify.c
39507 views
/*1* Copyright (c) 2018 Yubico AB. All rights reserved.2* Use of this source code is governed by a BSD-style3* license that can be found in the LICENSE file.4* SPDX-License-Identifier: BSD-2-Clause5*/67#include <fido.h>8#include <stdio.h>9#include <stdlib.h>10#include <string.h>11#ifdef HAVE_UNISTD_H12#include <unistd.h>13#endif1415#include "../openbsd-compat/openbsd-compat.h"16#include "extern.h"1718static fido_cred_t *19prepare_cred(FILE *in_f, int type, int flags)20{21fido_cred_t *cred = NULL;22struct blob cdh;23struct blob authdata;24struct blob id;25struct blob sig;26struct blob x5c;27char *rpid = NULL;28char *fmt = NULL;29int r;3031memset(&cdh, 0, sizeof(cdh));32memset(&authdata, 0, sizeof(authdata));33memset(&id, 0, sizeof(id));34memset(&sig, 0, sizeof(sig));35memset(&x5c, 0, sizeof(x5c));3637r = base64_read(in_f, &cdh);38r |= string_read(in_f, &rpid);39r |= string_read(in_f, &fmt);40r |= base64_read(in_f, &authdata);41r |= base64_read(in_f, &id);42r |= base64_read(in_f, &sig);43if (r < 0)44errx(1, "input error");4546(void)base64_read(in_f, &x5c);4748if (flags & FLAG_DEBUG) {49fprintf(stderr, "client data hash:\n");50xxd(cdh.ptr, cdh.len);51fprintf(stderr, "relying party id: %s\n", rpid);52fprintf(stderr, "format: %s\n", fmt);53fprintf(stderr, "authenticator data:\n");54xxd(authdata.ptr, authdata.len);55fprintf(stderr, "credential id:\n");56xxd(id.ptr, id.len);57fprintf(stderr, "signature:\n");58xxd(sig.ptr, sig.len);59fprintf(stderr, "x509:\n");60xxd(x5c.ptr, x5c.len);61}6263if ((cred = fido_cred_new()) == NULL)64errx(1, "fido_cred_new");6566if ((r = fido_cred_set_type(cred, type)) != FIDO_OK ||67(r = fido_cred_set_clientdata_hash(cred, cdh.ptr,68cdh.len)) != FIDO_OK ||69(r = fido_cred_set_rp(cred, rpid, NULL)) != FIDO_OK ||70(r = fido_cred_set_authdata(cred, authdata.ptr,71authdata.len)) != FIDO_OK ||72(r = fido_cred_set_sig(cred, sig.ptr, sig.len)) != FIDO_OK ||73(r = fido_cred_set_fmt(cred, fmt)) != FIDO_OK)74errx(1, "fido_cred_set: %s", fido_strerr(r));7576if (x5c.ptr != NULL) {77if ((r = fido_cred_set_x509(cred, x5c.ptr, x5c.len)) != FIDO_OK)78errx(1, "fido_cred_set_x509: %s", fido_strerr(r));79}8081if (flags & FLAG_UV) {82if ((r = fido_cred_set_uv(cred, FIDO_OPT_TRUE)) != FIDO_OK)83errx(1, "fido_cred_set_uv: %s", fido_strerr(r));84}85if (flags & FLAG_HMAC) {86if ((r = fido_cred_set_extensions(cred,87FIDO_EXT_HMAC_SECRET)) != FIDO_OK)88errx(1, "fido_cred_set_extensions: %s", fido_strerr(r));89}9091free(cdh.ptr);92free(authdata.ptr);93free(id.ptr);94free(sig.ptr);95free(x5c.ptr);96free(rpid);97free(fmt);9899return (cred);100}101102int103cred_verify(int argc, char **argv)104{105fido_cred_t *cred = NULL;106char *in_path = NULL;107char *out_path = NULL;108FILE *in_f = NULL;109FILE *out_f = NULL;110int type = COSE_ES256;111int flags = 0;112int cred_prot = -1;113int ch;114int r;115116while ((ch = getopt(argc, argv, "c:dhi:o:v")) != -1) {117switch (ch) {118case 'c':119if ((cred_prot = base10(optarg)) < 0)120errx(1, "-c: invalid argument '%s'", optarg);121break;122case 'd':123flags |= FLAG_DEBUG;124break;125case 'h':126flags |= FLAG_HMAC;127break;128case 'i':129in_path = optarg;130break;131case 'o':132out_path = optarg;133break;134case 'v':135flags |= FLAG_UV;136break;137default:138usage();139}140}141142argc -= optind;143argv += optind;144145if (argc > 1)146usage();147148in_f = open_read(in_path);149out_f = open_write(out_path);150151if (argc > 0 && cose_type(argv[0], &type) < 0)152errx(1, "unknown type %s", argv[0]);153154fido_init((flags & FLAG_DEBUG) ? FIDO_DEBUG : 0);155cred = prepare_cred(in_f, type, flags);156157if (cred_prot > 0) {158r = fido_cred_set_prot(cred, cred_prot);159if (r != FIDO_OK) {160errx(1, "fido_cred_set_prot: %s", fido_strerr(r));161}162}163164if (fido_cred_x5c_ptr(cred) == NULL) {165if ((r = fido_cred_verify_self(cred)) != FIDO_OK)166errx(1, "fido_cred_verify_self: %s", fido_strerr(r));167} else {168if ((r = fido_cred_verify(cred)) != FIDO_OK)169errx(1, "fido_cred_verify: %s", fido_strerr(r));170}171172print_cred(out_f, type, cred);173fido_cred_free(&cred);174175fclose(in_f);176fclose(out_f);177in_f = NULL;178out_f = NULL;179180exit(0);181}182183184