|
1 | 1 | defmodule WebPushElixir do
|
2 | 2 | require Logger
|
3 | 3 |
|
| 4 | + @auth_info "Content-Encoding: auth" <> <<0>> |
| 5 | + @one_buffer <<1>> |
| 6 | + |
4 | 7 | def gen_keypair do
|
5 | 8 | {public, private} = :crypto.generate_key(:ecdh, :prime256v1)
|
6 | 9 |
|
7 | 10 | fn ->
|
8 | 11 | Logger.info(%{:public_key => Base.url_encode64(public, padding: false)})
|
9 | 12 | Logger.info(%{:private_key => Base.url_encode64(private, padding: false)})
|
| 13 | + |
10 | 14 | Logger.info(%{:subject => "mailto:[email protected]"})
|
11 | 15 | end
|
12 | 16 | end
|
| 17 | + |
| 18 | + def encrypt(message, subscription) do |
| 19 | + client_public_key = Base.url_decode64!(subscription.keys.p256dh, padding: false) |
| 20 | + client_auth_secret = Base.url_decode64!(subscription.keys.auth, padding: false) |
| 21 | + |
| 22 | + salt = :crypto.strong_rand_bytes(16) |
| 23 | + |
| 24 | + {server_public_key, server_private_key} = :crypto.generate_key(:ecdh, :prime256v1) |
| 25 | + |
| 26 | + shared_secret = :crypto.compute_key(:ecdh, client_public_key, server_private_key, :prime256v1) |
| 27 | + |
| 28 | + prk = hkdf(client_auth_secret, shared_secret, @auth_info, 32) |
| 29 | + |
| 30 | + context = create_context(client_public_key, server_public_key) |
| 31 | + |
| 32 | + content_encryption_key_info = create_info("aesgcm", context) |
| 33 | + content_encryption_key = hkdf(salt, prk, content_encryption_key_info, 16) |
| 34 | + |
| 35 | + nonce_info = create_info("nonce", context) |
| 36 | + nonce = hkdf(salt, prk, nonce_info, 12) |
| 37 | + |
| 38 | + ciphertext = encrypt_payload(message, content_encryption_key, nonce) |
| 39 | + |
| 40 | + %{ciphertext: ciphertext, salt: salt, server_public_key: server_public_key} |
| 41 | + end |
| 42 | + |
| 43 | + defp hkdf(salt, ikm, info, length) do |
| 44 | + prk = |
| 45 | + :crypto.mac_init(:hmac, :sha256, salt) |
| 46 | + |> :crypto.mac_update(ikm) |
| 47 | + |> :crypto.mac_final() |
| 48 | + |
| 49 | + :crypto.mac_init(:hmac, :sha256, prk) |
| 50 | + |> :crypto.mac_update(info) |
| 51 | + |> :crypto.mac_update(@one_buffer) |
| 52 | + |> :crypto.mac_final() |
| 53 | + |> :binary.part(0, length) |
| 54 | + end |
| 55 | + |
| 56 | + defp create_context(client_public_key, server_public_key) do |
| 57 | + <<0, byte_size(client_public_key)::unsigned-big-integer-size(16)>> <> |
| 58 | + client_public_key <> |
| 59 | + <<byte_size(server_public_key)::unsigned-big-integer-size(16)>> <> server_public_key |
| 60 | + end |
| 61 | + |
| 62 | + defp create_info(type, context) do |
| 63 | + "Content-Encoding: " <> type <> <<0>> <> "P-256" <> context |
| 64 | + end |
| 65 | + |
| 66 | + defp encrypt_payload(plaintext, content_encryption_key, nonce) do |
| 67 | + {cipher_text, cipher_tag} = |
| 68 | + :crypto.crypto_one_time_aead( |
| 69 | + :aes_128_gcm, |
| 70 | + content_encryption_key, |
| 71 | + nonce, |
| 72 | + plaintext, |
| 73 | + "", |
| 74 | + true |
| 75 | + ) |
| 76 | + |
| 77 | + cipher_text <> cipher_tag |
| 78 | + end |
13 | 79 | end
|
0 commit comments