Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitccc47f5

Browse files
authored
Reduce unnecessary casting in SslStreamPal (#51324)
1 parent9b364cf commitccc47f5

File tree

3 files changed

+41
-49
lines changed

3 files changed

+41
-49
lines changed

‎src/libraries/System.Net.Security/src/System/Net/Security/SslStreamPal.Android.cs‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ public static SafeFreeCredentials AcquireCredentialsHandle(
5454
returnnewSafeFreeSslCredentials(certificateContext,protocols,policy);
5555
}
5656

57-
internalstaticbyte[]?GetNegotiatedApplicationProtocol(SafeDeleteContext?context)
57+
internalstaticbyte[]?GetNegotiatedApplicationProtocol(SafeDeleteSslContext?context)
5858
{
5959
if(context==null)
6060
returnnull;
6161

62-
returnInterop.AndroidCrypto.SSLStreamGetApplicationProtocol(((SafeDeleteSslContext)context).SslContext);
62+
returnInterop.AndroidCrypto.SSLStreamGetApplicationProtocol(context.SslContext);
6363
}
6464

6565
publicstaticSecurityStatusPalEncryptMessage(
66-
SafeDeleteContextsecurityContext,
66+
SafeDeleteSslContextsecurityContext,
6767
ReadOnlyMemory<byte>input,
6868
intheaderSize,
6969
inttrailerSize,
@@ -75,8 +75,7 @@ public static SecurityStatusPal EncryptMessage(
7575

7676
try
7777
{
78-
SafeDeleteSslContextsslContext=(SafeDeleteSslContext)securityContext;
79-
SafeSslHandlesslHandle=sslContext.SslContext;
78+
SafeSslHandlesslHandle=securityContext.SslContext;
8079

8180
PAL_SSLStreamStatusret=Interop.AndroidCrypto.SSLStreamWrite(sslHandle,input);
8281
SecurityStatusPalErrorCodestatusCode=retswitch
@@ -88,13 +87,13 @@ public static SecurityStatusPal EncryptMessage(
8887
_=>SecurityStatusPalErrorCode.InternalError
8988
};
9089

91-
if(sslContext.BytesReadyForConnection<=output?.Length)
90+
if(securityContext.BytesReadyForConnection<=output?.Length)
9291
{
93-
resultSize=sslContext.ReadPendingWrites(output,0,output.Length);
92+
resultSize=securityContext.ReadPendingWrites(output,0,output.Length);
9493
}
9594
else
9695
{
97-
output=sslContext.ReadPendingWrites()!;
96+
output=securityContext.ReadPendingWrites()!;
9897
resultSize=output.Length;
9998
}
10099

@@ -107,17 +106,16 @@ public static SecurityStatusPal EncryptMessage(
107106
}
108107

109108
publicstaticSecurityStatusPalDecryptMessage(
110-
SafeDeleteContextsecurityContext,
109+
SafeDeleteSslContextsecurityContext,
111110
byte[]buffer,
112111
refintoffset,
113112
refintcount)
114113
{
115114
try
116115
{
117-
SafeDeleteSslContextsslContext=(SafeDeleteSslContext)securityContext;
118-
SafeSslHandlesslHandle=sslContext.SslContext;
116+
SafeSslHandlesslHandle=securityContext.SslContext;
119117

120-
sslContext.Write(buffer.AsSpan(offset,count));
118+
securityContext.Write(buffer.AsSpan(offset,count));
121119

122120
PAL_SSLStreamStatusret=Interop.AndroidCrypto.SSLStreamRead(sslHandle,buffer.AsSpan(offset,count),outintread);
123121
if(ret==PAL_SSLStreamStatus.Error)
@@ -162,10 +160,10 @@ public static void QueryContextStreamSizes(
162160
}
163161

164162
publicstaticvoidQueryContextConnectionInfo(
165-
SafeDeleteContextsecurityContext,
163+
SafeDeleteSslContextsecurityContext,
166164
outSslConnectionInfoconnectionInfo)
167165
{
168-
connectionInfo=newSslConnectionInfo(((SafeDeleteSslContext)securityContext).SslContext);
166+
connectionInfo=newSslConnectionInfo(securityContext.SslContext);
169167
}
170168

171169
privatestaticSecurityStatusPalHandshakeInternal(
@@ -225,10 +223,9 @@ public static SecurityStatusPal ApplyAlertToken(
225223

226224
publicstaticSecurityStatusPalApplyShutdownToken(
227225
refSafeFreeCredentials?credentialsHandle,
228-
SafeDeleteContextsecurityContext)
226+
SafeDeleteSslContextsecurityContext)
229227
{
230-
SafeDeleteSslContextsslContext=((SafeDeleteSslContext)securityContext);
231-
SafeSslHandlesslHandle=sslContext.SslContext;
228+
SafeSslHandlesslHandle=securityContext.SslContext;
232229

233230

234231
boolsuccess=Interop.AndroidCrypto.SSLStreamShutdown(sslHandle);

‎src/libraries/System.Net.Security/src/System/Net/Security/SslStreamPal.OSX.cs‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@ public static SafeFreeCredentials AcquireCredentialsHandle(
6161
returnnewSafeFreeSslCredentials(certificateContext,protocols,policy);
6262
}
6363

64-
internalstaticbyte[]?GetNegotiatedApplicationProtocol(SafeDeleteContext?context)
64+
internalstaticbyte[]?GetNegotiatedApplicationProtocol(SafeDeleteSslContext?context)
6565
{
6666
if(context==null)
6767
returnnull;
6868

69-
returnInterop.AppleCrypto.SslGetAlpnSelected(((SafeDeleteSslContext)context).SslContext);
69+
returnInterop.AppleCrypto.SslGetAlpnSelected(context.SslContext);
7070
}
7171

7272
publicstaticSecurityStatusPalEncryptMessage(
73-
SafeDeleteContextsecurityContext,
73+
SafeDeleteSslContextsecurityContext,
7474
ReadOnlyMemory<byte>input,
7575
intheaderSize,
7676
inttrailerSize,
@@ -83,8 +83,7 @@ public static SecurityStatusPal EncryptMessage(
8383

8484
try
8585
{
86-
SafeDeleteSslContextsslContext=(SafeDeleteSslContext)securityContext;
87-
SafeSslHandlesslHandle=sslContext.SslContext;
86+
SafeSslHandlesslHandle=securityContext.SslContext;
8887

8988
unsafe
9089
{
@@ -104,13 +103,13 @@ public static SecurityStatusPal EncryptMessage(
104103
Interop.AppleCrypto.CreateExceptionForOSStatus((int)status));
105104
}
106105

107-
if(sslContext.BytesReadyForConnection<=output?.Length)
106+
if(securityContext.BytesReadyForConnection<=output?.Length)
108107
{
109-
resultSize=sslContext.ReadPendingWrites(output,0,output.Length);
108+
resultSize=securityContext.ReadPendingWrites(output,0,output.Length);
110109
}
111110
else
112111
{
113-
output=sslContext.ReadPendingWrites()!;
112+
output=securityContext.ReadPendingWrites()!;
114113
resultSize=output.Length;
115114
}
116115

@@ -138,17 +137,16 @@ public static SecurityStatusPal EncryptMessage(
138137
}
139138

140139
publicstaticSecurityStatusPalDecryptMessage(
141-
SafeDeleteContextsecurityContext,
140+
SafeDeleteSslContextsecurityContext,
142141
byte[]buffer,
143142
refintoffset,
144143
refintcount)
145144
{
146145
try
147146
{
148-
SafeDeleteSslContextsslContext=(SafeDeleteSslContext)securityContext;
149-
SafeSslHandlesslHandle=sslContext.SslContext;
147+
SafeSslHandlesslHandle=securityContext.SslContext;
150148

151-
sslContext.Write(buffer.AsSpan(offset,count));
149+
securityContext.Write(buffer.AsSpan(offset,count));
152150

153151
unsafe
154152
{
@@ -212,10 +210,10 @@ public static void QueryContextStreamSizes(
212210
}
213211

214212
publicstaticvoidQueryContextConnectionInfo(
215-
SafeDeleteContextsecurityContext,
213+
SafeDeleteSslContextsecurityContext,
216214
outSslConnectionInfoconnectionInfo)
217215
{
218-
connectionInfo=newSslConnectionInfo(((SafeDeleteSslContext)securityContext).SslContext);
216+
connectionInfo=newSslConnectionInfo(securityContext.SslContext);
219217
}
220218

221219
privatestaticSecurityStatusPalHandshakeInternal(
@@ -307,10 +305,9 @@ public static SecurityStatusPal ApplyAlertToken(
307305

308306
publicstaticSecurityStatusPalApplyShutdownToken(
309307
refSafeFreeCredentials?credentialsHandle,
310-
SafeDeleteContextsecurityContext)
308+
SafeDeleteSslContextsecurityContext)
311309
{
312-
SafeDeleteSslContextsslContext=((SafeDeleteSslContext)securityContext);
313-
SafeSslHandlesslHandle=sslContext.SslContext;
310+
SafeSslHandlesslHandle=securityContext.SslContext;
314311

315312
intosStatus=Interop.AppleCrypto.SslShutdown(sslHandle);
316313

‎src/libraries/System.Net.Security/src/System/Net/Security/SslStreamPal.Unix.cs‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public static SafeFreeCredentials AcquireCredentialsHandle(SslStreamCertificateC
4242
returnnewSafeFreeSslCredentials(certificateContext?.Certificate,protocols,policy);
4343
}
4444

45-
publicstaticSecurityStatusPalEncryptMessage(SafeDeleteContextsecurityContext,ReadOnlyMemory<byte>input,intheaderSize,inttrailerSize,refbyte[]output,outintresultSize)
45+
publicstaticSecurityStatusPalEncryptMessage(SafeDeleteSslContextsecurityContext,ReadOnlyMemory<byte>input,intheaderSize,inttrailerSize,refbyte[]output,outintresultSize)
4646
{
4747
returnEncryptDecryptHelper(securityContext,input,offset:0,size:0,encrypt:true,output:refoutput,resultSize:outresultSize);
4848
}
4949

50-
publicstaticSecurityStatusPalDecryptMessage(SafeDeleteContextsecurityContext,byte[]buffer,refintoffset,refintcount)
50+
publicstaticSecurityStatusPalDecryptMessage(SafeDeleteSslContextsecurityContext,byte[]buffer,refintoffset,refintcount)
5151
{
5252
SecurityStatusPalretVal=EncryptDecryptHelper(securityContext,buffer,offset,count,false,refbuffer,outintresultSize);
5353
if(retVal.ErrorCode==SecurityStatusPalErrorCode.OK||
@@ -58,7 +58,7 @@ public static SecurityStatusPal DecryptMessage(SafeDeleteContext securityContext
5858
returnretVal;
5959
}
6060

61-
publicstaticChannelBinding?QueryContextChannelBinding(SafeDeleteContextsecurityContext,ChannelBindingKindattribute)
61+
publicstaticChannelBinding?QueryContextChannelBinding(SafeDeleteSslContextsecurityContext,ChannelBindingKindattribute)
6262
{
6363
ChannelBinding?bindingHandle;
6464

@@ -74,7 +74,7 @@ public static SecurityStatusPal DecryptMessage(SafeDeleteContext securityContext
7474
else
7575
{
7676
bindingHandle=Interop.OpenSsl.QueryChannelBinding(
77-
((SafeDeleteSslContext)securityContext).SslContext,
77+
securityContext.SslContext,
7878
attribute);
7979
}
8080

@@ -86,9 +86,9 @@ public static void QueryContextStreamSizes(SafeDeleteContext? securityContext, o
8686
streamSizes=StreamSizes.Default;
8787
}
8888

89-
publicstaticvoidQueryContextConnectionInfo(SafeDeleteContextsecurityContext,outSslConnectionInfoconnectionInfo)
89+
publicstaticvoidQueryContextConnectionInfo(SafeDeleteSslContextsecurityContext,outSslConnectionInfoconnectionInfo)
9090
{
91-
connectionInfo=newSslConnectionInfo(((SafeDeleteSslContext)securityContext).SslContext);
91+
connectionInfo=newSslConnectionInfo(securityContext.SslContext);
9292
}
9393

9494
publicstaticbyte[]ConvertAlpnProtocolListToByteArray(List<SslApplicationProtocol>applicationProtocols)
@@ -111,13 +111,13 @@ private static SecurityStatusPal HandshakeInternal(SafeFreeCredentials credentia
111111
context=newSafeDeleteSslContext((credentialasSafeFreeSslCredentials)!,sslAuthenticationOptions);
112112
}
113113

114-
booldone=Interop.OpenSsl.DoSslHandshake(((SafeDeleteSslContext)context).SslContext,inputBuffer,outoutput,outoutputSize);
114+
booldone=Interop.OpenSsl.DoSslHandshake(context.SslContext,inputBuffer,outoutput,outoutputSize);
115115

116116
// When the handshake is done, and the context is server, check if the alpnHandle target was set to null during ALPN.
117117
// If it was, then that indicates ALPN failed, send failure.
118118
// We have this workaround, as openssl supports terminating handshake only from version 1.1.0,
119119
// whereas ALPN is supported from version 1.0.2.
120-
SafeSslHandlesslContext=((SafeDeleteSslContext)context).SslContext;
120+
SafeSslHandlesslContext=context.SslContext;
121121
if(done&&sslAuthenticationOptions.IsServer&&sslAuthenticationOptions.ApplicationProtocols!=null&&sslContext.AlpnHandle.IsAllocated&&sslContext.AlpnHandle.Target==null)
122122
{
123123
returnnewSecurityStatusPal(SecurityStatusPalErrorCode.InternalError,Interop.OpenSsl.CreateSslException(SR.net_alpn_failed));
@@ -142,21 +142,21 @@ private static SecurityStatusPal HandshakeInternal(SafeFreeCredentials credentia
142142
}
143143
}
144144

145-
internalstaticbyte[]?GetNegotiatedApplicationProtocol(SafeDeleteContext?context)
145+
internalstaticbyte[]?GetNegotiatedApplicationProtocol(SafeDeleteSslContext?context)
146146
{
147147
if(context==null)
148148
returnnull;
149149

150-
returnInterop.Ssl.SslGetAlpnSelected(((SafeDeleteSslContext)context).SslContext);
150+
returnInterop.Ssl.SslGetAlpnSelected(context.SslContext);
151151
}
152152

153-
privatestaticSecurityStatusPalEncryptDecryptHelper(SafeDeleteContextsecurityContext,ReadOnlyMemory<byte>input,intoffset,intsize,boolencrypt,refbyte[]output,outintresultSize)
153+
privatestaticSecurityStatusPalEncryptDecryptHelper(SafeDeleteSslContextsecurityContext,ReadOnlyMemory<byte>input,intoffset,intsize,boolencrypt,refbyte[]output,outintresultSize)
154154
{
155155
resultSize=0;
156156
try
157157
{
158158
Interop.Ssl.SslErrorCodeerrorCode=Interop.Ssl.SslErrorCode.SSL_ERROR_NONE;
159-
SafeSslHandlescHandle=((SafeDeleteSslContext)securityContext).SslContext;
159+
SafeSslHandlescHandle=securityContext.SslContext;
160160

161161
if(encrypt)
162162
{
@@ -194,10 +194,8 @@ public static SecurityStatusPal ApplyAlertToken(ref SafeFreeCredentials? credent
194194
returnnewSecurityStatusPal(SecurityStatusPalErrorCode.OK);
195195
}
196196

197-
publicstaticSecurityStatusPalApplyShutdownToken(refSafeFreeCredentials?credentialsHandle,SafeDeleteContextsecurityContext)
197+
publicstaticSecurityStatusPalApplyShutdownToken(refSafeFreeCredentials?credentialsHandle,SafeDeleteSslContextsslContext)
198198
{
199-
SafeDeleteSslContextsslContext=((SafeDeleteSslContext)securityContext);
200-
201199
// Unset the quiet shutdown option initially configured.
202200
Interop.Ssl.SslSetQuietShutdown(sslContext.SslContext,0);
203201

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp