bio_asn1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Experimental ASN1 BIO. When written through the data is converted to an
  11. * ASN1 string type: default is OCTET STRING. Additional functions can be
  12. * provided to add prefix and suffix data.
  13. */
  14. #include <string.h>
  15. #include "internal/bio.h"
  16. #include <openssl/asn1.h>
  17. #include "internal/cryptlib.h"
  18. /* Must be large enough for biggest tag+length */
  19. #define DEFAULT_ASN1_BUF_SIZE 20
  20. typedef enum {
  21. ASN1_STATE_START,
  22. ASN1_STATE_PRE_COPY,
  23. ASN1_STATE_HEADER,
  24. ASN1_STATE_HEADER_COPY,
  25. ASN1_STATE_DATA_COPY,
  26. ASN1_STATE_POST_COPY,
  27. ASN1_STATE_DONE
  28. } asn1_bio_state_t;
  29. typedef struct BIO_ASN1_EX_FUNCS_st {
  30. asn1_ps_func *ex_func;
  31. asn1_ps_func *ex_free_func;
  32. } BIO_ASN1_EX_FUNCS;
  33. typedef struct BIO_ASN1_BUF_CTX_t {
  34. /* Internal state */
  35. asn1_bio_state_t state;
  36. /* Internal buffer */
  37. unsigned char *buf;
  38. /* Size of buffer */
  39. int bufsize;
  40. /* Current position in buffer */
  41. int bufpos;
  42. /* Current buffer length */
  43. int buflen;
  44. /* Amount of data to copy */
  45. int copylen;
  46. /* Class and tag to use */
  47. int asn1_class, asn1_tag;
  48. asn1_ps_func *prefix, *prefix_free, *suffix, *suffix_free;
  49. /* Extra buffer for prefix and suffix data */
  50. unsigned char *ex_buf;
  51. int ex_len;
  52. int ex_pos;
  53. void *ex_arg;
  54. } BIO_ASN1_BUF_CTX;
  55. static int asn1_bio_write(BIO *h, const char *buf, int num);
  56. static int asn1_bio_read(BIO *h, char *buf, int size);
  57. static int asn1_bio_puts(BIO *h, const char *str);
  58. static int asn1_bio_gets(BIO *h, char *str, int size);
  59. static long asn1_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  60. static int asn1_bio_new(BIO *h);
  61. static int asn1_bio_free(BIO *data);
  62. static long asn1_bio_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
  63. static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size);
  64. static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  65. asn1_ps_func *cleanup, asn1_bio_state_t next);
  66. static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  67. asn1_ps_func *setup,
  68. asn1_bio_state_t ex_state,
  69. asn1_bio_state_t other_state);
  70. static const BIO_METHOD methods_asn1 = {
  71. BIO_TYPE_ASN1,
  72. "asn1",
  73. /* TODO: Convert to new style write function */
  74. bwrite_conv,
  75. asn1_bio_write,
  76. /* TODO: Convert to new style read function */
  77. bread_conv,
  78. asn1_bio_read,
  79. asn1_bio_puts,
  80. asn1_bio_gets,
  81. asn1_bio_ctrl,
  82. asn1_bio_new,
  83. asn1_bio_free,
  84. asn1_bio_callback_ctrl,
  85. };
  86. const BIO_METHOD *BIO_f_asn1(void)
  87. {
  88. return &methods_asn1;
  89. }
  90. static int asn1_bio_new(BIO *b)
  91. {
  92. BIO_ASN1_BUF_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  93. if (ctx == NULL)
  94. return 0;
  95. if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
  96. OPENSSL_free(ctx);
  97. return 0;
  98. }
  99. BIO_set_data(b, ctx);
  100. BIO_set_init(b, 1);
  101. return 1;
  102. }
  103. static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
  104. {
  105. if ((ctx->buf = OPENSSL_malloc(size)) == NULL) {
  106. ASN1err(ASN1_F_ASN1_BIO_INIT, ERR_R_MALLOC_FAILURE);
  107. return 0;
  108. }
  109. ctx->bufsize = size;
  110. ctx->asn1_class = V_ASN1_UNIVERSAL;
  111. ctx->asn1_tag = V_ASN1_OCTET_STRING;
  112. ctx->state = ASN1_STATE_START;
  113. return 1;
  114. }
  115. static int asn1_bio_free(BIO *b)
  116. {
  117. BIO_ASN1_BUF_CTX *ctx;
  118. if (b == NULL)
  119. return 0;
  120. ctx = BIO_get_data(b);
  121. if (ctx == NULL)
  122. return 0;
  123. OPENSSL_free(ctx->buf);
  124. OPENSSL_free(ctx);
  125. BIO_set_data(b, NULL);
  126. BIO_set_init(b, 0);
  127. return 1;
  128. }
  129. static int asn1_bio_write(BIO *b, const char *in, int inl)
  130. {
  131. BIO_ASN1_BUF_CTX *ctx;
  132. int wrmax, wrlen, ret;
  133. unsigned char *p;
  134. BIO *next;
  135. ctx = BIO_get_data(b);
  136. next = BIO_next(b);
  137. if (in == NULL || inl < 0 || ctx == NULL || next == NULL)
  138. return 0;
  139. wrlen = 0;
  140. ret = -1;
  141. for (;;) {
  142. switch (ctx->state) {
  143. /* Setup prefix data, call it */
  144. case ASN1_STATE_START:
  145. if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
  146. ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
  147. return 0;
  148. break;
  149. /* Copy any pre data first */
  150. case ASN1_STATE_PRE_COPY:
  151. ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
  152. ASN1_STATE_HEADER);
  153. if (ret <= 0)
  154. goto done;
  155. break;
  156. case ASN1_STATE_HEADER:
  157. ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
  158. if (!ossl_assert(ctx->buflen <= ctx->bufsize))
  159. return 0;
  160. p = ctx->buf;
  161. ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
  162. ctx->copylen = inl;
  163. ctx->state = ASN1_STATE_HEADER_COPY;
  164. break;
  165. case ASN1_STATE_HEADER_COPY:
  166. ret = BIO_write(next, ctx->buf + ctx->bufpos, ctx->buflen);
  167. if (ret <= 0)
  168. goto done;
  169. ctx->buflen -= ret;
  170. if (ctx->buflen)
  171. ctx->bufpos += ret;
  172. else {
  173. ctx->bufpos = 0;
  174. ctx->state = ASN1_STATE_DATA_COPY;
  175. }
  176. break;
  177. case ASN1_STATE_DATA_COPY:
  178. if (inl > ctx->copylen)
  179. wrmax = ctx->copylen;
  180. else
  181. wrmax = inl;
  182. ret = BIO_write(next, in, wrmax);
  183. if (ret <= 0)
  184. goto done;
  185. wrlen += ret;
  186. ctx->copylen -= ret;
  187. in += ret;
  188. inl -= ret;
  189. if (ctx->copylen == 0)
  190. ctx->state = ASN1_STATE_HEADER;
  191. if (inl == 0)
  192. goto done;
  193. break;
  194. case ASN1_STATE_POST_COPY:
  195. case ASN1_STATE_DONE:
  196. BIO_clear_retry_flags(b);
  197. return 0;
  198. }
  199. }
  200. done:
  201. BIO_clear_retry_flags(b);
  202. BIO_copy_next_retry(b);
  203. return (wrlen > 0) ? wrlen : ret;
  204. }
  205. static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  206. asn1_ps_func *cleanup, asn1_bio_state_t next)
  207. {
  208. int ret;
  209. if (ctx->ex_len <= 0)
  210. return 1;
  211. for (;;) {
  212. ret = BIO_write(BIO_next(b), ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
  213. if (ret <= 0)
  214. break;
  215. ctx->ex_len -= ret;
  216. if (ctx->ex_len > 0)
  217. ctx->ex_pos += ret;
  218. else {
  219. if (cleanup)
  220. cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
  221. ctx->state = next;
  222. ctx->ex_pos = 0;
  223. break;
  224. }
  225. }
  226. return ret;
  227. }
  228. static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  229. asn1_ps_func *setup,
  230. asn1_bio_state_t ex_state,
  231. asn1_bio_state_t other_state)
  232. {
  233. if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
  234. BIO_clear_retry_flags(b);
  235. return 0;
  236. }
  237. if (ctx->ex_len > 0)
  238. ctx->state = ex_state;
  239. else
  240. ctx->state = other_state;
  241. return 1;
  242. }
  243. static int asn1_bio_read(BIO *b, char *in, int inl)
  244. {
  245. BIO *next = BIO_next(b);
  246. if (next == NULL)
  247. return 0;
  248. return BIO_read(next, in, inl);
  249. }
  250. static int asn1_bio_puts(BIO *b, const char *str)
  251. {
  252. return asn1_bio_write(b, str, strlen(str));
  253. }
  254. static int asn1_bio_gets(BIO *b, char *str, int size)
  255. {
  256. BIO *next = BIO_next(b);
  257. if (next == NULL)
  258. return 0;
  259. return BIO_gets(next, str, size);
  260. }
  261. static long asn1_bio_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  262. {
  263. BIO *next = BIO_next(b);
  264. if (next == NULL)
  265. return 0;
  266. return BIO_callback_ctrl(next, cmd, fp);
  267. }
  268. static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
  269. {
  270. BIO_ASN1_BUF_CTX *ctx;
  271. BIO_ASN1_EX_FUNCS *ex_func;
  272. long ret = 1;
  273. BIO *next;
  274. ctx = BIO_get_data(b);
  275. if (ctx == NULL)
  276. return 0;
  277. next = BIO_next(b);
  278. switch (cmd) {
  279. case BIO_C_SET_PREFIX:
  280. ex_func = arg2;
  281. ctx->prefix = ex_func->ex_func;
  282. ctx->prefix_free = ex_func->ex_free_func;
  283. break;
  284. case BIO_C_GET_PREFIX:
  285. ex_func = arg2;
  286. ex_func->ex_func = ctx->prefix;
  287. ex_func->ex_free_func = ctx->prefix_free;
  288. break;
  289. case BIO_C_SET_SUFFIX:
  290. ex_func = arg2;
  291. ctx->suffix = ex_func->ex_func;
  292. ctx->suffix_free = ex_func->ex_free_func;
  293. break;
  294. case BIO_C_GET_SUFFIX:
  295. ex_func = arg2;
  296. ex_func->ex_func = ctx->suffix;
  297. ex_func->ex_free_func = ctx->suffix_free;
  298. break;
  299. case BIO_C_SET_EX_ARG:
  300. ctx->ex_arg = arg2;
  301. break;
  302. case BIO_C_GET_EX_ARG:
  303. *(void **)arg2 = ctx->ex_arg;
  304. break;
  305. case BIO_CTRL_FLUSH:
  306. if (next == NULL)
  307. return 0;
  308. /* Call post function if possible */
  309. if (ctx->state == ASN1_STATE_HEADER) {
  310. if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
  311. ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
  312. return 0;
  313. }
  314. if (ctx->state == ASN1_STATE_POST_COPY) {
  315. ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
  316. ASN1_STATE_DONE);
  317. if (ret <= 0)
  318. return ret;
  319. }
  320. if (ctx->state == ASN1_STATE_DONE)
  321. return BIO_ctrl(next, cmd, arg1, arg2);
  322. else {
  323. BIO_clear_retry_flags(b);
  324. return 0;
  325. }
  326. default:
  327. if (next == NULL)
  328. return 0;
  329. return BIO_ctrl(next, cmd, arg1, arg2);
  330. }
  331. return ret;
  332. }
  333. static int asn1_bio_set_ex(BIO *b, int cmd,
  334. asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
  335. {
  336. BIO_ASN1_EX_FUNCS extmp;
  337. extmp.ex_func = ex_func;
  338. extmp.ex_free_func = ex_free_func;
  339. return BIO_ctrl(b, cmd, 0, &extmp);
  340. }
  341. static int asn1_bio_get_ex(BIO *b, int cmd,
  342. asn1_ps_func **ex_func,
  343. asn1_ps_func **ex_free_func)
  344. {
  345. BIO_ASN1_EX_FUNCS extmp;
  346. int ret;
  347. ret = BIO_ctrl(b, cmd, 0, &extmp);
  348. if (ret > 0) {
  349. *ex_func = extmp.ex_func;
  350. *ex_free_func = extmp.ex_free_func;
  351. }
  352. return ret;
  353. }
  354. int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
  355. asn1_ps_func *prefix_free)
  356. {
  357. return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
  358. }
  359. int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
  360. asn1_ps_func **pprefix_free)
  361. {
  362. return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
  363. }
  364. int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
  365. asn1_ps_func *suffix_free)
  366. {
  367. return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
  368. }
  369. int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
  370. asn1_ps_func **psuffix_free)
  371. {
  372. return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
  373. }