| 1 |
{==============================================================================| |
| 2 |
| Project : Ararat Synapse | 001.000.000 | |
| 3 |
|==============================================================================| |
| 4 |
| Content: SSL support by GnuTLS | |
| 5 |
|==============================================================================| |
| 6 |
| Copyright (C) 2013-2023 Alexander Koblov <[email protected]> | |
| 7 |
| All rights reserved. | |
| 8 |
| | |
| 9 |
| Redistribution and use in source and binary forms, with or without | |
| 10 |
| modification, are permitted provided that the following conditions are met: | |
| 11 |
| | |
| 12 |
| Redistributions of source code must retain the above copyright notice, this | |
| 13 |
| list of conditions and the following disclaimer. | |
| 14 |
| | |
| 15 |
| Redistributions in binary form must reproduce the above copyright notice, | |
| 16 |
| this list of conditions and the following disclaimer in the documentation | |
| 17 |
| and/or other materials provided with the distribution. | |
| 18 |
| | |
| 19 |
| Neither the name of Lukas Gebauer nor the names of its contributors may | |
| 20 |
| be used to endorse or promote products derived from this software without | |
| 21 |
| specific prior written permission. | |
| 22 |
| | |
| 23 |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 24 |
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 25 |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 26 |
| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR | |
| 27 |
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 |
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 29 |
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| 30 |
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 31 |
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 32 |
| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | |
| 33 |
| DAMAGE. | |
| 34 |
|==============================================================================| |
| 35 |
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).| |
| 36 |
| Portions created by Lukas Gebauer are Copyright (C) 2005-2023. | |
| 37 |
| Portions created by Petr Fejfar are Copyright (C) 2011-2012. | |
| 38 |
| All Rights Reserved. | |
| 39 |
|==============================================================================} |
| 40 |
|
| 41 |
{:@abstract(SSL plugin for GnuTLS) |
| 42 |
|
| 43 |
Compatibility with GnuTLS versions: |
| 44 |
3.0.0+ |
| 45 |
|
| 46 |
GnuTLS libraries are loaded dynamicly - you not need GnuTLS librares even you |
| 47 |
compile your application with this unit. SSL just not working when you not have |
| 48 |
GnuTLS libraries. |
| 49 |
} |
| 50 |
|
| 51 |
{$MODE DELPHI} |
| 52 |
|
| 53 |
{$IFDEF UNICODE} |
| 54 |
{$WARN IMPLICIT_STRING_CAST OFF} |
| 55 |
{$WARN IMPLICIT_STRING_CAST_LOSS OFF} |
| 56 |
{$ENDIF} |
| 57 |
|
| 58 |
unit ssl_gnutls; |
| 59 |
|
| 60 |
interface |
| 61 |
|
| 62 |
uses |
| 63 |
SysUtils, Classes, |
| 64 |
blcksock, synsock, synautil, |
| 65 |
ssl_gnutls_lib; |
| 66 |
|
| 67 |
type |
| 68 |
{:@abstract(class implementing GnuTLS SSL plugin.) |
| 69 |
Instance of this class will be created for each @link(TTCPBlockSocket). |
| 70 |
You not need to create instance of this class, all is done by Synapse itself!} |
| 71 |
|
| 72 |
{ TSSLGnuTLS } |
| 73 |
|
| 74 |
TSSLGnuTLS = class(TCustomSSL) |
| 75 |
private |
| 76 |
FShutdown: Integer; |
| 77 |
FDatum: gnutls_datum_t; |
| 78 |
FSession: gnutls_session_t; |
| 79 |
FPriorities: array[Byte] of AnsiChar; |
| 80 |
FCredentials: gnutls_certificate_credentials_t; |
| 81 |
protected |
| 82 |
function Init: Boolean; |
| 83 |
function DeInit: Boolean; |
| 84 |
function Prepare: Boolean; |
| 85 |
function SSLCheck: Boolean; |
| 86 |
public |
| 87 |
{:See @inherited} |
| 88 |
constructor Create(const Value: TTCPBlockSocket); override; |
| 89 |
destructor Destroy; override; |
| 90 |
{:See @inherited} |
| 91 |
function LibVersion: String; override; |
| 92 |
{:See @inherited} |
| 93 |
function LibName: String; override; |
| 94 |
{:See @inherited} |
| 95 |
function Connect: boolean; override; |
| 96 |
{:See @inherited} |
| 97 |
function Shutdown: boolean; override; |
| 98 |
{:See @inherited} |
| 99 |
function BiShutdown: boolean; override; |
| 100 |
{:See @inherited} |
| 101 |
function SendBuffer(Buffer: TMemory; Len: Integer): Integer; override; |
| 102 |
{:See @inherited} |
| 103 |
function RecvBuffer(Buffer: TMemory; Len: Integer): Integer; override; |
| 104 |
{:See @inherited} |
| 105 |
function WaitingData: Integer; override; |
| 106 |
{:See @inherited} |
| 107 |
function GetSSLVersion: string; override; |
| 108 |
{:See @inherited} |
| 109 |
function GetCipherName: string; override; |
| 110 |
{:See @inherited} |
| 111 |
function GetCipherBits: integer; override; |
| 112 |
{:See @inherited} |
| 113 |
function GetCipherAlgBits: integer; override; |
| 114 |
end; |
| 115 |
|
| 116 |
implementation |
| 117 |
|
| 118 |
{==============================================================================} |
| 119 |
|
| 120 |
constructor TSSLGnuTLS.Create(const Value: TTCPBlockSocket); |
| 121 |
begin |
| 122 |
inherited Create(Value); |
| 123 |
end; |
| 124 |
|
| 125 |
destructor TSSLGnuTLS.Destroy; |
| 126 |
begin |
| 127 |
DeInit; |
| 128 |
inherited Destroy; |
| 129 |
end; |
| 130 |
|
| 131 |
function TSSLGnuTLS.LibVersion: String; |
| 132 |
begin |
| 133 |
Result := 'GnuTLS ' + gnutls_check_version('3.0.0'); |
| 134 |
end; |
| 135 |
|
| 136 |
function TSSLGnuTLS.LibName: String; |
| 137 |
begin |
| 138 |
Result := 'ssl_gnutls'; |
| 139 |
end; |
| 140 |
|
| 141 |
function TSSLGnuTLS.Init: Boolean; |
| 142 |
begin |
| 143 |
Result := False; |
| 144 |
FLastError := 0; |
| 145 |
FLastErrorDesc := EmptyStr; |
| 146 |
|
| 147 |
case FSSLType of |
| 148 |
LT_SSLv3: |
| 149 |
FPriorities := 'NONE:+VERS-SSL3.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL'; |
| 150 |
LT_TLSv1: |
| 151 |
FPriorities := 'NONE:+VERS-TLS1.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL'; |
| 152 |
LT_TLSv1_1: |
| 153 |
FPriorities := 'NONE:+VERS-TLS1.1:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL'; |
| 154 |
LT_TLSv1_2: |
| 155 |
FPriorities := 'NONE:+VERS-TLS1.2:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL'; |
| 156 |
LT_TLSv1_3: |
| 157 |
FPriorities := 'NONE:+VERS-TLS1.3:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL'; |
| 158 |
LT_all: |
| 159 |
FPriorities := 'NONE:+VERS-TLS-ALL:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL'; |
| 160 |
else |
| 161 |
Exit; |
| 162 |
end; |
| 163 |
|
| 164 |
FLastError := gnutls_certificate_allocate_credentials(FCredentials); |
| 165 |
if not SSLCheck then Exit; |
| 166 |
|
| 167 |
FLastError := gnutls_init(@FSession, GNUTLS_CLIENT); |
| 168 |
if not SSLCheck then Exit; |
| 169 |
|
| 170 |
FLastError := gnutls_priority_set_direct(FSession, FPriorities, nil); |
| 171 |
if not SSLCheck then Exit; |
| 172 |
|
| 173 |
FLastError := gnutls_credentials_set(FSession, GNUTLS_CRD_CERTIFICATE, FCredentials); |
| 174 |
if not SSLCheck then Exit; |
| 175 |
|
| 176 |
if Length(FCertificateFile) > 0 then |
| 177 |
begin |
| 178 |
gnutls_certificate_set_x509_trust_file(FCredentials, |
| 179 |
PAnsiChar(FCertificateFile), |
| 180 |
GNUTLS_X509_FMT_PEM); |
| 181 |
end; |
| 182 |
if Length(FPrivateKeyFile) > 0 then |
| 183 |
begin |
| 184 |
gnutls_certificate_set_x509_key_file(FCredentials, |
| 185 |
PAnsiChar(FCertificateFile), |
| 186 |
PAnsiChar(FPrivateKeyFile), |
| 187 |
GNUTLS_X509_FMT_PEM); |
| 188 |
end; |
| 189 |
Result := True; |
| 190 |
end; |
| 191 |
|
| 192 |
function TSSLGnuTLS.DeInit: Boolean; |
| 193 |
begin |
| 194 |
Result := True; |
| 195 |
|
| 196 |
if Assigned(FSessionNew) then |
| 197 |
begin |
| 198 |
gnutls_free(FDatum.data); |
| 199 |
FSessionNew := nil; |
| 200 |
FDatum.data := nil; |
| 201 |
FDatum.size := 0 |
| 202 |
end; |
| 203 |
|
| 204 |
if Assigned(FCredentials) then |
| 205 |
begin |
| 206 |
gnutls_certificate_free_credentials(FCredentials); |
| 207 |
FCredentials := nil; |
| 208 |
end; |
| 209 |
|
| 210 |
if Assigned(FSession) then |
| 211 |
begin |
| 212 |
gnutls_deinit(FSession); |
| 213 |
FSession := nil |
| 214 |
end; |
| 215 |
|
| 216 |
FSSLEnabled := False; |
| 217 |
end; |
| 218 |
|
| 219 |
function TSSLGnuTLS.Prepare: Boolean; |
| 220 |
begin |
| 221 |
DeInit; |
| 222 |
if Init then |
| 223 |
Result := True |
| 224 |
else begin |
| 225 |
DeInit; |
| 226 |
Result := False; |
| 227 |
end; |
| 228 |
end; |
| 229 |
|
| 230 |
function TSSLGnuTLS.SSLCheck: Boolean; |
| 231 |
var |
| 232 |
P : PAnsiChar; |
| 233 |
begin |
| 234 |
if FLastError = GNUTLS_E_SUCCESS then |
| 235 |
begin |
| 236 |
Result := True; |
| 237 |
FLastErrorDesc := EmptyStr; |
| 238 |
end |
| 239 |
else begin |
| 240 |
Result := False; |
| 241 |
P := gnutls_strerror(FLastError); |
| 242 |
FLastErrorDesc := StrPas(P); |
| 243 |
end; |
| 244 |
end; |
| 245 |
|
| 246 |
function TSSLGnuTLS.Connect: boolean; |
| 247 |
var |
| 248 |
B: Boolean; |
| 249 |
begin |
| 250 |
Result := False; |
| 251 |
if FSocket.Socket = INVALID_SOCKET then |
| 252 |
Exit; |
| 253 |
if Prepare then |
| 254 |
begin |
| 255 |
gnutls_transport_set_ptr(FSession, gnutls_transport_ptr_t(FSocket.Socket)); |
| 256 |
// Reuse session |
| 257 |
if Assigned(FSessionOld) then begin |
| 258 |
gnutls_session_set_data(FSession, gnutls_datum_ptr_t(FSessionOld)^.data, gnutls_datum_ptr_t(FSessionOld)^.size); |
| 259 |
end; |
| 260 |
// do blocking call of SSL_Connect |
| 261 |
if FSocket.ConnectionTimeout <= 0 then |
| 262 |
begin |
| 263 |
repeat |
| 264 |
FLastError := gnutls_handshake(FSession); |
| 265 |
until (FLastError <> GNUTLS_E_AGAIN) and (FLastError <> GNUTLS_E_INTERRUPTED); |
| 266 |
end |
| 267 |
// do non-blocking call of SSL_Connect |
| 268 |
else begin |
| 269 |
B := FSocket.NonBlockMode; |
| 270 |
FSocket.NonBlockMode := True; |
| 271 |
repeat |
| 272 |
FLastError := gnutls_handshake(FSession); |
| 273 |
until (FLastError <> GNUTLS_E_AGAIN) and (FLastError <> GNUTLS_E_INTERRUPTED); |
| 274 |
FSocket.NonBlockMode := B; |
| 275 |
end; |
| 276 |
if SSLCheck then |
| 277 |
begin |
| 278 |
if (FSessionOld = nil) then |
| 279 |
begin |
| 280 |
if (gnutls_session_get_data2(FSession, @FDatum) = GNUTLS_E_SUCCESS) then |
| 281 |
begin |
| 282 |
FSessionNew := @FDatum; |
| 283 |
end; |
| 284 |
end; |
| 285 |
FSSLEnabled := True; |
| 286 |
FShutdown := 0; |
| 287 |
Result := True; |
| 288 |
end; |
| 289 |
end; |
| 290 |
end; |
| 291 |
|
| 292 |
function TSSLGnuTLS.Shutdown: boolean; |
| 293 |
begin |
| 294 |
Result := BiShutdown; |
| 295 |
end; |
| 296 |
|
| 297 |
function TSSLGnuTLS.BiShutdown: boolean; |
| 298 |
begin |
| 299 |
if (FShutdown > 0) then |
| 300 |
gnutls_bye(FSession, GNUTLS_SHUT_WR) |
| 301 |
else begin |
| 302 |
gnutls_bye(FSession, GNUTLS_SHUT_RDWR); |
| 303 |
end; |
| 304 |
Inc(FShutdown); |
| 305 |
|
| 306 |
DeInit; |
| 307 |
Result := True; |
| 308 |
end; |
| 309 |
|
| 310 |
function TSSLGnuTLS.SendBuffer(Buffer: TMemory; Len: Integer): Integer; |
| 311 |
begin |
| 312 |
FLastError := 0; |
| 313 |
FLastErrorDesc := EmptyStr; |
| 314 |
|
| 315 |
repeat |
| 316 |
Result := gnutls_record_send(FSession, Buffer , Len); |
| 317 |
until (Result <> GNUTLS_E_AGAIN) and (Result <> GNUTLS_E_INTERRUPTED); |
| 318 |
|
| 319 |
if (Result < 0) then |
| 320 |
begin |
| 321 |
FLastError := Result; |
| 322 |
Result := 0; |
| 323 |
end; |
| 324 |
end; |
| 325 |
|
| 326 |
function TSSLGnuTLS.RecvBuffer(Buffer: TMemory; Len: Integer): Integer; |
| 327 |
begin |
| 328 |
FLastError := 0; |
| 329 |
FLastErrorDesc := EmptyStr; |
| 330 |
|
| 331 |
repeat |
| 332 |
Result := gnutls_record_recv(FSession, Buffer , Len); |
| 333 |
until (Result <> GNUTLS_E_AGAIN) and (Result <> GNUTLS_E_INTERRUPTED); |
| 334 |
|
| 335 |
if (Result < 0) then |
| 336 |
begin |
| 337 |
FLastError := Result; |
| 338 |
Result := 0; |
| 339 |
end; |
| 340 |
end; |
| 341 |
|
| 342 |
function TSSLGnuTLS.WaitingData: Integer; |
| 343 |
begin |
| 344 |
Result := gnutls_record_check_pending(FSession); |
| 345 |
end; |
| 346 |
|
| 347 |
function TSSLGnuTLS.GetSSLVersion: string; |
| 348 |
begin |
| 349 |
if (FSession = nil) then |
| 350 |
Result := EmptyStr |
| 351 |
else |
| 352 |
Result := gnutls_protocol_get_name(gnutls_protocol_get_version(FSession)); |
| 353 |
end; |
| 354 |
|
| 355 |
function TSSLGnuTLS.GetCipherName: string; |
| 356 |
var |
| 357 |
kx: gnutls_kx_algorithm_t; |
| 358 |
mac: gnutls_mac_algorithm_t; |
| 359 |
cipher: gnutls_cipher_algorithm_t; |
| 360 |
begin |
| 361 |
if (FSession = nil) then |
| 362 |
Result := EmptyStr |
| 363 |
else begin |
| 364 |
kx := gnutls_kx_get(FSession); |
| 365 |
mac := gnutls_mac_get(FSession); |
| 366 |
cipher := gnutls_cipher_get(FSession); |
| 367 |
Result := gnutls_cipher_suite_get_name(kx, cipher, mac); |
| 368 |
end; |
| 369 |
end; |
| 370 |
|
| 371 |
function TSSLGnuTLS.GetCipherBits: integer; |
| 372 |
begin |
| 373 |
Result := GetCipherAlgBits; |
| 374 |
end; |
| 375 |
|
| 376 |
function TSSLGnuTLS.GetCipherAlgBits: integer; |
| 377 |
begin |
| 378 |
if (FSession = nil) then |
| 379 |
Result := 0 |
| 380 |
else |
| 381 |
Result := (gnutls_cipher_get_key_size(gnutls_cipher_get(FSession)) * 8); |
| 382 |
end; |
| 383 |
|
| 384 |
{==============================================================================} |
| 385 |
|
| 386 |
initialization |
| 387 |
if (SSLImplementation = TSSLNone) and InitSSLInterface then |
| 388 |
SSLImplementation := TSSLGnuTLS; |
| 389 |
|
| 390 |
end. |