zidCache.c 33.1 KB
Newer Older
johan's avatar
johan committed
1 2 3 4 5 6
/**
 @file zidCache.c

 @brief all ZID and cache related operations are implemented in this file
 - get or create ZID
 - get/update associated secrets
7
 It supports cacheless implementation when cache file access functions are null
johan's avatar
johan committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

 @author Johan Pascal

 @copyright Copyright (C) 2014 Belledonne Communications, Grenoble, France
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
25
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
johan's avatar
johan committed
26 27
 */
#include <stdlib.h>
johan's avatar
johan committed
28
#include <string.h>
29
#include "typedef.h"
30
#include <bctoolbox/crypto.h>
31
#include "cryptoUtils.h"
32
#include "zidCache.h"
33

johan's avatar
johan committed
34 35
#ifdef ZIDCACHE_ENABLED
#include "sqlite3.h"
36 37 38 39
#ifdef HAVE_LIBXML2
#include <libxml/tree.h>
#include <libxml/parser.h>
#endif /* HAVE_LIBXML2 */
40

Ghislain MARY's avatar
Ghislain MARY committed
41 42 43 44 45 46
#ifdef _WIN32
#include <malloc.h>
#else
#include <alloca.h>
#endif

johan's avatar
johan committed
47 48 49
/* define a version number for the DB schema as an interger MMmmpp */
/* current version is 0.0.1 */
#define ZIDCACHE_DBSCHEMA_VERSION_NUMBER 000001
johan's avatar
johan committed
50

johan's avatar
johan committed
51 52
static int callback_getSelfZID(void *data, int argc, char **argv, char **colName){
	uint8_t **selfZID = (uint8_t **)data;
johan's avatar
johan committed
53

johan's avatar
johan committed
54 55 56 57 58
	/* we selected zid only, it must then be in argv[0] */
	if (argv[0]) {
		*selfZID = (uint8_t *)malloc(12*sizeof(uint8_t));
		memcpy(*selfZID, argv[0], 12);
	}
59

johan's avatar
johan committed
60 61 62 63 64 65 66 67
	return 0;
}

static int callback_getUserVersion(void *data, int argc, char **argv, char **colName){
	int *userVersion = (int *)data;

	if (argv[0]) {
		*userVersion = atoi(argv[0]);
johan's avatar
johan committed
68 69
	}

johan's avatar
johan committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	return 0;
}

/* ZID cache is split in several tables
 * ziduri : zuid(unique key) | ZID | selfuri | peeruri
 *         zuid(ZID/URI binding id) will be used for fastest access to the cache, it binds a local user(self uri/self ZID) to a peer identified both by URI and ZID 
 *         self ZID is stored in this table too in a record having 'self' as peer uri, each local user(uri) has a different ZID 
 *
 * All values except zuid in the following tables are blob, actual integers are split and stored in big endian by callers
 * zrtp : zuid(as foreign key) | rs1 | rs2 | aux secret | pbx secret | pvs flag
 * lime : zuid(as foreign key) | sndKey | rcvKey | sndSId | rcvSId | snd Index | rcv Index | valid
 */
