bio_ber.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* crypto/evp/bio_ber.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <errno.h>
  60. #include "cryptlib.h"
  61. #include <openssl/buffer.h>
  62. #include <openssl/evp.h>
  63. static int ber_write(BIO *h,char *buf,int num);
  64. static int ber_read(BIO *h,char *buf,int size);
  65. /*static int ber_puts(BIO *h,char *str); */
  66. /*static int ber_gets(BIO *h,char *str,int size); */
  67. static long ber_ctrl(BIO *h,int cmd,long arg1,char *arg2);
  68. static int ber_new(BIO *h);
  69. static int ber_free(BIO *data);
  70. static long ber_callback_ctrl(BIO *h,int cmd,void *(*fp)());
  71. #define BER_BUF_SIZE (32)
  72. /* This is used to hold the state of the BER objects being read. */
  73. typedef struct ber_struct
  74. {
  75. int tag;
  76. int class;
  77. long length;
  78. int inf;
  79. int num_left;
  80. int depth;
  81. } BER_CTX;
  82. typedef struct bio_ber_struct
  83. {
  84. int tag;
  85. int class;
  86. long length;
  87. int inf;
  88. /* most of the following are used when doing non-blocking IO */
  89. /* reading */
  90. long num_left; /* number of bytes still to read/write in block */
  91. int depth; /* used with indefinite encoding. */
  92. int finished; /* No more read data */
  93. /* writting */
  94. char *w_addr;
  95. int w_offset;
  96. int w_left;
  97. int buf_len;
  98. int buf_off;
  99. unsigned char buf[BER_BUF_SIZE];
  100. } BIO_BER_CTX;
  101. static BIO_METHOD methods_ber=
  102. {
  103. BIO_TYPE_CIPHER,"cipher",
  104. ber_write,
  105. ber_read,
  106. NULL, /* ber_puts, */
  107. NULL, /* ber_gets, */
  108. ber_ctrl,
  109. ber_new,
  110. ber_free,
  111. ber_callback_ctrl,
  112. };
  113. BIO_METHOD *BIO_f_ber(void)
  114. {
  115. return(&methods_ber);
  116. }
  117. static int ber_new(BIO *bi)
  118. {
  119. BIO_BER_CTX *ctx;
  120. ctx=(BIO_BER_CTX *)OPENSSL_malloc(sizeof(BIO_BER_CTX));
  121. if (ctx == NULL) return(0);
  122. memset((char *)ctx,0,sizeof(BIO_BER_CTX));
  123. bi->init=0;
  124. bi->ptr=(char *)ctx;
  125. bi->flags=0;
  126. return(1);
  127. }
  128. static int ber_free(BIO *a)
  129. {
  130. BIO_BER_CTX *b;
  131. if (a == NULL) return(0);
  132. b=(BIO_BER_CTX *)a->ptr;
  133. OPENSSL_cleanse(a->ptr,sizeof(BIO_BER_CTX));
  134. OPENSSL_free(a->ptr);
  135. a->ptr=NULL;
  136. a->init=0;
  137. a->flags=0;
  138. return(1);
  139. }
  140. int bio_ber_get_header(BIO *bio, BIO_BER_CTX *ctx)
  141. {
  142. char buf[64];
  143. int i,j,n;
  144. int ret;
  145. unsigned char *p;
  146. unsigned long length
  147. int tag;
  148. int class;
  149. long max;
  150. BIO_clear_retry_flags(b);
  151. /* Pack the buffer down if there is a hole at the front */
  152. if (ctx->buf_off != 0)
  153. {
  154. p=ctx->buf;
  155. j=ctx->buf_off;
  156. n=ctx->buf_len-j;
  157. for (i=0; i<n; i++)
  158. {
  159. p[0]=p[j];
  160. p++;
  161. }
  162. ctx->buf_len-j;
  163. ctx->buf_off=0;
  164. }
  165. /* If there is more room, read some more data */
  166. i=BER_BUF_SIZE-ctx->buf_len;
  167. if (i)
  168. {
  169. i=BIO_read(bio->next_bio,&(ctx->buf[ctx->buf_len]),i);
  170. if (i <= 0)
  171. {
  172. BIO_copy_next_retry(b);
  173. return(i);
  174. }
  175. else
  176. ctx->buf_len+=i;
  177. }
  178. max=ctx->buf_len;
  179. p=ctx->buf;
  180. ret=ASN1_get_object(&p,&length,&tag,&class,max);
  181. if (ret & 0x80)
  182. {
  183. if ((ctx->buf_len < BER_BUF_SIZE) &&
  184. (ERR_GET_REASON(ERR_peek_error()) == ASN1_R_TOO_LONG))
  185. {
  186. ERR_clear_error(); /* clear the error */
  187. BIO_set_retry_read(b);
  188. }
  189. return(-1);
  190. }
  191. /* We have no error, we have a header, so make use of it */
  192. if ((ctx->tag >= 0) && (ctx->tag != tag))
  193. {
  194. BIOerr(BIO_F_BIO_BER_GET_HEADER,BIO_R_TAG_MISMATCH);
  195. sprintf(buf,"tag=%d, got %d",ctx->tag,tag);
  196. ERR_add_error_data(1,buf);
  197. return(-1);
  198. }
  199. if (ret & 0x01)
  200. if (ret & V_ASN1_CONSTRUCTED)
  201. }
  202. static int ber_read(BIO *b, char *out, int outl)
  203. {
  204. int ret=0,i,n;
  205. BIO_BER_CTX *ctx;
  206. BIO_clear_retry_flags(b);
  207. if (out == NULL) return(0);
  208. ctx=(BIO_BER_CTX *)b->ptr;
  209. if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
  210. if (ctx->finished) return(0);
  211. again:
  212. /* First see if we are half way through reading a block */
  213. if (ctx->num_left > 0)
  214. {
  215. if (ctx->num_left < outl)
  216. n=ctx->num_left;
  217. else
  218. n=outl;
  219. i=BIO_read(b->next_bio,out,n);
  220. if (i <= 0)
  221. {
  222. BIO_copy_next_retry(b);
  223. return(i);
  224. }
  225. ctx->num_left-=i;
  226. outl-=i;
  227. ret+=i;
  228. if (ctx->num_left <= 0)
  229. {
  230. ctx->depth--;
  231. if (ctx->depth <= 0)
  232. ctx->finished=1;
  233. }
  234. if (outl <= 0)
  235. return(ret);
  236. else
  237. goto again;
  238. }
  239. else /* we need to read another BER header */
  240. {
  241. }
  242. }
  243. static int ber_write(BIO *b, char *in, int inl)
  244. {
  245. int ret=0,n,i;
  246. BIO_ENC_CTX *ctx;
  247. ctx=(BIO_ENC_CTX *)b->ptr;
  248. ret=inl;
  249. BIO_clear_retry_flags(b);
  250. n=ctx->buf_len-ctx->buf_off;
  251. while (n > 0)
  252. {
  253. i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
  254. if (i <= 0)
  255. {
  256. BIO_copy_next_retry(b);
  257. return(i);
  258. }
  259. ctx->buf_off+=i;
  260. n-=i;
  261. }
  262. /* at this point all pending data has been written */
  263. if ((in == NULL) || (inl <= 0)) return(0);
  264. ctx->buf_off=0;
  265. while (inl > 0)
  266. {
  267. n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
  268. EVP_CipherUpdate(&(ctx->cipher),
  269. (unsigned char *)ctx->buf,&ctx->buf_len,
  270. (unsigned char *)in,n);
  271. inl-=n;
  272. in+=n;
  273. ctx->buf_off=0;
  274. n=ctx->buf_len;
  275. while (n > 0)
  276. {
  277. i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
  278. if (i <= 0)
  279. {
  280. BIO_copy_next_retry(b);
  281. return(i);
  282. }
  283. n-=i;
  284. ctx->buf_off+=i;
  285. }
  286. ctx->buf_len=0;
  287. ctx->buf_off=0;
  288. }
  289. BIO_copy_next_retry(b);
  290. return(ret);
  291. }
  292. static long ber_ctrl(BIO *b, int cmd, long num, char *ptr)
  293. {
  294. BIO *dbio;
  295. BIO_ENC_CTX *ctx,*dctx;
  296. long ret=1;
  297. int i;
  298. ctx=(BIO_ENC_CTX *)b->ptr;
  299. switch (cmd)
  300. {
  301. case BIO_CTRL_RESET:
  302. ctx->ok=1;
  303. ctx->finished=0;
  304. EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL,
  305. ctx->cipher.berrypt);
  306. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  307. break;
  308. case BIO_CTRL_EOF: /* More to read */
  309. if (ctx->cont <= 0)
  310. ret=1;
  311. else
  312. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  313. break;
  314. case BIO_CTRL_WPENDING:
  315. ret=ctx->buf_len-ctx->buf_off;
  316. if (ret <= 0)
  317. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  318. break;
  319. case BIO_CTRL_PENDING: /* More to read in buffer */
  320. ret=ctx->buf_len-ctx->buf_off;
  321. if (ret <= 0)
  322. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  323. break;
  324. case BIO_CTRL_FLUSH:
  325. /* do a final write */
  326. again:
  327. while (ctx->buf_len != ctx->buf_off)
  328. {
  329. i=ber_write(b,NULL,0);
  330. if (i < 0)
  331. {
  332. ret=i;
  333. break;
  334. }
  335. }
  336. if (!ctx->finished)
  337. {
  338. ctx->finished=1;
  339. ctx->buf_off=0;
  340. ret=EVP_CipherFinal_ex(&(ctx->cipher),
  341. (unsigned char *)ctx->buf,
  342. &(ctx->buf_len));
  343. ctx->ok=(int)ret;
  344. if (ret <= 0) break;
  345. /* push out the bytes */
  346. goto again;
  347. }
  348. /* Finally flush the underlying BIO */
  349. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  350. break;
  351. case BIO_C_GET_CIPHER_STATUS:
  352. ret=(long)ctx->ok;
  353. break;
  354. case BIO_C_DO_STATE_MACHINE:
  355. BIO_clear_retry_flags(b);
  356. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  357. BIO_copy_next_retry(b);
  358. break;
  359. case BIO_CTRL_DUP:
  360. dbio=(BIO *)ptr;
  361. dctx=(BIO_ENC_CTX *)dbio->ptr;
  362. memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher));
  363. dbio->init=1;
  364. break;
  365. default:
  366. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  367. break;
  368. }
  369. return(ret);
  370. }
  371. static long ber_callback_ctrl(BIO *b, int cmd, void *(*fp)())
  372. {
  373. long ret=1;
  374. if (b->next_bio == NULL) return(0);
  375. switch (cmd)
  376. {
  377. default:
  378. ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
  379. break;
  380. }
  381. return(ret);
  382. }
  383. /*
  384. void BIO_set_cipher_ctx(b,c)
  385. BIO *b;
  386. EVP_CIPHER_ctx *c;
  387. {
  388. if (b == NULL) return;
  389. if ((b->callback != NULL) &&
  390. (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
  391. return;
  392. b->init=1;
  393. ctx=(BIO_ENC_CTX *)b->ptr;
  394. memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
  395. if (b->callback != NULL)
  396. b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
  397. }
  398. */
  399. void BIO_set_cipher(BIO *b, EVP_CIPHER *c, unsigned char *k, unsigned char *i,
  400. int e)
  401. {
  402. BIO_ENC_CTX *ctx;
  403. if (b == NULL) return;
  404. if ((b->callback != NULL) &&
  405. (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
  406. return;
  407. b->init=1;
  408. ctx=(BIO_ENC_CTX *)b->ptr;
  409. EVP_CipherInit_ex(&(ctx->cipher),c,NULL,k,i,e);
  410. if (b->callback != NULL)
  411. b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
  412. }