http.c 7.4 KB

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