http.c 6.3 KB

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