2
0

bio_asn1.c 11 KB

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