-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlvlibtls.cpp
340 lines (296 loc) · 9.87 KB
/
lvlibtls.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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
334
335
336
337
338
// lvlibtls.cpp : Defines the exported functions for the DLL application.
//
#define LVLIBTLS_EXPORTS
#include "lvlibtls.h" //this header
#include "libressl\include\tls.h" //libtls
#include "cintools\extcode.h" //labview
#include "openssl\ssl.h" //required by libtls
#include <string.h>
#include <vector>
#include <stdlib.h>
//define output queue size -- ie how many written chunks may be in memory at once
#ifndef LVLIBTLSOUTQSIZE
#define LVLIBTLSOUTQSIZE 10000
#endif
#ifdef __cplusplus
extern "C" {
#endif
//internal callback called by libtls when it needs to read or write data
//cb_arg is initialized in connect/accept to be the lvSocketContext referred to as lvstate
ssize_t lvReadCallback(struct tls *ctx, void *buf, size_t buflen, void *cb_arg);
ssize_t lvWriteCallback(struct tls *ctx, const void *buf, size_t buflen, void *cb_arg);
//once a TCP connection is established, this creates the lv context, initializes tls ref, etc needed for a client TLS connection
//initializes lvstate
//returns >= if OK
LVLIBTLS_API int tlsConnectLvSocket(struct tls * clientTls, const char *servername, lvSocketContext ** lvstate)
{
if (clientTls && servername && lvstate) {
(*lvstate) = new lvSocketContext{};
return tls_connect_cbs(clientTls, lvReadCallback, lvWriteCallback, (*lvstate), servername);
}
else {
return -1;
}
}
//server equivalent to connect -- creates TLS structures needed to manage the server, called on each new TCP connection
//initializes lvstate
//returns >= if OK
LVLIBTLS_API int tlsAcceptLvSocket(struct tls * serverTls, struct tls ** newConnection, lvSocketContext ** lvstate)
{
if (serverTls && newConnection && lvstate) {
(*lvstate) = new lvSocketContext{};
auto ret = tls_accept_cbs(serverTls, newConnection, lvReadCallback, lvWriteCallback, (*lvstate));
return ret;
}
else {
return -1;
}
}
//release memory associated with lvSocketContext created by connect/accept functions
//frees lvstate
LVLIBTLS_API void tlsFreeLvState(lvSocketContext * lvstate)
{
if (lvstate) {
//both queues could have initialized data, so we need to pop everything off and deallocate
while (!lvstate->fromTlstoNetQ.empty()) {
auto elem = lvstate->fromTlstoNetQ.front();
DSDisposeHandle(elem);
lvstate->fromTlstoNetQ.pop();
}
while (!lvstate->toTlsfromNetQ.empty()) {
auto elem = lvstate->toTlsfromNetQ.front();
DSDisposeHandle(elem.s);
lvstate->toTlsfromNetQ.pop();
}
delete lvstate;
}
}
//Initializes tls lib, can be called any number of times NOT CONCURRENT.
//Used as initializer callback in LV
LVLIBTLS_API MgErr lvLibReserve(InstanceDataPtr * i)
{
tls_init();
return mgNoErr;
}
//Writes plaintext string to libtls, which encrypts it
//Once completed, data is avail using GetOutputData
//Returns libtls error if the tls state machine requires more data (eg re-handshake)
//returns -1 if passed invalid pointers
LVLIBTLS_API int tlsWrite(tls * clientTls, LStrHandle data)
{
if (!clientTls || !data) {
return -1;
}
//avoid copies because of us, pass lv buffer data directly to tls
auto ret = tls_write(clientTls, LHStrBuf(data), LHStrLen(data));
return ret;
}
//helper to read a password-protected file using libressl lib
//Returns -1 if passed bad ptrs
LVLIBTLS_API int tlsReadFile(LStrHandle filename, LStrHandle data, LStrHandle password)
{
if (filename && data && password && LHStrBuf(filename) && LHStrLen(filename) > 0) {
//copy lvstrs to cstrs
char* pw = nullptr;
if (password && LHStrBuf(password) && LHStrLen(password) > 0) {
pw = new char[LHStrLen(password) + 1];
pw[LHStrLen(password)] = 0;
memcpy(pw, LHStrBuf(password), LHStrLen(password));
}
auto file = new char[LHStrLen(filename)+1];
file[LHStrLen(filename)] = 0;
memcpy(file, LHStrBuf(filename), LHStrLen(filename));
//call the tls load to do the actual decrypting
//can't do this directly from LV due to return value -- need to free the memory
size_t len = 0;
auto filestr = tls_load_file(file, &len, pw);
delete[] file;
delete[] pw;
//libtls cstr->lvstr
if (!filestr) {
return -1;
}
DSSetHandleSize(data, len+sizeof(LStr::cnt));
LStrLen(LHStrPtr(data)) = len;
memcpy(LHStrBuf(data), filestr, len);
for (size_t i = 0; i < len; i++)
{
filestr[i] = 0;
}
//tls_unload_file was not available at time of writing
//should be avail in later versions
return len;
}
else {
return -1;
}
}
//called when libTLS is ready to put data on the network, initialized in connect/accept
//cb_arg is initialized in connect/accept to be the lvSocketContext referred to as lvstate
//returns general error (-1) to libtls if passed bad ptrs
//returns TLS_WANT_POLLOUT if output queue is filled and we need lv to do a tcp write
//return buflen (>=0) if success
ssize_t lvWriteCallback(struct tls *ctx, const void *buf, size_t buflen, void *cb_arg) {
if (cb_arg && buf && ctx && buflen > 0) {
auto lvstate = (lvSocketContext*)cb_arg;
if ((lvstate->fromTlstoNetQ.size()) >= LVLIBTLSOUTQSIZE ) {
//"filled" queue
return TLS_WANT_POLLOUT;
}
//if we have space, allocate new lvstr and copy data (*buf) into it
auto bufStr = (LStrHandle)DSNewHandle(buflen + sizeof(LStr::cnt));
if (!bufStr) {
return -1;
}
LStrLen(LHStrPtr(bufStr)) = buflen;
memcpy(LHStrBuf(bufStr), buf, buflen);
try {
lvstate->fromTlstoNetQ.push(bufStr);
}
catch (...) {
if(bufStr) {
DSDisposeHandle(bufStr);
}
return -1;
}
return buflen;
}
else { return -1; }
}
//Labview calls this function to see if there is any data it should write to the tcp connection
//return -2 if an unknown error occurred
//return -1 if ptrs invalid
//else return queue size BEFORE POP, >=0 (eg if == 0, data is invalid and the q is empty)
LVLIBTLS_API int tlsGetOutputData(lvSocketContext * lvstate, LStrHandle* data)
{
try
{
if (lvstate && data) {
LStrHandle buf;
auto q = &(lvstate->fromTlstoNetQ);
if (q->empty()) {
return 0;
}
buf = q->front();
q->pop();
if (!buf) {
return -2;
}
//swap the pointers to avoid calling DSNewHandle
//Most likely, *data is a null handle anyway
auto junkData = (*data);
(*data) = buf;
if (junkData) {
DSDisposeHandle(junkData);
}
return (q->size()) + 1;
}
else {
return -1;
}
}
catch (...)
{
return -2;
}
}
//Once ProvidedInputData, this function is called to process the data and decrypt.
//Returns libtls error codes if more of the tcp stream is needed.
//return -1 if ptrs invalid
//Returns data len >=0 if OK
LVLIBTLS_API int tlsRead(tls * clientTls, LStrHandle data, size_t maxSize)
{
if (!clientTls || !data || 0 == maxSize) {
return -1;
}
DSSetHandleSize(data, maxSize + sizeof(LStr::cnt));
auto ret = tls_read(clientTls, LHStrBuf(data), maxSize);
if (ret <= 0) {
DSSetHSzClr(data, sizeof(LStr::cnt)); //empty string
}
else {
DSSetHandleSize(data, ret + sizeof(LStr::cnt));
LStrLen(LHStrPtr(data)) = ret;
}
return ret;
}
//If labview reads from a tcp connection, call this function to provide that string to libressl for decryption
//return -1 if bad ptr or malloc failed
//return -2 if unknown internal error (ie if enqueue fails)
LVLIBTLS_API int tlsProvideInputData(lvSocketContext* lvstate, LStrHandle* data)
{
if (lvstate && data && *data && LHStrPtr(*data) && LHStrBuf(*data) && LHStrLen(*data) > 0) {
auto emptyStr = (LStrHandle)DSNewHClr(sizeof(LStr::cnt)); //empty lstrhandle
if (!emptyStr) {
return -1;
}
//just swap pointers rather than copying
//labview will get an empty string out of *data as a result
LStrHandle localData = *data;
*data = emptyStr;
try {
(lvstate->toTlsfromNetQ).push(lvOffsetString{ localData, 0 });
}
catch (...)
{
return -2;
}
return 1;
}
else {
return -1;
}
}
//called when libTLS is ready to read data from the 'network', initialized in connect/accept
//cb_arg is initialized in connect/accept to be the lvSocketContext referred to as lvstate
//returns general error (-1) to libtls if passed bad ptrs
//returns TLS_WANT_POLLIN if input queue is empty and we need lv to do a tcp read
//return buflen (>=0) if success
ssize_t lvReadCallback(struct tls *ctx, void *buf, size_t buflen, void *cb_arg) {
if (cb_arg && buf && ctx && buflen > 0) {
auto tlsStr = (unsigned char *)buf;
auto lvstate = (lvSocketContext*)cb_arg;
size_t currLen = 0;
//currLen updated as data is read
while (currLen < buflen) {
if (lvstate->toTlsfromNetQ.empty()) {
//no more data avail -- if no data was available, we need lv to do a read
break;
}
else {
//the first element of the queue will have some data in it
//grab it and copy it into tls buf
auto needLen = buflen - currLen;
lvOffsetString& netBuf = lvstate->toTlsfromNetQ.front();
auto netStr = netBuf.s;
auto netOffset = netBuf.offset;
if (needLen >= (LHStrLen(netStr) - netOffset)) {
//want more data than avail from this buffer, so we consume this packet
auto moveLen = (LHStrLen(netStr) - netOffset);
memcpy((tlsStr + currLen), LHStrBuf(netStr) + netOffset, moveLen);
currLen += moveLen;
//consume:
DSDisposeHandle(netStr);
lvstate->toTlsfromNetQ.pop();
}
else {
//does not consume full data set, so adjust offset and leave on queue
memcpy((tlsStr + currLen), LHStrBuf(netStr) + netOffset, needLen);
netBuf.offset += needLen; //netbuf is a *ref* to the first element in the queue
currLen += needLen;
//dont pop, keep data in queue
}
}
}
if (currLen) { //if we got any data -- we don't have to fill the buffer, necessarily
return currLen;
}
else { //if we never got any data, tell lv to do a read
return TLS_WANT_POLLIN;
}
}
else { return -1; }
}
#ifdef __cplusplus
}
#endif