tangd.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include "keys.h"
  29. static void
  30. str_cleanup(char **str)
  31. {
  32. if (str)
  33. free(*str);
  34. }
  35. static int
  36. adv(enum http_method method, const char *path, const char *body,
  37. regmatch_t matches[], void *misc)
  38. {
  39. __attribute__((cleanup(str_cleanup))) char *adv = NULL;
  40. __attribute__((cleanup(str_cleanup))) char *thp = NULL;
  41. __attribute__((cleanup(cleanup_tang_keys_info))) struct tang_keys_info *tki = NULL;
  42. json_auto_t *jws = NULL;
  43. const char *jwkdir = misc;
  44. tki = read_keys(jwkdir);
  45. if (!tki || tki->m_keys_count == 0) {
  46. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  47. }
  48. if (matches[1].rm_so < matches[1].rm_eo) {
  49. size_t size = matches[1].rm_eo - matches[1].rm_so;
  50. thp = strndup(&path[matches[1].rm_so], size);
  51. if (!thp)
  52. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  53. }
  54. jws = find_jws(tki, thp);
  55. if (!jws) {
  56. return http_reply(HTTP_STATUS_NOT_FOUND, NULL);
  57. }
  58. adv = json_dumps(jws, 0);
  59. if (!adv)
  60. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  61. return http_reply(HTTP_STATUS_OK,
  62. "Content-Type: application/jose+json\r\n"
  63. "Content-Length: %zu\r\n"
  64. "\r\n%s", strlen(adv), adv);
  65. }
  66. static int
  67. rec(enum http_method method, const char *path, const char *body,
  68. regmatch_t matches[], void *misc)
  69. {
  70. __attribute__((cleanup(str_cleanup))) char *enc = NULL;
  71. __attribute__((cleanup(str_cleanup))) char *thp = NULL;
  72. __attribute__((cleanup(cleanup_tang_keys_info))) struct tang_keys_info *tki = NULL;
  73. size_t size = matches[1].rm_eo - matches[1].rm_so;
  74. const char *jwkdir = misc;
  75. json_auto_t *jwk = NULL;
  76. json_auto_t *req = NULL;
  77. json_auto_t *rep = NULL;
  78. const char *alg = NULL;
  79. const char *kty = NULL;
  80. const char *d = NULL;
  81. /*
  82. * Parse and validate the request JWK
  83. */
  84. req = json_loads(body, 0, NULL);
  85. if (!req)
  86. return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
  87. if (!jose_jwk_prm(NULL, req, false, "deriveKey"))
  88. return http_reply(HTTP_STATUS_FORBIDDEN, NULL);
  89. if (json_unpack(req, "{s:s,s?s}", "kty", &kty, "alg", &alg) < 0)
  90. return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
  91. if (strcmp(kty, "EC") != 0)
  92. return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
  93. if (alg && strcmp(alg, "ECMR") != 0)
  94. return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
  95. /*
  96. * Parse and validate the server-side JWK
  97. */
  98. tki = read_keys(jwkdir);
  99. if (!tki || tki->m_keys_count == 0) {
  100. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  101. }
  102. thp = strndup(&path[matches[1].rm_so], size);
  103. if (!thp)
  104. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  105. jwk = find_jwk(tki, thp);
  106. if (!jwk)
  107. return http_reply(HTTP_STATUS_NOT_FOUND, NULL);
  108. if (!jose_jwk_prm(NULL, jwk, true, "deriveKey"))
  109. return http_reply(HTTP_STATUS_FORBIDDEN, NULL);
  110. if (json_unpack(jwk, "{s:s,s?s}", "d", &d, "alg", &alg) < 0)
  111. return http_reply(HTTP_STATUS_FORBIDDEN, NULL);
  112. if (alg && strcmp(alg, "ECMR") != 0)
  113. return http_reply(HTTP_STATUS_FORBIDDEN, NULL);
  114. /*
  115. * Perform the exchange and return
  116. */
  117. rep = jose_jwk_exc(NULL, jwk, req);
  118. if (!rep)
  119. return http_reply(HTTP_STATUS_BAD_REQUEST, NULL);
  120. if (json_object_set_new(rep, "alg", json_string("ECMR")) < 0)
  121. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  122. if (json_object_set_new(rep, "key_ops", json_pack("[s]", "deriveKey")) < 0)
  123. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  124. enc = json_dumps(rep, JSON_SORT_KEYS | JSON_COMPACT);
  125. if (!enc)
  126. return http_reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, NULL);
  127. return http_reply(HTTP_STATUS_OK,
  128. "Content-Type: application/jwk+json\r\n"
  129. "Content-Length: %zu\r\n"
  130. "\r\n%s", strlen(enc), enc);
  131. }
  132. static struct http_dispatch dispatch[] = {
  133. { adv, 1 << HTTP_GET, 2, "^/+adv/+([0-9A-Za-z_-]+)$" },
  134. { adv, 1 << HTTP_GET, 2, "^/+adv/*$" },
  135. { rec, 1 << HTTP_POST, 2, "^/+rec/+([0-9A-Za-z_-]+)$" },
  136. {}
  137. };
  138. int
  139. main(int argc, char *argv[])
  140. {
  141. struct http_state state = { .dispatch = dispatch, .misc = argv[1] };
  142. struct http_parser parser = { .data = &state };
  143. struct stat st = {};
  144. char req[4096] = {};
  145. size_t rcvd = 0;
  146. int r = 0;
  147. http_parser_init(&parser, HTTP_REQUEST);
  148. if (argc != 2) {
  149. fprintf(stderr, "Usage: %s <jwkdir>\n", argv[0]);
  150. return EXIT_FAILURE;
  151. }
  152. if (stat(argv[1], &st) != 0) {
  153. fprintf(stderr, "Error calling stat() on path: %s: %m\n", argv[1]);
  154. return EXIT_FAILURE;
  155. }
  156. if (!S_ISDIR(st.st_mode)) {
  157. fprintf(stderr, "Path is not a directory: %s\n", argv[1]);
  158. return EXIT_FAILURE;
  159. }
  160. for (;;) {
  161. r = read(STDIN_FILENO, &req[rcvd], sizeof(req) - rcvd - 1);
  162. if (r == 0)
  163. return rcvd > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
  164. if (r < 0)
  165. return EXIT_FAILURE;
  166. rcvd += r;
  167. r = http_parser_execute(&parser, &http_settings, req, rcvd);
  168. if (parser.http_errno != 0) {
  169. fprintf(stderr, "HTTP Parsing Error: %s\n",
  170. http_errno_description(parser.http_errno));
  171. return EXIT_SUCCESS;
  172. }
  173. memmove(req, &req[r], rcvd - r);
  174. rcvd -= r;
  175. }
  176. return EXIT_SUCCESS;
  177. }