Skip to content

Commit fa06511

Browse files
Use CsWin32
1 parent 571d0a2 commit fa06511

File tree

6 files changed

+399
-658
lines changed

6 files changed

+399
-658
lines changed

src/Files.App.CsWin32/NativeMethods.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,35 @@ GetMenuItemCount
236236
GetMenuItemInfo
237237
IsWow64Process2
238238
GetCurrentProcess
239+
CertFreeCertificateContext
240+
CryptMsgGetParam
241+
CryptMsgClose
242+
CryptMsgOpenToDecode
243+
CryptMsgUpdate
244+
CertOpenStore
245+
CryptDecodeObject
246+
CertFindCertificateInStore
247+
CertComparePublicKeyInfo
248+
CryptQueryObject
249+
CertCloseStore
250+
WinVerifyTrust
251+
FileTimeToSystemTime
252+
FileTimeToLocalFileTime
253+
SystemTimeToFileTime
254+
CRYPTOAPI_BLOB
255+
CMSG_SIGNER_INFO
256+
SignDataHandle
257+
CRYPT_ATTRIBUTE
258+
FILETIME
259+
CRYPT_BIT_BLOB
260+
CERT_ALT_NAME_INFO
261+
CERT_CONTEXT
262+
CERT_INFO
263+
CRYPT_ALGORITHM_IDENTIFIER
264+
CERT_PUBLIC_KEY_INFO
265+
CATALOG_INFO
266+
WINTRUST_FILE_INFO
267+
WINTRUST_DATA
268+
HCERTSTORE
269+
HCRYPTMSG
270+
CERT_QUERY_ENCODING_TYPE

src/Files.App/Data/Models/SignatureInfoItem.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
using Files.App.Utils.Signatures;
55
using System.Windows.Input;
6+
using Windows.Win32.Foundation;
67

