1 /**
2  @file bzrtp.h
3 
4  @brief Public entry points to the ZRTP implementation
5 
6  @author Johan Pascal
7 
8  @copyright Copyright (C) 2014 Belledonne Communications, Grenoble, France
9 
10  This program is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License
12  as published by the Free Software Foundation; either version 2
13  of the License, or (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23  */
24 #ifndef BZRTP_H
25 #define BZRTP_H
26 
27 #include <stdint.h>
28 #include "bctoolbox/crypto.h"
29 
30 #ifdef _MSC_VER
31 	#ifdef BZRTP_STATIC
32 		#define BZRTP_EXPORT
33 	#else
34 		#ifdef BZRTP_EXPORTS
35 			#define BZRTP_EXPORT __declspec(dllexport)
36 		#else
37 			#define BZRTP_EXPORT __declspec(dllimport)
38 		#endif
39 	#endif
40 #else
41 	#define BZRTP_EXPORT __attribute__ ((visibility ("default")))
42 #endif
43 
44 /**
45  * Define different types of crypto functions */
46 #define ZRTP_HASH_TYPE			0x01
47 #define ZRTP_CIPHERBLOCK_TYPE 	0x02
48 #define ZRTP_AUTHTAG_TYPE		0x04
49 #define ZRTP_KEYAGREEMENT_TYPE	0x08
50 #define ZRTP_SAS_TYPE			0x10
51 
52 /**
53  * map the differents algorithm (some may not be available) to integer */
54 
55 #define ZRTP_UNSET_ALGO			0x00
56 
57 #define	ZRTP_HASH_S256			0x11
58 #define	ZRTP_HASH_S384			0x12
59 #define	ZRTP_HASH_N256			0x13
60 #define	ZRTP_HASH_N384			0x14
61 
62 #define ZRTP_CIPHER_AES1		0x21
63 #define ZRTP_CIPHER_AES2		0x22
64 #define ZRTP_CIPHER_AES3		0x23
65 #define ZRTP_CIPHER_2FS1		0x24
66 #define ZRTP_CIPHER_2FS2		0x25
67 #define ZRTP_CIPHER_2FS3		0x26
68 
69 #define ZRTP_AUTHTAG_HS32		0x31
70 #define ZRTP_AUTHTAG_HS80		0x32
71 #define ZRTP_AUTHTAG_SK32		0x33
72 #define ZRTP_AUTHTAG_SK64		0x34
73 
74 /**
75  * WARNING : it is very important to keep the key agreement defined in that order
76  * as it is used to easily sort them from faster(DH2k) to slower(EC52)
77  */
78 #define ZRTP_KEYAGREEMENT_DH2k	0x41
79 #define ZRTP_KEYAGREEMENT_EC25	0x42
80 #define ZRTP_KEYAGREEMENT_DH3k	0x43
81 #define ZRTP_KEYAGREEMENT_EC38	0x44
82 #define ZRTP_KEYAGREEMENT_EC52	0x45
83 
84 #define ZRTP_KEYAGREEMENT_Prsh	0x46
85 #define ZRTP_KEYAGREEMENT_Mult	0x47
86 
87 #define ZRTP_SAS_B32			0x51
88 #define ZRTP_SAS_B256			0x52
89 
90 
91 /**
92  * Define to give client indication on which srtp secrets are valid when given
93  */
94 #define ZRTP_SRTP_SECRETS_FOR_SENDER	0x01
95 #define ZRTP_SRTP_SECRETS_FOR_RECEIVER	0x02
96 
97 /**
98  * brief The data structure containing the keys and algorithms to be used by srtp
99  * Also stores SAS and informations about the crypto algorithms selected during ZRTP negotiation */
100 typedef struct bzrtpSrtpSecrets_struct  {
101 	uint8_t *selfSrtpKey; /**< The key used by local part to encrypt */
102 	uint8_t selfSrtpKeyLength; /**< The length in byte of the key */
103 	uint8_t *selfSrtpSalt; /**< The salt used by local part to encrypt */
104 	uint8_t selfSrtpSaltLength; /**< The length in byte of the salt */
105 	uint8_t *peerSrtpKey; /**< The key used by local part to decrypt */
106 	uint8_t peerSrtpKeyLength; /**< The length in byte of the key */
107 	uint8_t *peerSrtpSalt; /**< The salt used by local part to decrypt */
108 	uint8_t peerSrtpSaltLength; /**< The length in byte of the salt */
109 	uint8_t cipherAlgo; /**< The cipher block algorithm selected durign ZRTP negotiation and used by srtp */
110 	uint8_t cipherKeyLength; /**< The key length in bytes for the cipher block algorithm used by srtp */
111 	uint8_t authTagAlgo; /**< srtp authentication tag algorithm agreed on after Hello packet exchange */
112 	char *sas; /**< a null terminated char containing the Short Authentication String */
113 	uint8_t sasLength; /**< The length of sas, including the termination character */
114 	uint8_t hashAlgo; /**< The hash algo selected during ZRTP negotiation */
115 	uint8_t keyAgreementAlgo; /**< The key agreement algo selected during ZRTP negotiation */
116 	uint8_t sasAlgo; /**< The SAS rendering algo selected during ZRTP negotiation */
117 	uint8_t cacheMismatch; /**< Flag set to 1 in case of ZRTP cache mismatch, may occurs only on first channel(the one computing SAS) */
118 } bzrtpSrtpSecrets_t;
119 
120 
121 /* define message levels */
122 #define BZRTP_MESSAGE_ERROR	0x00
123 #define BZRTP_MESSAGE_WARNING	0x01
124 #define BZRTP_MESSAGE_LOG	0x02
125 #define BZRTP_MESSAGE_DEBUG	0x03
126 
127 /* define message codes */
128 #define BZRTP_MESSAGE_CACHEMISMATCH 		0x01
129 #define BZRTP_MESSAGE_PEERVERSIONOBSOLETE	0x02
130 #define BZRTP_MESSAGE_PEERNOTBZRTP		0x03
131 
132 /**
133  * Function pointer used by bzrtp to free memory allocated by callbacks.
134 **/
135 typedef void (*zrtpFreeBuffer_callback)(void *);
136 /**
137  * @brief All the callback functions provided by the client needed by the ZRTP engine
138  */
139 typedef struct bzrtpCallbacks_struct {
140 	/* messaging status and warnings */
141 	int (* bzrtp_statusMessage)(void *clientData, const uint8_t messageLevel, const uint8_t messageId, const char *messageString); /**< Sending messages to caller: error, warnings, logs, the messageString can be NULL or a NULL terminated string */
142 	int bzrtp_messageLevel; /**< Filter calls to this callback to levels inferiors to this setting (BZRTP_MESSAGE_ERROR, BZRTP_MESSAGE_WARNING, BZRTP_MESSAGE_LOG, BZRTP_MESSAGE_DEBUG )*/
143 
144 	/* sending packets */
145 	int (* bzrtp_sendData)(void *clientData, const uint8_t *packetString, uint16_t packetLength); /**< Send a ZRTP packet to peer. Shall return 0 on success */
146 
147 	/* dealing with SRTP session */
148 	int (* bzrtp_srtpSecretsAvailable)(void *clientData, const bzrtpSrtpSecrets_t *srtpSecrets, uint8_t part); /**< Send the srtp secrets to the client, for either sender, receiver or both according to the part parameter value. Client may wait for the end of ZRTP process before using it */
149 	int (* bzrtp_startSrtpSession)(void *clientData, const bzrtpSrtpSecrets_t *srtpSecrets, int32_t verified); /**< ZRTP process ended well, client is given the SAS and informations about the crypto algo used during ZRTP negotiation. He may start his SRTP session if not done when calling srtpSecretsAvailable */
150 
151 	/* ready for exported keys */
152 	int (* bzrtp_contextReadyForExportedKeys)(void *clientData, int zuid, uint8_t role); /**< Tell the client that this is the time to create any exported keys, s0 is erased just after the call to this callback. Callback is given the peerZID and zuid to adress the correct node in cache and current role which is needed to set a pair of keys for IM encryption */
153 } bzrtpCallbacks_t;
154 
155 #define ZRTP_MAGIC_COOKIE 0x5a525450
156 #define ZRTP_VERSION	"1.10"
157 
158 /* error code definition */
159 #define BZRTP_ERROR_INVALIDCALLBACKID				0x0001
160 #define	BZRTP_ERROR_CONTEXTNOTREADY					0x0002
161 #define BZRTP_ERROR_INVALIDCONTEXT					0x0004
162 #define BZRTP_ERROR_MULTICHANNELNOTSUPPORTEDBYPEER	0x0008
163 #define BZRTP_ERROR_UNABLETOADDCHANNEL				0x0010
164 #define BZRTP_ERROR_UNABLETOSTARTCHANNEL			0x0020
165 #define BZRTP_ERROR_OUTPUTBUFFER_LENGTH				0x0040
166 #define BZRTP_ERROR_HELLOHASH_MISMATCH				0x0080
167 #define BZRTP_ERROR_CHANNELALREADYSTARTED			0x0100
168 #define BZRTP_ERROR_CACHEDISABLED				0x0200
169 #define BZRTP_ERROR_CACHEMIGRATIONFAILED			0x0400
170 
171 /* channel status definition */
172 #define BZRTP_CHANNEL_NOTFOUND						0x1000
173 #define BZRTP_CHANNEL_INITIALISED					0x1001
174 #define BZRTP_CHANNEL_ONGOING						0x1002
175 #define BZRTP_CHANNEL_SECURE						0x1004
176 #define BZRTP_CHANNEL_ERROR						0x1008
177 
178 /* role mapping */
179 #define BZRTP_ROLE_INITIATOR	0
180 #define	BZRTP_ROLE_RESPONDER	1
181 
182 /* cache related value */
183 #define BZRTP_CACHE_SETUP		0x2000
184 #define BZRTP_CACHE_UPDATE		0x2001
185 #define BZRTP_CACHE_DATA_NOTFOUND	0x2002
186 /**
187  * @brief bzrtpContext_t The ZRTP engine context
188  * Store current state, timers, HMAC and encryption keys
189 */
190 typedef struct bzrtpContext_struct bzrtpContext_t;
191 
192 /**
193  * Create context structure and initialise it
194  *
195  * @return The ZRTP engine context data
196  *
197 */
198 BZRTP_EXPORT bzrtpContext_t *bzrtp_createBzrtpContext(void);
199 
200 /**
201  * @brief Perform initialisation which can't be done without ZIDcache acces
202  * - get ZID and create the first channel context
203  *
204  *   @param[in]		context		The context to initialise
205  *   @param[in]		selfSSRC	The SSRC given to the first channel context created within the zrtpContext
206  *
207  *   @return 0 on success
208  */
209 BZRTP_EXPORT int bzrtp_initBzrtpContext(bzrtpContext_t *context, uint32_t selfSSRC);
210 
211 /**
212  * Free memory of context structure to a channel, if all channels are freed, free the global zrtp context
213  * @param[in]	context		Context hosting the channel to be destroyed.(note: the context zrtp context itself is destroyed with the last channel)
214  * @param[in]	selfSSRC	The SSRC identifying the channel to be destroyed
215  *
216  * @return the number of channel still active in this ZRTP context
217 */
218 BZRTP_EXPORT int bzrtp_destroyBzrtpContext(bzrtpContext_t *context, uint32_t selfSSRC);
219 
220 /**
221  * @brief Allocate a function pointer to the callback function identified by his id
222  * @param[in/out]	context				The zrtp context to set the callback function
223  * @param[in] 		cbs 				A structure containing all the callbacks to supply.
224  *
225  * @return 0 on success
226  *
227 */
228 BZRTP_EXPORT int bzrtp_setCallbacks(bzrtpContext_t *context, const bzrtpCallbacks_t *cbs);
229 
230 /**
231  * @brief Set the pointer allowing cache access
232  *
233  * @param[in]	zidCachePointer		Used by internal function to access cache: turn into a sqlite3 pointer if cache is enabled
234  * @param[in]   selfURI			Local URI used for this communication, needed to perform cache operation, NULL terminated string, duplicated by this function
235  * @param[in]   peerURI			Peer URI used for this communication, needed to perform cache operation, NULL terminated string, duplicated by this function
236  *
237  * @return 0 or BZRTP_CACHE_SETUP(if cache is populated by this call) on success, error code otherwise
238 */
239 BZRTP_EXPORT int bzrtp_setZIDCache(bzrtpContext_t *context, void *zidCache, const char *selfURI, const char *peerURI);
240 
241 /**
242  * @brief Set the client data pointer in a channel context
243  * This pointer is returned to the client by the callbacks function, used to store associated contexts (RTP session)
244  * @param[in/out]	zrtpContext		The ZRTP context we're dealing with
245  * @param[in]		selfSSRC		The SSRC identifying the channel to be linked to the client Data
246  * @param[in]		clientData		The clientData pointer, casted to a (void *)
247  *
248  * @return 0 on success
249  *
250 */
251 BZRTP_EXPORT int bzrtp_setClientData(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, void *clientData);
252 
253 /**
254  * @brief Add a channel to an existing context
255  *
256  * @param[in/out]	zrtpContext	The zrtp context who will get the additionnal channel
257  * @param[in]		selfSSRC	The SSRC given to the channel context
258  *
259  * @return 0 on succes, error code otherwise
260  */
261 BZRTP_EXPORT int bzrtp_addChannel(bzrtpContext_t *zrtpContext, uint32_t selfSSRC);
262 
263 
264 /**
265  * @brief Start the state machine of the specified channel
266  * To be able to start an addional channel, we must be in secure state
267  *
268  * @param[in/out]	zrtpContext			The ZRTP context hosting the channel to be started
269  * @param[in]		selfSSRC			The SSRC identifying the channel to be started(will start sending Hello packets and listening for some)
270  *
271  * @return			0 on succes, error code otherwise
272  */
273 BZRTP_EXPORT int bzrtp_startChannelEngine(bzrtpContext_t *zrtpContext, uint32_t selfSSRC);
274 
275 /**
276  * @brief Send the current time to a specified channel, it will check if it has to trig some timer
277  *
278  * @param[in/out]	zrtpContext			The ZRTP context hosting the channel
279  * @param[in]		selfSSRC			The SSRC identifying the channel
280  * @param[in]		timeReference		The current time in ms
281  *
282  * @return			0 on succes, error code otherwise
283  */
284 BZRTP_EXPORT int bzrtp_iterate(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, uint64_t timeReference);
285 
286 /**
287  * @brief Process a received message
288  *
289  * @param[in/out]	zrtpContext				The ZRTP context we're dealing with
290  * @param[in]		selfSSRC				The SSRC identifying the channel receiving the message
291  * @param[in]		zrtpPacketString		The packet received
292  * @param[in]		zrtpPacketStringLength	Length of the packet in bytes
293  *
294  * @return 	0 on success, errorcode otherwise
295  */
296 BZRTP_EXPORT int bzrtp_processMessage(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, uint8_t *zrtpPacketString, uint16_t zrtpPacketStringLength);
297 
298 /**
299  * @brief Called by user when the SAS has been verified
300  *
301  * @param[in/out]	zrtpContext				The ZRTP context we're dealing with
302  */
303 BZRTP_EXPORT void bzrtp_SASVerified(bzrtpContext_t *zrtpContext);
304 
305 /**
306  * @brief Called by user when the SAS has been set to unverified
307  *
308  * @param[in/out]	zrtpContext				The ZRTP context we're dealing with
309  */
310 BZRTP_EXPORT void bzrtp_resetSASVerified(bzrtpContext_t *zrtpContext);
311 
312 /**
313  * @brief Reset the retransmission timer of a given channel.
314  * Packets will be sent again if appropriate:
315  *  - when in responder role, zrtp engine only answer to packets sent by the initiator.
316  *  - if we are still in discovery phase, Hello or Commit packets will be resent.
317  *
318  * @param[in/out]	zrtpContext				The ZRTP context we're dealing with
319  * @param[in]		selfSSRC				The SSRC identifying the channel to reset
320  *
321  * return 0 on success, error code otherwise
322  */
323 BZRTP_EXPORT int bzrtp_resetRetransmissionTimer(bzrtpContext_t *zrtpContext, uint32_t selfSSRC);
324 
325 /**
326  * @brief Get the supported crypto types
327  *
328  * @param[int]		zrtpContext				The ZRTP context we're dealing with
329  * @param[in]		algoType				mapped to defines, must be in [ZRTP_HASH_TYPE, ZRTP_CIPHERBLOCK_TYPE, ZRTP_AUTHTAG_TYPE, ZRTP_KEYAGREEMENT_TYPE or ZRTP_SAS_TYPE]
330  * @param[out]		supportedTypes			mapped to uint8_t value of the 4 char strings giving the supported types as string according to rfc section 5.1.2 to 5.1.6
331  *
332  * @return number of supported types, 0 on error
333  */
334 BZRTP_EXPORT uint8_t bzrtp_getSupportedCryptoTypes(bzrtpContext_t *zrtpContext, uint8_t algoType, uint8_t supportedTypes[7]);
335 
336 /**
337  * @brief set the supported crypto types
338  *
339  * @param[int/out]	zrtpContext				The ZRTP context we're dealing with
340  * @param[in]		algoType				mapped to defines, must be in [ZRTP_HASH_TYPE, ZRTP_CIPHERBLOCK_TYPE, ZRTP_AUTHTAG_TYPE, ZRTP_KEYAGREEMENT_TYPE or ZRTP_SAS_TYPE]
341  * @param[in]		supportedTypes			mapped to uint8_t value of the 4 char strings giving the supported types as string according to rfc section 5.1.2 to 5.1.6
342  * @param[in]		supportedTypesCount		number of supported crypto types
343  */
344 BZRTP_EXPORT void bzrtp_setSupportedCryptoTypes(bzrtpContext_t *zrtpContext, uint8_t algoType, uint8_t supportedTypes[7], uint8_t supportedTypesCount);
345 
346 /**
347  * @brief Set the peer hello hash given by signaling to a ZRTP channel
348  *
349  * @param[in/out]	zrtpContext						The ZRTP context we're dealing with
350  * @param[in]		selfSSRC						The SSRC identifying the channel
351  * @param[in]		peerHelloHashHexString			A NULL terminated string containing the hexadecimal form of the hash received in signaling,
352  * 													may contain ZRTP version as header.
353  * @param[in]		peerHelloHashHexStringLength	Length of hash string (shall be at least 64 as the hash is a SHA256 so 32 bytes,
354  * 													more if it contains the version header)
355  *
356  * @return 	0 on success, errorcode otherwise
357  */
358 BZRTP_EXPORT int bzrtp_setPeerHelloHash(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, uint8_t *peerHelloHashHexString, size_t peerHelloHashHexStringLength);
359 
360 /**
361  * @brief Get the self hello hash from ZRTP channel
362  *
363  * @param[in/out]	zrtpContext			The ZRTP context we're dealing with
364  * @param[in]		selfSSRC			The SSRC identifying the channel
365  * @param[out]		output				A NULL terminated string containing the hexadecimal form of the hash received in signaling,
366  * 										contain ZRTP version as header. Buffer must be allocated by caller.
367  * @param[in]		outputLength		Length of output buffer, shall be at least 70 : 5 chars for version, 64 for the hash itself, SHA256), NULL termination
368  *
369  * @return 	0 on success, errorcode otherwise
370  */
371 BZRTP_EXPORT int bzrtp_getSelfHelloHash(bzrtpContext_t *zrtpContext, uint32_t selfSSRC, uint8_t *output, size_t outputLength);
372 
373 /**
374  * @brief Get the channel status
375  *
376  * @param[in]		zrtpContext			The ZRTP context we're dealing with
377  * @param[in]		selfSSRC			The SSRC identifying the channel
378  *
379  * @return	BZRTP_CHANNEL_NOTFOUND 		no channel matching this SSRC doesn't exists in the zrtp context
380  * 			BZRTP_CHANNEL_INITIALISED	channel initialised but not started
381  * 			BZRTP_CHANNEL_ONGOING		ZRTP key exchange in ongoing
382  *			BZRTP_CHANNEL_SECURE		Channel is secure
383  *			BZRTP_CHANNEL_ERROR			An error occured on this channel
384  */
385 BZRTP_EXPORT int bzrtp_getChannelStatus(bzrtpContext_t *zrtpContext, uint32_t selfSSRC);
386 
387 /*** Cache related functions ***/
388 /**
389  * @brief Check the given sqlite3 DB and create requested tables if needed
390  * 	Also manage DB schema upgrade
391  * @param[in/out]	db	Pointer to the sqlite3 db open connection
392  * 				Use a void * to keep this API when building cacheless
393  *
394  * @return 0 on success, BZRTP_CACHE_SETUP if cache was empty, BZRTP_CACHE_UPDATE if db structure was updated, error code otherwise
395  */
396 BZRTP_EXPORT int bzrtp_initCache(void *db);
397 
398 /**
399  * @brief : retrieve ZID from cache
400  * ZID is randomly generated if cache is empty or inexistant
401  * ZID is randomly generated in case of cacheless implementation(db argument is NULL)
402  *
403  * @param[in/out] 	db		sqlite3 database(or NULL if we don't use cache at runtime)
404  * 					Use a void * to keep this API when building cacheless
405  * @param[in]		selfURI		the sip uri of local user, NULL terminated string
406  * @param[out]		selfZID		the ZID, retrieved from cache or randomly generated
407  * @param[in]		RNGContext	A RNG context used to generate ZID if needed
408  *
409  * @return		0 on success, or BZRTP_CACHE_DATA_NOTFOUND if no ZID matching the URI was found and no RNGContext is given to generate one
410  */
411 BZRTP_EXPORT int bzrtp_getSelfZID(void *db, const char *selfURI, uint8_t selfZID[12], bctbx_rng_context_t *RNGContext);
412 
413 /**
414  * @brief get the cache internal id used to bind local uri(hence local ZID associated to it)<->peer uri/peer ZID.
415  *	Providing a valid local URI(already present in cache), a peer ZID and peer URI will return the zuid creating it if needed
416  *	Any pair ZID/sipURI shall identify an account on a device.
417  *
418  * @param[in/out]	db		the opened sqlite database pointer
419  * @param[in]		selfURI		local URI, must be already associated to a ZID in the DB(association is performed by any call of getSelfZID on this URI)
420  * @param[in]		peerURI		peer URI
421  * @param[in]		peerZID		peer ZID
422  * @param[out]		zuid		the internal db reference to the data row matching this particular pair of correspondant
423  *
424  * @return 0 on success, error code otherwise
425  */
426 BZRTP_EXPORT int bzrtp_cache_getZuid(void *dbPointer, const char *selfURI, const char *peerURI, const uint8_t peerZID[12], int *zuid);
427 
428 /**
429  * @brief Write(insert or update) data in cache, adressing it by zuid (ZID/URI binding id used in cache)
430  * 		Get arrays of column names, values to be inserted, lengths of theses values
431  *		All three arrays must be the same lenght: columnsCount
432  * 		If the row isn't present in the given table, it will be inserted
433  *
434  * @param[in/out]	dbPointer	Pointer to an already opened sqlite db
435  * @param[in]		zuid		The DB internal id to adress the correct row(binding between local uri and peer ZID+URI)
436  * @param[in]		tableName	The name of the table to write in the db, must already exists. Null terminated string
437  * @param[in]		columns		An array of null terminated strings containing the name of the columns to update
438  * @param[in]		values		An array of buffers containing the values to insert/update matching the order of columns array
439  * @param[in]		lengths		An array of integer containing the lengths of values array buffer matching the order of columns array
440  * @param[in]		columnsCount	length common to columns,values and lengths arrays
441  *
442  * @return 0 on succes, error code otherwise
443  */
444 BZRTP_EXPORT int bzrtp_cache_write(void *dbPointer, int zuid, char *tableName, char **columns, uint8_t **values, size_t *lengths, uint8_t columnsCount);
445 
446 /**
447  * @brief Read data from specified table/columns from cache adressing it by zuid (ZID/URI binding id used in cache)
448  * 		Get arrays of column names, values to be read, and the number of colums to be read
449  *		Produce an array of values(uint8_t arrays) and a array of corresponding lengths
450  *		Values memory is allocated by this function and must be freed by caller
451  *
452  * @param[in/out]	dbPointer	Pointer to an already opened sqlite db
453  * @param[in]		zuid		The DB internal id to adress the correct row(binding between local uri and peer ZID+URI)
454  * @param[in]		tableName	The name of the table to read in the db. Null terminated string
455  * @param[in]		columns		An array of null terminated strings containing the name of the columns to read, the array's length  is columnsCount
456  * @param[out]		values		An array of uint8_t pointers, each one will be allocated to the read value and they must be freed by caller
457  * @param[out]		lengths		An array of integer containing the lengths of values array buffer read
458  * @param[in]		columnsCount	length common to columns,values and lengths arrays
459  *
460  * @return 0 on succes, error code otherwise
461  */
462 BZRTP_EXPORT int bzrtp_cache_read(void *dbPointer, int zuid, char *tableName, char **columns, uint8_t **values, size_t *lengths, uint8_t columnsCount);
463 
464 /**
465  * @brief Perform migration from xml version to sqlite3 version of cache
466  *	Warning: new version of cache associate a ZID to each local URI, the old one did not
467  *		the migration function will associate any data in the cache to the sip URI given in parameter which shall be the default URI
468  * @param[in]		cacheXml	a pointer to an xmlDocPtr structure containing the old cache to be migrated
469  * @param[in/out]	cacheSqlite	a pointer to an sqlite3 structure containing a cache initialised using bzrtp_cache_init function
470  * @param[in]		selfURI		default sip URI for this end point, NULL terminated char
471  *
472  * @return	0 on success, BZRTP_ERROR_CACHEDISABLED when bzrtp was not compiled with cache enabled, BZRTP_ERROR_CACHEMIGRATIONFAILED on error during migration
473  */
474 BZRTP_EXPORT int bzrtp_cache_migration(void *cacheXmlPtr, void *cacheSqlite, const char *selfURI);
475 
476 /*
477  * @brief  Allow client to compute an exported according to RFC section 4.5.2
478  *		Check the context is ready(we already have a master exported key and KDF context)
479  * 		and run KDF(master exported key, "Label", KDF_Context, negotiated hash Length)
480  *
481  * @param[in]		zrtpContext		The ZRTP context we're dealing with
482  * @param[in]		label			Label used in the KDF
483  * @param[in]		labelLength		Length of previous buffer
484  * @param[out]		derivedKey		Buffer to store the derived key
485  * @param[in/out]	derivedKeyLength	Length of previous buffer(updated to fit actual length of data produced if too long)
486  *
487  * @return 0 on succes, error code otherwise
488  */
489 BZRTP_EXPORT int bzrtp_exportKey(bzrtpContext_t *zrtpContext, char *label, size_t labelLength, uint8_t *derivedKey, size_t *derivedKeyLength);
490 
491 #endif /* ifndef BZRTP_H */
492