2022-09-20 18:12:21 +00:00
|
|
|
/* snac - A simple, minimalistic ActivityPub instance */
|
2023-01-17 08:50:16 +00:00
|
|
|
/* copyright (c) 2022 - 2023 grunfink / MIT license */
|
2022-09-20 18:12:21 +00:00
|
|
|
|
|
|
|
#include "xs.h"
|
|
|
|
#include "xs_io.h"
|
|
|
|
#include "xs_encdec.h"
|
|
|
|
#include "xs_openssl.h"
|
|
|
|
#include "xs_curl.h"
|
2022-10-02 07:27:17 +00:00
|
|
|
#include "xs_time.h"
|
2023-01-08 09:02:31 +00:00
|
|
|
#include "xs_json.h"
|
2022-09-20 18:12:21 +00:00
|
|
|
|
|
|
|
#include "snac.h"
|
|
|
|
|
2023-02-02 03:05:50 +00:00
|
|
|
xs_dict *http_signed_request_raw(const char *keyid, const char *seckey,
|
|
|
|
const char *method, const char *url,
|
2023-02-02 02:49:38 +00:00
|
|
|
xs_dict *headers,
|
|
|
|
const char *body, int b_size,
|
|
|
|
int *status, xs_str **payload, int *p_size,
|
2023-01-24 14:06:58 +00:00
|
|
|
int timeout)
|
2022-09-20 19:00:16 +00:00
|
|
|
/* does a signed HTTP request */
|
2022-09-20 18:12:21 +00:00
|
|
|
{
|
2023-02-02 02:49:38 +00:00
|
|
|
xs *l1 = NULL;
|
|
|
|
xs *date = NULL;
|
|
|
|
xs *digest = NULL;
|
|
|
|
xs *s64 = NULL;
|
|
|
|
xs *signature = NULL;
|
|
|
|
xs *hdrs = NULL;
|
2022-09-20 19:00:16 +00:00
|
|
|
char *host;
|
|
|
|
char *target;
|
2022-09-21 06:57:02 +00:00
|
|
|
char *k, *v;
|
2023-02-02 03:05:50 +00:00
|
|
|
xs_dict *response;
|
2022-09-20 19:00:16 +00:00
|
|
|
|
2022-10-02 07:27:17 +00:00
|
|
|
date = xs_str_utctime(0, "%a, %d %b %Y %H:%M:%S GMT");
|
2022-09-20 19:00:16 +00:00
|
|
|
|
|
|
|
{
|
2023-05-02 04:49:00 +00:00
|
|
|
xs *s = xs_replace_n(url, "https:/" "/", "", 1);
|
2022-09-20 19:00:16 +00:00
|
|
|
l1 = xs_split_n(s, "/", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* strip the url to get host and target */
|
|
|
|
host = xs_list_get(l1, 0);
|
|
|
|
|
|
|
|
if (xs_list_len(l1) == 2)
|
|
|
|
target = xs_list_get(l1, 1);
|
|
|
|
else
|
|
|
|
target = "";
|
|
|
|
|
|
|
|
/* digest */
|
2022-09-25 16:54:05 +00:00
|
|
|
{
|
|
|
|
xs *s;
|
2022-09-25 16:50:53 +00:00
|
|
|
|
2022-09-25 16:54:05 +00:00
|
|
|
if (body != NULL)
|
|
|
|
s = xs_sha256_base64(body, b_size);
|
|
|
|
else
|
|
|
|
s = xs_sha256_base64("", 0);
|
|
|
|
|
|
|
|
digest = xs_fmt("SHA-256=%s", s);
|
|
|
|
}
|
2022-09-20 19:00:16 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
/* build the string to be signed */
|
|
|
|
xs *s = xs_fmt("(request-target): %s /%s\n"
|
|
|
|
"host: %s\n"
|
2022-09-25 16:50:53 +00:00
|
|
|
"digest: %s\n"
|
2022-09-20 19:00:16 +00:00
|
|
|
"date: %s",
|
|
|
|
strcmp(method, "POST") == 0 ? "post" : "get",
|
|
|
|
target, host, digest, date);
|
|
|
|
|
2022-09-26 12:45:31 +00:00
|
|
|
s64 = xs_evp_sign(seckey, s, strlen(s));
|
2022-09-20 19:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* build now the signature header */
|
2023-02-02 03:16:17 +00:00
|
|
|
signature = xs_fmt("keyId=\"%s#main-key\","
|
2022-09-20 19:00:16 +00:00
|
|
|
"algorithm=\"rsa-sha256\","
|
|
|
|
"headers=\"(request-target) host digest date\","
|
|
|
|
"signature=\"%s\"",
|
2023-02-02 03:05:50 +00:00
|
|
|
keyid, s64);
|
2022-09-20 19:00:16 +00:00
|
|
|
|
2022-09-21 06:57:02 +00:00
|
|
|
/* transfer the original headers */
|
|
|
|
hdrs = xs_dict_new();
|
|
|
|
while (xs_dict_iter(&headers, &k, &v))
|
|
|
|
hdrs = xs_dict_append(hdrs, k, v);
|
2022-09-20 19:00:16 +00:00
|
|
|
|
2022-09-21 06:57:02 +00:00
|
|
|
/* add the new headers */
|
2022-09-26 10:29:26 +00:00
|
|
|
if (strcmp(method, "POST") == 0)
|
|
|
|
hdrs = xs_dict_append(hdrs, "content-type", "application/activity+json");
|
|
|
|
else
|
|
|
|
hdrs = xs_dict_append(hdrs, "accept", "application/activity+json");
|
|
|
|
|
2023-03-02 16:28:29 +00:00
|
|
|
xs *user_agent = xs_fmt("%s; +%s/", USER_AGENT, srv_baseurl);
|
|
|
|
|
2022-09-21 06:57:02 +00:00
|
|
|
hdrs = xs_dict_append(hdrs, "date", date);
|
|
|
|
hdrs = xs_dict_append(hdrs, "signature", signature);
|
|
|
|
hdrs = xs_dict_append(hdrs, "digest", digest);
|
2022-09-25 19:45:58 +00:00
|
|
|
hdrs = xs_dict_append(hdrs, "host", host);
|
2023-03-02 16:28:29 +00:00
|
|
|
hdrs = xs_dict_append(hdrs, "user-agent", user_agent);
|
2022-09-21 06:57:02 +00:00
|
|
|
|
2022-09-25 16:50:53 +00:00
|
|
|
response = xs_http_request(method, url, hdrs,
|
2023-01-24 14:06:58 +00:00
|
|
|
body, b_size, status, payload, p_size, timeout);
|
2022-09-25 16:50:53 +00:00
|
|
|
|
2023-03-02 16:13:17 +00:00
|
|
|
srv_archive("SEND", url, hdrs, body, b_size, *status, response, *payload, *p_size);
|
2022-09-25 16:50:53 +00:00
|
|
|
|
|
|
|
return response;
|
2022-09-20 18:12:21 +00:00
|
|
|
}
|
2022-09-29 12:44:24 +00:00
|
|
|
|
|
|
|
|
2023-02-02 03:05:50 +00:00
|
|
|
xs_dict *http_signed_request(snac *snac, const char *method, const char *url,
|
|
|
|
xs_dict *headers,
|
|
|
|
const char *body, int b_size,
|
|
|
|
int *status, xs_str **payload, int *p_size,
|
|
|
|
int timeout)
|
|
|
|
/* does a signed HTTP request */
|
|
|
|
{
|
|
|
|
char *seckey = xs_dict_get(snac->key, "secret");
|
|
|
|
xs_dict *response;
|
|
|
|
|
2023-02-02 03:16:17 +00:00
|
|
|
response = http_signed_request_raw(snac->actor, seckey, method, url,
|
2023-02-02 03:05:50 +00:00
|
|
|
headers, body, b_size, status, payload, p_size, timeout);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-01 07:25:36 +00:00
|
|
|
int check_signature(snac *snac, xs_dict *req, xs_str **err)
|
2022-09-29 12:44:24 +00:00
|
|
|
/* check the signature */
|
|
|
|
{
|
|
|
|
char *sig_hdr = xs_dict_get(req, "signature");
|
|
|
|
xs *keyId = NULL;
|
|
|
|
xs *headers = NULL;
|
|
|
|
xs *signature = NULL;
|
2023-01-07 23:26:48 +00:00
|
|
|
xs *created = NULL;
|
|
|
|
xs *expires = NULL;
|
2022-09-29 12:44:24 +00:00
|
|
|
char *pubkey;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
{
|
|
|
|
/* extract the values */
|
|
|
|
xs *l = xs_split(sig_hdr, ",");
|
2023-03-01 07:25:36 +00:00
|
|
|
xs_list *p;
|
|
|
|
xs_val *v;
|
2022-09-29 12:44:24 +00:00
|
|
|
|
|
|
|
p = l;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
if (xs_startswith(v, "keyId"))
|
2023-01-12 08:28:02 +00:00
|
|
|
keyId = xs_crop_i(xs_dup(v), 7, -1);
|
2022-09-29 12:44:24 +00:00
|
|
|
else
|
|
|
|
if (xs_startswith(v, "headers"))
|
2023-01-12 08:28:02 +00:00
|
|
|
headers = xs_crop_i(xs_dup(v), 9, -1);
|
2022-09-29 12:44:24 +00:00
|
|
|
else
|
|
|
|
if (xs_startswith(v, "signature"))
|
2023-01-12 08:28:02 +00:00
|
|
|
signature = xs_crop_i(xs_dup(v), 11, -1);
|
2023-01-07 23:26:48 +00:00
|
|
|
else
|
|
|
|
if (xs_startswith(v, "created"))
|
2023-01-12 08:28:02 +00:00
|
|
|
created = xs_crop_i(xs_dup(v), 9, -1);
|
2023-01-07 23:26:48 +00:00
|
|
|
else
|
|
|
|
if (xs_startswith(v, "expires"))
|
2023-01-12 08:28:02 +00:00
|
|
|
expires = xs_crop_i(xs_dup(v), 9, -1);
|
2022-09-29 12:44:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyId == NULL || headers == NULL || signature == NULL) {
|
2023-01-08 09:19:25 +00:00
|
|
|
*err = xs_fmt("bad signature header");
|
2023-01-08 09:11:33 +00:00
|
|
|
return 0;
|
2022-09-29 12:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* strip the # from the keyId */
|
|
|
|
if ((p = strchr(keyId, '#')) != NULL)
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
xs *actor = NULL;
|
2023-01-11 08:38:19 +00:00
|
|
|
|
|
|
|
if (!valid_status(actor_request(snac, keyId, &actor))) {
|
2023-01-08 09:19:25 +00:00
|
|
|
*err = xs_fmt("unknown actor %s", keyId);
|
2023-01-08 09:11:33 +00:00
|
|
|
return 0;
|
2022-09-29 12:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((p = xs_dict_get(actor, "publicKey")) == NULL ||
|
|
|
|
((pubkey = xs_dict_get(p, "publicKeyPem")) == NULL)) {
|
2023-01-08 09:19:25 +00:00
|
|
|
*err = xs_fmt("cannot get pubkey from %s", keyId);
|
2023-01-08 09:11:33 +00:00
|
|
|
return 0;
|
2022-09-29 12:44:24 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 13:08:56 +00:00
|
|
|
/* now build the string to be signed */
|
|
|
|
xs *sig_str = xs_str_new(NULL);
|
|
|
|
|
|
|
|
{
|
|
|
|
xs *l = xs_split(headers, " ");
|
2023-03-01 07:25:36 +00:00
|
|
|
xs_list *p;
|
|
|
|
xs_val *v;
|
2022-09-29 13:08:56 +00:00
|
|
|
|
|
|
|
p = l;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
char *hc;
|
|
|
|
xs *ss = NULL;
|
|
|
|
|
|
|
|
if (*sig_str != '\0')
|
|
|
|
sig_str = xs_str_cat(sig_str, "\n");
|
|
|
|
|
|
|
|
if (strcmp(v, "(request-target)") == 0) {
|
|
|
|
ss = xs_fmt("%s: post %s", v, xs_dict_get(req, "path"));
|
|
|
|
}
|
2023-01-07 23:26:48 +00:00
|
|
|
else
|
|
|
|
if (strcmp(v, "(created)") == 0) {
|
|
|
|
ss = xs_fmt("%s: %s", v, created);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(v, "(expires)") == 0) {
|
|
|
|
ss = xs_fmt("%s: %s", v, expires);
|
|
|
|
}
|
2022-09-29 13:08:56 +00:00
|
|
|
else {
|
|
|
|
/* add the header */
|
|
|
|
if ((hc = xs_dict_get(req, v)) == NULL) {
|
2023-01-08 09:19:25 +00:00
|
|
|
*err = xs_fmt("cannot find header '%s'", v);
|
2023-01-08 09:11:33 +00:00
|
|
|
return 0;
|
2022-09-29 13:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ss = xs_fmt("%s: %s", v, hc);
|
|
|
|
}
|
|
|
|
|
|
|
|
sig_str = xs_str_cat(sig_str, ss);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 02:33:42 +00:00
|
|
|
if (xs_evp_verify(pubkey, sig_str, strlen(sig_str), signature) != 1) {
|
2023-01-08 09:19:25 +00:00
|
|
|
*err = xs_fmt("RSA verify error %s", keyId);
|
2023-01-08 09:11:33 +00:00
|
|
|
return 0;
|
2022-09-29 13:08:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 12:44:24 +00:00
|
|
|
return 1;
|
2023-01-08 09:11:33 +00:00
|
|
|
}
|