cgraham

cgraham

Hello,

I am trying to get an alexa skill certified by Amazon. As part of the certification process, they want the server to validate the request has a valid X.509 certificate chain from an Amazon domain. They provide an impl in Java.
(https://github.com/amzn/alexa-skills-kit-java/blob/master/src/com/amazon/speech/speechlet/authentication/SpeechletRequestSignatureVerifier.java)

I am trying to build a version in Phoenix / Elixir. (I have the rest of the system working - just not this.).

Does anyone have a good suggestion as to which libraries/plug-ins I should use to implement these functions (specifically reading and verifying the X.509 certificate chain)?

I am assuming it would work best as a Phoenix Plug but I am open to other suggestions too.

I am also happy to make it open source if we can figure out a solution that works.

Thanks!

Showing Posts 1 to 10

gon782

gon782

The first place I’d look is the crypto module in the erlang main distribution. You have access to it off the bat, you just have to do Application.start :crypto. There’s also the public_key module that seems related.

OvermindDL1

OvermindDL1

Except that will not include it in a release, instead you want to add it to the (extra) applications list in your mix.exs file, that way it will both be included in release as well as loaded. :slight_smile:

gon782

gon782

Yes, obviously I meant for when he wants to check it out in iex. It’s not even started in a normal iex session, nor is it accessible for autocomplete, hence the suggestion.

bbense

bbense

There’s a trick. If you do

iex> h :crypto

the module will be loaded and then the tab completion will work.

cgraham

cgraham OP

Thank you guys very much! public_key is definitely the library I want to use. I am not sure I even need the RSA library/application which is great.

I am totally inexperienced with Erlang unfortunately and cannot seem to get it all working via iex. I am wondering what I am doing wrong.

Here’s what I got:

Application.ensure_all_started :inets
Application.ensure_all_started :ssl

\# amazon cert chain
{:ok, resp} = :httpc.request(:get, {'https://s3.amazonaws.com/echo.api/echo-api-cert.pem', []}, [], [body_format: :binary])
{{_, 200, 'OK'}, _headers, certificate_chain_bin} = resp
cert_chain = :public_key.pem_decode(certificate_chain_bin)

\# root cert
{:ok, resp} = :httpc.request(:get, {'https://www.symantec.com/content/dam/symantec/docs/other-resources/verisign-class-3-public-primary-certification-authority-g5-en.pem', []}, [], [body_format: :binary])
{{_, 200, 'OK'}, _headers, root_cert} = resp
[root_cert_decoded] = :public_key.pem_decode(root_cert)

foo = :public_key.pkix_path_validation(root_cert_decoded, cert_chain, nil)

No matter what I put in (nil, or a function call, etc…) it is returning with an error of some sort.

It looks like the erlang library is asking me to pass a function as part of the options but when I try to send it, it complains. I have tried looking at the Erlang codebase but could not identify the problem (let alone if I am using the correct version of Erlang :)). Thoughts on what I am doing wrong?

I have also tried the following but to no avail:

verify_fun = fn(cert, event, state) -> { :valid, state} end
foo = :public_key.pkix_path_validation(:RSA, cert_chain, [{:verify_fun, verify_fun}])

Docs are here:

Thanks in advance if you are able to figure out what I am doing wrong!

cgraham

cgraham OP

Ok. Made some progress. it looks like all of the path_validation needed to be converted to otp style certificates. Working on an update which I hope to post if I get it actually working. Oy!

xlphs

xlphs

I’m not familiar with X.509 but based on my experience with ssl cert verification I think you are supposed to do just a bit more work on the result of :public_key.pem_decode.

I created a quick script here public_key_verify.exs · GitHub, run it with elixir public_key_verify.exs. I got a bunch of errors printed out inside verify_fun, the echo cert has expired.

cgraham

cgraham OP

Thanks, xlphs!

I got one step further on my own but am still stuck. The live valid certificate is at : https://s3.amazonaws.com/echo.api/echo-api-cert-4.pem

And I think the trusted cert should be: https://www.symantec.com/content/dam/symantec/docs/other-resources/verisign-class-3-public-primary-certification-authority-g4-en.pem

Unfortunately, if I change the URLs in your script I still get :invalid_issuer, :invalid_key_usage, :invalid_signature errors.

I am guessing we might have the wrong trusted cert, but no matter what I try I get the same errors. (I have tried multiple root certs).

In any case, that’s my current blocking point. Feels like I should not be ignoring those errors :slight_smile:

voltone

voltone

The trust anchor is actually the root certificate from @xlphs’s script, the one ending with “G5”. One way to verify how the various certificates relate is to use OpenSSL’s CLI:

cat cert.pem | openssl x509 -text -noout

You can either compare the Subject and Issuer RDNs, or check if the Authority Key Identifier matches the Subject Key Identifier of the issuing cert. Note that you’d have to split Amazon’s PEM file into two files to be able to pipe each on into OpenSSL.

The other thing to note is that pkix_path_validation expects the certificate chain to be in the opposite order of what’s in the file from Amazon, with the peer cert at the end:

CertChain = [der_encoded()]
A list of DER-encoded certificates in trust order ending with the peer certificate.

So make sure you pass the list through Enum.reverse/1 first.

cgraham

cgraham OP

Awesome! It worked! I thought the certs were in order and not reversed.

One other question - is there any way to access an erlang set of Trusted certificates or do I just need to build out a list myself since this is a lower level library? I noticed the ssl package has trusted certs but could not find a way to access it directly. No worries if not. I think it is fine to just download or cache the one I need for now.

Thanks again everyone!

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews