Great! I’d say the proper way to do it is to use the erlang records and access the information that way.
I’d define the Records in a separate module and then require them from the module where you want to use them.
Something like:
defmodule MyPublicKey.Records do
require Record
import Record, only: [defrecord: 2, extract: 2]
@public_key "public_key/include/public_key.hrl"
defrecord :"Certificate", extract(:Certificate, from_lib: @public_key)
defrecord :"TBSCertificate", extract(:TBSCertificate, from_lib: @public_key)
defrecord :"SubjectPublicKeyInfo", extract(:SubjectPublicKeyInfo, from_lib: @public_key)
end
defmodule MyPublicKey do
require MyPublicKey.Records
def extract_public_from_cert(certfile) do
cert_text = File.read!(certfile) |> String.trim()
[pem_entry] = :public_key.pem_decode(cert_text)
cert_decoded = :public_key.pem_entry_decode(pem_entry)
plk_der = cert_decoded
|> MyPublicKey.Records."Certificate"(:tbsCertificate)
|> MyPublicKey.Records."TBSCertificate"(:subjectPublicKeyInfo)
|> MyPublicKey.Records."SubjectPublicKeyInfo"(:subjectPublicKey)
:public_key.der_decode(:RSAPublicKey, plk_der)
end
end


















