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.
 
 
 
 
 
 

496 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. {
  140. OPENSSL_free(ctx);
  141. return 0;
  142. }
  143. b->init = 1;
  144. b->ptr = (char *)ctx;
  145. b->flags = 0;
  146. return 1;
  147. }
  148. static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
  149. {
  150. ctx->buf = OPENSSL_malloc(size);
  151. if (!ctx->buf)
  152. return 0;
  153. ctx->bufsize = size;
  154. ctx->bufpos = 0;
  155. ctx->buflen = 0;
  156. ctx->copylen = 0;
  157. ctx->asn1_class = V_ASN1_UNIVERSAL;
  158. ctx->asn1_tag = V_ASN1_OCTET_STRING;
  159. ctx->ex_buf = 0;
  160. ctx->ex_pos = 0;
  161. ctx->ex_len = 0;
  162. ctx->state = ASN1_STATE_START;
  163. return 1;
  164. }
  165. static int asn1_bio_free(BIO *b)
  166. {
  167. BIO_ASN1_BUF_CTX *ctx;
  168. ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
  169. if (ctx == NULL)
  170. return 0;
  171. if (ctx->buf)
  172. OPENSSL_free(ctx->buf);
  173. OPENSSL_free(ctx);
  174. b->init = 0;
  175. b->ptr = NULL;
  176. b->flags = 0;
  177. return 1;
  178. }
  179. static int asn1_bio_write(BIO *b, const char *in , int inl)
  180. {
  181. BIO_ASN1_BUF_CTX *ctx;
  182. int wrmax, wrlen, ret;
  183. unsigned char *p;
  184. if (!in || (inl < 0) || (b->next_bio == NULL))
  185. return 0;
  186. ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
  187. if (ctx == NULL)
  188. return 0;
  189. wrlen = 0;
  190. ret = -1;
  191. for(;;)
  192. {
  193. switch (ctx->state)
  194. {
  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 =
  210. ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
  211. assert(ctx->buflen <= ctx->bufsize);
  212. p = ctx->buf;
  213. ASN1_put_object(&p, 0, inl,
  214. 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,
  220. ctx->buf + ctx->bufpos, ctx->buflen);
  221. if (ret <= 0)
  222. goto done;
  223. ctx->buflen -= ret;
  224. if (ctx->buflen)
  225. ctx->bufpos += ret;
  226. else
  227. {
  228. ctx->bufpos = 0;
  229. ctx->state = ASN1_STATE_DATA_COPY;
  230. }
  231. break;
  232. case ASN1_STATE_DATA_COPY:
  233. if (inl > ctx->copylen)
  234. wrmax = ctx->copylen;
  235. else
  236. wrmax = inl;
  237. ret = BIO_write(b->next_bio, in, wrmax);
  238. if (ret <= 0)
  239. break;
  240. wrlen += ret;
  241. ctx->copylen -= ret;
  242. in += ret;
  243. inl -= ret;
  244. if (ctx->copylen == 0)
  245. ctx->state = ASN1_STATE_HEADER;
  246. if (inl == 0)
  247. goto done;
  248. break;
  249. default:
  250. BIO_clear_retry_flags(b);
  251. return 0;
  252. }
  253. }
  254. done:
  255. BIO_clear_retry_flags(b);
  256. BIO_copy_next_retry(b);
  257. return (wrlen > 0) ? wrlen : ret;
  258. }
  259. static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  260. asn1_ps_func *cleanup, asn1_bio_state_t next)
  261. {
  262. int ret;
  263. if (ctx->ex_len <= 0)
  264. return 1;
  265. for(;;)
  266. {
  267. ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos,
  268. ctx->ex_len);
  269. if (ret <= 0)
  270. break;
  271. ctx->ex_len -= ret;
  272. if (ctx->ex_len > 0)
  273. ctx->ex_pos += ret;
  274. else
  275. {
  276. if(cleanup)
  277. cleanup(b, &ctx->ex_buf, &ctx->ex_len,
  278. &ctx->ex_arg);
  279. ctx->state = next;
  280. ctx->ex_pos = 0;
  281. break;
  282. }
  283. }
  284. return ret;
  285. }
  286. static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
  287. asn1_ps_func *setup,
  288. asn1_bio_state_t ex_state,
  289. asn1_bio_state_t other_state)
  290. {
  291. if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg))
  292. {
  293. BIO_clear_retry_flags(b);
  294. return 0;
  295. }
  296. if (ctx->ex_len > 0)
  297. ctx->state = ex_state;
  298. else
  299. ctx->state = other_state;
  300. return 1;
  301. }
  302. static int asn1_bio_read(BIO *b, char *in , int inl)
  303. {
  304. if (!b->next_bio)
  305. return 0;
  306. return BIO_read(b->next_bio, in , inl);
  307. }
  308. static int asn1_bio_puts(BIO *b, const char *str)
  309. {
  310. return asn1_bio_write(b, str, strlen(str));
  311. }
  312. static int asn1_bio_gets(BIO *b, char *str, int size)
  313. {
  314. if (!b->next_bio)
  315. return 0;
  316. return BIO_gets(b->next_bio, str , size);
  317. }
  318. static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb fp)
  319. {
  320. if (b->next_bio == NULL) return(0);
  321. return BIO_callback_ctrl(b->next_bio,cmd,fp);
  322. }
  323. static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
  324. {
  325. BIO_ASN1_BUF_CTX *ctx;
  326. BIO_ASN1_EX_FUNCS *ex_func;
  327. long ret = 1;
  328. ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
  329. if (ctx == NULL)
  330. return 0;
  331. switch(cmd)
  332. {
  333. case BIO_C_SET_PREFIX:
  334. ex_func = arg2;
  335. ctx->prefix = ex_func->ex_func;
  336. ctx->prefix_free = ex_func->ex_free_func;
  337. break;
  338. case BIO_C_GET_PREFIX:
  339. ex_func = arg2;
  340. ex_func->ex_func = ctx->prefix;
  341. ex_func->ex_free_func = ctx->prefix_free;
  342. break;
  343. case BIO_C_SET_SUFFIX:
  344. ex_func = arg2;
  345. ctx->suffix = ex_func->ex_func;
  346. ctx->suffix_free = ex_func->ex_free_func;
  347. break;
  348. case BIO_C_GET_SUFFIX:
  349. ex_func = arg2;
  350. ex_func->ex_func = ctx->suffix;
  351. ex_func->ex_free_func = ctx->suffix_free;
  352. break;
  353. case BIO_C_SET_EX_ARG:
  354. ctx->ex_arg = arg2;
  355. break;
  356. case BIO_C_GET_EX_ARG:
  357. *(void **)arg2 = ctx->ex_arg;
  358. break;
  359. case BIO_CTRL_FLUSH:
  360. if (!b->next_bio)
  361. return 0;
  362. /* Call post function if possible */
  363. if (ctx->state == ASN1_STATE_HEADER)
  364. {
  365. if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
  366. ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
  367. return 0;
  368. }
  369. if (ctx->state == ASN1_STATE_POST_COPY)
  370. {
  371. ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
  372. ASN1_STATE_DONE);
  373. if (ret <= 0)
  374. return ret;
  375. }
  376. if (ctx->state == ASN1_STATE_DONE)
  377. return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
  378. else
  379. {
  380. BIO_clear_retry_flags(b);
  381. return 0;
  382. }
  383. break;
  384. default:
  385. if (!b->next_bio)
  386. return 0;
  387. return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
  388. }
  389. return ret;
  390. }
  391. static int asn1_bio_set_ex(BIO *b, int cmd,
  392. asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
  393. {
  394. BIO_ASN1_EX_FUNCS extmp;
  395. extmp.ex_func = ex_func;
  396. extmp.ex_free_func = ex_free_func;
  397. return BIO_ctrl(b, cmd, 0, &extmp);
  398. }
  399. static int asn1_bio_get_ex(BIO *b, int cmd,
  400. asn1_ps_func **ex_func, asn1_ps_func **ex_free_func)
  401. {
  402. BIO_ASN1_EX_FUNCS extmp;
  403. int ret;
  404. ret = BIO_ctrl(b, cmd, 0, &extmp);
  405. if (ret > 0)
  406. {
  407. *ex_func = extmp.ex_func;
  408. *ex_free_func = extmp.ex_free_func;
  409. }
  410. return ret;
  411. }
  412. int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free)
  413. {
  414. return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
  415. }
  416. int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, asn1_ps_func **pprefix_free)
  417. {
  418. return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
  419. }
  420. int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free)
  421. {
  422. return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
  423. }
  424. int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, asn1_ps_func **psuffix_free)
  425. {
  426. return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
  427. }