Skip to content

Commit

Permalink
fix(x509.*) pass correct digest parameter to sign
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrullo authored and fffonion committed Aug 9, 2020
1 parent 26ed56a commit 982ad48
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
11 changes: 8 additions & 3 deletions lib/resty/openssl/x509/crl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,17 @@ function _M:sign(pkey, digest)
if not pkey_lib.istype(pkey) then
return false, "x509.crl:sign: expect a pkey instance at #1"
end
if digest and not digest_lib.istype(digest) then
return false, "x509.crl:sign: expect a digest instance at #2"

if digest then
if not digest_lib.istype(digest) then
return false, "x509.crl:sign: expect a digest instance at #2"
elseif not digest.dtyp then
return false, "x509.crl:sign: expect a digest instance to have dtyp member"
end
end

-- returns size of signature if success
if C.X509_CRL_sign(self.ctx, pkey.ctx, digest and digest.ctx) == 0 then
if C.X509_CRL_sign(self.ctx, pkey.ctx, digest and digest.dtyp) == 0 then
return false, format_error("x509.crl:sign")
end

Expand Down
11 changes: 8 additions & 3 deletions lib/resty/openssl/x509/csr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,17 @@ function _M:sign(pkey, digest)
if not pkey_lib.istype(pkey) then
return false, "x509.csr:sign: expect a pkey instance at #1"
end
if digest and not digest_lib.istype(digest) then
return false, "x509.csr:sign: expect a digest instance at #2"

if digest then
if not digest_lib.istype(digest) then
return false, "x509.csr:sign: expect a digest instance at #2"
elseif not digest.dtyp then
return false, "x509.csr:sign: expect a digest instance to have dtyp member"
end
end

-- returns size of signature if success
if C.X509_REQ_sign(self.ctx, pkey.ctx, digest and digest.ctx) == 0 then
if C.X509_REQ_sign(self.ctx, pkey.ctx, digest and digest.dtyp) == 0 then
return false, format_error("x509.csr:sign")
end

Expand Down
11 changes: 8 additions & 3 deletions lib/resty/openssl/x509/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,17 @@ function _M:sign(pkey, digest)
if not pkey_lib.istype(pkey) then
return false, "x509:sign: expect a pkey instance at #1"
end
if digest and not digest_lib.istype(digest) then
return false, "x509:sign: expect a digest instance at #2"

if digest then
if not digest_lib.istype(digest) then
return false, "x509:sign: expect a digest instance at #2"
elseif not digest.dtyp then
return false, "x509:sign: expect a digest instance to have dtyp member"
end
end

-- returns size of signature if success
if C.X509_sign(self.ctx, pkey.ctx, digest and digest.ctx) == 0 then
if C.X509_sign(self.ctx, pkey.ctx, digest and digest.dtyp) == 0 then
return false, format_error("x509:sign")
end

Expand Down
11 changes: 8 additions & 3 deletions scripts/templates/x509_functions.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ function _M:sign(pkey, digest)
if not pkey_lib.istype(pkey) then
return false, "{{modname}}:sign: expect a pkey instance at #1"
end
if digest and not digest_lib.istype(digest) then
return false, "{{modname}}:sign: expect a digest instance at #2"

if digest then
if not digest_lib.istype(digest) then
return false, "{{modname}}:sign: expect a digest instance at #2"
elseif not digest.dtyp then
return false, "{{modname}}:sign: expect a digest instance to have dtyp member"
end
end

-- returns size of signature if success
if C.{{ module.type }}_sign(self.ctx, pkey.ctx, digest and digest.ctx) == 0 then
if C.{{ module.type }}_sign(self.ctx, pkey.ctx, digest and digest.dtyp) == 0 then
return false, format_error("{{ modname }}:sign")
end

Expand Down
2 changes: 1 addition & 1 deletion t/openssl/helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local function create_self_signed(key_opts, names)
cert:set_subject_name(nm)
cert:set_issuer_name(nm)

cert:sign(key)
assert(cert:sign(key))

return cert, key
end
Expand Down

0 comments on commit 982ad48

Please sign in to comment.