diff options
Diffstat (limited to 'src/messenger/messenger_api_message.h')
-rw-r--r-- | src/messenger/messenger_api_message.h | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/src/messenger/messenger_api_message.h b/src/messenger/messenger_api_message.h new file mode 100644 index 000000000..0f0a97e9c --- /dev/null +++ b/src/messenger/messenger_api_message.h | |||
@@ -0,0 +1,190 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2020 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @author Tobias Frisch | ||
22 | * @file src/messenger/messenger_api_message.h | ||
23 | * @brief messenger api: client and service implementation of GNUnet MESSENGER service | ||
24 | */ | ||
25 | |||
26 | #ifndef GNUNET_MESSENGER_API_MESSAGE_H | ||
27 | #define GNUNET_MESSENGER_API_MESSAGE_H | ||
28 | |||
29 | #include "platform.h" | ||
30 | #include "gnunet_crypto_lib.h" | ||
31 | #include "gnunet_identity_service.h" | ||
32 | #include "gnunet_mq_lib.h" | ||
33 | #include "gnunet_signatures.h" | ||
34 | |||
35 | #include "gnunet_messenger_service.h" | ||
36 | |||
37 | #include "messenger_api_ego.h" | ||
38 | |||
39 | /** | ||
40 | * Creates and allocates a new message with a specific <i>kind</i>. | ||
41 | * | ||
42 | * @param kind Kind of message | ||
43 | * @return New message | ||
44 | */ | ||
45 | struct GNUNET_MESSENGER_Message* | ||
46 | create_message (enum GNUNET_MESSENGER_MessageKind kind); | ||
47 | |||
48 | /** | ||
49 | * Creates and allocates a copy of a given <i>message</i>. | ||
50 | * | ||
51 | * @param message Message | ||
52 | * @return New message | ||
53 | */ | ||
54 | struct GNUNET_MESSENGER_Message* | ||
55 | copy_message (const struct GNUNET_MESSENGER_Message *message); | ||
56 | |||
57 | /** | ||
58 | * Destroys a message and frees its memory fully. | ||
59 | * | ||
60 | * @param message Message | ||
61 | */ | ||
62 | void | ||
63 | destroy_message (struct GNUNET_MESSENGER_Message *message); | ||
64 | |||
65 | /** | ||
66 | * Returns the minimal size in bytes to encode a message of a specific <i>kind</i>. | ||
67 | * | ||
68 | * @param kind Kind of message | ||
69 | * @return Minimal size to encode | ||
70 | */ | ||
71 | uint16_t | ||
72 | get_message_kind_size (enum GNUNET_MESSENGER_MessageKind kind); | ||
73 | |||
74 | /** | ||
75 | * Returns the exact size in bytes to encode a given <i>message</i>. | ||
76 | * | ||
77 | * @param message Message | ||
78 | * @return Size to encode | ||
79 | */ | ||
80 | uint16_t | ||
81 | get_message_size (const struct GNUNET_MESSENGER_Message *message); | ||
82 | |||
83 | /** | ||
84 | * Encodes a given <i>message</i> into a <i>buffer</i> of a maximal <i>length</i> in bytes. | ||
85 | * | ||
86 | * @param message Message | ||
87 | * @param length Maximal length to encode | ||
88 | * @param[out] buffer Buffer | ||
89 | */ | ||
90 | void | ||
91 | encode_message (const struct GNUNET_MESSENGER_Message *message, uint16_t length, char *buffer); | ||
92 | |||
93 | /** | ||
94 | * Decodes a <i>message</i> from a given <i>buffer</i> of a maximal <i>length</i> in bytes. | ||
95 | * | ||
96 | * If the buffer is too small for a message of its decoded kind the function fails with | ||
97 | * resulting GNUNET_NO after decoding only the messages header. | ||
98 | * | ||
99 | * On success the function returns GNUNET_YES. | ||
100 | * | ||
101 | * @param[out] message Message | ||
102 | * @param length Maximal length to decode | ||
103 | * @param buffer Buffer | ||
104 | * @return GNUNET_YES on success, otherwise GNUNET_NO | ||
105 | */ | ||
106 | int | ||
107 | decode_message (struct GNUNET_MESSENGER_Message *message, uint16_t length, const char *buffer); | ||
108 | |||
109 | /** | ||
110 | * Calculates a <i>hash</i> of a given <i>buffer</i> of a <i>length</i> in bytes. | ||
111 | * | ||
112 | * @param length Length of buffer | ||
113 | * @param buffer Buffer | ||
114 | * @param[out] hash Hash | ||
115 | */ | ||
116 | void | ||
117 | hash_message (uint16_t length, const char *buffer, struct GNUNET_HashCode *hash); | ||
118 | |||
119 | /** | ||
120 | * Signs the <i>hash</i> of a <i>message</i> with a given <i>ego</i> and writes the signature | ||
121 | * into the <i>buffer</i> as well. | ||
122 | * | ||
123 | * @param[out] message Message | ||
124 | * @param length Length of buffer | ||
125 | * @param[out] buffer Buffer | ||
126 | * @param hash Hash of message | ||
127 | * @param ego EGO | ||
128 | */ | ||
129 | void | ||
130 | sign_message (struct GNUNET_MESSENGER_Message *message, uint16_t length, char *buffer, | ||
131 | const struct GNUNET_HashCode *hash, const struct GNUNET_MESSENGER_Ego *ego); | ||
132 | |||
133 | /** | ||
134 | * Verifies the signature of a given <i>message</i> and its <i>hash</i> with a specific | ||
135 | * public key. The function returns GNUNET_OK if the signature was valid, otherwise | ||
136 | * GNUNET_SYSERR. | ||
137 | * | ||
138 | * @param message Message | ||
139 | * @param hash Hash of message | ||
140 | * @param key Public key of EGO | ||
141 | * @return GNUNET_OK on success, otherwise GNUNET_SYSERR | ||
142 | */ | ||
143 | int | ||
144 | verify_message (const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash, | ||
145 | const struct GNUNET_IDENTITY_PublicKey *key); | ||
146 | |||
147 | /** | ||
148 | * Encrypts a <i>message</i> using a given public <i>key</i> and replaces its body | ||
149 | * and kind with the now private encrypted <i>message</i>. The function returns | ||
150 | * GNUNET_YES if the operation succeeded, otherwise GNUNET_NO. | ||
151 | * | ||
152 | * @param message Message | ||
153 | * @param key Public key of EGO | ||
154 | * @return GNUNET_YES on success, otherwise GNUNET_NO | ||
155 | */ | ||
156 | int | ||
157 | encrypt_message (struct GNUNET_MESSENGER_Message *message, const struct GNUNET_IDENTITY_PublicKey *key); | ||
158 | |||
159 | /** | ||
160 | * Decrypts a private <i>message</i> using a given private <i>key</i> and replaces its body | ||
161 | * and kind with the inner encrypted message. The function returns GNUNET_YES if the | ||
162 | * operation succeeded, otherwise GNUNET_NO. | ||
163 | * | ||
164 | * @param message Message | ||
165 | * @param key Private key of EGO | ||
166 | * @return GNUNET_YES on success, otherwise GNUNET_NO | ||
167 | */ | ||
168 | int | ||
169 | decrypt_message (struct GNUNET_MESSENGER_Message *message, const struct GNUNET_IDENTITY_PrivateKey *key); | ||
170 | |||
171 | #define GNUNET_MESSENGER_PACK_MODE_ENVELOPE 0x1 | ||
172 | #define GNUNET_MESSENGER_PACK_MODE_UNKNOWN 0x0 | ||
173 | |||
174 | /** | ||
175 | * Encodes the <i>message</i> to pack it into a newly allocated envelope if <i>mode</i> | ||
176 | * is equal to GNUNET_MESSENGER_PACK_MODE_ENVELOPE. Independent of the mode the message | ||
177 | * will be hashed if <i>hash</i> is not NULL and it will be signed if the <i>ego</i> is | ||
178 | * not NULL. | ||
179 | * | ||
180 | * @param[out] message Message | ||
181 | * @param[out] hash Hash of message | ||
182 | * @param ego EGO to sign | ||
183 | * @param mode Mode of packing | ||
184 | * @return Envelope or NULL | ||
185 | */ | ||
186 | struct GNUNET_MQ_Envelope* | ||
187 | pack_message (struct GNUNET_MESSENGER_Message *message, struct GNUNET_HashCode *hash, | ||
188 | const struct GNUNET_MESSENGER_Ego *ego, int mode); | ||
189 | |||
190 | #endif //GNUNET_MESSENGER_API_MESSAGE_H | ||