ssl_pm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "ssl_pm.h"
  14. #include "ssl_port.h"
  15. #include "ssl_dbg.h"
  16. /* mbedtls include */
  17. #include "mbedtls/platform.h"
  18. #include "mbedtls/net_sockets.h"
  19. #include "mbedtls/debug.h"
  20. #include "mbedtls/entropy.h"
  21. #include "mbedtls/ctr_drbg.h"
  22. #include "mbedtls/error.h"
  23. #include "mbedtls/certs.h"
  24. #define X509_INFO_STRING_LENGTH 8192
  25. struct ssl_pm
  26. {
  27. /* local socket file description */
  28. mbedtls_net_context fd;
  29. /* remote client socket file description */
  30. mbedtls_net_context cl_fd;
  31. mbedtls_ssl_config conf;
  32. mbedtls_ctr_drbg_context ctr_drbg;
  33. mbedtls_ssl_context ssl;
  34. mbedtls_entropy_context entropy;
  35. };
  36. struct x509_pm
  37. {
  38. mbedtls_x509_crt *x509_crt;
  39. mbedtls_x509_crt *ex_crt;
  40. };
  41. struct pkey_pm
  42. {
  43. mbedtls_pk_context *pkey;
  44. mbedtls_pk_context *ex_pkey;
  45. };
  46. unsigned int max_content_len;
  47. /*********************************************************************************************/
  48. /************************************ SSL arch interface *************************************/
  49. #ifdef CONFIG_OPENSSL_LOWLEVEL_DEBUG
  50. /* mbedtls debug level */
  51. #define MBEDTLS_DEBUG_LEVEL 4
  52. /**
  53. * @brief mbedtls debug function
  54. */
  55. static void ssl_platform_debug(void *ctx, int level,
  56. const char *file, int line,
  57. const char *str)
  58. {
  59. /* Shorten 'file' from the whole file path to just the filename
  60. This is a bit wasteful because the macros are compiled in with
  61. the full _FILE_ path in each case.
  62. */
  63. char *file_sep = rindex(file, '/');
  64. if(file_sep)
  65. file = file_sep + 1;
  66. SSL_DEBUG(SSL_DEBUG_ON, "%s:%d %s", file, line, str);
  67. }
  68. #endif
  69. /**
  70. * @brief create SSL low-level object
  71. */
  72. int ssl_pm_new(SSL *ssl)
  73. {
  74. struct ssl_pm *ssl_pm;
  75. int ret;
  76. const unsigned char pers[] = "OpenSSL PM";
  77. size_t pers_len = sizeof(pers);
  78. int endpoint;
  79. int version;
  80. const SSL_METHOD *method = ssl->method;
  81. ssl_pm = ssl_mem_zalloc(sizeof(struct ssl_pm));
  82. if (!ssl_pm) {
  83. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (ssl_pm)");
  84. goto no_mem;
  85. }
  86. max_content_len = ssl->ctx->read_buffer_len;
  87. mbedtls_net_init(&ssl_pm->fd);
  88. mbedtls_net_init(&ssl_pm->cl_fd);
  89. mbedtls_ssl_config_init(&ssl_pm->conf);
  90. mbedtls_ctr_drbg_init(&ssl_pm->ctr_drbg);
  91. mbedtls_entropy_init(&ssl_pm->entropy);
  92. mbedtls_ssl_init(&ssl_pm->ssl);
  93. ret = mbedtls_ctr_drbg_seed(&ssl_pm->ctr_drbg, mbedtls_entropy_func, &ssl_pm->entropy, pers, pers_len);
  94. if (ret) {
  95. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ctr_drbg_seed() return -0x%x", -ret);
  96. goto mbedtls_err1;
  97. }
  98. if (method->endpoint) {
  99. endpoint = MBEDTLS_SSL_IS_SERVER;
  100. } else {
  101. endpoint = MBEDTLS_SSL_IS_CLIENT;
  102. }
  103. ret = mbedtls_ssl_config_defaults(&ssl_pm->conf, endpoint, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
  104. if (ret) {
  105. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_config_defaults() return -0x%x", -ret);
  106. goto mbedtls_err2;
  107. }
  108. if (TLS_ANY_VERSION != ssl->version) {
  109. if (TLS1_2_VERSION == ssl->version)
  110. version = MBEDTLS_SSL_MINOR_VERSION_3;
  111. else if (TLS1_1_VERSION == ssl->version)
  112. version = MBEDTLS_SSL_MINOR_VERSION_2;
  113. else if (TLS1_VERSION == ssl->version)
  114. version = MBEDTLS_SSL_MINOR_VERSION_1;
  115. else
  116. version = MBEDTLS_SSL_MINOR_VERSION_0;
  117. mbedtls_ssl_conf_max_version(&ssl_pm->conf, MBEDTLS_SSL_MAJOR_VERSION_3, version);
  118. mbedtls_ssl_conf_min_version(&ssl_pm->conf, MBEDTLS_SSL_MAJOR_VERSION_3, version);
  119. } else {
  120. mbedtls_ssl_conf_max_version(&ssl_pm->conf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3);
  121. mbedtls_ssl_conf_min_version(&ssl_pm->conf, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_0);
  122. }
  123. if (ssl->ctx->ssl_alpn.alpn_status == ALPN_ENABLE) {
  124. #ifdef MBEDTLS_SSL_ALPN
  125. mbedtls_ssl_conf_alpn_protocols( &ssl_pm->conf, ssl->ctx->ssl_alpn.alpn_list );
  126. #else
  127. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "CONFIG_MBEDTLS_SSL_ALPN must be enabled to use ALPN", -1);
  128. #endif // MBEDTLS_SSL_ALPN
  129. }
  130. mbedtls_ssl_conf_rng(&ssl_pm->conf, mbedtls_ctr_drbg_random, &ssl_pm->ctr_drbg);
  131. #ifdef CONFIG_OPENSSL_LOWLEVEL_DEBUG
  132. mbedtls_debug_set_threshold(MBEDTLS_DEBUG_LEVEL);
  133. mbedtls_ssl_conf_dbg(&ssl_pm->conf, ssl_platform_debug, NULL);
  134. #else
  135. mbedtls_ssl_conf_dbg(&ssl_pm->conf, NULL, NULL);
  136. #endif
  137. ret = mbedtls_ssl_setup(&ssl_pm->ssl, &ssl_pm->conf);
  138. if (ret) {
  139. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_setup() return -0x%x", -ret);
  140. goto mbedtls_err2;
  141. }
  142. mbedtls_ssl_set_bio(&ssl_pm->ssl, &ssl_pm->fd, mbedtls_net_send, mbedtls_net_recv, NULL);
  143. ssl->ssl_pm = ssl_pm;
  144. return 0;
  145. mbedtls_err2:
  146. mbedtls_ssl_config_free(&ssl_pm->conf);
  147. mbedtls_ctr_drbg_free(&ssl_pm->ctr_drbg);
  148. mbedtls_err1:
  149. mbedtls_entropy_free(&ssl_pm->entropy);
  150. ssl_mem_free(ssl_pm);
  151. no_mem:
  152. return -1;
  153. }
  154. /**
  155. * @brief free SSL low-level object
  156. */
  157. void ssl_pm_free(SSL *ssl)
  158. {
  159. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  160. mbedtls_ctr_drbg_free(&ssl_pm->ctr_drbg);
  161. mbedtls_entropy_free(&ssl_pm->entropy);
  162. mbedtls_ssl_config_free(&ssl_pm->conf);
  163. mbedtls_ssl_free(&ssl_pm->ssl);
  164. ssl_mem_free(ssl_pm);
  165. ssl->ssl_pm = NULL;
  166. }
  167. /**
  168. * @brief reload SSL low-level certification object
  169. */
  170. static int ssl_pm_reload_crt(SSL *ssl)
  171. {
  172. int ret;
  173. int mode = MBEDTLS_SSL_VERIFY_UNSET;
  174. struct ssl_pm *ssl_pm = ssl->ssl_pm;
  175. struct x509_pm *ca_pm = (struct x509_pm *)ssl->client_CA->x509_pm;
  176. struct pkey_pm *pkey_pm = (struct pkey_pm *)ssl->cert->pkey->pkey_pm;
  177. struct x509_pm *crt_pm = (struct x509_pm *)ssl->cert->x509->x509_pm;
  178. /* OpenSSL verification modes outline (see `man SSL_set_verify` for more details)
  179. *
  180. * | openssl mode | Server | Client |
  181. * | SSL_VERIFY_NONE | will not send a client certificate request | server certificate which will be checked |
  182. * handshake will be continued regardless |
  183. * | SSL_VERIFY_PEER | depends on SSL_VERIFY_FAIL_IF_NO_PEER_CERT | handshake is terminated if verify fails |
  184. * (unless anonymous ciphers--not supported |
  185. * | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | handshake is terminated if | ignored |
  186. * client cert verify fails | |
  187. */
  188. if (ssl->method->endpoint == MBEDTLS_SSL_IS_SERVER) {
  189. if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  190. mode = MBEDTLS_SSL_VERIFY_REQUIRED;
  191. else if (ssl->verify_mode & SSL_VERIFY_PEER)
  192. mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
  193. else if (ssl->verify_mode == SSL_VERIFY_NONE)
  194. mode = MBEDTLS_SSL_VERIFY_NONE;
  195. } else if (ssl->method->endpoint == MBEDTLS_SSL_IS_CLIENT) {
  196. if (ssl->verify_mode & SSL_VERIFY_PEER)
  197. mode = MBEDTLS_SSL_VERIFY_REQUIRED;
  198. else if (ssl->verify_mode == SSL_VERIFY_NONE)
  199. mode = MBEDTLS_SSL_VERIFY_NONE;
  200. }
  201. mbedtls_ssl_conf_authmode(&ssl_pm->conf, mode);
  202. if (ca_pm->x509_crt) {
  203. mbedtls_ssl_conf_ca_chain(&ssl_pm->conf, ca_pm->x509_crt, NULL);
  204. } else if (ca_pm->ex_crt) {
  205. mbedtls_ssl_conf_ca_chain(&ssl_pm->conf, ca_pm->ex_crt, NULL);
  206. }
  207. if (crt_pm->x509_crt && pkey_pm->pkey) {
  208. ret = mbedtls_ssl_conf_own_cert(&ssl_pm->conf, crt_pm->x509_crt, pkey_pm->pkey);
  209. } else if (crt_pm->ex_crt && pkey_pm->ex_pkey) {
  210. ret = mbedtls_ssl_conf_own_cert(&ssl_pm->conf, crt_pm->ex_crt, pkey_pm->ex_pkey);
  211. } else {
  212. ret = 0;
  213. }
  214. if (ret) {
  215. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_conf_own_cert() return -0x%x", -ret);
  216. ret = -1;
  217. }
  218. return ret;
  219. }
  220. /*
  221. * Perform the mbedtls SSL handshake instead of mbedtls_ssl_handshake.
  222. * We can add debug here.
  223. */
  224. static int mbedtls_handshake( mbedtls_ssl_context *ssl )
  225. {
  226. int ret = 0;
  227. while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
  228. ret = mbedtls_ssl_handshake_step(ssl);
  229. SSL_DEBUG(SSL_PLATFORM_DEBUG_LEVEL, "ssl ret %d state %d", ret, ssl->state);
  230. if (ret != 0)
  231. break;
  232. }
  233. return ret;
  234. }
  235. int ssl_pm_handshake(SSL *ssl)
  236. {
  237. int ret;
  238. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  239. ret = ssl_pm_reload_crt(ssl);
  240. if (ret)
  241. return 0;
  242. ssl_speed_up_enter();
  243. while((ret = mbedtls_handshake(&ssl_pm->ssl)) != 0) {
  244. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  245. break;
  246. }
  247. }
  248. ssl_speed_up_exit();
  249. if (ret) {
  250. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_handshake() return -0x%x", -ret);
  251. ret = 0;
  252. } else {
  253. struct x509_pm *x509_pm = (struct x509_pm *)ssl->session->peer->x509_pm;
  254. x509_pm->ex_crt = (mbedtls_x509_crt *)mbedtls_ssl_get_peer_cert(&ssl_pm->ssl);
  255. ret = 1;
  256. }
  257. return ret;
  258. }
  259. int ssl_pm_shutdown(SSL *ssl)
  260. {
  261. int ret;
  262. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  263. ret = mbedtls_ssl_close_notify(&ssl_pm->ssl);
  264. if (ret) {
  265. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_close_notify() return -0x%x", -ret);
  266. ret = -1;
  267. } else {
  268. struct x509_pm *x509_pm = (struct x509_pm *)ssl->session->peer->x509_pm;
  269. x509_pm->ex_crt = NULL;
  270. }
  271. return ret;
  272. }
  273. int ssl_pm_clear(SSL *ssl)
  274. {
  275. return ssl_pm_shutdown(ssl);
  276. }
  277. int ssl_pm_read(SSL *ssl, void *buffer, int len)
  278. {
  279. int ret;
  280. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  281. ret = mbedtls_ssl_read(&ssl_pm->ssl, buffer, len);
  282. if (ret < 0) {
  283. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_read() return -0x%x", -ret);
  284. ret = -1;
  285. }
  286. return ret;
  287. }
  288. int ssl_pm_send(SSL *ssl, const void *buffer, int len)
  289. {
  290. int ret;
  291. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  292. ret = mbedtls_ssl_write(&ssl_pm->ssl, buffer, len);
  293. if (ret < 0) {
  294. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_write() return -0x%x", -ret);
  295. ret = -1;
  296. }
  297. return ret;
  298. }
  299. int ssl_pm_pending(const SSL *ssl)
  300. {
  301. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  302. return mbedtls_ssl_get_bytes_avail(&ssl_pm->ssl);
  303. }
  304. void ssl_pm_set_fd(SSL *ssl, int fd, int mode)
  305. {
  306. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  307. ssl_pm->fd.fd = fd;
  308. }
  309. void ssl_pm_set_hostname(SSL *ssl, const char *hostname)
  310. {
  311. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  312. mbedtls_ssl_set_hostname(&ssl_pm->ssl, hostname);
  313. }
  314. int ssl_pm_get_fd(const SSL *ssl, int mode)
  315. {
  316. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  317. return ssl_pm->fd.fd;
  318. }
  319. OSSL_HANDSHAKE_STATE ssl_pm_get_state(const SSL *ssl)
  320. {
  321. OSSL_HANDSHAKE_STATE state;
  322. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  323. switch (ssl_pm->ssl.state)
  324. {
  325. case MBEDTLS_SSL_CLIENT_HELLO:
  326. state = TLS_ST_CW_CLNT_HELLO;
  327. break;
  328. case MBEDTLS_SSL_SERVER_HELLO:
  329. state = TLS_ST_SW_SRVR_HELLO;
  330. break;
  331. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  332. state = TLS_ST_SW_CERT;
  333. break;
  334. case MBEDTLS_SSL_SERVER_HELLO_DONE:
  335. state = TLS_ST_SW_SRVR_DONE;
  336. break;
  337. case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
  338. state = TLS_ST_CW_KEY_EXCH;
  339. break;
  340. case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
  341. state = TLS_ST_CW_CHANGE;
  342. break;
  343. case MBEDTLS_SSL_CLIENT_FINISHED:
  344. state = TLS_ST_CW_FINISHED;
  345. break;
  346. case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
  347. state = TLS_ST_SW_CHANGE;
  348. break;
  349. case MBEDTLS_SSL_SERVER_FINISHED:
  350. state = TLS_ST_SW_FINISHED;
  351. break;
  352. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  353. state = TLS_ST_CW_CERT;
  354. break;
  355. case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
  356. state = TLS_ST_SR_KEY_EXCH;
  357. break;
  358. case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
  359. state = TLS_ST_SW_SESSION_TICKET;
  360. break;
  361. case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
  362. state = TLS_ST_SW_CERT_REQ;
  363. break;
  364. case MBEDTLS_SSL_HANDSHAKE_OVER:
  365. state = TLS_ST_OK;
  366. break;
  367. default :
  368. state = TLS_ST_BEFORE;
  369. break;
  370. }
  371. return state;
  372. }
  373. int x509_pm_show_info(X509 *x)
  374. {
  375. int ret;
  376. char *buf;
  377. mbedtls_x509_crt *x509_crt;
  378. struct x509_pm *x509_pm = x->x509_pm;
  379. if (x509_pm->x509_crt)
  380. x509_crt = x509_pm->x509_crt;
  381. else if (x509_pm->ex_crt)
  382. x509_crt = x509_pm->ex_crt;
  383. else
  384. x509_crt = NULL;
  385. if (!x509_crt)
  386. return -1;
  387. buf = ssl_mem_malloc(X509_INFO_STRING_LENGTH);
  388. if (!buf) {
  389. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (buf)");
  390. goto no_mem;
  391. }
  392. ret = mbedtls_x509_crt_info(buf, X509_INFO_STRING_LENGTH - 1, "", x509_crt);
  393. if (ret <= 0) {
  394. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_x509_crt_info() return -0x%x", -ret);
  395. goto mbedtls_err1;
  396. }
  397. buf[ret] = 0;
  398. ssl_mem_free(buf);
  399. SSL_DEBUG(SSL_DEBUG_ON, "%s", buf);
  400. return 0;
  401. mbedtls_err1:
  402. ssl_mem_free(buf);
  403. no_mem:
  404. return -1;
  405. }
  406. int x509_pm_new(X509 *x, X509 *m_x)
  407. {
  408. struct x509_pm *x509_pm;
  409. x509_pm = ssl_mem_zalloc(sizeof(struct x509_pm));
  410. if (!x509_pm) {
  411. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (x509_pm)");
  412. goto failed1;
  413. }
  414. x->x509_pm = x509_pm;
  415. if (m_x) {
  416. struct x509_pm *m_x509_pm = (struct x509_pm *)m_x->x509_pm;
  417. x509_pm->ex_crt = m_x509_pm->x509_crt;
  418. }
  419. return 0;
  420. failed1:
  421. return -1;
  422. }
  423. void x509_pm_free(X509 *x)
  424. {
  425. struct x509_pm *x509_pm = (struct x509_pm *)x->x509_pm;
  426. if (x509_pm->x509_crt) {
  427. mbedtls_x509_crt_free(x509_pm->x509_crt);
  428. ssl_mem_free(x509_pm->x509_crt);
  429. x509_pm->x509_crt = NULL;
  430. }
  431. ssl_mem_free(x->x509_pm);
  432. x->x509_pm = NULL;
  433. }
  434. int x509_pm_load(X509 *x, const unsigned char *buffer, int len)
  435. {
  436. int ret;
  437. unsigned char *load_buf;
  438. struct x509_pm *x509_pm = (struct x509_pm *)x->x509_pm;
  439. if (x509_pm->x509_crt)
  440. mbedtls_x509_crt_free(x509_pm->x509_crt);
  441. if (!x509_pm->x509_crt) {
  442. x509_pm->x509_crt = ssl_mem_malloc(sizeof(mbedtls_x509_crt));
  443. if (!x509_pm->x509_crt) {
  444. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (x509_pm->x509_crt)");
  445. goto no_mem;
  446. }
  447. }
  448. load_buf = ssl_mem_malloc(len + 1);
  449. if (!load_buf) {
  450. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (load_buf)");
  451. goto failed;
  452. }
  453. ssl_memcpy(load_buf, buffer, len);
  454. load_buf[len] = '\0';
  455. mbedtls_x509_crt_init(x509_pm->x509_crt);
  456. ret = mbedtls_x509_crt_parse(x509_pm->x509_crt, load_buf, len + 1);
  457. ssl_mem_free(load_buf);
  458. if (ret) {
  459. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_x509_crt_parse return -0x%x", -ret);
  460. goto failed;
  461. }
  462. return 0;
  463. failed:
  464. mbedtls_x509_crt_free(x509_pm->x509_crt);
  465. ssl_mem_free(x509_pm->x509_crt);
  466. x509_pm->x509_crt = NULL;
  467. no_mem:
  468. return -1;
  469. }
  470. int pkey_pm_new(EVP_PKEY *pk, EVP_PKEY *m_pkey)
  471. {
  472. struct pkey_pm *pkey_pm;
  473. pkey_pm = ssl_mem_zalloc(sizeof(struct pkey_pm));
  474. if (!pkey_pm)
  475. return -1;
  476. pk->pkey_pm = pkey_pm;
  477. if (m_pkey) {
  478. struct pkey_pm *m_pkey_pm = (struct pkey_pm *)m_pkey->pkey_pm;
  479. pkey_pm->ex_pkey = m_pkey_pm->pkey;
  480. }
  481. return 0;
  482. }
  483. void pkey_pm_free(EVP_PKEY *pk)
  484. {
  485. struct pkey_pm *pkey_pm = (struct pkey_pm *)pk->pkey_pm;
  486. if (pkey_pm->pkey) {
  487. mbedtls_pk_free(pkey_pm->pkey);
  488. ssl_mem_free(pkey_pm->pkey);
  489. pkey_pm->pkey = NULL;
  490. }
  491. ssl_mem_free(pk->pkey_pm);
  492. pk->pkey_pm = NULL;
  493. }
  494. int pkey_pm_load(EVP_PKEY *pk, const unsigned char *buffer, int len)
  495. {
  496. int ret;
  497. unsigned char *load_buf;
  498. struct pkey_pm *pkey_pm = (struct pkey_pm *)pk->pkey_pm;
  499. if (pkey_pm->pkey)
  500. mbedtls_pk_free(pkey_pm->pkey);
  501. if (!pkey_pm->pkey) {
  502. pkey_pm->pkey = ssl_mem_malloc(sizeof(mbedtls_pk_context));
  503. if (!pkey_pm->pkey) {
  504. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (pkey_pm->pkey)");
  505. goto no_mem;
  506. }
  507. }
  508. load_buf = ssl_mem_malloc(len + 1);
  509. if (!load_buf) {
  510. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (load_buf)");
  511. goto failed;
  512. }
  513. ssl_memcpy(load_buf, buffer, len);
  514. load_buf[len] = '\0';
  515. mbedtls_pk_init(pkey_pm->pkey);
  516. ret = mbedtls_pk_parse_key(pkey_pm->pkey, load_buf, len + 1, NULL, 0);
  517. ssl_mem_free(load_buf);
  518. if (ret) {
  519. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_pk_parse_key return -0x%x", -ret);
  520. goto failed;
  521. }
  522. return 0;
  523. failed:
  524. mbedtls_pk_free(pkey_pm->pkey);
  525. ssl_mem_free(pkey_pm->pkey);
  526. pkey_pm->pkey = NULL;
  527. no_mem:
  528. return -1;
  529. }
  530. void ssl_pm_set_bufflen(SSL *ssl, int len)
  531. {
  532. max_content_len = len;
  533. }
  534. long ssl_pm_get_verify_result(const SSL *ssl)
  535. {
  536. uint32_t ret;
  537. long verify_result;
  538. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  539. ret = mbedtls_ssl_get_verify_result(&ssl_pm->ssl);
  540. if (ret) {
  541. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_get_verify_result() return 0x%x", ret);
  542. verify_result = X509_V_ERR_UNSPECIFIED;
  543. } else
  544. verify_result = X509_V_OK;
  545. return verify_result;
  546. }
  547. /**
  548. * @brief set expected hostname on peer cert CN
  549. */
  550. int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
  551. const char *name, size_t namelen)
  552. {
  553. SSL *ssl = (SSL *)((char *)param - offsetof(SSL, param));
  554. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  555. char *name_cstr = NULL;
  556. if (namelen) {
  557. name_cstr = malloc(namelen + 1);
  558. if (!name_cstr) {
  559. return 0;
  560. }
  561. memcpy(name_cstr, name, namelen);
  562. name_cstr[namelen] = '\0';
  563. name = name_cstr;
  564. }
  565. mbedtls_ssl_set_hostname(&ssl_pm->ssl, name);
  566. if (namelen) {
  567. free(name_cstr);
  568. }
  569. return 1;
  570. }