-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCertUtil.cs
377 lines (305 loc) · 12.8 KB
/
CertUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace HttpConfig
{
public class CertUtil
{
private CertUtil() {}
#region Methods
[DllImport("Cryptui.dll", SetLastError=true, CharSet=CharSet.Unicode, EntryPoint="CryptUIDlgSelectCertificateW")]
public unsafe static extern IntPtr CryptUIDlgSelectCertificate(
[In] IntPtr pcsc); // PCCRYPTUI_SELECTCERTIFICATE_STRUCT
[DllImport("Cryptui.dll", SetLastError=true, CharSet=CharSet.Unicode)]
public static extern bool CryptUIDlgViewContext(
ContextType dwContextType,
IntPtr pvContext,
IntPtr hwnd,
string pwszTitle,
int dwFlags,
IntPtr pvReserved);
[DllImport("Crypt32.dll")]
public static extern bool CertCloseStore(
IntPtr hCertStore,
int dwFlags);
[DllImport("Crypt32.dll", SetLastError=true)]
public static extern bool CertFreeCertificateContext(
IntPtr pCertContext);
[DllImport("Crypt32.dll", SetLastError=true)]
public static extern bool CertGetCertificateContextProperty(
IntPtr pCertContext,
int dwPropId,
IntPtr pvData,
ref int pcbData);
[DllImport("Crypt32.dll", CharSet=CharSet.Ansi)]
public static extern IntPtr CertOpenStore(
IntPtr lpszStoreProvider,
int dwMsgAndCertEncodingType,
int hCryptProv,
int dwFlags,
string pvPara);
[DllImport("Crypt32.dll", SetLastError=true, EntryPoint="CertGetNameStringW")]
public static extern int CertGetNameString(
IntPtr pCertContext,
CertNameType dwType,
int dwFlags,
IntPtr pvTypePara,
IntPtr pszNameString,
int cchNameString);
[DllImport("Crypt32.dll", SetLastError=true)]
public static extern IntPtr CertFindCertificateInStore(
IntPtr hCertStore,
int dwCertEncodingType,
int dwFindFlags,
int dwFindType,
IntPtr pvFindPara,
IntPtr pPrevCertContext);
[DllImport("Kernel32.dll", EntryPoint="RtlZeroMemory")]
public static extern void ZeroMemory(IntPtr dest, int size);
#endregion Methods
/*
DWORD dwSize;
HWND hwndParent;
DWORD dwFlags;
LPCTSTR szTitle;
DWORD dwDontUseColumn;
LPCTSTR szDisplayString;
PFNCFILTERPROC pFilterCallback;
PFNCCERTDISPLAYPROC pDisplayCallback;
void* pvCallbackData;
DWORD cDisplayStores;
HCERTSTORE* rghDisplayStores;
DWORD cStores;
HCERTSTORE* rghStores;
DWORD cPropSheetPages;
LPCPROPSHEETPAGE rgPropSheetPages;
HCERTSTORE hSelectedCertStore;
*/
#region Structures
[StructLayout(LayoutKind.Sequential)]
public struct CRYPTUI_SELECTCERTIFICATE_STRUCT
{
public uint dwSize;
public IntPtr hwndParent;
public uint dwFlags;
[MarshalAs(UnmanagedType.LPWStr)]
public string szTitle;
public uint dwDontUseColumn;
[MarshalAs(UnmanagedType.LPWStr)]
public string szDisplayString;
public IntPtr pFilterCallback; // PFNCFILTERPROC
public IntPtr pDisplayCallback; // PFNCCERTDISPLAYPROC
public IntPtr pvCallbackData; // void*
public uint cDisplayStores;
public IntPtr rghDisplayStores; // HCERTSTORE*
public uint cStores;
public IntPtr rghStores; //HCERTSTORE*
public uint cPropSheetPages;
public IntPtr rgPropSheetPages; // LPCPROPSHEETPAGE
public IntPtr hSelectedCertStore; // HCERTSTORE
}
[StructLayout(LayoutKind.Sequential)]
public struct CRYPTOAPI_BLOB
{
public int cbData;
public IntPtr pbData;
}
[StructLayout(LayoutKind.Sequential)]
public struct CERT_CONTEXT
{
public int dwCertEncodingType;
public IntPtr pbCertEncoded;
public int cbCertEncoded;
public IntPtr pCertInfo; //PCERT_INFO
public IntPtr hCertStore;
}
[StructLayout(LayoutKind.Sequential)]
public struct CERT_INFO
{
public int dwVersion;
public CRYPTOAPI_BLOB SerialNumber;
public CRYPT_ALGORITHM_IDENTIFIER SignatureAlgorithm;
public CRYPTOAPI_BLOB Issuer;
public FILETIME NotBefore;
public FILETIME NotAfter;
public CRYPTOAPI_BLOB Subject;
public CERT_PUBLIC_KEY_INFO SubjectPublicKeyInfo;
public CRYPTOAPI_BLOB IssuerUniqueId;
public CRYPTOAPI_BLOB SubjectUniqueId;
public int cExtension;
public IntPtr rgExtension; // PCERT_EXTENSION
}
[StructLayout(LayoutKind.Sequential)]
public struct CRYPT_ALGORITHM_IDENTIFIER
{
[MarshalAs(UnmanagedType.LPStr)] public string pszObjId;
public CRYPTOAPI_BLOB Parameters;
}
[StructLayout(LayoutKind.Sequential)]
public struct FILETIME
{
public uint dwLowDateTime;
public uint dwHighDateTime;
}
[StructLayout(LayoutKind.Sequential)]
public struct CERT_PUBLIC_KEY_INFO
{
public CRYPT_ALGORITHM_IDENTIFIER Algorithm;
public CRYPTOAPI_BLOB PublicKey;
}
#endregion Structures
#region Enumerations
public enum ContextType : int
{
Certificate = 1,
Crl = 2,
Ctl = 3
}
public enum CertStoreLocation : int
{
CurrentUser = 0x00010000,
LocalMachine = 0x00020000
}
public enum CryptEncoding : int
{
CRYPT_ASN_ENCODING = 0x00000001,
CRYPT_NDR_ENCODING = 0x00000002,
X509_ASN_ENCODING = 0x00000001,
X509_NDR_ENCODING = 0x00000002,
PKCS_7_ASN_ENCODING = 0x00010000,
PKCS_7_NDR_ENCODING = 0x00020000
}
public enum CertNameType : int
{
CERT_NAME_EMAIL_TYPE = 1,
CERT_NAME_RDN_TYPE = 2,
CERT_NAME_ATTR_TYPE = 3,
CERT_NAME_SIMPLE_DISPLAY_TYPE = 4,
CERT_NAME_FRIENDLY_DISPLAY_TYPE = 5,
CERT_NAME_DNS_TYPE = 6,
CERT_NAME_URL_TYPE = 7,
CERT_NAME_UPN_TYPE = 8
}
#endregion Enumerations
public static readonly IntPtr X509_UNICODE_NAME = new IntPtr(20);
public static readonly int CERT_SHA1_HASH_PROP_ID = 3;
public static readonly IntPtr CERT_STORE_PROV_SYSTEM_A = new IntPtr(9);
public static readonly IntPtr CERT_STORE_PROV_SYSTEM_W = new IntPtr(10);
public static readonly int CERT_FIND_HASH = 0x00010000;
public static readonly string UNKNOWN_CERT_NAME = "<UNKNOWN>";
public static byte[] GetCertHash(IntPtr pCert)
{
IntPtr pHash = IntPtr.Zero;
int hashLength = 0;
try
{
if(!CertGetCertificateContextProperty(pCert, CERT_SHA1_HASH_PROP_ID, pHash, ref hashLength))
throw new Exception("CertGetCertificateContextProperty failed. Error = " + Marshal.GetLastWin32Error());
pHash = Marshal.AllocHGlobal(hashLength);
if(!CertGetCertificateContextProperty(pCert, CERT_SHA1_HASH_PROP_ID, pHash, ref hashLength))
throw new Exception("CertGetCertificateContextProperty failed. Error = " + Marshal.GetLastWin32Error());
byte[] hash = new byte[hashLength];
Marshal.Copy(pHash, hash, 0, hashLength);
return hash;
}
finally
{
if(pHash != IntPtr.Zero)
Marshal.FreeHGlobal(pHash);
}
}
public static string BytesToHex(byte[] bytes)
{
StringBuilder hex = new StringBuilder(bytes.Length * 2);
foreach(byte b in bytes)
hex.AppendFormat("{0:X2}", b);
return hex.ToString();
}
public static string GetCertNameAttribute(IntPtr pCert, CertNameType nameType)
{
IntPtr pName = IntPtr.Zero;
int length = 0;
length = CertGetNameString(pCert, nameType, 0, IntPtr.Zero, pName, length);
if(length == 1)
return "";
pName = Marshal.AllocHGlobal(length * 2);
length = CertGetNameString(pCert, nameType, 0, IntPtr.Zero, pName, length);
string name = Marshal.PtrToStringUni(pName, length - 1);
Marshal.FreeHGlobal(pName);
return name;
}
public static string GetCertNameFromStoreAndHash(string storeName, byte[] certHash)
{
IntPtr hCertStore = IntPtr.Zero;
IntPtr hCert = IntPtr.Zero;
IntPtr pBlob = IntPtr.Zero;
IntPtr pHash = IntPtr.Zero;
string name = UNKNOWN_CERT_NAME;
try
{
hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, 0, (int)CertStoreLocation.LocalMachine, storeName);
if(hCertStore == IntPtr.Zero)
return name;
pHash = Marshal.AllocHGlobal(certHash.Length);
Marshal.Copy(certHash, 0, pHash, certHash.Length);
CRYPTOAPI_BLOB hashBlob = new CRYPTOAPI_BLOB();
hashBlob.cbData = certHash.Length;
hashBlob.pbData = pHash;
pBlob = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CRYPTOAPI_BLOB)));
Marshal.StructureToPtr(hashBlob, pBlob, false);
hCert = CertFindCertificateInStore(hCertStore, 0, 0, CERT_FIND_HASH, pBlob, IntPtr.Zero);
if(hCert == IntPtr.Zero)
return name;
name = GetCertNameAttribute(hCert, CertNameType.CERT_NAME_FRIENDLY_DISPLAY_TYPE);
return name;
}
finally
{
if(pBlob != IntPtr.Zero)
Marshal.FreeHGlobal(pBlob);
if(pHash != IntPtr.Zero)
Marshal.FreeHGlobal(pHash);
if(hCert != IntPtr.Zero)
CertFreeCertificateContext(hCert);
if(hCertStore != IntPtr.Zero)
CertCloseStore(hCertStore, 0);
}
}
public static void ViewCertificate(string storeName, byte[] certHash, IntPtr hwnd)
{
IntPtr hCertStore = IntPtr.Zero;
IntPtr hCert = IntPtr.Zero;
IntPtr pBlob = IntPtr.Zero;
IntPtr pHash = IntPtr.Zero;
try
{
hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM_A, 0, 0, (int)CertStoreLocation.LocalMachine, storeName);
if(hCertStore == IntPtr.Zero)
throw new Exception("CertOpenStore failed. Error = " + Marshal.GetLastWin32Error().ToString());
pHash = Marshal.AllocHGlobal(certHash.Length);
Marshal.Copy(certHash, 0, pHash, certHash.Length);
CRYPTOAPI_BLOB hashBlob = new CRYPTOAPI_BLOB();
hashBlob.cbData = certHash.Length;
hashBlob.pbData = pHash;
pBlob = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CRYPTOAPI_BLOB)));
Marshal.StructureToPtr(hashBlob, pBlob, false);
hCert = CertFindCertificateInStore(hCertStore, 0, 0, CERT_FIND_HASH, pBlob, IntPtr.Zero);
if(hCert == IntPtr.Zero)
throw new Exception("CertFindCertificateInStore failed. Error = " + Marshal.GetLastWin32Error().ToString());
string name = GetCertNameAttribute(hCert, CertNameType.CERT_NAME_FRIENDLY_DISPLAY_TYPE);
CryptUIDlgViewContext(ContextType.Certificate, hCert, hwnd, name, 0, IntPtr.Zero);
}
finally
{
if(pBlob != IntPtr.Zero)
Marshal.FreeHGlobal(pBlob);
if(pHash != IntPtr.Zero)
Marshal.FreeHGlobal(pHash);
if(hCert != IntPtr.Zero)
CertFreeCertificateContext(hCert);
if(hCertStore != IntPtr.Zero)
CertCloseStore(hCertStore, 0);
}
}
}
}