libs-imp-openssl.lmt /size: 3028 b    last modification: 2024-01-16 10:22
1if not modules then modules = { } end modules ['libs-imp-openssl'] = {
2    version   = 1.001,
3    comment   = "companion to luat-lib.mkxl",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local type = type
10local dirname = file.dirname
11
12local libname    = "openssl"
13local libfiles   = os.name == "windows"
14   and { "libcrypto-3-x64", "libssl-3-x64" }
15    or { "libcrypto",       "libssl" }
16
17local openssllib = resolvers.libraries.validoptional(libname)
18
19if not openssllib then return end
20
21local openssl_sign       = openssllib.sign
22local openssl_verify     = openssllib.verify
23local openssl_getversion = openssllib.getversion
24
25local report             = logs.reporter(libname)
26
27local function okay()
28    if resolvers.libraries.optionalloaded(libname,libfiles) then
29     -- openssllib.initialize()
30        okay = function() return true end
31    else
32        okay = function() return false end
33    end
34    return okay()
35end
36
37local messages = {
38    "invalid certificate file",
39    "invalid certificate",
40    "invalid private key",
41    "invalid data file",
42    "invalid signature",
43    "unable to open output file",
44    "unable to reset file",
45    "unable to save signature",
46    "incomplete specification",
47    "library is unitialized",
48}
49
50-- this     : datafile   = "oeps.tmp"
51-- or that  : data       = io.loaddata("oeps.tmp")
52-- mandate  : certfile   = "cert.pem"
53-- mandate  : password   = "test"
54-- optional : resultfile = "oeps.sig"
55
56local function sign(specification)
57    if okay() then
58        local t = type(specification)
59        if t == "table" then
60            local result, message = openssl_sign(specification)
61            if result then
62                return true, message
63            else
64                report(messages[message] or "unknown error")
65            end
66        else
67            report("invalid argument")
68        end
69    else
70        report("no openssl library loaded")
71    end
72    return false
73end
74
75local function verify(specification)
76    if okay() then
77        local t = type(specification)
78        if t == "table" then
79            local result, message = openssl_verify(specification)
80            if result then
81                return true, message
82            else
83                report(messages[message] or "unknown error")
84            end
85        else
86            report("invalid argument")
87        end
88    else
89        report("no openssl library loaded")
90    end
91    return false
92end
93
94local function getversion()
95    return okay() and openssl_getversion()
96end
97
98local openssl = {
99    getversion = getversion,
100    sign       = sign,
101    verify     = verify,
102    libfiles   = libfiles,
103    libfile    = libfiles[1],
104    libpath    = dirname(libfiles[1]),
105}
106
107package.loaded[libname] = openssl
108
109return openssl
110
111-- local result, message = openssl.sign {
112--     datafile   = "oeps.pdf",
113--     certfile   = "cert.pem",
114--     password   = "test",
115--     resultfile = "oeps.xxx",
116-- }
117