@@ -10,14 +10,39 @@ namespace System.Net.Security
1010{
1111 internal sealed class SslAuthenticationOptions
1212 {
13- internal SslAuthenticationOptions ( SslClientAuthenticationOptions sslClientAuthenticationOptions , RemoteCertificateValidationCallback ? remoteCallback , LocalCertSelectionCallback ? localCallback )
13+ internal SslAuthenticationOptions ( )
14+ {
15+ TargetHost = string . Empty ;
16+ }
17+
18+ internal void UpdateOptions ( SslClientAuthenticationOptions sslClientAuthenticationOptions )
1419 {
1520 Debug . Assert ( sslClientAuthenticationOptions . TargetHost != null ) ;
1621
22+ if ( CertValidationDelegate == null )
23+ {
24+ CertValidationDelegate = sslClientAuthenticationOptions . RemoteCertificateValidationCallback ;
25+ }
26+ else if ( sslClientAuthenticationOptions . RemoteCertificateValidationCallback != null &&
27+ CertValidationDelegate != sslClientAuthenticationOptions . RemoteCertificateValidationCallback )
28+ {
29+ // Callback was set in constructor to differet value.
30+ throw new InvalidOperationException ( SR . Format ( SR . net_conflicting_options , nameof ( RemoteCertificateValidationCallback ) ) ) ;
31+ }
32+
33+ if ( CertSelectionDelegate == null )
34+ {
35+ CertSelectionDelegate = sslClientAuthenticationOptions . LocalCertificateSelectionCallback ;
36+ }
37+ else if ( sslClientAuthenticationOptions . LocalCertificateSelectionCallback != null &&
38+ CertSelectionDelegate != sslClientAuthenticationOptions . LocalCertificateSelectionCallback )
39+ {
40+ throw new InvalidOperationException ( SR . Format ( SR . net_conflicting_options , nameof ( LocalCertificateSelectionCallback ) ) ) ;
41+ }
42+
1743 // Common options.
1844 AllowRenegotiation = sslClientAuthenticationOptions . AllowRenegotiation ;
1945 ApplicationProtocols = sslClientAuthenticationOptions . ApplicationProtocols ;
20- CertValidationDelegate = remoteCallback ;
2146 CheckCertName = true ;
2247 EnabledSslProtocols = FilterOutIncompatibleSslProtocols ( sslClientAuthenticationOptions . EnabledSslProtocols ) ;
2348 EncryptionPolicy = sslClientAuthenticationOptions . EncryptionPolicy ;
@@ -27,32 +52,57 @@ internal SslAuthenticationOptions(SslClientAuthenticationOptions sslClientAuthen
2752 TargetHost = sslClientAuthenticationOptions . TargetHost . TrimEnd ( '.' ) ;
2853
2954 // Client specific options.
30- CertSelectionDelegate = localCallback ;
3155 CertificateRevocationCheckMode = sslClientAuthenticationOptions . CertificateRevocationCheckMode ;
3256 ClientCertificates = sslClientAuthenticationOptions . ClientCertificates ;
3357 CipherSuitesPolicy = sslClientAuthenticationOptions . CipherSuitesPolicy ;
3458 }
3559
36- internal SslAuthenticationOptions ( SslServerAuthenticationOptions sslServerAuthenticationOptions )
60+ internal void UpdateOptions ( ServerOptionsSelectionCallback optionCallback , object ? state )
3761 {
38- // Common options.
39- AllowRenegotiation = sslServerAuthenticationOptions . AllowRenegotiation ;
40- ApplicationProtocols = sslServerAuthenticationOptions . ApplicationProtocols ;
4162 CheckCertName = false ;
42- EnabledSslProtocols = FilterOutIncompatibleSslProtocols ( sslServerAuthenticationOptions . EnabledSslProtocols ) ;
43- EncryptionPolicy = sslServerAuthenticationOptions . EncryptionPolicy ;
63+ TargetHost = string . Empty ;
4464 IsServer = true ;
45- RemoteCertRequired = sslServerAuthenticationOptions . ClientCertificateRequired ;
46- if ( NetEventSource . Log . IsEnabled ( ) )
65+ UserState = state ;
66+ ServerOptionDelegate = optionCallback ;
67+ }
68+
69+ internal void UpdateOptions ( SslServerAuthenticationOptions sslServerAuthenticationOptions )
70+ {
71+ if ( sslServerAuthenticationOptions . ServerCertificate == null &&
72+ sslServerAuthenticationOptions . ServerCertificateContext == null &&
73+ sslServerAuthenticationOptions . ServerCertificateSelectionCallback == null &&
74+ CertSelectionDelegate == null )
4775 {
48- NetEventSource . Info ( this , $ "Server RemoteCertRequired: { RemoteCertRequired } .") ;
76+ throw new NotSupportedException ( SR . net_ssl_io_no_server_cert ) ;
77+ }
78+
79+ if ( ( sslServerAuthenticationOptions . ServerCertificate != null ||
80+ sslServerAuthenticationOptions . ServerCertificateContext != null ||
81+ CertSelectionDelegate != null ) &&
82+ sslServerAuthenticationOptions . ServerCertificateSelectionCallback != null )
83+ {
84+ throw new InvalidOperationException ( SR . Format ( SR . net_conflicting_options , nameof ( ServerCertificateSelectionCallback ) ) ) ;
85+ }
86+
87+ if ( CertValidationDelegate == null )
88+ {
89+ CertValidationDelegate = sslServerAuthenticationOptions . RemoteCertificateValidationCallback ;
90+ }
91+ else if ( sslServerAuthenticationOptions . RemoteCertificateValidationCallback != null &&
92+ CertValidationDelegate != sslServerAuthenticationOptions . RemoteCertificateValidationCallback )
93+ {
94+ // Callback was set in constructor to differet value.
95+ throw new InvalidOperationException ( SR . Format ( SR . net_conflicting_options , nameof ( RemoteCertificateValidationCallback ) ) ) ;
4996 }
50- TargetHost = string . Empty ;
5197
52- // Server specific options.
98+ IsServer = true ;
99+ AllowRenegotiation = sslServerAuthenticationOptions . AllowRenegotiation ;
100+ ApplicationProtocols = sslServerAuthenticationOptions . ApplicationProtocols ;
101+ EnabledSslProtocols = FilterOutIncompatibleSslProtocols ( sslServerAuthenticationOptions . EnabledSslProtocols ) ;
102+ EncryptionPolicy = sslServerAuthenticationOptions . EncryptionPolicy ;
103+ RemoteCertRequired = sslServerAuthenticationOptions . ClientCertificateRequired ;
53104 CipherSuitesPolicy = sslServerAuthenticationOptions . CipherSuitesPolicy ;
54105 CertificateRevocationCheckMode = sslServerAuthenticationOptions . CertificateRevocationCheckMode ;
55-
56106 if ( sslServerAuthenticationOptions . ServerCertificateContext != null )
57107 {
58108 CertificateContext = sslServerAuthenticationOptions . ServerCertificateContext ;
@@ -70,7 +120,7 @@ internal SslAuthenticationOptions(SslServerAuthenticationOptions sslServerAuthen
70120 {
71121 // This is legacy fix-up. If the Certificate did not have key, we will search stores and we
72122 // will try to find one with matching hash.
73- certificateWithKey = SecureChannel . FindCertificateWithPrivateKey ( this , true , sslServerAuthenticationOptions . ServerCertificate ) ;
123+ certificateWithKey = SslStream . FindCertificateWithPrivateKey ( this , true , sslServerAuthenticationOptions . ServerCertificate ) ;
74124 if ( certificateWithKey == null )
75125 {
76126 throw new AuthenticationException ( SR . net_ssl_io_no_server_cert ) ;
@@ -80,45 +130,9 @@ internal SslAuthenticationOptions(SslServerAuthenticationOptions sslServerAuthen
80130 }
81131 }
82132
83- if ( sslServerAuthenticationOptions . RemoteCertificateValidationCallback != null )
133+ if ( sslServerAuthenticationOptions . ServerCertificateSelectionCallback != null )
84134 {
85- CertValidationDelegate = sslServerAuthenticationOptions . RemoteCertificateValidationCallback ;
86- }
87- }
88-
89- internal SslAuthenticationOptions ( ServerOptionsSelectionCallback optionCallback , object ? state , RemoteCertificateValidationCallback ? remoteCallback )
90- {
91- CheckCertName = false ;
92- TargetHost = string . Empty ;
93- IsServer = true ;
94- UserState = state ;
95- ServerOptionDelegate = optionCallback ;
96- CertValidationDelegate = remoteCallback ;
97- }
98-
99- internal void UpdateOptions ( SslServerAuthenticationOptions sslServerAuthenticationOptions )
100- {
101- AllowRenegotiation = sslServerAuthenticationOptions . AllowRenegotiation ;
102- ApplicationProtocols = sslServerAuthenticationOptions . ApplicationProtocols ;
103- EnabledSslProtocols = FilterOutIncompatibleSslProtocols ( sslServerAuthenticationOptions . EnabledSslProtocols ) ;
104- EncryptionPolicy = sslServerAuthenticationOptions . EncryptionPolicy ;
105- RemoteCertRequired = sslServerAuthenticationOptions . ClientCertificateRequired ;
106- CipherSuitesPolicy = sslServerAuthenticationOptions . CipherSuitesPolicy ;
107- CertificateRevocationCheckMode = sslServerAuthenticationOptions . CertificateRevocationCheckMode ;
108- if ( sslServerAuthenticationOptions . ServerCertificateContext != null )
109- {
110- CertificateContext = sslServerAuthenticationOptions . ServerCertificateContext ;
111- }
112- else if ( sslServerAuthenticationOptions . ServerCertificate is X509Certificate2 certificateWithKey &&
113- certificateWithKey . HasPrivateKey )
114- {
115- // given cert is X509Certificate2 with key. We can use it directly.
116- CertificateContext = SslStreamCertificateContext . Create ( certificateWithKey ) ;
117- }
118-
119- if ( sslServerAuthenticationOptions . RemoteCertificateValidationCallback != null )
120- {
121- CertValidationDelegate = sslServerAuthenticationOptions . RemoteCertificateValidationCallback ;
135+ ServerCertSelectionDelegate = sslServerAuthenticationOptions . ServerCertificateSelectionCallback ;
122136 }
123137 }
124138
@@ -150,10 +164,10 @@ private static SslProtocols FilterOutIncompatibleSslProtocols(SslProtocols proto
150164 internal bool RemoteCertRequired { get ; set ; }
151165 internal bool CheckCertName { get ; set ; }
152166 internal RemoteCertificateValidationCallback ? CertValidationDelegate { get ; set ; }
153- internal LocalCertSelectionCallback ? CertSelectionDelegate { get ; set ; }
154- internal ServerCertSelectionCallback ? ServerCertSelectionDelegate { get ; set ; }
167+ internal LocalCertificateSelectionCallback ? CertSelectionDelegate { get ; set ; }
168+ internal ServerCertificateSelectionCallback ? ServerCertSelectionDelegate { get ; set ; }
155169 internal CipherSuitesPolicy ? CipherSuitesPolicy { get ; set ; }
156- internal object ? UserState { get ; }
157- internal ServerOptionsSelectionCallback ? ServerOptionDelegate { get ; }
170+ internal object ? UserState { get ; set ; }
171+ internal ServerOptionsSelectionCallback ? ServerOptionDelegate { get ; set ; }
158172 }
159173}
0 commit comments