http.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_encdec.h"
  6. #include "xs_openssl.h"
  7. #include "xs_curl.h"
  8. #include "xs_time.h"
  9. #include "xs_json.h"
  10. #include "snac.h"
  11. d_char *http_signed_request(snac *snac, char *method, char *url,
  12. d_char *headers,
  13. d_char *body, int b_size,
  14. int *status, d_char **payload, int *p_size,
  15. int timeout)
  16. /* does a signed HTTP request */
  17. {
  18. xs *l1;
  19. xs *date;
  20. xs *digest;
  21. xs *s64;
  22. xs *signature;
  23. xs *hdrs;
  24. char *host;
  25. char *target;
  26. char *seckey;
  27. char *k, *v;
  28. d_char *response;
  29. date = xs_str_utctime(0, "%a, %d %b %Y %H:%M:%S GMT");
  30. {
  31. xs *s = xs_replace(url, "https:/" "/", "");
  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. seckey = xs_dict_get(snac->key, "secret");
  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. snac->actor, s64);
  66. /* transfer the original headers */
  67. hdrs = xs_dict_new();
  68. while (xs_dict_iter(&headers, &k, &v))
  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. 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", hdrs, body, b_size, *status, response, *payload, *p_size);
  83. return response;
  84. }
  85. static int _check_signature(snac *snac, char *req, char **err)
  86. /* check the signature */
  87. {
  88. char *sig_hdr = xs_dict_get(req, "signature");
  89. xs *keyId = NULL;
  90. xs *headers = NULL;
  91. xs *signature = NULL;
  92. xs *created = NULL;
  93. xs *expires = NULL;
  94. char *pubkey;
  95. char *p;
  96. {
  97. /* extract the values */
  98. xs *l = xs_split(sig_hdr, ",");
  99. char *v;
  100. p = l;
  101. while (xs_list_iter(&p, &v)) {
  102. if (xs_startswith(v, "keyId"))
  103. keyId = xs_crop_i(xs_dup(v), 7, -1);
  104. else
  105. if (xs_startswith(v, "headers"))
  106. headers = xs_crop_i(xs_dup(v), 9, -1);
  107. else
  108. if (xs_startswith(v, "signature"))
  109. signature = xs_crop_i(xs_dup(v), 11, -1);
  110. else
  111. if (xs_startswith(v, "created"))
  112. created = xs_crop_i(xs_dup(v), 9, -1);
  113. else
  114. if (xs_startswith(v, "expires"))
  115. expires = xs_crop_i(xs_dup(v), 9, -1);
  116. }
  117. }
  118. if (keyId == NULL || headers == NULL || signature == NULL) {
  119. *err = xs_fmt("bad signature header");
  120. return 0;
  121. }
  122. /* strip the # from the keyId */
  123. if ((p = strchr(keyId, '#')) != NULL)
  124. *p = '\0';
  125. xs *actor = NULL;
  126. if (!valid_status(actor_request(snac, keyId, &actor))) {
  127. *err = xs_fmt("unknown actor %s", keyId);
  128. return 0;
  129. }
  130. if ((p = xs_dict_get(actor, "publicKey")) == NULL ||
  131. ((pubkey = xs_dict_get(p, "publicKeyPem")) == NULL)) {
  132. *err = xs_fmt("cannot get pubkey from %s", keyId);
  133. return 0;
  134. }
  135. /* now build the string to be signed */
  136. xs *sig_str = xs_str_new(NULL);
  137. {
  138. xs *l = xs_split(headers, " ");
  139. char *v;
  140. p = l;
  141. while (xs_list_iter(&p, &v)) {
  142. char *hc;
  143. xs *ss = NULL;
  144. if (*sig_str != '\0')
  145. sig_str = xs_str_cat(sig_str, "\n");
  146. if (strcmp(v, "(request-target)") == 0) {
  147. ss = xs_fmt("%s: post %s", v, xs_dict_get(req, "path"));
  148. }
  149. else
  150. if (strcmp(v, "(created)") == 0) {
  151. ss = xs_fmt("%s: %s", v, created);
  152. }
  153. else
  154. if (strcmp(v, "(expires)") == 0) {
  155. ss = xs_fmt("%s: %s", v, expires);
  156. }
  157. else {
  158. /* add the header */
  159. if ((hc = xs_dict_get(req, v)) == NULL) {
  160. *err = xs_fmt("cannot find header '%s'", v);
  161. return 0;
  162. }
  163. ss = xs_fmt("%s: %s", v, hc);
  164. }
  165. sig_str = xs_str_cat(sig_str, ss);
  166. }
  167. }
  168. if (xs_evp_verify(pubkey, sig_str, strlen(sig_str), signature) != 1) {
  169. *err = xs_fmt("RSA verify error %s", keyId);
  170. return 0;
  171. }
  172. return 1;
  173. }
  174. int check_signature(snac *snac, char *req)
  175. /* checks the signature and archives the error */
  176. {
  177. int ret;
  178. xs *err = NULL;
  179. if ((ret = _check_signature(snac, req, &err)) == 0) {
  180. snac_debug(snac, 1, xs_fmt("check_signature %s", err));
  181. xs *ntid = tid(0);
  182. xs *fn = xs_fmt("%s/error/check_signature_%s", srv_basedir, ntid);
  183. FILE *f;
  184. if ((f = fopen(fn, "w")) != NULL) {
  185. fprintf(f, "Error: %s\nRequest headers:\n", err);
  186. xs *j = xs_json_dumps_pp(req, 4);
  187. fwrite(j, strlen(j), 1, f);
  188. fclose(f);
  189. }
  190. }
  191. return ret;
  192. }