http.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. char *pubkey;
  91. char *p;
  92. {
  93. /* extract the values */
  94. xs *l = xs_split(sig_hdr, ",");
  95. char *v;
  96. p = l;
  97. while (xs_list_iter(&p, &v)) {
  98. if (xs_startswith(v, "keyId"))
  99. keyId = xs_crop(xs_dup(v), 7, -1);
  100. else
  101. if (xs_startswith(v, "headers"))
  102. headers = xs_crop(xs_dup(v), 9, -1);
  103. else
  104. if (xs_startswith(v, "signature"))
  105. signature = xs_crop(xs_dup(v), 11, -1);
  106. }
  107. }
  108. if (keyId == NULL || headers == NULL || signature == NULL) {
  109. snac_debug(snac, 1, xs_fmt("bad signature header"));
  110. return 0;
  111. }
  112. /* strip the # from the keyId */
  113. if ((p = strchr(keyId, '#')) != NULL)
  114. *p = '\0';
  115. /* the actor must already be here */
  116. xs *actor = NULL;
  117. if (!valid_status(actor_get(snac, keyId, &actor))) {
  118. snac_debug(snac, 1, xs_fmt("check_signature unknown actor %s", keyId));
  119. return 0;
  120. }
  121. if ((p = xs_dict_get(actor, "publicKey")) == NULL ||
  122. ((pubkey = xs_dict_get(p, "publicKeyPem")) == NULL)) {
  123. snac_debug(snac, 1, xs_fmt("cannot get pubkey from actor %s", keyId));
  124. return 0;
  125. }
  126. /* now build the string to be signed */
  127. xs *sig_str = xs_str_new(NULL);
  128. {
  129. xs *l = xs_split(headers, " ");
  130. char *v;
  131. p = l;
  132. while (xs_list_iter(&p, &v)) {
  133. char *hc;
  134. xs *ss = NULL;
  135. if (*sig_str != '\0')
  136. sig_str = xs_str_cat(sig_str, "\n");
  137. if (strcmp(v, "(request-target)") == 0) {
  138. ss = xs_fmt("%s: post %s", v, xs_dict_get(req, "path"));
  139. }
  140. else {
  141. /* add the header */
  142. if ((hc = xs_dict_get(req, v)) == NULL) {
  143. snac_debug(snac, 1,
  144. xs_fmt("check_signature cannot find header %s", v));
  145. return 0;
  146. }
  147. ss = xs_fmt("%s: %s", v, hc);
  148. }
  149. sig_str = xs_str_cat(sig_str, ss);
  150. }
  151. }
  152. if (xs_evp_verify(pubkey, sig_str, strlen(sig_str), signature) != 1) {
  153. snac_debug(snac, 1, xs_fmt("rsa verify error %s", keyId));
  154. }
  155. return 1;
  156. }