diff options
author | 2019-11-14 21:14:10 +0000 | |
---|---|---|
committer | 2019-11-14 21:14:10 +0000 | |
commit | d75efeb73338e77fafc7850eff396e820d2d7935 (patch) | |
tree | aae45e648d23698ee6ff08637f11476e42e5a447 /lib/libfido2/src/blob.c | |
parent | Unleash all the available openings and let the midlayer sort things (diff) | |
download | wireguard-openbsd-d75efeb73338e77fafc7850eff396e820d2d7935.tar.xz wireguard-openbsd-d75efeb73338e77fafc7850eff396e820d2d7935.zip |
import libfido2 (git HEAD). This library allows communication with
U2F/FIDO2 devices over USB.
feedback and "start the churn" deraadt@
Diffstat (limited to 'lib/libfido2/src/blob.c')
-rw-r--r-- | lib/libfido2/src/blob.c | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/lib/libfido2/src/blob.c b/lib/libfido2/src/blob.c new file mode 100644 index 00000000000..5415d87c74d --- /dev/null +++ b/lib/libfido2/src/blob.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2018 Yubico AB. All rights reserved. + * Use of this source code is governed by a BSD-style + * license that can be found in the LICENSE file. + */ + +#include <string.h> +#include "fido.h" + +fido_blob_t * +fido_blob_new(void) +{ + return (calloc(1, sizeof(fido_blob_t))); +} + +int +fido_blob_set(fido_blob_t *b, const unsigned char *ptr, size_t len) +{ + if (b->ptr != NULL) { + explicit_bzero(b->ptr, b->len); + free(b->ptr); + b->ptr = NULL; + } + + b->len = 0; + + if (ptr == NULL || len == 0) { + log_debug("%s: ptr=%p, len=%zu", __func__, (const void *)ptr, + len); + return (-1); + } + + if ((b->ptr = malloc(len)) == NULL) { + log_debug("%s: malloc", __func__); + return (-1); + } + + memcpy(b->ptr, ptr, len); + b->len = len; + + return (0); +} + +void +fido_blob_free(fido_blob_t **bp) +{ + fido_blob_t *b; + + if (bp == NULL || (b = *bp) == NULL) + return; + + if (b->ptr) { + explicit_bzero(b->ptr, b->len); + free(b->ptr); + } + + explicit_bzero(b, sizeof(*b)); + free(b); + + *bp = NULL; +} + +void +free_blob_array(fido_blob_array_t *array) +{ + if (array->ptr == NULL) + return; + + for (size_t i = 0; i < array->len; i++) { + fido_blob_t *b = &array->ptr[i]; + if (b->ptr != NULL) { + explicit_bzero(b->ptr, b->len); + free(b->ptr); + b->ptr = NULL; + } + } + + free(array->ptr); + array->ptr = NULL; + array->len = 0; +} + +cbor_item_t * +fido_blob_encode(const fido_blob_t *b) +{ + if (b == NULL || b->ptr == NULL) + return (NULL); + + return (cbor_build_bytestring(b->ptr, b->len)); +} + +int +fido_blob_decode(const cbor_item_t *item, fido_blob_t *b) +{ + return (cbor_bytestring_copy(item, &b->ptr, &b->len)); +} + +int +fido_blob_is_empty(const fido_blob_t *b) +{ + return (b->ptr == NULL || b->len == 0); +} |