123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #include "xs.h"
- #include "xs_io.h"
- #include "xs_encdec.h"
- #include "xs_openssl.h"
- #include "xs_curl.h"
- #include "xs_time.h"
- #include "snac.h"
- d_char *http_signed_request(snac *snac, char *method, char *url,
- d_char *headers,
- d_char *body, int b_size,
- int *status, d_char **payload, int *p_size)
- {
- xs *l1;
- xs *date;
- xs *digest;
- xs *s64;
- xs *signature;
- xs *hdrs;
- char *host;
- char *target;
- char *seckey;
- char *k, *v;
- d_char *response;
- date = xs_str_utctime(0, "%a, %d %b %Y %H:%M:%S GMT");
- {
- xs *s = xs_replace(url, "https:/" "/", "");
- l1 = xs_split_n(s, "/", 1);
- }
-
- host = xs_list_get(l1, 0);
- if (xs_list_len(l1) == 2)
- target = xs_list_get(l1, 1);
- else
- target = "";
-
- {
- xs *s;
- if (body != NULL)
- s = xs_sha256_base64(body, b_size);
- else
- s = xs_sha256_base64("", 0);
- digest = xs_fmt("SHA-256=%s", s);
- }
- seckey = xs_dict_get(snac->key, "secret");
- {
-
- xs *s = xs_fmt("(request-target): %s /%s\n"
- "host: %s\n"
- "digest: %s\n"
- "date: %s",
- strcmp(method, "POST") == 0 ? "post" : "get",
- target, host, digest, date);
- s64 = xs_evp_sign(seckey, s, strlen(s));
- }
-
- signature = xs_fmt("keyId=\"%s#main-key\","
- "algorithm=\"rsa-sha256\","
- "headers=\"(request-target) host digest date\","
- "signature=\"%s\"",
- snac->actor, s64);
-
- hdrs = xs_dict_new();
- while (xs_dict_iter(&headers, &k, &v))
- hdrs = xs_dict_append(hdrs, k, v);
-
- 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");
- hdrs = xs_dict_append(hdrs, "date", date);
- hdrs = xs_dict_append(hdrs, "signature", signature);
- hdrs = xs_dict_append(hdrs, "digest", digest);
- hdrs = xs_dict_append(hdrs, "host", host);
- hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
- response = xs_http_request(method, url, hdrs,
- body, b_size, status, payload, p_size);
- srv_archive("SEND", hdrs, body, b_size, *status, response, *payload, *p_size);
- return response;
- }
- int check_signature(snac *snac, char *req)
- {
- char *sig_hdr = xs_dict_get(req, "signature");
- xs *keyId = NULL;
- xs *headers = NULL;
- xs *signature = NULL;
- char *pubkey;
- char *p;
- {
-
- xs *l = xs_split(sig_hdr, ",");
- char *v;
- p = l;
- while (xs_list_iter(&p, &v)) {
- if (xs_startswith(v, "keyId"))
- keyId = xs_crop(xs_dup(v), 7, -1);
- else
- if (xs_startswith(v, "headers"))
- headers = xs_crop(xs_dup(v), 9, -1);
- else
- if (xs_startswith(v, "signature"))
- signature = xs_crop(xs_dup(v), 11, -1);
- }
- }
- if (keyId == NULL || headers == NULL || signature == NULL) {
- snac_debug(snac, 1, xs_fmt("bad signature header"));
- return 0;
- }
-
- if ((p = strchr(keyId, '#')) != NULL)
- *p = '\0';
-
- xs *actor = NULL;
- if (!valid_status(actor_get(snac, keyId, &actor))) {
- snac_debug(snac, 1, xs_fmt("check_signature unknown actor %s", keyId));
- return 0;
- }
- if ((p = xs_dict_get(actor, "publicKey")) == NULL ||
- ((pubkey = xs_dict_get(p, "publicKeyPem")) == NULL)) {
- snac_debug(snac, 1, xs_fmt("cannot get pubkey from actor %s", keyId));
- return 0;
- }
-
- xs *sig_str = xs_str_new(NULL);
- {
- xs *l = xs_split(headers, " ");
- char *v;
- 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"));
- }
- else {
-
- if ((hc = xs_dict_get(req, v)) == NULL) {
- snac_debug(snac, 1,
- xs_fmt("check_signature cannot find header %s", v));
- return 0;
- }
- ss = xs_fmt("%s: %s", v, hc);
- }
- sig_str = xs_str_cat(sig_str, ss);
- }
- }
- if (xs_evp_verify(pubkey, sig_str, strlen(sig_str), signature) != 1) {
- snac_debug(snac, 1, xs_fmt("rsa verify error %s", keyId));
- }
- return 1;
- }
|