bio_asn1.c 11 KB

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