summaryrefslogtreecommitdiff
path: root/plugins/CryptoPP/src/cpp_cntx.cpp
blob: 72c35dcc96373ba11bdea5e0753eabb4313e2e43 (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
#include "commonheaders.h"


list<pCNTX> CL; // CL.size() CL.empty()

HANDLE thread_timeout = 0;

unsigned __stdcall sttTimeoutThread(LPVOID);


// get context data on context id
pCNTX get_context_on_id(HANDLE context) {

    if (	!thread_timeout ) {
	unsigned int tID;
	thread_timeout = (HANDLE) _beginthreadex(NULL, 0, sttTimeoutThread, NULL, 0, &tID);
    }

    if ( context ) {
	pCNTX cntx = (pCNTX) context;
	if ( cntx->header == HEADER && cntx->footer == FOOTER )
		return cntx;
#if defined(_DEBUG) || defined(NETLIB_LOG)
	else
		Sent_NetLog("get_context_on_id: corrupted context %08X", cntx);
#endif
    }
    return NULL;
}

/*
pCNTX get_context_on_id(HANDLE context) {
	return get_context_on_id((int)context);
}
*/

// create context, return context id
HANDLE __cdecl cpp_create_context(int mode) {

	list<pCNTX>::iterator i;
	pCNTX cntx = NULL;

	EnterCriticalSection(&localContextMutex);

	if ( !CL.empty() ) {
		for(i=CL.begin(); i!=CL.end(); ++i) { // èùåì ïóñòîé
			if ( (*i)->header==EMPTYH && (*i)->footer==EMPTYH ) {
	    	    cntx = (pCNTX) *i;
	    	    break;
			}
		}
	}

	if ( !cntx ) { // íå íàøëè - ñîçäàåì íîâûé
	    cntx = (pCNTX) malloc(sizeof(CNTX));
	    CL.push_back(cntx); // äîáàâèëè â êîíåö ñïèñêà
	}

	memset(cntx,0,sizeof(CNTX)); // î÷èùàåì âûäåëåííûé áëîê
	cntx->header = HEADER;
	cntx->footer = FOOTER;
	cntx->mode = mode;

	LeaveCriticalSection(&localContextMutex);

	return (HANDLE)cntx;
}


// delete context
void __cdecl cpp_delete_context(HANDLE context) {

	pCNTX tmp = get_context_on_id(context);
	if (tmp) { // ïîìå÷àåì íà óäàëåíèå
		tmp->deleted = gettime()+10; // áóäåò óäàëåí ÷åðåç 10 ñåêóíä
	}
}


// reset context
void __cdecl cpp_reset_context(HANDLE context) {

	pCNTX tmp = get_context_on_id(context);
	if (tmp)	cpp_free_keys(tmp);
}


// allocate pdata
PBYTE cpp_alloc_pdata(pCNTX ptr) {
	if ( !ptr->pdata ) {
	    if ( ptr->mode & MODE_PGP ) {
			ptr->pdata = (PBYTE) malloc(sizeof(PGPDATA));
			memset(ptr->pdata,0,sizeof(PGPDATA));
	    }
	    else
	    if ( ptr->mode & MODE_GPG ) {
			ptr->pdata = (PBYTE) malloc(sizeof(GPGDATA));
			memset(ptr->pdata,0,sizeof(GPGDATA));
	    }
	    else
	    if ( ptr->mode & MODE_RSA ) {
			rsa_alloc(ptr);
	    }
	    else {
			ptr->pdata = (PBYTE) malloc(sizeof(SIMDATA));
			memset(ptr->pdata,0,sizeof(SIMDATA));
	    }
	}
	return ptr->pdata;
}


// free memory from keys
void cpp_free_keys(pCNTX ptr) {

	SAFE_FREE(ptr->tmp);
	cpp_alloc_pdata(ptr);
	if ( ptr->mode & MODE_PGP ) {
		pPGPDATA p = (pPGPDATA) ptr->pdata;
		SAFE_FREE(p->pgpKeyID);
		SAFE_FREE(p->pgpKey);
		SAFE_FREE(ptr->pdata);
	}
	else
	if ( ptr->mode & MODE_GPG ) {
		pGPGDATA p = (pGPGDATA) ptr->pdata;
		SAFE_FREE(p->gpgKeyID);
		SAFE_FREE(ptr->pdata);
	}
	else
	if ( ptr->mode & MODE_RSA ) {
		rsa_free(ptr);
		SAFE_DELETE(ptr->pdata);
	}
	else {
		pSIMDATA p = (pSIMDATA) ptr->pdata;
		SAFE_FREE(p->PubA);
		SAFE_FREE(p->KeyA);
		SAFE_FREE(p->KeyB);
		SAFE_FREE(p->KeyX);
		SAFE_FREE(p->KeyP);
		SAFE_DELETE(p->dh);
		SAFE_FREE(ptr->pdata);
	}
}


// search not established RSA/AES contexts && clear deleted contexts
unsigned __stdcall sttTimeoutThread( LPVOID ) {

	list<pCNTX>::iterator i;
	while(1) {
		Sleep( 1000 ); // ðàç â ñåêóíäó
		if ( CL.empty() ) continue;
		u_int time = gettime();
		// ïðîáåãàåì âñå êîíòåêñòû
		EnterCriticalSection(&localContextMutex);
	    for(i=CL.begin(); i!=CL.end(); ++i) {
	    	pCNTX tmp = *i;
			if ( tmp->header!=HEADER || tmp->footer!=FOOTER ) continue;
			// ïðîïóñêàåì ïðèâàòíûå êëþ÷è
	    	if ( tmp->mode&MODE_PRIV_KEY ) continue;
	    	else
			if ( tmp->deleted && tmp->deleted < time ) {
				// óäàëèòü ïîìå÷åííûé äëÿ óäàëåíèÿ êîíòåêñò
				cpp_free_keys(tmp);
				tmp->deleted = 0;
				tmp->header = tmp->footer = EMPTYH;
	    	}
	    	else
			if ( tmp->mode&MODE_RSA && tmp->pdata ) {
				// ïðîâåðÿåì íå ïðîòóõëî ëè ñîåäèíåíèå
				pRSADATA p = (pRSADATA) tmp->pdata;
				if ( p->time && p->time < time ) {
					rsa_timeout((HANDLE)tmp,p);
				}
	    	}
	    } // for
		LeaveCriticalSection(&localContextMutex);
	} //while
}


// EOF