@@ -2,10 +2,12 @@ local ffi = require "ffi"
2
2
local C = ffi .C
3
3
local ffi_gc = ffi .gc
4
4
local ffi_new = ffi .new
5
+ local ffi_cast = ffi .cast
5
6
6
7
require " resty.openssl.include.x509"
7
8
require " resty.openssl.include.x509.extension"
8
9
local objects_lib = require " resty.openssl.objects"
10
+ local stack_lib = require (" resty.openssl.stack" )
9
11
local util = require " resty.openssl.util"
10
12
local format_error = require (" resty.openssl.err" ).format_error
11
13
@@ -103,6 +105,35 @@ function _M.from_data(any, nid, crit)
103
105
return self , nil
104
106
end
105
107
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
+
106
137
function _M :get_object ()
107
138
-- retruns the internal pointer
108
139
local asn1 = C .X509_EXTENSION_get_object (self .ctx )
0 commit comments