http.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "snac.h"
  10. d_char *http_signed_request(snac *snac, char *method, char *url,
  11. d_char *headers,
  12. d_char *body, int b_size,
  13. int *status, d_char **payload, int *p_size)
  14. /* does a signed HTTP request */
  15. {
  16. xs *l1;
  17. xs *date;
  18. xs *digest;
  19. xs *s64;
  20. xs *signature;
  21. xs *hdrs;
  22. char *host;
  23. char *target;
  24. char *seckey;
  25. char *k, *v;
  26. d_char *response;
  27. date = xs_str_utctime(0, "%a, %d %b %Y %H:%M:%S GMT");
  28. {
  29. xs *s = xs_replace(url, "https:/" "/", "");
  30. l1 = xs_split_n(s, "/", 1);
  31. }
  32. /* strip the url to get host and target */
  33. host = xs_list_get(l1, 0);
  34. if (xs_list_len(l1) == 2)
  35. target = xs_list_get(l1, 1);
  36. else
  37. target = "";
  38. /* digest */
  39. {
  40. xs *s;
  41. if (body != NULL)
  42. s = xs_sha256_base64(body, b_size);
  43. else
  44. s = xs_sha256_base64("", 0);
  45. digest = xs_fmt("SHA-256=%s", s);
  46. }
  47. seckey = xs_dict_get(snac->key, "secret");
  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. snac->actor, 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. hdrs = xs_dict_append(hdrs, "date", date);
  74. hdrs = xs_dict_append(hdrs, "signature", signature);
  75. hdrs = xs_dict_append(hdrs, "digest", digest);
  76. hdrs = xs_dict_append(hdrs, "host", host);
  77. hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
  78. response = xs_http_request(method, url, hdrs,
  79. body, b_size, status, payload, p_size);
  80. srv_archive("SEND", hdrs, body, b_size, *status, response, *payload, *p_size);
  81. return response;
  82. }
  83. int check_signature(snac *snac, char *req)
  84. /* check the signature */
  85. {
  86. char *sig_hdr = xs_dict_get(req, "signature");
  87. xs *keyId = NULL;
  88. xs *headers = NULL;
  89. xs *signature = NULL;
  90. xs *created = NULL;
  91. xs *expires = NULL;
  92. char *pubkey;
  93. char *p;
  94. {
  95. /* extract the values */
  96. xs *l = xs_split(sig_hdr, ",");
  97. char *v;
  98. p = l;
  99. while (xs_list_iter(&p, &v)) {
  100. if (xs_startswith(v, "keyId"))
  101. keyId = xs_crop(xs_dup(v), 7, -1);
  102. else
  103. if (xs_startswith(v, "headers"))
  104. headers = xs_crop(xs_dup(v), 9, -1);
  105. else
  106. if (xs_startswith(v, "signature"))
  107. signature = xs_crop(xs_dup(v), 11, -1);
  108. else
  109. if (xs_startswith(v, "created"))
  110. created = xs_crop(xs_dup(v), 9, -1);
  111. else
  112. if (xs_startswith(v, "expires"))
  113. expires = xs_crop(xs_dup(v), 9, -1);
  114. }
  115. }
  116. if (keyId == NULL || headers == NULL || signature == NULL) {
  117. snac_debug(snac, 0, xs_fmt("check_signature bad signature header"));
  118. goto error;
  119. }
  120. /* strip the # from the keyId */
  121. if ((p = strchr(keyId, '#')) != NULL)
  122. *p = '\0';
  123. /* the actor must already be here */
  124. xs *actor = NULL;
  125. if (!valid_status(actor_get(snac, keyId, &actor))) {
  126. snac_debug(snac, 0, xs_fmt("check_signature unknown actor %s", keyId));
  127. goto error;
  128. }
  129. if ((p = xs_dict_get(actor, "publicKey")) == NULL ||
  130. ((pubkey = xs_dict_get(p, "publicKeyPem")) == NULL)) {
  131. snac_debug(snac, 0, xs_fmt("check_signature cannot get pubkey from %s", keyId));
  132. goto error;
  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. snac_debug(snac, 0,
  160. xs_fmt("check_signature cannot find header %s", v));
  161. goto error;
  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. snac_debug(snac, 0, xs_fmt("check_signature rsa verify error %s", keyId));
  170. goto error;
  171. }
  172. return 1;
  173. error:
  174. return 0;
  175. }