hssl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include "hssl.h"
  2. #include "hplatform.h"
  3. static hssl_ctx_t s_ssl_ctx = NULL;
  4. const char* hssl_backend() {
  5. #ifdef WITH_OPENSSL
  6. return "openssl";
  7. #elif defined(WITH_MBEDTLS)
  8. return "mbedtls";
  9. #else
  10. return "null";
  11. #endif
  12. }
  13. hssl_ctx_t hssl_ctx_instance() {
  14. if (s_ssl_ctx == NULL) {
  15. s_ssl_ctx = hssl_ctx_init(NULL);
  16. }
  17. return s_ssl_ctx;
  18. }
  19. #ifdef WITH_OPENSSL
  20. #include "openssl/ssl.h"
  21. #include "openssl/err.h"
  22. #ifdef _MSC_VER
  23. //#pragma comment(lib, "libssl.a")
  24. //#pragma comment(lib, "libcrypto.a")
  25. #endif
  26. hssl_ctx_t hssl_ctx_init(hssl_ctx_init_param_t* param) {
  27. static int s_initialized = 0;
  28. if (s_initialized == 0) {
  29. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  30. SSL_library_init();
  31. SSL_load_error_strings();
  32. #else
  33. OPENSSL_init_ssl(OPENSSL_INIT_SSL_DEFAULT, NULL);
  34. #endif
  35. s_initialized = 1;
  36. }
  37. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  38. SSL_CTX* ctx = SSL_CTX_new(SSLv23_method());
  39. #else
  40. SSL_CTX* ctx = SSL_CTX_new(TLS_method());
  41. #endif
  42. if (ctx == NULL) return NULL;
  43. int mode = SSL_VERIFY_NONE;
  44. if (param) {
  45. if (param->ca_file && *param->ca_file) {
  46. if (!SSL_CTX_load_verify_locations(ctx, param->ca_file, NULL)) {
  47. fprintf(stderr, "ssl ca_file verify failed!\n");
  48. goto error;
  49. }
  50. }
  51. if (param->crt_file && *param->crt_file) {
  52. if (!SSL_CTX_use_certificate_file(ctx, param->crt_file, SSL_FILETYPE_PEM)) {
  53. fprintf(stderr, "ssl crt_file error!\n");
  54. goto error;
  55. }
  56. }
  57. if (param->key_file && *param->key_file) {
  58. if (!SSL_CTX_use_PrivateKey_file(ctx, param->key_file, SSL_FILETYPE_PEM)) {
  59. fprintf(stderr, "ssl key_file error!\n");
  60. goto error;
  61. }
  62. if (!SSL_CTX_check_private_key(ctx)) {
  63. fprintf(stderr, "ssl key_file check failed!\n");
  64. goto error;
  65. }
  66. }
  67. if (param->verify_peer) {
  68. mode = SSL_VERIFY_PEER;
  69. }
  70. }
  71. SSL_CTX_set_verify(ctx, mode, NULL);
  72. s_ssl_ctx = ctx;
  73. return ctx;
  74. error:
  75. SSL_CTX_free(ctx);
  76. return NULL;
  77. }
  78. void hssl_ctx_cleanup(hssl_ctx_t ssl_ctx) {
  79. if (ssl_ctx) {
  80. if (ssl_ctx == s_ssl_ctx) {
  81. s_ssl_ctx = NULL;
  82. }
  83. SSL_CTX_free((SSL_CTX*)ssl_ctx);
  84. ssl_ctx = NULL;
  85. }
  86. }
  87. hssl_t hssl_new(hssl_ctx_t ssl_ctx, int fd) {
  88. SSL* ssl = SSL_new((SSL_CTX*)ssl_ctx);
  89. if (ssl == NULL) return NULL;
  90. SSL_set_fd(ssl, fd);
  91. return ssl;
  92. }
  93. void hssl_free(hssl_t ssl) {
  94. if (ssl) {
  95. SSL_free((SSL*)ssl);
  96. ssl = NULL;
  97. }
  98. }
  99. int hssl_accept(hssl_t ssl) {
  100. int ret = SSL_accept((SSL*)ssl);
  101. if (ret == 1) return 0;
  102. int err = SSL_get_error((SSL*)ssl, ret);
  103. if (err == SSL_ERROR_WANT_READ) {
  104. return HSSL_WANT_READ;
  105. }
  106. else if (err == SSL_ERROR_WANT_WRITE) {
  107. return HSSL_WANT_WRITE;
  108. }
  109. return err;
  110. }
  111. int hssl_connect(hssl_t ssl) {
  112. int ret = SSL_connect((SSL*)ssl);
  113. if (ret == 1) return 0;
  114. int err = SSL_get_error((SSL*)ssl, ret);
  115. if (err == SSL_ERROR_WANT_READ) {
  116. return HSSL_WANT_READ;
  117. }
  118. else if (err == SSL_ERROR_WANT_WRITE) {
  119. return HSSL_WANT_WRITE;
  120. }
  121. return err;
  122. }
  123. int hssl_read(hssl_t ssl, void* buf, int len) {
  124. return SSL_read((SSL*)ssl, buf, len);
  125. }
  126. int hssl_write(hssl_t ssl, const void* buf, int len) {
  127. return SSL_write((SSL*)ssl, buf, len);
  128. }
  129. int hssl_close(hssl_t ssl) {
  130. SSL_shutdown((SSL*)ssl);
  131. return 0;
  132. }
  133. #elif defined(WITH_MBEDTLS)
  134. #include "mbedtls/entropy.h"
  135. #include "mbedtls/ctr_drbg.h"
  136. #include "mbedtls/certs.h"
  137. #include "mbedtls/x509.h"
  138. #include "mbedtls/ssl.h"
  139. #include "mbedtls/net.h"
  140. #include "mbedtls/error.h"
  141. #include "mbedtls/debug.h"
  142. #if defined(MBEDTLS_SSL_CACHE_C)
  143. #include "mbedtls/ssl_cache.h"
  144. #endif
  145. #ifdef _MSC_VER
  146. //#pragma comment(lib, "libmbedtls.a")
  147. //#pragma comment(lib, "libmbedx509.a")
  148. //#pragma comment(lib, "libmbedcrypto.a")
  149. #endif
  150. struct mbedtls_ctx {
  151. mbedtls_entropy_context entropy;
  152. mbedtls_ctr_drbg_context ctr_drbg;
  153. mbedtls_ssl_config conf;
  154. mbedtls_x509_crt cert;
  155. mbedtls_pk_context pkey;
  156. #if defined(MBEDTLS_SSL_CACHE_C)
  157. mbedtls_ssl_cache_context cache;
  158. #endif
  159. };
  160. hssl_ctx_t hssl_ctx_init(hssl_ctx_init_param_t* param) {
  161. struct mbedtls_ctx* ctx = (struct mbedtls_ctx*)malloc(sizeof(struct mbedtls_ctx));
  162. if (ctx == NULL) return NULL;
  163. mbedtls_ssl_config_init(&ctx->conf);
  164. #if defined(MBEDTLS_SSL_CACHE_C)
  165. mbedtls_ssl_cache_init(&ctx->cache);
  166. #endif
  167. mbedtls_x509_crt_init(&ctx->cert);
  168. mbedtls_pk_init(&ctx->pkey);
  169. mbedtls_entropy_init(&ctx->entropy);
  170. mbedtls_ctr_drbg_init(&ctx->ctr_drbg);
  171. int mode = MBEDTLS_SSL_VERIFY_NONE;
  172. int endpoint = MBEDTLS_SSL_IS_CLIENT;
  173. bool check = false;
  174. if (param) {
  175. if (param->crt_file && *param->crt_file) {
  176. if (mbedtls_x509_crt_parse_file(&ctx->cert, param->crt_file) != 0) {
  177. fprintf(stderr, "ssl crt_file error!\n");
  178. goto error;
  179. }
  180. }
  181. if (param->key_file && *param->key_file) {
  182. if (mbedtls_pk_parse_keyfile(&ctx->pkey, param->key_file, NULL) != 0) {
  183. fprintf(stderr, "ssl key_file error!\n");
  184. goto error;
  185. }
  186. check = true;
  187. }
  188. if (param->verify_peer) {
  189. mode = MBEDTLS_SSL_VERIFY_REQUIRED;
  190. }
  191. if (param->endpoint == 0) {
  192. endpoint = MBEDTLS_SSL_IS_SERVER;
  193. }
  194. }
  195. mbedtls_ctr_drbg_seed(&ctx->ctr_drbg, mbedtls_entropy_func, &ctx->entropy, NULL, 0);
  196. if (mbedtls_ssl_config_defaults(&ctx->conf, endpoint,
  197. MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
  198. fprintf(stderr, "ssl config error!\n");
  199. goto error;
  200. }
  201. mbedtls_ssl_conf_authmode(&ctx->conf, mode);
  202. mbedtls_ssl_conf_rng(&ctx->conf, mbedtls_ctr_drbg_random, &ctx->ctr_drbg);
  203. #if defined(MBEDTLS_SSL_CACHE_C)
  204. mbedtls_ssl_conf_session_cache(&ctx->conf, &ctx->cache, mbedtls_ssl_cache_get, mbedtls_ssl_cache_set);
  205. #endif
  206. if (check) {
  207. mbedtls_ssl_conf_ca_chain(&ctx->conf, ctx->cert.next, NULL);
  208. if (mbedtls_ssl_conf_own_cert(&ctx->conf, &ctx->cert, &ctx->pkey) != 0) {
  209. fprintf(stderr, "ssl key_file check failed!\n");
  210. goto error;
  211. }
  212. }
  213. s_ssl_ctx = ctx;
  214. return ctx;
  215. error:
  216. free(ctx);
  217. return NULL;
  218. }
  219. void hssl_ctx_cleanup(hssl_ctx_t ssl_ctx) {
  220. if (!ssl_ctx) return;
  221. if (ssl_ctx == s_ssl_ctx) {
  222. s_ssl_ctx = NULL;
  223. }
  224. struct mbedtls_ctx *mctx = (struct mbedtls_ctx *)ssl_ctx;
  225. mbedtls_x509_crt_free(&mctx->cert);
  226. mbedtls_pk_free(&mctx->pkey);
  227. mbedtls_ssl_config_free(&mctx->conf);
  228. #if defined(MBEDTLS_SSL_CACHE_C)
  229. mbedtls_ssl_cache_free(&mctx->cache);
  230. #endif
  231. mbedtls_ctr_drbg_free(&mctx->ctr_drbg);
  232. mbedtls_entropy_free(&mctx->entropy);
  233. free(mctx);
  234. }
  235. static int __mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len) {
  236. int fd = (intptr_t)ctx;
  237. int n = write(fd, buf, len);
  238. if (n >= 0) return n;
  239. return ((errno == EAGAIN || errno == EINPROGRESS) ? MBEDTLS_ERR_SSL_WANT_WRITE : -1);
  240. }
  241. static int __mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len) {
  242. int fd = (intptr_t)ctx;
  243. int n = read(fd, buf, len);
  244. if (n >= 0) return n;
  245. return ((errno == EAGAIN || errno == EINPROGRESS) ? MBEDTLS_ERR_SSL_WANT_READ : -1);
  246. }
  247. hssl_t hssl_new(hssl_ctx_t ssl_ctx, int fd) {
  248. struct mbedtls_ctx* mctx = (struct mbedtls_ctx*)ssl_ctx;
  249. mbedtls_ssl_context* ssl = (mbedtls_ssl_context*)malloc(sizeof(mbedtls_ssl_context));
  250. if (ssl == NULL) return NULL;
  251. mbedtls_ssl_init(ssl);
  252. mbedtls_ssl_setup(ssl, &mctx->conf);
  253. mbedtls_ssl_set_bio(ssl, (void*)(intptr_t)fd, __mbedtls_net_send, __mbedtls_net_recv, NULL);
  254. return ssl;
  255. }
  256. void hssl_free(hssl_t ssl) {
  257. if (ssl) {
  258. mbedtls_ssl_free(ssl);
  259. ssl = NULL;
  260. }
  261. }
  262. static int hssl_handshake(hssl_t ssl) {
  263. int ret = mbedtls_ssl_handshake(ssl);
  264. if (ret != 0) {
  265. if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
  266. return HSSL_WANT_READ;
  267. }
  268. else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  269. return HSSL_WANT_WRITE;
  270. }
  271. }
  272. return ret;
  273. }
  274. int hssl_accept(hssl_t ssl) {
  275. return hssl_handshake(ssl);
  276. }
  277. int hssl_connect(hssl_t ssl) {
  278. return hssl_handshake(ssl);
  279. }
  280. int hssl_read(hssl_t ssl, void* buf, int len) {
  281. return mbedtls_ssl_read(ssl, buf, len);
  282. }
  283. int hssl_write(hssl_t ssl, const void* buf, int len) {
  284. return mbedtls_ssl_write(ssl, buf, len);
  285. }
  286. int hssl_close(hssl_t ssl) {
  287. return 0;
  288. }
  289. #else
  290. hssl_ctx_t hssl_ctx_init(hssl_ctx_init_param_t* param) {
  291. fprintf(stderr, "Please recompile WITH_SSL.\n");
  292. return NULL;
  293. }
  294. void hssl_ctx_cleanup(hssl_ctx_t ssl_ctx) {
  295. }
  296. hssl_t hssl_new(hssl_ctx_t ssl_ctx, int fd) {
  297. return (void*)(intptr_t)fd;
  298. }
  299. void hssl_free(hssl_t ssl) {
  300. }
  301. int hssl_accept(hssl_t ssl) {
  302. return 0;
  303. }
  304. int hssl_connect(hssl_t ssl) {
  305. return 0;
  306. }
  307. int hssl_read(hssl_t ssl, void* buf, int len) {
  308. int fd = (intptr_t)ssl;
  309. return read(fd, buf, len);
  310. }
  311. int hssl_write(hssl_t ssl, const void* buf, int len) {
  312. int fd = (intptr_t)ssl;
  313. return write(fd, buf, len);
  314. }
  315. int hssl_close(hssl_t ssl) {
  316. return 0;
  317. }
  318. #endif