ps/Modules/Alkami.Ops.Common/NativeMethods/SafeNativeMethods.cs
2023-05-30 22:51:22 -07:00

56 lines
2.4 KiB
C#

using System;
using System.Runtime.InteropServices;
namespace Alkami.Ops.Common.NativeMethods
{
internal static class SafeNativeMethods
{
#region CRYPT32 - Certificates and Store Management
/// <summary>
/// This struct is used when working with PFX files
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct CRYPT_DATA_BLOB
{
public int cbData;
public IntPtr pbData;
}
[DllImport("CRYPT32", SetLastError = true)]
internal static extern Boolean PFXExportCertStoreEx(
IntPtr hCertStore,
ref CRYPT_DATA_BLOB pPFX,
[MarshalAs(UnmanagedType.LPWStr)] String szPassword,
IntPtr pvReserved,
uint dwFlags
);
internal const uint EXPORT_PRIVATE_KEYS = 0x0004;
[DllImport("CRYPT32")]
internal static extern bool PFXIsPFXBlob(ref CRYPT_DATA_BLOB pPfx);
[DllImport("CRYPT32", SetLastError = true)]
internal static extern Boolean CertAddCertificateContextToStore(IntPtr hCertStore, IntPtr pCertContext, Int32 dwAddDisposition, ref IntPtr ppStoreContext);
[DllImport("CRYPT32", EntryPoint = "CertAddEncodedCertificateToStore", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CertAddEncodedCertificateToStore(IntPtr certStore, int certEncodingType, byte[] certEncoded, int certEncodedLength, int addDisposition, IntPtr certContext);
[DllImport("CRYPT32", EntryPoint = "CertCloseStore", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool CertCloseStore(IntPtr storeProvider, int flags);
[DllImport("CRYPT32", EntryPoint = "CertEnumCertificatesInStore", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern IntPtr CertEnumCertificatesInStore(IntPtr storeProvider, IntPtr prevCertContext);
[DllImport("CRYPT32", EntryPoint = "CertOpenStore", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern IntPtr CertOpenStore(int storeProvider, int encodingType, int hcryptProv, int flags, string pvPara);
[DllImport("CRYPT32", SetLastError = true)]
internal static extern IntPtr PFXImportCertStore(ref CRYPT_DATA_BLOB pPfx, [MarshalAs(UnmanagedType.LPWStr)] String szPassword, uint dwFlags);
#endregion CRYPT32 - Certificates and Store Management
}
}