-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
pal_ssl.c
1066 lines (885 loc) · 26 KB
/
pal_ssl.c
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "pal_ssl.h"
#include "openssl.h"
#include "pal_evp_pkey.h"
#include "pal_evp_pkey_rsa.h"
#include "pal_x509.h"
#include <assert.h>
#include <string.h>
#include <stdbool.h>
c_static_assert(PAL_SSL_ERROR_NONE == SSL_ERROR_NONE);
c_static_assert(PAL_SSL_ERROR_SSL == SSL_ERROR_SSL);
c_static_assert(PAL_SSL_ERROR_WANT_READ == SSL_ERROR_WANT_READ);
c_static_assert(PAL_SSL_ERROR_WANT_WRITE == SSL_ERROR_WANT_WRITE);
c_static_assert(PAL_SSL_ERROR_SYSCALL == SSL_ERROR_SYSCALL);
c_static_assert(PAL_SSL_ERROR_ZERO_RETURN == SSL_ERROR_ZERO_RETURN);
#define DOTNET_DEFAULT_CIPHERSTRING \
"ECDHE-ECDSA-AES256-GCM-SHA384:" \
"ECDHE-ECDSA-AES128-GCM-SHA256:" \
"ECDHE-RSA-AES256-GCM-SHA384:" \
"ECDHE-RSA-AES128-GCM-SHA256:" \
"ECDHE-ECDSA-AES256-SHA384:" \
"ECDHE-ECDSA-AES128-SHA256:" \
"ECDHE-RSA-AES256-SHA384:" \
"ECDHE-RSA-AES128-SHA256:" \
int32_t CryptoNative_EnsureOpenSslInitialized(void);
#ifdef NEED_OPENSSL_1_0
static void EnsureLibSsl10Initialized()
{
SSL_library_init();
SSL_load_error_strings();
}
#endif
static int32_t g_config_specified_ciphersuites = 0;
static char* g_emptyAlpn = "";
static void DetectCiphersuiteConfiguration()
{
#ifdef FEATURE_DISTRO_AGNOSTIC_SSL
if (API_EXISTS(SSL_state))
{
// For portable builds NEED_OPENSSL_1_1 is always set.
// OpenSSL 1.0 does not support CipherSuites so there is no way for caller to override default
g_config_specified_ciphersuites = 1;
return;
}
#endif
// This routine will always produce g_config_specified_ciphersuites = 1 on OpenSSL 1.0.x,
// so if we're building direct for 1.0.x (the only time NEED_OPENSSL_1_1 is undefined) then
// just omit all the code here.
//
// The method uses OpenSSL 1.0.x API, except for the fallback function SSL_CTX_config, to
// make the portable version easier.
#if defined NEED_OPENSSL_1_1 || defined NEED_OPENSSL_3_0
// Check to see if there's a registered default CipherString. If not, we will use our own.
SSL_CTX* ctx = SSL_CTX_new(TLS_method());
assert(ctx != NULL);
// SSL_get_ciphers returns a shared pointer, no need to save/free it.
// It gets invalidated every time we touch the configuration, so we can't ask just once, either.
SSL* ssl = SSL_new(ctx);
assert(ssl != NULL);
int defaultCount = sk_SSL_CIPHER_num(SSL_get_ciphers(ssl));
SSL_free(ssl);
int rv = SSL_CTX_set_cipher_list(ctx, "ALL");
assert(rv);
ssl = SSL_new(ctx);
assert(ssl != NULL);
int allCount = sk_SSL_CIPHER_num(SSL_get_ciphers(ssl));
SSL_free(ssl);
// It isn't expected that the default list and the "ALL" list have the same cardinality,
// but if that does happen (custom build, config, et cetera) then use the "RSA" list
// instead of the "ALL" list. Since the RSA list doesn't include legacy ciphersuites
// appropriate for ECDSA server certificates, it should be different than the ALL list.
if (allCount == defaultCount)
{
rv = SSL_CTX_set_cipher_list(ctx, "RSA");
assert(rv);
ssl = SSL_new(ctx);
assert(ssl != NULL);
allCount = sk_SSL_CIPHER_num(SSL_get_ciphers(ssl));
SSL_free(ssl);
// If the implicit default, "ALL", and "RSA" all have the same cardinality, just fail.
assert(allCount != defaultCount);
}
if (!SSL_CTX_config(ctx, "system_default"))
{
// There's no system_default configuration, so no default CipherString.
ERR_clear_error();
}
else
{
ssl = SSL_new(ctx);
assert(ssl != NULL);
int after = sk_SSL_CIPHER_num(SSL_get_ciphers(ssl));
SSL_free(ssl);
g_config_specified_ciphersuites = (allCount != after);
}
SSL_CTX_free(ctx);
#else
// OpenSSL 1.0 does not support CipherSuites so there is no way for caller to override default
g_config_specified_ciphersuites = 1;
#endif
}
void CryptoNative_EnsureLibSslInitialized()
{
CryptoNative_EnsureOpenSslInitialized();
// If portable, call the 1.0 initializer when needed.
// If 1.0, call it statically.
// In 1.1 no action is required, since EnsureOpenSslInitialized does both libraries.
#ifdef FEATURE_DISTRO_AGNOSTIC_SSL
if (API_EXISTS(SSL_state))
{
EnsureLibSsl10Initialized();
}
#elif OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_1_1_0_RTM
EnsureLibSsl10Initialized();
#endif
DetectCiphersuiteConfiguration();
}
const SSL_METHOD* CryptoNative_SslV2_3Method()
{
// No error queue impact.
const SSL_METHOD* method = TLS_method();
assert(method != NULL);
return method;
}
SSL_CTX* CryptoNative_SslCtxCreate(const SSL_METHOD* method)
{
ERR_clear_error();
SSL_CTX* ctx = SSL_CTX_new(method);
if (ctx != NULL)
{
// As of OpenSSL 1.1.0, compression is disabled by default. In case an older build
// is used, ensure it's disabled.
//
// The other .NET platforms are server-preference, and the common consensus seems
// to be to use server preference (as of June 2020), so just always assert that.
SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION | SSL_OP_CIPHER_SERVER_PREFERENCE);
#ifdef NEED_OPENSSL_3_0
if (CryptoNative_OpenSslVersionNumber() >= OPENSSL_VERSION_3_0_RTM)
{
// OpenSSL 3.0 forbids client-initiated renegotiation by default. To avoid platform
// differences, we explicitly enable it and handle AllowRenegotiation flag in managed
// code as in previous versions
#ifndef SSL_OP_ALLOW_CLIENT_RENEGOTIATION
#define SSL_OP_ALLOW_CLIENT_RENEGOTIATION ((uint64_t)1 << (uint64_t)8)
#endif
SSL_CTX_set_options(ctx, SSL_OP_ALLOW_CLIENT_RENEGOTIATION);
}
#endif
// If openssl.cnf doesn't have an opinion for CipherString, then use this value instead
if (!g_config_specified_ciphersuites)
{
if (!SSL_CTX_set_cipher_list(ctx, DOTNET_DEFAULT_CIPHERSTRING))
{
SSL_CTX_free(ctx);
return NULL;
}
}
}
return ctx;
}
/*
Openssl supports setting ecdh curves by default from version 1.1.0.
For lower versions, this is the recommended approach.
Returns 1 on success, 0 on failure.
*/
static long TrySetECDHNamedCurve(SSL_CTX* ctx)
{
#ifdef NEED_OPENSSL_1_0
int64_t version = CryptoNative_OpenSslVersionNumber();
long result = 0;
if (version >= OPENSSL_VERSION_1_1_0_RTM)
{
// OpenSSL 1.1+ automatically set up ECDH
result = 1;
}
else if (version >= OPENSSL_VERSION_1_0_2_RTM)
{
#ifndef SSL_CTRL_SET_ECDH_AUTO
#define SSL_CTRL_SET_ECDH_AUTO 94
#endif
// Expanded form of SSL_CTX_set_ecdh_auto(ctx, 1)
result = SSL_CTX_ctrl(ctx, SSL_CTRL_SET_ECDH_AUTO, 1, NULL);
}
else
{
EC_KEY *ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
if (ecdh != NULL)
{
result = SSL_CTX_set_tmp_ecdh(ctx, ecdh);
EC_KEY_free(ecdh);
}
}
return result;
#else
(void)ctx;
return 1;
#endif
}
static void ResetCtxProtocolRestrictions(SSL_CTX* ctx)
{
#ifndef SSL_CTRL_SET_MIN_PROTO_VERSION
#define SSL_CTRL_SET_MIN_PROTO_VERSION 123
#endif
#ifndef SSL_CTRL_SET_MAX_PROTO_VERSION
#define SSL_CTRL_SET_MAX_PROTO_VERSION 124
#endif
SSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION, 0, NULL);
SSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION, 0, NULL);
}
void CryptoNative_SslCtxSetProtocolOptions(SSL_CTX* ctx, SslProtocols protocols)
{
// void shim functions don't lead to exceptions, so skip the unconditional error clearing.
// Ensure that ECDHE is available
if (TrySetECDHNamedCurve(ctx) == 0)
{
ERR_clear_error();
}
// protocols may be 0, meaning system default, in which case let OpenSSL do what OpenSSL wants.
if (protocols == 0)
{
return;
}
unsigned long protocolOptions = 0;
if ((protocols & PAL_SSL_SSL2) != PAL_SSL_SSL2)
{
protocolOptions |= SSL_OP_NO_SSLv2;
}
if ((protocols & PAL_SSL_SSL3) != PAL_SSL_SSL3)
{
protocolOptions |= SSL_OP_NO_SSLv3;
}
if ((protocols & PAL_SSL_TLS) != PAL_SSL_TLS)
{
protocolOptions |= SSL_OP_NO_TLSv1;
}
if ((protocols & PAL_SSL_TLS11) != PAL_SSL_TLS11)
{
protocolOptions |= SSL_OP_NO_TLSv1_1;
}
if ((protocols & PAL_SSL_TLS12) != PAL_SSL_TLS12)
{
protocolOptions |= SSL_OP_NO_TLSv1_2;
}
// protocol options were specified, and there's no handler yet for TLS 1.3.
#ifndef SSL_OP_NO_TLSv1_3
#define SSL_OP_NO_TLSv1_3 0x20000000U
#endif
if ((protocols & PAL_SSL_TLS13) != PAL_SSL_TLS13)
{
protocolOptions |= SSL_OP_NO_TLSv1_3;
}
// We manually set protocols - we need to reset OpenSSL restrictions
// to a maximum possible range
ResetCtxProtocolRestrictions(ctx);
// OpenSSL 1.0 calls this long, OpenSSL 1.1 calls it unsigned long.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wsign-conversion"
SSL_CTX_set_options(ctx, protocolOptions);
#pragma clang diagnostic pop
}
SSL* CryptoNative_SslCreate(SSL_CTX* ctx)
{
ERR_clear_error();
return SSL_new(ctx);
}
int32_t CryptoNative_SslGetError(SSL* ssl, int32_t ret)
{
// The error queue should be cleaned outside, if done here there will be no info
// for managed exception.
return SSL_get_error(ssl, ret);
}
void CryptoNative_SslDestroy(SSL* ssl)
{
if (ssl)
{
SSL_free(ssl);
}
}
void CryptoNative_SslCtxDestroy(SSL_CTX* ctx)
{
if (ctx)
{
SSL_CTX_free(ctx);
}
}
void CryptoNative_SslSetConnectState(SSL* ssl)
{
// void shim functions don't lead to exceptions, so skip the unconditional error clearing.
SSL_set_connect_state(ssl);
}
void CryptoNative_SslSetAcceptState(SSL* ssl)
{
// void shim functions don't lead to exceptions, so skip the unconditional error clearing.
SSL_set_accept_state(ssl);
}
const char* CryptoNative_SslGetVersion(SSL* ssl)
{
// No error queue impact.
return SSL_get_version(ssl);
}
int32_t CryptoNative_SslGetFinished(SSL* ssl, void* buf, int32_t count)
{
// No error queue impact.
size_t result = SSL_get_finished(ssl, buf, (size_t)count);
assert(result <= INT32_MAX);
return (int32_t)result;
}
int32_t CryptoNative_SslGetPeerFinished(SSL* ssl, void* buf, int32_t count)
{
// No error queue impact.
size_t result = SSL_get_peer_finished(ssl, buf, (size_t)count);
assert(result <= INT32_MAX);
return (int32_t)result;
}
int32_t CryptoNative_SslSessionReused(SSL* ssl)
{
// No error queue impact.
return SSL_session_reused(ssl) == 1;
}
int32_t CryptoNative_SslWrite(SSL* ssl, const void* buf, int32_t num, int32_t* error)
{
ERR_clear_error();
int32_t result = SSL_write(ssl, buf, num);
if (result > 0)
{
*error = SSL_ERROR_NONE;
}
else
{
*error = CryptoNative_SslGetError(ssl, result);
}
return result;
}
int32_t CryptoNative_SslRead(SSL* ssl, void* buf, int32_t num, int32_t* error)
{
ERR_clear_error();
int32_t result = SSL_read(ssl, buf, num);
if (result > 0)
{
*error = SSL_ERROR_NONE;
}
else
{
*error = CryptoNative_SslGetError(ssl, result);
}
return result;
}
static int verify_callback(int preverify_ok, X509_STORE_CTX* store)
{
(void)preverify_ok;
(void)store;
// We don't care. Real verification happens in managed code.
return 1;
}
int32_t CryptoNative_SslRenegotiate(SSL* ssl, int32_t* error)
{
ERR_clear_error();
#ifdef NEED_OPENSSL_1_1
// TLS1.3 uses different API for renegotiation/delayed client cert request
#ifndef TLS1_3_VERSION
#define TLS1_3_VERSION 0x0304
#endif
if (SSL_version(ssl) == TLS1_3_VERSION)
{
// this is just a sanity check, if TLS 1.3 was negotiated, then the function must be available
if (API_EXISTS(SSL_verify_client_post_handshake))
{
// Post-handshake auth reqires SSL_VERIFY_PEER to be set
CryptoNative_SslSetVerifyPeer(ssl);
return SSL_verify_client_post_handshake(ssl);
}
else
{
return 0;
}
}
#endif
// The openssl context is destroyed so we can't use ticket or session resumption.
SSL_set_options(ssl, SSL_OP_NO_TICKET | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
int pending = SSL_renegotiate_pending(ssl);
if (!pending)
{
SSL_set_verify(ssl, SSL_VERIFY_PEER, verify_callback);
int ret = SSL_renegotiate(ssl);
if(ret != 1)
{
*error = CryptoNative_SslGetError(ssl, ret);
return ret;
}
return CryptoNative_SslDoHandshake(ssl, error);
}
*error = SSL_ERROR_NONE;
return 0;
}
int32_t CryptoNative_IsSslRenegotiatePending(SSL* ssl)
{
ERR_clear_error();
SSL_peek(ssl, NULL, 0);
return SSL_renegotiate_pending(ssl) != 0;
}
int32_t CryptoNative_SslShutdown(SSL* ssl)
{
ERR_clear_error();
return SSL_shutdown(ssl);
}
void CryptoNative_SslSetBio(SSL* ssl, BIO* rbio, BIO* wbio)
{
// void shim functions don't lead to exceptions, so skip the unconditional error clearing.
SSL_set_bio(ssl, rbio, wbio);
}
int32_t CryptoNative_SslDoHandshake(SSL* ssl, int32_t* error)
{
ERR_clear_error();
int32_t result = SSL_do_handshake(ssl);
if (result == 1)
{
*error = SSL_ERROR_NONE;
}
else
{
*error = CryptoNative_SslGetError(ssl, result);
}
return result;
}
int32_t CryptoNative_IsSslStateOK(SSL* ssl)
{
// No error queue impact.
return SSL_is_init_finished(ssl);
}
X509* CryptoNative_SslGetPeerCertificate(SSL* ssl)
{
// No error queue impact.
return SSL_get1_peer_certificate(ssl);
}
X509Stack* CryptoNative_SslGetPeerCertChain(SSL* ssl)
{
// No error queue impact.
return SSL_get_peer_cert_chain(ssl);
}
int32_t CryptoNative_SslUseCertificate(SSL* ssl, X509* x)
{
ERR_clear_error();
return SSL_use_certificate(ssl, x);
}
int32_t CryptoNative_SslUsePrivateKey(SSL* ssl, EVP_PKEY* pkey)
{
ERR_clear_error();
return SSL_use_PrivateKey(ssl, pkey);
}
int32_t CryptoNative_SslCtxUseCertificate(SSL_CTX* ctx, X509* x)
{
ERR_clear_error();
return SSL_CTX_use_certificate(ctx, x);
}
int32_t CryptoNative_SslCtxUsePrivateKey(SSL_CTX* ctx, EVP_PKEY* pkey)
{
ERR_clear_error();
return SSL_CTX_use_PrivateKey(ctx, pkey);
}
int32_t CryptoNative_SslCtxCheckPrivateKey(SSL_CTX* ctx)
{
ERR_clear_error();
return SSL_CTX_check_private_key(ctx);
}
void CryptoNative_SslCtxSetQuietShutdown(SSL_CTX* ctx)
{
// void shim functions don't lead to exceptions, so skip the unconditional error clearing.
SSL_CTX_set_quiet_shutdown(ctx, 1);
}
void CryptoNative_SslSetQuietShutdown(SSL* ssl, int mode)
{
// void shim functions don't lead to exceptions, so skip the unconditional error clearing.
SSL_set_quiet_shutdown(ssl, mode);
}
X509NameStack* CryptoNative_SslGetClientCAList(SSL* ssl)
{
// No error queue impact.
return SSL_get_client_CA_list(ssl);
}
void CryptoNative_SslSetVerifyPeer(SSL* ssl)
{
// void shim functions don't lead to exceptions, so skip the unconditional error clearing.
SSL_set_verify(ssl, SSL_VERIFY_PEER, verify_callback);
}
void CryptoNative_SslCtxSetCaching(SSL_CTX* ctx, int mode)
{
// void shim functions don't lead to exceptions, so skip the unconditional error clearing.
// We never reuse same CTX for both client and server
SSL_CTX_ctrl(ctx, SSL_CTRL_SET_SESS_CACHE_MODE, mode ? SSL_SESS_CACHE_BOTH : SSL_SESS_CACHE_OFF, NULL);
if (mode == 0)
{
SSL_CTX_set_options(ctx, SSL_OP_NO_TICKET);
}
}
int32_t CryptoNative_SslCtxSetEncryptionPolicy(SSL_CTX* ctx, EncryptionPolicy policy)
{
// No error queue impact.
switch (policy)
{
case AllowNoEncryption:
case NoEncryption:
// No minimum security policy, same as OpenSSL 1.0
SSL_CTX_set_security_level(ctx, 0);
ResetCtxProtocolRestrictions(ctx);
return true;
case RequireEncryption:
return true;
}
return false;
}
int32_t CryptoNative_SslCtxSetCiphers(SSL_CTX* ctx, const char* cipherList, const char* cipherSuites)
{
ERR_clear_error();
int32_t ret = true;
// for < TLS 1.3
if (cipherList != NULL)
{
ret &= SSL_CTX_set_cipher_list(ctx, cipherList);
if (!ret)
{
return ret;
}
}
// for TLS 1.3
#if HAVE_OPENSSL_SET_CIPHERSUITES
if (CryptoNative_Tls13Supported() && cipherSuites != NULL)
{
ret &= SSL_CTX_set_ciphersuites(ctx, cipherSuites);
}
#else
(void)cipherSuites;
#endif
return ret;
}
int32_t CryptoNative_SetCiphers(SSL* ssl, const char* cipherList, const char* cipherSuites)
{
ERR_clear_error();
int32_t ret = true;
// for < TLS 1.3
if (cipherList != NULL)
{
ret &= SSL_set_cipher_list(ssl, cipherList);
if (!ret)
{
return ret;
}
}
// for TLS 1.3
#if HAVE_OPENSSL_SET_CIPHERSUITES
if (CryptoNative_Tls13Supported() && cipherSuites != NULL)
{
ret &= SSL_set_ciphersuites(ssl, cipherSuites);
}
#else
(void)cipherSuites;
#endif
return ret;
}
const char* CryptoNative_GetOpenSslCipherSuiteName(SSL* ssl, int32_t cipherSuite, int32_t* isTls12OrLower)
{
// No error queue impact.
#if HAVE_OPENSSL_SET_CIPHERSUITES
unsigned char cs[2];
const SSL_CIPHER* cipher;
const char* ret;
*isTls12OrLower = 0;
cs[0] = (cipherSuite >> 8) & 0xFF;
cs[1] = cipherSuite & 0xFF;
cipher = SSL_CIPHER_find(ssl, cs);
if (cipher == NULL)
return NULL;
ret = SSL_CIPHER_get_name(cipher);
if (ret == NULL)
return NULL;
// we should get (NONE) only when cipher is NULL
assert(strcmp("(NONE)", ret) != 0);
const char* version = SSL_CIPHER_get_version(cipher);
assert(version != NULL);
assert(strcmp(version, "unknown") != 0);
// same rules apply for DTLS as for TLS so just shortcut
if (version[0] == 'D')
{
version++;
}
// check if tls1.2 or lower
// check most common case first
if (strncmp("TLSv1", version, 5) == 0)
{
const char* tlsver = version + 5;
// true for TLSv1, TLSv1.0, TLSv1.1, TLS1.2, anything else is assumed to be newer
*isTls12OrLower =
tlsver[0] == 0 ||
(tlsver[0] == '.' && tlsver[1] >= '0' && tlsver[1] <= '2' && tlsver[2] == 0);
}
else
{
// if we don't know it assume it is new
// worst case scenario OpenSSL will ignore it
*isTls12OrLower =
strncmp("SSLv", version, 4) == 0;
}
return ret;
#else
(void)ssl;
(void)cipherSuite;
*isTls12OrLower = 0;
return NULL;
#endif
}
int32_t CryptoNative_Tls13Supported()
{
// No error queue impact.
#if HAVE_OPENSSL_SET_CIPHERSUITES
return API_EXISTS(SSL_CTX_set_ciphersuites);
#else
return false;
#endif
}
int32_t CryptoNative_SslCtxAddExtraChainCert(SSL_CTX* ctx, X509* x509)
{
ERR_clear_error();
if (!x509 || !ctx)
{
return 0;
}
if (SSL_CTX_add_extra_chain_cert(ctx, x509) == 1)
{
return 1;
}
return 0;
}
int32_t CryptoNative_SslAddExtraChainCert(SSL* ssl, X509* x509)
{
ERR_clear_error();
if (!x509 || !ssl)
{
return 0;
}
if (SSL_ctrl(ssl, SSL_CTRL_CHAIN_CERT, 1,(void*)x509) == 1)
{
return 1;
}
return 0;
}
int32_t CryptoNative_SslAddClientCAs(SSL* ssl, X509** x509s, uint32_t count)
{
if (!x509s || !ssl)
{
return 0;
}
for (uint32_t i = 0; i < count; i++)
{
int res = SSL_add_client_CA(ssl, x509s[i]);
if (res != 1)
{
return res;
}
}
return 1;
}
void CryptoNative_SslCtxSetAlpnSelectCb(SSL_CTX* ctx, SslCtxSetAlpnCallback cb, void* arg)
{
// void shim functions don't lead to exceptions, so skip the unconditional error clearing.
#if HAVE_OPENSSL_ALPN
if (API_EXISTS(SSL_CTX_set_alpn_select_cb))
{
(void)arg;
SSL_CTX_set_alpn_select_cb(ctx, cb, g_emptyAlpn);
}
#else
(void)ctx;
(void)cb;
(void)arg;
#endif
}
static int client_certificate_cb(SSL *ssl, void* state)
{
(void*)ssl;
(void*)state;
// if we return negative number handshake will pause with SSL_ERROR_WANT_X509_LOOKUP
return -1;
}
void CryptoNative_SslSetClientCertCallback(SSL* ssl, int set)
{
// void shim functions don't lead to exceptions, so skip the unconditional error clearing.
SSL_set_cert_cb(ssl, set ? client_certificate_cb : NULL, NULL);
}
void CryptoNative_SslSetPostHandshakeAuth(SSL* ssl, int32_t val)
{
#ifdef NEED_OPENSSL_1_1
if (API_EXISTS(SSL_set_post_handshake_auth))
{
SSL_set_post_handshake_auth(ssl, val);
}
#else
(void)ssl;
(void)val;
#endif
}
int32_t CryptoNative_SslSetData(SSL* ssl, void *ptr)
{
ERR_clear_error();
return SSL_set_ex_data(ssl, 0, ptr);
}
void* CryptoNative_SslGetData(SSL* ssl)
{
// No error queue impact.
return SSL_get_ex_data(ssl, 0);
}
int32_t CryptoNative_SslSetAlpnProtos(SSL* ssl, const uint8_t* protos, uint32_t protos_len)
{
ERR_clear_error();
#if HAVE_OPENSSL_ALPN
if (API_EXISTS(SSL_CTX_set_alpn_protos))
{
return SSL_set_alpn_protos(ssl, protos, protos_len);
}
else
#else
(void)ctx;
(void)protos;
(void)protos_len;
#endif
{
return 0;
}
}
void CryptoNative_SslGet0AlpnSelected(SSL* ssl, const uint8_t** protocol, uint32_t* len)
{
// void shim functions don't lead to exceptions, so skip the unconditional error clearing.
#if HAVE_OPENSSL_ALPN
if (API_EXISTS(SSL_get0_alpn_selected))
{
SSL_get0_alpn_selected(ssl, protocol, len);
}
else
#else
(void)ssl;
#endif
{
*protocol = NULL;
*len = 0;
}
}
int32_t CryptoNative_SslSetTlsExtHostName(SSL* ssl, uint8_t* name)
{
ERR_clear_error();
return (int32_t)SSL_set_tlsext_host_name(ssl, name);
}
int32_t CryptoNative_SslGetCurrentCipherId(SSL* ssl, int32_t* cipherId)
{
// No error queue impact.
const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl);
if (!cipher)
{
*cipherId = -1;
return 0;
}
// OpenSSL uses its own identifier
// lower 2 bytes of that ID contain IANA value
*cipherId = SSL_CIPHER_get_id(cipher) & 0xFFFF;
return 1;
}
// This function generates key pair and creates simple certificate.
static int MakeSelfSignedCertificate(X509 * cert, EVP_PKEY* evp)
{
RSA* rsa = NULL;
ASN1_TIME* time = ASN1_TIME_new();
X509_NAME * asnName;
unsigned char * name = (unsigned char*)"localhost";
int ret = 0;
EVP_PKEY* pkey = CryptoNative_RsaGenerateKey(2048);
if (pkey != NULL)
{
rsa = EVP_PKEY_get1_RSA(pkey);
EVP_PKEY_free(pkey);
}
if (rsa != NULL)
{
if (EVP_PKEY_set1_RSA(evp, rsa) == 1)
{
rsa = NULL;
}
X509_set_pubkey(cert, evp);
asnName = X509_get_subject_name(cert);
X509_NAME_add_entry_by_txt(asnName, "CN", MBSTRING_ASC, name, -1, -1, 0);
asnName = X509_get_issuer_name(cert);
X509_NAME_add_entry_by_txt(asnName, "CN", MBSTRING_ASC, name, -1, -1, 0);
ASN1_TIME_set(time, 0);
X509_set1_notBefore(cert, time);
X509_set1_notAfter(cert, time);
ret = X509_sign(cert, evp, EVP_sha256());
}
if (rsa != NULL)
{
RSA_free(rsa);
}
if (time != NULL)
{
ASN1_TIME_free(time);
}
return ret;
}
int32_t CryptoNative_OpenSslGetProtocolSupport(SslProtocols protocol)
{
// Many of these helpers already clear the error queue, and we unconditionally
// clear it at the end.
int ret = 0;
SSL_CTX* clientCtx = CryptoNative_SslCtxCreate(TLS_method());
SSL_CTX* serverCtx = CryptoNative_SslCtxCreate(TLS_method());
X509 * cert = X509_new();
EVP_PKEY* evp = CryptoNative_EvpPkeyCreate();
BIO *bio1 = BIO_new(BIO_s_mem());
BIO *bio2 = BIO_new(BIO_s_mem());
SSL* client = NULL;
SSL* server = NULL;
if (clientCtx != NULL && serverCtx != NULL && cert != NULL && evp != NULL && bio1 != NULL && bio2 != NULL)
{
CryptoNative_SslCtxSetProtocolOptions(serverCtx, protocol);
CryptoNative_SslCtxSetProtocolOptions(clientCtx, protocol);
SSL_CTX_set_verify(clientCtx, SSL_VERIFY_NONE, NULL);
SSL_CTX_set_verify(serverCtx, SSL_VERIFY_NONE, NULL);
if (MakeSelfSignedCertificate(cert, evp))
{
CryptoNative_SslCtxUseCertificate(serverCtx, cert);
CryptoNative_SslCtxUsePrivateKey(serverCtx, evp);
server = CryptoNative_SslCreate(serverCtx);
SSL_set_accept_state(server);
client = CryptoNative_SslCreate(clientCtx);
SSL_set_connect_state(client);