Skip to content

Commit dbd3f74

Browse files
committed
feat(x509.extension) add X509V3_set_issuer_pkey in OpenSSL 3.0
1 parent 0946c59 commit dbd3f74

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -3262,9 +3262,13 @@ data = {
32623262
subject = resty.openssl.x509 instance,
32633263
request = resty.openssl.x509.csr instance,
32643264
crl = resty.openssl.x509.crl instance,
3265+
issuer_pkey = resty.openssl.pkey instance, -- >= OpenSSL 3.0
32653266
}
32663267
```
32673268

3269+
From OpenSSL 3.0, `issuer_pkey` can be specified as a fallback source for
3270+
generating the authority key identifier extension when `issuer` is same as `subject`.
3271+
32683272
When `data` is a string, it's the full nconf string. Using section lookup from `value` to
32693273
`data` is also supported.
32703274

lib/resty/openssl/x509/extension.lua

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local objects_lib = require "resty.openssl.objects"
1515
local stack_lib = require("resty.openssl.stack")
1616
local util = require "resty.openssl.util"
1717
local format_error = require("resty.openssl.err").format_error
18+
local OPENSSL_30 = require("resty.openssl.version").OPENSSL_30
1819
local BORINGSSL = require("resty.openssl.version").BORINGSSL
1920

2021
local _M = {}
@@ -29,6 +30,10 @@ local extension_types = {
2930
crl = "resty.openssl.x509.crl",
3031
}
3132

33+
if OPENSSL_30 then
34+
extension_types["issuer_pkey"] = "resty.openssl.pkey"
35+
end
36+
3237
local nconf_load
3338
if BORINGSSL then
3439
nconf_load = function()
@@ -87,6 +92,13 @@ function _M.new(txtnid, value, data)
8792
end
8893
end
8994
C.X509V3_set_ctx(x509_ctx_ptr[0], args.issuer, args.subject, args.request, args.crl, 0)
95+
96+
if OPENSSL_30 and args.issuer_pkey then
97+
if C.X509V3_set_issuer_pkey(x509_ctx_ptr[0], args.issuer_pkey) ~= 1 then
98+
return nil, format_error("x509.extension.new: X509V3_set_issuer_pkey")
99+
end
100+
end
101+
90102
elseif type(data) == 'string' then
91103
err = nconf_load(conf, data)
92104
if err then

t/openssl/x509/extension.t

+37-3
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,45 @@ CA Issuers - URI:http://cacerts.digicert.com/DigiCertHighAssuranceTLSHybridECCSH
170170
content_by_lua_block {
171171
local f = io.open("t/fixtures/Github.pem"):read("*a")
172172
local x509 = myassert(require("resty.openssl.x509").new(f))
173+
f = io.open("t/fixtures/test.crt"):read("*a")
174+
local ic = myassert(require("resty.openssl.x509").new(f))
175+
f = io.open("t/fixtures/test.key"):read("*a")
176+
local ik = myassert(require("resty.openssl.pkey").new(f))
173177
174178
local extension = require("resty.openssl.x509.extension")
175179
local c = myassert(extension.new("subjectKeyIdentifier", "hash",
176-
{
177-
subject = x509,
178-
}))
180+
{
181+
subject = x509,
182+
}))
183+
184+
ngx.say(tostring(c))
185+
186+
if require("resty.openssl.version").OPENSSL_30 then
187+
c = myassert(extension.new("authorityKeyIdentifier", "keyid",
188+
{
189+
subject = x509,
190+
issuer = x509,
191+
}))
192+
193+
if tostring(c) ~= "0." then
194+
ngx.log(ngx.ERR, "authorityKeyIdentifier should be empty but got " .. tostring(c))
195+
end
196+
197+
c = myassert(extension.new("authorityKeyIdentifier", "keyid",
198+
{
199+
subject = x509,
200+
issuer = x509,
201+
issuer_pkey = ik,
202+
}))
203+
-- when set with issuer_pkey, the X509V3_print doesn't include "keyid:" prefix
204+
ngx.print("keyid:")
205+
else
206+
c = myassert(extension.new("authorityKeyIdentifier", "keyid",
207+
{
208+
subject = x509,
209+
issuer = ic,
210+
}))
211+
end
179212
180213
ngx.say(tostring(c))
181214
}
@@ -184,6 +217,7 @@ CA Issuers - URI:http://cacerts.digicert.com/DigiCertHighAssuranceTLSHybridECCSH
184217
GET /t
185218
--- response_body_like eval
186219
"27:B1:7E:9F:BB:26:99:50:D8:F3:C3:53:5B:FE:31:16:B0:BB:1E:72
220+
keyid:CF:03:F5:09:EB:83:D2:4F:10:DE:65:92:90:E9:93:3E:38:4C:E8:7C
187221
"
188222
--- no_error_log
189223
[error]

0 commit comments

Comments
 (0)