I'm writing an nginx module in LUA and I'm finding very sparse documentation on a specific topic. I need to convert an RSA public key stored in JWK format to PEM. I need to do this almost entirely manually in LUA code (for many reasons). That means no ASN.1 library.
I'm having trouble finding even a high level/pseudo code example of the algorithm to convert them. I understand the basics (JWK is base64url encoded modulus and exponent, PEM is is the same values DER-encoded and put into an ASN.1 sequence and then base64 encoded w/ -----BEGIN PUBLIC KEY---- prefix/etc). I'm having trouble actually implementing it, though, because ASN.1 documentation is... dense.
Can someone provide me with a high-level overview of the actual bit-string packing that needs to happen to produce a proper PEM representation of a public key... assuming I can unpack/base64url decode the numbers and store them in a binary string already.
I'm attaching a sample key in JWK format for reference:
{
"alg": "RS256",
"e": "AQAB",
"n": "iKqiD4cr7FZKm6f05K4r-GQOvjRqjOeFmOho9V7SAXYwCyJluaGBLVvDWO1XlduPLOrsG_Wgs67SOG5qeLPR8T1zDK4bfJAo1Tvbw
YeTwVSfd_0mzRq8WaVc_2JtEK7J-4Z0MdVm_dJmcMHVfDziCRohSZthN__WM2NwGnbewWnla0wpEsU3QMZ05_OxvbBdQZaDUsNSx4
6is29eCdYwhkAfFd_cFRq3DixLEYUsRwmOqwABwwDjBTNvgZOomrtD8BRFWSTlwsbrNZtJMYU33wuLO9ynFkZnY6qRKVHr3YToIrq
NBXw0RWCheTouQ-snfAB6wcE2WDN3N5z760ejqQ",
"kid": "U5R8cHbGw445Qbq8zVO1PcCpXL8yG6IcovVa3laCoxM",
"kty": "RSA",
"use": "sig"
}