Crowdhailer

Crowdhailer

Creator of Raxx

I’m experimenting with using the web crypto API. and want to verify signatures from the browser in Elixir (or erlang, so far I have used only modules that are also available in erlang).

The steps in the browser are:

  • generate a key pair.
  • export public-key in PEM format
  • Sign message
  • Base64 encode the signature
  • Send the PEM public-key and encoded signature to the backend

The steps to verify on the backend:

  • decode PEM
  • decode signature
  • use :public_key.verify

The exact code I run is available in this gist.

This files can be used by copy pasting sign.js into the browser console and verify.ex into an iex session.

Doing this the verify function will return false.

I have tried several variations of how I encode/decode. I do need these encode steps because I eventually want to send the signature in the authorization header of a request.
Any help would be greatly appreciated.

Showing Posts 1 to 10

voltone

voltone

The JS API returns a ‘raw’ signature, whereas OTP’s :public_key API expects a DER-encoded signature.

Try this:

message = "abc"

encoded_signature = "AOqjfvb1P0fYdbnq+f3XsbOR/Lylq4csC5e1Ks4cKRTcWKQyH4CVr/XRHCmZDEWOpaA8gwuM3Z3qD21vMaixySUPASTDnd1J8shNx51MCbONIReTNuH53kzxykLpf0riSyMbEBQtie4/pxlrolUOsDja2f/ikgM/lGXtCTXz2faV4m2Y"

public_PEM = "-----BEGIN PUBLIC KEY-----
MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQB7PNQVbADLNyobtijE5NVZUvHs74h
iMntCbp0C8pdU1IQRWlAfDeEs/iuxA32VARw9Q5/0mim8Si8JcpCJnhS0u8AESMf
Ux3WqzHhB33t4q3iPsJbM7zmN91QNnbYErrGqEDCmSruPpKw1iK5dJ3/xQZbkpmR
ztoVwrZoCoGUu+WTqEI=
-----END PUBLIC KEY-----"

raw_signature = Base.decode64!(encoded_signature)
size = div(byte_size(raw_signature), 2)
<<r::binary-size(size), s::binary-size(size)>> = raw_signature
signature = <<48, 129, 136, 2, size, r::binary, 2, size, s::binary>>

[key_entry] = :public_key.pem_decode(public_PEM)
public_key = :public_key.pem_entry_decode(key_entry)

:public_key.verify(
  message,
  :sha256,
  signature,
  public_key
)
# returns true
voltone

voltone

To clean up that code, here’s a module that can help convert from raw format to DER format using :public_key:

I might add it to x509 at some point…

Crowdhailer

Crowdhailer OP

Creator of Raxx

Thanks for this. So to use the functions bellow I would use?

signature = ECDSASignature.new(raw_signature) |>ECDSASignature.to_der()

Also does the :public_key module not expect DER encoding for RSA keys? My original code seams to work for rsa signatures

Exadra37

Exadra37

I am curious to know what are you trying to achieve with this approach of sending the signature in the authorization header of a request?

voltone

voltone

Correct

An RSA signature is just a single integer, which is simply encoded as a binary by all implementations. An ECDSA signature consists of two integers. Some implementations wrap those in an ASN.1 ‘Sequence’ and others just concatenate them (which may require zero-padding of the smaller of the two, to remove ambiguity about where to split).

Crowdhailer

Crowdhailer OP

Creator of Raxx

Just some experiments at the moment, but in general i’m looking at alternatives to passwords

Exadra37

Exadra37

Client side generated secrets can be reverse engineered in mobile apps, that ship their code obfuscated in binaries, and in the web is even more easy to reverse engineer this process, thus anything generated programmatically in the client side cannot be trusted for authentication purposes.

Crowdhailer

Crowdhailer OP

Creator of Raxx

What about Crypto.getRandomValues()?

Exadra37

Exadra37

Sorry I was not meaning the code used to generate the secret… What I was trying to say is that any secret generated in the client side can be reused by an automated script to access the backend as the genuine web app or mobile app.

Another approach is that the automated script can use the same code of your app to generate the authorization secret, thus your backend will trust in requests from automated bots, when you want it to only trust in requests from your genuine web app or mobile app.

Crowdhailer

Crowdhailer OP

Creator of Raxx

Ahh I think I see. I don’t think I have any problems with people using their own client to access my API, if they want to go to that effort

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
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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

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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews