bio_asn1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/asn1.h>
  57. #include <assert.h>
  58. #include <string.h>
  59. #include <openssl/bio.h>
  60. #include <openssl/mem.h>
  61. /* Must be large enough for biggest tag+length */
  62. #define DEFAULT_ASN1_BUF_SIZE 20
  63. typedef enum {
  64. ASN1_STATE_START,
  65. ASN1_STATE_PRE_COPY,
  66. ASN1_STATE_HEADER,
  67. ASN1_STATE_HEADER_COPY,
  68. ASN1_STATE_DATA_COPY,
  69. ASN1_STATE_POST_COPY,
  70. ASN1_STATE_DONE
  71. } asn1_bio_state_t;
  72. typedef struct BIO_ASN1_EX_FUNCS_st {
  73. asn1_ps_func *ex_func;
  74. asn1_ps_func *ex_free_func;
  75. } BIO_ASN1_EX_FUNCS;
  76. typedef struct BIO_ASN1_BUF_CTX_t {
  77. /* Internal state */
  78. asn1_bio_state_t state;
  79. /* Internal buffer */
  80. unsigned char *buf;
  81. /* Size of buffer */
  82. int bufsize;
  83. /* Current position in buffer */
  84. int bufpos;
  85. /* Current buffer length */
  86. int buflen;
  87. /* Amount of data to copy */
  88. int copylen;
  89. /* Class and tag to use */
  90. int asn1_class, asn1_tag;
  91. asn1_ps_func *prefix, *prefix_free, *suffix, *suffix_free;
  92. /* Extra buffer for prefix and suffix data */
  93. unsigned char *ex_buf;
  94. int ex_len;
  95. int ex_pos;
  96. void *ex_arg;
  97. } BIO_ASN1_BUF_CTX;
  98. static int asn1_bio_write(BIO *h, const char *buf, int num);
  99. static int asn1_bio_read(BIO *h, char *buf, int size);
  100. static int asn1_bio_puts(BIO *h, const char *str);
  101. static int asn1_bio_gets(BIO *h, char *str, int size);
  102. static long asn1_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  103. static int asn1_bio_new(BIO *h);
  104. static int asn1_bio_free(BIO *data);
  105. static long asn1_bio_callback_ctrl(BIO *h, int cmd, bio_info_cb fp);
  106. static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size);
  107. static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  108. asn1_ps_func *cleanup, asn1_bio_state_t next);
  109. static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  110. asn1_ps_func *setup,
  111. asn1_bio_state_t ex_state,
  112. asn1_bio_state_t other_state);
  113. static const BIO_METHOD methods_asn1 = {
  114. BIO_TYPE_ASN1,
  115. "asn1",
  116. asn1_bio_write,
  117. asn1_bio_read,
  118. asn1_bio_puts,
  119. asn1_bio_gets,
  120. asn1_bio_ctrl,
  121. asn1_bio_new,
  122. asn1_bio_free,
  123. asn1_bio_callback_ctrl,
  124. };
  125. const BIO_METHOD *BIO_f_asn1(void)
  126. {
  127. return (&methods_asn1);
  128. }
  129. static int asn1_bio_new(BIO *b)
  130. {
  131. BIO_ASN1_BUF_CTX *ctx;
  132. ctx = OPENSSL_malloc(sizeof(BIO_ASN1_BUF_CTX));
  133. if (!ctx)
  134. return 0;
  135. if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
  136. OPENSSL_free(ctx);
  137. return 0;
  138. }
  139. b->init = 1;
  140. b->ptr = (char *)ctx;
  141. b->flags = 0;
  142. return 1;
  143. }
  144. static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
  145. {
  146. ctx->buf = OPENSSL_malloc(size);
  147. if (!ctx->buf)
  148. return 0;
  149. ctx->bufsize = size;
  150. ctx->bufpos = 0;
  151. ctx->buflen = 0;
  152. ctx->copylen = 0;
  153. ctx->asn1_class = V_ASN1_UNIVERSAL;
  154. ctx->asn1_tag = V_ASN1_OCTET_STRING;
  155. ctx->ex_buf = 0;
  156. ctx->ex_pos = 0;
  157. ctx->ex_len = 0;
  158. ctx->state = ASN1_STATE_START;
  159. return 1;
  160. }
  161. static int asn1_bio_free(BIO *b)
  162. {
  163. BIO_ASN1_BUF_CTX *ctx;
  164. ctx = (BIO_ASN1_BUF_CTX *)b->ptr;
  165. if (ctx == NULL)
  166. return 0;
  167. if (ctx->buf)
  168. OPENSSL_free(ctx->buf);
  169. OPENSSL_free(ctx);
  170. b->init = 0;
  171. b->ptr = NULL;
  172. b->flags = 0;
  173. return 1;
  174. }
  175. static int asn1_bio_write(BIO *b, const char *in, int inl)
  176. {
  177. BIO_ASN1_BUF_CTX *ctx;
  178. int wrmax, wrlen, ret;
  179. unsigned char *p;
  180. if (!in || (inl < 0) || (b->next_bio == NULL))
  181. return 0;
  182. ctx = (BIO_ASN1_BUF_CTX *)b->ptr;
  183. if (ctx == NULL)
  184. return 0;
  185. wrlen = 0;
  186. ret = -1;
  187. for (;;) {
  188. switch (ctx->state) {
  189. /* Setup prefix data, call it */
  190. case ASN1_STATE_START:
  191. if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
  192. ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
  193. return 0;
  194. break;
  195. /* Copy any pre data first */
  196. case ASN1_STATE_PRE_COPY:
  197. ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
  198. ASN1_STATE_HEADER);
  199. if (ret <= 0)
  200. goto done;
  201. break;
  202. case ASN1_STATE_HEADER:
  203. ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
  204. assert(ctx->buflen <= ctx->bufsize);
  205. p = ctx->buf;
  206. ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
  207. ctx->copylen = inl;
  208. ctx->state = ASN1_STATE_HEADER_COPY;
  209. break;
  210. case ASN1_STATE_HEADER_COPY:
  211. ret = BIO_write(b->next_bio, ctx->buf + ctx->bufpos, ctx->buflen);
  212. if (ret <= 0)
  213. goto done;
  214. ctx->buflen -= ret;
  215. if (ctx->buflen)
  216. ctx->bufpos += ret;
  217. else {
  218. ctx->bufpos = 0;
  219. ctx->state = ASN1_STATE_DATA_COPY;
  220. }
  221. break;
  222. case ASN1_STATE_DATA_COPY:
  223. if (inl > ctx->copylen)
  224. wrmax = ctx->copylen;
  225. else
  226. wrmax = inl;
  227. ret = BIO_write(b->next_bio, in, wrmax);
  228. if (ret <= 0)
  229. break;
  230. wrlen += ret;
  231. ctx->copylen -= ret;
  232. in += ret;
  233. inl -= ret;
  234. if (ctx->copylen == 0)
  235. ctx->state = ASN1_STATE_HEADER;
  236. if (inl == 0)
  237. goto done;
  238. break;
  239. default:
  240. BIO_clear_retry_flags(b);
  241. return 0;
  242. }
  243. }
  244. done:
  245. BIO_clear_retry_flags(b);
  246. BIO_copy_next_retry(b);
  247. return (wrlen > 0) ? wrlen : ret;
  248. }
  249. static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  250. asn1_ps_func *cleanup, asn1_bio_state_t next)
  251. {
  252. int ret;
  253. if (ctx->ex_len <= 0)
  254. return 1;
  255. for (;;) {
  256. ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
  257. if (ret <= 0)
  258. break;
  259. ctx->ex_len -= ret;
  260. if (ctx->ex_len > 0)
  261. ctx->ex_pos += ret;
  262. else {
  263. if (cleanup)
  264. cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
  265. ctx->state = next;
  266. ctx->ex_pos = 0;
  267. break;
  268. }
  269. }
  270. return ret;
  271. }
  272. static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  273. asn1_ps_func *setup,
  274. asn1_bio_state_t ex_state,
  275. asn1_bio_state_t other_state)
  276. {
  277. if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
  278. BIO_clear_retry_flags(b);
  279. return 0;
  280. }
  281. if (ctx->ex_len > 0)
  282. ctx->state = ex_state;
  283. else
  284. ctx->state = other_state;
  285. return 1;
  286. }
  287. static int asn1_bio_read(BIO *b, char *in, int inl)
  288. {
  289. if (!b->next_bio)
  290. return 0;
  291. return BIO_read(b->next_bio, in, inl);
  292. }
  293. static int asn1_bio_puts(BIO *b, const char *str)
  294. {
  295. return asn1_bio_write(b, str, strlen(str));
  296. }
  297. static int asn1_bio_gets(BIO *b, char *str, int size)
  298. {
  299. if (!b->next_bio)
  300. return 0;
  301. return BIO_gets(b->next_bio, str, size);
  302. }
  303. static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb fp)
  304. {
  305. if (b->next_bio == NULL)
  306. return (0);
  307. return BIO_callback_ctrl(b->next_bio, cmd, fp);
  308. }
  309. static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
  310. {
  311. BIO_ASN1_BUF_CTX *ctx;
  312. BIO_ASN1_EX_FUNCS *ex_func;
  313. long ret = 1;
  314. ctx = (BIO_ASN1_BUF_CTX *)b->ptr;
  315. if (ctx == NULL)
  316. return 0;
  317. switch (cmd) {
  318. case BIO_C_SET_PREFIX:
  319. ex_func = arg2;
  320. ctx->prefix = ex_func->ex_func;
  321. ctx->prefix_free = ex_func->ex_free_func;
  322. break;
  323. case BIO_C_GET_PREFIX:
  324. ex_func = arg2;
  325. ex_func->ex_func = ctx->prefix;
  326. ex_func->ex_free_func = ctx->prefix_free;
  327. break;
  328. case BIO_C_SET_SUFFIX:
  329. ex_func = arg2;
  330. ctx->suffix = ex_func->ex_func;
  331. ctx->suffix_free = ex_func->ex_free_func;
  332. break;
  333. case BIO_C_GET_SUFFIX:
  334. ex_func = arg2;
  335. ex_func->ex_func = ctx->suffix;
  336. ex_func->ex_free_func = ctx->suffix_free;
  337. break;
  338. case BIO_C_SET_EX_ARG:
  339. ctx->ex_arg = arg2;
  340. break;
  341. case BIO_C_GET_EX_ARG:
  342. *(void **)arg2 = ctx->ex_arg;
  343. break;
  344. case BIO_CTRL_FLUSH:
  345. if (!b->next_bio)
  346. return 0;
  347. /* Call post function if possible */
  348. if (ctx->state == ASN1_STATE_HEADER) {
  349. if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
  350. ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
  351. return 0;
  352. }
  353. if (ctx->state == ASN1_STATE_POST_COPY) {
  354. ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
  355. ASN1_STATE_DONE);
  356. if (ret <= 0)
  357. return ret;
  358. }
  359. if (ctx->state == ASN1_STATE_DONE)
  360. return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
  361. else {
  362. BIO_clear_retry_flags(b);
  363. return 0;
  364. }
  365. break;
  366. default:
  367. if (!b->next_bio)
  368. return 0;
  369. return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
  370. }
  371. return ret;
  372. }
  373. static int asn1_bio_set_ex(BIO *b, int cmd,
  374. asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
  375. {
  376. BIO_ASN1_EX_FUNCS extmp;
  377. extmp.ex_func = ex_func;
  378. extmp.ex_free_func = ex_free_func;
  379. return BIO_ctrl(b, cmd, 0, &extmp);
  380. }
  381. static int asn1_bio_get_ex(BIO *b, int cmd,
  382. asn1_ps_func **ex_func,
  383. asn1_ps_func **ex_free_func)
  384. {
  385. BIO_ASN1_EX_FUNCS extmp;
  386. int ret;
  387. ret = BIO_ctrl(b, cmd, 0, &extmp);
  388. if (ret > 0) {
  389. *ex_func = extmp.ex_func;
  390. *ex_free_func = extmp.ex_free_func;
  391. }
  392. return ret;
  393. }
  394. int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
  395. asn1_ps_func *prefix_free)
  396. {
  397. return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
  398. }
  399. int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
  400. asn1_ps_func **pprefix_free)
  401. {
  402. return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
  403. }
  404. int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
  405. asn1_ps_func *suffix_free)
  406. {
  407. return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
  408. }
  409. int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
  410. asn1_ps_func **psuffix_free)
  411. {
  412. return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
  413. }