2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
4
using System ;
5
- using System . Diagnostics ;
6
5
using System . Runtime . InteropServices ;
7
- using System . Security . Cryptography ;
8
6
9
7
using Microsoft . Win32 . SafeHandles ;
10
8
11
9
internal static partial class Interop
12
10
{
13
11
internal static partial class NativeCrypto
14
12
{
13
+ private delegate int NegativeSizeReadMethod < in THandle > ( THandle handle , byte [ ] buf , int cBuf ) ;
14
+
15
15
[ 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 ) ;
17
17
18
18
[ 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 ) ;
20
20
21
21
[ DllImport ( Libraries . CryptoInterop ) ]
22
22
internal static extern IntPtr GetX509NotBefore ( SafeX509Handle x509 ) ;
@@ -34,7 +34,7 @@ internal static partial class NativeCrypto
34
34
internal static extern IntPtr GetX509PublicKeyAlgorithm ( SafeX509Handle x509 ) ;
35
35
36
36
[ 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 ) ;
38
38
39
39
[ DllImport ( Libraries . CryptoInterop ) ]
40
40
internal static extern IntPtr GetX509PublicKeyBytes ( SafeX509Handle x509 ) ;
@@ -80,23 +80,22 @@ private static extern int SetX509ChainVerifyTime(
80
80
81
81
internal static byte [ ] GetAsn1StringBytes ( IntPtr asn1 )
82
82
{
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
+ }
91
85
92
- int ret = GetAsn1StringBytes ( asn1 , bytes , bytes . Length ) ;
86
+ internal static byte [ ] GetX509Thumbprint ( SafeX509Handle x509 )
87
+ {
88
+ return GetDynamicBuffer ( GetX509Thumbprint , x509 ) ;
89
+ }
93
90
94
- if ( ret != 1 )
95
- {
96
- throw new CryptographicException ( ) ;
97
- }
91
+ internal static byte [ ] GetX509NameRawBytes ( IntPtr x509Name )
92
+ {
93
+ return GetDynamicBuffer ( GetX509NameRawBytes , x509Name ) ;
94
+ }
98
95
99
- return bytes ;
96
+ internal static byte [ ] GetX509PublicKeyParameterBytes ( SafeX509Handle x509 )
97
+ {
98
+ return GetDynamicBuffer ( GetX509PublicKeyParameterBytes , x509 ) ;
100
99
}
101
100
102
101
internal static void SetX509ChainVerifyTime ( SafeX509StoreCtxHandle ctx , DateTime verifyTime )
@@ -119,8 +118,29 @@ internal static void SetX509ChainVerifyTime(SafeX509StoreCtxHandle ctx, DateTime
119
118
120
119
if ( succeeded != 1 )
121
120
{
122
- throw new CryptographicException ( ) ;
121
+ throw Interop . libcrypto . CreateOpenSslCryptographicException ( ) ;
123
122
}
124
123
}
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
+ }
125
145
}
126
146
}
0 commit comments