/
SecureChannel.cs
1285 lines (1097 loc) · 51.8 KB
/
SecureChannel.cs
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.
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Security;
using System.Security.Authentication;
using System.Security.Authentication.ExtendedProtection;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace System.Net.Security
{
// SecureChannel - a wrapper on SSPI based functionality.
// Provides an additional abstraction layer over SSPI for SslStream.
internal class SecureChannel
{
// When reading a frame from the wire first read this many bytes for the header.
internal const int ReadHeaderSize = 5;
private SafeFreeCredentials? _credentialsHandle;
private SafeDeleteSslContext? _securityContext;
private SslConnectionInfo? _connectionInfo;
private X509Certificate? _selectedClientCertificate;
private X509Certificate2? _remoteCertificate;
private bool _remoteCertificateExposed;
// These are the MAX encrypt buffer output sizes, not the actual sizes.
private int _headerSize = 5; //ATTN must be set to at least 5 by default
private int _trailerSize = 16;
private int _maxDataSize = 16354;
private bool _refreshCredentialNeeded;
private readonly SslAuthenticationOptions _sslAuthenticationOptions;
private SslApplicationProtocol _negotiatedApplicationProtocol;
private static readonly Oid s_serverAuthOid = new Oid("1.3.6.1.5.5.7.3.1", "1.3.6.1.5.5.7.3.1");
private static readonly Oid s_clientAuthOid = new Oid("1.3.6.1.5.5.7.3.2", "1.3.6.1.5.5.7.3.2");
private SslStream? _ssl;
internal SecureChannel(SslAuthenticationOptions sslAuthenticationOptions, SslStream sslStream)
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Log.SecureChannelCtor(this, sslStream, sslAuthenticationOptions.TargetHost!, sslAuthenticationOptions.ClientCertificates, sslAuthenticationOptions.EncryptionPolicy);
SslStreamPal.VerifyPackageInfo();
if (sslAuthenticationOptions.TargetHost == null)
{
NetEventSource.Fail(this, "sslAuthenticationOptions.TargetHost == null");
}
_securityContext = null;
_refreshCredentialNeeded = true;
_sslAuthenticationOptions = sslAuthenticationOptions;
_ssl = sslStream;
}
//
// SecureChannel properties
//
// LocalServerCertificate - local certificate for server mode channel
// LocalClientCertificate - selected certificated used in the client channel mode otherwise null
// IsRemoteCertificateAvailable - true if the remote side has provided a certificate
// HeaderSize - Header & trailer sizes used in the TLS stream
// TrailerSize -
//
internal X509Certificate? LocalServerCertificate
{
get
{
return _sslAuthenticationOptions.CertificateContext?.Certificate;
}
}
internal X509Certificate? LocalClientCertificate
{
get
{
return _selectedClientCertificate;
}
}
internal bool IsRemoteCertificateAvailable
{
get
{
return _remoteCertificate != null;
}
}
internal X509Certificate? RemoteCertificate
{
get
{
_remoteCertificateExposed = true;
return _remoteCertificate;
}
}
internal ChannelBinding? GetChannelBinding(ChannelBindingKind kind)
{
ChannelBinding? result = null;
if (_securityContext != null)
{
result = SslStreamPal.QueryContextChannelBinding(_securityContext, kind);
}
return result;
}
internal X509RevocationMode CheckCertRevocationStatus
{
get
{
return _sslAuthenticationOptions.CertificateRevocationCheckMode;
}
}
internal int MaxDataSize
{
get
{
return _maxDataSize;
}
}
internal SslConnectionInfo? ConnectionInfo
{
get
{
return _connectionInfo;
}
}
internal bool IsValidContext
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return !(_securityContext == null || _securityContext.IsInvalid);
}
}
internal bool IsServer
{
get
{
return _sslAuthenticationOptions.IsServer;
}
}
internal bool RemoteCertRequired
{
get
{
return _sslAuthenticationOptions.RemoteCertRequired;
}
}
internal SslApplicationProtocol NegotiatedApplicationProtocol
{
get
{
return _negotiatedApplicationProtocol;
}
}
internal void SetRefreshCredentialNeeded()
{
_refreshCredentialNeeded = true;
}
internal void Close()
{
if (!_remoteCertificateExposed)
{
_remoteCertificate?.Dispose();
_remoteCertificate = null;
}
_securityContext?.Dispose();
_credentialsHandle?.Dispose();
_ssl = null;
GC.SuppressFinalize(this);
}
//
// SECURITY: we open a private key container on behalf of the caller
// and we require the caller to have permission associated with that operation.
//
internal static X509Certificate2? FindCertificateWithPrivateKey(object instance, bool isServer, X509Certificate certificate)
{
if (certificate == null)
{
return null;
}
if (NetEventSource.Log.IsEnabled())
NetEventSource.Log.LocatingPrivateKey(certificate, instance);
try
{
// Protecting from X509Certificate2 derived classes.
X509Certificate2? certEx = MakeEx(certificate);
if (certEx != null)
{
if (certEx.HasPrivateKey)
{
if (NetEventSource.Log.IsEnabled())
NetEventSource.Log.CertIsType2(instance);
return certEx;
}
if (!object.ReferenceEquals(certificate, certEx))
{
certEx.Dispose();
}
}
X509Certificate2Collection collectionEx;
string certHash = certEx!.Thumbprint;
// ELSE Try the MY user and machine stores for private key check.
// For server side mode MY machine store takes priority.
X509Store? store = CertificateValidationPal.EnsureStoreOpened(isServer);
if (store != null)
{
collectionEx = store.Certificates.Find(X509FindType.FindByThumbprint, certHash, false);
if (collectionEx.Count > 0 && collectionEx[0].HasPrivateKey)
{
if (NetEventSource.Log.IsEnabled())
NetEventSource.Log.FoundCertInStore(isServer, instance);
return collectionEx[0];
}
}
store = CertificateValidationPal.EnsureStoreOpened(!isServer);
if (store != null)
{
collectionEx = store.Certificates.Find(X509FindType.FindByThumbprint, certHash, false);
if (collectionEx.Count > 0 && collectionEx[0].HasPrivateKey)
{
if (NetEventSource.Log.IsEnabled())
NetEventSource.Log.FoundCertInStore(!isServer, instance);
return collectionEx[0];
}
}
}
catch (CryptographicException)
{
}
if (NetEventSource.Log.IsEnabled())
NetEventSource.Log.NotFoundCertInStore(instance);
return null;
}
private static X509Certificate2? MakeEx(X509Certificate certificate)
{
Debug.Assert(certificate != null, "certificate != null");
if (certificate.GetType() == typeof(X509Certificate2))
{
return (X509Certificate2)certificate;
}
X509Certificate2? certificateEx = null;
try
{
if (certificate.Handle != IntPtr.Zero)
{
certificateEx = new X509Certificate2(certificate);
}
}
catch (SecurityException) { }
catch (CryptographicException) { }
return certificateEx;
}
//
// Get certificate_authorities list, according to RFC 5246, Section 7.4.4.
// Used only by client SSL code, never returns null.
//
private string[] GetRequestCertificateAuthorities()
{
string[] issuers = Array.Empty<string>();
if (IsValidContext)
{
issuers = CertificateValidationPal.GetRequestCertificateAuthorities(_securityContext!);
}
return issuers;
}
/*++
AcquireCredentials - Attempts to find Client Credential
Information, that can be sent to the server. In our case,
this is only Client Certificates, that we have Credential Info.
How it works:
case 0: Cert Selection delegate is present
Always use its result as the client cert answer.
Try to use cached credential handle whenever feasible.
Do not use cached anonymous creds if the delegate has returned null
and the collection is not empty (allow responding with the cert later).
case 1: Certs collection is empty
Always use the same statically acquired anonymous SSL Credential
case 2: Before our Connection with the Server
If we have a cached credential handle keyed by first X509Certificate
**content** in the passed collection, then we use that cached
credential and hoping to restart a session.
Otherwise create a new anonymous (allow responding with the cert later).
case 3: After our Connection with the Server (i.e. during handshake or re-handshake)
The server has requested that we send it a Certificate then
we Enumerate a list of server sent Issuers trying to match against
our list of Certificates, the first match is sent to the server.
Once we got a cert we again try to match cached credential handle if possible.
This will not restart a session but helps minimizing the number of handles we create.
In the case of an error getting a Certificate or checking its private Key we fall back
to the behavior of having no certs, case 1.
Returns: True if cached creds were used, false otherwise.
--*/
private bool AcquireClientCredentials(ref byte[]? thumbPrint)
{
// Acquire possible Client Certificate information and set it on the handle.
X509Certificate? clientCertificate = null; // This is a candidate that can come from the user callback or be guessed when targeting a session restart.
List<X509Certificate>? filteredCerts = null; // This is an intermediate client certs collection that try to use if no selectedCert is available yet.
string[] issuers; // This is a list of issuers sent by the server, only valid is we do know what the server cert is.
bool sessionRestartAttempt = false; // If true and no cached creds we will use anonymous creds.
if (_sslAuthenticationOptions.CertSelectionDelegate != null)
{
issuers = GetRequestCertificateAuthorities();
if (NetEventSource.Log.IsEnabled())
NetEventSource.Info(this, "Calling CertificateSelectionCallback");
X509Certificate2? remoteCert = null;
try
{
remoteCert = CertificateValidationPal.GetRemoteCertificate(_securityContext!);
if (_sslAuthenticationOptions.ClientCertificates == null)
{
_sslAuthenticationOptions.ClientCertificates = new X509CertificateCollection();
}
clientCertificate = _sslAuthenticationOptions.CertSelectionDelegate(_sslAuthenticationOptions.TargetHost!, _sslAuthenticationOptions.ClientCertificates, remoteCert, issuers);
}
finally
{
remoteCert?.Dispose();
}
if (clientCertificate != null)
{
if (_credentialsHandle == null)
{
sessionRestartAttempt = true;
}
EnsureInitialized(ref filteredCerts).Add(clientCertificate);
if (NetEventSource.Log.IsEnabled())
NetEventSource.Log.CertificateFromDelegate(this);
}
else
{
if (_sslAuthenticationOptions.ClientCertificates == null || _sslAuthenticationOptions.ClientCertificates.Count == 0)
{
if (NetEventSource.Log.IsEnabled())
NetEventSource.Log.NoDelegateNoClientCert(this);
sessionRestartAttempt = true;
}
else
{
if (NetEventSource.Log.IsEnabled())
NetEventSource.Log.NoDelegateButClientCert(this);
}
}
}
else if (_credentialsHandle == null && _sslAuthenticationOptions.ClientCertificates != null && _sslAuthenticationOptions.ClientCertificates.Count > 0)
{
// This is where we attempt to restart a session by picking the FIRST cert from the collection.
// Otherwise it is either server sending a client cert request or the session is renegotiated.
clientCertificate = _sslAuthenticationOptions.ClientCertificates[0];
sessionRestartAttempt = true;
if (clientCertificate != null)
{
EnsureInitialized(ref filteredCerts).Add(clientCertificate);
}
if (NetEventSource.Log.IsEnabled())
NetEventSource.Log.AttemptingRestartUsingCert(clientCertificate, this);
}
else if (_sslAuthenticationOptions.ClientCertificates != null && _sslAuthenticationOptions.ClientCertificates.Count > 0)
{
//
// This should be a server request for the client cert sent over currently anonymous sessions.
//
issuers = GetRequestCertificateAuthorities();
if (NetEventSource.Log.IsEnabled())
{
if (issuers == null || issuers.Length == 0)
{
NetEventSource.Log.NoIssuersTryAllCerts(this);
}
else
{
NetEventSource.Log.LookForMatchingCerts(issuers.Length, this);
}
}
for (int i = 0; i < _sslAuthenticationOptions.ClientCertificates.Count; ++i)
{
//
// Make sure we add only if the cert matches one of the issuers.
// If no issuers were sent and then try all client certs starting with the first one.
//
if (issuers != null && issuers.Length != 0)
{
X509Certificate2? certificateEx = null;
X509Chain? chain = null;
try
{
certificateEx = MakeEx(_sslAuthenticationOptions.ClientCertificates[i]);
if (certificateEx == null)
{
continue;
}
if (NetEventSource.Log.IsEnabled())
NetEventSource.Info(this, $"Root cert: {certificateEx}");
chain = new X509Chain();
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.IgnoreInvalidName;
chain.Build(certificateEx);
bool found = false;
//
// We ignore any errors happened with chain.
//
if (chain.ChainElements.Count > 0)
{
int elementsCount = chain.ChainElements.Count;
for (int ii = 0; ii < elementsCount; ++ii)
{
string issuer = chain.ChainElements[ii].Certificate!.Issuer;
found = Array.IndexOf(issuers, issuer) != -1;
if (found)
{
if (NetEventSource.Log.IsEnabled())
NetEventSource.Info(this, $"Matched {issuer}");
break;
}
if (NetEventSource.Log.IsEnabled())
NetEventSource.Info(this, $"No match: {issuer}");
}
}
if (!found)
{
continue;
}
}
finally
{
if (chain != null)
{
chain.Dispose();
int elementsCount = chain.ChainElements.Count;
for (int element = 0; element < elementsCount; element++)
{
chain.ChainElements[element].Certificate!.Dispose();
}
}
if (certificateEx != null && (object)certificateEx != (object)_sslAuthenticationOptions.ClientCertificates[i])
{
certificateEx.Dispose();
}
}
}
if (NetEventSource.Log.IsEnabled())
NetEventSource.Log.SelectedCert(_sslAuthenticationOptions.ClientCertificates[i], this);
EnsureInitialized(ref filteredCerts).Add(_sslAuthenticationOptions.ClientCertificates[i]);
}
}
bool cachedCred = false; // This is a return result from this method.
X509Certificate2? selectedCert = null; // This is a final selected cert (ensured that it does have private key with it).
clientCertificate = null;
if (NetEventSource.Log.IsEnabled())
{
if (filteredCerts != null && filteredCerts.Count != 0)
{
NetEventSource.Log.CertsAfterFiltering(filteredCerts.Count, this);
NetEventSource.Log.FindingMatchingCerts(this);
}
else
{
NetEventSource.Log.CertsAfterFiltering(0, this);
NetEventSource.Info(this, "No client certificate to choose from");
}
}
//
// ATTN: When the client cert was returned by the user callback OR it was guessed AND it has no private key,
// THEN anonymous (no client cert) credential will be used.
//
// SECURITY: Accessing X509 cert Credential is disabled for semitrust.
// We no longer need to demand for unmanaged code permissions.
// FindCertificateWithPrivateKey should do the right demand for us.
if (filteredCerts != null)
{
for (int i = 0; i < filteredCerts.Count; ++i)
{
clientCertificate = filteredCerts[i];
if ((selectedCert = FindCertificateWithPrivateKey(this, _sslAuthenticationOptions.IsServer, clientCertificate)) != null)
{
break;
}
clientCertificate = null;
selectedCert = null;
}
}
if ((object?)clientCertificate != (object?)selectedCert && !clientCertificate!.Equals(selectedCert))
{
NetEventSource.Fail(this, "'selectedCert' does not match 'clientCertificate'.");
}
if (NetEventSource.Log.IsEnabled())
NetEventSource.Info(this, $"Selected cert = {selectedCert}");
try
{
// Try to locate cached creds first.
//
// SECURITY: selectedCert ref if not null is a safe object that does not depend on possible **user** inherited X509Certificate type.
//
byte[]? guessedThumbPrint = selectedCert?.GetCertHash();
SafeFreeCredentials? cachedCredentialHandle = SslSessionsCache.TryCachedCredential(guessedThumbPrint, _sslAuthenticationOptions.EnabledSslProtocols, _sslAuthenticationOptions.IsServer, _sslAuthenticationOptions.EncryptionPolicy);
// We can probably do some optimization here. If the selectedCert is returned by the delegate
// we can always go ahead and use the certificate to create our credential
// (instead of going anonymous as we do here).
if (sessionRestartAttempt &&
cachedCredentialHandle == null &&
selectedCert != null &&
SslStreamPal.StartMutualAuthAsAnonymous)
{
if (NetEventSource.Log.IsEnabled())
NetEventSource.Info(this, "Reset to anonymous session.");
// IIS does not renegotiate a restarted session if client cert is needed.
// So we don't want to reuse **anonymous** cached credential for a new SSL connection if the client has passed some certificate.
// The following block happens if client did specify a certificate but no cached creds were found in the cache.
// Since we don't restart a session the server side can still challenge for a client cert.
if ((object?)clientCertificate != (object?)selectedCert)
{
selectedCert.Dispose();
}
guessedThumbPrint = null;
selectedCert = null;
clientCertificate = null;
}
if (cachedCredentialHandle != null)
{
if (NetEventSource.Log.IsEnabled())
NetEventSource.Log.UsingCachedCredential(this);
_credentialsHandle = cachedCredentialHandle;
_selectedClientCertificate = clientCertificate;
cachedCred = true;
if (selectedCert != null)
{
_sslAuthenticationOptions.CertificateContext = SslStreamCertificateContext.Create(selectedCert!);
}
}
else
{
if (selectedCert != null)
{
_sslAuthenticationOptions.CertificateContext = SslStreamCertificateContext.Create(selectedCert!);
}
_credentialsHandle = SslStreamPal.AcquireCredentialsHandle(_sslAuthenticationOptions.CertificateContext,
_sslAuthenticationOptions.EnabledSslProtocols, _sslAuthenticationOptions.EncryptionPolicy, _sslAuthenticationOptions.IsServer);
thumbPrint = guessedThumbPrint; // Delay until here in case something above threw.
_selectedClientCertificate = clientCertificate;
}
}
finally
{
if (selectedCert != null && _sslAuthenticationOptions.CertificateContext != null)
{
_sslAuthenticationOptions.CertificateContext = SslStreamCertificateContext.Create(selectedCert);
}
}
return cachedCred;
}
private static List<T> EnsureInitialized<T>(ref List<T>? list) => list ?? (list = new List<T>());
//
// Acquire Server Side Certificate information and set it on the class.
//
private bool AcquireServerCredentials(ref byte[]? thumbPrint)
{
X509Certificate? localCertificate = null;
X509Certificate2? selectedCert = null;
bool cachedCred = false;
// There are three options for selecting the server certificate. When
// selecting which to use, we prioritize the new ServerCertSelectionDelegate
// API. If the new API isn't used we call LocalCertSelectionCallback (for compat
// with .NET Framework), and if neither is set we fall back to using CertificateContext.
if (_sslAuthenticationOptions.ServerCertSelectionDelegate != null)
{
localCertificate = _sslAuthenticationOptions.ServerCertSelectionDelegate(_sslAuthenticationOptions.TargetHost);
if (localCertificate == null)
{
if (NetEventSource.Log.IsEnabled())
NetEventSource.Error(this, $"ServerCertSelectionDelegate returned no certificaete for '{_sslAuthenticationOptions.TargetHost}'.");
throw new AuthenticationException(SR.net_ssl_io_no_server_cert);
}
if (NetEventSource.Log.IsEnabled())
NetEventSource.Info(this, "ServerCertSelectionDelegate selected Cert");
}
else if (_sslAuthenticationOptions.CertSelectionDelegate != null)
{
X509CertificateCollection tempCollection = new X509CertificateCollection();
tempCollection.Add(_sslAuthenticationOptions.CertificateContext!.Certificate!);
// We pass string.Empty here to maintain strict compatability with .NET Framework.
localCertificate = _sslAuthenticationOptions.CertSelectionDelegate(string.Empty, tempCollection, null, Array.Empty<string>());
if (localCertificate == null)
{
if (NetEventSource.Log.IsEnabled())
NetEventSource.Error(this, $"CertSelectionDelegate returned no certificaete for '{_sslAuthenticationOptions.TargetHost}'.");
throw new NotSupportedException(SR.net_ssl_io_no_server_cert);
}
if (NetEventSource.Log.IsEnabled())
NetEventSource.Info(this, "CertSelectionDelegate selected Cert");
}
else if (_sslAuthenticationOptions.CertificateContext != null)
{
selectedCert = _sslAuthenticationOptions.CertificateContext.Certificate;
}
if (selectedCert == null)
{
// We will get here if vertificate was slected via legacy callback using X509Certificate
// Fail immediately if no certificate was given.
if (localCertificate == null)
{
if (NetEventSource.Log.IsEnabled())
NetEventSource.Error(this, "Certiticate callback returned no certificaete.");
throw new NotSupportedException(SR.net_ssl_io_no_server_cert);
}
// SECURITY: Accessing X509 cert Credential is disabled for semitrust.
// We no longer need to demand for unmanaged code permissions.
// EnsurePrivateKey should do the right demand for us.
selectedCert = FindCertificateWithPrivateKey(this, _sslAuthenticationOptions.IsServer, localCertificate);
if (selectedCert == null)
{
throw new NotSupportedException(SR.net_ssl_io_no_server_cert);
}
if (!localCertificate.Equals(selectedCert))
{
NetEventSource.Fail(this, "'selectedCert' does not match 'localCertificate'.");
}
_sslAuthenticationOptions.CertificateContext = SslStreamCertificateContext.Create(selectedCert);
}
//
// Note selectedCert is a safe ref possibly cloned from the user passed Cert object
//
byte[] guessedThumbPrint = selectedCert.GetCertHash();
SafeFreeCredentials? cachedCredentialHandle = SslSessionsCache.TryCachedCredential(guessedThumbPrint, _sslAuthenticationOptions.EnabledSslProtocols, _sslAuthenticationOptions.IsServer, _sslAuthenticationOptions.EncryptionPolicy);
if (cachedCredentialHandle != null)
{
_credentialsHandle = cachedCredentialHandle;
cachedCred = true;
}
else
{
_credentialsHandle = SslStreamPal.AcquireCredentialsHandle(_sslAuthenticationOptions.CertificateContext, _sslAuthenticationOptions.EnabledSslProtocols,
_sslAuthenticationOptions.EncryptionPolicy, _sslAuthenticationOptions.IsServer);
thumbPrint = guessedThumbPrint;
}
return cachedCred;
}
//
internal ProtocolToken NextMessage(ReadOnlySpan<byte> incomingBuffer)
{
byte[]? nextmsg = null;
SecurityStatusPal status = GenerateToken(incomingBuffer, ref nextmsg);
if (!_sslAuthenticationOptions.IsServer && status.ErrorCode == SecurityStatusPalErrorCode.CredentialsNeeded)
{
if (NetEventSource.Log.IsEnabled())
NetEventSource.Info(this, "NextMessage() returned SecurityStatusPal.CredentialsNeeded");
SetRefreshCredentialNeeded();
status = GenerateToken(incomingBuffer, ref nextmsg);
}
ProtocolToken token = new ProtocolToken(nextmsg, status);
if (NetEventSource.Log.IsEnabled())
{
if (token.Failed)
{
NetEventSource.Error(this, $"Authentication failed. Status: {status.ToString()}, Exception message: {token.GetException()!.Message}");
}
}
return token;
}
/*++
GenerateToken - Called after each successive state
in the Client - Server handshake. This function
generates a set of bytes that will be sent next to
the server. The server responds, each response,
is pass then into this function, again, and the cycle
repeats until successful connection, or failure.
Input:
input - bytes from the wire
output - ref to byte [], what we will send to the
server in response
Return:
status - error information
--*/
private SecurityStatusPal GenerateToken(ReadOnlySpan<byte> inputBuffer, ref byte[]? output)
{
byte[]? result = Array.Empty<byte>();
SecurityStatusPal status = default;
bool cachedCreds = false;
byte[]? thumbPrint = null;
//
// Looping through ASC or ISC with potentially cached credential that could have been
// already disposed from a different thread before ISC or ASC dir increment a cred ref count.
//
try
{
do
{
thumbPrint = null;
if (_refreshCredentialNeeded)
{
cachedCreds = _sslAuthenticationOptions.IsServer
? AcquireServerCredentials(ref thumbPrint)
: AcquireClientCredentials(ref thumbPrint);
}
if (_sslAuthenticationOptions.IsServer)
{
status = SslStreamPal.AcceptSecurityContext(
ref _credentialsHandle!,
ref _securityContext,
inputBuffer,
ref result,
_sslAuthenticationOptions);
}
else
{
status = SslStreamPal.InitializeSecurityContext(
ref _credentialsHandle!,
ref _securityContext,
_sslAuthenticationOptions.TargetHost,
inputBuffer,
ref result,
_sslAuthenticationOptions);
}
} while (cachedCreds && _credentialsHandle == null);
}
finally
{
if (_refreshCredentialNeeded)
{
_refreshCredentialNeeded = false;
//
// Assuming the ISC or ASC has referenced the credential,
// we want to call dispose so to decrement the effective ref count.
//
_credentialsHandle?.Dispose();
//
// This call may bump up the credential reference count further.
// Note that thumbPrint is retrieved from a safe cert object that was possible cloned from the user passed cert.
//
if (!cachedCreds && _securityContext != null && !_securityContext.IsInvalid && _credentialsHandle != null && !_credentialsHandle.IsInvalid)
{
SslSessionsCache.CacheCredential(_credentialsHandle, thumbPrint, _sslAuthenticationOptions.EnabledSslProtocols, _sslAuthenticationOptions.IsServer, _sslAuthenticationOptions.EncryptionPolicy);
}
}
}
output = result;
return status;
}
/*++
ProcessHandshakeSuccess -
Called on successful completion of Handshake -
used to set header/trailer sizes for encryption use
Fills in the information about established protocol
--*/
internal void ProcessHandshakeSuccess()
{
if (_negotiatedApplicationProtocol == default)
{
// try to get ALPN info unless we already have it. (renegotiation)
byte[]? alpnResult = SslStreamPal.GetNegotiatedApplicationProtocol(_securityContext!);
_negotiatedApplicationProtocol = alpnResult == null ? default : new SslApplicationProtocol(alpnResult, false);
}
SslStreamPal.QueryContextStreamSizes(_securityContext!, out StreamSizes streamSizes);
try
{
_headerSize = streamSizes.Header;
_trailerSize = streamSizes.Trailer;
_maxDataSize = checked(streamSizes.MaximumMessage - (_headerSize + _trailerSize));
Debug.Assert(_maxDataSize > 0, "_maxDataSize > 0");
}
catch (Exception e) when (!ExceptionCheck.IsFatal(e))
{
NetEventSource.Fail(this, "StreamSizes out of range.");
throw;
}
SslStreamPal.QueryContextConnectionInfo(_securityContext!, out _connectionInfo);
}
/*++
Encrypt - Encrypts our bytes before we send them over the wire
PERF: make more efficient, this does an extra copy when the offset
is non-zero.
Input:
buffer - bytes for sending
offset -
size -
output - Encrypted bytes
--*/
internal SecurityStatusPal Encrypt(ReadOnlyMemory<byte> buffer, ref byte[] output, out int resultSize)
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.DumpBuffer(this, buffer);
byte[] writeBuffer = output;
SecurityStatusPal secStatus = SslStreamPal.EncryptMessage(
_securityContext!,
buffer,
_headerSize,
_trailerSize,
ref writeBuffer,
out resultSize);
if (secStatus.ErrorCode != SecurityStatusPalErrorCode.OK)
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, $"ERROR {secStatus}");
}
else
{
output = writeBuffer;
}
return secStatus;
}
internal SecurityStatusPal Decrypt(byte[]? payload, ref int offset, ref int count)
{
if ((uint)offset > (uint)(payload == null ? 0 : payload.Length))
{
NetEventSource.Fail(this, "Argument 'offset' out of range.");
throw new ArgumentOutOfRangeException(nameof(offset));
}
if ((uint)count > (uint)(payload == null ? 0 : payload.Length - offset))
{
NetEventSource.Fail(this, "Argument 'count' out of range.");
throw new ArgumentOutOfRangeException(nameof(count));
}
return SslStreamPal.DecryptMessage(_securityContext!, payload!, ref offset, ref count);
}
/*++
VerifyRemoteCertificate - Validates the content of a Remote Certificate
checkCRL if true, checks the certificate revocation list for validity.
checkCertName, if true checks the CN field of the certificate
--*/
//This method validates a remote certificate.
internal bool VerifyRemoteCertificate(RemoteCertificateValidationCallback? remoteCertValidationCallback, ref ProtocolToken? alertToken, out SslPolicyErrors sslPolicyErrors, out X509ChainStatusFlags chainStatus)
{
sslPolicyErrors = SslPolicyErrors.None;
chainStatus = X509ChainStatusFlags.NoError;
// We don't catch exceptions in this method, so it's safe for "accepted" be initialized with true.
bool success = false;
X509Chain? chain = null;
X509Certificate2Collection? remoteCertificateStore = null;
try
{
X509Certificate2? certificate = CertificateValidationPal.GetRemoteCertificate(_securityContext, out remoteCertificateStore);
if (_remoteCertificate != null && certificate != null &&
certificate.RawData.AsSpan().SequenceEqual(_remoteCertificate.RawData))
{
// This is renegotiation or TLS 1.3 and the certificate did not change.
// There is no reason to process callback again as we already established trust.
return true;
}
_remoteCertificate = certificate;
if (_remoteCertificate == null)
{
if (NetEventSource.Log.IsEnabled() && RemoteCertRequired) NetEventSource.Error(this, $"Remote certificate required, but no remote certificate received");
sslPolicyErrors |= SslPolicyErrors.RemoteCertificateNotAvailable;
}
else
{
chain = new X509Chain();
chain.ChainPolicy.RevocationMode = _sslAuthenticationOptions.CertificateRevocationCheckMode;
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
// Authenticate the remote party: (e.g. when operating in server mode, authenticate the client).
chain.ChainPolicy.ApplicationPolicy.Add(_sslAuthenticationOptions.IsServer ? s_clientAuthOid : s_serverAuthOid);
if (remoteCertificateStore != null)
{
chain.ChainPolicy.ExtraStore.AddRange(remoteCertificateStore);
}
sslPolicyErrors |= CertificateValidationPal.VerifyCertificateProperties(
_securityContext!,
chain,
_remoteCertificate,
_sslAuthenticationOptions.CheckCertName,
_sslAuthenticationOptions.IsServer,
_sslAuthenticationOptions.TargetHost);
}
if (remoteCertValidationCallback != null)
{
object? sender = _ssl;
if (sender == null)
{
throw new ObjectDisposedException(nameof(SslStream));