Skip to content

Commit d355f89

Browse files
committedSep 28, 2022
added machine keys for enumeration
1 parent 6e52606 commit d355f89

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed
 

‎WinCryptKeyExplorer/Models/CspProviderVM.cs

+30-27
Original file line numberDiff line numberDiff line change
@@ -98,40 +98,43 @@ public Boolean IsBusy {
9898
public void EnumKeys() {
9999
IsBusy = true;
100100
Keys.Clear();
101-
try {
102-
Int32 hresult = NCryptOpenStorageProvider(out SafeNCryptProviderHandle phProvider, Name, 0x00000040);
103-
if (hresult > 0) {
104-
MsgBox.Show("Error", "Failed to open provider:\n" + new Win32Exception(hresult).Message);
101+
foreach (UInt32 flags in new[] { 0x40, 0x60 }) {
102+
try {
103+
Int32 hresult = NCryptOpenStorageProvider(out SafeNCryptProviderHandle phProvider, Name, 0);
104+
if (hresult > 0) {
105+
MsgBox.Show("Error", "Failed to open provider:\n" + new Win32Exception(hresult).Message);
105106

106-
return;
107-
}
107+
return;
108+
}
108109

109-
IntPtr ppKeyName = IntPtr.Zero;
110-
IntPtr ppEnumState = IntPtr.Zero;
111-
do {
112-
hresult = NCryptEnumKeys(phProvider, null, ref ppKeyName, ref ppEnumState, 0x00000040);
113-
if (hresult == 0) {
114-
NCryptKeyName keyStruct = Marshal.PtrToStructure<NCryptKeyName>(ppKeyName);
115-
Keys.Add(new KeyContainerVM {
116-
KeyName = keyStruct.pszName,
117-
Algorithm = keyStruct.pszAlgid,
118-
KeySpec = (X509KeySpec2)keyStruct.dwLegacyKeySpec,
119-
Flags = keyStruct.dwFlags
120-
});
110+
IntPtr ppKeyName = IntPtr.Zero;
111+
IntPtr ppEnumState = IntPtr.Zero;
112+
do {
113+
hresult = NCryptEnumKeys(phProvider, null, ref ppKeyName, ref ppEnumState, flags);
114+
if (hresult == 0) {
115+
NCryptKeyName keyStruct = Marshal.PtrToStructure<NCryptKeyName>(ppKeyName);
116+
Keys.Add(new KeyContainerVM {
117+
KeyName = keyStruct.pszName,
118+
Algorithm = keyStruct.pszAlgid,
119+
KeySpec = (X509KeySpec2)keyStruct.dwLegacyKeySpec,
120+
Flags = keyStruct.dwFlags
121+
});
121122

122-
NCryptFreeObject(ppKeyName);
123-
} else {
124-
ppKeyName = IntPtr.Zero;
123+
NCryptFreeObject(ppKeyName);
124+
} else {
125+
ppKeyName = IntPtr.Zero;
126+
}
127+
} while (!IntPtr.Zero.Equals(ppKeyName));
128+
NCryptFreeObject(phProvider.DangerousGetHandle());
129+
if (!IntPtr.Zero.Equals(ppEnumState)) {
130+
NCryptFreeObject(ppEnumState);
125131
}
126-
} while (!IntPtr.Zero.Equals(ppKeyName));
127-
NCryptFreeObject(phProvider.DangerousGetHandle());
128-
if (!IntPtr.Zero.Equals(ppEnumState)) {
129-
NCryptFreeObject(ppEnumState);
132+
} catch (Exception ex) {
133+
MsgBox.Show("Error", "Failed to enumerate keys:\n" + ex.Message);
130134
}
131-
} catch (Exception ex) {
132-
MsgBox.Show("Error", "Failed to enumerate keys:\n" + ex.Message);
133135
}
134136

137+
135138
IsBusy = false;
136139
}
137140
}

‎WinCryptKeyExplorer/Models/KeyContainerVM.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ public class KeyContainerVM {
55
public String KeyName { get; set; }
66
public String Algorithm { get; set; }
77
public X509KeySpec2 KeySpec { get; set; }
8-
public UInt32 Flags { get; set; }
8+
public KeyFlags Flags { get; set; }
99
}
1010
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace WinCryptKeyExplorer.Models {
4+
[Flags]
5+
public enum KeyFlags : UInt32 {
6+
None = 0,
7+
MachineKeySet = 0x20,
8+
Silent = 0x40,
9+
OverwriteKey = 0x80,
10+
WriteKeyToLegacyStore = 0x200,
11+
DoNotFinalize = 0x400,
12+
PersistOnly = 0x40000000,
13+
Persist = 0x80000000
14+
}
15+
}

‎WinCryptKeyExplorer/Utils/Win32.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Runtime.InteropServices;
33
using System.Text;
44
using Microsoft.Win32.SafeHandles;
5+
using WinCryptKeyExplorer.Models;
56

67
namespace WinCryptKeyExplorer.Utils {
78
static class Win32 {
@@ -60,7 +61,7 @@ public struct NCryptKeyName {
6061
[MarshalAs(UnmanagedType.LPWStr)]
6162
public String pszAlgid;
6263
public Int32 dwLegacyKeySpec;
63-
public UInt32 dwFlags;
64+
public KeyFlags dwFlags;
6465
}
6566
}
6667
}

‎WinCryptKeyExplorer/WinCryptKeyExplorer.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
</ApplicationDefinition>
7777
<Compile Include="Models\AlgorithmOperationFlags2.cs" />
7878
<Compile Include="Models\AlgorithmType.cs" />
79+
<Compile Include="Models\KeyFlags.cs" />
7980
<Compile Include="Models\X509KeySpec2.cs" />
8081
<Compile Include="Models\CspProvAlg.cs" />
8182
<Compile Include="Models\KeyContainerVM.cs" />

0 commit comments

Comments
 (0)
Please sign in to comment.