Skip to content

Commit 3eb5fa4

Browse files
authored
Merge branch 'master' into NDES
2 parents aa62989 + 75109f6 commit 3eb5fa4

File tree

11 files changed

+262
-1325
lines changed

11 files changed

+262
-1325
lines changed

ADCS.CertMod.Managed/ADCS.CertMod.Managed.csproj

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@
4444
<SourceRoot Include="$(MSBuildThisFileDirectory)/" />
4545
</ItemGroup>
4646
<ItemGroup>
47-
<Reference Include="CERTCLILib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d5db31a0b7668d81, processorArchitecture=MSIL">
48-
<SpecificVersion>False</SpecificVersion>
49-
<EmbedInteropTypes>False</EmbedInteropTypes>
50-
<HintPath>..\lib\CERTCLILib.dll</HintPath>
51-
</Reference>
5247
<Reference Include="System" />
5348
<Reference Include="System.Core" />
5449
<Reference Include="System.Security" />
@@ -75,6 +70,9 @@
7570
<Compile Include="NDES\SCEPChallengeStoreEntry.cs" />
7671
<Compile Include="NDES\SCEPDisposition.cs" />
7772
<Compile Include="NDES\SCEPFailInfo.cs" />
73+
<Compile Include="Interop\CertServerComFactory.cs" />
74+
<Compile Include="Interop\ICertServerExit.cs" />
75+
<Compile Include="Interop\ICertServerPolicy.cs" />
7876
<Compile Include="ObjectPool.cs" />
7977
<Compile Include="Policy\CertServerPolicyManaged.cs" />
8078
<Compile Include="PrivateKeyFlags.cs" />
@@ -115,5 +113,6 @@
115113
<ItemGroup>
116114
<None Include="strongname.snk" />
117115
</ItemGroup>
116+
<ItemGroup />
118117
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
119118
</Project>

ADCS.CertMod.Managed/CertServerExitPolicyManaged.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using System;
2-
using CERTCLILib;
2+
using ADCS.CertMod.Managed.Interop;
33

44
namespace ADCS.CertMod.Managed;
55

