Skip to content

Commit 75bc426

Browse files
committed
fully create proper PEM without hard-coded prefix
fixes #170
1 parent 513a997 commit 75bc426

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Diff for: lib/resty/openidc.lua

+15-12
Original file line numberDiff line numberDiff line change
@@ -602,16 +602,10 @@ local wrap = ('.'):rep(64)
602602

603603
local envelope = "-----BEGIN %s-----\n%s\n-----END %s-----\n"
604604

605-
local function der2pem(data, header, typ)
605+
local function der2pem(data, typ)
606606
typ = typ:upper() or "CERTIFICATE"
607-
if header == nil then
608-
data = b64(data)
609-
return string.format(envelope, typ, data:gsub(wrap, '%0\n', (#data-1)/64), typ)
610-
else
611-
-- ADDING b64 RSA HEADER WITH OID
612-
data = header .. b64(data)
613-
return string.format(envelope, typ, data:gsub(wrap, '%0\n', (#data-1)/64), typ)
614-
end
607+
data = b64(data)
608+
return string.format(envelope, typ, data:gsub(wrap, '%0\n', (#data-1)/64), typ)
615609
end
616610

617611

@@ -654,6 +648,11 @@ local function encode_sequence_of_integer(array)
654648
return encode_sequence(array,encode_binary_integer)
655649
end
656650

651+
local function encode_bit_string(array)
652+
local s = "\0" .. array -- first octet holds the number of unused bits
653+
return "\3" .. encode_length(#s) .. s
654+
end
655+
657656
local function openidc_pem_from_x5c(x5c)
658657
-- TODO check x5c length
659658
ngx.log(ngx.DEBUG, "Found x5c, getting PEM public key from x5c entry of json public key")
@@ -672,9 +671,13 @@ local function openidc_pem_from_rsa_n_and_e(n, e)
672671
openidc_base64_url_decode(n), openidc_base64_url_decode(e)
673672
}
674673
local encoded_key = encode_sequence_of_integer(der_key)
675-
676-
--PEM KEY FROM PUBLIC KEYS, PASSING 64 BIT ENCODED RSA HEADER STRING WHICH IS SAME FOR ALL KEYS
677-
local pem = der2pem(encoded_key,"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A","PUBLIC KEY")
674+
local pem = der2pem(encode_sequence({
675+
encode_sequence({
676+
"\6\9\42\134\72\134\247\13\1\1\1" -- OID :rsaEncryption
677+
.. "\5\0" -- ASN.1 NULL of length 0
678+
}),
679+
encode_bit_string(encoded_key)
680+
}), "PUBLIC KEY")
678681
ngx.log(ngx.DEBUG, "Generated pem key from n and e: ", pem)
679682
return pem
680683
end

0 commit comments

Comments
 (0)