http.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_openssl.h"
  6. #include "xs_curl.h"
  7. #include "xs_time.h"
  8. #include "xs_json.h"
  9. #include "xs_http.h"
  10. #include "snac.h"
  11. xs_dict *http_signed_request_raw(const char *keyid, const char *seckey,
  12. const char *method, const char *url,
  13. const xs_dict *headers,
  14. const char *body, int b_size,
  15. int *status, xs_str **payload, int *p_size,
  16. int timeout)
  17. /* does a signed HTTP request */
  18. {
  19. xs *l1 = NULL;
  20. xs *date = NULL;
  21. xs *digest = NULL;
  22. xs *s64 = NULL;
  23. xs *signature = NULL;
  24. xs *hdrs = NULL;
  25. const char *host;
  26. const char *target;
  27. const char *k, *v;
  28. xs_dict *response;
  29. date = xs_str_utctime(0, "%a, %d %b %Y %H:%M:%S GMT");
  30. {
  31. xs *s1 = xs_replace_n(url, "http:/" "/", "", 1);
  32. xs *s = xs_replace_n(s1, "https:/" "/", "", 1);
  33. l1 = xs_split_n(s, "/", 1);
  34. }
  35. /* strip the url to get host and target */
  36. host = xs_list_get(l1, 0);
  37. if (xs_list_len(l1) == 2)
  38. target = xs_list_get(l1, 1);
  39. else
  40. target = "";
  41. /* digest */
  42. {
  43. xs *s;
  44. if (body != NULL)
  45. s = xs_sha256_base64(body, b_size);
  46. else
  47. s = xs_sha256_base64("", 0);
  48. digest = xs_fmt("SHA-256=%s", s);
  49. }
  50. {
  51. /* build the string to be signed */
  52. xs *s = xs_fmt("(request-target): %s /%s\n"
  53. "host: %s\n"
  54. "digest: %s\n"
  55. "date: %s",
  56. strcmp(method, "POST") == 0 ? "post" : "get",
  57. target, host, digest, date);
  58. s64 = xs_evp_sign(seckey, s, strlen(s));
  59. }
  60. /* build now the signature header */
  61. signature = xs_fmt("keyId=\"%s#main-key\","
  62. "algorithm=\"rsa-sha256\","
  63. "headers=\"(request-target) host digest date\","
  64. "signature=\"%s\"",
  65. keyid, s64);
  66. /* transfer the original headers */
  67. hdrs = xs_dict_new();
  68. int c = 0;
  69. while (xs_dict_next(headers, &k, &v, &c))
  70. hdrs = xs_dict_append(hdrs, k, v);
  71. /* add the new headers */
  72. if (strcmp(method, "POST") == 0)
  73. hdrs = xs_dict_append(hdrs, "content-type", "application/activity+json");
  74. else
  75. hdrs = xs_dict_append(hdrs, "accept", "application/activity+json");
  76. xs *user_agent = xs_fmt("%s; +%s/", USER_AGENT, srv_baseurl);
  77. hdrs = xs_dict_append(hdrs, "date", date);
  78. hdrs = xs_dict_append(hdrs, "signature", signature);
  79. hdrs = xs_dict_append(hdrs, "digest", digest);
  80. hdrs = xs_dict_append(hdrs, "host", host);
  81. hdrs = xs_dict_append(hdrs, "user-agent", user_agent);
  82. response = xs_http_request(method, url, hdrs,
  83. body, b_size, status, payload, p_size, timeout);
  84. srv_archive("SEND", url, hdrs, body, b_size, *status, response, *payload, *p_size);
  85. return response;
  86. }
  87. xs_dict *http_signed_request(snac *snac, const char *method, const char *url,
  88. const xs_dict *headers,
  89. const char *body, int b_size,
  90. int *status, xs_str **payload, int *p_size,
  91. int timeout)
  92. /* does a signed HTTP request */
  93. {
  94. const char *seckey = xs_dict_get(snac->key, "secret");
  95. xs_dict *response;
  96. response = http_signed_request_raw(snac->actor, seckey, method, url,
  97. headers, body, b_size, status, payload, p_size, timeout);
  98. return response;
  99. }
  100. int check_signature(const xs_dict *req, xs_str **err)
  101. /* check the signature */
  102. {
  103. const char *sig_hdr = xs_dict_get(req, "signature");
  104. xs *keyId = NULL;
  105. xs *headers = NULL;
  106. xs *signature = NULL;
  107. xs *created = NULL;
  108. xs *expires = NULL;
  109. char *p;
  110. const char *pubkey;
  111. const char *k;
  112. if (xs_is_null(sig_hdr)) {
  113. *err = xs_fmt("missing 'signature' header");
  114. return 0;
  115. }
  116. {
  117. /* extract the values */
  118. xs *l = xs_split(sig_hdr, ",");
  119. int c = 0;
  120. const xs_val *v;
  121. while (xs_list_next(l, &v, &c)) {
  122. xs *kv = xs_split_n(v, "=", 1);
  123. if (xs_list_len(kv) != 2)
  124. continue;
  125. xs *k1 = xs_strip_i(xs_dup(xs_list_get(kv, 0)));
  126. xs *v1 = xs_strip_chars_i(xs_dup(xs_list_get(kv, 1)), " \"");
  127. if (!strcmp(k1, "keyId"))
  128. keyId = xs_dup(v1);
  129. else
  130. if (!strcmp(k1, "headers"))
  131. headers = xs_dup(v1);
  132. else
  133. if (!strcmp(k1, "signature"))
  134. signature = xs_dup(v1);
  135. else
  136. if (!strcmp(k1, "created"))
  137. created = xs_dup(v1);
  138. else
  139. if (!strcmp(k1, "expires"))
  140. expires = xs_dup(v1);
  141. }
  142. }
  143. if (keyId == NULL || headers == NULL || signature == NULL) {
  144. *err = xs_fmt("bad signature header");
  145. return 0;
  146. }
  147. /* strip the # from the keyId */
  148. if ((p = strchr(keyId, '#')) != NULL)
  149. *p = '\0';
  150. /* also strip cgi variables */
  151. if ((p = strchr(keyId, '?')) != NULL)
  152. *p = '\0';
  153. xs *actor = NULL;
  154. int status;
  155. if (!valid_status((status = actor_request(NULL, keyId, &actor)))) {
  156. *err = xs_fmt("actor request error %s %d", keyId, status);
  157. return 0;
  158. }
  159. if ((k = xs_dict_get(actor, "publicKey")) == NULL ||
  160. ((pubkey = xs_dict_get(k, "publicKeyPem")) == NULL)) {
  161. *err = xs_fmt("cannot get pubkey from %s", keyId);
  162. return 0;
  163. }
  164. /* now build the string to be signed */
  165. xs *sig_str = xs_str_new(NULL);
  166. {
  167. xs *l = xs_split(headers, " ");
  168. xs_list *p;
  169. const xs_val *v;
  170. p = l;
  171. while (xs_list_iter(&p, &v)) {
  172. const char *hc;
  173. xs *ss = NULL;
  174. if (*sig_str != '\0')
  175. sig_str = xs_str_cat(sig_str, "\n");
  176. if (strcmp(v, "(request-target)") == 0) {
  177. ss = xs_fmt("%s: post %s", v, xs_dict_get(req, "path"));
  178. }
  179. else
  180. if (strcmp(v, "(created)") == 0) {
  181. ss = xs_fmt("%s: %s", v, created);
  182. }
  183. else
  184. if (strcmp(v, "(expires)") == 0) {
  185. ss = xs_fmt("%s: %s", v, expires);
  186. }
  187. else
  188. if (strcmp(v, "host") == 0) {
  189. hc = xs_dict_get(req, "host");
  190. /* if there is no host header or some garbage like
  191. address:host has arrived here due to misconfiguration,
  192. signature verify will totally fail, so let's Leroy Jenkins
  193. with the global server hostname instead */
  194. if (hc == NULL || xs_str_in(hc, ":") != -1)
  195. hc = xs_dict_get(srv_config, "host");
  196. ss = xs_fmt("host: %s", hc);
  197. }
  198. else {
  199. /* add the header */
  200. if ((hc = xs_dict_get(req, v)) == NULL) {
  201. *err = xs_fmt("cannot find header '%s'", v);
  202. return 0;
  203. }
  204. ss = xs_fmt("%s: %s", v, hc);
  205. }
  206. sig_str = xs_str_cat(sig_str, ss);
  207. }
  208. }
  209. if (xs_evp_verify(pubkey, sig_str, strlen(sig_str), signature) != 1) {
  210. *err = xs_fmt("RSA verify error %s", keyId);
  211. return 0;
  212. }
  213. return 1;
  214. }