Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make System.Security.Cryptography.X509Certificates compliant with interop guidelines - part 3 #61435

Merged
merged 2 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
internal static unsafe partial bool CryptAcquireContext(out IntPtr psafeProvHandle,
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,45 @@
// 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 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,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,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;
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
internal static unsafe partial SafeCertContextHandle CertFindCertificateInStore(SafeCertStoreHandle hCertStore,
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,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;
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'd put all of the contents of CERT_CHAIN_PARA.cs into this file, since there's no need for it with any other function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
internal static unsafe partial bool CertGetCertificateChain(IntPtr hChainEngine,
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);
}
}
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.

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
internal static unsafe partial bool CertGetIntendedKeyUsage(CertEncodingType dwCertEncodingType,
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,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;
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static partial bool CertSaveStore(SafeCertStoreHandle hCertStore,
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,
}
}
}
Loading