10afa8e06SEd Maste /*
20afa8e06SEd Maste * Copyright (c) 2018 Yubico AB. All rights reserved.
30afa8e06SEd Maste * Use of this source code is governed by a BSD-style
40afa8e06SEd Maste * license that can be found in the LICENSE file.
5*2ccfa855SEd Maste * SPDX-License-Identifier: BSD-2-Clause
60afa8e06SEd Maste */
70afa8e06SEd Maste
80afa8e06SEd Maste #include "fido.h"
90afa8e06SEd Maste
100afa8e06SEd Maste fido_blob_t *
fido_blob_new(void)110afa8e06SEd Maste fido_blob_new(void)
120afa8e06SEd Maste {
130afa8e06SEd Maste return calloc(1, sizeof(fido_blob_t));
140afa8e06SEd Maste }
150afa8e06SEd Maste
160afa8e06SEd Maste void
fido_blob_reset(fido_blob_t * b)170afa8e06SEd Maste fido_blob_reset(fido_blob_t *b)
180afa8e06SEd Maste {
190afa8e06SEd Maste freezero(b->ptr, b->len);
200afa8e06SEd Maste explicit_bzero(b, sizeof(*b));
210afa8e06SEd Maste }
220afa8e06SEd Maste
230afa8e06SEd Maste int
fido_blob_set(fido_blob_t * b,const u_char * ptr,size_t len)240afa8e06SEd Maste fido_blob_set(fido_blob_t *b, const u_char *ptr, size_t len)
250afa8e06SEd Maste {
260afa8e06SEd Maste fido_blob_reset(b);
270afa8e06SEd Maste
280afa8e06SEd Maste if (ptr == NULL || len == 0) {
290afa8e06SEd Maste fido_log_debug("%s: ptr=%p, len=%zu", __func__,
300afa8e06SEd Maste (const void *)ptr, len);
310afa8e06SEd Maste return -1;
320afa8e06SEd Maste }
330afa8e06SEd Maste
340afa8e06SEd Maste if ((b->ptr = malloc(len)) == NULL) {
350afa8e06SEd Maste fido_log_debug("%s: malloc", __func__);
360afa8e06SEd Maste return -1;
370afa8e06SEd Maste }
380afa8e06SEd Maste
390afa8e06SEd Maste memcpy(b->ptr, ptr, len);
400afa8e06SEd Maste b->len = len;
410afa8e06SEd Maste
420afa8e06SEd Maste return 0;
430afa8e06SEd Maste }
440afa8e06SEd Maste
450afa8e06SEd Maste int
fido_blob_append(fido_blob_t * b,const u_char * ptr,size_t len)460afa8e06SEd Maste fido_blob_append(fido_blob_t *b, const u_char *ptr, size_t len)
470afa8e06SEd Maste {
480afa8e06SEd Maste u_char *tmp;
490afa8e06SEd Maste
500afa8e06SEd Maste if (ptr == NULL || len == 0) {
510afa8e06SEd Maste fido_log_debug("%s: ptr=%p, len=%zu", __func__,
520afa8e06SEd Maste (const void *)ptr, len);
530afa8e06SEd Maste return -1;
540afa8e06SEd Maste }
550afa8e06SEd Maste if (SIZE_MAX - b->len < len) {
560afa8e06SEd Maste fido_log_debug("%s: overflow", __func__);
570afa8e06SEd Maste return -1;
580afa8e06SEd Maste }
590afa8e06SEd Maste if ((tmp = realloc(b->ptr, b->len + len)) == NULL) {
600afa8e06SEd Maste fido_log_debug("%s: realloc", __func__);
610afa8e06SEd Maste return -1;
620afa8e06SEd Maste }
630afa8e06SEd Maste b->ptr = tmp;
640afa8e06SEd Maste memcpy(&b->ptr[b->len], ptr, len);
650afa8e06SEd Maste b->len += len;
660afa8e06SEd Maste
670afa8e06SEd Maste return 0;
680afa8e06SEd Maste }
690afa8e06SEd Maste
700afa8e06SEd Maste void
fido_blob_free(fido_blob_t ** bp)710afa8e06SEd Maste fido_blob_free(fido_blob_t **bp)
720afa8e06SEd Maste {
730afa8e06SEd Maste fido_blob_t *b;
740afa8e06SEd Maste
750afa8e06SEd Maste if (bp == NULL || (b = *bp) == NULL)
760afa8e06SEd Maste return;
770afa8e06SEd Maste
780afa8e06SEd Maste fido_blob_reset(b);
790afa8e06SEd Maste free(b);
800afa8e06SEd Maste *bp = NULL;
810afa8e06SEd Maste }
820afa8e06SEd Maste
830afa8e06SEd Maste void
fido_free_blob_array(fido_blob_array_t * array)840afa8e06SEd Maste fido_free_blob_array(fido_blob_array_t *array)
850afa8e06SEd Maste {
860afa8e06SEd Maste if (array->ptr == NULL)
870afa8e06SEd Maste return;
880afa8e06SEd Maste
890afa8e06SEd Maste for (size_t i = 0; i < array->len; i++) {
900afa8e06SEd Maste fido_blob_t *b = &array->ptr[i];
910afa8e06SEd Maste freezero(b->ptr, b->len);
920afa8e06SEd Maste b->ptr = NULL;
930afa8e06SEd Maste }
940afa8e06SEd Maste
950afa8e06SEd Maste free(array->ptr);
960afa8e06SEd Maste array->ptr = NULL;
970afa8e06SEd Maste array->len = 0;
980afa8e06SEd Maste }
990afa8e06SEd Maste
1000afa8e06SEd Maste cbor_item_t *
fido_blob_encode(const fido_blob_t * b)1010afa8e06SEd Maste fido_blob_encode(const fido_blob_t *b)
1020afa8e06SEd Maste {
1030afa8e06SEd Maste if (b == NULL || b->ptr == NULL)
1040afa8e06SEd Maste return NULL;
1050afa8e06SEd Maste
1060afa8e06SEd Maste return cbor_build_bytestring(b->ptr, b->len);
1070afa8e06SEd Maste }
1080afa8e06SEd Maste
1090afa8e06SEd Maste int
fido_blob_decode(const cbor_item_t * item,fido_blob_t * b)1100afa8e06SEd Maste fido_blob_decode(const cbor_item_t *item, fido_blob_t *b)
1110afa8e06SEd Maste {
1120afa8e06SEd Maste return cbor_bytestring_copy(item, &b->ptr, &b->len);
1130afa8e06SEd Maste }
1140afa8e06SEd Maste
1150afa8e06SEd Maste int
fido_blob_is_empty(const fido_blob_t * b)1160afa8e06SEd Maste fido_blob_is_empty(const fido_blob_t *b)
1170afa8e06SEd Maste {
1180afa8e06SEd Maste return b->ptr == NULL || b->len == 0;
1190afa8e06SEd Maste }
1200afa8e06SEd Maste
1210afa8e06SEd Maste int
fido_blob_serialise(fido_blob_t * b,const cbor_item_t * item)1220afa8e06SEd Maste fido_blob_serialise(fido_blob_t *b, const cbor_item_t *item)
1230afa8e06SEd Maste {
1240afa8e06SEd Maste size_t alloc;
1250afa8e06SEd Maste
1260afa8e06SEd Maste if (!fido_blob_is_empty(b))
1270afa8e06SEd Maste return -1;
1280afa8e06SEd Maste if ((b->len = cbor_serialize_alloc(item, &b->ptr, &alloc)) == 0) {
1290afa8e06SEd Maste b->ptr = NULL;
1300afa8e06SEd Maste return -1;
1310afa8e06SEd Maste }
1320afa8e06SEd Maste
1330afa8e06SEd Maste return 0;
1340afa8e06SEd Maste }
135