-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrivialMailSender.cpp
1283 lines (1153 loc) · 49.1 KB
/
TrivialMailSender.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "TrivialMailSender.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
struct IUnknown; // Workaround Windows header problem.
#define SECURITY_WIN32
#include <WS2tcpip.h> // Must come first. https://stackoverflow.com/questions/1372480/c-redefinition-header-files-winsock2-h
#include <Windns.h>
#include <Windows.h>
#include <security.h>
#include <schannel.h>
#pragma comment(lib, "Crypt32.lib")
#pragma comment(lib, "Dnsapi.lib")
#pragma comment(lib, "Rpcrt4.lib")
#pragma comment(lib, "Secur32.lib")
#pragma comment (lib, "Ws2_32.lib")
#define PRINTF_SERVER_COMMUNICATION 0
static const size_t IoBufferSize = 65536;
static const size_t DomainNameSizeMax = 512;
static const size_t ReceiveBufferSize = 1024;
static const size_t SendMessageBufferSize = 8096;
// ##################################################################################################################
// ################ General helper functions ########################################################################
// ##################################################################################################################
static inline void FormatDateTime(char* OutStr, size_t OutCapacity, time_t DateTime)
{
tm Loc;
if (gmtime_s(&Loc, &DateTime))
{
*OutStr = '\0';
return;
}
static const char Weeks[][4] = { "Sun", "Mon", "Tue", "Wen", "Thu", "Fri", "Sat" };
static const char Months[][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
snprintf(OutStr, OutCapacity, "%s, %d %s %d %02d:%02d:%02d +0000", Weeks[Loc.tm_wday],
Loc.tm_mday, Months[Loc.tm_mon], 1900 + Loc.tm_year, Loc.tm_hour, Loc.tm_min, Loc.tm_sec);
}
static inline void GetGUID(char* OutStr, size_t OutCapacity)
{
GUID Guid;
if (UuidCreate(&Guid) != S_OK)
Guid = {};
snprintf(OutStr, OutCapacity, "%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX", // https://stackoverflow.com/a/18114061
Guid.Data1, Guid.Data2, Guid.Data3,
Guid.Data4[0], Guid.Data4[1], Guid.Data4[2], Guid.Data4[3],
Guid.Data4[4], Guid.Data4[5], Guid.Data4[6], Guid.Data4[7]);
}
static inline void FindEmailAddress(char const* EmailAddress, size_t EmailAddressLength, size_t* OutBegin, size_t* OutLength)
{
size_t Begin = 0;
size_t Length = EmailAddressLength;
// If there are any brackets like <> then they contain the email address...
char const* const Bra = reinterpret_cast<char const*>(memchr(EmailAddress + Begin, '<', Length));
char const* const Ket = reinterpret_cast<char const*>(memchr(EmailAddress + Begin, '>', Length));
if (Bra && Ket && Bra < Ket)
{
Begin = Bra + 1 - EmailAddress;
Length = Ket - (Bra + 1);
}
else
{
// Othrwise maybe thre are brackets like (). In that case, the email address is in front.
char const* const Bra2 = reinterpret_cast<char const*>(memchr(EmailAddress + Begin, '(', Length));
char const* const Ket2 = reinterpret_cast<char const*>(memchr(EmailAddress + Begin, ')', Length));
if (Bra2 && Ket2 && Bra2 < Ket2)
{
Begin = 0;
Length = Bra2 - EmailAddress;
}
}
// Trim unnecessary whitespace...
while (Length && isspace(EmailAddress[Begin]))
++Begin;
while (Length && isspace(EmailAddress[Begin + Length - 1]))
--Length;
// Return...
*OutBegin = Begin;
*OutLength = Length;
}
static inline void FindServerAddress(char const* EmailAddress, size_t EmailAddressLength, size_t* OutBegin, size_t* OutLength)
{
FindEmailAddress(EmailAddress, EmailAddressLength, OutBegin, OutLength);
char const* At = reinterpret_cast<char const*>(memchr(EmailAddress + *OutBegin, '@', *OutLength));
if (At)
{
const size_t End = *OutBegin + *OutLength;
*OutBegin = At + 1 - EmailAddress;
*OutLength = End - *OutBegin;
}
}
static inline size_t EncodeBase64GetSize(size_t InLength)
{
return 4 * ((InLength + 2) / 3);
}
static inline void EncodeBase64(char* OutStr, void const* InPtr, size_t InLength)
{
static const alignas(64) char Table[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
char* OutIter = OutStr;
unsigned char const* InPtr2 = reinterpret_cast<unsigned char const*>(InPtr);
// Process bulk input...
const size_t FastInputLength = (InLength / 3) * 3;
size_t InputIndex = 0, OutputIndex = 0;
while (InputIndex < FastInputLength)
{
const uint32_t Byte0 = InPtr2[InputIndex++];
const uint32_t Byte1 = InPtr2[InputIndex++];
const uint32_t Byte2 = InPtr2[InputIndex++];
const uint32_t Combined = (Byte0 << 16) + (Byte1 << 8) + Byte2;
*(OutIter++) = Table[(Combined >> 18) & 0x3F];
*(OutIter++) = Table[(Combined >> 12) & 0x3F];
*(OutIter++) = Table[(Combined >> 6) & 0x3F];
*(OutIter++) = Table[(Combined >> 0) & 0x3F];
}
// Process rest...
switch (InLength - FastInputLength)
{
case 2:
{
const uint32_t Byte0 = InPtr2[InputIndex++];
const uint32_t Byte1 = InPtr2[InputIndex++];
const uint32_t Combined = (Byte0 << 16) + (Byte1 << 8);
*(OutIter++) = Table[(Combined >> 18) & 0x3F];
*(OutIter++) = Table[(Combined >> 12) & 0x3F];
*(OutIter++) = Table[(Combined >> 6) & 0x3F];
*(OutIter++) = '=';
}
break;
case 1:
{
const uint32_t Combined = static_cast<uint32_t>(InPtr2[InputIndex++]) << 16;
*(OutIter++) = Table[(Combined >> 18) & 0x3F];
*(OutIter++) = Table[(Combined >> 12) & 0x3F];
*(OutIter++) = '=';
*(OutIter++) = '=';
}
break;
}
}
// ##################################################################################################################
// ################ Security helper functions #######################################################################
// ##################################################################################################################
// These functions are very closely resembling the example code from http://www.coastrd.com/c-schannel-smtp
// and the still available versions of that i.e. in the TortoiseGit source code.
static inline SECURITY_STATUS CreateCredentials(SecurityFunctionTableW* SftPtr, CredHandle* CredentialsStructPtr)
{
// Build Schannel credential structure. Currently, this sample only specifies the protocol to be used
// (and optionally the certificate, of course). Real applications may wish to specify other parameters as well.
SCHANNEL_CRED SchannelCred = {};
SchannelCred.dwVersion = SCHANNEL_CRED_VERSION;
SchannelCred.grbitEnabledProtocols = 0; // SP_PROT_TLS1; // SP_PROT_PCT1; SP_PROT_SSL2; SP_PROT_SSL3; 0=default
//DWORD cSupportedAlgs = 0;
//ALG_ID rgbSupportedAlgs[16];
//rgbSupportedAlgs[cSupportedAlgs++] = CALG_DH_EPHEM; CALG_RSA_KEYX;
//if (cSupportedAlgs)
//{
// SchannelCred.cSupportedAlgs = cSupportedAlgs;
// SchannelCred.palgSupportedAlgs = rgbSupportedAlgs;
//}
SchannelCred.dwFlags |= SCH_CRED_NO_DEFAULT_CREDS;
// The "SCH_CRED_MANUAL_CRED_VALIDATION" flag is specified because this sample verifies the server certificate manually.
// Applications that expect to run with WinNT, Win9x, or WinME should specify this flag and also manually verify the server
// certificate. Applications running with newer versions of Windows can remove this flag, in which case the
// "InitializeSecurityContext" function will validate the server certificate automatically.
SchannelCred.dwFlags |= SCH_CRED_MANUAL_CRED_VALIDATION;
// Create an SSPI credential...
TimeStamp Expiry;
const SECURITY_STATUS Status = SftPtr->AcquireCredentialsHandle(nullptr, // Name of principal
const_cast<wchar_t*>(UNISP_NAME), // Name of package
SECPKG_CRED_OUTBOUND, // Flags indicating use
nullptr, // Pointer to login ID
&SchannelCred, // Package specific data
nullptr, // Pointer to GetKey() func
nullptr, // Value to pass to GetKey()
CredentialsStructPtr, // (out) Cred Handle
&Expiry); // (out) Lifetime (optional)
return Status;
}
static inline SECURITY_STATUS ClientHandshakeLoop(SOCKET Socket, SecurityFunctionTableW* SftPtr, CredHandle* CredentialsStructPtr, CtxtHandle* ContextStructPtr, bool DoInitialRead, SecBuffer* ExtraDataPtr)
{
static constexpr DWORD SSPIFlags =
ISC_REQ_SEQUENCE_DETECT | ISC_REQ_REPLAY_DETECT | ISC_REQ_CONFIDENTIALITY |
ISC_RET_EXTENDED_ERROR | ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_STREAM;
// Allocate data buffer...
char* IoBuffer = reinterpret_cast<char*>(malloc(IoBufferSize));
if (!IoBuffer)
return SEC_E_INSUFFICIENT_MEMORY;
DWORD IoBufferCount = 0;
bool DoRead = DoInitialRead;
// Loop until the handshake is finished or an error occurs...
SECURITY_STATUS Result = SEC_I_CONTINUE_NEEDED;
while (Result == SEC_I_CONTINUE_NEEDED || Result == SEC_E_INCOMPLETE_MESSAGE || Result == SEC_I_INCOMPLETE_CREDENTIALS)
{
if (0 == IoBufferCount || Result == SEC_E_INCOMPLETE_MESSAGE) // Read data from server.
{
if (DoRead)
{
const int DataCount = recv(Socket, IoBuffer + IoBufferCount, IoBufferSize - IoBufferCount, 0);
if (DataCount == SOCKET_ERROR)
{
Result = SEC_E_INTERNAL_ERROR;
break;
}
else if (DataCount == 0)
{
// printf("**** Server unexpectedly disconnected\n");
Result = SEC_E_INTERNAL_ERROR;
break;
}
// printf("%d bytes of handshake data received\n", cbData);
IoBufferCount += DataCount;
}
else
DoRead = true;
}
// Set up the input buffers. Buffer 0 is used to pass in data received from the server. Schannel will consume some or all
// of this. Leftover data (if any) will be placed in buffer 1 and given a buffer type of "SECBUFFER_EXTRA".
SecBuffer InBuffers[2];
InBuffers[0].pvBuffer = IoBuffer;
InBuffers[0].cbBuffer = IoBufferCount;
InBuffers[0].BufferType = SECBUFFER_TOKEN;
InBuffers[1].pvBuffer = nullptr;
InBuffers[1].cbBuffer = 0;
InBuffers[1].BufferType = SECBUFFER_EMPTY;
SecBufferDesc InBuffer;
InBuffer.cBuffers = 2;
InBuffer.pBuffers = InBuffers;
InBuffer.ulVersion = SECBUFFER_VERSION;
// Set up the output buffers. These are initialized to nullptr so as to make it less likely we'll attempt to free random garbage later.
SecBuffer OutBuffers[1];
OutBuffers[0].pvBuffer = nullptr;
OutBuffers[0].BufferType = SECBUFFER_TOKEN;
OutBuffers[0].cbBuffer = 0;
SecBufferDesc OutBuffer;
OutBuffer.cBuffers = 1;
OutBuffer.pBuffers = OutBuffers;
OutBuffer.ulVersion = SECBUFFER_VERSION;
// Call "InitializeSecurityContext"...
DWORD SSPIOutFlags;
TimeStamp Expiry;
Result = SftPtr->InitializeSecurityContext(CredentialsStructPtr, ContextStructPtr, nullptr, SSPIFlags, 0, SECURITY_NATIVE_DREP, &InBuffer, 0, nullptr, &OutBuffer, &SSPIOutFlags, &Expiry);
// If "InitializeSecurityContext" was successful (or if the error was one of the special extended ones),
// SocketSend the contends of the output buffer to the server.
if (Result == SEC_E_OK || Result == SEC_I_CONTINUE_NEEDED || FAILED(Result) && (SSPIOutFlags & ISC_RET_EXTENDED_ERROR))
{
if (OutBuffers[0].cbBuffer != 0 && OutBuffers[0].pvBuffer != nullptr)
{
const int cbData = send(Socket, reinterpret_cast<char const*>(OutBuffers[0].pvBuffer), OutBuffers[0].cbBuffer, 0);
if (cbData == SOCKET_ERROR || cbData == 0)
{
// printf("**** Error %d SocketSending data to server (2)\n", WSAGetLastError());
SftPtr->FreeContextBuffer(OutBuffers[0].pvBuffer);
SftPtr->DeleteSecurityContext(ContextStructPtr);
free(IoBuffer);
return SEC_E_INTERNAL_ERROR;
}
// printf("%d bytes of handshake data sent\n", cbData);
// Free output buffer.
SftPtr->FreeContextBuffer(OutBuffers[0].pvBuffer);
OutBuffers[0].pvBuffer = nullptr;
}
}
// If "InitializeSecurityContext" returned "SEC_E_INCOMPLETE_MESSAGE", then we need to read more data from the server and try again.
if (Result == SEC_E_INCOMPLETE_MESSAGE)
continue;
// If "InitializeSecurityContext" returned "SEC_E_OK", then the handshake completed successfully.
if (Result == SEC_E_OK)
{
// If the "extra" buffer contains data, this is encrypted application protocol layer stuff. It needs to be saved.
// The application layer will later decrypt it with "DecryptMessage".
//printf("Handshake was successful\n");
if (InBuffers[1].BufferType == SECBUFFER_EXTRA)
{
ExtraDataPtr->pvBuffer = LocalAlloc(LMEM_FIXED, InBuffers[1].cbBuffer);
if (ExtraDataPtr->pvBuffer == nullptr)
{
free(IoBuffer);
return SEC_E_INSUFFICIENT_MEMORY;
}
memmove(ExtraDataPtr->pvBuffer, IoBuffer + (IoBufferCount - InBuffers[1].cbBuffer), InBuffers[1].cbBuffer);
ExtraDataPtr->cbBuffer = InBuffers[1].cbBuffer;
ExtraDataPtr->BufferType = SECBUFFER_TOKEN;
// printf("%d bytes of app data was bundled with handshake data\n", ExtraDataPtr->cbBuffer);
}
else
{
ExtraDataPtr->pvBuffer = nullptr;
ExtraDataPtr->cbBuffer = 0;
ExtraDataPtr->BufferType = SECBUFFER_EMPTY;
}
break; // Quit.
}
// Check for fatal error...
if (FAILED(Result))
{
// printf("**** Error 0x%x returned by InitializeSecurityContext (2)\n", scRet);
break;
}
// If "InitializeSecurityContext" returned "SEC_I_INCOMPLETE_CREDENTIALS", then the server just requested client authentication.
if (Result == SEC_I_INCOMPLETE_CREDENTIALS)
{
// Busted. The server has requested client authentication and the credential we supplied didn't contain a client certificate.
// This function will read the list of trusted certificate authorities ("issuers") that was received from the server
// and attempt to find a usable client certificate that was issued by one of these. If this function is successful,
// then we will connect using the new certificate. Otherwise, we will attempt to connect anonymously (using our current credentials).
//GetNewClientCredentials(CredentialsStructPtr, ContextStructPtr);
// Go around again....
DoRead = FALSE;
Result = SEC_I_CONTINUE_NEEDED;
continue;
}
// Copy any leftover data from the "extra" buffer, and go around again...
if (InBuffers[1].BufferType == SECBUFFER_EXTRA)
{
memmove(IoBuffer, IoBuffer + (IoBufferCount - InBuffers[1].cbBuffer), InBuffers[1].cbBuffer);
IoBufferCount = InBuffers[1].cbBuffer;
}
else
IoBufferCount = 0;
}
// Delete the security context in the case of a fatal error...
if (FAILED(Result))
SftPtr->DeleteSecurityContext(ContextStructPtr);
free(IoBuffer);
return Result;
}
static inline SECURITY_STATUS PerformClientHandshake(SOCKET Socket, SecurityFunctionTableW* SftPtr, CredHandle* CredentialsStructPtr, CtxtHandle* ContextStructPtr, char const* ServerName, SecBuffer* ExtraDataPtr)
{
static constexpr DWORD SSPIFlags = ISC_REQ_SEQUENCE_DETECT | ISC_REQ_REPLAY_DETECT | ISC_REQ_CONFIDENTIALITY |
ISC_RET_EXTENDED_ERROR | ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_STREAM;
// Convert to UTF-16...
wchar_t Utf16ServerName[DomainNameSizeMax];
if (!MultiByteToWideChar(CP_UTF8, 0, ServerName, -1, Utf16ServerName, DomainNameSizeMax))
return GetLastError();
// Initiate a "ClientHello" message and generate a token...
SecBuffer OutBuffers[1];
OutBuffers[0].pvBuffer = nullptr;
OutBuffers[0].BufferType = SECBUFFER_TOKEN;
OutBuffers[0].cbBuffer = 0;
SecBufferDesc OutBuffer;
OutBuffer.cBuffers = 1;
OutBuffer.pBuffers = OutBuffers;
OutBuffer.ulVersion = SECBUFFER_VERSION;
DWORD SSPIOutFlags;
TimeStamp Expiry;
const SECURITY_STATUS Result = SftPtr->InitializeSecurityContext(CredentialsStructPtr, nullptr, Utf16ServerName, SSPIFlags, 0, SECURITY_NATIVE_DREP, nullptr, 0, ContextStructPtr, &OutBuffer, &SSPIOutFlags, &Expiry);
if (Result != SEC_I_CONTINUE_NEEDED)
{
// printf("**** Error %d returned by InitializeSecurityContext (1)\n", scRet);
return Result;
}
// SocketSend response to server if there is one...
if (OutBuffers[0].cbBuffer != 0 && OutBuffers[0].pvBuffer != nullptr)
{
const int DataCount = send(Socket, reinterpret_cast<char const*>(OutBuffers[0].pvBuffer), OutBuffers[0].cbBuffer, 0);
if (DataCount == SOCKET_ERROR || DataCount == 0)
{
// printf("**** Error %d SocketSending data to server (1)\n", WSAGetLastError());
SftPtr->FreeContextBuffer(OutBuffers[0].pvBuffer);
SftPtr->DeleteSecurityContext(ContextStructPtr);
return SEC_E_INTERNAL_ERROR;
}
// printf("%d bytes of handshake data sent\n", cbData);
SftPtr->FreeContextBuffer(OutBuffers[0].pvBuffer); // Free output buffer.
OutBuffers[0].pvBuffer = nullptr;
}
return ClientHandshakeLoop(Socket, SftPtr, CredentialsStructPtr, ContextStructPtr, true, ExtraDataPtr);
}
static inline DWORD VerifyServerCertificate(CERT_CONTEXT const* ServerCertPtr, char const* ServerName, DWORD dwCertFlags)
{
if (ServerCertPtr == nullptr || ServerName == nullptr)
return (DWORD)SEC_E_WRONG_PRINCIPAL;
// Convert to UTF-16...
wchar_t Utf16ServerName[DomainNameSizeMax];
if (!MultiByteToWideChar(CP_UTF8, 0, ServerName, -1, Utf16ServerName, DomainNameSizeMax))
return GetLastError();
// Build certificate chain...
static char const* const Usages[] = { szOID_PKIX_KP_SERVER_AUTH, szOID_SERVER_GATED_CRYPTO, szOID_SGC_NETSCAPE };
CERT_CHAIN_PARA ChainPara = {};
ChainPara.cbSize = sizeof(ChainPara);
ChainPara.RequestedUsage.dwType = USAGE_MATCH_TYPE_OR;
ChainPara.RequestedUsage.Usage.cUsageIdentifier = sizeof(Usages) / sizeof(char const*);
ChainPara.RequestedUsage.Usage.rgpszUsageIdentifier = const_cast<char**>(Usages);
CERT_CHAIN_CONTEXT const* ChainContextPtr = nullptr;
if (!CertGetCertificateChain(nullptr, ServerCertPtr, nullptr, ServerCertPtr->hCertStore, &ChainPara, 0, nullptr, &ChainContextPtr))
return GetLastError();
// Validate certificate chain...
HTTPSPolicyCallbackData PolicyHttps = {};
PolicyHttps.cbStruct = sizeof(HTTPSPolicyCallbackData);
PolicyHttps.dwAuthType = AUTHTYPE_SERVER;
PolicyHttps.fdwChecks = dwCertFlags;
PolicyHttps.pwszServerName = Utf16ServerName;
CERT_CHAIN_POLICY_PARA PolicyPara = {};
PolicyPara.cbSize = sizeof(PolicyPara);
PolicyPara.pvExtraPolicyPara = &PolicyHttps;
CERT_CHAIN_POLICY_STATUS PolicyStatus = {};
PolicyStatus.cbSize = sizeof(PolicyStatus);
if (!CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL, ChainContextPtr, &PolicyPara, &PolicyStatus))
{
const DWORD Status = GetLastError();
CertFreeCertificateChain(ChainContextPtr);
return Status;
}
if (PolicyStatus.dwError)
{
CertFreeCertificateChain(ChainContextPtr);
return PolicyStatus.dwError;
}
CertFreeCertificateChain(ChainContextPtr);
return SEC_E_OK;
}
static inline DWORD EncryptSocketSend(SOCKET Socket, SecurityFunctionTableW* SftPtr, CtxtHandle* ContextStructPtr, BYTE* IoBuffer, SecPkgContext_StreamSizes Sizes)
{
// http://msdn.microsoft.com/en-us/library/aa375378(VS.85).aspx
// The encrypted message is encrypted in place, overwriting the original contents of its buffer.
BYTE* const MessagePtr = IoBuffer + Sizes.cbHeader; // Offset by "header size"
const DWORD MessageLength = static_cast<DWORD>(strlen(reinterpret_cast<char*>(MessagePtr)));
// Encrypt the HTTP request...
SecBuffer Buffers[4];
Buffers[0].pvBuffer = IoBuffer; // Pointer to buffer 1
Buffers[0].cbBuffer = Sizes.cbHeader; // length of header
Buffers[0].BufferType = SECBUFFER_STREAM_HEADER; // Type of the buffer
Buffers[1].pvBuffer = MessagePtr; // Pointer to buffer 2
Buffers[1].cbBuffer = MessageLength; // length of the message
Buffers[1].BufferType = SECBUFFER_DATA; // Type of the buffer
Buffers[2].pvBuffer = MessagePtr + MessageLength; // Pointer to buffer 3
Buffers[2].cbBuffer = Sizes.cbTrailer; // length of the trailor
Buffers[2].BufferType = SECBUFFER_STREAM_TRAILER; // Type of the buffer
Buffers[3].pvBuffer = SECBUFFER_EMPTY; // Pointer to buffer 4
Buffers[3].cbBuffer = SECBUFFER_EMPTY; // length of buffer 4
Buffers[3].BufferType = SECBUFFER_EMPTY; // Type of the buffer 4
SecBufferDesc Message;
Message.ulVersion = SECBUFFER_VERSION; // Version number
Message.cBuffers = 4; // Number of buffers - must contain four SecBuffer structures.
Message.pBuffers = Buffers; // Pointer to array of buffers
const SECURITY_STATUS Result = SftPtr->EncryptMessage(ContextStructPtr, 0, &Message, 0); // must contain four SecBuffer structures.
if (FAILED(Result))
{
// printf("**** Error 0x%x returned by EncryptMessage\n", scRet);
return Result;
}
// SocketSend the encrypted data to the server...
return send(Socket, reinterpret_cast<char const*>(IoBuffer), Buffers[0].cbBuffer + Buffers[1].cbBuffer + Buffers[2].cbBuffer, 0);
}
static inline SECURITY_STATUS ReadDecrypt(SOCKET Socket, SecurityFunctionTableW* SftPtr, CredHandle* CredentialsStructPtr, CtxtHandle* ContextStructPtr, BYTE* IoBuffer, DWORD IoBufferLength)
{
// Calls "recv" for blocking socket read. (http://msdn.microsoft.com/en-us/library/ms740121(VS.85).aspx)
// The encrypted message is decrypted in place, overwriting the original contents of its buffer. (http://msdn.microsoft.com/en-us/library/aa375211(VS.85).aspx)
// Read data from server until done.
DWORD IoBufferCount = 0;
SECURITY_STATUS Result = 0;
do // Read some data.
{
if (IoBufferCount == 0 || Result == SEC_E_INCOMPLETE_MESSAGE) // get the data
{
const int DataCount = recv(Socket, reinterpret_cast<char*>(IoBuffer) + IoBufferCount, IoBufferLength - IoBufferCount, 0);
if (DataCount == SOCKET_ERROR)
{
// printf("**** Error %d reading data from server\n", WSAGetLastError());
Result = SEC_E_INTERNAL_ERROR;
break;
}
else if (DataCount == 0) // Server disconnected.
{
if (IoBufferCount)
{
// printf("**** Server unexpectedly disconnected\n");
Result = SEC_E_INTERNAL_ERROR;
return Result;
}
else
break; // All Done
}
else // Success.
{
// printf("%d bytes of (encrypted) application data received\n", cbData);
IoBufferCount += DataCount;
}
}
// Decrypt the received data...
SecBuffer Buffers[4];
Buffers[0].pvBuffer = IoBuffer;
Buffers[0].cbBuffer = IoBufferCount;
Buffers[0].BufferType = SECBUFFER_DATA; // Initial Type of the buffer 1
Buffers[1].BufferType = SECBUFFER_EMPTY; // Initial Type of the buffer 2
Buffers[2].BufferType = SECBUFFER_EMPTY; // Initial Type of the buffer 3
Buffers[3].BufferType = SECBUFFER_EMPTY; // Initial Type of the buffer 4
SecBufferDesc Message;
Message.ulVersion = SECBUFFER_VERSION; // Version number
Message.cBuffers = 4; // Number of buffers - must contain four SecBuffer structures.
Message.pBuffers = Buffers; // Pointer to array of buffers
Result = SftPtr->DecryptMessage(ContextStructPtr, &Message, 0, nullptr);
if (Result == SEC_I_CONTEXT_EXPIRED)
break; // Server signalled end of session
//if (scRet == SEC_E_INCOMPLETE_MESSAGE - Input buffer has partial encrypted record, read more
if (Result != SEC_E_OK && Result != SEC_I_RENEGOTIATE && Result != SEC_I_CONTEXT_EXPIRED)
return Result;
// Locate data and (optional) extra buffers...
SecBuffer* DataBufferPtr = nullptr;
SecBuffer* ExtraBufferPtr = nullptr;
for (int i = 1; i < 4; ++i)
{
if (DataBufferPtr == nullptr && Buffers[i].BufferType == SECBUFFER_DATA)
DataBufferPtr = &Buffers[i];
if (ExtraBufferPtr == nullptr && Buffers[i].BufferType == SECBUFFER_EXTRA)
ExtraBufferPtr = &Buffers[i];
}
// Display the decrypted data...
if (DataBufferPtr)
{
const DWORD length = DataBufferPtr->cbBuffer;
if (length) // check if last two chars are CRLF.
{
BYTE* const buff = static_cast<BYTE*>(DataBufferPtr->pvBuffer);
if (buff[length - 2] == 13 && buff[length - 1] == 10) // Found CRLF.
{
buff[length] = 0;
break;
}
}
}
// Move any "extra" data to the input buffer...
if (ExtraBufferPtr)
{
memmove(IoBuffer, ExtraBufferPtr->pvBuffer, ExtraBufferPtr->cbBuffer);
IoBufferCount = ExtraBufferPtr->cbBuffer;
}
else
IoBufferCount = 0;
// The server wants to perform another handshake sequence...
if (Result == SEC_I_RENEGOTIATE)
{
// printf("Server requested renegotiate!\n");
SecBuffer ExtraBuffer;
Result = ClientHandshakeLoop(Socket, SftPtr, CredentialsStructPtr, ContextStructPtr, false, &ExtraBuffer);
if (Result != SEC_E_OK)
return Result;
if (ExtraBuffer.pvBuffer) // Move any "extra" data to the input buffer...
{
memmove(IoBuffer, ExtraBuffer.pvBuffer, ExtraBuffer.cbBuffer);
IoBufferCount = ExtraBuffer.cbBuffer;
}
}
} while(true); // Loop till CRLF is found at the end of the data.
return SEC_E_OK;
}
static inline LONG SecurityDisconnectFromServer(SOCKET Socket, SecurityFunctionTableW* SftPtr, CredHandle* CredentialsStructPtr, CtxtHandle* ContextStructPtr)
{
DWORD Type = SCHANNEL_SHUTDOWN; // Notify schannel that we are about to close the connection.
SecBuffer OutBuffers[1];
OutBuffers[0].pvBuffer = &Type;
OutBuffers[0].BufferType = SECBUFFER_TOKEN;
OutBuffers[0].cbBuffer = sizeof(Type);
SecBufferDesc OutBuffer;
OutBuffer.cBuffers = 1;
OutBuffer.pBuffers = OutBuffers;
OutBuffer.ulVersion = SECBUFFER_VERSION;
const SECURITY_STATUS Status = SftPtr->ApplyControlToken(ContextStructPtr, &OutBuffer);
if (FAILED(Status))
{
// printf("**** Error 0x%x returned by ApplyControlToken\n", Status);
SftPtr->DeleteSecurityContext(ContextStructPtr); // Free the security context.
return Status;
}
// Build an SSL close notify message.
static constexpr DWORD SSPIFlags = ISC_REQ_SEQUENCE_DETECT | ISC_REQ_REPLAY_DETECT | ISC_REQ_CONFIDENTIALITY | ISC_RET_EXTENDED_ERROR | ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_STREAM;
OutBuffers[0].pvBuffer = nullptr;
OutBuffers[0].BufferType = SECBUFFER_TOKEN;
OutBuffers[0].cbBuffer = 0;
OutBuffer.cBuffers = 1;
OutBuffer.pBuffers = OutBuffers;
OutBuffer.ulVersion = SECBUFFER_VERSION;
DWORD SSPIOutFlags;
TimeStamp Expiry;
const SECURITY_STATUS Status2 = SftPtr->InitializeSecurityContext(CredentialsStructPtr, ContextStructPtr, nullptr, SSPIFlags, 0, SECURITY_NATIVE_DREP, nullptr, 0, ContextStructPtr, &OutBuffer, &SSPIOutFlags, &Expiry);
if (FAILED(Status2))
{
// printf("**** Error 0x%x returned by InitializeSecurityContext\n", Status);
SftPtr->DeleteSecurityContext(ContextStructPtr); // Free the security context.
return Status2;
}
BYTE* const MessagePtr = static_cast<BYTE*>(OutBuffers[0].pvBuffer);
const DWORD MessageLength = OutBuffers[0].cbBuffer;
// SocketSend the close notify message to the server.
if (MessagePtr != nullptr && MessageLength != 0)
{
const int cbData = send(Socket, reinterpret_cast<char const*>(MessagePtr), MessageLength, 0);
if (cbData == SOCKET_ERROR || cbData == 0)
{
const SECURITY_STATUS Result = WSAGetLastError();
SftPtr->DeleteSecurityContext(ContextStructPtr); // Free the security context.
return Result;
}
// printf("SocketSending Close Notify\n");
// printf("%d bytes of handshake data sent\n", cbData);
SftPtr->FreeContextBuffer(MessagePtr); // Free output buffer.
}
return Status2;
}
// ##################################################################################################################
// ################ Generic socket functions ########################################################################
// ##################################################################################################################
struct Socket
{
SOCKET SocketHandle = INVALID_SOCKET;
bool IsEncrypted = false;
// The next couple variables are used only with encrypted sockets...
DWORD IoBufferLength = 0;
BYTE* IoBuffer = nullptr;
SecurityFunctionTableW* SftPtr = nullptr;
CtxtHandle ContextStruct = {};
CredHandle CredentialsStruct = {};
SecPkgContext_StreamSizes Sizes = {};
};
static inline bool SocketOpen(Socket* This, char* OutErrMsg, size_t ErrMsgCapacity, char const* ServerName, unsigned short Port)
{
// Initialize Winsock...
WSADATA WsaDataObj;
if (const int Result = WSAStartup(MAKEWORD(2, 2), &WsaDataObj))
{
snprintf(OutErrMsg, ErrMsgCapacity, "\"WSAStartup\" failed with error code: %i", Result);
return false;
}
// Resolve server address...
char PortStr[16];
snprintf(PortStr, sizeof(PortStr), "%i", (int)Port);
addrinfo* ResultAddress;
addrinfo Hints = {};
Hints.ai_family = AF_UNSPEC;
Hints.ai_socktype = SOCK_STREAM;
Hints.ai_protocol = IPPROTO_TCP;
if (const int Result = getaddrinfo(ServerName, PortStr, &Hints, &ResultAddress))
{
snprintf(OutErrMsg, ErrMsgCapacity, "\"getaddrinfo\" failed with error code: %i", Result);
WSACleanup();
return false;
}
// Attempt to connect to an address until one succeeds...
for (addrinfo* Iter = ResultAddress; Iter; Iter = Iter->ai_next)
{
// Create a SOCKET for connecting to server...
This->SocketHandle = socket(Iter->ai_family, Iter->ai_socktype, Iter->ai_protocol);
if (This->SocketHandle == INVALID_SOCKET)
{
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(ResultAddress);
WSACleanup();
return false;
}
// Try connecting to server...
const int Result = connect(This->SocketHandle, Iter->ai_addr, (int)Iter->ai_addrlen);
if (Result != SOCKET_ERROR)
{
freeaddrinfo(ResultAddress);
return true;
}
closesocket(This->SocketHandle);
This->SocketHandle = INVALID_SOCKET;
}
freeaddrinfo(ResultAddress);
WSACleanup();
snprintf(OutErrMsg, ErrMsgCapacity, "Unable to connect to any of the addresses found by \"getaddrinfo\".");
return false;
}
static inline bool SocketStartSecurity(Socket* This, char* OutErrMsg, size_t ErrMsgCapacity, char const* ServerName)
{
This->SftPtr = InitSecurityInterfaceW();
if (const SECURITY_STATUS Status = CreateCredentials(This->SftPtr, &This->CredentialsStruct))
{
snprintf(OutErrMsg, ErrMsgCapacity, "%s failed with error code: %i.", "CreateCredentials", Status);
return false;
}
This->IsEncrypted = true;
SecBuffer ExtraData;
if (const SECURITY_STATUS Status = PerformClientHandshake(This->SocketHandle, This->SftPtr, &This->CredentialsStruct, &This->ContextStruct, ServerName, &ExtraData))
{
snprintf(OutErrMsg, ErrMsgCapacity, "%s failed with error code: %i.", "PerformClientHandshake", Status);
return false;
}
// Authenticate server's credentials and get server's certificate...
CERT_CONTEXT* RemoteCertContextPtr = nullptr;
if (const SECURITY_STATUS Status = This->SftPtr->QueryContextAttributes(&This->ContextStruct, SECPKG_ATTR_REMOTE_CERT_CONTEXT, static_cast<PVOID>(&RemoteCertContextPtr)))
{
snprintf(OutErrMsg, ErrMsgCapacity, "%s failed with error code: %i.", "QueryContextAttributes", Status);
return false;
}
if (const SECURITY_STATUS Status = VerifyServerCertificate(RemoteCertContextPtr, ServerName, 0))
{
snprintf(OutErrMsg, ErrMsgCapacity, "%s failed with error code: %i.", "VerifyServerCertificate", Status);
CertFreeCertificateContext(RemoteCertContextPtr);
return false;
}
// Create a buffer...
CertFreeCertificateContext(RemoteCertContextPtr);
if (const SECURITY_STATUS Status = This->SftPtr->QueryContextAttributes(&This->ContextStruct, SECPKG_ATTR_STREAM_SIZES, &This->Sizes))
{
snprintf(OutErrMsg, ErrMsgCapacity, "%s failed with error code: %i.", "QueryContextAttributes", Status);
return false;
}
This->IoBufferLength = This->Sizes.cbHeader + This->Sizes.cbMaximumMessage + This->Sizes.cbTrailer;
This->IoBuffer = static_cast<PBYTE>(LocalAlloc(LMEM_FIXED, This->IoBufferLength));
if (!This->IoBuffer)
{
snprintf(OutErrMsg, ErrMsgCapacity, "Could not allocate memory.");
return false;
}
SecureZeroMemory(This->IoBuffer, This->IoBufferLength);
return true;
}
static inline bool SocketSend(Socket* This, char* OutErrMsg, size_t ErrMsgCapacity, char const* ToSend, size_t ToSendSize)
{
if (This->IsEncrypted)
{
size_t SentSize = 0;
while (ToSendSize > SentSize)
{
const size_t ThisTimeSize = min(ToSendSize - SentSize, This->Sizes.cbMaximumMessage);
SecureZeroMemory(This->IoBuffer + This->Sizes.cbHeader, This->Sizes.cbMaximumMessage);
memcpy(This->IoBuffer + This->Sizes.cbHeader, ToSend + SentSize, ThisTimeSize);
DWORD cbData = EncryptSocketSend(This->SocketHandle, This->SftPtr, &This->ContextStruct, This->IoBuffer, This->Sizes);
if (cbData == SOCKET_ERROR || cbData == 0)
{
snprintf(OutErrMsg, ErrMsgCapacity, "A socket error occurred.");
return false;
}
SentSize += ThisTimeSize;
}
}
else if (send(This->SocketHandle, ToSend, static_cast<int>(ToSendSize), 0) != ToSendSize)
{
snprintf(OutErrMsg, ErrMsgCapacity, "\"SocketSend\" data failed.");
return false;
}
#if PRINTF_SERVER_COMMUNICATION
printf("Sent: %.*s", static_cast<int>(ToSendSize), ToSend);
#endif
return true;
}
static inline bool SocketSend(Socket* This, char* OutErrMsg, size_t ErrMsgCapacity, char const* ToSend)
{
return SocketSend(This, OutErrMsg, ErrMsgCapacity, ToSend, strlen(ToSend));
}
static inline bool SocketReceive(Socket* This, char* OutErrMsg, size_t ErrMsgCapacity, char* ReceiveBuffer, size_t ReceiveBufferSize)
{
if (This->IsEncrypted)
{
// TODO This is terrible as theire is no way to figure out how large the received data is.
const SECURITY_STATUS Status = ReadDecrypt(This->SocketHandle, This->SftPtr, &This->CredentialsStruct, &This->ContextStruct, This->IoBuffer, This->IoBufferLength);
if (Status != SEC_E_OK)
{
snprintf(OutErrMsg, ErrMsgCapacity, "Receiving encrypted data from the socket failed.");
return false;
}
SecureZeroMemory(ReceiveBuffer, ReceiveBufferSize);
memcpy(ReceiveBuffer, This->IoBuffer + This->Sizes.cbHeader, ReceiveBufferSize - 1);
}
else
{
const size_t ReceivedCount = recv(This->SocketHandle, ReceiveBuffer, static_cast<int>(ReceiveBufferSize - 1), 0);
if (ReceivedCount >= ReceiveBufferSize)
{
snprintf(OutErrMsg, ErrMsgCapacity, "Receiving data from the socket failed.");
return false;
}
ReceiveBuffer[ReceivedCount] = '\0';
}
#if PRINTF_SERVER_COMMUNICATION
printf("Received: %s", ReceiveBuffer);
#endif
return true;
}
static inline bool SocketGetResponse(Socket* This, char* OutErrMsg, size_t ErrMsgCapacity, char const* ExpectedCode)
{
const size_t ExpectedCodeLength = strlen(ExpectedCode);
char ReceiveBuffer[ReceiveBufferSize];
if (!SocketReceive(This, OutErrMsg, ErrMsgCapacity, ReceiveBuffer, ReceiveBufferSize))
return false;
if (memcmp(ReceiveBuffer, ExpectedCode, ExpectedCodeLength) != 0)
{
snprintf(OutErrMsg, ErrMsgCapacity, "Received invalid response: %s", ReceiveBuffer);
return false;
}
return true;
}
static inline void SocketClose(Socket* This)
{
if (This->IsEncrypted)
{
if (This->SftPtr)
{
SecurityDisconnectFromServer(This->SocketHandle, This->SftPtr, &This->CredentialsStruct, &This->ContextStruct);
This->SftPtr->DeleteSecurityContext(&This->ContextStruct);
This->SftPtr->FreeCredentialsHandle(&This->CredentialsStruct);
}
if (This->IoBuffer)
{
SecureZeroMemory(This->IoBuffer, This->IoBufferLength);
LocalFree(This->IoBuffer);
}
}
if (This->SocketHandle != INVALID_SOCKET)
{
shutdown(This->SocketHandle, SD_BOTH);
closesocket(This->SocketHandle);
}
WSACleanup();
}
// ##################################################################################################################
// ################ Public interface implementation #################################################################
// ##################################################################################################################
size_t TMS_SendEmails(
char* OutErrMsg,
size_t ErrMsgCapacity,
char const* SmtpHostServer,
unsigned short SmtpHostServerPort,
TMS_SecurityLevel SecurityLevelVar,
char const* AuthentificationUserName,
char const* AuthentificationPassword,
TMS_Mail const* Mails,
size_t MailCount)
{
// Open socket...
Socket SocketHandle = {};
if (!SocketOpen(&SocketHandle, OutErrMsg, ErrMsgCapacity, SmtpHostServer, SmtpHostServerPort))
return 0;
// Negotiate security...
bool Encrypt = false;
if (SecurityLevelVar <= WantTls)
{
if (!SocketGetResponse(&SocketHandle, OutErrMsg, ErrMsgCapacity, "220"))
return 0;
if (!SocketSend(&SocketHandle, OutErrMsg, ErrMsgCapacity, "STARTTLS\r\n"))
return 0;
if (SocketGetResponse(&SocketHandle, OutErrMsg, ErrMsgCapacity, "220"))
Encrypt = true;
else if (SecurityLevelVar == WantTls)
{
snprintf(OutErrMsg, ErrMsgCapacity, "Insufficient security by server.");
return 0;
}
}
else if (SecurityLevelVar == Ssl)
Encrypt = true;
// Start security...
if (Encrypt)
if (!SocketStartSecurity(&SocketHandle, OutErrMsg, ErrMsgCapacity, SmtpHostServer))
return SocketClose(&SocketHandle), 0;
if (SecurityLevelVar <= Ssl)
if (!SocketGetResponse(&SocketHandle, OutErrMsg, ErrMsgCapacity, "220"))
return SocketClose(&SocketHandle), 0;
// Get host name...
char HostName[DomainNameSizeMax] = {};
gethostname(HostName, DomainNameSizeMax - 7);
if (strchr(HostName, '.') == nullptr) // Make sure "helo" hostname can be interpreted as a FQDN.
strcat(HostName, ".local");
// Send welcome message...
char ServerMessage[SendMessageBufferSize];
sprintf_s(ServerMessage, "EHLO %s\r\n", HostName);
if (!SocketSend(&SocketHandle, OutErrMsg, ErrMsgCapacity, ServerMessage))
return SocketClose(&SocketHandle), 0;
if (!SocketGetResponse(&SocketHandle, OutErrMsg, ErrMsgCapacity, "250"))
return SocketClose(&SocketHandle), 0;
// Authentificate...
if (AuthentificationUserName && *AuthentificationUserName != '\0')
{
if (!SocketSend(&SocketHandle, OutErrMsg, ErrMsgCapacity, "auth login\r\n"))
return SocketClose(&SocketHandle), 0;
if (!SocketGetResponse(&SocketHandle, OutErrMsg, ErrMsgCapacity, "334"))
return SocketClose(&SocketHandle), 0;
// Send user name...
const size_t AuthentificationUserNameLength = strlen(AuthentificationUserName);
size_t Base64Size = EncodeBase64GetSize(AuthentificationUserNameLength);
if (Base64Size > sizeof(ServerMessage) - sizeof("\r\n"))
{
snprintf(OutErrMsg, ErrMsgCapacity, "Authentification user name too long to send.");
return SocketClose(&SocketHandle), 0;
}
EncodeBase64(ServerMessage, AuthentificationUserName, AuthentificationUserNameLength);
strcpy(ServerMessage + Base64Size, "\r\n");
if (!SocketSend(&SocketHandle, OutErrMsg, ErrMsgCapacity, ServerMessage))
return SocketClose(&SocketHandle), 0;
if (!SocketGetResponse(&SocketHandle, OutErrMsg, ErrMsgCapacity, "334"))
return SocketClose(&SocketHandle), 0;
// Send password...
if (AuthentificationPassword && AuthentificationPassword != '\0')
{