http.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "snac.h"
  9. d_char *http_signed_request(snac *snac, char *method, char *url,
  10. d_char *headers,
  11. d_char *body, int b_size,
  12. int *status, d_char **payload, int *p_size)
  13. /* does a signed HTTP request */
  14. {
  15. xs *l1;
  16. xs *date;
  17. xs *digest;
  18. xs *s64;
  19. xs *signature;
  20. xs *hdrs;
  21. char *host;
  22. char *target;
  23. char *seckey;
  24. char *k, *v;
  25. d_char *response;
  26. date = xs_utc_time("%a, %d %b %Y %H:%M:%S GMT");
  27. {
  28. xs *s = xs_replace(url, "https:/" "/", "");
  29. l1 = xs_split_n(s, "/", 1);
  30. }
  31. /* strip the url to get host and target */
  32. host = xs_list_get(l1, 0);
  33. if (xs_list_len(l1) == 2)
  34. target = xs_list_get(l1, 1);
  35. else
  36. target = "";
  37. /* digest */
  38. {
  39. xs *s;
  40. if (body != NULL)
  41. s = xs_sha256_base64(body, b_size);
  42. else
  43. s = xs_sha256_base64("", 0);
  44. digest = xs_fmt("SHA-256=%s", s);
  45. }
  46. seckey = xs_dict_get(snac->key, "secret");
  47. {
  48. /* build the string to be signed */
  49. xs *s = xs_fmt("(request-target): %s /%s\n"
  50. "host: %s\n"
  51. "digest: %s\n"
  52. "date: %s",
  53. strcmp(method, "POST") == 0 ? "post" : "get",
  54. target, host, digest, date);
  55. s64 = xs_evp_sign(seckey, s, strlen(s));
  56. }
  57. /* build now the signature header */
  58. signature = xs_fmt("keyId=\"%s#main-key\","
  59. "algorithm=\"rsa-sha256\","
  60. "headers=\"(request-target) host digest date\","
  61. "signature=\"%s\"",
  62. snac->actor, s64);
  63. /* transfer the original headers */
  64. hdrs = xs_dict_new();
  65. while (xs_dict_iter(&headers, &k, &v))
  66. hdrs = xs_dict_append(hdrs, k, v);
  67. /* add the new headers */
  68. if (strcmp(method, "POST") == 0)
  69. hdrs = xs_dict_append(hdrs, "content-type", "application/activity+json");
  70. else
  71. hdrs = xs_dict_append(hdrs, "accept", "application/activity+json");
  72. hdrs = xs_dict_append(hdrs, "date", date);
  73. hdrs = xs_dict_append(hdrs, "signature", signature);
  74. hdrs = xs_dict_append(hdrs, "digest", digest);
  75. hdrs = xs_dict_append(hdrs, "host", host);
  76. hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
  77. response = xs_http_request(method, url, hdrs,
  78. body, b_size, status, payload, p_size);
  79. srv_archive("SEND", hdrs, body, b_size, *status, response, *payload, *p_size);
  80. return response;
  81. }
  82. int check_signature(snac *snac, char *req)
  83. /* check the signature */
  84. {
  85. char *sig_hdr = xs_dict_get(req, "signature");
  86. xs *keyId = NULL;
  87. xs *headers = NULL;
  88. xs *signature = NULL;
  89. xs *sig_bin = NULL;
  90. int s_size;
  91. char *pubkey;
  92. char *p;
  93. {
  94. /* extract the values */
  95. xs *l = xs_split(sig_hdr, ",");
  96. char *v;
  97. p = l;
  98. while (xs_list_iter(&p, &v)) {
  99. if (xs_startswith(v, "keyId"))
  100. keyId = xs_crop(xs_dup(v), 7, -1);
  101. else
  102. if (xs_startswith(v, "headers"))
  103. headers = xs_crop(xs_dup(v), 9, -1);
  104. else
  105. if (xs_startswith(v, "signature"))
  106. signature = xs_crop(xs_dup(v), 12, -1);
  107. }
  108. }
  109. if (keyId == NULL || headers == NULL || signature == NULL) {
  110. snac_debug(snac, 1, xs_fmt("bad signature header"));
  111. return 0;
  112. }
  113. /* strip the # from the keyId */
  114. if ((p = strchr(keyId, '#')) != NULL)
  115. *p = '\0';
  116. /* the actor must already be here */
  117. xs *actor = NULL;
  118. if (!valid_status(actor_get(snac, keyId, &actor))) {
  119. snac_debug(snac, 1, xs_fmt("check_signature unknown actor %s", keyId));
  120. return 0;
  121. }
  122. if ((p = xs_dict_get(actor, "publicKey")) == NULL ||
  123. ((pubkey = xs_dict_get(p, "publicKeyPem")) == NULL)) {
  124. snac_debug(snac, 1, xs_fmt("cannot get pubkey from actor %s", keyId));
  125. return 0;
  126. }
  127. /* now build the string to be signed */
  128. xs *sig_str = xs_str_new(NULL);
  129. {
  130. xs *l = xs_split(headers, " ");
  131. char *v;
  132. p = l;
  133. while (xs_list_iter(&p, &v)) {
  134. char *hc;
  135. xs *ss = NULL;
  136. if (*sig_str != '\0')
  137. sig_str = xs_str_cat(sig_str, "\n");
  138. if (strcmp(v, "(request-target)") == 0) {
  139. ss = xs_fmt("%s: post %s", v, xs_dict_get(req, "path"));
  140. }
  141. else {
  142. /* add the header */
  143. if ((hc = xs_dict_get(req, v)) == NULL) {
  144. snac_debug(snac, 1,
  145. xs_fmt("check_signature cannot find header %s", v));
  146. return 0;
  147. }
  148. ss = xs_fmt("%s: %s", v, hc);
  149. }
  150. sig_str = xs_str_cat(sig_str, ss);
  151. }
  152. }
  153. /* convert the signature to binary */
  154. sig_bin = xs_base64_dec(signature, &s_size);
  155. if (xs_evp_verify(pubkey, sig_str, strlen(sig_str), sig_bin) != 1) {
  156. snac_debug(snac, 1, xs_fmt("rsa verify error %s", keyId));
  157. }
  158. return 1;
  159. }