int bzrtp_initCache(void *dbPointer) {
	char* errmsg=NULL;
	int ret;
	char *sql;
	sqlite3_stmt *stmt = NULL;
	int userVersion=-1;
	sqlite3 *db = (sqlite3 *)dbPointer;
	int retval = 0;

	if (dbPointer == NULL) { /* we are running cacheless */
		return BZRTP_ZIDCACHE_RUNTIME_CACHELESS;
	}

	/* get current db schema version (user_version pragma in sqlite )*/
	sql = sqlite3_mprintf("PRAGMA user_version;");
	ret = sqlite3_exec(db, sql, callback_getUserVersion, &userVersion, &errmsg);
Simon Morlat's avatar
Simon Morlat committed
98
	sqlite3_free(sql);
johan's avatar
johan committed
99 100 101 102 103 104 105 106 107 108 109 110 111
	if (ret!=SQLITE_OK) {
		sqlite3_free(errmsg);
		return BZRTP_ZIDCACHE_UNABLETOREAD;
	}

	/* Here check version number and provide upgrade is needed */
	if (userVersion != ZIDCACHE_DBSCHEMA_VERSION_NUMBER) {
		if (userVersion > ZIDCACHE_DBSCHEMA_VERSION_NUMBER) { /* nothing to do if we encounter a superior version number than expected, just hope it is compatible */
			//TODO: Log this event
		} else { /* Perform update if needed */
			/* update the schema version in DB metadata */
			sql = sqlite3_mprintf("PRAGMA user_version = %d;",ZIDCACHE_DBSCHEMA_VERSION_NUMBER);
			ret = sqlite3_prepare(db, sql, -1, &stmt, NULL);
Simon Morlat's avatar
Simon Morlat committed
112
			sqlite3_free(sql);
johan's avatar
johan committed
113 114
			if (ret != SQLITE_OK) {
				return BZRTP_ZIDCACHE_UNABLETOUPDATE;
johan's avatar
johan committed
115
			}
johan's avatar
johan committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
			ret = sqlite3_step(stmt);
			if (ret != SQLITE_DONE) {
				return BZRTP_ZIDCACHE_UNABLETOUPDATE;
			}
			sqlite3_finalize(stmt);

			/* setup return value : SETUP for a brand new populated cache, update if we updated the scheme */
			if (userVersion == 0) {
				retval = BZRTP_CACHE_SETUP;
			} else {
				retval = BZRTP_CACHE_UPDATE;
			}
		}
	}

	/* make sure foreign key are turned ON on this connection */
	ret = sqlite3_prepare(db, "PRAGMA foreign_keys = ON;", -1, &stmt, NULL);
	if (ret != SQLITE_OK) {
		return BZRTP_ZIDCACHE_UNABLETOUPDATE;
	}
	ret = sqlite3_step(stmt);
	if (ret != SQLITE_DONE) {
		return BZRTP_ZIDCACHE_UNABLETOUPDATE;
	}
	sqlite3_finalize(stmt);

	/* check/create the ziduri table */
	ret=sqlite3_exec(db,"CREATE TABLE IF NOT EXISTS ziduri ("
							"zuid          INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
							"zid		BLOB NOT NULL DEFAULT '000000000000',"
							"selfuri	 TEXT NOT NULL DEFAULT 'unset',"
							"peeruri	 TEXT NOT NULL DEFAULT 'unset'"
						");",
			0,0,&errmsg);
	if(ret != SQLITE_OK) {
		sqlite3_free(errmsg);
		return BZRTP_ZIDCACHE_UNABLETOUPDATE;
	}

	/* check/create the zrtp table */
	ret=sqlite3_exec(db,"CREATE TABLE IF NOT EXISTS zrtp ("
							"zuid		INTEGER NOT NULL DEFAULT 0 UNIQUE,"
							"rs1		BLOB DEFAULT NULL,"
							"rs2		BLOB DEFAULT NULL,"
							"aux		BLOB DEFAULT NULL,"
							"pbx		BLOB DEFAULT NULL,"
							"pvs		BLOB DEFAULT NULL,"
							"FOREIGN KEY(zuid) REFERENCES ziduri(zuid) ON UPDATE CASCADE ON DELETE CASCADE"

						");",
			0,0,&errmsg);
	if(ret != SQLITE_OK) {
		sqlite3_free(errmsg);
		return BZRTP_ZIDCACHE_UNABLETOUPDATE;
	}

	/* check/create the lime table */
	ret=sqlite3_exec(db,"CREATE TABLE IF NOT EXISTS lime ("
							"zuid		INTEGER NOT NULL DEFAULT 0 UNIQUE,"
							"sndKey		BLOB DEFAULT NULL,"
							"rcvKey		BLOB DEFAULT NULL,"
							"sndSId		BLOB DEFAULT NULL,"
							"rcvSId		BLOB DEFAULT NULL,"
							"sndIndex	BLOB DEFAULT NULL,"
							"rcvIndex	BLOB DEFAULT NULL,"
							"valid		BLOB DEFAULT NULL,"
							"FOREIGN KEY(zuid) REFERENCES ziduri(zuid) ON UPDATE CASCADE ON DELETE CASCADE"

						");",
			0,0,&errmsg);
	if(ret != SQLITE_OK) {
		sqlite3_free(errmsg);
		return BZRTP_ZIDCACHE_UNABLETOUPDATE;
	}


	return retval;
}

int bzrtp_getSelfZID(void *dbPointer, const char *selfURI, uint8_t selfZID[12], bctbx_rng_context_t *RNGContext) {
	char* errmsg=NULL;
	int ret;
	char *stmt;
	uint8_t *localZID = NULL;
	sqlite3 *db = (sqlite3 *)dbPointer;

	if (dbPointer == NULL) { /* we are running cacheless, generate a random ZID if we have a RNG*/
		if (RNGContext != NULL) {
			bctbx_rng_get(RNGContext, selfZID, 12);
			return 0;
		} else {
			return BZRTP_CACHE_DATA_NOTFOUND;
johan's avatar
johan committed
208 209 210
		}
	}

johan's avatar
johan committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	/* check/create the self zid in ziduri table, ORDER BY is just to ensure consistent return in case of inconsistent table(with several self ZID for the same selfuri) */
	stmt = sqlite3_mprintf("SELECT zid FROM ziduri WHERE selfuri=%Q AND peeruri='self' ORDER BY zuid LIMIT 1;",selfURI);
	ret = sqlite3_exec(db,stmt,callback_getSelfZID,&localZID,&errmsg);
	if (ret != SQLITE_OK) {
		sqlite3_free(errmsg);
		return BZRTP_ZIDCACHE_UNABLETOREAD;
	}
	sqlite3_free(stmt);

	/* Do we have a self ZID in cache? */
	if (localZID == NULL) {
		uint8_t generatedZID[12];
		sqlite3_stmt *insertStatement = NULL;

		/* generate a ZID if we can */
		if (RNGContext != NULL) {
			bctbx_rng_get(RNGContext, generatedZID, 12);
		} else {
			return BZRTP_CACHE_DATA_NOTFOUND;
		}
johan's avatar
johan committed
231

johan's avatar
johan committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
		/* insert it in the table */
		ret = sqlite3_prepare_v2(db, "INSERT INTO ziduri (zid,selfuri,peeruri) VALUES(?,?,?);", -1, &insertStatement, NULL);
		if (ret != SQLITE_OK) {
			return BZRTP_ZIDCACHE_UNABLETOUPDATE;
		}
		sqlite3_bind_blob(insertStatement, 1, generatedZID, 12, SQLITE_TRANSIENT);
		sqlite3_bind_text(insertStatement, 2, selfURI,-1,SQLITE_TRANSIENT);
		sqlite3_bind_text(insertStatement, 3, "self",-1,SQLITE_TRANSIENT);

		ret = sqlite3_step(insertStatement);
		if (ret!=SQLITE_DONE) {
			return BZRTP_ZIDCACHE_UNABLETOUPDATE;
		}
		sqlite3_finalize(insertStatement);

		/* copy it in the output buffer */
		memcpy(selfZID, generatedZID,12);
	} else { /* we found a ZID, copy it in the output buffer */
		memcpy(selfZID, localZID, 12);
		free(localZID);
	}
johan's avatar
johan committed
253

johan's avatar
johan committed
254 255 256 257
	return 0;
}

