@@ -11,6 +11,9 @@ require "resty.openssl.ossl_typ"
11
11
require " resty.openssl.evp"
12
12
require " resty.openssl.objects"
13
13
local stack_lib = require " resty.openssl.stack"
14
+ local pkey_lib = require " resty.openssl.pkey"
15
+ local altname_lib = require " resty.openssl.x509.altname"
16
+ local x509_name_lib = require " resty.openssl.x509.name"
14
17
local util = require " resty.openssl.util"
15
18
16
19
ffi .cdef [[
54
57
local _M = {}
55
58
local mt = { __index = _M , __tostring = __tostring }
56
59
60
+ local x509_req_ptr_ct = ffi .typeof (" X509_REQ*" )
61
+
57
62
function _M .new ()
58
63
local ctx = C .X509_REQ_new ()
59
64
if ctx == il then
@@ -68,8 +73,14 @@ function _M.new()
68
73
return self , nil
69
74
end
70
75
76
+ function _M .istype (l )
77
+ return l .ctx and ffi .istype (x509_req_ptr_ct , l .ctx )
78
+ end
71
79
72
80
function _M :setSubject (name )
81
+ if not x509_name_lib .istype (name ) then
82
+ return " expect a x509.name instance at #1"
83
+ end
73
84
local code = C .X509_REQ_set_subject_name (self .ctx , name .ctx )
74
85
if code ~= 1 then
75
86
return " X509_REQ_set_subject_name() failed: " .. code
@@ -104,12 +115,18 @@ local function xr_modifyRequestedExtension(csr, target_nid, value, crit, flags)
104
115
end
105
116
106
117
function _M :setSubjectAlt (alt )
118
+ if not altname_lib .istype (alt ) then
119
+ return " expect a x509.altname instance at #1"
120
+ end
107
121
-- #define NID_subject_alt_name 85
108
122
-- #define X509V3_ADD_REPLACE 2L
109
123
return xr_modifyRequestedExtension (self .ctx , 85 , alt .ctx , 0 , 2 )
110
124
end
111
125
112
126
function _M :setPublicKey (pkey )
127
+ if not pkey_lib .istype (pkey ) then
128
+ return " expect a pkey instance at #1"
129
+ end
113
130
local code = C .X509_REQ_set_pubkey (self .ctx , pkey .ctx )
114
131
if code ~= 1 then
115
132
return " X509_REQ_set_pubkey() failed: " .. code
118
135
119
136
local int_ptr = ffi .typeof (" int[1]" )
120
137
function _M :sign (pkey )
138
+ if not pkey_lib .istype (pkey ) then
139
+ return " expect a pkey instance at #1"
140
+ end
141
+
121
142
local nid = int_ptr ()
122
143
local code = C .EVP_PKEY_get_default_digest_nid (pkey .ctx , nid )
123
144
if code <= 0 then -- 1: advisory 2: mandatory
0 commit comments