http.c 7.4 KB

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