hssl.c 9.6 KB

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