/**
johan's avatar
johan committed
258
 * @brief Parse the cache to find secrets associated to the given ZID, set them and their length in the context if they are found 
johan's avatar
johan committed
259
 *
johan's avatar
johan committed
260
 * @param[in/out]	context		the current context, used to get the negotiated Hash algorithm, cache db, peerURI and store results
johan's avatar
johan committed
261
 * @param[in]		peerZID		a byte array of the peer ZID
johan's avatar
johan committed
262
 *
johan's avatar
johan committed
263
 * return 	0 on succes, error code otherwise 
johan's avatar
johan committed
264
 */
johan's avatar
johan committed
265 266 267 268 269 270
int bzrtp_getPeerAssociatedSecrets(bzrtpContext_t *context, uint8_t peerZID[12]) {
	char *stmt = NULL;
	int ret;
	sqlite3_stmt *sqlStmt = NULL;
	int length =0;

johan's avatar
johan committed
271
	if (context == NULL) {
johan's avatar
johan committed
272
		return BZRTP_ZIDCACHE_INVALID_CONTEXT;
johan's avatar
johan committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	}

	/* resert cached secret buffer */
	free(context->cachedSecret.rs1);
	free(context->cachedSecret.rs2);
	free(context->cachedSecret.pbxsecret);
	free(context->cachedSecret.auxsecret);
	context->cachedSecret.rs1 = NULL;
	context->cachedSecret.rs1Length = 0;
	context->cachedSecret.rs2 = NULL;
	context->cachedSecret.rs2Length = 0;
	context->cachedSecret.pbxsecret = NULL;
	context->cachedSecret.pbxsecretLength = 0;
	context->cachedSecret.auxsecret = NULL;
	context->cachedSecret.auxsecretLength = 0;
	context->cachedSecret.previouslyVerifiedSas = 0;

johan's avatar
johan committed
290 291 292 293
	/* are we going cacheless at runtime */
	if (context->zidCache == NULL) { /* we are running cacheless */
		return BZRTP_ZIDCACHE_RUNTIME_CACHELESS;
	}
294

johan's avatar
johan committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
	/* get all secrets from zrtp table, ORDER BY is just to ensure consistent return in case of inconsistent table) */
	stmt = sqlite3_mprintf("SELECT z.zuid, z.rs1, z.rs2, z.aux, z.pbx, z.pvs FROM ziduri as zu LEFT JOIN zrtp as z ON z.zuid=zu.zuid WHERE zu.selfuri=? AND zu.peeruri=? AND zu.zid=? ORDER BY zu.zuid LIMIT 1;");
	ret = sqlite3_prepare_v2(context->zidCache, stmt, -1, &sqlStmt, NULL);
	sqlite3_free(stmt);
	if (ret != SQLITE_OK) {
		return BZRTP_ZIDCACHE_UNABLETOREAD;
	}
	sqlite3_bind_text(sqlStmt, 1, context->selfURI,-1,SQLITE_TRANSIENT);
	sqlite3_bind_text(sqlStmt, 2, context->peerURI,-1,SQLITE_TRANSIENT);
	sqlite3_bind_blob(sqlStmt, 3, peerZID, 12, SQLITE_TRANSIENT);

	ret = sqlite3_step(sqlStmt);

	if (ret!=SQLITE_ROW) {
		sqlite3_finalize(sqlStmt);
		if (ret == SQLITE_DONE) {/* not found in cache, just leave cached secrets reset, but retrieve (or create) zuid */
			return bzrtp_cache_getZuid((void *)context->zidCache, context->selfURI, context->peerURI, context->peerZID, &context->zuid);
		} else { /* we had an error querying the DB... */
			return BZRTP_ZIDCACHE_UNABLETOREAD;
		}
	}

	/* get zuid from column 0 */
	context->zuid = sqlite3_column_int(sqlStmt, 0);

	/* retrieve values : rs1, rs2, aux, pbx, they all are blob, columns 1,2,3,4 */
	length = sqlite3_column_bytes(sqlStmt, 1);
	if (length>0) { /* we have rs1 */
		context->cachedSecret.rs1Length = length;
		context->cachedSecret.rs1 = (uint8_t *)malloc(length*sizeof(uint8_t));
		memcpy(context->cachedSecret.rs1, sqlite3_column_blob(sqlStmt, 1), length);
	}

	length = sqlite3_column_bytes(sqlStmt, 2);
	if (length>0) { /* we have rs2 */
		context->cachedSecret.rs2Length = length;
		context->cachedSecret.rs2 = (uint8_t *)malloc(length*sizeof(uint8_t));
		memcpy(context->cachedSecret.rs2, sqlite3_column_blob(sqlStmt, 2), length);
	}
johan's avatar
johan committed
334

johan's avatar
johan committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	length = sqlite3_column_bytes(sqlStmt, 3);
	if (length>0) { /* we have aux */
		context->cachedSecret.auxsecretLength = length;
		context->cachedSecret.auxsecret = (uint8_t *)malloc(length*sizeof(uint8_t));
		memcpy(context->cachedSecret.auxsecret, sqlite3_column_blob(sqlStmt, 3), length);
	}

	length = sqlite3_column_bytes(sqlStmt, 4);
	if (length>0) { /* we have pbx */
		context->cachedSecret.pbxsecretLength = length;
		context->cachedSecret.pbxsecret = (uint8_t *)malloc(length*sizeof(uint8_t));
		memcpy(context->cachedSecret.pbxsecret, sqlite3_column_blob(sqlStmt, 4), length);
	}

	/* pvs is stored as blob in memory, just get the first byte(length shall be one anyway) and consider */
	/* it may be NULL -> consider it 0 */
	length = sqlite3_column_bytes(sqlStmt, 5);
	if (length!=1) {
		context->cachedSecret.previouslyVerifiedSas = 0; /* anything wich is not 0x01 is considered 0, so none or more than 1 byte is 0 */
	} else {
		if (*((uint8_t *)sqlite3_column_blob(sqlStmt, 5)) == 0x01) {
			context->cachedSecret.previouslyVerifiedSas = 1;
		} else {
			context->cachedSecret.previouslyVerifiedSas = 0; /* anything wich is not 0x01 is considered 0 */
johan's avatar
johan committed
359
		}
johan's avatar
johan committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	}

	sqlite3_finalize(sqlStmt);
	return 0;
}

