|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Estonian Information System Authority |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#pragma once |
| 24 | + |
| 25 | +#include "pcsc-cpp/pcsc-cpp.hpp" |
| 26 | +#include "pcsc-cpp/pcsc-cpp-utils.hpp" |
| 27 | + |
| 28 | +namespace electronic_id |
| 29 | +{ |
| 30 | + |
| 31 | +/** |
| 32 | + * Represents a single Tag-Length-Value structure used in DER-encoded eID card files. |
| 33 | + * |
| 34 | + * The constructor parses the tag and length from the provided byte range, |
| 35 | + * then adjusts its iterators so that `begin` and `end` reference only the value bytes. |
| 36 | + * If the TLV is empty, `operator bool()` returns false. |
| 37 | + */ |
| 38 | +struct TLV |
| 39 | +{ |
| 40 | + using byte_vector = pcsc_cpp::byte_vector; |
| 41 | + uint16_t tag {}; |
| 42 | + uint32_t length {}; |
| 43 | + byte_vector::const_iterator begin; |
| 44 | + byte_vector::const_iterator end; |
| 45 | + |
| 46 | + PCSC_CPP_CONSTEXPR_VECTOR explicit TLV(const byte_vector& data) : |
| 47 | + TLV(data.cbegin(), data.cend()) |
| 48 | + { |
| 49 | + } |
| 50 | + |
| 51 | + PCSC_CPP_CONSTEXPR_VECTOR TLV(byte_vector::const_iterator _begin, |
| 52 | + byte_vector::const_iterator _end) : begin(_begin), end(_end) |
| 53 | + { |
| 54 | + if (!*this) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + tag = *begin++; |
| 59 | + if ((tag & 0x1F) == 0x1F) { // Multi-byte tag |
| 60 | + if (!*this) { |
| 61 | + THROW(std::invalid_argument, "Invalid TLV: Unexpected end of tag"); |
| 62 | + } |
| 63 | + tag = (tag << 8) | (*begin++); |
| 64 | + } |
| 65 | + |
| 66 | + if (!*this) { |
| 67 | + THROW(std::invalid_argument, "Invalid TLV: Missing length field"); |
| 68 | + } |
| 69 | + |
| 70 | + length = *begin++; |
| 71 | + if (length & 0x80) { // Extended length encoding |
| 72 | + auto num_bytes = uint8_t(length & 0x7F); |
| 73 | + if (num_bytes == 0 || num_bytes > 4 || std::distance(begin, end) < num_bytes) { |
| 74 | + THROW(std::invalid_argument, "Invalid TLV: Incorrect extended length encoding"); |
| 75 | + } |
| 76 | + |
| 77 | + length = 0; |
| 78 | + for (uint8_t i = 0; i < num_bytes; ++i) { |
| 79 | + length = (length << 8) | (*begin++); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (std::distance(begin, end) < length) { |
| 84 | + THROW(std::invalid_argument, "Invalid TLV: Insufficient value data"); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + PCSC_CPP_CONSTEXPR_VECTOR TLV child() const { return {begin, begin + length}; } |
| 89 | + |
| 90 | + PCSC_CPP_CONSTEXPR_VECTOR TLV& operator++() { return *this = {begin + length, end}; } |
| 91 | + |
| 92 | + template <typename... Tags> |
| 93 | + static PCSC_CPP_CONSTEXPR_VECTOR TLV path(TLV tlv, uint16_t tag, Tags... tags) |
| 94 | + { |
| 95 | + for (; tlv; ++tlv) { |
| 96 | + if (tlv.tag == tag) { |
| 97 | + if constexpr (sizeof...(tags) > 0) { |
| 98 | + return path(tlv.child(), uint16_t(tags)...); |
| 99 | + } |
| 100 | + return tlv; |
| 101 | + } |
| 102 | + } |
| 103 | + return TLV({}); |
| 104 | + } |
| 105 | + template <typename... Tags> |
| 106 | + static PCSC_CPP_CONSTEXPR_VECTOR TLV path(const byte_vector& data, uint16_t tag, Tags... tags) |
| 107 | + { |
| 108 | + return path(TLV(data), tag, tags...); |
| 109 | + } |
| 110 | + |
| 111 | + constexpr operator bool() const noexcept { return begin != end; } |
| 112 | +}; |
| 113 | + |
| 114 | +} // namespace electronic_id |
0 commit comments