http.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 grunfink - MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_encdec.h"
  6. #include "xs_openssl.h"
  7. #include "xs_curl.h"
  8. #include "snac.h"
  9. d_char *http_signed_request(snac *snac, char *method, char *url,
  10. d_char *headers,
  11. d_char *body, int b_size,
  12. int *status, d_char **payload, int *p_size)
  13. /* does a signed HTTP request */
  14. {
  15. xs *l1;
  16. xs *date;
  17. xs *digest;
  18. xs *s64;
  19. xs *signature;
  20. char *host;
  21. char *target;
  22. char *seckey;
  23. date = xs_utc_time("%a, %d %b %Y %H:%M:%S GMT");
  24. {
  25. xs *s = xs_replace(url, "https:/" "/", "");
  26. l1 = xs_split_n(s, "/", 1);
  27. }
  28. /* strip the url to get host and target */
  29. host = xs_list_get(l1, 0);
  30. if (xs_list_len(l1) == 2)
  31. target = xs_list_get(l1, 1);
  32. else
  33. target = "";
  34. /* digest */
  35. if (body != NULL)
  36. digest = xs_sha256_hex(body, b_size);
  37. else
  38. digest = xs_sha256_hex("", 0);
  39. seckey = xs_dict_get(snac->key, "secret");
  40. {
  41. /* build the string to be signed */
  42. xs *s = xs_fmt("(request-target): %s /%s\n"
  43. "host: %s\n"
  44. "digest: SHA-256=%s\n"
  45. "date: %s",
  46. strcmp(method, "POST") == 0 ? "post" : "get",
  47. target, host, digest, date);
  48. s64 = xs_rsa_sign(seckey, s, strlen(s));
  49. }
  50. /* build now the signature header */
  51. signature = xs_fmt("keyId=\"%s#main-key\","
  52. "algorithm=\"rsa-sha256\","
  53. "headers=\"(request-target) host digest date\","
  54. "signature=\"%s\"",
  55. snac->actor, s64);
  56. /* now add all these things to the headers */
  57. headers = xs_dict_append(headers, "content-type", "application/activity+json");
  58. headers = xs_dict_append(headers, "date", date);
  59. headers = xs_dict_append(headers, "signature", signature);
  60. headers = xs_dict_append(headers, "digest", digest);
  61. headers = xs_dict_append(headers, "user-agent", "snac/2.x");
  62. // return xs_http_request(method, url, headers,
  63. // body, b_size, status, payload, p_size);
  64. return NULL;
  65. }