/**
 * @brief get the cache internal id used to bind local uri(hence local ZID associated to it)<->peer uri/peer ZID.
 *	Providing a valid local URI(already present in cache), a peer ZID and peer URI will return the zuid creating it if needed
 *	Any pair ZID/sipURI shall identify an account on a device.
 *
 * @param[in/out]	db		the opened sqlite database pointer
 * @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)
 * @param[in]		peerURI		peer URI
 * @param[in]		peerZID		peer ZID
 * @param[out]		zuid		the internal db reference to the data row matching this particular pair of correspondant
 *
 * @return 0 on success, error code otherwise
 */
int bzrtp_cache_getZuid(void *dbPointer, const char *selfURI, const char *peerURI, const uint8_t peerZID[12], int *zuid) {
	char *stmt=NULL;
	int ret;
	sqlite3_stmt *sqlStmt = NULL;
	sqlite3 *db = (sqlite3 *)dbPointer;

	if (dbPointer == NULL) { /* we are running cacheless */
		return BZRTP_ZIDCACHE_RUNTIME_CACHELESS;
	}

	/* Try to fetch the requested zuid */
	stmt = sqlite3_mprintf("SELECT zuid FROM ziduri WHERE selfuri=? AND peeruri=? AND zid=? ORDER BY zuid LIMIT 1;");
	ret = sqlite3_prepare_v2(db, stmt, -1, &sqlStmt, NULL);
	sqlite3_free(stmt);
	if (ret != SQLITE_OK) {
		return BZRTP_ZIDCACHE_UNABLETOREAD;
	}

	sqlite3_bind_text(sqlStmt, 1, selfURI,-1,SQLITE_TRANSIENT);
	sqlite3_bind_text(sqlStmt, 2, peerURI,-1,SQLITE_TRANSIENT);
	sqlite3_bind_blob(sqlStmt, 3, peerZID, 12, SQLITE_TRANSIENT);

	ret = sqlite3_step(sqlStmt);

	if (ret!=SQLITE_ROW) { /* We didn't found this binding in the DB */
		sqlite3_finalize(sqlStmt);
		if (ret == SQLITE_DONE) { /* query executed correctly, just our data is not there */
			uint8_t *localZID = NULL;
			char *errmsg=NULL;

			/* check that we have a self ZID matching the self URI and insert a new row */
			stmt = sqlite3_mprintf("SELECT zid FROM ziduri WHERE selfuri=%Q AND peeruri='self' ORDER BY zuid LIMIT 1;",selfURI);
			ret = sqlite3_exec(db,stmt,callback_getSelfZID,&localZID,&errmsg);
			sqlite3_free(stmt);
			if (ret != SQLITE_OK) {
				sqlite3_free(errmsg);
				return BZRTP_ZIDCACHE_UNABLETOREAD;
			}

			if (localZID==NULL) { /* this sip URI is not in our DB, do not create an association with the peer ZID/URI binding */
				return BZRTP_ZIDCACHE_BADINPUTDATA;
			} else { /* yes we know this URI on local device, add a row in the ziduri table */
				stmt = sqlite3_mprintf("INSERT INTO ziduri (zid,selfuri,peeruri) VALUES(?,?,?);");
				ret = sqlite3_prepare_v2(db, stmt, -1, &sqlStmt, NULL);
				if (ret != SQLITE_OK) {
					return BZRTP_ZIDCACHE_UNABLETOUPDATE;
johan's avatar
johan committed
425
				}
johan's avatar
johan committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439
				sqlite3_free(stmt);

				sqlite3_bind_blob(sqlStmt, 1, peerZID, 12, SQLITE_TRANSIENT);
				sqlite3_bind_text(sqlStmt, 2, selfURI,-1,SQLITE_TRANSIENT);
				sqlite3_bind_text(sqlStmt, 3, peerURI,-1,SQLITE_TRANSIENT);

				ret = sqlite3_step(sqlStmt);
				if (ret!=SQLITE_DONE) {
					return BZRTP_ZIDCACHE_UNABLETOUPDATE;
				}
				sqlite3_finalize(sqlStmt);
				/* get the zuid created */
				*zuid = (int)sqlite3_last_insert_rowid(db);
				return 0;
johan's avatar
johan committed
440
			}
johan's avatar
johan committed
441 442
		} else { /* we had an error querying the DB... */
			return BZRTP_ZIDCACHE_UNABLETOREAD;
johan's avatar
johan committed
443 444
		}
	}
johan's avatar
johan committed
445

johan's avatar
johan committed
446 447 448 449
	/* retrieve value in column 0 */
	*zuid = sqlite3_column_int(sqlStmt, 0);
	sqlite3_finalize(sqlStmt);

johan's avatar
johan committed
450
	return 0;
johan's avatar
johan committed
451 452
}

453
/**
johan's avatar
johan committed
454 455 456 457
 * @brief Write(insert or update) data in cache, adressing it by zuid (ZID/URI binding id used in cache)
 * 		Get arrays of column names, values to be inserted, lengths of theses values
 *		All three arrays must be the same lenght: columnsCount
 * 		If the row isn't present in the given table, it will be inserted
458
 *
johan's avatar
johan committed
459 460 461 462 463 464 465
 * @param[in/out]	dbPointer	Pointer to an already opened sqlite db
 * @param[in]		zuid		The DB internal id to adress the correct row(binding between local uri and peer ZID+URI)
 * @param[in]		tableName	The name of the table to write in the db, must already exists. Null terminated string
 * @param[in]		columns		An array of null terminated strings containing the name of the columns to update
 * @param[in]		values		An array of buffers containing the values to insert/update matching the order of columns array
 * @param[in]		lengths		An array of integer containing the lengths of values array buffer matching the order of columns array
 * @param[in]		columnsCount	length common to columns,values and lengths arrays
466
 *
johan's avatar
johan committed
467
 * @return 0 on succes, error code otherwise
468
 */
johan's avatar
johan committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
int bzrtp_cache_write(void *dbPointer, int zuid, char *tableName, char **columns, uint8_t **values, size_t *lengths, uint8_t columnsCount) {
	char *stmt=NULL;
	int ret,i,j;
	sqlite3_stmt *sqlStmt = NULL;
	sqlite3 *db = (sqlite3 *)dbPointer;
	char *insertColumnsString=NULL;
	int insertColumnsStringLength=0;

	if (dbPointer == NULL) { /* we are running cacheless */
		return BZRTP_ZIDCACHE_RUNTIME_CACHELESS;
	}

	/* As the zuid row may not be already present in the table, we must run an insert or update SQL command which is not provided by sqlite3 */
	/* UPSERT strategy: run update first and check for changes */

	/* do not perform any check on table name or columns name, the sql statement will just fail when they are wrong */
	/* prepare the columns list string, use the %w formatting option as it shall protect us from anything in the column name  */
	for (i=0; i<columnsCount; i++) {
		insertColumnsStringLength += strlen(columns[i])+5; /* +5 for =?, */
	}
	insertColumnsString = (char *)malloc(++insertColumnsStringLength); /*+1 to add NULL termination */
	sqlite3_snprintf(insertColumnsStringLength,insertColumnsString,"%w=?",columns[0]);
	j=strlen(insertColumnsString);
	for (i=1; i<columnsCount; i++) {
		sqlite3_snprintf(insertColumnsStringLength-j,insertColumnsString+j,", %w=?",columns[i]);
		j=strlen(insertColumnsString);
	}

	stmt = sqlite3_mprintf("UPDATE %w SET %s WHERE zuid=%d;", tableName, insertColumnsString, zuid);
	free(insertColumnsString);
	ret = sqlite3_prepare_v2(db, stmt, -1, &sqlStmt, NULL);
	sqlite3_free(stmt);
	if (ret != SQLITE_OK) {
		return BZRTP_ZIDCACHE_UNABLETOUPDATE;
	}

	/* bind given values */
	for (i=0; i<columnsCount; i++) {
		sqlite3_bind_blob(sqlStmt, i+1, values[i], lengths[i], SQLITE_TRANSIENT);/* i+1 because index of sql bind is 1 based */
	}

	ret = sqlite3_step(sqlStmt);
	sqlite3_finalize(sqlStmt);

	if (ret!=SQLITE_DONE) {
		return BZRTP_ZIDCACHE_UNABLETOUPDATE;
	}

	/* now check we managed to update a row */
	if (sqlite3_changes(db)==0) { /* update failed: we must insert the row */
Ghislain MARY's avatar
Ghislain MARY committed
519
		char *valuesBindingString = alloca(2*columnsCount+3);
johan's avatar
johan committed
520 521 522 523 524 525 526 527 528 529
		insertColumnsStringLength+=6; /* +6 to add the initial 'zuid, ' to column list */
		insertColumnsString = (char *)malloc(insertColumnsStringLength+6);
		sqlite3_snprintf(insertColumnsStringLength,insertColumnsString,"%w","zuid");
		sprintf(valuesBindingString,"?");

		j=strlen(insertColumnsString);
		for (i=0; i<columnsCount; i++) {
			sqlite3_snprintf(insertColumnsStringLength-j,insertColumnsString+j,", %w",columns[i]);
			j=strlen(insertColumnsString);
			sprintf(valuesBindingString+2*i+1,",?"); /*2 char (,?) for each column plus the initial ? for the zuid column */
johan's avatar
johan committed
530
		}
johan's avatar
johan committed
531 532 533 534 535
		stmt = sqlite3_mprintf("INSERT INTO %w (%s) VALUES(%s);", tableName, insertColumnsString, valuesBindingString);
		free(insertColumnsString);
		ret = sqlite3_prepare_v2(db, stmt, -1, &sqlStmt, NULL);
		if (ret != SQLITE_OK) {
			return BZRTP_ZIDCACHE_UNABLETOUPDATE;
536 537
		}

johan's avatar
johan committed
538 539 540 541
		/* bind given values */
		sqlite3_bind_int(sqlStmt, 1, zuid);
		for (i=0; i<columnsCount; i++) { /* index of sql bind is 1 based */
			sqlite3_bind_blob(sqlStmt, i+2, values[i], lengths[i], SQLITE_TRANSIENT); /* i+2 because of zuid which is first and 1 based index in binding */
johan's avatar
johan committed
542
		}
543

johan's avatar
johan committed
544 545
		ret = sqlite3_step(sqlStmt);
		sqlite3_finalize(sqlStmt);
546

johan's avatar
johan committed
547 548 549 550
		/* there is a foreign key binding on zuid, which make it impossible to insert a row in zrtp table without an existing zuid */
		/* if it fails it is at this point: TODO: add a specific error return value for this case */
		if (ret!=SQLITE_DONE) {
			return BZRTP_ZIDCACHE_UNABLETOUPDATE;
johan's avatar
johan committed
551
		}
johan's avatar
johan committed
552
	}
johan's avatar
johan committed
553 554 555 556

	return 0;
}

557
/**
johan's avatar
johan committed
558 559 560 561
 * @brief Read data from specified table/columns from cache adressing it by zuid (ZID/URI binding id used in cache)
 * 		Get arrays of column names, values to be read, and the number of colums to be read
 *		Produce an array of values(uint8_t arrays) and a array of corresponding lengths
 *		Values memory is allocated by this function and must be freed by caller
562
 *
johan's avatar
johan committed
563 564 565 566 567 568 569
 * @param[in/out]	dbPointer	Pointer to an already opened sqlite db
 * @param[in]		zuid		The DB internal id to adress the correct row(binding between local uri and peer ZID+URI)
 * @param[in]		tableName	The name of the table to read in the db. Null terminated string
 * @param[in]		columns		An array of null terminated strings containing the name of the columns to read, the array's length  is columnsCount
 * @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
 * @param[out]		lengths		An array of integer containing the lengths of values array buffer read
 * @param[in]		columnsCount	length common to columns,values and lengths arrays
570
 *
johan's avatar
johan committed
571
 * @return 0 on succes, error code otherwise
572
 */
johan's avatar
johan committed
573 574 575 576 577 578 579 580 581 582 583
int bzrtp_cache_read(void *dbPointer, int zuid, char *tableName, char **columns, uint8_t **values, size_t *lengths, uint8_t columnsCount) {
	char *stmt=NULL;
	int ret,i,j;
	sqlite3_stmt *sqlStmt = NULL;
	sqlite3 *db = (sqlite3 *)dbPointer;
	char *readColumnsString=NULL;
	int readColumnsStringLength=0;

	if (dbPointer == NULL) { /* we are running cacheless */
		return BZRTP_ZIDCACHE_RUNTIME_CACHELESS;
	}
584

johan's avatar
johan committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
	/* do not perform any check on table name or columns name, the sql statement will just fail when they are wrong */
	/* prepare the columns list string, use the %w formatting option as it shall protect us from anything in the column name  */
	for (i=0; i<columnsCount; i++) {
		readColumnsStringLength += strlen(columns[i])+5; /* +2 for ', '*/
	}
	readColumnsString = (char *)malloc(++readColumnsStringLength); /*+1 to add NULL termination */
	sqlite3_snprintf(readColumnsStringLength,readColumnsString,"%w",columns[0]);
	j=strlen(readColumnsString);
	for (i=1; i<columnsCount; i++) {
		sqlite3_snprintf(readColumnsStringLength-j,readColumnsString+j,", %w",columns[i]);
		j=strlen(readColumnsString);
	}

	stmt = sqlite3_mprintf("SELECT %s FROM %w WHERE zuid=%d LIMIT 1;", readColumnsString, tableName, zuid);
	free(readColumnsString);
	ret = sqlite3_prepare_v2(db, stmt, -1, &sqlStmt, NULL);
	sqlite3_free(stmt);
	if (ret != SQLITE_OK) {
		return BZRTP_ZIDCACHE_UNABLETOREAD;
	}

	ret = sqlite3_step(sqlStmt);

	if (ret!=SQLITE_ROW) { /* Data was not found or request is not well formed, anyway we don't have the data so just return an error */
		sqlite3_finalize(sqlStmt);
		return BZRTP_ZIDCACHE_UNABLETOREAD;
611
	}
johan's avatar
johan committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

	/* retrieve values  */
	for (i=0; i<columnsCount; i++) {
		int length;
		length = sqlite3_column_bytes(sqlStmt, i);

		if (length>0) { /* we have rs1 */
			lengths[i] = length;
			values[i] = (uint8_t *)malloc(length*sizeof(uint8_t));
			memcpy(values[i], sqlite3_column_blob(sqlStmt, i), length);
		} else { /* data is null in DB */
			values[i] = NULL;
			lengths[i] = 0;
		}

	}
	sqlite3_finalize(sqlStmt);

	return 0;
}

633 634 635 636 637 638 639 640 641 642 643
/**
 * @brief Perform migration from xml version to sqlite3 version of cache
 *	Warning: new version of cache associate a ZID to each local URI, the old one did not
 *		the migration function will associate any data in the cache to the sip URI given in parameter which shall be the default URI
 * @param[in]		cacheXml	a pointer to an xmlDocPtr structure containing the old cache to be migrated
 * @param[in/out]	cacheSqlite	a pointer to an sqlite3 structure containing a cache initialised using bzrtp_cache_init function
 * @param[in]		selfURI		default sip URI for this end point, NULL terminated char
 *
 * @return	0 on success, BZRTP_ERROR_CACHEDISABLED when bzrtp was not compiled with cache enabled, BZRTP_ERROR_CACHEMIGRATIONFAILED on error during migration
 */
