Skip to content

Commit 15a5c7f

Browse files
committed
feat(x509.extension) add ability to convert to other data type
1 parent baefcb0 commit 15a5c7f

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

lib/resty/openssl/x509/altname.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,8 @@ _M.count = mt.__len
180180

181181
-- for use of test only
182182
function _M:_tostring()
183-
local all = self:all()
184183
local values = {}
185-
for k, v in pairs(all) do
184+
for k, v in pairs(self) do
186185
table.insert(values, k .. "=" .. v)
187186
end
188187
table.sort(values)

lib/resty/openssl/x509/extension.lua

+31
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ local ffi = require "ffi"
22
local C = ffi.C
33
local ffi_gc = ffi.gc
44
local ffi_new = ffi.new
5+
local ffi_cast = ffi.cast
56

67
require "resty.openssl.include.x509"
78
require "resty.openssl.include.x509.extension"
89
local objects_lib = require "resty.openssl.objects"
10+
local stack_lib = require("resty.openssl.stack")
911
local util = require "resty.openssl.util"
1012
local format_error = require("resty.openssl.err").format_error
1113

@@ -103,6 +105,35 @@ function _M.from_data(any, nid, crit)
103105
return self, nil
104106
end
105107

108+
local NID_subject_alt_name = C.OBJ_sn2nid("subjectAltName")
109+
assert(NID_subject_alt_name ~= 0)
110+
111+
function _M.to_data(extension, nid)
112+
if not _M.istype(extension) then
113+
return nil, "x509.extension.dup: expect a x509.extension ctx at #1"
114+
elseif type(nid) ~= "number" then
115+
return nil, "x509.extension.to_data: expect a table at #2"
116+
end
117+
118+
local void_ptr = C.X509V3_EXT_d2i(extension.ctx)
119+
if void_ptr == nil then
120+
return nil, format_error("x509.extension:to_data: X509V3_EXT_d2i")
121+
end
122+
123+
if nid == NID_subject_alt_name then
124+
-- Note: here we only free the stack itself not elements
125+
-- since there seems no way to increase ref count for a GENERAL_NAME
126+
-- we left the elements referenced by the new-dup'ed stack
127+
ffi_gc(void_ptr, stack_lib.gc_of("GENERAL_NAME"))
128+
local got = ffi_cast("GENERAL_NAMES*", void_ptr)
129+
local lib = require("resty.openssl.x509.altname")
130+
-- the internal ptr is returned, ie we need to copy it
131+
return lib.dup(got)
132+
end
133+
134+
return nil, string.format("x509.extension:to_data: don't know how to convert to NID %d", nid)
135+
end
136+
106137
function _M:get_object()
107138
-- retruns the internal pointer
108139
local asn1 = C.X509_EXTENSION_get_object(self.ctx)

scripts/type_x509.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
"type": "x509.altname",
121121
"dup": True,
122122
"extension": "subjectAltName",
123-
"sample_printable": 'DNS=www.github.com',
123+
"sample_printable": 'DNS=github.com/DNS=www.github.com',
124124
"get_converter": '''
125125
-- Note: here we only free the stack itself not elements
126126
-- since there seems no way to increase ref count for a GENERAL_NAME

t/openssl/x509.t

+1-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ SwIDAQAB
803803
--- request
804804
GET /t
805805
--- response_body eval
806-
"DNS=www.github.com"
806+
"DNS=github.com/DNS=www.github.com"
807807
--- no_error_log
808808
[error]
809809

t/openssl/x509/extension.t

+23
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,26 @@ DNS:test.com, DNS:test2.com
195195
'
196196
--- no_error_log
197197
[error]
198+
199+
=== TEST 8: Convert extension to data
200+
--- http_config eval: $::HttpConfig
201+
--- config
202+
location =/t {
203+
content_by_lua_block {
204+
local altname = require("resty.openssl.x509.altname").new()
205+
altname:add("DNS", "test.com")
206+
altname:add("DNS", "test2.com")
207+
local extension = require("resty.openssl.x509.extension")
208+
local c = myassert(extension.from_data(altname, 85, false))
209+
210+
local alt2 = myassert(extension.to_data(c, 85))
211+
ngx.say(alt2:_tostring())
212+
}
213+
}
214+
--- request
215+
GET /t
216+
--- response_body_like eval
217+
'DNS=test.com/DNS=test2.com
218+
'
219+
--- no_error_log
220+
[error]

0 commit comments

Comments
 (0)