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

Commit f5452f9

Browse files
committed
Make a helper method for invoking the negative-size NativeCrypto methods.
GetDynamicBuffer uses CreateOpenSslCryptographicException to allow OpenSSL errors to propagate through NativeCrypto.
1 parent be8013d commit f5452f9

File tree

2 files changed

+43
-67
lines changed

2 files changed

+43
-67
lines changed

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

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5-
using System.Diagnostics;
65
using System.Runtime.InteropServices;
7-
using System.Security.Cryptography;
86

97
using Microsoft.Win32.SafeHandles;
108

119
internal static partial class Interop
1210
{
1311
internal static partial class NativeCrypto
1412
{
13+
private delegate int NegativeSizeReadMethod<in THandle>(THandle handle, byte[] buf, int cBuf);
14+
1515
[DllImport(Libraries.CryptoInterop)]
16-
internal static extern int GetX509Thumbprint(SafeX509Handle x509, byte[] buf, int cBuf);
16+
private static extern int GetX509Thumbprint(SafeX509Handle x509, byte[] buf, int cBuf);
1717

1818
[DllImport(Libraries.CryptoInterop)]
19-
internal static extern int GetX509NameRawBytes(IntPtr x509Name, byte[] buf, int cBuf);
19+
private static extern int GetX509NameRawBytes(IntPtr x509Name, byte[] buf, int cBuf);
2020

2121
[DllImport(Libraries.CryptoInterop)]
2222
internal static extern IntPtr GetX509NotBefore(SafeX509Handle x509);
@@ -34,7 +34,7 @@ internal static partial class NativeCrypto
3434
internal static extern IntPtr GetX509PublicKeyAlgorithm(SafeX509Handle x509);
3535

3636
[DllImport(Libraries.CryptoInterop)]
37-
internal static extern int GetX509PublicKeyParameterBytes(SafeX509Handle x509, byte[] buf, int cBuf);
37+
private static extern int GetX509PublicKeyParameterBytes(SafeX509Handle x509, byte[] buf, int cBuf);
3838

3939
[DllImport(Libraries.CryptoInterop)]
4040
internal static extern IntPtr GetX509PublicKeyBytes(SafeX509Handle x509);
@@ -80,23 +80,22 @@ private static extern int SetX509ChainVerifyTime(
8080

8181
internal static byte[] GetAsn1StringBytes(IntPtr asn1)
8282
{
83-
int negativeSize = GetAsn1StringBytes(asn1, null, 0);
84-
85-
if (negativeSize > 0)
86-
{
87-
throw new CryptographicException();
88-
}
89-
90-
byte[] bytes = new byte[-negativeSize];
83+
return GetDynamicBuffer(GetAsn1StringBytes, asn1);
84+
}
9185

92-
int ret = GetAsn1StringBytes(asn1, bytes, bytes.Length);
86+
internal static byte[] GetX509Thumbprint(SafeX509Handle x509)
87+
{
88+
return GetDynamicBuffer(GetX509Thumbprint, x509);
89+
}
9390

94-
if (ret != 1)
95-
{
96-
throw new CryptographicException();
97-
}
91+
internal static byte[] GetX509NameRawBytes(IntPtr x509Name)
92+
{
93+
return GetDynamicBuffer(GetX509NameRawBytes, x509Name);
94+
}
9895

99-
return bytes;
96+
internal static byte[] GetX509PublicKeyParameterBytes(SafeX509Handle x509)
97+
{
98+
return GetDynamicBuffer(GetX509PublicKeyParameterBytes, x509);
10099
}
101100

102101
internal static void SetX509ChainVerifyTime(SafeX509StoreCtxHandle ctx, DateTime verifyTime)
@@ -119,8 +118,29 @@ internal static void SetX509ChainVerifyTime(SafeX509StoreCtxHandle ctx, DateTime
119118

120119
if (succeeded != 1)
121120
{
122-
throw new CryptographicException();
121+
throw Interop.libcrypto.CreateOpenSslCryptographicException();
123122
}
124123
}
124+
125+
private static byte[] GetDynamicBuffer<THandle>(NegativeSizeReadMethod<THandle> method, THandle handle)
126+
{
127+
int negativeSize = method(handle, null, 0);
128+
129+
if (negativeSize > 0)
130+
{
131+
throw Interop.libcrypto.CreateOpenSslCryptographicException();
132+
}
133+
134+
byte[] bytes = new byte[-negativeSize];
135+
136+
int ret = method(handle, bytes, bytes.Length);
137+
138+
if (ret != 1)
139+
{
140+
throw Interop.libcrypto.CreateOpenSslCryptographicException();
141+
}
142+
143+
return bytes;
144+
}
125145
}
126146
}

src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslX509CertificateReader.cs

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,7 @@ public byte[] Thumbprint
104104
{
105105
get
106106
{
107-
int negativeSize = Interop.NativeCrypto.GetX509Thumbprint(_cert, null, 0);
108-
109-
if (negativeSize >= 0)
110-
{
111-
throw new CryptographicException();
112-
}
113-
114-
byte[] buf = new byte[-negativeSize];
115-
int ret = Interop.NativeCrypto.GetX509Thumbprint(_cert, buf, buf.Length);
116-
117-
if (ret != 1)
118-
{
119-
throw new CryptographicException();
120-
}
121-
122-
return buf;
107+
return Interop.NativeCrypto.GetX509Thumbprint(_cert);
123108
}
124109
}
125110

@@ -136,22 +121,7 @@ public byte[] KeyAlgorithmParameters
136121
{
137122
get
138123
{
139-
int negativeLen = Interop.NativeCrypto.GetX509PublicKeyParameterBytes(_cert, null, 0);
140-
141-
if (negativeLen >= 0)
142-
{
143-
throw new CryptographicException();
144-
}
145-
146-
byte[] buf = new byte[-negativeLen];
147-
int ret = Interop.NativeCrypto.GetX509PublicKeyParameterBytes(_cert, buf, buf.Length);
148-
149-
if (ret != 1)
150-
{
151-
throw new CryptographicException();
152-
}
153-
154-
return buf;
124+
return Interop.NativeCrypto.GetX509PublicKeyParameterBytes(_cert);
155125
}
156126
}
157127

@@ -343,21 +313,7 @@ private static X500DistinguishedName LoadX500Name(IntPtr namePtr)
343313
{
344314
Interop.libcrypto.CheckValidOpenSslHandle(namePtr);
345315

346-
int negativeSize = Interop.NativeCrypto.GetX509NameRawBytes(namePtr, null, 0);
347-
348-
if (negativeSize > 0)
349-
{
350-
throw new CryptographicException();
351-
}
352-
353-
byte[] buf = new byte[-negativeSize];
354-
int ret = Interop.NativeCrypto.GetX509NameRawBytes(namePtr, buf, buf.Length);
355-
356-
if (ret != 1)
357-
{
358-
throw new CryptographicException();
359-
}
360-
316+
byte[] buf = Interop.NativeCrypto.GetX509NameRawBytes(namePtr);
361317
return new X500DistinguishedName(buf);
362318
}
363319

0 commit comments

Comments
 (0)