int bzrtp_cache_migration(void *cacheXmlPtr, void *cacheSqlite, const char *selfURI) {
644
#ifdef HAVE_LIBXML2
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
	if (cacheXmlPtr) {
		xmlDocPtr cacheXml = (xmlDocPtr)cacheXmlPtr;
		xmlNodePtr cur;
		xmlChar *selfZidHex=NULL;
		uint8_t selfZID[12];
		sqlite3 *db = (sqlite3 *)cacheSqlite;
		sqlite3_stmt *sqlStmt = NULL;
		int ret;

		/* parse the cache to get the selfZID and insert it in sqlcache */
		cur = xmlDocGetRootElement(cacheXml);
		/* if we found a root element, parse its children node */
		if (cur!=NULL)
		{
			cur = cur->xmlChildrenNode;
		}
		selfZidHex = NULL;
		while (cur!=NULL) {
			if ((!xmlStrcmp(cur->name, (const xmlChar *)"selfZID"))){ /* self ZID found, extract it */
				selfZidHex = xmlNodeListGetString(cacheXml, cur->xmlChildrenNode, 1);
				bctbx_str_to_uint8(selfZID, selfZidHex, 24);
				break;
			}
			cur = cur->next;
		}
		/* did we found a self ZID? */
		if (selfZidHex == NULL) {
			bctbx_warning("ZRTP/LIME cache migration: Failed to parse selfZID");
			return BZRTP_ERROR_CACHEMIGRATIONFAILED;
		}

		/* insert the selfZID in cache, associate it to default local sip:uri in case we have more than one */
		bctbx_message("ZRTP/LIME cache migration: found selfZID %.24s link it to default URI %s in SQL cache", selfZidHex, selfURI);
		xmlFree(selfZidHex);

		ret = sqlite3_prepare_v2(db, "INSERT INTO ziduri (zid,selfuri,peeruri) VALUES(?,?,?);", -1, &sqlStmt, NULL);
		if (ret != SQLITE_OK) {
			bctbx_warning("ZRTP/LIME cache migration: Failed to insert selfZID");
			return BZRTP_ERROR_CACHEMIGRATIONFAILED;
		}
		sqlite3_bind_blob(sqlStmt, 1, selfZID, 12, SQLITE_TRANSIENT);
		sqlite3_bind_text(sqlStmt, 2, selfURI,-1,SQLITE_TRANSIENT);
		sqlite3_bind_text(sqlStmt, 3, "self",-1,SQLITE_TRANSIENT);

		ret = sqlite3_step(sqlStmt);
		if (ret!=SQLITE_DONE) {
			bctbx_warning("ZRTP/LIME cache migration: Failed to insert selfZID");
			return BZRTP_ERROR_CACHEMIGRATIONFAILED;
		}
		sqlite3_finalize(sqlStmt);

		/* loop over all the peer node in the xml cache and get from them : uri(can be more than one), ZID, rs1, rs2, pvs, sndKey, rcvKey, sndSId, rcvSId, sndIndex, rcvIndex, valid */
		/* some of these may be missing(pvs, valid, rs2) but we'll consider them NULL */
		/* aux and pbx secrets were not used, so don't even bother looking for them */
		cur = xmlDocGetRootElement(cacheXml)->xmlChildrenNode;

		while (cur!=NULL) { /* loop on all peer nodes */
			if ((!xmlStrcmp(cur->name, (const xmlChar *)"peer"))) { /* found a peer node, check if there is a sipURI node in it (other nodes are just ignored) */
				int i;
				xmlNodePtr peerNodeChildren = cur->xmlChildrenNode;
				xmlChar *nodeContent = NULL;
				xmlChar *peerZIDString = NULL;
				uint8_t peerZID[12];
				uint8_t peerZIDFound=0;
				xmlChar *peerUri[128]; /* array to contain all the peer uris found in one node */
				/* hopefully they won't be more than 128(it would mean some peer has more than 128 accounts and we called all of them...) */
				int peerUriIndex=0; /* index of previous array */
				char *zrtpColNames[] = {"rs1", "rs2", "pvs"};
				uint8_t *zrtpColValues[] = {NULL, NULL, NULL};
				size_t zrtpColExpectedLengths[] = {32,32,1};
				size_t zrtpColLengths[] = {0,0,0};

				char *limeColNames[] = {"sndKey", "rcvKey", "sndSId", "rcvSId", "sndIndex", "rcvIndex", "valid"};
				uint8_t *limeColValues[] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
				size_t limeColExpectedLengths[] = {32,32,32,32,4,4,8};
				size_t limeColLengths[] = {0,0,0,0,0,0,0};

				/* check all the children nodes to retrieve all information we may get */
				while (peerNodeChildren!=NULL && peerUriIndex<128) {
					if (!xmlStrcmp(peerNodeChildren->name, (const xmlChar *)"uri")) { /* found a peer an URI node, get the content */
						peerUri[peerUriIndex] = xmlNodeListGetString(cacheXml, peerNodeChildren->xmlChildrenNode, 1);
						peerUriIndex++;
					}

					if (!xmlStrcmp(peerNodeChildren->name, (const xmlChar *)"ZID")) {
						peerZIDString = xmlNodeListGetString(cacheXml, peerNodeChildren->xmlChildrenNode, 1);
						bctbx_str_to_uint8(peerZID, peerZIDString, 24);

						peerZIDFound=1;
					}

					for (i=0; i<3; i++) {
						if (!xmlStrcmp(peerNodeChildren->name, (const xmlChar *)zrtpColNames[i])) {
							nodeContent = xmlNodeListGetString(cacheXml, peerNodeChildren->xmlChildrenNode, 1);
							zrtpColValues[i] = (uint8_t *)bctbx_malloc(zrtpColExpectedLengths[i]);
							bctbx_str_to_uint8(zrtpColValues[i], nodeContent, 2*zrtpColExpectedLengths[i]);
							zrtpColLengths[i]=zrtpColExpectedLengths[i];
						}
					}

					for (i=0; i<7; i++) {
						if (!xmlStrcmp(peerNodeChildren->name, (const xmlChar *)limeColNames[i])) {
							nodeContent = xmlNodeListGetString(cacheXml, peerNodeChildren->xmlChildrenNode, 1);
							limeColValues[i] = (uint8_t *)bctbx_malloc(limeColExpectedLengths[i]);
							bctbx_str_to_uint8(limeColValues[i], nodeContent, 2*limeColExpectedLengths[i]);
							limeColLengths[i]=limeColExpectedLengths[i];
						}
					}

					peerNodeChildren = peerNodeChildren->next;
					xmlFree(nodeContent);
					nodeContent=NULL;
				}

				if (peerUriIndex>0 && peerZIDFound==1) { /* we found at least an uri in this peer node, extract the keys all other informations */
					/* retrieve all the informations */

					/* loop over all the uri founds */
					for (i=0; i<peerUriIndex; i++) {
						char *stmt = NULL;
						int zuid;
						/* create the entry in the ziduri table (it will give us the zuid to be used to insert infos in lime and zrtp tables) */
						/* we could use directly the bzrtp_cache_getZuid function, but avoid useless query by directly inserting the data */
						stmt = sqlite3_mprintf("INSERT INTO ziduri (zid,selfuri,peeruri) VALUES(?,?,?);");
						ret = sqlite3_prepare_v2(db, stmt, -1, &sqlStmt, NULL);
						if (ret != SQLITE_OK) {
							bctbx_warning("ZRTP/LIME cache migration: Failed to insert peer ZID %s", peerUri[i]);
							return BZRTP_ERROR_CACHEMIGRATIONFAILED;
						}
						sqlite3_free(stmt);

						sqlite3_bind_blob(sqlStmt, 1, peerZID, 12, SQLITE_TRANSIENT);
						sqlite3_bind_text(sqlStmt, 2, selfURI, -1, SQLITE_TRANSIENT);
						sqlite3_bind_text(sqlStmt, 3, (const char *)(peerUri[i]), -1, SQLITE_TRANSIENT);

						ret = sqlite3_step(sqlStmt);
						if (ret!=SQLITE_DONE) {
							bctbx_warning("ZRTP/LIME cache migration: Failed to insert peer ZID %s", peerUri[i]);
							return BZRTP_ERROR_CACHEMIGRATIONFAILED;
						}
						sqlite3_finalize(sqlStmt);
						/* get the zuid created */
						zuid = (int)sqlite3_last_insert_rowid(db);

						bctbx_message("ZRTP/LIME cache migration: Inserted self %s peer %s ZID %s sucessfully with zuid %d\n", selfURI, peerUri[i], peerZIDString, zuid);
						xmlFree(peerUri[i]);
						peerUri[i]=NULL;

						/* now insert data in the zrtp and lime table, keep going even if it fails */
						if ((ret=bzrtp_cache_write(db, zuid, "zrtp", zrtpColNames, zrtpColValues, zrtpColLengths, 3)) != 0) {
							bctbx_error("ZRTP/LIME cache migration: could not insert data in zrtp table, return value %x", ret);
						}
						if ((ret=bzrtp_cache_write(db, zuid, "lime", limeColNames, limeColValues, limeColLengths, 7)) != 0) {
							bctbx_error("ZRTP/LIME cache migration: could not insert data in lime table, return value %x", ret);
						}
					}
				}
				bctbx_free(zrtpColValues[0]);
				bctbx_free(zrtpColValues[1]);
				bctbx_free(zrtpColValues[2]);
				for (i=0; i<7; i++) {
					bctbx_free(limeColValues[i]);
				}
				xmlFree(peerZIDString);
			}
			cur = cur->next;
		}
		return 0;
	}
	return BZRTP_ERROR_CACHEMIGRATIONFAILED;
815 816 817 818
#else /* HAVE_LIBXML2 */
	bctbx_error("ZRTP/LIME cache migration: could not perform migration as LIBMXL2 is not linked to bzrtp.");
	return BZRTP_ERROR_CACHEMIGRATIONFAILED;
#endif /* HAVE_LIBXML2 */
819 820
}

