rtmp_sys.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #ifndef __RTMP_SYS_H__
  2. #define __RTMP_SYS_H__
  3. /*
  4. * Copyright (C) 2010 Howard Chu
  5. *
  6. * This file is part of librtmp.
  7. *
  8. * librtmp is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as
  10. * published by the Free Software Foundation; either version 2.1,
  11. * or (at your option) any later version.
  12. *
  13. * librtmp is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with librtmp see the file COPYING. If not, write to
  20. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/lgpl.html
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <assert.h>
  28. #include <ctype.h>
  29. #include <stddef.h>
  30. #include <errno.h>
  31. #include <stdarg.h>
  32. #include <limits.h>
  33. #include <time.h>
  34. #include <stdint.h>
  35. #ifdef _WIN32
  36. #include <winsock2.h>
  37. #include <ws2tcpip.h>
  38. #include <Mstcpip.h>
  39. #ifdef _MSC_VER /* MSVC */
  40. #define snprintf _snprintf
  41. #define strcasecmp stricmp
  42. #define strncasecmp strnicmp
  43. #define vsnprintf _vsnprintf
  44. #endif
  45. #define GetSockError() WSAGetLastError()
  46. #define SetSockError(e) WSASetLastError(e)
  47. #define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
  48. #ifdef EWOULDBLOCK
  49. #undef EWOULDBLOCK
  50. #endif
  51. #define EWOULDBLOCK WSAETIMEDOUT /* we don't use nonblocking, but we do use timeouts */
  52. #define sleep(n) Sleep(n*1000)
  53. #define msleep(n) Sleep(n)
  54. #define SET_RCVTIMEO(tv,s) int tv = s*1000
  55. #else /* !_WIN32 */
  56. #include <sys/types.h>
  57. #include <sys/socket.h>
  58. #include <sys/times.h>
  59. #include <netdb.h>
  60. #include <unistd.h>
  61. #include <netinet/in.h>
  62. #include <netinet/tcp.h>
  63. #include <arpa/inet.h>
  64. #define GetSockError() errno
  65. #define SetSockError(e) errno = e
  66. #undef closesocket
  67. #define closesocket(s) close(s)
  68. #define msleep(n) usleep(n*1000)
  69. #define SET_RCVTIMEO(tv,s) struct timeval tv = {s,0}
  70. #ifndef INVALID_SOCKET
  71. #define INVALID_SOCKET -1
  72. #endif
  73. #endif
  74. #include "rtmp.h"
  75. #if defined(USE_MBEDTLS)
  76. #include <mbedtls/version.h>
  77. #include <mbedtls/net.h>
  78. #include <mbedtls/ssl.h>
  79. #include <mbedtls/ctr_drbg.h>
  80. #include <mbedtls/entropy.h>
  81. #define my_dhm_P \
  82. "E4004C1F94182000103D883A448B3F80" \
  83. "2CE4B44A83301270002C20D0321CFD00" \
  84. "11CCEF784C26A400F43DFB901BCA7538" \
  85. "F2C6B176001CF5A0FD16D2C48B1D0C1C" \
  86. "F6AC8E1DA6BCC3B4E1F96B0564965300" \
  87. "FFA1D0B601EB2800F489AA512C4B248C" \
  88. "01F76949A60BB7F00A40B1EAB64BDD48" \
  89. "E8A700D60B7F1200FA8E77B0A979DABF"
  90. #define my_dhm_G "4"
  91. #define SSL_SET_SESSION(S,resume,timeout,ctx) mbedtls_ssl_set_session(S,ctx)
  92. typedef struct tls_ctx
  93. {
  94. mbedtls_entropy_context entropy;
  95. mbedtls_ctr_drbg_context ctr_drbg;
  96. mbedtls_ssl_config conf;
  97. mbedtls_ssl_session ssn;
  98. mbedtls_x509_crt *cacert;
  99. mbedtls_net_context net;
  100. } tls_ctx;
  101. typedef struct tls_server_ctx
  102. {
  103. mbedtls_ssl_config *conf;
  104. mbedtls_ctr_drbg_context *ctr_drbg;
  105. mbedtls_pk_context key;
  106. mbedtls_x509_crt cert;
  107. } tls_server_ctx;
  108. typedef tls_ctx *TLS_CTX;
  109. #define TLS_client(ctx,s) \
  110. s = malloc(sizeof(mbedtls_ssl_context));\
  111. mbedtls_ssl_init(s);\
  112. mbedtls_ssl_setup(s, &ctx->conf);\
  113. mbedtls_ssl_config_defaults(&ctx->conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);\
  114. mbedtls_ssl_conf_authmode(&ctx->conf, MBEDTLS_SSL_VERIFY_REQUIRED);\
  115. mbedtls_ssl_conf_rng(&ctx->conf, mbedtls_ctr_drbg_random, &ctx->ctr_drbg)
  116. #define TLS_server(ctx,s)\
  117. s = malloc(sizeof(mbedtls_ssl_context));\
  118. mbedtls_ssl_init(s);\
  119. mbedtls_ssl_setup(s, ctx->conf);\
  120. mbedtls_ssl_conf_endpoint(ctx->conf, MBEDTLS_SSL_IS_SERVER);\
  121. mbedtls_ssl_conf_authmode(ctx->conf, MBEDTLS_SSL_VERIFY_REQUIRED);\
  122. mbedtls_ssl_conf_rng(ctx->conf, mbedtls_ctr_drbg_random, ctx->ctr_drbg);\
  123. mbedtls_ssl_conf_own_cert(ctx->conf, &ctx->cert, &ctx->key);\
  124. mbedtls_ssl_conf_dh_param_bin(ctx->conf,\
  125. (const unsigned char *)my_dhm_P, strlen(my_dhm_P),\
  126. (const unsigned char *)my_dhm_G, strlen(my_dhm_G))
  127. #define TLS_setfd(s,fd) mbedtls_ssl_set_bio(s, fd, mbedtls_net_send, mbedtls_net_recv, NULL)
  128. #define TLS_connect(s) mbedtls_ssl_handshake(s)
  129. #define TLS_accept(s) mbedtls_ssl_handshake(s)
  130. #define TLS_read(s,b,l) mbedtls_ssl_read(s,(unsigned char *)b,l)
  131. #define TLS_write(s,b,l) mbedtls_ssl_write(s,(unsigned char *)b,l)
  132. #define TLS_shutdown(s) mbedtls_ssl_close_notify(s)
  133. #define TLS_close(s) mbedtls_ssl_free(s); free(s)
  134. #elif defined(USE_POLARSSL)
  135. #include <polarssl/version.h>
  136. #include <polarssl/net.h>
  137. #include <polarssl/ssl.h>
  138. #include <polarssl/havege.h>
  139. #if POLARSSL_VERSION_NUMBER < 0x01010000
  140. #define havege_random havege_rand
  141. #endif
  142. #if POLARSSL_VERSION_NUMBER >= 0x01020000
  143. #define SSL_SET_SESSION(S,resume,timeout,ctx) ssl_set_session(S,ctx)
  144. #else
  145. #define SSL_SET_SESSION(S,resume,timeout,ctx) ssl_set_session(S,resume,timeout,ctx)
  146. #endif
  147. typedef struct tls_ctx
  148. {
  149. havege_state hs;
  150. ssl_session ssn;
  151. } tls_ctx;
  152. typedef struct tls_server_ctx
  153. {
  154. havege_state *hs;
  155. x509_cert cert;
  156. rsa_context key;
  157. ssl_session ssn;
  158. const char *dhm_P, *dhm_G;
  159. } tls_server_ctx;
  160. #define TLS_CTX tls_ctx *
  161. #define TLS_client(ctx,s) s = malloc(sizeof(ssl_context)); ssl_init(s);\
  162. ssl_set_endpoint(s, SSL_IS_CLIENT); ssl_set_authmode(s, SSL_VERIFY_NONE);\
  163. ssl_set_rng(s, havege_random, &ctx->hs);\
  164. ssl_set_ciphersuites(s, ssl_default_ciphersuites);\
  165. SSL_SET_SESSION(s, 1, 600, &ctx->ssn)
  166. #define TLS_server(ctx,s) s = malloc(sizeof(ssl_context)); ssl_init(s);\
  167. ssl_set_endpoint(s, SSL_IS_SERVER); ssl_set_authmode(s, SSL_VERIFY_NONE);\
  168. ssl_set_rng(s, havege_random, ((tls_server_ctx*)ctx)->hs);\
  169. ssl_set_ciphersuites(s, ssl_default_ciphersuites);\
  170. SSL_SET_SESSION(s, 1, 600, &((tls_server_ctx*)ctx)->ssn);\
  171. ssl_set_own_cert(s, &((tls_server_ctx*)ctx)->cert, &((tls_server_ctx*)ctx)->key);\
  172. ssl_set_dh_param(s, ((tls_server_ctx*)ctx)->dhm_P, ((tls_server_ctx*)ctx)->dhm_G)
  173. #define TLS_setfd(s,fd) ssl_set_bio(s, net_recv, &fd, net_send, &fd)
  174. #define TLS_connect(s) ssl_handshake(s)
  175. #define TLS_accept(s) ssl_handshake(s)
  176. #define TLS_read(s,b,l) ssl_read(s,(unsigned char *)b,l)
  177. #define TLS_write(s,b,l) ssl_write(s,(unsigned char *)b,l)
  178. #define TLS_shutdown(s) ssl_close_notify(s)
  179. #define TLS_close(s) ssl_free(s); free(s)
  180. #elif defined(USE_GNUTLS)
  181. #include <gnutls/gnutls.h>
  182. typedef struct tls_ctx
  183. {
  184. gnutls_certificate_credentials_t cred;
  185. gnutls_priority_t prios;
  186. } tls_ctx;
  187. #define TLS_CTX tls_ctx *
  188. #define TLS_client(ctx,s) gnutls_init((gnutls_session_t *)(&s), GNUTLS_CLIENT); gnutls_priority_set(s, ctx->prios); gnutls_credentials_set(s, GNUTLS_CRD_CERTIFICATE, ctx->cred)
  189. #define TLS_server(ctx,s) gnutls_init((gnutls_session_t *)(&s), GNUTLS_SERVER); gnutls_priority_set_direct(s, "NORMAL", NULL); gnutls_credentials_set(s, GNUTLS_CRD_CERTIFICATE, ctx)
  190. #define TLS_setfd(s,fd) gnutls_transport_set_ptr(s, (gnutls_transport_ptr_t)(long)fd)
  191. #define TLS_connect(s) gnutls_handshake(s)
  192. #define TLS_accept(s) gnutls_handshake(s)
  193. #define TLS_read(s,b,l) gnutls_record_recv(s,b,l)
  194. #define TLS_write(s,b,l) gnutls_record_send(s,b,l)
  195. #define TLS_shutdown(s) gnutls_bye(s, GNUTLS_SHUT_RDWR)
  196. #define TLS_close(s) gnutls_deinit(s)
  197. #elif defined(USE_ONLY_MD5)
  198. #include "md5.h"
  199. #include "cencode.h"
  200. #define MD5_DIGEST_LENGTH 16
  201. #else /* USE_OPENSSL */
  202. #define TLS_CTX SSL_CTX *
  203. #define TLS_client(ctx,s) s = SSL_new(ctx)
  204. #define TLS_server(ctx,s) s = SSL_new(ctx)
  205. #define TLS_setfd(s,fd) SSL_set_fd(s,fd)
  206. #define TLS_connect(s) SSL_connect(s)
  207. #define TLS_accept(s) SSL_accept(s)
  208. #define TLS_read(s,b,l) SSL_read(s,b,l)
  209. #define TLS_write(s,b,l) SSL_write(s,b,l)
  210. #define TLS_shutdown(s) SSL_shutdown(s)
  211. #define TLS_close(s) SSL_free(s)
  212. #endif
  213. #endif