Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 1173109

Browse files
committed
Respond to PR feedback.
1 parent 96e2b63 commit 1173109

File tree

8 files changed

+203
-214
lines changed

8 files changed

+203
-214
lines changed

src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Ssl.cs

Lines changed: 134 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ internal static partial class Crypto
4343
internal static extern void SslCtxDestroy(IntPtr ctx);
4444

4545
[DllImport(Libraries.CryptoNative)]
46-
private static extern IntPtr SSLGetVersion(SafeSslHandle ssl);
46+
private static extern IntPtr SslGetVersion(SafeSslHandle ssl);
4747

4848
internal static string GetProtocolVersion(SafeSslHandle ssl)
4949
{
50-
return Marshal.PtrToStringAnsi(SSLGetVersion(ssl));
50+
return Marshal.PtrToStringAnsi(SslGetVersion(ssl));
5151
}
5252

5353
[DllImport(Libraries.CryptoNative)]
@@ -69,3 +69,135 @@ internal static extern bool GetSslConnectionInfo(
6969
internal static unsafe extern int BioWrite(SafeBioHandle b, byte* data, int len);
7070
}
7171
}
72+
73+
namespace Microsoft.Win32.SafeHandles
74+
{
75+
internal sealed class SafeSslContextHandle : SafeHandle
76+
{
77+
private SafeSslContextHandle()
78+
: base(IntPtr.Zero, true)
79+
{
80+
}
81+
82+
public override bool IsInvalid
83+
{
84+
get { return handle == IntPtr.Zero; }
85+
}
86+
87+
protected override bool ReleaseHandle()
88+
{
89+
Interop.Crypto.SslCtxDestroy(handle);
90+
SetHandle(IntPtr.Zero);
91+
return true;
92+
}
93+
}
94+
95+
internal sealed class SafeSslHandle : SafeHandle
96+
{
97+
private SafeBioHandle _readBio;
98+
private SafeBioHandle _writeBio;
99+
private bool _isServer;
100+
101+
public bool IsServer
102+
{
103+
get { return _isServer; }
104+
}
105+
106+
public SafeBioHandle InputBio
107+
{
108+
get
109+
{
110+
return _readBio;
111+
}
112+
}
113+
114+
public SafeBioHandle OutputBio
115+
{
116+
get
117+
{
118+
return _writeBio;
119+
}
120+
}
121+
122+
public static SafeSslHandle Create(SafeSslContextHandle context, bool isServer)
123+
{
124+
SafeBioHandle readBio = Interop.Crypto.CreateMemoryBio();
125+
if (readBio.IsInvalid)
126+
{
127+
return new SafeSslHandle();
128+
}
129+
130+
SafeBioHandle writeBio = Interop.Crypto.CreateMemoryBio();
131+
if (writeBio.IsInvalid)
132+
{
133+
readBio.Dispose();
134+
return new SafeSslHandle();
135+
}
136+
137+
SafeSslHandle handle = Interop.Crypto.SslCreate(context);
138+
if (handle.IsInvalid)
139+
{
140+
readBio.Dispose();
141+
writeBio.Dispose();
142+
return handle;
143+
}
144+
handle._isServer = isServer;
145+
146+
// After SSL_set_bio, the BIO handles are owned by SSL pointer
147+
// and are automatically freed by SSL_free. To prevent a double
148+
// free, we need to keep the ref counts bumped up till SSL_free
149+
bool gotRef = false;
150+
readBio.DangerousAddRef(ref gotRef);
151+
try
152+
{
153+
bool ignore = false;
154+
writeBio.DangerousAddRef(ref ignore);
155+
}
156+
catch
157+
{
158+
if (gotRef)
159+
{
160+
readBio.DangerousRelease();
161+
}
162+
throw;
163+
}
164+
165+
Interop.libssl.SSL_set_bio(handle, readBio, writeBio);
166+
handle._readBio = readBio;
167+
handle._writeBio = writeBio;
168+
169+
if (isServer)
170+
{
171+
Interop.libssl.SSL_set_accept_state(handle);
172+
}
173+
else
174+
{
175+
Interop.libssl.SSL_set_connect_state(handle);
176+
}
177+
return handle;
178+
}
179+
180+
public override bool IsInvalid
181+
{
182+
get { return handle == IntPtr.Zero; }
183+
}
184+
185+
protected override bool ReleaseHandle()
186+
{
187+
Interop.Crypto.SslDestroy(handle);
188+
if (_readBio != null)
189+
{
190+
_readBio.SetHandleAsInvalid(); // BIO got freed in SslDestroy
191+
}
192+
if (_writeBio != null)
193+
{
194+
_writeBio.SetHandleAsInvalid(); // BIO got freed in SslDestroy
195+
}
196+
return true;
197+
}
198+
199+
private SafeSslHandle() : base(IntPtr.Zero, true)
200+
{
201+
}
202+
}
203+
}

src/Common/src/Interop/Unix/libssl/Interop.OpenSsl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ private static void Disconnect(SafeSslHandle context)
354354
int retVal = libssl.SSL_shutdown(context);
355355
if (retVal < 0)
356356
{
357-
//TODO (Issue #3362) check this error
357+
//TODO (Issue #4031) check this error
358358
Crypto.SslGetError(context, retVal);
359359
}
360360
}

src/Common/src/Interop/Unix/libssl/SslConnectionInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace System.Net
88
{
99
internal class SslConnectionInfo
1010
{
11-
public readonly SslProtocols Protocol;
11+
public readonly int Protocol;
1212
public readonly int DataCipherAlg;
1313
public readonly int DataKeySize;
1414
public readonly int DataHashAlg;
@@ -19,7 +19,7 @@ internal class SslConnectionInfo
1919
internal SslConnectionInfo(SafeSslHandle sslContext)
2020
{
2121
string protocolVersion = Interop.Crypto.GetProtocolVersion(sslContext);
22-
Protocol = MapProtocolVersion(protocolVersion);
22+
Protocol = (int)MapProtocolVersion(protocolVersion);
2323

2424
if (!Interop.Crypto.GetSslConnectionInfo(
2525
sslContext,

src/Common/src/Microsoft/Win32/SafeHandles/SafeSslHandle.Unix.cs

Lines changed: 0 additions & 139 deletions
This file was deleted.

src/Native/System.Security.Cryptography.Native/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ if (OPENSSL_TLSV12)
2727
endif()
2828

2929
include(CheckStructHasMember)
30-
check_struct_has_member ("SSL_CIPHER" algorithms openssl/ssl.h SSL_CIPHER_ALGORITHMS)
31-
if (SSL_CIPHER_ALGORITHMS)
32-
add_definitions(-DHAVE_SSL_CIPHER_ALGORITHMS=1)
30+
check_struct_has_member ("SSL_CIPHER" algorithm_enc openssl/ssl.h SSL_CIPHER_SPLIT_ALGORITHMS)
31+
if (SSL_CIPHER_SPLIT_ALGORITHMS)
32+
add_definitions(-DHAVE_SSL_CIPHER_SPLIT_ALGORITHMS=1)
3333
endif()
3434

3535
set(NATIVECRYPTO_SOURCES

0 commit comments

Comments
 (0)