1 | #pragma once |
2 | |
3 | #if __has_include(<nlohmann/json_fwd.hpp>) |
4 | #include <nlohmann/json_fwd.hpp> |
5 | #else |
6 | #include <nlohmann/json.hpp> |
7 | #endif |
8 | |
9 | #include <map> |
10 | #include <string> |
11 | #include <vector> |
12 | |
13 | /// @file |
14 | /// @brief Common types used by the crypto related endpoints. Common types used by room directory |
15 | /// endpoints. |
16 | |
17 | namespace mtx { |
18 | namespace crypto { |
19 | |
20 | using AlgorithmDevice = std::string; |
21 | |
22 | struct UnsignedDeviceInfo |
23 | { |
24 | //! The display name which the user set on the device. |
25 | std::string device_display_name; |
26 | |
27 | friend void from_json(const nlohmann::json &obj, UnsignedDeviceInfo &res); |
28 | friend void to_json(nlohmann::json &obj, const UnsignedDeviceInfo &res); |
29 | }; |
30 | |
31 | struct DeviceKeys |
32 | { |
33 | //! The ID of the user the device belongs to. |
34 | std::string user_id; |
35 | //! The ID of the device these keys belong to. |
36 | std::string device_id; |
37 | //! The encryption algorithms supported by this device. |
38 | std::vector<std::string> algorithms = {"m.olm.v1.curve25519-aes-sha2" , "m.megolm.v1.aes-sha2" }; |
39 | //! Public identity keys. |
40 | //! The names of the properties should be in the format <algorithm>:<device_id>. |
41 | //! The keys themselves should be encoded as specified by the key algorithm. |
42 | std::map<AlgorithmDevice, std::string> keys; |
43 | //! Signatures for the device key object. |
44 | //! A map from user ID, to a map from <algorithm>:<device_id> to the signature. |
45 | std::map<std::string, std::map<AlgorithmDevice, std::string>> signatures; |
46 | ///! Additional data added to the device key information |
47 | //! by intermediate servers, and not covered by the signatures. |
48 | UnsignedDeviceInfo unsigned_info; |
49 | |
50 | friend void from_json(const nlohmann::json &obj, DeviceKeys &res); |
51 | friend void to_json(nlohmann::json &obj, const DeviceKeys &res); |
52 | }; |
53 | |
54 | struct CrossSigningKeys |
55 | { |
56 | //! The ID of the user the device belongs to. |
57 | std::string user_id; |
58 | //! mentions the purpose of the key like either master,user_signing,self_signing |
59 | std::vector<std::string> usage; |
60 | //! Public keys. |
61 | //! The names of the properties should be in the format <algorithm>:<public_key>. |
62 | std::map<std::string, std::string> keys; |
63 | //! Signatures for the cross signing key object. |
64 | //! A map from user ID, to a map from <algorithm>:<public_key> to the signature. |
65 | std::map<std::string, std::map<std::string, std::string>> signatures; |
66 | |
67 | friend void from_json(const nlohmann::json &obj, CrossSigningKeys &res); |
68 | friend void to_json(nlohmann::json &obj, const CrossSigningKeys &res); |
69 | }; |
70 | |
71 | struct JWK |
72 | { |
73 | //! Required. Key type. Must be oct. |
74 | std::string kty; |
75 | //! Required. Key operations. Must at least contain encrypt and decrypt. |
76 | std::vector<std::string> key_ops; |
77 | //! Required. Algorithm. Must be A256CTR. |
78 | std::string alg; |
79 | //! Required. The key, encoded as urlsafe unpadded base64. |
80 | std::string k; |
81 | //! Required. Extractable. Must be true. This is a W3C extension. |
82 | bool ext; |
83 | |
84 | friend void from_json(const nlohmann::json &obj, JWK &res); |
85 | friend void to_json(nlohmann::json &obj, const JWK &res); |
86 | }; |
87 | |
88 | struct EncryptedFile |
89 | { |
90 | //! Required. The URL to the file. |
91 | std::string url; |
92 | //! Required. A JSON Web Key object. (The encryption key) |
93 | JWK key; |
94 | //! Required. The Initialisation Vector used by AES-CTR, encoded as unpadded base64. |
95 | std::string iv; |
96 | //! Required. A map from an algorithm name to a hash of the ciphertext, encoded as unpadded |
97 | //! base64. Clients should support the SHA-256 hash, which uses the key sha256. |
98 | std::map<std::string, std::string> hashes; |
99 | //! Required. Version of the encrypted attachments protocol. Must be v2. |
100 | std::string v; |
101 | |
102 | friend void from_json(const nlohmann::json &obj, EncryptedFile &res); |
103 | friend void to_json(nlohmann::json &obj, const EncryptedFile &res); |
104 | }; |
105 | |
106 | } // namespace crypto |
107 | |
108 | // |
109 | namespace common { |
110 | //! Whether or not the room will be visible by non members. |
111 | enum class RoomVisibility |
112 | { |
113 | //! A private visibility will hide the room from the published room list. |
114 | Private, |
115 | //! Indicates that the room will be shown in the published room list. |
116 | Public, |
117 | }; |
118 | |
119 | inline std::string |
120 | visibilityToString(RoomVisibility visibility) |
121 | { |
122 | if (visibility == RoomVisibility::Private) { |
123 | return "private" ; |
124 | } |
125 | |
126 | return "public" ; |
127 | } |
128 | |
129 | inline RoomVisibility |
130 | stringToVisibility(const std::string &s) |
131 | { |
132 | if (s == "private" ) { |
133 | return RoomVisibility::Private; |
134 | } |
135 | return RoomVisibility::Public; |
136 | } |
137 | } // namespace common |
138 | } // namespace mtx |
139 | |