-
Notifications
You must be signed in to change notification settings - Fork 5
/
NCryptUtils.lua
131 lines (101 loc) · 2.82 KB
/
NCryptUtils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
local ffi = require "ffi"
local bit = require "bit"
local bor = bit.bor
local WinError = require "WinError"
local NCrypt = require "ncrypt"
local NCLib = ffi.load("ncrypt")
local k32 = require "Kernel32"
local L = k32.AnsiToUnicode16
local A = k32.Unicode16ToAnsi
NCryptKeyName = ffi.typeof("NCryptKeyName");
NCryptKeyName_mt = {
__gc = function(self)
NCLib.NCryptFreeBuffer(self);
end,
}
NCryptKeyName = ffi.metatype(NCryptKeyName, NCryptKeyName_mt);
ffi.cdef[[
typedef struct {
NCRYPT_PROV_HANDLE Handle;
} NCryptStorageProvider;
]]
NCryptStorageProvider = ffi.typeof("NCryptStorageProvider")
NCryptStorageProvider_mt = {
__gc = function(self)
NCLib.NCryptFreeObject(self.Handle);
end,
__new = function(ct, pszProviderName)
local phProvider = ffi.new("NCRYPT_PROV_HANDLE[1]");
local pszProviderName = nil;
local status = NCLib.NCryptOpenStorageProvider(phProvider,
pszProviderName, 0);
if status ~= 0 then
return nil, status
end
local obj = ffi.new("NCryptStorageProvider", phProvider[0]);
return obj;
end,
__index = {
EnumerateAlgorithms = function(self, whichones)
whichones = whichones or bor(
NCrypt.NCRYPT_CIPHER_OPERATION,
NCrypt.NCRYPT_HASH_OPERATION,
NCrypt.NCRYPT_ASYMMETRIC_ENCRYPTION_OPERATION,
NCrypt.NCRYPT_SECRET_AGREEMENT_OPERATION,
NCrypt.NCRYPT_SIGNATURE_OPERATION);
local pdwAlgCount = ffi.new("uint32_t[1]");
local ppAlgList = ffi.new("PNCryptAlgorithmName[1]");
local status = NCLib.NCryptEnumAlgorithms(self.Handle,
whichones,
pdwAlgCount,
ppAlgList,
0);
if status ~= 0 then
return nil, status
end
-- Create a list with the algoritm
-- names in it
local res = {}
local AlgList = ppAlgList[0];
for i=0,pdwAlgCount[0]-1 do
local entry = {
name = A(AlgList[i].pszName),
class = AlgList[i].dwClass,
operations = AlgList[i].dwAlgOperations,
}
table.insert(res, entry);
end
return res
end,
IsAlgorithmSupported = function(self, algoname)
end,
GetAllKeys = function(self, flags)
flags = flags or 0
local res = {}
local ppKeyName = ffi.new("PNCryptKeyName[1]");
local ppEnumState = ffi.new("PVOID[1]");
local status
local dwFlags = flags;
repeat
local pszScope = nil
status = NCLib.NCryptEnumKeys(self.Handle,
pszScope,ppKeyName,ppEnumState,dwFlags);
if status == 0 then
local pKeyName = ppKeyName[0]
local entry = {
name = A(pKeyName.pszName),
algoid = A(pKeyName.pszAlgid),
}
table.insert(res, entry);
end
until status ~= 0
print(string.format("0x%x", status))
print(string.format("Status: 0x%x 0x%x 0x%x", HRESULT_PARTS(status)))
return res;
end,
},
}
NCryptStorageProvider = ffi.metatype(NCryptStorageProvider, NCryptStorageProvider_mt);
return {
NCryptStorageProvider = NCryptStorageProvider,
}