@@ -602,16 +602,10 @@ local wrap = ('.'):rep(64)
602
602
603
603
local envelope = " -----BEGIN %s-----\n %s\n -----END %s-----\n "
604
604
605
- local function der2pem (data , header , typ )
605
+ local function der2pem (data , typ )
606
606
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 )
615
609
end
616
610
617
611
@@ -654,6 +648,11 @@ local function encode_sequence_of_integer(array)
654
648
return encode_sequence (array ,encode_binary_integer )
655
649
end
656
650
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
+
657
656
local function openidc_pem_from_x5c (x5c )
658
657
-- TODO check x5c length
659
658
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)
672
671
openidc_base64_url_decode (n ), openidc_base64_url_decode (e )
673
672
}
674
673
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" )
678
681
ngx .log (ngx .DEBUG , " Generated pem key from n and e: " , pem )
679
682
return pem
680
683
end
0 commit comments