http.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. const 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. const char *host;
  25. const char *target;
  26. const 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. const 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. const 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(const xs_dict *req, xs_str **err)
  100. /* check the signature */
  101. {
  102. const 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 *p;
  109. const char *pubkey;
  110. const char *k;
  111. if (xs_is_null(sig_hdr)) {
  112. *err = xs_fmt("missing 'signature' header");
  113. return 0;
  114. }
  115. {
  116. /* extract the values */
  117. xs *l = xs_split(sig_hdr, ",");
  118. int c = 0;
  119. const xs_val *v;
  120. while (xs_list_next(l, &v, &c)) {
  121. xs *kv = xs_split_n(v, "=", 1);
  122. if (xs_list_len(kv) != 2)
  123. continue;
  124. xs *k1 = xs_strip_i(xs_dup(xs_list_get(kv, 0)));
  125. xs *v1 = xs_strip_chars_i(xs_dup(xs_list_get(kv, 1)), " \"");
  126. if (!strcmp(k1, "keyId"))
  127. keyId = xs_dup(v1);
  128. else
  129. if (!strcmp(k1, "headers"))
  130. headers = xs_dup(v1);
  131. else
  132. if (!strcmp(k1, "signature"))
  133. signature = xs_dup(v1);
  134. else
  135. if (!strcmp(k1, "created"))
  136. created = xs_dup(v1);
  137. else
  138. if (!strcmp(k1, "expires"))
  139. expires = xs_dup(v1);
  140. }
  141. }
  142. if (keyId == NULL || headers == NULL || signature == NULL) {
  143. *err = xs_fmt("bad signature header");
  144. return 0;
  145. }
  146. /* strip the # from the keyId */
  147. if ((p = strchr(keyId, '#')) != NULL)
  148. *p = '\0';
  149. /* also strip cgi variables */
  150. if ((p = strchr(keyId, '?')) != NULL)
  151. *p = '\0';
  152. xs *actor = NULL;
  153. int status;
  154. if (!valid_status((status = actor_request(NULL, keyId, &actor)))) {
  155. *err = xs_fmt("actor request error %s %d", keyId, status);
  156. return 0;
  157. }
  158. if ((k = xs_dict_get(actor, "publicKey")) == NULL ||
  159. ((pubkey = xs_dict_get(k, "publicKeyPem")) == NULL)) {
  160. *err = xs_fmt("cannot get pubkey from %s", keyId);
  161. return 0;
  162. }
  163. /* now build the string to be signed */
  164. xs *sig_str = xs_str_new(NULL);
  165. {
  166. xs *l = xs_split(headers, " ");
  167. xs_list *p;
  168. const xs_val *v;
  169. p = l;
  170. while (xs_list_iter(&p, &v)) {
  171. const char *hc;
  172. xs *ss = NULL;
  173. if (*sig_str != '\0')
  174. sig_str = xs_str_cat(sig_str, "\n");
  175. if (strcmp(v, "(request-target)") == 0) {
  176. ss = xs_fmt("%s: post %s", v, xs_dict_get(req, "path"));
  177. }
  178. else
  179. if (strcmp(v, "(created)") == 0) {
  180. ss = xs_fmt("%s: %s", v, created);
  181. }
  182. else
  183. if (strcmp(v, "(expires)") == 0) {
  184. ss = xs_fmt("%s: %s", v, expires);
  185. }
  186. else
  187. if (strcmp(v, "host") == 0) {
  188. hc = xs_dict_get(req, "host");
  189. /* if there is no host header or some garbage like
  190. address:host has arrived here due to misconfiguration,
  191. signature verify will totally fail, so let's Leroy Jenkins
  192. with the global server hostname instead */
  193. if (hc == NULL || xs_str_in(hc, ":") != -1)
  194. hc = xs_dict_get(srv_config, "host");
  195. ss = xs_fmt("host: %s", hc);
  196. }
  197. else {
  198. /* add the header */
  199. if ((hc = xs_dict_get(req, v)) == NULL) {
  200. *err = xs_fmt("cannot find header '%s'", v);
  201. return 0;
  202. }
  203. ss = xs_fmt("%s: %s", v, hc);
  204. }
  205. sig_str = xs_str_cat(sig_str, ss);
  206. }
  207. }
  208. if (xs_evp_verify(pubkey, sig_str, strlen(sig_str), signature) != 1) {
  209. *err = xs_fmt("RSA verify error %s", keyId);
  210. return 0;
  211. }
  212. return 1;
  213. }