summaryrefslogtreecommitdiff
path: root/plugins/Dbx_sqlite/src/dbcrypt.cpp
blob: 8ba9ae64fab43bacd2eff7b68adc2ab4d40c3e44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "stdafx.h"

/////////////////////////////////////////////////////////////////////////////////////////
// Saving encryption key in a database

STDMETHODIMP_(BOOL) CDbxSQLite::ReadCryptoKey(MBinBuffer &buf)
{
	DBVARIANT dbv = {};
	dbv.type = DBVT_BLOB;
	if (GetContactSetting(0, "CryptoEngine", "StoredKey", &dbv))
		return FALSE;

	buf.append(dbv.pbVal, dbv.cpbVal);
	return TRUE;
}

STDMETHODIMP_(BOOL) CDbxSQLite::StoreCryptoKey()
{
	size_t iKeyLength = m_crypto->getKeyLength();
	BYTE *pKey = (BYTE*)_alloca(iKeyLength);
	m_crypto->getKey(pKey, iKeyLength);

	DBCONTACTWRITESETTING dbcws = { "CryptoEngine", "StoredKey" };
	dbcws.value.type = DBVT_BLOB;
	dbcws.value.cpbVal = (WORD)iKeyLength;
	dbcws.value.pbVal = pKey;
	WriteContactSetting(0, &dbcws);

	SecureZeroMemory(pKey, iKeyLength);
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Saving encryption flag

STDMETHODIMP_(BOOL) CDbxSQLite::ReadEncryption()
{
	DBVARIANT dbv = {};
	dbv.type = DBVT_BYTE;
	return (GetContactSetting(0, "CryptoEngine", "DatabaseEncryption", &dbv)) ? false : dbv.bVal != 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Saving provider in a database

STDMETHODIMP_(CRYPTO_PROVIDER*) CDbxSQLite::ReadProvider()
{
	DBVARIANT dbv = {};
	dbv.type = DBVT_BLOB;
	if (GetContactSetting(0, "CryptoEngine", "Provider", &dbv))
		return nullptr;

	if (dbv.type != DBVT_BLOB) 
		return nullptr;

	auto *pProvider = Crypto_GetProvider(LPCSTR(dbv.pbVal));
	FreeVariant(&dbv);
	return pProvider;
}

STDMETHODIMP_(BOOL) CDbxSQLite::StoreProvider(CRYPTO_PROVIDER *pProvider)
{
	DBCONTACTWRITESETTING dbcws = { "CryptoEngine", "Provider" };
	dbcws.value.type = DBVT_BLOB;
	dbcws.value.pbVal = (PBYTE)pProvider->pszName;
	dbcws.value.cpbVal = (WORD)mir_strlen(pProvider->pszName) + 1;
	WriteContactSetting(0, &dbcws);
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Toggles full/partial encryption mode

STDMETHODIMP_(BOOL) CDbxSQLite::EnableEncryption(BOOL)
{
	return FALSE;
}