fmcgeough

fmcgeough

Oauth2 bad_cert invalid_key_usage

I’m adding oauth2 authentication to an internal company Phoenix web app. Did initial testing with github and google and was at least able to get the basics working really quickly. But now that I’m using our company’s oauth server I ran into an immediate issue. I’m testing locally so I have a client key/secret associated with a localhost callback. Our oauth team provided me with the client key and secret. I implemented an OAuth2.Strategy. I redirect to the authorize URL, enter my credentials and I get my callback with a code at “http://localhost:4000/auth/callback”. I then attempt to do a token exchange and the the code bombs out in the OAuth2.Client.get_token!/4 function with a {:tls_alert, ‘handshake failure’}.

I’ll dig into this tomorrow but I was curious whether I could get hackney (which Oauth2 library is using) to output the exact request it does.

First Post!

voltone

voltone

Looks like Hackney is unable to establish a TLS connection with the OAuth server’s token endpoint. The invalid_key_usage message suggests there is a problem with the server’s certificate.

You can try to connect to the server with OpenSSL to see if the connection succeeds (replace ‘forum.elixirforum.com’ with your server’s hostname):

openssl s_client -connect forum.elixirforum.com:443 -showcerts

If OpenSSL also fails to connect, it might show an error message that might help the server’s administrator resolve the issue. You can also copy the PEM encoded certificates (one at a time, including the ‘BEGIN’ and ‘END’ lines) into the following command:

openssl x509 -text -noout

For the server’s end-certificate you should typically see the following key usages:

    X509v3 extensions:
        X509v3 Key Usage: critical
            Digital Signature, Key Encipherment
        X509v3 Extended Key Usage: 
            TLS Web Server Authentication, TLS Web Client Authentication

Most Liked

fmcgeough

fmcgeough

the issue I was dealing with was due to the strictness of the Erlang TLS implementation. The OAuth server was returning its own certificate twice. Other implementations ignore this. The Erlang one did not. Try the openssl command line tool to check on your cert chain. It might be the same issue.

openssl s_client -connect your_server:443 -verify 3 -CAfile cacerts.pem

replace your_server with whatever your server name is. If the chain shows the same cert twice then its the same issue.

voltone

voltone

It really doesn’t sound like an issue in your code, though it might just be possible to work around TLS interop issues with the specific server you’re trying to talk to, with the right certificate verification hooks. But for that we need to find the root cause. The server isn’t reachable from the public internet by any chance, is it?

Last Post!

sredev

sredev

I actually had checked openssl previously, but I didn’t pay enough attention to the actual certificate I was getting until you mentioned the implementation strictness. It turns out in my case that the new server was configured to return a self-signed cert by default, so I just need to pass in server_name_indication to hackney to get the correct certificate. Thanks for the assist!

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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

Other popular topics Top

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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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 44265 214
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement