http.c 7.3 KB

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