| 1 |
{==============================================================================| |
| 2 |
| Project : Ararat Synapse | 001.004.001 | |
| 3 |
|==============================================================================| |
| 4 |
| Content: SSL support by OpenSSL | |
| 5 |
|==============================================================================| |
| 6 |
| Copyright (c)1999-2017, Lukas Gebauer | |
| 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-2017. | |
| 37 |
| Portions created by Petr Fejfar are Copyright (c)2011-2012. | |
| 38 |
| Portions created by Pepak are Copyright (c)2018. | |
| 39 |
| All Rights Reserved. | |
| 40 |
|==============================================================================| |
| 41 |
| Contributor(s): | |
| 42 |
|==============================================================================| |
| 43 |
| History: see HISTORY.HTM from distribution package | |
| 44 |
| (Found at URL: http://www.ararat.cz/synapse/) | |
| 45 |
|==============================================================================} |
| 46 |
|
| 47 |
//requires OpenSSL libraries! |
| 48 |
|
| 49 |
{:@abstract(SSL plugin for OpenSSL) |
| 50 |
|
| 51 |
Compatibility with OpenSSL versions: |
| 52 |
0.9.6 should work, known mysterious crashing on FreePascal and Linux platform. |
| 53 |
0.9.7 - 1.0.0 working fine. |
| 54 |
1.1.0 should work, under testing. |
| 55 |
|
| 56 |
OpenSSL libraries are loaded dynamicly - you not need OpenSSL librares even you |
| 57 |
compile your application with this unit. SSL just not working when you not have |
| 58 |
OpenSSL libraries. |
| 59 |
|
| 60 |
This plugin have limited support for .NET too! Because is not possible to use |
| 61 |
callbacks with CDECL calling convention under .NET, is not supported |
| 62 |
key/certificate passwords and multithread locking. :-( |
| 63 |
|
| 64 |
For handling keys and certificates you can use this properties: |
| 65 |
|
| 66 |
@link(TCustomSSL.CertificateFile) for PEM or ASN1 DER (cer) format. @br |
| 67 |
@link(TCustomSSL.Certificate) for ASN1 DER format only. @br |
| 68 |
@link(TCustomSSL.PrivateKeyFile) for PEM or ASN1 DER (key) format. @br |
| 69 |
@link(TCustomSSL.PrivateKey) for ASN1 DER format only. @br |
| 70 |
@link(TCustomSSL.CertCAFile) for PEM CA certificate bundle. @br |
| 71 |
@link(TCustomSSL.PFXFile) for PFX format. @br |
| 72 |
@link(TCustomSSL.PFX) for PFX format from binary string. @br |
| 73 |
|
| 74 |
This plugin is capable to create Ad-Hoc certificates. When you start SSL/TLS |
| 75 |
server without explicitly assigned key and certificate, then this plugin create |
| 76 |
Ad-Hoc key and certificate for each incomming connection by self. It slowdown |
| 77 |
accepting of new connections! |
| 78 |
} |
| 79 |
|
| 80 |
{$IFDEF FPC} |
| 81 |
{$MODE DELPHI} |
| 82 |
{$ENDIF} |
| 83 |
{$H+} |
| 84 |
|
| 85 |
{$IFDEF UNICODE} |
| 86 |
{$WARN IMPLICIT_STRING_CAST OFF} |
| 87 |
{$WARN IMPLICIT_STRING_CAST_LOSS OFF} |
| 88 |
{$ENDIF} |
| 89 |
|
| 90 |
unit ssl_openssl; |
| 91 |
|
| 92 |
interface |
| 93 |
|
| 94 |
uses |
| 95 |
SysUtils, Classes, |
| 96 |
blcksock, synsock, synautil, |
| 97 |
{$IFDEF CIL} |
| 98 |
System.Text, |
| 99 |
{$ENDIF} |
| 100 |
{$IFDEF DELPHI23_UP} |
| 101 |
AnsiStrings, |
| 102 |
{$ENDIF} |
| 103 |
ssl_openssl_lib; |
| 104 |
|
| 105 |
type |
| 106 |
{:@abstract(class implementing OpenSSL SSL plugin.) |
| 107 |
Instance of this class will be created for each @link(TTCPBlockSocket). |
| 108 |
You not need to create instance of this class, all is done by Synapse itself!} |
| 109 |
TSSLOpenSSL = class(TCustomSSL) |
| 110 |
private |
| 111 |
FServer: boolean; |
| 112 |
protected |
| 113 |
FSsl: PSSL; |
| 114 |
Fctx: PSSL_CTX; |
| 115 |
function NeedSigningCertificate: boolean; virtual; |
| 116 |
function SSLCheck: Boolean; |
| 117 |
function SetSslKeys: boolean; virtual; |
| 118 |
function Init: Boolean; |
| 119 |
function DeInit: Boolean; |
| 120 |
function Prepare: Boolean; |
| 121 |
function LoadPFX(pfxdata: ansistring): Boolean; |
| 122 |
function CreateSelfSignedCert(Host: string): Boolean; override; |
| 123 |
property Server: boolean read FServer; |
| 124 |
public |
| 125 |
{:See @inherited} |
| 126 |
constructor Create(const Value: TTCPBlockSocket); override; |
| 127 |
destructor Destroy; override; |
| 128 |
{:See @inherited} |
| 129 |
function LibVersion: String; override; |
| 130 |
{:See @inherited} |
| 131 |
function LibName: String; override; |
| 132 |
{:See @inherited and @link(ssl_cryptlib) for more details.} |
| 133 |
function Connect: boolean; override; |
| 134 |
{:See @inherited and @link(ssl_cryptlib) for more details.} |
| 135 |
function Accept: boolean; override; |
| 136 |
{:See @inherited} |
| 137 |
function Shutdown: boolean; override; |
| 138 |
{:See @inherited} |
| 139 |
function BiShutdown: boolean; override; |
| 140 |
{:See @inherited} |
| 141 |
function SendBuffer(Buffer: TMemory; Len: Integer): Integer; override; |
| 142 |
{:See @inherited} |
| 143 |
function RecvBuffer(Buffer: TMemory; Len: Integer): Integer; override; |
| 144 |
{:See @inherited} |
| 145 |
function WaitingData: Integer; override; |
| 146 |
{:See @inherited} |
| 147 |
function GetSSLVersion: string; override; |
| 148 |
{:See @inherited} |
| 149 |
function GetPeerSubject: string; override; |
| 150 |
{:See @inherited} |
| 151 |
function GetPeerSerialNo: integer; override; {pf} |
| 152 |
{:See @inherited} |
| 153 |
function GetPeerIssuer: string; override; |
| 154 |
{:See @inherited} |
| 155 |
function GetPeerName: string; override; |
| 156 |
{:See @inherited} |
| 157 |
function GetPeerNameHash: cardinal; override; {pf} |
| 158 |
{:See @inherited} |
| 159 |
function GetPeerFingerprint: ansistring; override; |
| 160 |
{:See @inherited} |
| 161 |
function GetCertInfo: string; override; |
| 162 |
{:See @inherited} |
| 163 |
function GetCipherName: string; override; |
| 164 |
{:See @inherited} |
| 165 |
function GetCipherBits: integer; override; |
| 166 |
{:See @inherited} |
| 167 |
function GetCipherAlgBits: integer; override; |
| 168 |
{:See @inherited} |
| 169 |
function GetVerifyCert: integer; override; |
| 170 |
end; |
| 171 |
|
| 172 |
implementation |
| 173 |
|
| 174 |
{==============================================================================} |
| 175 |
|
| 176 |
{$IFNDEF CIL} |
| 177 |
function PasswordCallback(buf:PAnsiChar; size:Integer; rwflag:Integer; userdata: Pointer):Integer; cdecl; |
| 178 |
var |
| 179 |
Password: AnsiString; |
| 180 |
begin |
| 181 |
Password := ''; |
| 182 |
if TCustomSSL(userdata) is TCustomSSL then |
| 183 |
Password := TCustomSSL(userdata).KeyPassword; |
| 184 |
if Length(Password) > (Size - 1) then |
| 185 |
SetLength(Password, Size - 1); |
| 186 |
Result := Length(Password); |
| 187 |
{$IFDEF DELPHI23_UP}AnsiStrings.{$ENDIF}StrLCopy(buf, PAnsiChar(Password + #0), Result + 1); |
| 188 |
end; |
| 189 |
{$ENDIF} |
| 190 |
|
| 191 |
{==============================================================================} |
| 192 |
|
| 193 |
constructor TSSLOpenSSL.Create(const Value: TTCPBlockSocket); |
| 194 |
begin |
| 195 |
inherited Create(Value); |
| 196 |
FCiphers := 'DEFAULT'; |
| 197 |
FSsl := nil; |
| 198 |
Fctx := nil; |
| 199 |
end; |
| 200 |
|
| 201 |
destructor TSSLOpenSSL.Destroy; |
| 202 |
begin |
| 203 |
DeInit; |
| 204 |
inherited Destroy; |
| 205 |
end; |
| 206 |
|
| 207 |
function TSSLOpenSSL.LibVersion: String; |
| 208 |
begin |
| 209 |
Result := SSLeayversion(0); |
| 210 |
end; |
| 211 |
|
| 212 |
function TSSLOpenSSL.LibName: String; |
| 213 |
begin |
| 214 |
Result := 'ssl_openssl'; |
| 215 |
end; |
| 216 |
|
| 217 |
function TSSLOpenSSL.SSLCheck: Boolean; |
| 218 |
var |
| 219 |
{$IFDEF CIL} |
| 220 |
sb: StringBuilder; |
| 221 |
{$ENDIF} |
| 222 |
s : AnsiString; |
| 223 |
begin |
| 224 |
Result := true; |
| 225 |
FLastErrorDesc := ''; |
| 226 |
FLastError := ErrGetError; |
| 227 |
ErrClearError; |
| 228 |
if FLastError <> 0 then |
| 229 |
begin |
| 230 |
Result := False; |
| 231 |
{$IFDEF CIL} |
| 232 |
sb := StringBuilder.Create(256); |
| 233 |
ErrErrorString(FLastError, sb, 256); |
| 234 |
FLastErrorDesc := Trim(sb.ToString); |
| 235 |
{$ELSE} |
| 236 |
s := StringOfChar(#0, 256); |
| 237 |
ErrErrorString(FLastError, s, Length(s)); |
| 238 |
FLastErrorDesc := s; |
| 239 |
{$ENDIF} |
| 240 |
end; |
| 241 |
end; |
| 242 |
|
| 243 |
function TSSLOpenSSL.CreateSelfSignedCert(Host: string): Boolean; |
| 244 |
var |
| 245 |
pk: EVP_PKEY; |
| 246 |
x: PX509; |
| 247 |
rsa: PRSA; |
| 248 |
t: PASN1_UTCTIME; |
| 249 |
name: PX509_NAME; |
| 250 |
b: PBIO; |
| 251 |
xn, y: integer; |
| 252 |
s: AnsiString; |
| 253 |
{$IFDEF CIL} |
| 254 |
sb: StringBuilder; |
| 255 |
{$ENDIF} |
| 256 |
begin |
| 257 |
Result := True; |
| 258 |
pk := EvpPkeynew; |
| 259 |
x := X509New; |
| 260 |
try |
| 261 |
rsa := RsaGenerateKey(2048, $10001, nil, nil); |
| 262 |
EvpPkeyAssign(pk, EVP_PKEY_RSA, rsa); |
| 263 |
X509SetVersion(x, 2); |
| 264 |
Asn1IntegerSet(X509getSerialNumber(x), 0); |
| 265 |
t := Asn1UtctimeNew; |
| 266 |
try |
| 267 |
X509GmtimeAdj(t, -60 * 60 *24); |
| 268 |
X509SetNotBefore(x, t); |
| 269 |
X509GmtimeAdj(t, 60 * 60 * 60 *24); |
| 270 |
X509SetNotAfter(x, t); |
| 271 |
finally |
| 272 |
Asn1UtctimeFree(t); |
| 273 |
end; |
| 274 |
X509SetPubkey(x, pk); |
| 275 |
Name := X509GetSubjectName(x); |
| 276 |
X509NameAddEntryByTxt(Name, 'C', $1001, 'CZ', -1, -1, 0); |
| 277 |
X509NameAddEntryByTxt(Name, 'CN', $1001, host, -1, -1, 0); |
| 278 |
x509SetIssuerName(x, Name); |
| 279 |
x509Sign(x, pk, EvpGetDigestByName('SHA1')); |
| 280 |
b := BioNew(BioSMem); |
| 281 |
try |
| 282 |
i2dX509Bio(b, x); |
| 283 |
xn := bioctrlpending(b); |
| 284 |
{$IFDEF CIL} |
| 285 |
sb := StringBuilder.Create(xn); |
| 286 |
y := bioread(b, sb, xn); |
| 287 |
if y > 0 then |
| 288 |
begin |
| 289 |
sb.Length := y; |
| 290 |
s := sb.ToString; |
| 291 |
end; |
| 292 |
{$ELSE} |
| 293 |
setlength(s, xn); |
| 294 |
y := bioread(b, s, xn); |
| 295 |
if y > 0 then |
| 296 |
setlength(s, y); |
| 297 |
{$ENDIF} |
| 298 |
finally |
| 299 |
BioFreeAll(b); |
| 300 |
end; |
| 301 |
FCertificate := s; |
| 302 |
b := BioNew(BioSMem); |
| 303 |
try |
| 304 |
i2dPrivatekeyBio(b, pk); |
| 305 |
xn := bioctrlpending(b); |
| 306 |
{$IFDEF CIL} |
| 307 |
sb := StringBuilder.Create(xn); |
| 308 |
y := bioread(b, sb, xn); |
| 309 |
if y > 0 then |
| 310 |
begin |
| 311 |
sb.Length := y; |
| 312 |
s := sb.ToString; |
| 313 |
end; |
| 314 |
{$ELSE} |
| 315 |
setlength(s, xn); |
| 316 |
y := bioread(b, s, xn); |
| 317 |
if y > 0 then |
| 318 |
setlength(s, y); |
| 319 |
{$ENDIF} |
| 320 |
finally |
| 321 |
BioFreeAll(b); |
| 322 |
end; |
| 323 |
FPrivatekey := s; |
| 324 |
finally |
| 325 |
X509free(x); |
| 326 |
EvpPkeyFree(pk); |
| 327 |
end; |
| 328 |
end; |
| 329 |
|
| 330 |
function TSSLOpenSSL.LoadPFX(pfxdata: Ansistring): Boolean; |
| 331 |
var |
| 332 |
cert, pkey, ca: SslPtr; |
| 333 |
b: PBIO; |
| 334 |
p12: SslPtr; |
| 335 |
begin |
| 336 |
Result := False; |
| 337 |
b := BioNew(BioSMem); |
| 338 |
try |
| 339 |
BioWrite(b, pfxdata, Length(PfxData)); |
| 340 |
p12 := d2iPKCS12bio(b, nil); |
| 341 |
if not Assigned(p12) then |
| 342 |
Exit; |
| 343 |
try |
| 344 |
cert := nil; |
| 345 |
pkey := nil; |
| 346 |
ca := nil; |
| 347 |
try {pf} |
| 348 |
if PKCS12parse(p12, FKeyPassword, pkey, cert, ca) > 0 then |
| 349 |
if SSLCTXusecertificate(Fctx, cert) > 0 then |
| 350 |
if SSLCTXusePrivateKey(Fctx, pkey) > 0 then |
| 351 |
Result := True; |
| 352 |
{pf} |
| 353 |
finally |
| 354 |
EvpPkeyFree(pkey); |
| 355 |
X509free(cert); |
| 356 |
SkX509PopFree(ca,_X509Free); // for ca=nil a new STACK was allocated... |
| 357 |
end; |
| 358 |
{/pf} |
| 359 |
finally |
| 360 |
PKCS12free(p12); |
| 361 |
end; |
| 362 |
finally |
| 363 |
BioFreeAll(b); |
| 364 |
end; |
| 365 |
end; |
| 366 |
|
| 367 |
function TSSLOpenSSL.SetSslKeys: boolean; |
| 368 |
var |
| 369 |
st: TFileStream; |
| 370 |
s: string; |
| 371 |
begin |
| 372 |
Result := False; |
| 373 |
if not assigned(FCtx) then |
| 374 |
Exit; |
| 375 |
try |
| 376 |
if FCertificateFile <> '' then |
| 377 |
if SslCtxUseCertificateChainFile(FCtx, FCertificateFile) <> 1 then |
| 378 |
if SslCtxUseCertificateFile(FCtx, FCertificateFile, SSL_FILETYPE_PEM) <> 1 then |
| 379 |
if SslCtxUseCertificateFile(FCtx, FCertificateFile, SSL_FILETYPE_ASN1) <> 1 then |
| 380 |
Exit; |
| 381 |
if FCertificate <> '' then |
| 382 |
if SslCtxUseCertificateASN1(FCtx, length(FCertificate), FCertificate) <> 1 then |
| 383 |
Exit; |
| 384 |
SSLCheck; |
| 385 |
if FPrivateKeyFile <> '' then |
| 386 |
if SslCtxUsePrivateKeyFile(FCtx, FPrivateKeyFile, SSL_FILETYPE_PEM) <> 1 then |
| 387 |
if SslCtxUsePrivateKeyFile(FCtx, FPrivateKeyFile, SSL_FILETYPE_ASN1) <> 1 then |
| 388 |
Exit; |
| 389 |
if FPrivateKey <> '' then |
| 390 |
if SslCtxUsePrivateKeyASN1(EVP_PKEY_RSA, FCtx, FPrivateKey, length(FPrivateKey)) <> 1 then |
| 391 |
Exit; |
| 392 |
SSLCheck; |
| 393 |
if FCertCAFile <> '' then |
| 394 |
if SslCtxLoadVerifyLocations(FCtx, FCertCAFile, '') <> 1 then |
| 395 |
Exit; |
| 396 |
if FPFXfile <> '' then |
| 397 |
begin |
| 398 |
try |
| 399 |
st := TFileStream.Create(FPFXfile, fmOpenRead or fmShareDenyNone); |
| 400 |
try |
| 401 |
s := ReadStrFromStream(st, st.Size); |
| 402 |
finally |
| 403 |
st.Free; |
| 404 |
end; |
| 405 |
if not LoadPFX(s) then |
| 406 |
Exit; |
| 407 |
except |
| 408 |
on Exception do |
| 409 |
Exit; |
| 410 |
end; |
| 411 |
end; |
| 412 |
if FPFX <> '' then |
| 413 |
if not LoadPFX(FPfx) then |
| 414 |
Exit; |
| 415 |
SSLCheck; |
| 416 |
Result := True; |
| 417 |
finally |
| 418 |
SSLCheck; |
| 419 |
end; |
| 420 |
end; |
| 421 |
|
| 422 |
function TSSLOpenSSL.NeedSigningCertificate: boolean; |
| 423 |
begin |
| 424 |
Result := (FCertificateFile = '') and (FCertificate = '') and (FPFXfile = '') and (FPFX = ''); |
| 425 |
end; |
| 426 |
|
| 427 |
function TSSLOpenSSL.Init: Boolean; |
| 428 |
var |
| 429 |
s: AnsiString; |
| 430 |
begin |
| 431 |
Result := False; |
| 432 |
FLastErrorDesc := ''; |
| 433 |
FLastError := 0; |
| 434 |
Fctx := nil; |
| 435 |
case FSSLType of |
| 436 |
LT_SSLv2: |
| 437 |
Fctx := SslCtxNew(SslMethodV2); |
| 438 |
LT_SSLv3: |
| 439 |
Fctx := SslCtxNew(SslMethodV3); |
| 440 |
LT_TLSv1: |
| 441 |
Fctx := SslCtxNew(SslMethodTLSV1); |
| 442 |
LT_TLSv1_1: |
| 443 |
Fctx := SslCtxNew(SslMethodTLSV11); |
| 444 |
LT_TLSv1_2: |
| 445 |
Fctx := SslCtxNew(SslMethodTLSV12); |
| 446 |
LT_all: |
| 447 |
begin |
| 448 |
//try new call for OpenSSL 1.1.0 first |
| 449 |
Fctx := SslCtxNew(SslMethodTLS); |
| 450 |
if Fctx=nil then |
| 451 |
//callback to previous versions |
| 452 |
Fctx := SslCtxNew(SslMethodV23); |
| 453 |
end; |
| 454 |
else |
| 455 |
Exit; |
| 456 |
end; |
| 457 |
if Fctx = nil then |
| 458 |
begin |
| 459 |
SSLCheck; |
| 460 |
Exit; |
| 461 |
end |
| 462 |
else |
| 463 |
begin |
| 464 |
s := FCiphers; |
| 465 |
SslCtxSetCipherList(Fctx, s); |
| 466 |
if FVerifyCert then |
| 467 |
SslCtxSetVerify(FCtx, SSL_VERIFY_PEER, nil) |
| 468 |
else |
| 469 |
SslCtxSetVerify(FCtx, SSL_VERIFY_NONE, nil); |
| 470 |
{$IFNDEF CIL} |
| 471 |
SslCtxSetDefaultPasswdCb(FCtx, @PasswordCallback); |
| 472 |
SslCtxSetDefaultPasswdCbUserdata(FCtx, self); |
| 473 |
{$ENDIF} |
| 474 |
|
| 475 |
if server and NeedSigningCertificate then |
| 476 |
begin |
| 477 |
CreateSelfSignedcert(FSocket.ResolveIPToName(FSocket.GetRemoteSinIP)); |
| 478 |
end; |
| 479 |
|
| 480 |
if not SetSSLKeys then |
| 481 |
Exit |
| 482 |
else |
| 483 |
begin |
| 484 |
Fssl := nil; |
| 485 |
Fssl := SslNew(Fctx); |
| 486 |
if Fssl = nil then |
| 487 |
begin |
| 488 |
SSLCheck; |
| 489 |
exit; |
| 490 |
end; |
| 491 |
end; |
| 492 |
end; |
| 493 |
Result := true; |
| 494 |
end; |
| 495 |
|
| 496 |
function TSSLOpenSSL.DeInit: Boolean; |
| 497 |
begin |
| 498 |
Result := True; |
| 499 |
if Assigned(FSessionNew) then |
| 500 |
begin |
| 501 |
SslSessionFree(FSessionNew); |
| 502 |
FSessionNew := nil; |
| 503 |
end; |
| 504 |
if assigned (Fssl) then |
| 505 |
sslfree(Fssl); |
| 506 |
Fssl := nil; |
| 507 |
if assigned (Fctx) then |
| 508 |
begin |
| 509 |
SslCtxFree(Fctx); |
| 510 |
Fctx := nil; |
| 511 |
ErrRemoveState(0); |
| 512 |
end; |
| 513 |
FSSLEnabled := False; |
| 514 |
end; |
| 515 |
|
| 516 |
function TSSLOpenSSL.Prepare: Boolean; |
| 517 |
begin |
| 518 |
Result := false; |
| 519 |
DeInit; |
| 520 |
if Init then |
| 521 |
Result := true |
| 522 |
else |
| 523 |
DeInit; |
| 524 |
end; |
| 525 |
|
| 526 |
function TSSLOpenSSL.Connect: boolean; |
| 527 |
var |
| 528 |
x: integer; |
| 529 |
b: boolean; |
| 530 |
err: integer; |
| 531 |
begin |
| 532 |
Result := False; |
| 533 |
if FSocket.Socket = INVALID_SOCKET then |
| 534 |
Exit; |
| 535 |
FServer := False; |
| 536 |
if Prepare then |
| 537 |
begin |
| 538 |
{$IFDEF CIL} |
| 539 |
if sslsetfd(FSsl, FSocket.Socket.Handle.ToInt32) < 1 then |
| 540 |
{$ELSE} |
| 541 |
if sslsetfd(FSsl, FSocket.Socket) < 1 then |
| 542 |
{$ENDIF} |
| 543 |
begin |
| 544 |
SSLCheck; |
| 545 |
Exit; |
| 546 |
end; |
| 547 |
// Reuse session |
| 548 |
if Assigned(FSessionOld) then begin |
| 549 |
SslSetSession(Fssl, FSessionOld); |
| 550 |
end; |
| 551 |
if SNIHost<>'' then |
| 552 |
begin |
| 553 |
SSLCtrl(Fssl, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, PAnsiChar(AnsiString(SNIHost))); |
| 554 |
SslSet1Host(Fssl, PAnsiChar(AnsiString(SNIHost))); |
| 555 |
end; |
| 556 |
if FSocket.ConnectionTimeout <= 0 then //do blocking call of SSL_Connect |
| 557 |
begin |
| 558 |
x := sslconnect(FSsl); |
| 559 |
if x < 1 then |
| 560 |
begin |
| 561 |
SSLcheck; |
| 562 |
Exit; |
| 563 |
end; |
| 564 |
end |
| 565 |
else //do non-blocking call of SSL_Connect |
| 566 |
begin |
| 567 |
b := Fsocket.NonBlockMode; |
| 568 |
Fsocket.NonBlockMode := true; |
| 569 |
repeat |
| 570 |
x := sslconnect(FSsl); |
| 571 |
err := SslGetError(FSsl, x); |
| 572 |
if err = SSL_ERROR_WANT_READ then |
| 573 |
if not FSocket.CanRead(FSocket.ConnectionTimeout) then |
| 574 |
break; |
| 575 |
if err = SSL_ERROR_WANT_WRITE then |
| 576 |
if not FSocket.CanWrite(FSocket.ConnectionTimeout) then |
| 577 |
break; |
| 578 |
until (err <> SSL_ERROR_WANT_READ) and (err <> SSL_ERROR_WANT_WRITE); |
| 579 |
Fsocket.NonBlockMode := b; |
| 580 |
if err <> SSL_ERROR_NONE then |
| 581 |
begin |
| 582 |
SSLcheck; |
| 583 |
Exit; |
| 584 |
end; |
| 585 |
end; |
| 586 |
if FverifyCert then |
| 587 |
if (GetVerifyCert <> 0) or (not DoVerifyCert) then |
| 588 |
Exit; |
| 589 |
FSSLEnabled := True; |
| 590 |
Result := True; |
| 591 |
end; |
| 592 |
if Result and (FSessionOld = nil) then begin |
| 593 |
FSessionNew := SslGet1Session(Fssl); |
| 594 |
end; |
| 595 |
end; |
| 596 |
|
| 597 |
function TSSLOpenSSL.Accept: boolean; |
| 598 |
var |
| 599 |
x: integer; |
| 600 |
begin |
| 601 |
Result := False; |
| 602 |
if FSocket.Socket = INVALID_SOCKET then |
| 603 |
Exit; |
| 604 |
FServer := True; |
| 605 |
if Prepare then |
| 606 |
begin |
| 607 |
{$IFDEF CIL} |
| 608 |
if sslsetfd(FSsl, FSocket.Socket.Handle.ToInt32) < 1 then |
| 609 |
{$ELSE} |
| 610 |
if sslsetfd(FSsl, FSocket.Socket) < 1 then |
| 611 |
{$ENDIF} |
| 612 |
begin |
| 613 |
SSLCheck; |
| 614 |
Exit; |
| 615 |
end; |
| 616 |
x := sslAccept(FSsl); |
| 617 |
if x < 1 then |
| 618 |
begin |
| 619 |
SSLcheck; |
| 620 |
Exit; |
| 621 |
end; |
| 622 |
FSSLEnabled := True; |
| 623 |
Result := True; |
| 624 |
end; |
| 625 |
end; |
| 626 |
|
| 627 |
function TSSLOpenSSL.Shutdown: boolean; |
| 628 |
begin |
| 629 |
if assigned(FSsl) then |
| 630 |
sslshutdown(FSsl); |
| 631 |
DeInit; |
| 632 |
Result := True; |
| 633 |
end; |
| 634 |
|
| 635 |
function TSSLOpenSSL.BiShutdown: boolean; |
| 636 |
var |
| 637 |
x: integer; |
| 638 |
begin |
| 639 |
if assigned(FSsl) then |
| 640 |
begin |
| 641 |
x := sslshutdown(FSsl); |
| 642 |
if x = 0 then |
| 643 |
begin |
| 644 |
Synsock.Shutdown(FSocket.Socket, 1); |
| 645 |
sslshutdown(FSsl); |
| 646 |
end; |
| 647 |
end; |
| 648 |
DeInit; |
| 649 |
Result := True; |
| 650 |
end; |
| 651 |
|
| 652 |
function TSSLOpenSSL.SendBuffer(Buffer: TMemory; Len: Integer): Integer; |
| 653 |
var |
| 654 |
err: integer; |
| 655 |
{$IFDEF CIL} |
| 656 |
s: ansistring; |
| 657 |
{$ENDIF} |
| 658 |
begin |
| 659 |
FLastError := 0; |
| 660 |
FLastErrorDesc := ''; |
| 661 |
repeat |
| 662 |
{$IFDEF CIL} |
| 663 |
s := StringOf(Buffer); |
| 664 |
Result := SslWrite(FSsl, s, Len); |
| 665 |
{$ELSE} |
| 666 |
Result := SslWrite(FSsl, Buffer , Len); |
| 667 |
{$ENDIF} |
| 668 |
err := SslGetError(FSsl, Result); |
| 669 |
until (err <> SSL_ERROR_WANT_READ) and (err <> SSL_ERROR_WANT_WRITE); |
| 670 |
if err = SSL_ERROR_ZERO_RETURN then |
| 671 |
Result := 0 |
| 672 |
else |
| 673 |
if (err <> 0) then |
| 674 |
FLastError := err; |
| 675 |
end; |
| 676 |
|
| 677 |
function TSSLOpenSSL.RecvBuffer(Buffer: TMemory; Len: Integer): Integer; |
| 678 |
var |
| 679 |
err: integer; |
| 680 |
{$IFDEF CIL} |
| 681 |
sb: stringbuilder; |
| 682 |
s: ansistring; |
| 683 |
{$ENDIF} |
| 684 |
begin |
| 685 |
FLastError := 0; |
| 686 |
FLastErrorDesc := ''; |
| 687 |
repeat |
| 688 |
{$IFDEF CIL} |
| 689 |
sb := StringBuilder.Create(Len); |
| 690 |
Result := SslRead(FSsl, sb, Len); |
| 691 |
if Result > 0 then |
| 692 |
begin |
| 693 |
sb.Length := Result; |
| 694 |
s := sb.ToString; |
| 695 |
System.Array.Copy(BytesOf(s), Buffer, length(s)); |
| 696 |
end; |
| 697 |
{$ELSE} |
| 698 |
Result := SslRead(FSsl, Buffer , Len); |
| 699 |
{$ENDIF} |
| 700 |
err := SslGetError(FSsl, Result); |
| 701 |
until (err <> SSL_ERROR_WANT_READ) and (err <> SSL_ERROR_WANT_WRITE); |
| 702 |
if err = SSL_ERROR_ZERO_RETURN then |
| 703 |
Result := 0 |
| 704 |
{pf}// Verze 1.1.0 byla s else tak jak to ted mam, |
| 705 |
// ve verzi 1.1.1 bylo ELSE zruseno, ale pak je SSL_ERROR_ZERO_RETURN |
| 706 |
// propagovano jako Chyba. |
| 707 |
{pf} else {/pf} if (err <> 0) then |
| 708 |
FLastError := err; |
| 709 |
end; |
| 710 |
|
| 711 |
function TSSLOpenSSL.WaitingData: Integer; |
| 712 |
begin |
| 713 |
Result := sslpending(Fssl); |
| 714 |
end; |
| 715 |
|
| 716 |
function TSSLOpenSSL.GetSSLVersion: string; |
| 717 |
begin |
| 718 |
if not assigned(FSsl) then |
| 719 |
Result := '' |
| 720 |
else |
| 721 |
Result := SSlGetVersion(FSsl); |
| 722 |
end; |
| 723 |
|
| 724 |
function TSSLOpenSSL.GetPeerSubject: string; |
| 725 |
var |
| 726 |
cert: PX509; |
| 727 |
s: ansistring; |
| 728 |
{$IFDEF CIL} |
| 729 |
sb: StringBuilder; |
| 730 |
{$ENDIF} |
| 731 |
begin |
| 732 |
if not assigned(FSsl) then |
| 733 |
begin |
| 734 |
Result := ''; |
| 735 |
Exit; |
| 736 |
end; |
| 737 |
cert := SSLGetPeerCertificate(Fssl); |
| 738 |
if not assigned(cert) then |
| 739 |
begin |
| 740 |
Result := ''; |
| 741 |
Exit; |
| 742 |
end; |
| 743 |
{$IFDEF CIL} |
| 744 |
sb := StringBuilder.Create(4096); |
| 745 |
Result := X509NameOneline(X509GetSubjectName(cert), sb, 4096); |
| 746 |
{$ELSE} |
| 747 |
setlength(s, 4096); |
| 748 |
Result := X509NameOneline(X509GetSubjectName(cert), s, Length(s)); |
| 749 |
{$ENDIF} |
| 750 |
X509Free(cert); |
| 751 |
end; |
| 752 |
|
| 753 |
|
| 754 |
function TSSLOpenSSL.GetPeerSerialNo: integer; {pf} |
| 755 |
var |
| 756 |
cert: PX509; |
| 757 |
SN: PASN1_INTEGER; |
| 758 |
begin |
| 759 |
if not assigned(FSsl) then |
| 760 |
begin |
| 761 |
Result := -1; |
| 762 |
Exit; |
| 763 |
end; |
| 764 |
cert := SSLGetPeerCertificate(Fssl); |
| 765 |
try |
| 766 |
if not assigned(cert) then |
| 767 |
begin |
| 768 |
Result := -1; |
| 769 |
Exit; |
| 770 |
end; |
| 771 |
SN := X509GetSerialNumber(cert); |
| 772 |
Result := Asn1IntegerGet(SN); |
| 773 |
finally |
| 774 |
X509Free(cert); |
| 775 |
end; |
| 776 |
end; |
| 777 |
|
| 778 |
function TSSLOpenSSL.GetPeerName: string; |
| 779 |
var |
| 780 |
s: ansistring; |
| 781 |
begin |
| 782 |
s := GetPeerSubject; |
| 783 |
s := SeparateRight(s, '/CN='); |
| 784 |
Result := Trim(SeparateLeft(s, '/')); |
| 785 |
end; |
| 786 |
|
| 787 |
function TSSLOpenSSL.GetPeerNameHash: cardinal; {pf} |
| 788 |
var |
| 789 |
cert: PX509; |
| 790 |
begin |
| 791 |
if not assigned(FSsl) then |
| 792 |
begin |
| 793 |
Result := 0; |
| 794 |
Exit; |
| 795 |
end; |
| 796 |
cert := SSLGetPeerCertificate(Fssl); |
| 797 |
try |
| 798 |
if not assigned(cert) then |
| 799 |
begin |
| 800 |
Result := 0; |
| 801 |
Exit; |
| 802 |
end; |
| 803 |
Result := X509NameHash(X509GetSubjectName(cert)); |
| 804 |
finally |
| 805 |
X509Free(cert); |
| 806 |
end; |
| 807 |
end; |
| 808 |
|
| 809 |
function TSSLOpenSSL.GetPeerIssuer: string; |
| 810 |
var |
| 811 |
cert: PX509; |
| 812 |
s: ansistring; |
| 813 |
{$IFDEF CIL} |
| 814 |
sb: StringBuilder; |
| 815 |
{$ENDIF} |
| 816 |
begin |
| 817 |
if not assigned(FSsl) then |
| 818 |
begin |
| 819 |
Result := ''; |
| 820 |
Exit; |
| 821 |
end; |
| 822 |
cert := SSLGetPeerCertificate(Fssl); |
| 823 |
if not assigned(cert) then |
| 824 |
begin |
| 825 |
Result := ''; |
| 826 |
Exit; |
| 827 |
end; |
| 828 |
{$IFDEF CIL} |
| 829 |
sb := StringBuilder.Create(4096); |
| 830 |
Result := X509NameOneline(X509GetIssuerName(cert), sb, 4096); |
| 831 |
{$ELSE} |
| 832 |
setlength(s, 4096); |
| 833 |
Result := X509NameOneline(X509GetIssuerName(cert), s, Length(s)); |
| 834 |
{$ENDIF} |
| 835 |
X509Free(cert); |
| 836 |
end; |
| 837 |
|
| 838 |
function TSSLOpenSSL.GetPeerFingerprint: ansistring; |
| 839 |
var |
| 840 |
cert: PX509; |
| 841 |
x: integer; |
| 842 |
{$IFDEF CIL} |
| 843 |
sb: StringBuilder; |
| 844 |
{$ENDIF} |
| 845 |
begin |
| 846 |
if not assigned(FSsl) then |
| 847 |
begin |
| 848 |
Result := ''; |
| 849 |
Exit; |
| 850 |
end; |
| 851 |
cert := SSLGetPeerCertificate(Fssl); |
| 852 |
if not assigned(cert) then |
| 853 |
begin |
| 854 |
Result := ''; |
| 855 |
Exit; |
| 856 |
end; |
| 857 |
{$IFDEF CIL} |
| 858 |
sb := StringBuilder.Create(EVP_MAX_MD_SIZE); |
| 859 |
X509Digest(cert, EvpGetDigestByName('MD5'), sb, x); |
| 860 |
sb.Length := x; |
| 861 |
Result := sb.ToString; |
| 862 |
{$ELSE} |
| 863 |
setlength(Result, EVP_MAX_MD_SIZE); |
| 864 |
X509Digest(cert, EvpGetDigestByName('MD5'), Result, x); |
| 865 |
SetLength(Result, x); |
| 866 |
{$ENDIF} |
| 867 |
X509Free(cert); |
| 868 |
end; |
| 869 |
|
| 870 |
function TSSLOpenSSL.GetCertInfo: string; |
| 871 |
var |
| 872 |
cert: PX509; |
| 873 |
x, y: integer; |
| 874 |
b: PBIO; |
| 875 |
s: AnsiString; |
| 876 |
{$IFDEF CIL} |
| 877 |
sb: stringbuilder; |
| 878 |
{$ENDIF} |
| 879 |
begin |
| 880 |
if not assigned(FSsl) then |
| 881 |
begin |
| 882 |
Result := ''; |
| 883 |
Exit; |
| 884 |
end; |
| 885 |
cert := SSLGetPeerCertificate(Fssl); |
| 886 |
if not assigned(cert) then |
| 887 |
begin |
| 888 |
Result := ''; |
| 889 |
Exit; |
| 890 |
end; |
| 891 |
try {pf} |
| 892 |
b := BioNew(BioSMem); |
| 893 |
try |
| 894 |
X509Print(b, cert); |
| 895 |
x := bioctrlpending(b); |
| 896 |
{$IFDEF CIL} |
| 897 |
sb := StringBuilder.Create(x); |
| 898 |
y := bioread(b, sb, x); |
| 899 |
if y > 0 then |
| 900 |
begin |
| 901 |
sb.Length := y; |
| 902 |
s := sb.ToString; |
| 903 |
end; |
| 904 |
{$ELSE} |
| 905 |
setlength(s,x); |
| 906 |
y := bioread(b,s,x); |
| 907 |
if y > 0 then |
| 908 |
setlength(s, y); |
| 909 |
{$ENDIF} |
| 910 |
Result := ReplaceString(s, LF, CRLF); |
| 911 |
finally |
| 912 |
BioFreeAll(b); |
| 913 |
end; |
| 914 |
{pf} |
| 915 |
finally |
| 916 |
X509Free(cert); |
| 917 |
end; |
| 918 |
{/pf} |
| 919 |
end; |
| 920 |
|
| 921 |
function TSSLOpenSSL.GetCipherName: string; |
| 922 |
begin |
| 923 |
if not assigned(FSsl) then |
| 924 |
Result := '' |
| 925 |
else |
| 926 |
Result := SslCipherGetName(SslGetCurrentCipher(FSsl)); |
| 927 |
end; |
| 928 |
|
| 929 |
function TSSLOpenSSL.GetCipherBits: integer; |
| 930 |
var |
| 931 |
x: integer; |
| 932 |
begin |
| 933 |
if not assigned(FSsl) then |
| 934 |
Result := 0 |
| 935 |
else |
| 936 |
Result := SSLCipherGetBits(SslGetCurrentCipher(FSsl), x); |
| 937 |
end; |
| 938 |
|
| 939 |
function TSSLOpenSSL.GetCipherAlgBits: integer; |
| 940 |
begin |
| 941 |
if not assigned(FSsl) then |
| 942 |
Result := 0 |
| 943 |
else |
| 944 |
SSLCipherGetBits(SslGetCurrentCipher(FSsl), Result); |
| 945 |
end; |
| 946 |
|
| 947 |
function TSSLOpenSSL.GetVerifyCert: integer; |
| 948 |
begin |
| 949 |
if not assigned(FSsl) then |
| 950 |
Result := 1 |
| 951 |
else |
| 952 |
Result := SslGetVerifyResult(FSsl); |
| 953 |
end; |
| 954 |
|
| 955 |
{==============================================================================} |
| 956 |
|
| 957 |
initialization |
| 958 |
if InitSSLInterface then |
| 959 |
SSLImplementation := TSSLOpenSSL; |
| 960 |
|
| 961 |
end. |