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.
 
 
 
 
 
 

493 regels
12 KiB

  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 <openssl/bio.h>
  59. #include <openssl/mem.h>
  60. /* Must be large enough for biggest tag+length */
  61. #define DEFAULT_ASN1_BUF_SIZE 20
  62. typedef enum
  63. {
  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. {
  74. asn1_ps_func *ex_func;
  75. asn1_ps_func *ex_free_func;
  76. } BIO_ASN1_EX_FUNCS;
  77. typedef struct BIO_ASN1_BUF_CTX_t
  78. {
  79. /* Internal state */
  80. asn1_bio_state_t state;
  81. /* Internal buffer */
  82. unsigned char *buf;
  83. /* Size of buffer */
  84. int bufsize;
  85. /* Current position in buffer */
  86. int bufpos;
  87. /* Current buffer length */
  88. int buflen;
  89. /* Amount of data to copy */
  90. int copylen;
  91. /* Class and tag to use */
  92. int asn1_class, asn1_tag;
  93. asn1_ps_func *prefix, *prefix_free, *suffix, *suffix_free;
  94. /* Extra buffer for prefix and suffix data */
  95. unsigned char *ex_buf;
  96. int ex_len;
  97. int ex_pos;
  98. void *ex_arg;
  99. } BIO_ASN1_BUF_CTX;
  100. static int asn1_bio_write(BIO *h, const char *buf,int num);
  101. static int asn1_bio_read(BIO *h, char *buf, int size);
  102. static int asn1_bio_puts(BIO *h, const char *str);
  103. static int asn1_bio_gets(BIO *h, char *str, int size);
  104. static long asn1_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  105. static int asn1_bio_new(BIO *h);
  106. static int asn1_bio_free(BIO *data);
  107. static long asn1_bio_callback_ctrl(BIO *h, int cmd, bio_info_cb fp);
  108. static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size);
  109. static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  110. asn1_ps_func *cleanup, asn1_bio_state_t next);
  111. static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  112. asn1_ps_func *setup,
  113. asn1_bio_state_t ex_state,
  114. asn1_bio_state_t other_state);
  115. static BIO_METHOD methods_asn1=
  116. {
  117. BIO_TYPE_ASN1,
  118. "asn1",
  119. asn1_bio_write,
  120. asn1_bio_read,
  121. asn1_bio_puts,
  122. asn1_bio_gets,
  123. asn1_bio_ctrl,
  124. asn1_bio_new,
  125. asn1_bio_free,
  126. asn1_bio_callback_ctrl,
  127. };
  128. BIO_METHOD *BIO_f_asn1(void)
  129. {
  130. return(&methods_asn1);
  131. }
  132. static int asn1_bio_new(BIO *b)
  133. {
  134. BIO_ASN1_BUF_CTX *ctx;
  135. ctx = OPENSSL_malloc(sizeof(BIO_ASN1_BUF_CTX));
  136. if (!ctx)
  137. return 0;
  138. if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE))
  139. return 0;
  140. b->init = 1;
  141. b->ptr = (char *)ctx;
  142. b->flags = 0;
  143. return 1;
  144. }
  145. static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
  146. {
  147. ctx->buf = OPENSSL_malloc(size);
  148. if (!ctx->buf)
  149. return 0;
  150. ctx->bufsize = size;
  151. ctx->bufpos = 0;
  152. ctx->buflen = 0;
  153. ctx->copylen = 0;
  154. ctx->asn1_class = V_ASN1_UNIVERSAL;
  155. ctx->asn1_tag = V_ASN1_OCTET_STRING;
  156. ctx->ex_buf = 0;
  157. ctx->ex_pos = 0;
  158. ctx->ex_len = 0;
  159. ctx->state = ASN1_STATE_START;
  160. return 1;
  161. }
  162. static int asn1_bio_free(BIO *b)
  163. {
  164. BIO_ASN1_BUF_CTX *ctx;
  165. ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
  166. if (ctx == NULL)
  167. return 0;
  168. if (ctx->buf)
  169. OPENSSL_free(ctx->buf);
  170. OPENSSL_free(ctx);
  171. b->init = 0;
  172. b->ptr = NULL;
  173. b->flags = 0;
  174. return 1;
  175. }
  176. static int asn1_bio_write(BIO *b, const char *in , int inl)
  177. {
  178. BIO_ASN1_BUF_CTX *ctx;
  179. int wrmax, wrlen, ret;
  180. unsigned char *p;
  181. if (!in || (inl < 0) || (b->next_bio == NULL))
  182. return 0;
  183. ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
  184. if (ctx == NULL)
  185. return 0;
  186. wrlen = 0;
  187. ret = -1;
  188. for(;;)
  189. {
  190. switch (ctx->state)
  191. {
  192. /* Setup prefix data, call it */
  193. case ASN1_STATE_START:
  194. if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
  195. ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
  196. return 0;
  197. break;
  198. /* Copy any pre data first */
  199. case ASN1_STATE_PRE_COPY:
  200. ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
  201. ASN1_STATE_HEADER);
  202. if (ret <= 0)
  203. goto done;
  204. break;
  205. case ASN1_STATE_HEADER:
  206. ctx->buflen =
  207. ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
  208. assert(ctx->buflen <= ctx->bufsize);
  209. p = ctx->buf;
  210. ASN1_put_object(&p, 0, inl,
  211. ctx->asn1_tag, ctx->asn1_class);
  212. ctx->copylen = inl;
  213. ctx->state = ASN1_STATE_HEADER_COPY;
  214. break;
  215. case ASN1_STATE_HEADER_COPY:
  216. ret = BIO_write(b->next_bio,
  217. 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. {
  225. ctx->bufpos = 0;
  226. ctx->state = ASN1_STATE_DATA_COPY;
  227. }
  228. break;
  229. case ASN1_STATE_DATA_COPY:
  230. if (inl > ctx->copylen)
  231. wrmax = ctx->copylen;
  232. else
  233. wrmax = inl;
  234. ret = BIO_write(b->next_bio, in, wrmax);
  235. if (ret <= 0)
  236. break;
  237. wrlen += ret;
  238. ctx->copylen -= ret;
  239. in += ret;
  240. inl -= ret;
  241. if (ctx->copylen == 0)
  242. ctx->state = ASN1_STATE_HEADER;
  243. if (inl == 0)
  244. goto done;
  245. break;
  246. default:
  247. BIO_clear_retry_flags(b);
  248. return 0;
  249. }
  250. }
  251. done:
  252. BIO_clear_retry_flags(b);
  253. BIO_copy_next_retry(b);
  254. return (wrlen > 0) ? wrlen : ret;
  255. }
  256. static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  257. asn1_ps_func *cleanup, asn1_bio_state_t next)
  258. {
  259. int ret;
  260. if (ctx->ex_len <= 0)
  261. return 1;
  262. for(;;)
  263. {
  264. ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos,
  265. ctx->ex_len);
  266. if (ret <= 0)
  267. break;
  268. ctx->ex_len -= ret;
  269. if (ctx->ex_len > 0)
  270. ctx->ex_pos += ret;
  271. else
  272. {
  273. if(cleanup)
  274. cleanup(b, &ctx->ex_buf, &ctx->ex_len,
  275. &ctx->ex_arg);
  276. ctx->state = next;
  277. ctx->ex_pos = 0;
  278. break;
  279. }
  280. }
  281. return ret;
  282. }
  283. static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  284. asn1_ps_func *setup,
  285. asn1_bio_state_t ex_state,
  286. asn1_bio_state_t other_state)
  287. {
  288. if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg))
  289. {
  290. BIO_clear_retry_flags(b);
  291. return 0;
  292. }
  293. if (ctx->ex_len > 0)
  294. ctx->state = ex_state;
  295. else
  296. ctx->state = other_state;
  297. return 1;
  298. }
  299. static int asn1_bio_read(BIO *b, char *in , int inl)
  300. {
  301. if (!b->next_bio)
  302. return 0;
  303. return BIO_read(b->next_bio, in , inl);
  304. }
  305. static int asn1_bio_puts(BIO *b, const char *str)
  306. {
  307. return asn1_bio_write(b, str, strlen(str));
  308. }
  309. static int asn1_bio_gets(BIO *b, char *str, int size)
  310. {
  311. if (!b->next_bio)
  312. return 0;
  313. return BIO_gets(b->next_bio, str , size);
  314. }
  315. static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb fp)
  316. {
  317. if (b->next_bio == NULL) return(0);
  318. return BIO_callback_ctrl(b->next_bio,cmd,fp);
  319. }
  320. static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
  321. {
  322. BIO_ASN1_BUF_CTX *ctx;
  323. BIO_ASN1_EX_FUNCS *ex_func;
  324. long ret = 1;
  325. ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
  326. if (ctx == NULL)
  327. return 0;
  328. switch(cmd)
  329. {
  330. case BIO_C_SET_PREFIX:
  331. ex_func = arg2;
  332. ctx->prefix = ex_func->ex_func;
  333. ctx->prefix_free = ex_func->ex_free_func;
  334. break;
  335. case BIO_C_GET_PREFIX:
  336. ex_func = arg2;
  337. ex_func->ex_func = ctx->prefix;
  338. ex_func->ex_free_func = ctx->prefix_free;
  339. break;
  340. case BIO_C_SET_SUFFIX:
  341. ex_func = arg2;
  342. ctx->suffix = ex_func->ex_func;
  343. ctx->suffix_free = ex_func->ex_free_func;
  344. break;
  345. case BIO_C_GET_SUFFIX:
  346. ex_func = arg2;
  347. ex_func->ex_func = ctx->suffix;
  348. ex_func->ex_free_func = ctx->suffix_free;
  349. break;
  350. case BIO_C_SET_EX_ARG:
  351. ctx->ex_arg = arg2;
  352. break;
  353. case BIO_C_GET_EX_ARG:
  354. *(void **)arg2 = ctx->ex_arg;
  355. break;
  356. case BIO_CTRL_FLUSH:
  357. if (!b->next_bio)
  358. return 0;
  359. /* Call post function if possible */
  360. if (ctx->state == ASN1_STATE_HEADER)
  361. {
  362. if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
  363. ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
  364. return 0;
  365. }
  366. if (ctx->state == ASN1_STATE_POST_COPY)
  367. {
  368. ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
  369. ASN1_STATE_DONE);
  370. if (ret <= 0)
  371. return ret;
  372. }
  373. if (ctx->state == ASN1_STATE_DONE)
  374. return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
  375. else
  376. {
  377. BIO_clear_retry_flags(b);
  378. return 0;
  379. }
  380. break;
  381. default:
  382. if (!b->next_bio)
  383. return 0;
  384. return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
  385. }
  386. return ret;
  387. }
  388. static int asn1_bio_set_ex(BIO *b, int cmd,
  389. asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
  390. {
  391. BIO_ASN1_EX_FUNCS extmp;
  392. extmp.ex_func = ex_func;
  393. extmp.ex_free_func = ex_free_func;
  394. return BIO_ctrl(b, cmd, 0, &extmp);
  395. }
  396. static int asn1_bio_get_ex(BIO *b, int cmd,
  397. asn1_ps_func **ex_func, asn1_ps_func **ex_free_func)
  398. {
  399. BIO_ASN1_EX_FUNCS extmp;
  400. int ret;
  401. ret = BIO_ctrl(b, cmd, 0, &extmp);
  402. if (ret > 0)
  403. {
  404. *ex_func = extmp.ex_func;
  405. *ex_free_func = extmp.ex_free_func;
  406. }
  407. return ret;
  408. }
  409. int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free)
  410. {
  411. return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
  412. }
  413. int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, asn1_ps_func **pprefix_free)
  414. {
  415. return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
  416. }
  417. int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free)
  418. {
  419. return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
  420. }
  421. int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, asn1_ps_func **psuffix_free)
  422. {
  423. return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
  424. }