summaryrefslogtreecommitdiff
path: root/plugins/SecureIM/src/svcs_rsa.cpp
blob: e60c19430a2cb1e04c2212fc81f330ece407cdcc (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
#include "commonheaders.h"

pRSA_EXPORT mir_exp = NULL;
RSA_IMPORT imp =
{
	rsa_inject,
	rsa_check_pub,
	rsa_notify
};

BOOL rsa_4096 = 0;

int __cdecl rsa_inject(HANDLE context, LPCSTR msg)
{
	pUinKey ptr = getUinCtx(context); if (!ptr) return 0;

	Sent_NetLog("rsa_inject: '%s'", msg);

	int len = (int)mir_strlen(msg) + 1;
	LPSTR buf = (LPSTR)mir_alloc(LEN_SECU + len);
	memcpy(buf, SIG_SECU, LEN_SECU);
	memcpy(buf + LEN_SECU, msg, len);
	// îòïðàâëÿåì ñîîáùåíèå
	splitMessageSend(ptr, buf);
	mir_free(buf);
	return 1;
}

#define MSGSIZE 1024

int __cdecl rsa_check_pub(HANDLE context, PBYTE pub, int pubLen, PBYTE sig, int sigLen)
{
	int v = 0, k = 0;
	pUinKey ptr = getUinCtx(context); if (!ptr) return 0;
	LPSTR cnm = (LPSTR)mir_alloc(NAMSIZE); getContactNameA(ptr->hContact, cnm);
	LPSTR uin = (LPSTR)mir_alloc(KEYSIZE); getContactUinA(ptr->hContact, uin);
	LPSTR msg = (LPSTR)mir_alloc(MSGSIZE);
	LPSTR sha = mir_strdup(to_hex(sig, sigLen));
	LPSTR sha_old = NULL;

	Sent_NetLog("rsa_check_pub: %s %s %s", cnm, uin, sha);

	DBVARIANT dbv;
	dbv.type = DBVT_BLOB;
	if (db_get(ptr->hContact, MODULENAME, "rsa_pub", &dbv) == 0) {
		k = 1;
		PBYTE buf = (PBYTE)alloca(sigLen); int len;
		mir_exp->rsa_get_hash((PBYTE)dbv.pbVal, dbv.cpbVal, (PBYTE)buf, &len);
		sha_old = mir_strdup(to_hex(buf, len));
		db_free(&dbv);
	}
	if (bAAK) {
		if (k)	mir_snprintf(msg, MSGSIZE, Translate(sim523), cnm, uin, sha, sha_old);
		else	mir_snprintf(msg, MSGSIZE, Translate(sim521), cnm, uin, sha);
		showPopupKRmsg(ptr->hContact, msg);
		HistoryLog(ptr->hContact, msg);
		v = 1;
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("rsa_check_pub: auto accepted");
#endif
	}
	else {
		if (k) mir_snprintf(msg, MSGSIZE, Translate(sim522), cnm, sha, sha_old);
		else	mir_snprintf(msg, MSGSIZE, Translate(sim520), cnm, sha);
		v = (msgbox(0, msg, MODULENAME, MB_YESNO | MB_ICONQUESTION) == IDYES);
#if defined(_DEBUG) || defined(NETLIB_LOG)
		Sent_NetLog("rsa_check_pub: manual accepted %d", v);
#endif
	}
	if (v) {
		db_set_blob(ptr->hContact, MODULENAME, "rsa_pub", pub, pubLen);
		ptr->keyLoaded = true;
	}
	mir_free(cnm);
	mir_free(uin);
	mir_free(msg);
	mir_free(sha);
	SAFE_FREE(sha_old);
	return v;
}

void __cdecl rsa_notify(HANDLE context, int state)
{
	pUinKey ptr = getUinCtx(context); if (!ptr) return;
	LPCSTR msg = NULL;

	Sent_NetLog("rsa_notify: 0x%x", state);

	switch (state) {
	case 1:
		showPopupEC(ptr->hContact);
		ShowStatusIconNotify(ptr->hContact);
		waitForExchange(ptr, 2); // äîñûëàåì ñîîáùåíèÿ èç î÷åðåäè
		return;

	case -1: // ñåññèÿ ðàçîðâàíà ïî îøèáêå, íåâåðíûé òèï ñîîáùåíèÿ
		msg = sim501; break;
	case -2: // ñåññèÿ ðàçîðâàíà ïî îøèáêå äðóãîé ñòîðîíîé
		msg = sim502; break;
	case -5: // îøèáêà äåêîäèðîâàíèÿ AES ñîîáùåíèÿ
		msg = sim505; break;
	case -6: // îøèáêà äåêîäèðîâàíèÿ RSA ñîîáùåíèÿ
		msg = sim506; break;
	case -7: // òàéìàóò óñòàíîâêè ñîåäèíåíèÿ (10 ñåêóíä)
		msg = sim507; break;
	case -8: // ñåññèÿ ðàçîðâàíà ïî ïðè÷èíå "disabled"
		msg = sim508; break;
	case -0x10: // ñåññèÿ ðàçîðâàíà ïî îøèáêå
	case -0x21:
	case -0x22:
	case -0x23:
	case -0x24:
	case -0x32:
	case -0x33:
	case -0x34:
	case -0x40:
	case -0x50:
	case -0x60:
	{
		char buf[1024];
		mir_snprintf(buf, sim510, -state);
		showPopupDCmsg(ptr->hContact, buf);
		ShowStatusIconNotify(ptr->hContact);
		if (ptr->cntx) deleteRSAcntx(ptr);
		waitForExchange(ptr, 3); // äîñûëàåì íåøèôðîâàíî
	}
		return;

	case -3: // ñîåäèíåíèå ðàçîðâàíî âðó÷íóþ
	case -4: // ñîåäèíåíèå ðàçîðâàíî âðó÷íóþ äðóãîé ñòîðîíîé
		showPopupDC(ptr->hContact);
		ShowStatusIconNotify(ptr->hContact);
		if (ptr->cntx) deleteRSAcntx(ptr);
		waitForExchange(ptr, 3); // äîñûëàåì íåøèôðîâàíî
		return;

	default:
		return;
	}
	showPopupDCmsg(ptr->hContact, msg);
	ShowStatusIconNotify(ptr->hContact);
	if (ptr->cntx) deleteRSAcntx(ptr);
	waitForExchange(ptr, 3); // äîñûëàåì íåøèôðîâàíî
}

void sttGenerateRSA(LPVOID)
{
	char priv_key[4096]; int priv_len;
	char pub_key[4096]; int pub_len;

	mir_exp->rsa_gen_keypair(CPP_MODE_RSA_4096);
	mir_exp->rsa_get_keypair(CPP_MODE_RSA_4096, (PBYTE)&priv_key, &priv_len, (PBYTE)&pub_key, &pub_len);

	db_set_blob(NULL, MODULENAME, "rsa_priv", priv_key, priv_len);
	db_set_blob(NULL, MODULENAME, "rsa_pub", pub_key, pub_len);
	rsa_4096 = 1;
}

// çàãðóæàåò ïàáëèê-êëþ÷ â RSA êîíòåêñò
BYTE loadRSAkey(pUinKey ptr)
{
	if (!ptr->keyLoaded) {
		DBVARIANT dbv;
		dbv.type = DBVT_BLOB;
		if (db_get(ptr->hContact, MODULENAME, "rsa_pub", &dbv) == 0) {
			ptr->keyLoaded = mir_exp->rsa_set_pubkey(ptr->cntx, dbv.pbVal, dbv.cpbVal);
			Sent_NetLog("loadRSAkey %d", ptr->keyLoaded);
			db_free(&dbv);
		}
	}
	return ptr->keyLoaded;
}

// ñîçäàåò RSA êîíòåêñò
void createRSAcntx(pUinKey ptr)
{
	if (!ptr->cntx) {
		ptr->cntx = cpp_create_context(CPP_MODE_RSA);
		ptr->keyLoaded = 0;
	}
}

// ïåðåñîçäàåò RSA êîíòåêñò
void resetRSAcntx(pUinKey ptr)
{
	if (ptr->cntx) {
		cpp_delete_context(ptr->cntx);
		ptr->cntx = cpp_create_context(CPP_MODE_RSA);
		ptr->keyLoaded = 0;
	}
}

// óäàëÿåò RSA êîíòåêñò
void deleteRSAcntx(pUinKey ptr)
{
	cpp_delete_context(ptr->cntx);
	ptr->cntx = 0;
	ptr->keyLoaded = 0;
}