b64.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2016 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <jose/b64.h>
  18. #include "misc.h"
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #define JOSE_B64_DEC_BLK 3
  22. #define JOSE_B64_ENC_BLK 4
  23. typedef struct {
  24. jose_io_t io;
  25. jose_io_t *next;
  26. size_t len;
  27. union {
  28. uint8_t db[16 * JOSE_B64_DEC_BLK];
  29. char eb[16 * JOSE_B64_ENC_BLK];
  30. };
  31. } io_t;
  32. static const char *map = JOSE_B64_MAP;
  33. static size_t
  34. b64_dlen(size_t elen)
  35. {
  36. switch (elen % JOSE_B64_ENC_BLK) {
  37. case 0: return elen / JOSE_B64_ENC_BLK * JOSE_B64_DEC_BLK;
  38. case 2: return elen / JOSE_B64_ENC_BLK * JOSE_B64_DEC_BLK + 1;
  39. case 3: return elen / JOSE_B64_ENC_BLK * JOSE_B64_DEC_BLK + 2;
  40. default: return SIZE_MAX;
  41. }
  42. }
  43. static size_t
  44. b64_elen(size_t dlen)
  45. {
  46. switch (dlen % JOSE_B64_DEC_BLK) {
  47. case 0: return dlen / JOSE_B64_DEC_BLK * JOSE_B64_ENC_BLK;
  48. case 1: return dlen / JOSE_B64_DEC_BLK * JOSE_B64_ENC_BLK + 2;
  49. case 2: return dlen / JOSE_B64_DEC_BLK * JOSE_B64_ENC_BLK + 3;
  50. default: return SIZE_MAX;
  51. }
  52. }
  53. static void
  54. io_free(jose_io_t *io)
  55. {
  56. io_t *i = containerof(io, io_t, io);
  57. jose_io_decref(i->next);
  58. zero(i, sizeof(*i));
  59. free(i);
  60. }
  61. static size_t
  62. min(size_t a, size_t b)
  63. {
  64. return a > b ? b : a;
  65. }
  66. static bool
  67. dec_feed(jose_io_t *io, const void *in, size_t len)
  68. {
  69. io_t *i = containerof(io, io_t, io);
  70. const char *enc = in;
  71. while (len > 0) {
  72. uint8_t buf[sizeof(i->eb) / JOSE_B64_ENC_BLK * JOSE_B64_DEC_BLK];
  73. size_t dl = 0;
  74. size_t el = 0;
  75. /* Copy input into our input buffer. */
  76. el = min(sizeof(i->eb) - i->len, len);
  77. memcpy(&i->eb[i->len], enc, el);
  78. i->len += el;
  79. enc += el;
  80. len -= el;
  81. /* Perform encoding into our output buffer. */
  82. el = i->len - i->len % JOSE_B64_ENC_BLK;
  83. dl = jose_b64_dec_buf(i->eb, el, buf, sizeof(buf));
  84. if (dl == SIZE_MAX)
  85. return false;
  86. i->len -= el;
  87. memmove(i->eb, &i->eb[el], i->len);
  88. if (!i->next->feed(i->next, buf, dl))
  89. return false;
  90. }
  91. return true;
  92. }
  93. static bool
  94. dec_done(jose_io_t *io)
  95. {
  96. io_t *i = containerof(io, io_t, io);
  97. uint8_t buf[sizeof(i->eb) / JOSE_B64_ENC_BLK * JOSE_B64_DEC_BLK];
  98. size_t dl = 0;
  99. dl = jose_b64_dec_buf(i->eb, i->len, buf, sizeof(buf));
  100. if (dl == SIZE_MAX)
  101. return false;
  102. i->len = 0;
  103. if (!i->next->feed(i->next, buf, dl))
  104. return false;
  105. return i->next->done(i->next);
  106. }
  107. static bool
  108. enc_feed(jose_io_t *io, const void *in, size_t len)
  109. {
  110. io_t *i = containerof(io, io_t, io);
  111. const char *dec = in;
  112. while (len > 0) {
  113. uint8_t buf[sizeof(i->db) / JOSE_B64_DEC_BLK * JOSE_B64_ENC_BLK];
  114. size_t dl = 0;
  115. size_t el = 0;
  116. /* Copy input into our input buffer. */
  117. dl = min(sizeof(i->db) - i->len, len);
  118. memcpy(&i->db[i->len], dec, dl);
  119. i->len += dl;
  120. dec += dl;
  121. len -= dl;
  122. /* Perform encoding into our output buffer. */
  123. dl = i->len - i->len % JOSE_B64_DEC_BLK;
  124. el = jose_b64_enc_buf(i->db, dl, buf, sizeof(buf));
  125. if (el == SIZE_MAX)
  126. return false;
  127. i->len -= dl;
  128. memmove(i->db, &i->db[dl], i->len);
  129. if (!i->next->feed(i->next, buf, el))
  130. return false;
  131. }
  132. return true;
  133. }
  134. static bool
  135. enc_done(jose_io_t *io)
  136. {
  137. io_t *i = containerof(io, io_t, io);
  138. uint8_t buf[sizeof(i->db) / JOSE_B64_DEC_BLK * JOSE_B64_ENC_BLK];
  139. size_t el = 0;
  140. el = jose_b64_enc_buf(i->db, i->len, buf, sizeof(buf));
  141. if (el == SIZE_MAX)
  142. return false;
  143. i->len = 0;
  144. if (!i->next->feed(i->next, buf, el))
  145. return false;
  146. return i->next->done(i->next);
  147. }
  148. size_t
  149. jose_b64_dec(const json_t *i, void *o, size_t ol)
  150. {
  151. const char *b64 = NULL;
  152. size_t len = 0;
  153. if (json_unpack((json_t *) i, "s%", &b64, &len) < 0)
  154. return SIZE_MAX;
  155. if (!o)
  156. return b64_dlen(len);
  157. return jose_b64_dec_buf(b64, len, o, ol);
  158. }
  159. jose_io_t *
  160. jose_b64_dec_io(jose_io_t *next)
  161. {
  162. jose_io_auto_t *io = NULL;
  163. io_t *i = NULL;
  164. i = calloc(1, sizeof(*i));
  165. if (!i)
  166. return NULL;
  167. io = jose_io_incref(&i->io);
  168. io->feed = dec_feed;
  169. io->done = dec_done;
  170. io->free = io_free;
  171. i->next = jose_io_incref(next);
  172. return jose_io_incref(io);
  173. }
  174. size_t
  175. jose_b64_dec_buf(const void *i, size_t il, void *o, size_t ol)
  176. {
  177. const size_t len = strlen(map);
  178. const char *e = i;
  179. uint8_t *d = o;
  180. uint8_t rem = 0;
  181. size_t oo = 0;
  182. if (il == SIZE_MAX)
  183. return SIZE_MAX;
  184. if (!o)
  185. return b64_dlen(il);
  186. if (ol < b64_dlen(il))
  187. return SIZE_MAX;
  188. for (size_t io = 0; io < il; io++) {
  189. uint8_t v = 0;
  190. for (const char c = e[io]; v < len && c != map[v]; v++)
  191. continue;
  192. if (v >= len)
  193. return SIZE_MAX;
  194. switch (io % JOSE_B64_ENC_BLK) {
  195. case 0:
  196. if (!e[io+1] || rem > 0)
  197. return SIZE_MAX;
  198. rem = v << 2;
  199. break;
  200. case 1:
  201. d[oo++] = rem | (v >> 4);
  202. rem = v << 4;
  203. break;
  204. case 2:
  205. d[oo++] = rem | (v >> 2);
  206. rem = v << 6;
  207. break;
  208. case 3:
  209. d[oo++] = rem | v;
  210. rem = 0;
  211. break;
  212. }
  213. }
  214. return rem > 0 ? SIZE_MAX : oo;
  215. }
  216. json_t *
  217. jose_b64_dec_load(const json_t *i)
  218. {
  219. uint8_t *buf = NULL;
  220. json_t *out = NULL;
  221. size_t size = 0;
  222. size = jose_b64_dec(i, NULL, 0);
  223. if (size == SIZE_MAX)
  224. return NULL;
  225. buf = calloc(1, size);
  226. if (!buf)
  227. return NULL;
  228. if (jose_b64_dec(i, buf, size) != size) {
  229. zero(buf, size);
  230. free(buf);
  231. return NULL;
  232. }
  233. out = json_loadb((char *) buf, size, JSON_DECODE_ANY, NULL);
  234. zero(buf, size);
  235. free(buf);
  236. return out;
  237. }
  238. json_t *
  239. jose_b64_enc(const void *i, size_t il)
  240. {
  241. json_t *out = NULL;
  242. char *enc = NULL;
  243. size_t elen = 0;
  244. elen = b64_elen(il);
  245. if (elen == SIZE_MAX)
  246. return NULL;
  247. enc = calloc(1, elen);
  248. if (!enc)
  249. return NULL;
  250. if (jose_b64_enc_buf(i, il, enc, elen) == elen)
  251. out = json_stringn(enc, elen);
  252. zero(enc, elen);
  253. free(enc);
  254. return out;
  255. }
  256. jose_io_t *
  257. jose_b64_enc_io(jose_io_t *next)
  258. {
  259. jose_io_auto_t *io = NULL;
  260. io_t *i = NULL;
  261. i = calloc(1, sizeof(*i));
  262. if (!i)
  263. return NULL;
  264. io = jose_io_incref(&i->io);
  265. io->feed = enc_feed;
  266. io->done = enc_done;
  267. io->free = io_free;
  268. i->next = jose_io_incref(next);
  269. return jose_io_incref(io);
  270. }
  271. size_t
  272. jose_b64_enc_buf(const void *i, size_t il, void *o, size_t ol)
  273. {
  274. const uint8_t *ib = i;
  275. uint8_t rem = 0;
  276. size_t oo = 0;
  277. char *ob = o;
  278. if (!o)
  279. return b64_elen(il);
  280. if (ol < b64_elen(il))
  281. return SIZE_MAX;
  282. for (size_t io = 0; io < il; io++) {
  283. uint8_t c = ib[io];
  284. switch (io % 3) {
  285. case 0:
  286. ob[oo++] = map[c >> 2];
  287. ob[oo++] = map[rem = (c & 0b11) << 4];
  288. break;
  289. case 1:
  290. ob[oo-1] = map[rem | (c >> 4)];
  291. ob[oo++] = map[rem = (c & 0b1111) << 2];
  292. break;
  293. case 2:
  294. ob[oo-1] = map[rem | (c >> 6)];
  295. ob[oo++] = map[c & 0b111111];
  296. break;
  297. }
  298. }
  299. return oo;
  300. }
  301. json_t *
  302. jose_b64_enc_dump(const json_t *i)
  303. {
  304. json_t *out = NULL;
  305. char *buf = NULL;
  306. buf = json_dumps(i, JSON_COMPACT | JSON_SORT_KEYS);
  307. if (!buf)
  308. return NULL;
  309. out = jose_b64_enc((const uint8_t *) buf, strlen(buf));
  310. zero(buf, strlen(buf));
  311. free(buf);
  312. return out;
  313. }