Debian Codegraph @

plugins/wfx/ftp/synapse/ssl_winssl_lib.pas

raw
1 {
2 SChannel to OpenSSL wrapper
3
4 Copyright (c) 2008 Boris Krasnovskiy
5 Copyright (c) 2013-2015 Alexander Koblov (pascal port)
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 }
20
21 unit ssl_winssl_lib;
22
23 {$mode delphi}
24
25 interface
26
27 uses
28 Windows, SynSock, JwaSspi, CTypes;
29
30 type
31 PSSL_CTX = ^SSL_CTX;
32 SSL_CTX = record
33 dwProtocol: DWORD;
34 bVerify: BOOL;
35 end;
36
37 PSSL_METHOD = ^SSL_METHOD;
38 SSL_METHOD = record
39 dummy: DWORD;
40 end;
41
42 PSSL = ^SSL;
43 SSL = record
44 s: TSocket;
45 ctx: PSSL_CTX;
46 hContext: CtxtHandle;
47 hCreds: CredHandle;
48 pbRecDataBuf: PByte;
49 cbRecDataBuf: LONG;
50 sbRecDataBuf: LONG;
51 pbIoBuffer: PByte;
52 cbIoBuffer: LONG;
53 sbIoBuffer: LONG;
54 exIoBuffer: BOOL;
55 rmshtdn: BOOL;
56 end;
57
58 function SSL_library_init(): cint; cdecl;
59 function SSL_set_fd(ssl: PSSL; fd: cint): cint; cdecl;
60 function SSL_CTX_new(method: PSSL_METHOD): PSSL_CTX; cdecl;
61 procedure SSL_CTX_free(ctx: PSSL_CTX); cdecl;
62 function SSL_new(ctx: PSSL_CTX): PSSL; cdecl;
63 procedure SSL_free(ssl: PSSL); cdecl;
64 function SSL_connect(ssl: PSSL): cint; cdecl;
65 function SSL_shutdown(ssl: PSSL): cint; cdecl;
66 function SSL_read(ssl: PSSL; buf: PByte; num: cint): cint; cdecl;
67 function SSL_write(ssl: PSSL; const buf: PByte; num: cint): cint; cdecl;
68 function SSL_pending(ssl: PSSL): cint; cdecl;
69 function SSLv23_method(): PSSL_METHOD; cdecl;
70 function SSLv2_method(): PSSL_METHOD; cdecl;
71 function SSLv3_method(): PSSL_METHOD; cdecl;
72 function TLSv1_method(): PSSL_METHOD; cdecl;
73 function TLSv1_1_method(): PSSL_METHOD; cdecl;
74 function TLSv1_2_method(): PSSL_METHOD; cdecl;
75 procedure SSL_CTX_set_verify(ctx: PSSL_CTX; mode: cint; func: Pointer); cdecl;
76 function SSL_get_error (ssl: PSSL; ret: cint): cint; cdecl;
77
78 implementation
79
80 uses
81 JwaWinError,
82 ssl_openssl_lib, blcksock, ssl_openssl;
83
84 const
85 SCHANNEL_CRED_VERSION = $00000004;
86
87 const
88 SCH_CRED_MANUAL_CRED_VALIDATION = $00000008;
89 SCH_CRED_NO_DEFAULT_CREDS = $00000010;
90
91 const
92 SCHANNEL_SHUTDOWN = 1; // gracefully close down a connection
93
94 const
95 SP_PROT_SSL2_SERVER = $00000004;
96 SP_PROT_SSL2_CLIENT = $00000008;
97 SP_PROT_SSL2 = (SP_PROT_SSL2_SERVER or SP_PROT_SSL2_CLIENT);
98
99 SP_PROT_SSL3_SERVER = $00000010;
100 SP_PROT_SSL3_CLIENT = $00000020;
101 SP_PROT_SSL3 = (SP_PROT_SSL3_SERVER or SP_PROT_SSL3_CLIENT);
102
103 SP_PROT_TLS1_SERVER = $00000040;
104 SP_PROT_TLS1_CLIENT = $00000080;
105 SP_PROT_TLS1 = (SP_PROT_TLS1_SERVER or SP_PROT_TLS1_CLIENT);
106
107 SP_PROT_TLS1_1_SERVER = $00000100;
108 SP_PROT_TLS1_1_CLIENT = $00000200;
109 SP_PROT_TLS1_1 = (SP_PROT_TLS1_1_SERVER or SP_PROT_TLS1_1_CLIENT);
110
111 SP_PROT_TLS1_2_SERVER = $00000400;
112 SP_PROT_TLS1_2_CLIENT = $00000800;
113 SP_PROT_TLS1_2 = (SP_PROT_TLS1_2_SERVER or SP_PROT_TLS1_2_CLIENT);
114
115 const
116 UNISP_NAME_A = AnsiString('Microsoft Unified Security Protocol Provider');
117 UNISP_NAME_W = WideString('Microsoft Unified Security Protocol Provider');
118
119
120
121 type
122 ALG_ID = type cuint;
123 HCERTSTORE = type HANDLE;
124 PCCERT_CONTEXT = type Pointer;
125
126 type
127 SCHANNEL_CRED = record
128 dwVersion: DWORD;
129 cCreds: DWORD;
130 paCred: PCCERT_CONTEXT;
131 hRootStore: HCERTSTORE;
132 cMappers: DWORD;
133 aphMappers: Pointer;
134 cSupportedAlgs: DWORD;
135 palgSupportedAlgs: ^ALG_ID;
136 grbitEnabledProtocols: DWORD;
137 dwMinimumCipherStrength: DWORD;
138 dwMaximumCipherStrength: DWORD;
139 dwSessionLifespan: DWORD;
140 dwFlags: DWORD;
141 dwCredFormat: DWORD;
142 end;
143
144 var
145 g_hSecurity: HMODULE;
146 g_pSSPI: PSecurityFunctionTableA;
147
148 function SSL_library_init(): cint; cdecl;
149 var
150 pInitSecurityInterface: INIT_SECURITY_INTERFACE_A;
151 begin
152 if (g_hSecurity <> 0) then Exit(1);
153
154 g_hSecurity:= LoadLibraryA('schannel.dll');
155 if (g_hSecurity = 0) then Exit(0);
156
157 pInitSecurityInterface := INIT_SECURITY_INTERFACE_A(GetProcAddress(g_hSecurity, SECURITY_ENTRYPOINT_ANSIA));
158 if (pInitSecurityInterface <> nil) then
159 g_pSSPI := pInitSecurityInterface();
160
161 if (g_pSSPI = nil) then
162 begin
163 FreeLibrary(g_hSecurity);
164 g_hSecurity := 0;
165 Exit(0);
166 end;
167
168 Result := 1;
169 end;
170
171 function SSL_set_fd(ssl: PSSL; fd: cint): cint; cdecl;
172 begin
173 if (ssl = nil) then Exit(0);
174
175 ssl^.s := TSocket(fd);
176 Result := 1;
177 end;
178
179 function SSL_CTX_new(method: PSSL_METHOD): PSSL_CTX; cdecl;
180 begin
181 if (g_hSecurity = 0) then Exit(nil);
182
183 Result := GetMem(SizeOf(SSL_CTX));
184
185 Result^.dwProtocol := DWORD(method);
186 end;
187
188 procedure SSL_CTX_free(ctx: PSSL_CTX); cdecl;
189 begin
190 FreeMem(ctx);
191 end;
192
193 function SSL_new(ctx: PSSL_CTX): PSSL; cdecl;
194 var
195 SchannelCred: SCHANNEL_CRED;
196 tsExpiry: TimeStamp;
197 scRet: SECURITY_STATUS;
198 begin
199 if (ctx = nil) then Exit(nil);
200
201 Result := GetMem(SizeOf(SSL));
202 ZeroMemory(Result, SizeOf(SSL));
203 Result^.ctx := ctx;
204
205 ZeroMemory(@SchannelCred, SizeOf(SchannelCred));
206
207 SchannelCred.dwVersion := SCHANNEL_CRED_VERSION;
208 SchannelCred.grbitEnabledProtocols := ctx^.dwProtocol;
209
210 SchannelCred.dwFlags := SchannelCred.dwFlags or SCH_CRED_NO_DEFAULT_CREDS;
211
212 if (not ctx^.bVerify) then
213 SchannelCred.dwFlags := SchannelCred.dwFlags or SCH_CRED_MANUAL_CRED_VALIDATION;
214
215 // Create an SSPI credential.
216 scRet := g_pSSPI^.AcquireCredentialsHandleA(
217 nil, // Name of principal
218 UNISP_NAME_A, // Name of package
219 SECPKG_CRED_OUTBOUND, // Flags indicating use
220 nil, // Pointer to logon ID
221 @SchannelCred, // Package specific data
222 nil, // Pointer to GetKey() func
223 nil, // Value to pass to GetKey()
224 @Result^.hCreds, // (out) Cred Handle
225 @tsExpiry); // (out) Lifetime (optional)
226
227 if (scRet <> SEC_E_OK) then
228 begin
229 FreeMem(Result);
230 Result := nil;
231 end;
232 end;
233
234 procedure SSL_free(ssl: PSSL); cdecl;
235 begin
236 if (ssl = nil) then Exit;
237
238 g_pSSPI^.FreeCredentialHandle(@ssl^.hCreds);
239 g_pSSPI^.DeleteSecurityContext(@ssl^.hContext);
240
241 FreeMem(ssl^.pbRecDataBuf);
242 FreeMem(ssl^.pbIoBuffer);
243 FreeMem(ssl);
244 end;
245
246 function ClientHandshakeLoop(ssl: PSSL; fDoInitialRead: BOOL): SECURITY_STATUS;
247 var
248 InBuffer: SecBufferDesc;
249 InBuffers: array [0..1] of SecBuffer;
250 OutBuffer: SecBufferDesc;
251 OutBuffers: array [0..0] of SecBuffer;
252 dwSSPIFlags: DWORD;
253 dwSSPIOutFlags: DWORD = 0;
254 tsExpiry: TimeStamp;
255 scRet: SECURITY_STATUS;
256 cbData: LONG;
257 fDoRead: BOOL;
258 tv: TTimeVal = (tv_sec: 10; tv_usec: 0);
259 fd: TFDSet;
260 begin
261 dwSSPIFlags := ISC_REQ_SEQUENCE_DETECT or
262 ISC_REQ_REPLAY_DETECT or
263 ISC_REQ_CONFIDENTIALITY or
264 ISC_RET_EXTENDED_ERROR or
265 ISC_REQ_ALLOCATE_MEMORY or
266 ISC_REQ_STREAM;
267
268 ssl^.cbIoBuffer := 0;
269
270 fDoRead := fDoInitialRead;
271
272 scRet := SEC_I_CONTINUE_NEEDED;
273
274 // Loop until the handshake is finished or an error occurs.
275 while (scRet = SEC_I_CONTINUE_NEEDED) or
276 (scRet = SEC_E_INCOMPLETE_MESSAGE) or
277 (scRet = SEC_I_INCOMPLETE_CREDENTIALS) do
278 begin
279 // Read server data
280 if (0 = ssl^.cbIoBuffer) or (scRet = SEC_E_INCOMPLETE_MESSAGE) then
281 begin
282 if (fDoRead) then
283 begin
284 // If buffer not large enough reallocate buffer
285 if (ssl^.sbIoBuffer <= ssl^.cbIoBuffer) then
286 begin
287 ssl^.sbIoBuffer += 2048;
288 ssl^.pbIoBuffer := PUCHAR(ReAllocMem(ssl^.pbIoBuffer, ssl^.sbIoBuffer));
289 end;
290
291 FD_ZERO(fd);
292 FD_SET(ssl^.s, fd);
293 if (select(1, @fd, nil, nil, @tv) <> 1) then
294 begin
295 scRet := SEC_E_INTERNAL_ERROR;
296 break;
297 end;
298
299 cbData := recv(ssl^.s,
300 ssl^.pbIoBuffer + ssl^.cbIoBuffer,
301 ssl^.sbIoBuffer - ssl^.cbIoBuffer,
302 0);
303 if (cbData = SOCKET_ERROR) then
304 begin
305 scRet := SEC_E_INTERNAL_ERROR;
306 break;
307 end
308 else if (cbData = 0) then
309 begin
310 scRet := SEC_E_INTERNAL_ERROR;
311 break;
312 end;
313
314 ssl^.cbIoBuffer += cbData;
315 end
316 else begin
317 fDoRead := TRUE;
318 end;
319 end;
320
321
322 // Set up the input buffers. Buffer 0 is used to pass in data
323 // received from the server. Schannel will consume some or all
324 // of this. Leftover data (if any) will be placed in buffer 1 and
325 // given a buffer type of SECBUFFER_EXTRA.
326
327 InBuffers[0].pvBuffer := ssl^.pbIoBuffer;
328 InBuffers[0].cbBuffer := ssl^.cbIoBuffer;
329 InBuffers[0].BufferType := SECBUFFER_TOKEN;
330
331 InBuffers[1].pvBuffer := nil;
332 InBuffers[1].cbBuffer := 0;
333 InBuffers[1].BufferType := SECBUFFER_EMPTY;
334
335 InBuffer.cBuffers := 2;
336 InBuffer.pBuffers := InBuffers;
337 InBuffer.ulVersion := SECBUFFER_VERSION;
338
339 // Set up the output buffers. These are initialized to NULL
340 // so as to make it less likely we'll attempt to free random
341 // garbage later.
342
343 OutBuffers[0].pvBuffer := nil;
344 OutBuffers[0].BufferType:= SECBUFFER_TOKEN;
345 OutBuffers[0].cbBuffer := 0;
346
347 OutBuffer.cBuffers := 1;
348 OutBuffer.pBuffers := OutBuffers;
349 OutBuffer.ulVersion := SECBUFFER_VERSION;
350
351 scRet := g_pSSPI^.InitializeSecurityContextA(@ssl^.hCreds,
352 @ssl^.hContext,
353 nil,
354 dwSSPIFlags,
355 0,
356 SECURITY_NATIVE_DREP,
357 @InBuffer,
358 0,
359 nil,
360 @OutBuffer,
361 dwSSPIOutFlags,
362 @tsExpiry);
363
364 // If success (or if the error was one of the special extended ones),
365 // send the contents of the output buffer to the server.
366 if (scRet = SEC_E_OK) or
367 (scRet = SEC_I_CONTINUE_NEEDED) or
368 (FAILED(scRet) and (dwSSPIOutFlags and ISC_RET_EXTENDED_ERROR <> 0)) then
369 begin
370 if (OutBuffers[0].cbBuffer <> 0) and (OutBuffers[0].pvBuffer <> nil) then
371 begin
372 cbData := send(ssl^.s,
373 OutBuffers[0].pvBuffer,
374 OutBuffers[0].cbBuffer,
375 0);
376 if (cbData = SOCKET_ERROR) or (cbData = 0) then
377 begin
378 g_pSSPI^.FreeContextBuffer(OutBuffers[0].pvBuffer);
379 g_pSSPI^.DeleteSecurityContext(@ssl^.hContext);
380 Exit(SEC_E_INTERNAL_ERROR);
381 end;
382
383 // Free output buffer.
384 g_pSSPI^.FreeContextBuffer(OutBuffers[0].pvBuffer);
385 OutBuffers[0].pvBuffer := nil;
386 end;
387 end;
388
389 // we need to read more data from the server and try again.
390 if (scRet = SEC_E_INCOMPLETE_MESSAGE) then continue;
391
392 // handshake completed successfully.
393 if (scRet = SEC_E_OK) then
394 begin
395 // Store remaining data for further use
396 if (InBuffers[1].BufferType = SECBUFFER_EXTRA) then
397 begin
398 ssl^.exIoBuffer := True;
399 MoveMemory(ssl^.pbIoBuffer,
400 ssl^.pbIoBuffer + (ssl^.cbIoBuffer - InBuffers[1].cbBuffer),
401 InBuffers[1].cbBuffer);
402 ssl^.cbIoBuffer := InBuffers[1].cbBuffer;
403 end
404 else
405 ssl^.cbIoBuffer := 0;
406 break;
407 end;
408
409 // Check for fatal error.
410 if (FAILED(scRet)) then break;
411
412 // server just requested client authentication.
413 if (scRet = SEC_I_INCOMPLETE_CREDENTIALS) then
414 begin
415 // Server has requested client authentication and
416 // GetNewClientCredentials(ssl);
417
418 // Go around again.
419 fDoRead := FALSE;
420 scRet := SEC_I_CONTINUE_NEEDED;
421 continue;
422 end;
423
424
425 // Copy any leftover data from the buffer, and go around again.
426 if ( InBuffers[1].BufferType = SECBUFFER_EXTRA ) then
427 begin
428 ssl^.exIoBuffer := True;
429 MoveMemory(ssl^.pbIoBuffer,
430 ssl^.pbIoBuffer + (ssl^.cbIoBuffer - InBuffers[1].cbBuffer),
431 InBuffers[1].cbBuffer);
432
433 ssl^.cbIoBuffer := InBuffers[1].cbBuffer;
434 end
435 else
436 ssl^.cbIoBuffer := 0;
437 end;
438
439 // Delete the security context in the case of a fatal error.
440 if (FAILED(scRet)) then
441 begin
442 g_pSSPI^.DeleteSecurityContext(@ssl^.hContext);
443 end;
444
445 if (ssl^.cbIoBuffer = 0) then
446 begin
447 FreeMem(ssl^.pbIoBuffer);
448 ssl^.pbIoBuffer := nil;
449 ssl^.sbIoBuffer := 0;
450 end;
451
452 Result := scRet;
453 end;
454
455
456 function SSL_connect(ssl: PSSL): cint; cdecl;
457 var
458 OutBuffer: SecBufferDesc;
459 OutBuffers: array[0..0] of SecBuffer;
460 dwSSPIFlags: DWORD;
461 dwSSPIOutFlags: DWORD = 0;
462 tsExpiry: TimeStamp;
463 scRet: SECURITY_STATUS;
464 cbData: LONG;
465 sock: TVarSin;
466 begin
467 if (ssl = nil) then Exit(0);
468
469 dwSSPIFlags := ISC_REQ_SEQUENCE_DETECT or
470 ISC_REQ_REPLAY_DETECT or
471 ISC_REQ_CONFIDENTIALITY or
472 ISC_RET_EXTENDED_ERROR or
473 ISC_REQ_ALLOCATE_MEMORY or
474 ISC_REQ_STREAM;
475
476 // Initiate a ClientHello message and generate a token.
477
478 OutBuffers[0].pvBuffer := nil;
479 OutBuffers[0].BufferType := SECBUFFER_TOKEN;
480 OutBuffers[0].cbBuffer := 0;
481
482 OutBuffer.cBuffers := 1;
483 OutBuffer.pBuffers := OutBuffers;
484 OutBuffer.ulVersion := SECBUFFER_VERSION;
485
486 GetPeerName(ssl^.s, sock);
487
488 scRet := g_pSSPI^.InitializeSecurityContextA(
489 @ssl^.hCreds,
490 nil,
491 inet_ntoa(sock.sin_addr),
492 dwSSPIFlags,
493 0,
494 SECURITY_NATIVE_DREP,
495 nil,
496 0,
497 @ssl^.hContext,
498 @OutBuffer,
499 dwSSPIOutFlags,
500 @tsExpiry);
501
502 if (scRet <> SEC_I_CONTINUE_NEEDED) then
503 begin
504 Exit(0);
505 end;
506
507 // Send response to server if there is one.
508 if (OutBuffers[0].cbBuffer <> 0) and (OutBuffers[0].pvBuffer <> nil) then
509 begin
510 cbData := send(ssl^.s,
511 OutBuffers[0].pvBuffer,
512 OutBuffers[0].cbBuffer,
513 0);
514 if (cbData = SOCKET_ERROR) or (cbData = 0) then
515 begin
516 g_pSSPI^.FreeContextBuffer(OutBuffers[0].pvBuffer);
517 g_pSSPI^.DeleteSecurityContext(@ssl^.hContext);
518 Exit(0);
519 end;
520
521 // Free output buffer.
522 g_pSSPI^.FreeContextBuffer(OutBuffers[0].pvBuffer);
523 OutBuffers[0].pvBuffer := nil;
524 end;
525
526 Result := cint(ClientHandshakeLoop(ssl, TRUE) = SEC_E_OK);
527 end;
528
529 function SSL_shutdown(ssl: PSSL): cint; cdecl;
530 var
531 dwType: DWORD;
532 OutBuffer: SecBufferDesc;
533 OutBuffers: array[0..0] of SecBuffer;
534 dwSSPIFlags: DWORD;
535 dwSSPIOutFlags: DWORD = 0;
536 tsExpiry: TimeStamp;
537 Status: DWORD;
538 begin
539 if (ssl = nil) then Exit(SOCKET_ERROR);
540
541 dwType := SCHANNEL_SHUTDOWN;
542
543 OutBuffers[0].pvBuffer := @dwType;
544 OutBuffers[0].BufferType := SECBUFFER_TOKEN;
545 OutBuffers[0].cbBuffer := SizeOf(dwType);
546
547 OutBuffer.cBuffers := 1;
548 OutBuffer.pBuffers := OutBuffers;
549 OutBuffer.ulVersion := SECBUFFER_VERSION;
550
551 Status := g_pSSPI^.ApplyControlToken(@ssl^.hContext, @OutBuffer);
552 if (FAILED(Status)) then Exit(cint(ssl^.rmshtdn));
553
554 //
555 // Build an SSL close notify message.
556 //
557
558 dwSSPIFlags := ISC_REQ_SEQUENCE_DETECT or
559 ISC_REQ_REPLAY_DETECT or
560 ISC_REQ_CONFIDENTIALITY or
561 ISC_RET_EXTENDED_ERROR or
562 ISC_REQ_ALLOCATE_MEMORY or
563 ISC_REQ_STREAM;
564
565 OutBuffers[0].pvBuffer := nil;
566 OutBuffers[0].BufferType := SECBUFFER_TOKEN;
567 OutBuffers[0].cbBuffer := 0;
568
569 OutBuffer.cBuffers := 1;
570 OutBuffer.pBuffers := OutBuffers;
571 OutBuffer.ulVersion := SECBUFFER_VERSION;
572
573 Status := g_pSSPI^.InitializeSecurityContextA(
574 @ssl^.hCreds,
575 @ssl^.hContext,
576 nil,
577 dwSSPIFlags,
578 0,
579 SECURITY_NATIVE_DREP,
580 nil,
581 0,
582 @ssl^.hContext,
583 @OutBuffer,
584 dwSSPIOutFlags,
585 @tsExpiry);
586
587 if (FAILED(Status)) then Exit(cint(ssl^.rmshtdn));
588
589 // Send the close notify message to the server.
590 if (OutBuffers[0].pvBuffer <> nil) and (OutBuffers[0].cbBuffer <> 0) then
591 begin
592 send(ssl^.s, OutBuffers[0].pvBuffer, OutBuffers[0].cbBuffer, 0);
593 g_pSSPI^.FreeContextBuffer(OutBuffers[0].pvBuffer);
594 end;
595
596 // Free the security context.
597 g_pSSPI^.DeleteSecurityContext(@ssl^.hContext);
598
599 Result := cint(ssl^.rmshtdn);
600 end;
601
602 function SSL_read(ssl: PSSL; buf: PByte; num: cint): cint; cdecl;
603 var
604 scRet: SECURITY_STATUS;
605 cbData: LONG;
606 i: cint;
607 Message: SecBufferDesc;
608 Buffers: array [0..3] of SecBuffer;
609 pDataBuffer: PSecBuffer;
610 pExtraBuffer: PSecBuffer;
611 bytes, rbytes: LONG;
612 fQOP: ULONG = 0;
613 begin
614 if (ssl = nil) then Exit(SOCKET_ERROR);
615
616 if (num = 0) then Exit(0);
617
618 if (ssl^.cbRecDataBuf <> 0) then
619 begin
620 bytes := Min(num, ssl^.cbRecDataBuf);
621 CopyMemory(buf, ssl^.pbRecDataBuf, bytes);
622
623 rbytes := ssl^.cbRecDataBuf - bytes;
624 MoveMemory(ssl^.pbRecDataBuf, ssl^.pbRecDataBuf + bytes, rbytes);
625 ssl^.cbRecDataBuf := rbytes;
626
627 Exit(bytes);
628 end;
629
630 scRet := SEC_E_OK;
631
632 while (True) do
633 begin
634 if (0 = ssl^.cbIoBuffer) or (scRet = SEC_E_INCOMPLETE_MESSAGE) then
635 begin
636 if (ssl^.sbIoBuffer <= ssl^.cbIoBuffer) then
637 begin
638 ssl^.sbIoBuffer += 2048;
639 ssl^.pbIoBuffer := PUCHAR(ReAllocMem(ssl^.pbIoBuffer, ssl^.sbIoBuffer));
640 end;
641
642 cbData := recv(ssl^.s, ssl^.pbIoBuffer + ssl^.cbIoBuffer, ssl^.sbIoBuffer - ssl^.cbIoBuffer, 0);
643 if (cbData = SOCKET_ERROR) then
644 begin
645 Exit(SOCKET_ERROR);
646 end
647 else if (cbData = 0) then
648 begin
649 // Server disconnected.
650 if (ssl^.cbIoBuffer <> 0) then
651 begin
652 scRet := SEC_E_INTERNAL_ERROR;
653 Exit(SOCKET_ERROR);
654 end
655 else
656 Exit(0);
657 end
658 else
659 ssl^.cbIoBuffer += cbData;
660 end;
661
662 // Attempt to decrypt the received data.
663 Buffers[0].pvBuffer := ssl^.pbIoBuffer;
664 Buffers[0].cbBuffer := ssl^.cbIoBuffer;
665 Buffers[0].BufferType := SECBUFFER_DATA;
666
667 Buffers[1].BufferType := SECBUFFER_EMPTY;
668 Buffers[2].BufferType := SECBUFFER_EMPTY;
669 Buffers[3].BufferType := SECBUFFER_EMPTY;
670
671 Message.ulVersion := SECBUFFER_VERSION;
672 Message.cBuffers := 4;
673 Message.pBuffers := Buffers;
674
675 if (@g_pSSPI^.DecryptMessage <> nil) then
676 scRet := g_pSSPI^.DecryptMessage(@ssl^.hContext, @Message, 0, fQOP)
677 else
678 scRet := DECRYPT_MESSAGE_FN(g_pSSPI^.Reserved4)(@ssl^.hContext, @Message, 0, fQOP);
679
680 if (scRet = SEC_E_INCOMPLETE_MESSAGE) then
681 begin
682 // The input buffer contains only a fragment of an
683 // encrypted record. Loop around and read some more
684 // data.
685 continue;
686 end;
687
688 // Server signaled end of session
689 if (scRet = SEC_I_CONTEXT_EXPIRED) then
690 begin
691 ssl^.rmshtdn := TRUE;
692 SSL_shutdown(ssl);
693 Exit(0);
694 end;
695
696 if (scRet <> SEC_E_OK) and
697 (scRet <> SEC_I_RENEGOTIATE) and
698 (scRet <> SEC_I_CONTEXT_EXPIRED) then
699 begin
700 Exit(SOCKET_ERROR);
701 end;
702
703 // Locate data and (optional) extra buffers.
704 pDataBuffer := nil;
705 pExtraBuffer := nil;
706 for i := 1 to 3 do
707 begin
708 if (pDataBuffer = nil) and (Buffers[i].BufferType = SECBUFFER_DATA) then
709 begin
710 pDataBuffer := @Buffers[i];
711 end;
712
713 if (pExtraBuffer = nil) and (Buffers[i].BufferType = SECBUFFER_EXTRA) then
714 begin
715 pExtraBuffer := @Buffers[i];
716 end;
717 end;
718
719 // Return decrypted data.
720 if Assigned(pDataBuffer) then
721 begin
722 bytes := Min(num, pDataBuffer^.cbBuffer);
723 CopyMemory(buf, pDataBuffer^.pvBuffer, bytes);
724
725 rbytes := pDataBuffer^.cbBuffer - bytes;
726 if (rbytes > 0) then
727 begin
728 if (ssl^.sbRecDataBuf < rbytes) then
729 begin
730 ssl^.sbRecDataBuf := rbytes;
731 ssl^.pbRecDataBuf := PUCHAR(ReAllocMem(ssl^.pbRecDataBuf, rbytes));
732 end;
733 CopyMemory(ssl^.pbRecDataBuf, pDataBuffer^.pvBuffer + bytes, rbytes);
734 ssl^.cbRecDataBuf := rbytes;
735 end;
736 end;
737
738 // Move any "extra" data to the input buffer.
739 if Assigned(pExtraBuffer) then
740 begin
741 MoveMemory(ssl^.pbIoBuffer, pExtraBuffer^.pvBuffer, pExtraBuffer^.cbBuffer);
742 ssl^.cbIoBuffer := pExtraBuffer^.cbBuffer;
743 end
744 else
745 ssl^.cbIoBuffer := 0;
746
747 if (pDataBuffer <> nil) and (bytes <> 0) then Exit(bytes);
748
749 if (scRet = SEC_I_RENEGOTIATE) then
750 begin
751 // The server wants to perform another handshake
752 // sequence.
753
754 scRet := ClientHandshakeLoop(ssl, FALSE);
755 if (scRet <> SEC_E_OK) then Exit(SOCKET_ERROR);
756 end;
757 end;
758 end;
759
760
761 function SSL_write(ssl: PSSL; const buf: PByte; num: cint): cint; cdecl;
762 var
763 Sizes: SecPkgContext_StreamSizes;
764 scRet: SECURITY_STATUS;
765 cbData: LONG;
766 Message: SecBufferDesc;
767 Buffers: array[0..3] of SecBuffer;
768 pbDataBuffer: PUCHAR;
769 pbMessage: PUCHAR;
770 cbMessage: DWORD;
771 sendOff: DWORD = 0;
772 begin
773 if (ssl = nil) then Exit(SOCKET_ERROR);
774
775 FillChar(Buffers, SizeOf(Buffers), 0);
776
777 scRet := g_pSSPI^.QueryContextAttributesA(@ssl^.hContext, SECPKG_ATTR_STREAM_SIZES, @Sizes);
778 if (scRet <> SEC_E_OK) then Exit(scRet);
779
780 pbDataBuffer := PUCHAR(GetMem(Sizes.cbMaximumMessage + Sizes.cbHeader + Sizes.cbTrailer));
781
782 pbMessage := pbDataBuffer + Sizes.cbHeader;
783
784 while (sendOff < DWORD(num)) do
785 begin
786 cbMessage := Min(Sizes.cbMaximumMessage, DWORD(num) - sendOff);
787 CopyMemory(pbMessage, buf + sendOff, cbMessage);
788
789 Buffers[0].pvBuffer := pbDataBuffer;
790 Buffers[0].cbBuffer := Sizes.cbHeader;
791 Buffers[0].BufferType := SECBUFFER_STREAM_HEADER;
792
793 Buffers[1].pvBuffer := pbMessage;
794 Buffers[1].cbBuffer := cbMessage;
795 Buffers[1].BufferType := SECBUFFER_DATA;
796
797 Buffers[2].pvBuffer := pbMessage + cbMessage;
798 Buffers[2].cbBuffer := Sizes.cbTrailer;
799 Buffers[2].BufferType := SECBUFFER_STREAM_TRAILER;
800
801 Buffers[3].BufferType := SECBUFFER_EMPTY;
802
803 Message.ulVersion := SECBUFFER_VERSION;
804 Message.cBuffers := 4;
805 Message.pBuffers := Buffers;
806
807 if (@g_pSSPI^.EncryptMessage <> nil) then
808 scRet := g_pSSPI^.EncryptMessage(@ssl^.hContext, 0, @Message, 0)
809 else
810 scRet := ENCRYPT_MESSAGE_FN(g_pSSPI^.Reserved3)(@ssl^.hContext, 0, @Message, 0);
811
812 if (FAILED(scRet)) then break;
813
814 // Calculate encrypted packet size
815 cbData := Buffers[0].cbBuffer + Buffers[1].cbBuffer + Buffers[2].cbBuffer;
816
817 // Send the encrypted data to the server.
818 cbData := send(ssl^.s, pbDataBuffer, cbData, 0);
819 if (cbData = SOCKET_ERROR) or (cbData = 0) then
820 begin
821 g_pSSPI^.DeleteSecurityContext(@ssl^.hContext);
822 scRet := SEC_E_INTERNAL_ERROR;
823 break;
824 end;
825
826 sendOff += cbMessage;
827 end;
828
829 FreeMem(pbDataBuffer);
830
831 if scRet = SEC_E_OK then
832 Result := num
833 else
834 Result := SOCKET_ERROR;
835 end;
836
837 function SSL_pending(ssl: PSSL): cint; cdecl;
838 begin
839 if (ssl = nil) then Exit(0);
840
841 if ssl^.cbRecDataBuf > 0 then
842 Result := ssl^.cbRecDataBuf
843 else if ssl^.exIoBuffer then
844 begin
845 ssl^.exIoBuffer := False;
846 Result := ssl^.cbIoBuffer
847 end
848 else
849 Result := 0;
850 end;
851
852 function SSLv23_method(): PSSL_METHOD; cdecl;
853 begin
854 Result:= PSSL_METHOD(SP_PROT_SSL3 or SP_PROT_TLS1 or SP_PROT_TLS1_1);
855 end;
856
857 function SSLv2_method(): PSSL_METHOD; cdecl;
858 begin
859 Result := PSSL_METHOD(SP_PROT_SSL2);
860 end;
861
862 function SSLv3_method(): PSSL_METHOD; cdecl;
863 begin
864 Result := PSSL_METHOD(SP_PROT_SSL3);
865 end;
866
867 function TLSv1_method(): PSSL_METHOD; cdecl;
868 begin
869 Result := PSSL_METHOD(SP_PROT_TLS1);
870 end;
871
872 function TLSv1_1_method(): PSSL_METHOD; cdecl;
873 begin
874 Result := PSSL_METHOD(SP_PROT_TLS1_1);
875 end;
876
877 function TLSv1_2_method(): PSSL_METHOD; cdecl;
878 begin
879 Result := PSSL_METHOD(SP_PROT_TLS1_2);
880 end;
881
882 procedure SSL_CTX_set_verify(ctx: PSSL_CTX; mode: cint; func: Pointer); cdecl;
883 begin
884 if (ctx <> nil) then ctx^.bVerify := mode <> 0;
885 end;
886
887 function SSL_get_error (ssl: PSSL; ret: cint): cint; cdecl;
888 begin
889 if (ret > 0) then
890 Result := SSL_ERROR_NONE
891 else
892 Result := SSL_ERROR_ZERO_RETURN;
893 end;
894
895 var
896 lpBuffer: TMemoryBasicInformation;
897 begin
898 if (IsSSLloaded = False) then
899 begin
900 if VirtualQuery(@lpBuffer, @lpBuffer, SizeOf(lpBuffer)) = SizeOf(lpBuffer) then
901 begin
902 SetLength(DLLSSLName, MAX_PATH);
903 SetLength(DLLSSLName, GetModuleFileName(THandle(lpBuffer.AllocationBase),
904 PAnsiChar(DLLSSLName), MAX_PATH));
905 DLLUtilName := DLLSSLName;
906
907 if InitSSLInterface then
908 SSLImplementation := TSSLOpenSSL;
909 end;
910 end;
911 end.