Skip to content

Commit

Permalink
Make S.S.C.X509Certificates compliant with interop guidelines - part 3
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobsaila committed Nov 22, 2021
1 parent 0e9ce96 commit c334e55
Show file tree
Hide file tree
Showing 49 changed files with 738 additions and 310 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@

internal static partial class Interop
{
public static partial class cryptoapi
internal static partial class Advapi32
{
[GeneratedDllImport(Libraries.Advapi32, EntryPoint = "CryptAcquireContextW", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static unsafe partial bool CryptAcquireContext(out IntPtr psafeProvHandle, char* pszContainer, char* pszProvider, int dwProvType, Crypt32.CryptAcquireContextFlags dwFlags);
internal static unsafe partial bool CryptAcquireContext(
out IntPtr psafeProvHandle,
char* pszContainer,
char* pszProvider,
int dwProvType,
Interop.Crypt32.CryptAcquireContextFlags dwFlags);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Crypt32
{
[StructLayout(LayoutKind.Sequential)]
internal struct CERT_CHAIN_ENGINE_CONFIG
{
public int cbSize;
public IntPtr hRestrictedRoot;
public IntPtr hRestrictedTrust;
public IntPtr hRestrictedOther;
public int cAdditionalStore;
public IntPtr rghAdditionalStore;
public ChainEngineConfigFlags dwFlags;
public int dwUrlRetrievalTimeout;
public int MaximumCachedCertificates;
public int CycleDetectionModulus;
public IntPtr hExclusiveRoot;
public IntPtr hExclusiveTrustedPeople;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

internal static partial class Interop
{
internal static partial class Crypt32
{
[Flags]
internal enum CertChainFlags : int
{
None = 0x00000000,
CERT_CHAIN_DISABLE_AUTH_ROOT_AUTO_UPDATE = 0x00000100,
CERT_CHAIN_DISABLE_AIA = 0x00002000,
CERT_CHAIN_REVOCATION_CHECK_END_CERT = 0x10000000,
CERT_CHAIN_REVOCATION_CHECK_CHAIN = 0x20000000,
CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT = 0x40000000,
CERT_CHAIN_REVOCATION_CHECK_CACHE_ONLY = unchecked((int)0x80000000),
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class Crypt32
{
[GeneratedDllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static partial bool CertControlStore(SafeCertStoreHandle hCertStore, CertControlStoreFlags dwFlags, CertControlStoreType dwControlType, IntPtr pvCtrlPara);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

internal static partial class Interop
{
internal static partial class Crypt32
{
[Flags]
internal enum CertControlStoreFlags : int
{
None = 0x00000000,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

internal static partial class Interop
{
internal static partial class Crypt32
{
internal enum CertControlStoreType : int
{
CERT_STORE_CTRL_AUTO_RESYNC = 4,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class Crypt32
{
[GeneratedDllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static partial bool CertCreateCertificateChainEngine(ref CERT_CHAIN_ENGINE_CONFIG pConfig, out SafeChainEngineHandle hChainEngineHandle);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Crypt32
{
// Note: CertDeleteCertificateFromStore always calls CertFreeCertificateContext on pCertContext, even if an error is encountered.
[GeneratedDllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static unsafe partial bool CertDeleteCertificateFromStore(CERT_CONTEXT* pCertContext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class Crypt32
{
[GeneratedDllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static unsafe partial SafeCertContextHandle CertFindCertificateInStore(
SafeCertStoreHandle hCertStore,
CertEncodingType dwCertEncodingType,
CertFindFlags dwFindFlags,
CertFindType dwFindType,
void* pvFindPara,
CERT_CONTEXT* pPrevCertContext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Crypt32
{
[GeneratedDllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static unsafe partial CERT_EXTENSION* CertFindExtension([MarshalAs(UnmanagedType.LPStr)] string pszObjId, int cExtensions, IntPtr rgExtensions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

internal static partial class Interop
{
internal static partial class Crypt32
{
[Flags]
internal enum CertFindFlags : int
{
None = 0x00000000,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

internal static partial class Interop
{
internal static partial class Crypt32
{
internal enum CertFindType : int
{
CERT_FIND_SUBJECT_CERT = 0x000b0000,
CERT_FIND_HASH = 0x00010000,
CERT_FIND_SUBJECT_STR = 0x00080007,
CERT_FIND_ISSUER_STR = 0x00080004,
CERT_FIND_EXISTING = 0x000d0000,
CERT_FIND_ANY = 0x00000000,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Crypt32
{
[GeneratedDllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static partial void CertFreeCertificateChain(IntPtr pChainContext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Crypt32
{
[GeneratedDllImport(Libraries.Crypt32)]
internal static partial void CertFreeCertificateChainEngine(IntPtr hChainEngine);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class Crypt32
{
[GeneratedDllImport(Libraries.Crypt32, SetLastError = true)]
internal static unsafe partial bool CertGetCertificateChain(
IntPtr hChainEngine,
SafeCertContextHandle pCertContext,
FILETIME* pTime,
SafeCertStoreHandle hStore,
ref CERT_CHAIN_PARA pChainPara,
CertChainFlags dwFlags,
IntPtr pvReserved,
out SafeX509ChainHandle ppChainContext);

[StructLayout(LayoutKind.Sequential)]
internal unsafe struct CERT_CHAIN_PARA
{
public int cbSize;
public CERT_USAGE_MATCH RequestedUsage;
public CERT_USAGE_MATCH RequestedIssuancePolicy;
public int dwUrlRetrievalTimeout;
public int fCheckRevocationFreshnessTime;
public int dwRevocationFreshnessTime;
public FILETIME* pftCacheResync;
public int pStrongSignPara;
public int dwStrongSignFlags;
}

[StructLayout(LayoutKind.Sequential)]
internal struct CERT_USAGE_MATCH
{
public CertUsageMatchType dwType;
public CTL_USAGE Usage;
}

internal enum CertUsageMatchType : int
{
USAGE_MATCH_TYPE_AND = 0x00000000,
USAGE_MATCH_TYPE_OR = 0x00000001,
}

[StructLayout(LayoutKind.Sequential)]
internal struct CTL_USAGE
{
public int cUsageIdentifier;
public IntPtr rgpszUsageIdentifier;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Crypt32
{
// Note: It's somewhat unusual to use an API enum as a parameter type to a P/Invoke but in this case, X509KeyUsageFlags was intentionally designed as bit-wise
// identical to the wincrypt CERT_*_USAGE values.
[GeneratedDllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static unsafe partial bool CertGetIntendedKeyUsage(
CertEncodingType dwCertEncodingType,
CERT_INFO* pCertInfo,
out X509KeyUsageFlags pbKeyUsage,
int cbKeyUsage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class Crypt32
{
[GeneratedDllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)]
internal static unsafe partial bool CertGetValidUsages(int cCerts, ref SafeCertContextHandle rghCerts, out int cNumOIDs, void* rghOIDs, ref int pcbOIDs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class Crypt32
{
[GeneratedDllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)]
public static partial bool CertSaveStore(
SafeCertStoreHandle hCertStore,
CertEncodingType dwMsgAndCertEncodingType,
CertStoreSaveAs dwSaveAs,
CertStoreSaveTo dwSaveTo,
ref DATA_BLOB pvSaveToPara,
int dwFlags);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

internal static partial class Interop
{
internal static partial class Crypt32
{
internal enum CertStoreSaveAs : int
{
CERT_STORE_SAVE_AS_STORE = 1,
CERT_STORE_SAVE_AS_PKCS7 = 2,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

internal static partial class Interop
{
internal static partial class Crypt32
{
internal enum CertStoreSaveTo : int
{
CERT_STORE_SAVE_TO_MEMORY = 2
}
}
}
Loading

0 comments on commit c334e55

Please sign in to comment.