bio_asn1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 = 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. switch (ctx->state) {
  195. /* Setup prefix data, call it */
  196. case ASN1_STATE_START:
  197. if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
  198. ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
  199. return 0;
  200. break;
  201. /* Copy any pre data first */
  202. case ASN1_STATE_PRE_COPY:
  203. ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
  204. ASN1_STATE_HEADER);
  205. if (ret <= 0)
  206. goto done;
  207. break;
  208. case ASN1_STATE_HEADER:
  209. ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
  210. OPENSSL_assert(ctx->buflen <= ctx->bufsize);
  211. p = ctx->buf;
  212. ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
  213. ctx->copylen = inl;
  214. ctx->state = ASN1_STATE_HEADER_COPY;
  215. break;
  216. case ASN1_STATE_HEADER_COPY:
  217. ret = BIO_write(b->next_bio, ctx->buf + ctx->bufpos, ctx->buflen);
  218. if (ret <= 0)
  219. goto done;
  220. ctx->buflen -= ret;
  221. if (ctx->buflen)
  222. ctx->bufpos += ret;
  223. else {
  224. ctx->bufpos = 0;
  225. ctx->state = ASN1_STATE_DATA_COPY;
  226. }
  227. break;
  228. case ASN1_STATE_DATA_COPY:
  229. if (inl > ctx->copylen)
  230. wrmax = ctx->copylen;
  231. else
  232. wrmax = inl;
  233. ret = BIO_write(b->next_bio, in, wrmax);
  234. if (ret <= 0)
  235. break;
  236. wrlen += ret;
  237. ctx->copylen -= ret;
  238. in += ret;
  239. inl -= ret;
  240. if (ctx->copylen == 0)
  241. ctx->state = ASN1_STATE_HEADER;
  242. if (inl == 0)
  243. goto done;
  244. break;
  245. default:
  246. BIO_clear_retry_flags(b);
  247. return 0;
  248. }
  249. }
  250. done:
  251. BIO_clear_retry_flags(b);
  252. BIO_copy_next_retry(b);
  253. return (wrlen > 0) ? wrlen : ret;
  254. }
  255. static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  256. asn1_ps_func *cleanup, asn1_bio_state_t next)
  257. {
  258. int ret;
  259. if (ctx->ex_len <= 0)
  260. return 1;
  261. for (;;) {
  262. ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
  263. if (ret <= 0)
  264. break;
  265. ctx->ex_len -= ret;
  266. if (ctx->ex_len > 0)
  267. ctx->ex_pos += ret;
  268. else {
  269. if (cleanup)
  270. cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
  271. ctx->state = next;
  272. ctx->ex_pos = 0;
  273. break;
  274. }
  275. }
  276. return ret;
  277. }
  278. static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  279. asn1_ps_func *setup,
  280. asn1_bio_state_t ex_state,
  281. asn1_bio_state_t other_state)
  282. {
  283. if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
  284. BIO_clear_retry_flags(b);
  285. return 0;
  286. }
  287. if (ctx->ex_len > 0)
  288. ctx->state = ex_state;
  289. else
  290. ctx->state = other_state;
  291. return 1;
  292. }
  293. static int asn1_bio_read(BIO *b, char *in, int inl)
  294. {
  295. if (!b->next_bio)
  296. return 0;
  297. return BIO_read(b->next_bio, in, inl);
  298. }
  299. static int asn1_bio_puts(BIO *b, const char *str)
  300. {
  301. return asn1_bio_write(b, str, strlen(str));
  302. }
  303. static int asn1_bio_gets(BIO *b, char *str, int size)
  304. {
  305. if (!b->next_bio)
  306. return 0;
  307. return BIO_gets(b->next_bio, str, size);
  308. }
  309. static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  310. {
  311. if (b->next_bio == NULL)
  312. return (0);
  313. return BIO_callback_ctrl(b->next_bio, cmd, fp);
  314. }
  315. static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
  316. {
  317. BIO_ASN1_BUF_CTX *ctx;
  318. BIO_ASN1_EX_FUNCS *ex_func;
  319. long ret = 1;
  320. ctx = (BIO_ASN1_BUF_CTX *)b->ptr;
  321. if (ctx == NULL)
  322. return 0;
  323. switch (cmd) {
  324. case BIO_C_SET_PREFIX:
  325. ex_func = arg2;
  326. ctx->prefix = ex_func->ex_func;
  327. ctx->prefix_free = ex_func->ex_free_func;
  328. break;
  329. case BIO_C_GET_PREFIX:
  330. ex_func = arg2;
  331. ex_func->ex_func = ctx->prefix;
  332. ex_func->ex_free_func = ctx->prefix_free;
  333. break;
  334. case BIO_C_SET_SUFFIX:
  335. ex_func = arg2;
  336. ctx->suffix = ex_func->ex_func;
  337. ctx->suffix_free = ex_func->ex_free_func;
  338. break;
  339. case BIO_C_GET_SUFFIX:
  340. ex_func = arg2;
  341. ex_func->ex_func = ctx->suffix;
  342. ex_func->ex_free_func = ctx->suffix_free;
  343. break;
  344. case BIO_C_SET_EX_ARG:
  345. ctx->ex_arg = arg2;
  346. break;
  347. case BIO_C_GET_EX_ARG:
  348. *(void **)arg2 = ctx->ex_arg;
  349. break;
  350. case BIO_CTRL_FLUSH:
  351. if (!b->next_bio)
  352. return 0;
  353. /* Call post function if possible */
  354. if (ctx->state == ASN1_STATE_HEADER) {
  355. if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
  356. ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
  357. return 0;
  358. }
  359. if (ctx->state == ASN1_STATE_POST_COPY) {
  360. ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
  361. ASN1_STATE_DONE);
  362. if (ret <= 0)
  363. return ret;
  364. }
  365. if (ctx->state == ASN1_STATE_DONE)
  366. return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
  367. else {
  368. BIO_clear_retry_flags(b);
  369. return 0;
  370. }
  371. break;
  372. default:
  373. if (!b->next_bio)
  374. return 0;
  375. return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
  376. }
  377. return ret;
  378. }
  379. static int asn1_bio_set_ex(BIO *b, int cmd,
  380. asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
  381. {
  382. BIO_ASN1_EX_FUNCS extmp;
  383. extmp.ex_func = ex_func;
  384. extmp.ex_free_func = ex_free_func;
  385. return BIO_ctrl(b, cmd, 0, &extmp);
  386. }
  387. static int asn1_bio_get_ex(BIO *b, int cmd,
  388. asn1_ps_func **ex_func,
  389. asn1_ps_func **ex_free_func)
  390. {
  391. BIO_ASN1_EX_FUNCS extmp;
  392. int ret;
  393. ret = BIO_ctrl(b, cmd, 0, &extmp);
  394. if (ret > 0) {
  395. *ex_func = extmp.ex_func;
  396. *ex_free_func = extmp.ex_free_func;
  397. }
  398. return ret;
  399. }
  400. int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
  401. asn1_ps_func *prefix_free)
  402. {
  403. return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
  404. }
  405. int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
  406. asn1_ps_func **pprefix_free)
  407. {
  408. return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
  409. }
  410. int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
  411. asn1_ps_func *suffix_free)
  412. {
  413. return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
  414. }
  415. int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
  416. asn1_ps_func **psuffix_free)
  417. {
  418. return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
  419. }