1
0

hssl.c 9.0 KB

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