66
abstract class CertServerExitPolicyManaged : ICertServerModule {
77
readonly Action<Int32> _setContext;
8-
readonly Func<String, Int32, IntPtr, Object> _getRequestProperty;
8+
readonly Func<String, Int32, IntPtr, Int32> _getRequestProperty;
99
readonly Func<String, String> _getRequestAttribute;
10-
readonly Func<String, Int32, IntPtr, Object> _getCertificateProperty;
11-
readonly Func<String, Int32, IntPtr, Object> _getCertificateExtension;
10+
readonly Func<String, Int32, IntPtr, Int32> _getCertificateProperty;
11+
readonly Func<String, Int32, IntPtr, Int32> _getCertificateExtension;
1212
readonly Func<Int32> _getCertificateExtensionFlags;
1313
readonly Action<Int32> _enumerateExtensionsSetup;
1414
readonly Func<String> _enumerateExtensions;
@@ -55,14 +55,14 @@ protected CertServerExitPolicyManaged(ICertServerPolicy comClass) {
5555
public void SetContext(Int32 Context) {
5656
_setContext.Invoke(Context);
5757
}
58-
public void GetRequestProperty(String strPropertyName, Int32 PropertyType, IntPtr pvarPropertyValue) {
59-
_getRequestProperty.Invoke(strPropertyName, PropertyType, pvarPropertyValue);
58+
public Int32 GetRequestProperty(String strPropertyName, Int32 PropertyType, IntPtr pvarPropertyValue) {
59+
return _getRequestProperty.Invoke(strPropertyName, PropertyType, pvarPropertyValue);
6060
}
6161
public String GetRequestAttribute(String strAttributeName) {
6262
return _getRequestAttribute.Invoke(strAttributeName);
6363
}
64-
public void GetCertificateProperty(String strPropertyName, Int32 PropertyType, IntPtr pvarPropertyValue) {
65-
_getCertificateProperty.Invoke(strPropertyName, PropertyType, pvarPropertyValue);
64+
public Int32 GetCertificateProperty(String strPropertyName, Int32 PropertyType, IntPtr pvarPropertyValue) {
65+
return _getCertificateProperty.Invoke(strPropertyName, PropertyType, pvarPropertyValue);
6666
}
6767
public void GetCertificateExtension(String strExtensionName, Int32 Type, IntPtr pvarValue) {
6868
_getCertificateExtension.Invoke(strExtensionName, Type, pvarValue);
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using CERTCLILib;
1+
using ADCS.CertMod.Managed.Interop;
22

33
namespace ADCS.CertMod.Managed.Exit;
44

5-
class CertServerExitManaged : CertServerExitPolicyManaged {
6-
public CertServerExitManaged() : base(new CCertServerExitClass()) { }
7-
}
5+
class CertServerExitManaged() : CertServerExitPolicyManaged(CertServerComFactory.CreateCertServerExit());

ADCS.CertMod.Managed/Extensions/CertServerModuleExtensions.cs

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,59 @@ namespace ADCS.CertMod.Managed.Extensions;
55

66
static class CertServerModuleExtensions {
77
#region private helpers
8-
9-
public static T GetInSubjectProperty<T>(this ICertServerModule certExit, IntPtr pvarPropertyValue, RequestSubjectName subjectName) {
10-
certExit.getScalarProperty(pvarPropertyValue, "Subject." + subjectName, out T retValue);
8+
9+
public static T GetInSubjectProperty<T>(this ICertServerModule certServerModule, IntPtr pvarPropertyValue, RequestSubjectName subjectName) {
10+
certServerModule.getScalarProperty(pvarPropertyValue, "Subject." + subjectName, out T retValue);
1111

1212
return retValue;
1313
}
14-
public static T GetOutSubjectProperty<T>(this ICertServerModule certExit, IntPtr pvarPropertyValue, RequestSubjectName subjectName) {
15-
certExit.getScalarProperty(pvarPropertyValue, subjectName.ToString(), out T retValue);
14+
public static T GetOutSubjectProperty<T>(this ICertServerModule certServerModule, IntPtr pvarPropertyValue, RequestSubjectName subjectName) {
15+
certServerModule.getScalarProperty(pvarPropertyValue, subjectName.ToString(), out T retValue);
1616

1717
return retValue;
1818
}
1919

20-
public static Byte[] GetInSubjectNameBin(this ICertServerModule certExit, IntPtr pvarPropertyValue, RequestSubjectName propertyName) {
21-
return certExit.getBinaryProperty(pvarPropertyValue, "Subject." + propertyName);
20+
public static Byte[] GetInSubjectNameBin(this ICertServerModule certServerModule, IntPtr pvarPropertyValue, RequestSubjectName propertyName) {
21+
return certServerModule.getBinaryProperty(pvarPropertyValue, "Subject." + propertyName);
2222
}
23-
public static Byte[] GetOutSubjectNameBin(this ICertServerModule certExit, IntPtr pvarPropertyValue, RequestSubjectName propertyName) {
24-
return certExit.getBinaryProperty(pvarPropertyValue, propertyName.ToString());
23+
public static Byte[] GetOutSubjectNameBin(this ICertServerModule certServerModule, IntPtr pvarPropertyValue, RequestSubjectName propertyName) {
24+
return certServerModule.getBinaryProperty(pvarPropertyValue, propertyName.ToString());
2525
}
2626

27-
public static T GetRequestProperty<T>(this ICertServerModule certExit, IntPtr pvarPropertyValue, RequestPropertyName propertyName) {
28-
certExit.getScalarProperty(pvarPropertyValue, propertyName.ToString(), out T retValue);
27+
public static T GetRequestProperty<T>(this ICertServerModule certServerModule, IntPtr pvarPropertyValue, RequestPropertyName propertyName) {
28+
certServerModule.getScalarProperty(pvarPropertyValue, propertyName.ToString(), out T retValue);
2929

3030
return retValue;
3131
}
32-
public static Byte[] GetRequestPropertyBin(this ICertServerModule certExit, IntPtr pvarPropertyValue, RequestPropertyName propertyName) {
33-
return certExit.getBinaryProperty(pvarPropertyValue, propertyName.ToString());
32+
public static Byte[] GetRequestPropertyBin(this ICertServerModule certServerModule, IntPtr pvarPropertyValue, RequestPropertyName propertyName) {
33+
return certServerModule.getBinaryProperty(pvarPropertyValue, propertyName.ToString());
3434
}
3535

36-
public static T GetCertProperty<T>(this ICertServerModule certExit, IntPtr pvarPropertyValue, CertificatePropertyName propertyName) {
37-
certExit.getScalarProperty(pvarPropertyValue, propertyName.ToString(), out T retValue, true);
36+
public static T GetCertProperty<T>(this ICertServerModule certServerModule, IntPtr pvarPropertyValue, CertificatePropertyName propertyName) {
37+
certServerModule.getScalarProperty(pvarPropertyValue, propertyName.ToString(), out T retValue, true);
3838

3939
return retValue;
4040
}
41-
public static Byte[] GetCertPropertyBin(this ICertServerModule certExit, IntPtr pvarPropertyValue, CertificatePropertyName propertyName) {
42-
return certExit.getBinaryProperty(pvarPropertyValue, propertyName.ToString(), true);
41+
public static Byte[] GetCertPropertyBin(this ICertServerModule certServerModule, IntPtr pvarPropertyValue, CertificatePropertyName propertyName) {
42+
return certServerModule.getBinaryProperty(pvarPropertyValue, propertyName.ToString(), true);
4343
}
4444

45-
public static Int32? GetLongProperty(this ICertServerModule certExit, IntPtr pvarPropertyValue, String propertyName, Boolean cert = false) {
46-
if (certExit.getScalarProperty(pvarPropertyValue, propertyName, out Int32 retValue, cert)) {
45+
public static Int32? GetLongProperty(this ICertServerModule certServerModule, IntPtr pvarPropertyValue, String propertyName, Boolean cert = false) {
46+
if (certServerModule.getScalarProperty(pvarPropertyValue, propertyName, out Int32 retValue, cert)) {
4747
return retValue;
4848
}
4949

5050
return null;
5151
}
52-
public static DateTime? GetDateTimeProperty(this ICertServerModule certExit, IntPtr pvarPropertyValue, String propertyName, Boolean cert = false) {
53-
if (certExit.getScalarProperty(pvarPropertyValue, propertyName, out DateTime retValue, cert)) {
52+
public static DateTime? GetDateTimeProperty(this ICertServerModule certServerModule, IntPtr pvarPropertyValue, String propertyName, Boolean cert = false) {
53+
if (certServerModule.getScalarProperty(pvarPropertyValue, propertyName, out DateTime retValue, cert)) {
5454
return retValue;
5555
}
5656

5757
return null;
5858
}
5959

60-
static Boolean getScalarProperty<T>(this ICertServerModule certExit, IntPtr pvarPropertyValue, String propertyName, out T retValue, Boolean cert = false) {
60+
static Boolean getScalarProperty<T>(this ICertServerModule certServerModule, IntPtr pvarPropertyValue, String propertyName, out T retValue, Boolean cert = false) {
6161
retValue = default;
6262
Type leftType = typeof(T);
6363
Int32 propType;
@@ -72,36 +72,31 @@ static Boolean getScalarProperty<T>(this ICertServerModule certExit, IntPtr pvar
7272
return false;
7373
}
7474

75-
try {
76-
if (cert) {
77-
certExit.GetCertificateProperty(propertyName, propType, pvarPropertyValue);
78-
} else {
79-
certExit.GetRequestProperty(propertyName, propType, pvarPropertyValue);
80-
}
81-
82-
retValue = (T)Marshal.GetObjectForNativeVariant(pvarPropertyValue);
83-
OleAut32.VariantClear(pvarPropertyValue);
84-
85-
return true;
86-
} catch {
75+
Int32 hresult = cert
76+
? certServerModule.GetCertificateProperty(propertyName, propType, pvarPropertyValue)
77+
: certServerModule.GetRequestProperty(propertyName, propType, pvarPropertyValue);
78+
if (hresult != 0) {
8779
return false;
8880
}
81+
82+
retValue = (T)Marshal.GetObjectForNativeVariant(pvarPropertyValue);
83+
OleAut32.VariantClear(pvarPropertyValue);
84+
85+
return true;
8986
}
90-
static Byte[] getBinaryProperty(this ICertServerModule certExit, IntPtr pvarPropertyValue, String propertyName, Boolean cert = false) {
91-
try {
92-
if (cert) {
93-
certExit.GetCertificateProperty(propertyName, CertSrvH.PROPTYPE_BINARY, pvarPropertyValue);
94-
} else {
95-
certExit.GetRequestProperty(propertyName, CertSrvH.PROPTYPE_BINARY, pvarPropertyValue);
96-
}
97-
98-
Byte[] retValue = pvarPropertyValue.GetBstrBinary(null);
99-
OleAut32.VariantClear(pvarPropertyValue);
87+
static Byte[] getBinaryProperty(this ICertServerModule certServerModule, IntPtr pvarPropertyValue, String propertyName, Boolean cert = false) {
88+
Int32 hresult = cert
89+
? certServerModule.GetCertificateProperty(propertyName, CertSrvH.PROPTYPE_BINARY, pvarPropertyValue)
90+
: certServerModule.GetRequestProperty(propertyName, CertSrvH.PROPTYPE_BINARY, pvarPropertyValue);
10091

101-
return retValue;
102-
} catch {
92+
if (hresult != 0) {
10393
return default;
10494
}
95+
96+
Byte[] retValue = pvarPropertyValue.GetBstrBinary(null);
97+
OleAut32.VariantClear(pvarPropertyValue);
98+
99+
return retValue;
105100
}
106101

107102
#endregion

ADCS.CertMod.Managed/ICertServerModule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace ADCS.CertMod.Managed;
44

55
interface ICertServerModule {
66
void SetContext(Int32 Context);
7-
void GetRequestProperty(String strPropertyName, Int32 PropertyType, IntPtr pvarPropertyValue);
7+
Int32 GetRequestProperty(String strPropertyName, Int32 PropertyType, IntPtr pvarPropertyValue);
88
String GetRequestAttribute(String strAttributeName);
9-
void GetCertificateProperty(String strPropertyName, Int32 PropertyType, IntPtr pvarPropertyValue);
9+
Int32 GetCertificateProperty(String strPropertyName, Int32 PropertyType, IntPtr pvarPropertyValue);
1010
void GetCertificateExtension(String strExtensionName, Int32 Type, IntPtr pvarValue);
1111
RequestExtensionFlags GetCertificateExtensionFlags();
1212
void EnumerateExtensionsSetup(Int32 Flags);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Runtime.InteropServices;
2+
// ReSharper disable SuspiciousTypeConversion.Global
3+
4+
namespace ADCS.CertMod.Managed.Interop;
5+
6+
/// <summary>
7+
/// Represents CertServer* COM class factory.
8+
/// </summary>
9+
static class CertServerComFactory {
10+
/// <summary>
11+
/// Creates an instance of <strong>ICertServerExit</strong> COM interface.
12+
/// </summary>
13+
/// <returns>ICertServerExit.</returns>
14+
public static ICertServerExit CreateCertServerExit() {
15+
return (ICertServerExit)new CCertServerExitClass();
16+
}
17+
/// <summary>
18+
/// Creates an instance of <strong>ICertServerPolicy</strong> COM interface.
19+
/// </summary>
20+
/// <returns>ICertServerPolicy.</returns>
21+
public static ICertServerPolicy CreateCertServerPolicy() {
22+
return (ICertServerPolicy)new CCertServerPolicyClass();
23+
}
24+
25+
[Guid("4c4a5e40-732c-11d0-8816-00a0c903b83c")]
26+
[TypeLibType(TypeLibTypeFlags.FCanCreate)]
27+
[ClassInterface(ClassInterfaceType.None)]
28+
[ComImport]
29+
class CCertServerExitClass;
30+
31+
[Guid("aa000926-ffbe-11cf-8800-00a0c903b83c")]
32+
[TypeLibType(TypeLibTypeFlags.FCanCreate)]
33+
[ClassInterface(ClassInterfaceType.None)]
34+
[ComImport]
35+
class CCertServerPolicyClass;
36+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
namespace ADCS.CertMod.Managed.Interop;
6+
7+
[Guid("4ba9eb90-732c-11d0-8816-00a0c903b83c")] // from certif.h
8+
[TypeLibType(TypeLibTypeFlags.FDual | TypeLibTypeFlags.FDispatchable)]
9+
[ComImport]
10+
interface ICertServerExit {
11+
[DispId(1610743808)]
12+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
13+
void SetContext(
14+
[In] Int32 Context);
15+
16+
[DispId(1610743809)]
17+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
18+
[PreserveSig] // force HRESULT instead of exception
19+
Int32 GetRequestProperty(
20+
[MarshalAs(UnmanagedType.BStr), In] String strPropertyName,
21+
[In] Int32 PropertyType,
22+
[Out] IntPtr pvarPropertyValue);
23+
24+
[DispId(1610743810)]
25+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
26+
[return: MarshalAs(UnmanagedType.BStr)]
27+
String GetRequestAttribute(
28+
[MarshalAs(UnmanagedType.BStr), In] String strAttributeName);
29+
30+
[DispId(1610743811)]
31+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
32+
[PreserveSig] // force HRESULT instead of exception
33+
Int32 GetCertificateProperty(
34+
[MarshalAs(UnmanagedType.BStr), In] String strPropertyName,
35+
[In] Int32 PropertyType,
36+
[Out] IntPtr pvarPropertyValue);
37+
38+
[DispId(1610743812)]
39+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
40+
Int32 GetCertificateExtension(
41+
[MarshalAs(UnmanagedType.BStr), In] String strExtensionName,
42+
[In] Int32 Type,
43+
[Out] IntPtr pvarValue);
44+
45+
[DispId(1610743813)]
46+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
47+
Int32 GetCertificateExtensionFlags();
48+
49+
[DispId(1610743814)]
50+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
51+
void EnumerateExtensionsSetup(
52+
[In] Int32 Flags);
53+
54+
[DispId(1610743815)]
55+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
56+
[return: MarshalAs(UnmanagedType.BStr)]
57+
String EnumerateExtensions();
58+
59+
[DispId(1610743816)]
60+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
61+
void EnumerateExtensionsClose();
62+
63+
[DispId(1610743817)]
64+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
65+
void EnumerateAttributesSetup(
66+
[In] Int32 Flags);
67+
68+
[DispId(1610743818)]
69+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
70+
[return: MarshalAs(UnmanagedType.BStr)]
71+
String EnumerateAttributes();
72+
73+
[DispId(1610743819)]
74+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
75+
void EnumerateAttributesClose();
76+
}

0 commit comments

Comments
 (0)