http.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. xs_dict *http_signed_request_raw(const char *keyid, const char *seckey,
  12. const char *method, const char *url,
  13. xs_dict *headers,
  14. const char *body, int b_size,
  15. int *status, xs_str **payload, int *p_size,
  16. int timeout)
  17. /* does a signed HTTP request */
  18. {
  19. xs *l1 = NULL;
  20. xs *date = NULL;
  21. xs *digest = NULL;
  22. xs *s64 = NULL;
  23. xs *signature = NULL;
  24. xs *hdrs = NULL;
  25. char *host;
  26. char *target;
  27. char *k, *v;
  28. xs_dict *response;
  29. date = xs_str_utctime(0, "%a, %d %b %Y %H:%M:%S GMT");
  30. {
  31. xs *s = xs_replace_n(url, "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. 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. xs *user_agent = xs_fmt("%s; +%s/", USER_AGENT, srv_baseurl);
  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", url, hdrs, body, b_size, *status, response, *payload, *p_size);
  83. return response;
  84. }
  85. xs_dict *http_signed_request(snac *snac, const char *method, const char *url,
  86. xs_dict *headers,
  87. const char *body, int b_size,
  88. int *status, xs_str **payload, int *p_size,
  89. int timeout)
  90. /* does a signed HTTP request */
  91. {
  92. char *seckey = xs_dict_get(snac->key, "secret");
  93. xs_dict *response;
  94. response = http_signed_request_raw(snac->actor, seckey, method, url,
  95. headers, body, b_size, status, payload, p_size, timeout);
  96. return response;
  97. }
  98. int check_signature(snac *snac, xs_dict *req, xs_str **err)
  99. /* check the signature */
  100. {
  101. char *sig_hdr = xs_dict_get(req, "signature");
  102. xs *keyId = NULL;
  103. xs *headers = NULL;
  104. xs *signature = NULL;
  105. xs *created = NULL;
  106. xs *expires = NULL;
  107. char *pubkey;
  108. char *p;
  109. {
  110. /* extract the values */
  111. xs *l = xs_split(sig_hdr, ",");
  112. xs_list *p;
  113. xs_val *v;
  114. p = l;
  115. while (xs_list_iter(&p, &v)) {
  116. if (xs_startswith(v, "keyId"))
  117. keyId = xs_crop_i(xs_dup(v), 7, -1);
  118. else
  119. if (xs_startswith(v, "headers"))
  120. headers = xs_crop_i(xs_dup(v), 9, -1);
  121. else
  122. if (xs_startswith(v, "signature"))
  123. signature = xs_crop_i(xs_dup(v), 11, -1);
  124. else
  125. if (xs_startswith(v, "created"))
  126. created = xs_crop_i(xs_dup(v), 9, -1);
  127. else
  128. if (xs_startswith(v, "expires"))
  129. expires = xs_crop_i(xs_dup(v), 9, -1);
  130. }
  131. }
  132. if (keyId == NULL || headers == NULL || signature == NULL) {
  133. *err = xs_fmt("bad signature header");
  134. return 0;
  135. }
  136. /* strip the # from the keyId */
  137. if ((p = strchr(keyId, '#')) != NULL)
  138. *p = '\0';
  139. xs *actor = NULL;
  140. if (!valid_status(actor_request(snac, keyId, &actor))) {
  141. *err = xs_fmt("unknown actor %s", keyId);
  142. return 0;
  143. }
  144. if ((p = xs_dict_get(actor, "publicKey")) == NULL ||
  145. ((pubkey = xs_dict_get(p, "publicKeyPem")) == NULL)) {
  146. *err = xs_fmt("cannot get pubkey from %s", keyId);
  147. return 0;
  148. }
  149. /* now build the string to be signed */
  150. xs *sig_str = xs_str_new(NULL);
  151. {
  152. xs *l = xs_split(headers, " ");
  153. xs_list *p;
  154. xs_val *v;
  155. p = l;
  156. while (xs_list_iter(&p, &v)) {
  157. char *hc;
  158. xs *ss = NULL;
  159. if (*sig_str != '\0')
  160. sig_str = xs_str_cat(sig_str, "\n");
  161. if (strcmp(v, "(request-target)") == 0) {
  162. ss = xs_fmt("%s: post %s", v, xs_dict_get(req, "path"));
  163. }
  164. else
  165. if (strcmp(v, "(created)") == 0) {
  166. ss = xs_fmt("%s: %s", v, created);
  167. }
  168. else
  169. if (strcmp(v, "(expires)") == 0) {
  170. ss = xs_fmt("%s: %s", v, expires);
  171. }
  172. else {
  173. /* add the header */
  174. if ((hc = xs_dict_get(req, v)) == NULL) {
  175. *err = xs_fmt("cannot find header '%s'", v);
  176. return 0;
  177. }
  178. ss = xs_fmt("%s: %s", v, hc);
  179. }
  180. sig_str = xs_str_cat(sig_str, ss);
  181. }
  182. }
  183. if (xs_evp_verify(pubkey, sig_str, strlen(sig_str), signature) != 1) {
  184. *err = xs_fmt("RSA verify error %s", keyId);
  185. return 0;
  186. }
  187. return 1;
  188. }