http.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 *actor, 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(xs_dup(v), 7, -1);
  103. else
  104. if (xs_startswith(v, "headers"))
  105. headers = xs_crop(xs_dup(v), 9, -1);
  106. else
  107. if (xs_startswith(v, "signature"))
  108. signature = xs_crop(xs_dup(v), 11, -1);
  109. else
  110. if (xs_startswith(v, "created"))
  111. created = xs_crop(xs_dup(v), 9, -1);
  112. else
  113. if (xs_startswith(v, "expires"))
  114. expires = xs_crop(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. #if 0
  122. /* strip the # from the keyId */
  123. if ((p = strchr(keyId, '#')) != NULL)
  124. *p = '\0';
  125. /* the actor must already be here */
  126. xs *actor = NULL;
  127. if (!valid_status(actor_get(snac, keyId, &actor))) {
  128. *err = xs_fmt("unknown actor %s", keyId);
  129. return 0;
  130. }
  131. #endif
  132. if ((p = xs_dict_get(actor, "publicKey")) == NULL ||
  133. ((pubkey = xs_dict_get(p, "publicKeyPem")) == NULL)) {
  134. *err = xs_fmt("cannot get pubkey from %s", keyId);
  135. return 0;
  136. }
  137. /* now build the string to be signed */
  138. xs *sig_str = xs_str_new(NULL);
  139. {
  140. xs *l = xs_split(headers, " ");
  141. char *v;
  142. p = l;
  143. while (xs_list_iter(&p, &v)) {
  144. char *hc;
  145. xs *ss = NULL;
  146. if (*sig_str != '\0')
  147. sig_str = xs_str_cat(sig_str, "\n");
  148. if (strcmp(v, "(request-target)") == 0) {
  149. ss = xs_fmt("%s: post %s", v, xs_dict_get(req, "path"));
  150. }
  151. else
  152. if (strcmp(v, "(created)") == 0) {
  153. ss = xs_fmt("%s: %s", v, created);
  154. }
  155. else
  156. if (strcmp(v, "(expires)") == 0) {
  157. ss = xs_fmt("%s: %s", v, expires);
  158. }
  159. else {
  160. /* add the header */
  161. if ((hc = xs_dict_get(req, v)) == NULL) {
  162. *err = xs_fmt("cannot find header '%s'", v);
  163. return 0;
  164. }
  165. ss = xs_fmt("%s: %s", v, hc);
  166. }
  167. sig_str = xs_str_cat(sig_str, ss);
  168. }
  169. }
  170. if (xs_evp_verify(pubkey, sig_str, strlen(sig_str), signature) != 1) {
  171. *err = xs_fmt("RSA verify error %s", keyId);
  172. return 0;
  173. }
  174. return 1;
  175. }
  176. int check_signature(snac *snac, char *req, char *actor)
  177. /* checks the signature and archives the error */
  178. {
  179. int ret;
  180. xs *err = NULL;
  181. if ((ret = _check_signature(snac, req, actor, &err)) == 0) {
  182. snac_debug(snac, 1, xs_fmt("check_signature %s", err));
  183. xs *ntid = tid(0);
  184. xs *fn = xs_fmt("%s/error/check_signature_%s", srv_basedir, ntid);
  185. FILE *f;
  186. if ((f = fopen(fn, "w")) != NULL) {
  187. fprintf(f, "Error: %s\nRequest headers:\n", err);
  188. xs *j = xs_json_dumps_pp(req, 4);
  189. fwrite(j, strlen(j), 1, f);
  190. fclose(f);
  191. }
  192. }
  193. return ret;
  194. }