TLSSocket.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef BELL_TLS_SOCKET_H
  2. #define BELL_TLS_SOCKET_H
  3. #include "BellLogger.h"
  4. #include "BellSocket.h"
  5. #include <cstring>
  6. #include <ctype.h>
  7. #include <fstream>
  8. #include <iostream>
  9. #include <memory>
  10. #include <netdb.h>
  11. #include <netinet/in.h>
  12. #include <netinet/tcp.h>
  13. #include <sstream>
  14. #include <stdlib.h>
  15. #include <string>
  16. #include <sys/socket.h>
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. #include <vector>
  20. #ifdef BELL_USE_MBEDTLS
  21. #include "mbedtls/net_sockets.h"
  22. #include "mbedtls/ssl.h"
  23. #include "mbedtls/entropy.h"
  24. #include "mbedtls/ctr_drbg.h"
  25. #include "mbedtls/debug.h"
  26. #else
  27. #include <openssl/bio.h>
  28. #include <openssl/err.h>
  29. #include <openssl/ssl.h>
  30. #endif
  31. namespace bell {
  32. class TLSSocket : public bell::Socket {
  33. private:
  34. #ifdef BELL_USE_MBEDTLS
  35. mbedtls_net_context server_fd;
  36. mbedtls_entropy_context entropy;
  37. mbedtls_ctr_drbg_context ctr_drbg;
  38. mbedtls_ssl_context ssl;
  39. mbedtls_ssl_config conf;
  40. #else
  41. BIO *sbio, *out;
  42. int len;
  43. char tmpbuf[1024];
  44. SSL_CTX *ctx;
  45. SSL *ssl;
  46. int sockFd;
  47. int sslFd;
  48. #endif
  49. bool isClosed = false;
  50. public:
  51. TLSSocket();
  52. ~TLSSocket() { close(); };
  53. void open(std::string host);
  54. size_t read(uint8_t *buf, size_t len);
  55. size_t write(uint8_t *buf, size_t len);
  56. void close();
  57. };
  58. } // namespace bell
  59. #endif