// Copyright 2017 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef DEVICE_FIDO_U2F_PARSING_UTILS_H_ #define DEVICE_FIDO_U2F_PARSING_UTILS_H_ #include #include #include #include #include #include "base/component_export.h" #include "base/containers/span.h" #include "base/optional.h" namespace device { namespace u2f_parsing_utils { // U2FResponse offsets. The format of a U2F response is defined in // https://fidoalliance.org/specs/fido-u2f-v1.2-ps-20170411/fido-u2f-raw-message-formats-v1.2-ps-20170411.html#registration-response-message-success COMPONENT_EXPORT(DEVICE_FIDO) extern const uint32_t kU2fResponseKeyHandleLengthPos; COMPONENT_EXPORT(DEVICE_FIDO) extern const uint32_t kU2fResponseKeyHandleStartPos; COMPONENT_EXPORT(DEVICE_FIDO) extern const char kEs256[]; // Returns a materialized copy of |span|, that is, a vector with the same // elements. COMPONENT_EXPORT(DEVICE_FIDO) std::vector Materialize(base::span span); COMPONENT_EXPORT(DEVICE_FIDO) base::Optional> MaterializeOrNull( base::Optional> span); // Appends |in_values| to the end of |target|. The underlying container for // |in_values| should *not* be |target|. COMPONENT_EXPORT(DEVICE_FIDO) void Append(std::vector* target, base::span in_values); // Safely extracts, with bound checking, a contiguous subsequence of |span| of // the given |length| and starting at |pos|. Returns an empty vector/span if the // requested range is out-of-bound. COMPONENT_EXPORT(DEVICE_FIDO) std::vector Extract(base::span span, size_t pos, size_t length); COMPONENT_EXPORT(DEVICE_FIDO) base::span ExtractSpan(base::span span, size_t pos, size_t length); // Safely extracts, with bound checking, the suffix of the given |span| starting // at the given position |pos|. Returns an empty vector/span if the requested // starting position is out-of-bound. COMPONENT_EXPORT(DEVICE_FIDO) std::vector ExtractSuffix(base::span span, size_t pos); COMPONENT_EXPORT(DEVICE_FIDO) base::span ExtractSuffixSpan(base::span span, size_t pos); template bool ExtractArray(base::span span, size_t pos, std::array* array) { const auto extracted_span = ExtractSpan(span, pos, N); if (extracted_span.size() != N) return false; std::copy(extracted_span.begin(), extracted_span.end(), array->begin()); return true; } // Partitions |span| into N = ⌈span.size() / max_chunk_size⌉ consecutive chunks. // The first N-1 chunks are of size |max_chunk_size|, and the Nth chunk is of // size span.size() % max_chunk_size. |max_chunk_size| must be greater than 0. // Returns an empty vector in case |span| is empty. COMPONENT_EXPORT(DEVICE_FIDO) std::vector> SplitSpan(base::span span, size_t max_chunk_size); } // namespace u2f_parsing_utils } // namespace device #endif // DEVICE_FIDO_U2F_PARSING_UTILS_H_