summaryrefslogtreecommitdiff
path: root/xmake/modules/utils/wdk/testcert.lua
blob: 0e8b552348d77fdf9be946efc76fdcd28113c4d3 (plain)
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
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author      ruki
-- @file        testcert.lua
--

-- imports
import("core.base.option")
import("core.base.global")
import("core.project.config")
import("detect.sdks.find_wdk")

-- get tool
function _get_tool(wdk, name)

    -- init cache
    _g.tools = _g.tools or {}

    -- get it from the cache
    local tool = _g.tools[name]
    if not tool then

        -- get arch
        local arch = config.arch() or os.arch()

        -- get tool
        tool = path.join(wdk.bindir, arch, name .. ".exe")
        if not os.isexec(tool) then
            tool = path.join(wdk.bindir, wdk.sdkver, arch, name .. ".exe")
        end
        assert(os.isexec(tool), name .. " not found!")
        _g.tools[name] = tool
    end
    return tool
end

-- install test certificate
function _install(wdk)

    -- get signtool
    local signtool = _get_tool(wdk, "signtool")

    -- check test certificate first
    local ok = try
    {
        function ()
            local tmpfile = os.tmpfile(os.programfile())
            if not os.isfile(tmpfile) then
                os.cp(os.programfile(), tmpfile)
            end
            os.vrunv(signtool, {"sign", "/v", "/a", "/s", "PrivateCertStore", "/n", "xmake.io(test)", tmpfile})
            return true
        end
    }
    if ok then
        return
    end

    -- get makecert
    local makecert = _get_tool(wdk, "makecert")

    -- get certmgr
    local certmgr = _get_tool(wdk, "certmgr")

    -- get a test certificate
    local testcer = path.join(global.directory(), "sign", "test.cer")
    local company = "xmake.io(test)"

    -- make a new test certificate @note need re-generate certificate when reinstalling
    local signdir = path.directory(testcer)
    if not os.isdir(signdir)  then
        os.mkdir(signdir)
    end
    os.vrunv(makecert, {"-r", "-pe", "-ss", "PrivateCertStore", "-n", "CN=" .. company, testcer})

    -- register this test certificate
    try
    {
        function ()
            os.vrunv(certmgr, {"/add", testcer, "/s", "/r", "localMachine", "root"})
            os.vrunv(certmgr, {"/add", testcer, "/s", "/r", "localMachine", "trustedpublisher"})
        end,
        catch
        {
            function (errors)
                os.tryrm(testcer)
                raise(errors)
            end
        }
    }

    -- trace
    print("install test certificate ok!")
    print("  - company: %s", company)
    print("  - cerfile: %s", testcer)
end

-- entry function
function main(action)

    -- find wdk envirnoment first
    local wdk = find_wdk()
    assert(wdk, "wdk not found!")

    -- install or uninstall test certificate
    if action == "install" then
        _install(wdk)
    end
end