A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

443 lines
11 KiB

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