nhpip

nhpip

Decoding an X.509 certificate

Hi,

I’ve got a project where I have to decode an X.509 certificate. I’m using Erlang’s public_key module to decode either a PEM or DER file. I get back a beautiful Erlang record. I know I can use Record for this purpose, but that seems overkill. I want to avoid adding yet another library.

Anyone got any recommendations for nicely extracting items out of a nested tuple in Elixir?

Thanks

Marked As Solved

nhpip

nhpip

Thanks. I ended up using the X509 library. Took 5 lines of code.

Also Liked

voltone

voltone

Recent OTP versions support generating code from ASN.1 that produces and accepts maps:

iex(1)> path = ~c"path/to/source/of/otp/lib/public_key/asn1/OTP-PUB-KEY.set.asn"
iex(2)> :asn1ct.compile(path, [:maps])
iex(3)> :code.load_abs(~c"OTP-PUB-KEY")
iex(4)> {:ok, cert} = :"OTP-PUB-KEY".decode(:"Certificate", der)
{:ok,
 %{
   signature: <<133, 191, 235, 15, 159, 223, 81, 127, 179, 46, 167, 62, 52, 82,
     250, 40, 4, 169, 130, 49, 63, 172, 36, 83, 164, 138, 162, 50, 233, 76, 254,
     50, 216, 144, 27, 209, 244, 68, 99, 74, 40, 207, 201, 3, 50, 87, 71, ...>>,
   tbsCertificate: %{
     version: :v3,
     signature: %{
       algorithm: {1, 2, 840, 113549, 1, 1, 11},
       parameters: <<5, 0>>
     },
     extensions: [
       %{
         critical: true,
         extnID: {2, 5, 29, 19},
         extnValue: <<48, 6, 1, 1, 255, 2, 1, 1>>
       },
       %{critical: true, extnID: {2, 5, 29, 15}, extnValue: <<3, 2, 1, 134>>},
       %{
         critical: false,
         extnID: {2, 5, 29, 14},
         extnValue: <<4, 20, 103, 184, 255, 213, 187, 5, 43, 104, 112, 7, 148,
           46, 97, 212, 57, 234, 235, 127, 203, 121>>
       },
       %{
         critical: false,
         extnID: {2, 5, 29, 35},
         extnValue: <<48, 22, 128, 20, 103, 184, 255, 213, 187, 5, 43, 104, 112,
           7, 148, 46, 97, 212, 57, 234, 235, 127, 203, 121>>
       }
     ],
     serialNumber: 4655353446208655840,
     issuer: {:rdnSequence,
      [
        [%{type: {2, 5, 4, 6}, value: <<19, 2, 85, 83>>}],
        [%{type: {2, 5, 4, 8}, value: <<12, 2, 78, 84>>}],
        [%{type: {2, 5, 4, 7}, value: "\f\vSpringfield"}],
        [%{type: {2, 5, 4, 10}, value: "\f\tACME Inc."}]
      ]},
     validity: %{
       notBefore: {:utcTime, ~c"220525065848Z"},
       notAfter: {:utcTime, ~c"470525070348Z"}
     },
     subject: {:rdnSequence,
      [
        [%{type: {2, 5, 4, 6}, value: <<19, 2, 85, 83>>}],
        [%{type: {2, 5, 4, 8}, value: <<12, 2, 78, 84>>}],
        [%{type: {2, 5, 4, 7}, value: "\f\vSpringfield"}],
        [%{type: {2, 5, 4, 10}, value: "\f\tACME Inc."}]
      ]},
     subjectPublicKeyInfo: %{
       algorithm: %{
         algorithm: {1, 2, 840, 113549, 1, 1, 1},
         parameters: <<5, 0>>
       },
       subjectPublicKey: <<48, 130, 1, 10, 2, 130, 1, 1, 0, 180, 114, 112, 36,
         255, 225, 242, 197, 38, 146, 113, 132, 20, 169, 223, 175, 93, 149, 110,
         209, 12, 217, 199, 112, 222, 188, 84, ...>>
     }
   },
   signatureAlgorithm: %{
     algorithm: {1, 2, 840, 113549, 1, 1, 11},
     parameters: <<5, 0>>
   }
 }}
iex(5)> cert.tbsCertificate.validity.notAfter
{:utcTime, ~c"470525070348Z"}
D4no0

D4no0

Pattern matching, using elem/2, are you looking for anything in particular?

I would recommend to use x509 library though, it’s really neat library written by @voltone. When you start decoding things by hand, that code is really hard to comprehend afterwards, better use a well tested library instead IMO.

voltone

voltone

You could use get_in/2 and Access.elem/1, e.g.

iex(1)> tbs = 1
iex(2)> validity = 5
iex(3)> not_after = 2
iex(4)> get_in(cert, [Access.elem(tbs), Access.elem(validity), Access.elem(not_after)])
{:utcTime, ~c"470525070659Z"}

But I also like @D4no0 's suggestion :slight_smile:

D4no0

D4no0

This is the problem we are struggling on a daily basis, the best bet now is to ask the community for advice.

Ironically enough with this “AI” hype, this is actually a place where a LLM that would be trained exclusively on documentation of elixir libraries would be excellent.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement