Skip to content

Commit 77295ad

Browse files
github-actions[bot]wfurtbartonjs
authored
[release/7.0] set session ID when TLS resume is enabled (#75507)
* set session ID when TLS resume is enabled * feedback from review * remove random.h * Apply suggestions from code review Co-authored-by: Jeremy Barton <[email protected]> Co-authored-by: wfurt <[email protected]> Co-authored-by: Jeremy Barton <[email protected]>
1 parent b483931 commit 77295ad

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSsl.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,20 @@ internal static unsafe SafeSslContextHandle AllocateSslContext(SslAuthentication
203203
{
204204
if (sslAuthenticationOptions.IsServer)
205205
{
206-
Ssl.SslCtxSetCaching(sslCtx, 1, s_cacheSize, null, null);
206+
Span<byte> contextId = stackalloc byte[32];
207+
RandomNumberGenerator.Fill(contextId);
208+
Ssl.SslCtxSetCaching(sslCtx, 1, s_cacheSize, contextId.Length, contextId, null, null);
207209
}
208210
else
209211
{
210-
int result = Ssl.SslCtxSetCaching(sslCtx, 1, s_cacheSize, &NewSessionCallback, &RemoveSessionCallback);
212+
int result = Ssl.SslCtxSetCaching(sslCtx, 1, s_cacheSize, 0, null, &NewSessionCallback, &RemoveSessionCallback);
211213
Debug.Assert(result == 1);
212214
sslCtx.EnableSessionCache();
213215
}
214216
}
215217
else
216218
{
217-
Ssl.SslCtxSetCaching(sslCtx, 0, -1, null, null);
219+
Ssl.SslCtxSetCaching(sslCtx, 0, -1, 0, null, null, null);
218220
}
219221

220222
if (sslAuthenticationOptions.IsServer && sslAuthenticationOptions.ApplicationProtocols != null && sslAuthenticationOptions.ApplicationProtocols.Count != 0)

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.SslCtx.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal static partial class Ssl
3333
internal static unsafe partial void SslCtxSetAlpnSelectCb(SafeSslContextHandle ctx, delegate* unmanaged<IntPtr, byte**, byte*, byte*, uint, IntPtr, int> callback, IntPtr arg);
3434

3535
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_SslCtxSetCaching")]
36-
internal static unsafe partial int SslCtxSetCaching(SafeSslContextHandle ctx, int mode, int cacheSize, delegate* unmanaged<IntPtr, IntPtr, int> neewSessionCallback, delegate* unmanaged<IntPtr, IntPtr, void> removeSessionCallback);
36+
internal static unsafe partial int SslCtxSetCaching(SafeSslContextHandle ctx, int mode, int cacheSize, int contextIdLength, Span<byte> contextId, delegate* unmanaged<IntPtr, IntPtr, int> neewSessionCallback, delegate* unmanaged<IntPtr, IntPtr, void> removeSessionCallback);
3737

3838
internal static bool AddExtraChainCertificates(SafeSslContextHandle ctx, X509Certificate2[] chain)
3939
{

src/native/libs/System.Security.Cryptography.Native/opensslshim.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ const EVP_CIPHER* EVP_chacha20_poly1305(void);
486486
REQUIRED_FUNCTION(SSL_CTX_set_quiet_shutdown) \
487487
FALLBACK_FUNCTION(SSL_CTX_set_options) \
488488
FALLBACK_FUNCTION(SSL_CTX_set_security_level) \
489+
REQUIRED_FUNCTION(SSL_CTX_set_session_id_context) \
489490
REQUIRED_FUNCTION(SSL_CTX_set_verify) \
490491
REQUIRED_FUNCTION(SSL_CTX_use_certificate) \
491492
REQUIRED_FUNCTION(SSL_CTX_use_PrivateKey) \
@@ -965,6 +966,7 @@ FOR_ALL_OPENSSL_FUNCTIONS
965966
#define SSL_CTX_set_options SSL_CTX_set_options_ptr
966967
#define SSL_CTX_set_quiet_shutdown SSL_CTX_set_quiet_shutdown_ptr
967968
#define SSL_CTX_set_security_level SSL_CTX_set_security_level_ptr
969+
#define SSL_CTX_set_session_id_context SSL_CTX_set_session_id_context_ptr
968970
#define SSL_CTX_set_verify SSL_CTX_set_verify_ptr
969971
#define SSL_CTX_use_certificate SSL_CTX_use_certificate_ptr
970972
#define SSL_CTX_use_PrivateKey SSL_CTX_use_PrivateKey_ptr

src/native/libs/System.Security.Cryptography.Native/pal_ssl.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ void CryptoNative_SslSetVerifyPeer(SSL* ssl)
655655
SSL_set_verify(ssl, SSL_VERIFY_PEER, verify_callback);
656656
}
657657

658-
int CryptoNative_SslCtxSetCaching(SSL_CTX* ctx, int mode, int cacheSize, SslCtxNewSessionCallback newSessionCb, SslCtxRemoveSessionCallback removeSessionCb)
658+
int CryptoNative_SslCtxSetCaching(SSL_CTX* ctx, int mode, int cacheSize, int contextIdLength, uint8_t* contextId, SslCtxNewSessionCallback newSessionCb, SslCtxRemoveSessionCallback removeSessionCb)
659659
{
660660
int retValue = 1;
661661
if (mode && !API_EXISTS(SSL_SESSION_get0_hostname))
@@ -683,6 +683,11 @@ int CryptoNative_SslCtxSetCaching(SSL_CTX* ctx, int mode, int cacheSize, SslCtxN
683683
SSL_CTX_ctrl(ctx, SSL_CTRL_SET_SESS_CACHE_SIZE, (long)cacheSize, NULL);
684684
}
685685

686+
if (contextIdLength > 0 && contextId != NULL)
687+
{
688+
SSL_CTX_set_session_id_context(ctx, contextId, contextIdLength <= SSL_MAX_SID_CTX_LENGTH ? (unsigned int)contextIdLength : SSL_MAX_SID_CTX_LENGTH);
689+
}
690+
686691
if (newSessionCb != NULL)
687692
{
688693
SSL_CTX_sess_set_new_cb(ctx, newSessionCb);

src/native/libs/System.Security.Cryptography.Native/pal_ssl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ PALEXPORT void CryptoNative_SslSetPostHandshakeAuth(SSL* ssl, int32_t val);
162162
/*
163163
Sets session caching. 0 is disabled.
164164
*/
165-
PALEXPORT int CryptoNative_SslCtxSetCaching(SSL_CTX* ctx, int mode, int cacheSize, SslCtxNewSessionCallback newCb, SslCtxRemoveSessionCallback removeCb);
165+
PALEXPORT int CryptoNative_SslCtxSetCaching(SSL_CTX* ctx, int mode, int cacheSize, int contextIdLength, uint8_t* contextId, SslCtxNewSessionCallback newSessionCb, SslCtxRemoveSessionCallback removeSessionCb);
166166

167167
/*
168168
Returns name associated with given ssl session.

0 commit comments

Comments
 (0)