/src/rnp/src/librekey/kbx_blob.hpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, [Ribose Inc](https://www.ribose.com). |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * |
14 | | * THIS SOFTWARE IS PROVIDED BY THE RIBOSE, INC. AND CONTRIBUTORS ``AS IS'' |
15 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
16 | | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS |
18 | | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
19 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
20 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
21 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
22 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
23 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
24 | | * POSSIBILITY OF SUCH DAMAGE. |
25 | | */ |
26 | | |
27 | | #ifndef RNP_KBX_BLOB_HPP |
28 | | #define RNP_KBX_BLOB_HPP |
29 | | |
30 | | #include <vector> |
31 | | #include "repgp/repgp_def.h" |
32 | | #include "fingerprint.hpp" |
33 | | |
34 | | typedef enum : uint8_t { |
35 | | KBX_EMPTY_BLOB = 0, |
36 | | KBX_HEADER_BLOB = 1, |
37 | | KBX_PGP_BLOB = 2, |
38 | | KBX_X509_BLOB = 3 |
39 | | } kbx_blob_type_t; |
40 | | |
41 | | class kbx_blob_t { |
42 | | protected: |
43 | | kbx_blob_type_t type_; |
44 | | std::vector<uint8_t> image_; |
45 | | |
46 | | uint8_t ru8(size_t idx); |
47 | | uint16_t ru16(size_t idx); |
48 | | uint32_t ru32(size_t idx); |
49 | | |
50 | | public: |
51 | 80.1k | virtual ~kbx_blob_t() = default; |
52 | | kbx_blob_t(std::vector<uint8_t> &data); |
53 | | virtual bool |
54 | | parse() |
55 | 3.61k | { |
56 | 3.61k | return true; |
57 | 3.61k | }; |
58 | | |
59 | | kbx_blob_type_t |
60 | | type() |
61 | 79.5k | { |
62 | 79.5k | return type_; |
63 | 79.5k | } |
64 | | |
65 | | std::vector<uint8_t> & |
66 | | image() |
67 | 73.4k | { |
68 | 73.4k | return image_; |
69 | 73.4k | } |
70 | | |
71 | | uint32_t |
72 | | length() const noexcept |
73 | 2.61k | { |
74 | 2.61k | return image_.size(); |
75 | 2.61k | } |
76 | | }; |
77 | | |
78 | | class kbx_header_blob_t : public kbx_blob_t { |
79 | | protected: |
80 | | uint8_t version_{}; |
81 | | uint16_t flags_{}; |
82 | | uint32_t file_created_at_{}; |
83 | | uint32_t last_maintenance_run_{}; |
84 | | |
85 | | public: |
86 | 2.61k | kbx_header_blob_t(std::vector<uint8_t> &data) : kbx_blob_t(data){}; |
87 | | bool parse(); |
88 | | |
89 | | uint32_t |
90 | | file_created_at() |
91 | 0 | { |
92 | 0 | return file_created_at_; |
93 | 0 | } |
94 | | }; |
95 | | |
96 | | typedef struct { |
97 | | uint8_t fp[PGP_MAX_FINGERPRINT_SIZE]; |
98 | | uint32_t keyid_offset; |
99 | | uint16_t flags; |
100 | | } kbx_pgp_key_t; |
101 | | |
102 | | typedef struct { |
103 | | uint32_t offset; |
104 | | uint32_t length; |
105 | | uint16_t flags; |
106 | | uint8_t validity; |
107 | | } kbx_pgp_uid_t; |
108 | | |
109 | | typedef struct { |
110 | | uint32_t expired; |
111 | | } kbx_pgp_sig_t; |
112 | | |
113 | | class kbx_pgp_blob_t : public kbx_blob_t { |
114 | | protected: |
115 | | uint8_t version_{}; |
116 | | uint16_t flags_{}; |
117 | | uint32_t keyblock_offset_{}; |
118 | | uint32_t keyblock_length_{}; |
119 | | |
120 | | std::vector<uint8_t> sn_{}; |
121 | | std::vector<kbx_pgp_key_t> keys_{}; |
122 | | std::vector<kbx_pgp_uid_t> uids_{}; |
123 | | std::vector<kbx_pgp_sig_t> sigs_{}; |
124 | | |
125 | | uint8_t ownertrust_{}; |
126 | | uint8_t all_validity_{}; |
127 | | |
128 | | uint32_t recheck_after_{}; |
129 | | uint32_t latest_timestamp_{}; |
130 | | uint32_t blob_created_at_{}; |
131 | | |
132 | | public: |
133 | 73.8k | kbx_pgp_blob_t(std::vector<uint8_t> &data) : kbx_blob_t(data){}; |
134 | | |
135 | | uint32_t |
136 | | keyblock_offset() |
137 | 73.4k | { |
138 | 73.4k | return keyblock_offset_; |
139 | 73.4k | } |
140 | | |
141 | | uint32_t |
142 | | keyblock_length() |
143 | 146k | { |
144 | 146k | return keyblock_length_; |
145 | 146k | } |
146 | | |
147 | | size_t |
148 | | nkeys() |
149 | 0 | { |
150 | 0 | return keys_.size(); |
151 | 0 | } |
152 | | size_t |
153 | | nuids() |
154 | 0 | { |
155 | 0 | return uids_.size(); |
156 | 0 | } |
157 | | size_t |
158 | | nsigs() |
159 | 0 | { |
160 | 0 | return sigs_.size(); |
161 | 0 | } |
162 | | |
163 | | bool parse(); |
164 | | }; |
165 | | |
166 | | #endif |