bio_asn1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. bwrite_conv,
  74. asn1_bio_write,
  75. bread_conv,
  76. asn1_bio_read,
  77. asn1_bio_puts,
  78. asn1_bio_gets,
  79. asn1_bio_ctrl,
  80. asn1_bio_new,
  81. asn1_bio_free,
  82. asn1_bio_callback_ctrl,
  83. };
  84. const BIO_METHOD *BIO_f_asn1(void)
  85. {
  86. return &methods_asn1;
  87. }
  88. static int asn1_bio_new(BIO *b)
  89. {
  90. BIO_ASN1_BUF_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  91. if (ctx == NULL)
  92. return 0;
  93. if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
  94. OPENSSL_free(ctx);
  95. return 0;
  96. }
  97. BIO_set_data(b, ctx);
  98. BIO_set_init(b, 1);
  99. return 1;
  100. }
  101. static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
  102. {
  103. if (size <= 0) {
  104. ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_INVALID_ARGUMENT);
  105. return 0;
  106. }
  107. if ((ctx->buf = OPENSSL_malloc(size)) == NULL)
  108. return 0;
  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. if (ctx->prefix_free != NULL)
  124. ctx->prefix_free(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
  125. if (ctx->suffix_free != NULL)
  126. ctx->suffix_free(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
  127. OPENSSL_free(ctx->buf);
  128. OPENSSL_free(ctx);
  129. BIO_set_data(b, NULL);
  130. BIO_set_init(b, 0);
  131. return 1;
  132. }
  133. static int asn1_bio_write(BIO *b, const char *in, int inl)
  134. {
  135. BIO_ASN1_BUF_CTX *ctx;
  136. int wrmax, wrlen, ret;
  137. unsigned char *p;
  138. BIO *next;
  139. ctx = BIO_get_data(b);
  140. next = BIO_next(b);
  141. if (in == NULL || inl < 0 || ctx == NULL || next == NULL)
  142. return 0;
  143. wrlen = 0;
  144. ret = -1;
  145. for (;;) {
  146. switch (ctx->state) {
  147. /* Setup prefix data, call it */
  148. case ASN1_STATE_START:
  149. if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
  150. ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
  151. return -1;
  152. break;
  153. /* Copy any pre data first */
  154. case ASN1_STATE_PRE_COPY:
  155. ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
  156. ASN1_STATE_HEADER);
  157. if (ret <= 0)
  158. goto done;
  159. break;
  160. case ASN1_STATE_HEADER:
  161. ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
  162. if (!ossl_assert(ctx->buflen <= ctx->bufsize))
  163. return -1;
  164. p = ctx->buf;
  165. ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
  166. ctx->copylen = inl;
  167. ctx->state = ASN1_STATE_HEADER_COPY;
  168. break;
  169. case ASN1_STATE_HEADER_COPY:
  170. ret = BIO_write(next, ctx->buf + ctx->bufpos, ctx->buflen);
  171. if (ret <= 0)
  172. goto done;
  173. ctx->buflen -= ret;
  174. if (ctx->buflen)
  175. ctx->bufpos += ret;
  176. else {
  177. ctx->bufpos = 0;
  178. ctx->state = ASN1_STATE_DATA_COPY;
  179. }
  180. break;
  181. case ASN1_STATE_DATA_COPY:
  182. if (inl > ctx->copylen)
  183. wrmax = ctx->copylen;
  184. else
  185. wrmax = inl;
  186. ret = BIO_write(next, in, wrmax);
  187. if (ret <= 0)
  188. goto done;
  189. wrlen += ret;
  190. ctx->copylen -= ret;
  191. in += ret;
  192. inl -= ret;
  193. if (ctx->copylen == 0)
  194. ctx->state = ASN1_STATE_HEADER;
  195. if (inl == 0)
  196. goto done;
  197. break;
  198. case ASN1_STATE_POST_COPY:
  199. case ASN1_STATE_DONE:
  200. BIO_clear_retry_flags(b);
  201. return 0;
  202. }
  203. }
  204. done:
  205. BIO_clear_retry_flags(b);
  206. BIO_copy_next_retry(b);
  207. return (wrlen > 0) ? wrlen : ret;
  208. }
  209. static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  210. asn1_ps_func *cleanup, asn1_bio_state_t next)
  211. {
  212. int ret;
  213. if (ctx->ex_len <= 0)
  214. return 1;
  215. for (;;) {
  216. ret = BIO_write(BIO_next(b), ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
  217. if (ret <= 0)
  218. break;
  219. ctx->ex_len -= ret;
  220. if (ctx->ex_len > 0)
  221. ctx->ex_pos += ret;
  222. else {
  223. if (cleanup)
  224. cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
  225. ctx->state = next;
  226. ctx->ex_pos = 0;
  227. break;
  228. }
  229. }
  230. return ret;
  231. }
  232. static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  233. asn1_ps_func *setup,
  234. asn1_bio_state_t ex_state,
  235. asn1_bio_state_t other_state)
  236. {
  237. if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
  238. BIO_clear_retry_flags(b);
  239. return 0;
  240. }
  241. if (ctx->ex_len > 0)
  242. ctx->state = ex_state;
  243. else
  244. ctx->state = other_state;
  245. return 1;
  246. }
  247. static int asn1_bio_read(BIO *b, char *in, int inl)
  248. {
  249. BIO *next = BIO_next(b);
  250. if (next == NULL)
  251. return 0;
  252. return BIO_read(next, in, inl);
  253. }
  254. static int asn1_bio_puts(BIO *b, const char *str)
  255. {
  256. return asn1_bio_write(b, str, strlen(str));
  257. }
  258. static int asn1_bio_gets(BIO *b, char *str, int size)
  259. {
  260. BIO *next = BIO_next(b);
  261. if (next == NULL)
  262. return 0;
  263. return BIO_gets(next, str, size);
  264. }
  265. static long asn1_bio_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  266. {
  267. BIO *next = BIO_next(b);
  268. if (next == NULL)
  269. return 0;
  270. return BIO_callback_ctrl(next, cmd, fp);
  271. }
  272. static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
  273. {
  274. BIO_ASN1_BUF_CTX *ctx;
  275. BIO_ASN1_EX_FUNCS *ex_func;
  276. long ret = 1;
  277. BIO *next;
  278. ctx = BIO_get_data(b);
  279. if (ctx == NULL)
  280. return 0;
  281. next = BIO_next(b);
  282. switch (cmd) {
  283. case BIO_C_SET_PREFIX:
  284. ex_func = arg2;
  285. ctx->prefix = ex_func->ex_func;
  286. ctx->prefix_free = ex_func->ex_free_func;
  287. break;
  288. case BIO_C_GET_PREFIX:
  289. ex_func = arg2;
  290. ex_func->ex_func = ctx->prefix;
  291. ex_func->ex_free_func = ctx->prefix_free;
  292. break;
  293. case BIO_C_SET_SUFFIX:
  294. ex_func = arg2;
  295. ctx->suffix = ex_func->ex_func;
  296. ctx->suffix_free = ex_func->ex_free_func;
  297. break;
  298. case BIO_C_GET_SUFFIX:
  299. ex_func = arg2;
  300. ex_func->ex_func = ctx->suffix;
  301. ex_func->ex_free_func = ctx->suffix_free;
  302. break;
  303. case BIO_C_SET_EX_ARG:
  304. ctx->ex_arg = arg2;
  305. break;
  306. case BIO_C_GET_EX_ARG:
  307. *(void **)arg2 = ctx->ex_arg;
  308. break;
  309. case BIO_CTRL_FLUSH:
  310. if (next == NULL)
  311. return 0;
  312. /* Call post function if possible */
  313. if (ctx->state == ASN1_STATE_HEADER) {
  314. if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
  315. ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
  316. return 0;
  317. }
  318. if (ctx->state == ASN1_STATE_POST_COPY) {
  319. ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
  320. ASN1_STATE_DONE);
  321. if (ret <= 0)
  322. return ret;
  323. }
  324. if (ctx->state == ASN1_STATE_DONE)
  325. return BIO_ctrl(next, cmd, arg1, arg2);
  326. else {
  327. BIO_clear_retry_flags(b);
  328. return 0;
  329. }
  330. default:
  331. if (next == NULL)
  332. return 0;
  333. return BIO_ctrl(next, cmd, arg1, arg2);
  334. }
  335. return ret;
  336. }
  337. static int asn1_bio_set_ex(BIO *b, int cmd,
  338. asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
  339. {
  340. BIO_ASN1_EX_FUNCS extmp;
  341. extmp.ex_func = ex_func;
  342. extmp.ex_free_func = ex_free_func;
  343. return BIO_ctrl(b, cmd, 0, &extmp);
  344. }
  345. static int asn1_bio_get_ex(BIO *b, int cmd,
  346. asn1_ps_func **ex_func,
  347. asn1_ps_func **ex_free_func)
  348. {
  349. BIO_ASN1_EX_FUNCS extmp;
  350. int ret;
  351. ret = BIO_ctrl(b, cmd, 0, &extmp);
  352. if (ret > 0) {
  353. *ex_func = extmp.ex_func;
  354. *ex_free_func = extmp.ex_free_func;
  355. }
  356. return ret;
  357. }
  358. int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
  359. asn1_ps_func *prefix_free)
  360. {
  361. return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
  362. }
  363. int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
  364. asn1_ps_func **pprefix_free)
  365. {
  366. return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
  367. }
  368. int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
  369. asn1_ps_func *suffix_free)
  370. {
  371. return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
  372. }
  373. int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
  374. asn1_ps_func **psuffix_free)
  375. {
  376. return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
  377. }