-
Notifications
You must be signed in to change notification settings - Fork 90
/
Grijjy.OpenSSL.pas
480 lines (419 loc) · 12.2 KB
/
Grijjy.OpenSSL.pas
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
unit Grijjy.OpenSSL;
{ OpenSSL handler for Grijjy connections }
{$I Grijjy.inc}
interface
uses
Grijjy.OpenSSL.API,
System.SysUtils,
Grijjy.MemoryPool;
const
DEFAULT_BLOCK_SIZE = 4096;
type
{ Callback events }
TgoOpenSSLNotify = procedure of object;
TgoOpenSSLData = procedure(const ABuffer: Pointer; const ASize: Integer) of object;
{ OpenSSL protocol handler }
TgoOpenSSL = class(TObject)
protected
FOnConnected: TgoOpenSSLNotify;
FOnRead: TgoOpenSSLData;
FOnWrite: TgoOpenSSLData;
private
{ OpenSSL related objects }
FHandshaking: Boolean;
FSSLContext: PSSL_CTX;
FSSL: PSSL;
FBIORead: PBIO;
FBIOWrite: PBIO;
FSSLWriteBuffer: Pointer;
FSSLReadBuffer: Pointer;
{ Hostname for SNI }
FHost: String;
FPort: Word;
{ Certificate and Private Key }
FCertificate: TBytes;
FPrivateKey: TBytes;
FPassword: UnicodeString;
public
constructor Create;
destructor Destroy; override;
public
{ Start SSL connect handshake }
function Connect(const AALPN: Boolean = False): Boolean;
{ Free SSL related objects }
procedure Release;
{ Do SSL read from socket }
procedure Read(const ABuffer: Pointer = nil; const ASize: Integer = 0);
{ Do SSL write to socket }
function Write(const ABuffer: Pointer; const ASize: Integer): Boolean;
{ Returns True if ALPN is negotiated }
function ALPN: Boolean;
public
{ Host }
property Host: String read FHost write FHost;
{ Port }
property Port: Word read FPort write FPort;
{ Certificate in PEM format }
property Certificate: TBytes read FCertificate write FCertificate;
{ Private key in PEM format }
property PrivateKey: TBytes read FPrivateKey write FPrivateKey;
{ Password for private key }
property Password: UnicodeString read FPassword write FPassword;
public
{ Fired when the SSL connection is established }
property OnConnected: TgoOpenSSLNotify read FOnConnected write FOnConnected;
{ Fired when decrypted SSL data is ready to be read }
property OnRead: TgoOpenSSLData read FOnRead write FOnRead;
{ Fired when encrypted SSL data is ready to be sent }
property OnWrite: TgoOpenSSLData read FOnWrite write FOnWrite;
end;
{ Helper class for SSL }
TgoSSLHelper = class
private class var
FTarget: Integer;
public
class procedure LoadSSL;
class procedure UnloadSSL;
class procedure SetCertificate(ctx: PSSL_CTX; const ACertificate, APrivateKey: TBytes;
const APassword: UnicodeString = ''); overload;
class procedure SetCertificate(ctx: PSSL_CTX; const ACertificateFile, APrivateKeyFile: UnicodeString;
const APassword: UnicodeString = ''); overload;
public
class function Sign_RSASHA256(const AData: TBytes; const APrivateKey: TBytes;
out ASignature: TBytes): Boolean;
class function HMAC_SHA256(const AKey, AData: RawByteString): String;
class function HMAC_SHA1(const AKey, AData: RawByteString): TBytes;
end;
implementation
uses
System.IOUtils,
System.SyncObjs,
System.Classes;
var
_MemBufferPool: TgoMemoryPool;
{ TgoOpenSSL }
constructor TgoOpenSSL.Create;
begin
inherited Create;
FHandshaking := False;
FSSL := nil;
FSSLContext := nil;
FSSLWriteBuffer := nil;
FSSLReadBuffer := nil;
end;
destructor TgoOpenSSL.Destroy;
begin
Release;
ERR_remove_thread_state(0);
inherited Destroy;
end;
function TgoOpenSSL.Connect(const AALPN: Boolean): Boolean;
begin
Result := False;
{ create ssl context }
FSSLContext := SSL_CTX_new(SSLv23_method);
if FSSLContext <> nil then
begin
{ if we are connecting using the http2 protocol and TLS }
if AALPN then
begin
{ force TLS 1.2 }
SetSSLCTXOptions(FSSLContext,
SSL_OP_ALL + SSL_OP_NO_SSLv2 + SSL_OP_NO_SSLv3 + SSL_OP_NO_COMPRESSION);
{ enable Application-Layer Protocol Negotiation Extension }
SSL_CTX_set_alpn_protos(FSSLContext, #2'h2', 3);
end;
{ no certificate validation }
SSL_CTX_set_verify(FSSLContext, SSL_VERIFY_NONE, nil);
{ apply PEM Certificate }
if FCertificate <> nil then
begin
if FPrivateKey = nil then
TgoSSLHelper.SetCertificate(FSSLContext, FCertificate, FCertificate, FPassword)
else
TgoSSLHelper.SetCertificate(FSSLContext, FCertificate, FPrivateKey, FPassword);
{ Example loading certificate directly from a file:
S := ExtractFilePath(ParamStr(0)) + 'Grijjy.pem';
SSL_CTX_use_certificate_file(FSSLContext, PAnsiChar(S), 1);
SSL_CTX_use_RSAPrivateKey_file(FSSLContext, PAnsiChar(S), 1);
}
{ Example loading CA certificate directly from a file:
SSL_CTX_load_verify_locations(FSSLContext, 'entrust_2048_ca.cer', nil);
}
{ Example loading CA certificate into memory:
X509_Store := SSL_CTX_get_cert_store(FSSLContext);
ABIO := BIO_new(BIO_s_file);
BIO_read_filename(ABIO, PAnsiChar(AFile));
ACert := PEM_read_bio_X509(ABIO, nil, nil, nil);
X509_STORE_add_cert(X509_Store, ACert); }
end;
{ create an SSL struct for the connection }
FSSL := SSL_new(FSSLContext);
if FSSL <> nil then
begin
{ Apply SNI hostname }
SSL_set_tlsext_host_name(FSSL, FHost);
{ create the read and write BIO }
FBIORead := BIO_new(BIO_s_mem);
if FBIORead <> nil then
begin
FBIOWrite := BIO_new(BIO_s_mem);
if FBIOWrite <> nil then
begin
FHandshaking := True;
{ relate the BIO to the SSL object }
SSL_set_bio(FSSL, FBIORead, FBIOWrite);
{ ssl session should start the negotiation }
SSL_set_connect_state(FSSL);
{ allocate buffers }
FSSLWriteBuffer :=_MemBufferPool.RequestMem;
FSSLReadBuffer :=_MemBufferPool.RequestMem;
{ start ssl handshake sequence }
Read;
{ SSL success }
Result := True;
end;
end;
end;
end;
end;
procedure TgoOpenSSL.Release;
begin
{ free handle }
if FSSL <> nil then
begin
SSL_shutdown(FSSL);
SSL_free(FSSL);
FSSL := nil;
end;
{ free context }
if FSSLContext <> nil then
begin
SSL_CTX_free(FSSLContext);
FSSLContext := nil;
end;
{ release buffers }
if FSSLWriteBuffer <> nil then
begin
_MemBufferPool.ReleaseMem(FSSLWriteBuffer);
FSSLWriteBuffer := nil;
end;
if FSSLReadBuffer <> nil then
begin
_MemBufferPool.ReleaseMem(FSSLReadBuffer);
FSSLReadBuffer := nil;
end;
end;
procedure TgoOpenSSL.Read(const ABuffer: Pointer; const ASize: Integer);
var
Bytes: Integer;
Error: Integer;
begin
while True do
begin
BIO_write(FBIORead, ABuffer, ASize);
if not BIORetry(FBIORead) then
Break;
end;
while True do
begin
Bytes := SSL_read(FSSL, FSSLReadBuffer, DEFAULT_BLOCK_SIZE);
if Bytes > 0 then
begin
if Assigned(FOnRead) then
FOnRead(FSSLReadBuffer, Bytes)
end
else
begin
Error := SSL_get_error(FSSL, Bytes);
if not SSLErrorFatal(Error) then
Break
else
Exit;
end;
end;
{ handshake data needs to be written? }
if BIO_ctrl(FBIOWrite, BIO_CTRL_PENDING, 0, nil) <> 0 then
begin
Bytes := BIO_read(FBIOWrite, FSSLWriteBuffer, DEFAULT_BLOCK_SIZE);
if Bytes > 0 then
begin
if Assigned(FOnWrite) then
FOnWrite(FSSLWriteBuffer, Bytes);
end
else
begin
Error := SSL_get_error(FSSL, Bytes);
if SSLErrorFatal(Error) then
Exit;
end;
end;
{ with ssl we are only connected and can write once the handshake is finished }
if FHandshaking then
if SSL_state(FSSL) = SSL_ST_OK then
begin
FHandshaking := False;
if Assigned(FOnConnected) then
FOnConnected;
end
end;
function TgoOpenSSL.Write(const ABuffer: Pointer; const ASize: Integer): Boolean;
var
Bytes: Integer;
Error: Integer;
begin
Result := False;
Bytes := SSL_write(FSSL, ABuffer, ASize);
if Bytes <> ASize then
begin
Error := SSL_get_error(FSSL, Bytes);
if SSLErrorFatal(Error) then
Exit;
end;
while BIO_ctrl(FBIOWrite, BIO_CTRL_PENDING, 0, nil) <> 0 do
begin
Bytes := BIO_read(FBIOWrite, FSSLWriteBuffer, DEFAULT_BLOCK_SIZE);
if Bytes > 0 then
begin
Result := True;
if Assigned(FOnWrite) then
FOnWrite(FSSLWriteBuffer, Bytes);
end
else
begin
Error := SSL_get_error(FSSL, Bytes);
if SSLErrorFatal(Error) then
Exit;
end;
end;
end;
function TgoOpenSSL.ALPN: Boolean;
var
ALPN: MarshaledAString;
ALPNLen: Integer;
begin
SSL_get0_alpn_selected(FSSL, ALPN, ALPNLen);
Result := (ALPNLen = 2) and (ALPN[0] = 'h') and (ALPN[1] = '2');
end;
{ TgoSSLHelper }
class procedure TgoSSLHelper.LoadSSL;
begin
if (TInterlocked.Increment(FTarget) = 1) then
begin
LoadLIBEAY;
LoadSSLEAY;
SSLInitialize;
end;
end;
class procedure TgoSSLHelper.UnloadSSL;
begin
if (TInterlocked.Decrement(FTarget) = 0) then
begin
SSLFinalize;
UnloadSSLEAY;
UnloadLIBEAY;
end;
end;
class procedure TgoSSLHelper.SetCertificate(ctx: PSSL_CTX; const ACertificate, APrivateKey: TBytes;
const APassword: UnicodeString = '');
var
BIOCert, BIOPrivateKey: PBIO;
Certificate: PX509;
PrivateKey: PEVP_PKEY;
Password: RawByteString;
begin
BIOCert := BIO_new_mem_buf(@ACertificate[0], Length(ACertificate));
BIOPrivateKey := BIO_new_mem_buf(@APrivateKey[0], Length(APrivateKey));
Certificate := PEM_read_bio_X509(BIOCert, nil, nil, nil);
if APassword <> '' then
begin
Password := MarshaledAString(RawByteString(APassword));
PrivateKey := PEM_read_bio_PrivateKey(BIOPrivateKey, nil, nil, @Password[1]);
end
else
PrivateKey := PEM_read_bio_PrivateKey(BIOPrivateKey, nil, nil, nil);
SSL_CTX_use_certificate(ctx, Certificate);
SSL_CTX_use_privatekey(ctx, PrivateKey);
X509_free(Certificate);
EVP_PKEY_free(PrivateKey);
BIO_free(BIOCert);
BIO_free(BIOPrivateKey);
if (SSL_CTX_check_private_key(ctx) = 0) then
raise Exception.Create('Private key does not match the certificate public key');
end;
class procedure TgoSSLHelper.SetCertificate(ctx: PSSL_CTX; const ACertificateFile, APrivateKeyFile: UnicodeString;
const APassword: UnicodeString = '');
var
Certificate, PrivateKey: TBytes;
begin
Certificate := TFile.ReadAllBytes(ACertificateFile);
PrivateKey := TFile.ReadAllBytes(APrivateKeyFile);
SetCertificate(ctx, Certificate, PrivateKey, APassword);
end;
class function TgoSSLHelper.Sign_RSASHA256(const AData: TBytes; const APrivateKey: TBytes;
out ASignature: TBytes): Boolean;
var
BIOPrivateKey: PBIO;
PrivateKey: PEVP_PKEY;
Ctx: PEVP_MD_CTX;
SHA256: PEVP_MD;
Size: Cardinal;
begin
BIOPrivateKey := BIO_new_mem_buf(@APrivateKey[0], Length(APrivateKey));
PrivateKey := PEM_read_bio_PrivateKey(BIOPrivateKey, nil, nil, nil);
Ctx := EVP_MD_CTX_create;
try
SHA256 := EVP_sha256;
if (EVP_DigestSignInit(Ctx, nil, SHA256, nil, PrivateKey) > 0) and
(EVP_DigestUpdate(Ctx, @AData[0], Length(AData)) > 0) and
(EVP_DigestSignFinal(Ctx, nil, Size) > 0) then
begin
SetLength(ASignature, Size);
Result := EVP_DigestSignFinal(Ctx, @ASignature[0], Size) > 0;
end
else
Result := False;
finally
EVP_MD_CTX_destroy(Ctx);
end;
end;
class function TgoSSLHelper.HMAC_SHA256(const AKey, AData: RawByteString): String;
const
EVP_MAX_MD_SIZE = 64;
var
MessageAuthCode: PByte;
Size: Integer;
Buffer, Text: TBytes;
begin
Size := EVP_MAX_MD_SIZE;
SetLength(Buffer, Size);
MessageAuthCode := HMAC(EVP_sha256, @AKey[1], Length(AKey), @AData[1], Length(AData), @Buffer[0], Size);
if MessageAuthCode <> nil then
begin
SetLength(Text, Size * 2);
BinToHex(Buffer, 0, Text, 0, Size);
Result := TEncoding.UTF8.GetString(Text).ToLower;
end;
end;
class function TgoSSLHelper.HMAC_SHA1(const AKey, AData: RawByteString): TBytes;
const
EVP_MAX_MD_SIZE = 20;
var
MessageAuthCode: PByte;
Size: Integer;
begin
Size := EVP_MAX_MD_SIZE;
SetLength(Result, Size);
MessageAuthCode := HMAC(EVP_sha1, @AKey[1], Length(AKey), @AData[1], Length(AData), @Result[0], Size);
if MessageAuthCode <> nil then
SetLength(Result, Size);
end;
initialization
TgoSSLHelper.LoadSSL;
SSL_load_error_strings;
SSL_library_init;
_MemBufferPool := TgoMemoryPool.Create(DEFAULT_BLOCK_SIZE);
finalization
TgoSSLHelper.UnloadSSL;
_MemBufferPool.Free;
end.