johan's avatar
johan committed
821 822 823
#else /* ZIDCACHE_ENABLED */

int bzrtp_getSelfZID(void *dbPointer, const char *selfURI, uint8_t selfZID[12], bctbx_rng_context_t *RNGContext) {
824
	/* we are running cacheless, return a random number */
johan's avatar
johan committed
825 826 827 828 829
	if (RNGContext != NULL) {
		bctbx_rng_get(RNGContext, selfZID, 12);
	} else {
		return BZRTP_CACHE_DATA_NOTFOUND;
	}
830 831
	return 0; 
}
johan's avatar
johan committed
832 833

int bzrtp_getPeerAssociatedSecrets(bzrtpContext_t *context, uint8_t peerZID[12]) {
834
		if (context == NULL) {
johan's avatar
johan committed
835
		return BZRTP_ZIDCACHE_INVALID_CONTEXT;
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
	}

	/* resert cached secret buffer */
	free(context->cachedSecret.rs1);
	free(context->cachedSecret.rs2);
	free(context->cachedSecret.pbxsecret);
	free(context->cachedSecret.auxsecret);
	context->cachedSecret.rs1 = NULL;
	context->cachedSecret.rs1Length = 0;
	context->cachedSecret.rs2 = NULL;
	context->cachedSecret.rs2Length = 0;
	context->cachedSecret.pbxsecret = NULL;
	context->cachedSecret.pbxsecretLength = 0;
	context->cachedSecret.auxsecret = NULL;
	context->cachedSecret.auxsecretLength = 0;
	context->cachedSecret.previouslyVerifiedSas = 0;

	return 0;
}
johan's avatar
johan committed
855

johan's avatar
johan committed
856 857
int bzrtp_cache_write(void *dbPointer, int zuid, char *tableName, char **columns, uint8_t **values, size_t *lengths, uint8_t columnsCount) {
	return BZRTP_ERROR_CACHEDISABLED;
858 859
}

johan's avatar
johan committed
860 861 862
int bzrtp_cache_read(void *dbPointer, int zuid, char *tableName, char **columns, uint8_t **values, size_t *lengths, uint8_t columnsCount) {
	return BZRTP_ERROR_CACHEDISABLED;
}
863 864 865 866

int bzrtp_cache_migration(xmlDocPtr cacheXml, void *cacheSqlite, const char *selfURI) {
	return BZRTP_ERROR_CACHEDISABLED;
}
johan's avatar
johan committed
867
#endif /* ZIDCACHE_ENABLED */