|
| 1 | +# SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +# Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | +defmodule BojRest.CredentialDecryptor do |
| 4 | + @moduledoc """ |
| 5 | + Decrypts per-invocation credentials from a POST /cartridge/:name/invoke body. |
| 6 | +
|
| 7 | + Wire format for ENCRYPTED credentials (remote callers): |
| 8 | +
|
| 9 | + "credentials": { |
| 10 | + "v": 1, |
| 11 | + "encrypted": true, |
| 12 | + "caller_pubkey": "<base64url 32-byte X25519 public key>", |
| 13 | + "nonce": "<base64url 12-byte ChaCha20-Poly1305 nonce>", |
| 14 | + "ciphertext": "<base64url encrypted JSON + 16-byte Poly1305 tag>" |
| 15 | + } |
| 16 | +
|
| 17 | + The plaintext that was encrypted is a JSON object of env-var name → value: |
| 18 | +
|
| 19 | + {"GITHUB_TOKEN": "ghp_xxxx", "OTHER_KEY": "value"} |
| 20 | +
|
| 21 | + ECDH shared secret derivation: |
| 22 | + shared = X25519(node_private_key, caller_pubkey) |
| 23 | + plaintext = ChaCha20-Poly1305-Decrypt(shared[0..31], nonce, ciphertext) |
| 24 | +
|
| 25 | + Wire format for PLAINTEXT credentials (loopback callers only): |
| 26 | +
|
| 27 | + "credentials": {"GITHUB_TOKEN": "ghp_xxxx"} |
| 28 | +
|
| 29 | + Plaintext is only accepted when the caller's remote IP is 127.0.0.1 or ::1. |
| 30 | + Any attempt to send plaintext credentials from a non-loopback address is |
| 31 | + rejected with 403. |
| 32 | +
|
| 33 | + Returns {:ok, env_map} | {:error, reason_string}. |
| 34 | + """ |
| 35 | + |
| 36 | + @aad "boj-invoke-v1" |
| 37 | + |
| 38 | + @type env_map :: %{String.t() => String.t()} |
| 39 | + @type result :: {:ok, env_map()} | {:error, String.t()} |
| 40 | + |
| 41 | + @doc """ |
| 42 | + Extract and decrypt credentials from a request body map. |
| 43 | +
|
| 44 | + `is_local` should be true when `conn.remote_ip` is a loopback address. |
| 45 | + Returns `{:ok, %{}}` (empty map) when no credentials field is present. |
| 46 | + """ |
| 47 | + @spec extract(map(), boolean()) :: result() |
| 48 | + def extract(body, is_local) do |
| 49 | + case Map.get(body, "credentials") do |
| 50 | + nil -> |
| 51 | + {:ok, %{}} |
| 52 | + |
| 53 | + %{"encrypted" => true} = enc -> |
| 54 | + decrypt(enc) |
| 55 | + |
| 56 | + plain when is_map(plain) -> |
| 57 | + if is_local do |
| 58 | + validate_plain(plain) |
| 59 | + else |
| 60 | + {:error, "plaintext credentials are only accepted from loopback — use encrypted form for remote callers"} |
| 61 | + end |
| 62 | + |
| 63 | + _ -> |
| 64 | + {:error, "credentials must be a JSON object"} |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + # ── encrypted path ──────────────────────────────────────────────────────── |
| 69 | + |
| 70 | + defp decrypt(%{"v" => v}) when v != 1 do |
| 71 | + {:error, "unsupported credential encryption version #{v}"} |
| 72 | + end |
| 73 | + |
| 74 | + defp decrypt(%{"caller_pubkey" => b64_pub, "nonce" => b64_nonce, "ciphertext" => b64_ct}) do |
| 75 | + with {:pub, {:ok, caller_pub}} <- {:pub, Base.url_decode64(b64_pub, padding: false)}, |
| 76 | + {:pub_len, true} <- {:pub_len, byte_size(caller_pub) == 32}, |
| 77 | + {:nonce, {:ok, nonce}} <- {:nonce, Base.url_decode64(b64_nonce, padding: false)}, |
| 78 | + {:nonce_len, true} <- {:nonce_len, byte_size(nonce) == 12}, |
| 79 | + {:ct, {:ok, ct_with_tag}} <- {:ct, Base.url_decode64(b64_ct, padding: false)}, |
| 80 | + {:ct_len, true} <- {:ct_len, byte_size(ct_with_tag) > 16} do |
| 81 | + tag_offset = byte_size(ct_with_tag) - 16 |
| 82 | + <<ciphertext::binary-size(tag_offset), tag::binary-size(16)>> = ct_with_tag |
| 83 | + |
| 84 | + node_priv = BojRest.NodeKey.private_key() |
| 85 | + shared = :crypto.compute_key(:ecdh, caller_pub, node_priv, :x25519) |
| 86 | + |
| 87 | + case :crypto.crypto_one_time_aead( |
| 88 | + :chacha20_poly1305, |
| 89 | + shared, |
| 90 | + nonce, |
| 91 | + ciphertext, |
| 92 | + @aad, |
| 93 | + tag, |
| 94 | + false |
| 95 | + ) do |
| 96 | + plaintext when is_binary(plaintext) -> |
| 97 | + case Jason.decode(plaintext) do |
| 98 | + {:ok, map} -> validate_plain(map) |
| 99 | + {:error, _} -> {:error, "decrypted credentials are not valid JSON"} |
| 100 | + end |
| 101 | + |
| 102 | + :error -> |
| 103 | + {:error, "credential decryption failed — wrong node key or tampered ciphertext"} |
| 104 | + end |
| 105 | + else |
| 106 | + {:pub, _} -> {:error, "caller_pubkey is not valid base64url"} |
| 107 | + {:pub_len, _} -> {:error, "caller_pubkey must be 32 bytes"} |
| 108 | + {:nonce, _} -> {:error, "nonce is not valid base64url"} |
| 109 | + {:nonce_len, _} -> {:error, "nonce must be 12 bytes"} |
| 110 | + {:ct, _} -> {:error, "ciphertext is not valid base64url"} |
| 111 | + {:ct_len, _} -> {:error, "ciphertext too short"} |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + defp decrypt(_), do: {:error, "encrypted credentials missing caller_pubkey, nonce, or ciphertext"} |
| 116 | + |
| 117 | + # ── plaintext validation ────────────────────────────────────────────────── |
| 118 | + |
| 119 | + # Ensure the plain map is string→string (env var names and values only). |
| 120 | + defp validate_plain(map) do |
| 121 | + if Enum.all?(map, fn {k, v} -> is_binary(k) and is_binary(v) end) do |
| 122 | + {:ok, map} |
| 123 | + else |
| 124 | + {:error, "credential keys and values must all be strings"} |
| 125 | + end |
| 126 | + end |
| 127 | +end |
0 commit comments