ustream-io-wolfssl.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * ustream-ssl - library for SSL over ustream
  3. *
  4. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <string.h>
  19. #include <libubox/ustream.h>
  20. #include "ustream-ssl.h"
  21. #include "ustream-internal.h"
  22. static int s_ustream_read(char *buf, int len, void *ctx)
  23. {
  24. struct ustream *s = ctx;
  25. char *sbuf;
  26. int slen;
  27. if (s->eof)
  28. return -3;
  29. sbuf = ustream_get_read_buf(s, &slen);
  30. if (slen > len)
  31. slen = len;
  32. if (!slen)
  33. return -2;
  34. memcpy(buf, sbuf, slen);
  35. ustream_consume(s, slen);
  36. return slen;
  37. }
  38. static int s_ustream_write(char *buf, int len, void *ctx)
  39. {
  40. struct ustream *s = ctx;
  41. if (s->write_error)
  42. return len;
  43. return ustream_write(s, buf, len, false);
  44. }
  45. static int io_recv_cb(SSL* ssl, char *buf, int sz, void *ctx)
  46. {
  47. return s_ustream_read(buf, sz, ctx);
  48. }
  49. static int io_send_cb(SSL* ssl, char *buf, int sz, void *ctx)
  50. {
  51. return s_ustream_write(buf, sz, ctx);
  52. }
  53. __hidden void ustream_set_io(struct ustream_ssl *us)
  54. {
  55. if (!us->conn) {
  56. wolfSSL_set_fd(us->ssl, us->fd.fd);
  57. return;
  58. }
  59. wolfSSL_SSLSetIORecv(us->ssl, io_recv_cb);
  60. wolfSSL_SSLSetIOSend(us->ssl, io_send_cb);
  61. wolfSSL_SetIOReadCtx(us->ssl, us->conn);
  62. wolfSSL_SetIOWriteCtx(us->ssl, us->conn);
  63. }