tangd.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright (c) 2016 Red Hat, Inc.
  4. * Author: Nathaniel McCallum <npmccallum@redhat.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "http.h"
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <limits.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <jose/jose.h>
  28. static void
  29. str_cleanup(char **str)
  30. {
  31. if (str)
  32. free(*str);
  33. }
  34. static void
  35. FILE_cleanup(FILE **file)
  36. {
  37. if (file && *file)
  38. fclose(*file);
  39. }
  40. static int
  41. adv(enum http_method method, const char *path, const char *body,
  42. regmatch_t matches[], void *misc)
  43. {
  44. __attribute__((cleanup(FILE_cleanup))) FILE *file = NULL;
  45. __attribute__((cleanup(str_cleanup))) char *adv = NULL;
  46. __attribute__((cleanup(str_cleanup))) char *thp = NULL;
  47. char filename[PATH_MAX] = {};
  48. const char *cachedir = misc;
  49. struct stat st = {};
  50. if (matches[1].rm_so < matches[1].rm_eo) {
  51. size_t size = matches[1].rm_eo - matches[1].rm_so;
  52. thp = strndup(&path[matches[1].rm_so], size);
  53. if (!thp)
  54. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  55. }
  56. if (snprintf(filename, sizeof(filename),
  57. "%s/%s.jws", cachedir, thp ? thp : "default") < 0)
  58. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  59. file = fopen(filename, "r");
  60. if (!file)
  61. return http_reply(HTTP_STATUS_NOT_FOUND, NULL);
  62. if (fstat(fileno(file), &st) != 0)
  63. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  64. adv = calloc(st.st_size + 1, 1);
  65. if (!adv)
  66. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  67. if (fread(adv, st.st_size, 1, file) != 1)
  68. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  69. return http_reply(HTTP_STATUS_OK,
  70. "Content-Type: application/jose+json\r\n"
  71. "Content-Length: %zu\r\n"
  72. "\r\n%s", strlen(adv), adv);
  73. }
  74. static int
  75. rec(enum http_method method, const char *path, const char *body,
  76. regmatch_t matches[], void *misc)
  77. {
  78. __attribute__((cleanup(str_cleanup))) char *enc = NULL;
  79. __attribute__((cleanup(str_cleanup))) char *thp = NULL;
  80. size_t size = matches[1].rm_eo - matches[1].rm_so;
  81. char filename[PATH_MAX] = {};
  82. const char *cachedir = misc;
  83. json_auto_t *jwk = NULL;
  84. json_auto_t *req = NULL;
  85. json_auto_t *rep = NULL;
  86. const char *alg = NULL;
  87. const char *kty = NULL;
  88. const char *d = NULL;
  89. /*
  90. * Parse and validate the request JWK
  91. */
  92. req = json_loads(body, 0, NULL);
  93. if (!req)
  94. return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
  95. if (!jose_jwk_prm(NULL, req, false, "deriveKey"))
  96. return http_reply(HTTP_STATUS_FORBIDDEN, NULL);
  97. if (json_unpack(req, "{s:s,s?s}", "kty", &kty, "alg", &alg) < 0)
  98. return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
  99. if (strcmp(kty, "EC") != 0)
  100. return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
  101. if (alg && strcmp(alg, "ECMR") != 0)
  102. return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
  103. /*
  104. * Parse and validate the server-side JWK
  105. */
  106. thp = strndup(&path[matches[1].rm_so], size);
  107. if (!thp)
  108. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  109. if (snprintf(filename, sizeof(filename), "%s/%s.jwk", cachedir, thp) < 0)
  110. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  111. jwk = json_load_file(filename, 0, NULL);
  112. if (!jwk)
  113. return http_reply(HTTP_STATUS_NOT_FOUND, NULL);
  114. if (!jose_jwk_prm(NULL, jwk, true, "deriveKey"))
  115. return http_reply(HTTP_STATUS_FORBIDDEN, NULL);
  116. if (json_unpack(jwk, "{s:s,s?s}", "d", &d, "alg", &alg) < 0)
  117. return http_reply(HTTP_STATUS_FORBIDDEN, NULL);
  118. if (alg && strcmp(alg, "ECMR") != 0)
  119. return http_reply(HTTP_STATUS_FORBIDDEN, NULL);
  120. /*
  121. * Perform the exchange and return
  122. */
  123. rep = jose_jwk_exc(NULL, jwk, req);
  124. if (!rep)
  125. return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
  126. if (json_object_set_new(rep, "alg", json_string("ECMR")) < 0)
  127. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  128. if (json_object_set_new(rep, "key_ops", json_pack("[s]", "deriveKey")) < 0)
  129. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  130. enc = json_dumps(rep, JSON_SORT_KEYS | JSON_COMPACT);
  131. if (!enc)
  132. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  133. return http_reply(HTTP_STATUS_OK,
  134. "Content-Type: application/jwk+json\r\n"
  135. "Content-Length: %zu\r\n"
  136. "\r\n%s", strlen(enc), enc);
  137. }
  138. static struct http_dispatch dispatch[] = {
  139. { adv, 1 << HTTP_GET, 2, "^/+adv/+([0-9A-Za-z_-]+)$" },
  140. { adv, 1 << HTTP_GET, 2, "^/+adv/*$" },
  141. { rec, 1 << HTTP_POST, 2, "^/+rec/+([0-9A-Za-z_-]+)$" },
  142. {}
  143. };
  144. int
  145. main(int argc, char *argv[])
  146. {
  147. struct http_state state = { .dispatch = dispatch, .misc = argv[1] };
  148. struct http_parser parser = { .data = &state };
  149. struct stat st = {};
  150. char req[4096] = {};
  151. size_t rcvd = 0;
  152. int r = 0;
  153. http_parser_init(&parser, HTTP_REQUEST);
  154. if (argc != 2) {
  155. fprintf(stderr, "Usage: %s <cachedir>\n", argv[0]);
  156. return EXIT_FAILURE;
  157. }
  158. if (stat(argv[1], &st) != 0) {
  159. fprintf(stderr, "Error calling stat() on path: %s: %m\n", argv[1]);
  160. return EXIT_FAILURE;
  161. }
  162. if (!S_ISDIR(st.st_mode)) {
  163. fprintf(stderr, "Path is not a directory: %s\n", argv[1]);
  164. return EXIT_FAILURE;
  165. }
  166. for (;;) {
  167. r = read(STDIN_FILENO, &req[rcvd], sizeof(req) - rcvd - 1);
  168. if (r == 0)
  169. return rcvd > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
  170. if (r < 0)
  171. return EXIT_FAILURE;
  172. rcvd += r;
  173. r = http_parser_execute(&parser, &http_settings, req, rcvd);
  174. if (parser.http_errno != 0) {
  175. fprintf(stderr, "HTTP Parsing Error: %s\n",
  176. http_errno_description(parser.http_errno));
  177. return EXIT_FAILURE;
  178. }
  179. memmove(req, &req[r], rcvd - r);
  180. rcvd -= r;
  181. }
  182. return EXIT_SUCCESS;
  183. }