hssl.c 9.1 KB

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