78
namespace Files.App.Data.Models
89
{
910
public sealed partial class SignatureInfoItem : ObservableObject
1011
{
1112
private readonly string _fileName;
1213

13-
private readonly IntPtr _hwndParent;
14+
private readonly HWND _hwndParent;
1415

1516
private readonly int _index;
1617

@@ -73,7 +74,7 @@ public bool Verified
7374

7475
public ICommand OpenDetailsCommand { get; }
7576

76-
public SignatureInfoItem(string fileName, int index, IntPtr hWnd, List<CertNodeInfoItem> chain)
77+
public SignatureInfoItem(string fileName, int index, HWND hWnd, List<CertNodeInfoItem> chain)
7778
{
7879
_fileName = fileName;
7980
_hwndParent = hWnd;

src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs

Lines changed: 6 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Runtime.InteropServices;
66
using System.Text;
77
using Windows.Win32.Foundation;
8+
using Windows.Win32.Security.Cryptography;
89
using Windows.Win32.System.Com;
910

1011
namespace Files.App.Helpers
@@ -349,138 +350,17 @@ out IntPtr pszPath
349350

350351
// crypt32.dll
351352
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
352-
public static extern uint CertGetNameStringA(
353-
IntPtr pCertContext,
353+
public unsafe static extern uint CertGetNameStringA(
354+
CERT_CONTEXT* pCertContext,
354355
uint dwType,
355356
uint dwFlags,
356-
IntPtr pvTypePara,
357-
IntPtr pszNameString,
357+
void* pvTypePara,
358+
PCSTR pszNameString,
358359
uint cchNameString
359360
);
360361

361-
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
362-
public static extern bool CertFreeCertificateContext(
363-
IntPtr pCertContext
364-
);
365-
366-
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
367-
public static extern bool CryptMsgGetParam(
368-
IntPtr hCryptMsg,
369-
uint dwParamType,
370-
uint dwIndex,
371-
IntPtr pParam,
372-
ref uint dwOutSize
373-
);
374-
375-
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
376-
public static extern bool CryptMsgClose(
377-
IntPtr hCryptMsg
378-
);
379-
380-
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
381-
public static extern IntPtr CryptMsgOpenToDecode(
382-
uint dwMsgEncodingType,
383-
uint dwFlags,
384-
uint dwMsgType,
385-
IntPtr hCryptProv,
386-
IntPtr pRecipientInfo,
387-
IntPtr pStreamInfo
388-
);
389-
390-
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
391-
public static extern bool CryptMsgUpdate(
392-
IntPtr hCryptMsg,
393-
IntPtr pbData,
394-
uint cbDatam,
395-
bool fFinal
396-
);
397-
398-
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
399-
public static extern IntPtr CertOpenStore(
400-
IntPtr lpszStoreProvider,
401-
uint dwEncodingType,
402-
IntPtr hCryptProv,
403-
uint dwFlags,
404-
IntPtr pvPara
405-
);
406-
407-
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
408-
public static extern bool CryptDecodeObject(
409-
uint dwCertEncodingType,
410-
IntPtr lpszStructType,
411-
IntPtr pbEncoded,
412-
uint cbEncoded,
413-
uint dwFlags,
414-
IntPtr pvStructInfo,
415-
ref uint pcbStructInfo
416-
);
417-
418-
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
419-
public static extern IntPtr CertFindCertificateInStore(
420-
IntPtr hCertStore,
421-
uint dwCertEncodingType,
422-
uint dwFindFlags,
423-
uint dwFindType,
424-
IntPtr pvFindPara,
425-
IntPtr pPrevCertContext
426-
);
427-
428-
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
429-
public static extern bool CertComparePublicKeyInfo(
430-
uint dwCertEncodingType,
431-
IntPtr pPublicKey1,
432-
IntPtr pPublicKey2
433-
);
434-
435-
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
436-
public static extern bool CryptQueryObject(
437-
uint dwObjectType,
438-
IntPtr pvObject,
439-
uint dwExpectedContentTypeFlags,
440-
uint dwExpectedFormatTypeFlags,
441-
uint dwFlags,
442-
ref uint pdwMsgAndCertEncodingType,
443-
ref uint pdwContentType,
444-
ref uint pdwFormatType,
445-
ref IntPtr phCertStore,
446-
ref IntPtr phMsg,
447-
ref IntPtr ppvContext
448-
);
449-
450-
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
451-
public static extern bool CertCloseStore(
452-
IntPtr hCertStore,
453-
uint dwFlags
454-
);
455-
456-
[DllImport("wintrust.dll")]
457-
public static extern long WinVerifyTrust(
458-
IntPtr hwnd,
459-
IntPtr pgActionID,
460-
IntPtr pWVTData
461-
);
462-
463-
// kernel32.dll
464-
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
465-
public static extern bool FileTimeToSystemTime(
466-
IntPtr lpFileTime,
467-
IntPtr lpSystemTime
468-
);
469-
470-
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
471-
public static extern bool FileTimeToLocalFileTime(
472-
IntPtr lpFileTime,
473-
IntPtr lpLocalFileTime
474-
);
475-
476-
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
477-
public static extern bool SystemTimeToFileTime(
478-
IntPtr lpSystemTime,
479-
IntPtr lpFileTime
480-
);
481-
482362
// cryptui.dll
483363
[DllImport("cryptui.dll", SetLastError = true, CharSet = CharSet.Auto)]
484-
public static extern bool CryptUIDlgViewSignerInfo(IntPtr pViewInfo);
364+
public unsafe static extern bool CryptUIDlgViewSignerInfo(CRYPTUI_VIEWSIGNERINFO_STRUCT* pViewInfo);
485365
}
486366
}

src/Files.App/Helpers/Win32/Win32PInvoke.Structs.cs

Lines changed: 16 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using System.IO;
55
using System.Runtime.InteropServices;
6+
using Windows.Win32.Foundation;
7+
using Windows.Win32.Security.Cryptography;
68

79
namespace Files.App.Helpers
810
{
@@ -199,159 +201,35 @@ public struct WIN32_FIND_DATA
199201
}
200202

201203
[StructLayout(LayoutKind.Sequential)]
202-
public struct CRYPTOAPI_BLOB
203-
{
204-
public uint cbData;
205-
public IntPtr pbData;
206-
}
207-
208-
[StructLayout(LayoutKind.Sequential)]
209-
public struct CMSG_SIGNER_INFO
210-
{
211-
public uint dwVersion;
212-
public CRYPTOAPI_BLOB Issuer;
213-
public CRYPTOAPI_BLOB SerialNumber;
214-
public CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm;
215-
public CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm;
216-
public CRYPTOAPI_BLOB EncryptedHash;
217-
public CRYPTOAPI_BLOB AuthAttrs;
218-
public CRYPTOAPI_BLOB UnauthAttrs;
219-
}
220-
221-
[StructLayout(LayoutKind.Sequential)]
222-
public struct SignDataHandle
204+
public unsafe struct SignDataHandle
223205
{
224206
public uint dwObjSize;
225-
public IntPtr pSignerInfo;
226-
public IntPtr hCertStoreHandle;
207+
public CMSG_SIGNER_INFO* pSignerInfo;
208+
public HCERTSTORE hCertStoreHandle;
227209
}
228210

229211
[StructLayout(LayoutKind.Sequential)]
230-
public struct CRYPT_ATTRIBUTE
212+
public unsafe struct CRYPTOAPI_BLOB
231213
{
232-
[MarshalAs(UnmanagedType.LPStr)]
233-
public string pszObjId;
234-
public uint cValue;
235-
public IntPtr rgValue;
236-
}
237-
238-
[StructLayout(LayoutKind.Sequential)]
239-
public struct FILETIME
240-
{
241-
public uint dwLowDateTime;
242-
public uint dwHighDateTime;
243-
}
244-
245-
[StructLayout(LayoutKind.Sequential)]
246-
public struct CRYPT_BIT_BLOB
247-
{
248-
private readonly uint cbData;
249-
private readonly IntPtr pbData;
250-
private readonly uint cUnusedBits;
251-
}
252-
253-
[StructLayout(LayoutKind.Sequential)]
254-
public struct CERT_ALT_NAME_INFO
255-
{
256-
public uint cAltEntry;
257-
public IntPtr rgAltEntry;
258-
}
259-
260-
[StructLayout(LayoutKind.Sequential)]
261-
public struct CERT_CONTEXT
262-
{
263-
public uint dwCertEncodingType;
264-
public IntPtr pbCertEncoded;
265-
public uint cbCertEncoded;
266-
public IntPtr pCertInfo;
267-
public IntPtr hCertStore;
268-
}
269-
270-
[StructLayout(LayoutKind.Sequential)]
271-
public struct CERT_INFO
272-
{
273-
public uint dwVersion;
274-
public CRYPTOAPI_BLOB SerialNumber;
275-
public CRYPT_ALGORITHM_IDENTIFIER SignatureAlgorithm;
276-
public CRYPTOAPI_BLOB Issuer;
277-
public FILETIME NotBefore;
278-
public FILETIME NotAfter;
279-
public CRYPTOAPI_BLOB Subject;
280-
public CERT_PUBLIC_KEY_INFO SubjectPublicKeyInfo;
281-
public CRYPT_BIT_BLOB IssuerUniqueId;
282-
public CRYPT_BIT_BLOB SubjectUniqueId;
283-
public uint cExtension;
284-
public IntPtr rgExtension;
285-
}
286-
287-
[StructLayout(LayoutKind.Sequential)]
288-
public struct CRYPT_ALGORITHM_IDENTIFIER
289-
{
290-
[MarshalAs(UnmanagedType.LPStr)]
291-
public string pszObjId;
292-
public CRYPTOAPI_BLOB Parameters;
293-
}
294-
295-
[StructLayout(LayoutKind.Sequential)]
296-
public struct CERT_PUBLIC_KEY_INFO
297-
{
298-
public CRYPT_ALGORITHM_IDENTIFIER Algorithm;
299-
public CRYPTOAPI_BLOB PublicKey;
300-
}
301-
302-
[StructLayout(LayoutKind.Sequential)]
303-
public struct CATALOG_INFO
304-
{
305-
public uint cbStruct;
306-
public char[] wszCatalogFile = new char[256];
307-
308-
public CATALOG_INFO()
309-
{
310-
}
311-
}
312-
313-
[StructLayout(LayoutKind.Sequential)]
314-
public struct WINTRUST_FILE_INFO
315-
{
316-
public uint cbStruct;
317-
public IntPtr pcwszFilePath;
318-
public IntPtr hFile;
319-
public IntPtr pgKnownSubject;
320-
}
321-
322-
[StructLayout(LayoutKind.Sequential)]
323-
public struct WINTRUST_DATA
324-
{
325-
public uint cbStruct;
326-
public IntPtr pPolicyCallbackData;
327-
public IntPtr pSIPClientData;
328-
public uint dwUIChoice;
329-
public uint fdwRevocationChecks;
330-
public uint dwUnionChoice;
331-
public IntPtr pFile;
332-
public uint dwStateAction;
333-
public IntPtr hVWTStateData;
334-
public IntPtr pwszURLReference;
335-
public uint dwProvFlags;
336-
public uint dwUIContext;
337-
public IntPtr pSignatureSettings;
214+
public uint cbData;
215+
public void* pbData;
338216
}
339217

340218
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
341-
public struct CRYPTUI_VIEWSIGNERINFO_STRUCT
219+
public unsafe struct CRYPTUI_VIEWSIGNERINFO_STRUCT
342220
{
343221
public uint dwSize;
344-
public IntPtr hwndParent;
222+
public HWND hwndParent;
345223
public uint dwFlags;
346-
public IntPtr szTitle;
347-
public IntPtr pSignerInfo;
348-
public IntPtr hMsg;
349-
public IntPtr pszOID;
224+
public PCSTR szTitle;
225+
public CMSG_SIGNER_INFO* pSignerInfo;
226+
public void* hMsg;
227+
public PCSTR pszOID;
350228
public uint? dwReserved;
351229
public uint cStores;
352-
public IntPtr rghStores;
230+
public HCERTSTORE* rghStores;
353231
public uint cPropPages;
354-
public IntPtr rgPropPages;
232+
public void* rgPropPages;
355233
}
356234
}
357235
}

0 commit comments

Comments
 (0)