http.c 5.2 KB

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