bio_asn1.c 13 KB

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