ssl_pm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. mbedtls_ssl_conf_alpn_protocols( &ssl_pm->conf, ssl->ctx->ssl_alpn.alpn_list );
  125. }
  126. mbedtls_ssl_conf_rng(&ssl_pm->conf, mbedtls_ctr_drbg_random, &ssl_pm->ctr_drbg);
  127. #ifdef CONFIG_OPENSSL_LOWLEVEL_DEBUG
  128. mbedtls_debug_set_threshold(MBEDTLS_DEBUG_LEVEL);
  129. mbedtls_ssl_conf_dbg(&ssl_pm->conf, ssl_platform_debug, NULL);
  130. #else
  131. mbedtls_ssl_conf_dbg(&ssl_pm->conf, NULL, NULL);
  132. #endif
  133. ret = mbedtls_ssl_setup(&ssl_pm->ssl, &ssl_pm->conf);
  134. if (ret) {
  135. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_setup() return -0x%x", -ret);
  136. goto mbedtls_err2;
  137. }
  138. mbedtls_ssl_set_bio(&ssl_pm->ssl, &ssl_pm->fd, mbedtls_net_send, mbedtls_net_recv, NULL);
  139. ssl->ssl_pm = ssl_pm;
  140. return 0;
  141. mbedtls_err2:
  142. mbedtls_ssl_config_free(&ssl_pm->conf);
  143. mbedtls_ctr_drbg_free(&ssl_pm->ctr_drbg);
  144. mbedtls_err1:
  145. mbedtls_entropy_free(&ssl_pm->entropy);
  146. ssl_mem_free(ssl_pm);
  147. no_mem:
  148. return -1;
  149. }
  150. /**
  151. * @brief free SSL low-level object
  152. */
  153. void ssl_pm_free(SSL *ssl)
  154. {
  155. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  156. mbedtls_ctr_drbg_free(&ssl_pm->ctr_drbg);
  157. mbedtls_entropy_free(&ssl_pm->entropy);
  158. mbedtls_ssl_config_free(&ssl_pm->conf);
  159. mbedtls_ssl_free(&ssl_pm->ssl);
  160. ssl_mem_free(ssl_pm);
  161. ssl->ssl_pm = NULL;
  162. }
  163. /**
  164. * @brief reload SSL low-level certification object
  165. */
  166. static int ssl_pm_reload_crt(SSL *ssl)
  167. {
  168. int ret;
  169. int mode;
  170. struct ssl_pm *ssl_pm = ssl->ssl_pm;
  171. struct x509_pm *ca_pm = (struct x509_pm *)ssl->client_CA->x509_pm;
  172. struct pkey_pm *pkey_pm = (struct pkey_pm *)ssl->cert->pkey->pkey_pm;
  173. struct x509_pm *crt_pm = (struct x509_pm *)ssl->cert->x509->x509_pm;
  174. if (ssl->verify_mode == SSL_VERIFY_PEER)
  175. mode = MBEDTLS_SSL_VERIFY_REQUIRED;
  176. else if (ssl->verify_mode == SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  177. mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
  178. else if (ssl->verify_mode == SSL_VERIFY_CLIENT_ONCE)
  179. mode = MBEDTLS_SSL_VERIFY_UNSET;
  180. else
  181. mode = MBEDTLS_SSL_VERIFY_NONE;
  182. mbedtls_ssl_conf_authmode(&ssl_pm->conf, mode);
  183. if (ca_pm->x509_crt) {
  184. mbedtls_ssl_conf_ca_chain(&ssl_pm->conf, ca_pm->x509_crt, NULL);
  185. } else if (ca_pm->ex_crt) {
  186. mbedtls_ssl_conf_ca_chain(&ssl_pm->conf, ca_pm->ex_crt, NULL);
  187. }
  188. if (crt_pm->x509_crt && pkey_pm->pkey) {
  189. ret = mbedtls_ssl_conf_own_cert(&ssl_pm->conf, crt_pm->x509_crt, pkey_pm->pkey);
  190. } else if (crt_pm->ex_crt && pkey_pm->ex_pkey) {
  191. ret = mbedtls_ssl_conf_own_cert(&ssl_pm->conf, crt_pm->ex_crt, pkey_pm->ex_pkey);
  192. } else {
  193. ret = 0;
  194. }
  195. if (ret) {
  196. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_conf_own_cert() return -0x%x", -ret);
  197. ret = -1;
  198. }
  199. return ret;
  200. }
  201. /*
  202. * Perform the mbedtls SSL handshake instead of mbedtls_ssl_handshake.
  203. * We can add debug here.
  204. */
  205. static int mbedtls_handshake( mbedtls_ssl_context *ssl )
  206. {
  207. int ret = 0;
  208. while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
  209. ret = mbedtls_ssl_handshake_step(ssl);
  210. SSL_DEBUG(SSL_PLATFORM_DEBUG_LEVEL, "ssl ret %d state %d", ret, ssl->state);
  211. if (ret != 0)
  212. break;
  213. }
  214. return ret;
  215. }
  216. int ssl_pm_handshake(SSL *ssl)
  217. {
  218. int ret;
  219. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  220. ret = ssl_pm_reload_crt(ssl);
  221. if (ret)
  222. return 0;
  223. ssl_speed_up_enter();
  224. while((ret = mbedtls_handshake(&ssl_pm->ssl)) != 0) {
  225. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  226. break;
  227. }
  228. }
  229. ssl_speed_up_exit();
  230. if (ret) {
  231. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_handshake() return -0x%x", -ret);
  232. ret = 0;
  233. } else {
  234. struct x509_pm *x509_pm = (struct x509_pm *)ssl->session->peer->x509_pm;
  235. x509_pm->ex_crt = (mbedtls_x509_crt *)mbedtls_ssl_get_peer_cert(&ssl_pm->ssl);
  236. ret = 1;
  237. }
  238. return ret;
  239. }
  240. int ssl_pm_shutdown(SSL *ssl)
  241. {
  242. int ret;
  243. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  244. ret = mbedtls_ssl_close_notify(&ssl_pm->ssl);
  245. if (ret) {
  246. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_close_notify() return -0x%x", -ret);
  247. ret = -1;
  248. } else {
  249. struct x509_pm *x509_pm = (struct x509_pm *)ssl->session->peer->x509_pm;
  250. x509_pm->ex_crt = NULL;
  251. }
  252. return ret;
  253. }
  254. int ssl_pm_clear(SSL *ssl)
  255. {
  256. return ssl_pm_shutdown(ssl);
  257. }
  258. int ssl_pm_read(SSL *ssl, void *buffer, int len)
  259. {
  260. int ret;
  261. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  262. ret = mbedtls_ssl_read(&ssl_pm->ssl, buffer, len);
  263. if (ret < 0) {
  264. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_read() return -0x%x", -ret);
  265. ret = -1;
  266. }
  267. return ret;
  268. }
  269. int ssl_pm_send(SSL *ssl, const void *buffer, int len)
  270. {
  271. int ret;
  272. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  273. ret = mbedtls_ssl_write(&ssl_pm->ssl, buffer, len);
  274. if (ret < 0) {
  275. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_write() return -0x%x", -ret);
  276. ret = -1;
  277. }
  278. return ret;
  279. }
  280. int ssl_pm_pending(const SSL *ssl)
  281. {
  282. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  283. return mbedtls_ssl_get_bytes_avail(&ssl_pm->ssl);
  284. }
  285. void ssl_pm_set_fd(SSL *ssl, int fd, int mode)
  286. {
  287. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  288. ssl_pm->fd.fd = fd;
  289. }
  290. void ssl_pm_set_hostname(SSL *ssl, const char *hostname)
  291. {
  292. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  293. mbedtls_ssl_set_hostname(&ssl_pm->ssl, hostname);
  294. }
  295. int ssl_pm_get_fd(const SSL *ssl, int mode)
  296. {
  297. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  298. return ssl_pm->fd.fd;
  299. }
  300. OSSL_HANDSHAKE_STATE ssl_pm_get_state(const SSL *ssl)
  301. {
  302. OSSL_HANDSHAKE_STATE state;
  303. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  304. switch (ssl_pm->ssl.state)
  305. {
  306. case MBEDTLS_SSL_CLIENT_HELLO:
  307. state = TLS_ST_CW_CLNT_HELLO;
  308. break;
  309. case MBEDTLS_SSL_SERVER_HELLO:
  310. state = TLS_ST_SW_SRVR_HELLO;
  311. break;
  312. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  313. state = TLS_ST_SW_CERT;
  314. break;
  315. case MBEDTLS_SSL_SERVER_HELLO_DONE:
  316. state = TLS_ST_SW_SRVR_DONE;
  317. break;
  318. case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
  319. state = TLS_ST_CW_KEY_EXCH;
  320. break;
  321. case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
  322. state = TLS_ST_CW_CHANGE;
  323. break;
  324. case MBEDTLS_SSL_CLIENT_FINISHED:
  325. state = TLS_ST_CW_FINISHED;
  326. break;
  327. case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
  328. state = TLS_ST_SW_CHANGE;
  329. break;
  330. case MBEDTLS_SSL_SERVER_FINISHED:
  331. state = TLS_ST_SW_FINISHED;
  332. break;
  333. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  334. state = TLS_ST_CW_CERT;
  335. break;
  336. case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
  337. state = TLS_ST_SR_KEY_EXCH;
  338. break;
  339. case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
  340. state = TLS_ST_SW_SESSION_TICKET;
  341. break;
  342. case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
  343. state = TLS_ST_SW_CERT_REQ;
  344. break;
  345. case MBEDTLS_SSL_HANDSHAKE_OVER:
  346. state = TLS_ST_OK;
  347. break;
  348. default :
  349. state = TLS_ST_BEFORE;
  350. break;
  351. }
  352. return state;
  353. }
  354. int x509_pm_show_info(X509 *x)
  355. {
  356. int ret;
  357. char *buf;
  358. mbedtls_x509_crt *x509_crt;
  359. struct x509_pm *x509_pm = x->x509_pm;
  360. if (x509_pm->x509_crt)
  361. x509_crt = x509_pm->x509_crt;
  362. else if (x509_pm->ex_crt)
  363. x509_crt = x509_pm->ex_crt;
  364. else
  365. x509_crt = NULL;
  366. if (!x509_crt)
  367. return -1;
  368. buf = ssl_mem_malloc(X509_INFO_STRING_LENGTH);
  369. if (!buf) {
  370. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (buf)");
  371. goto no_mem;
  372. }
  373. ret = mbedtls_x509_crt_info(buf, X509_INFO_STRING_LENGTH - 1, "", x509_crt);
  374. if (ret <= 0) {
  375. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_x509_crt_info() return -0x%x", -ret);
  376. goto mbedtls_err1;
  377. }
  378. buf[ret] = 0;
  379. ssl_mem_free(buf);
  380. SSL_DEBUG(SSL_DEBUG_ON, "%s", buf);
  381. return 0;
  382. mbedtls_err1:
  383. ssl_mem_free(buf);
  384. no_mem:
  385. return -1;
  386. }
  387. int x509_pm_new(X509 *x, X509 *m_x)
  388. {
  389. struct x509_pm *x509_pm;
  390. x509_pm = ssl_mem_zalloc(sizeof(struct x509_pm));
  391. if (!x509_pm) {
  392. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (x509_pm)");
  393. goto failed1;
  394. }
  395. x->x509_pm = x509_pm;
  396. if (m_x) {
  397. struct x509_pm *m_x509_pm = (struct x509_pm *)m_x->x509_pm;
  398. x509_pm->ex_crt = m_x509_pm->x509_crt;
  399. }
  400. return 0;
  401. failed1:
  402. return -1;
  403. }
  404. void x509_pm_free(X509 *x)
  405. {
  406. struct x509_pm *x509_pm = (struct x509_pm *)x->x509_pm;
  407. if (x509_pm->x509_crt) {
  408. mbedtls_x509_crt_free(x509_pm->x509_crt);
  409. ssl_mem_free(x509_pm->x509_crt);
  410. x509_pm->x509_crt = NULL;
  411. }
  412. ssl_mem_free(x->x509_pm);
  413. x->x509_pm = NULL;
  414. }
  415. int x509_pm_load(X509 *x, const unsigned char *buffer, int len)
  416. {
  417. int ret;
  418. unsigned char *load_buf;
  419. struct x509_pm *x509_pm = (struct x509_pm *)x->x509_pm;
  420. if (x509_pm->x509_crt)
  421. mbedtls_x509_crt_free(x509_pm->x509_crt);
  422. if (!x509_pm->x509_crt) {
  423. x509_pm->x509_crt = ssl_mem_malloc(sizeof(mbedtls_x509_crt));
  424. if (!x509_pm->x509_crt) {
  425. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (x509_pm->x509_crt)");
  426. goto no_mem;
  427. }
  428. }
  429. load_buf = ssl_mem_malloc(len + 1);
  430. if (!load_buf) {
  431. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (load_buf)");
  432. goto failed;
  433. }
  434. ssl_memcpy(load_buf, buffer, len);
  435. load_buf[len] = '\0';
  436. mbedtls_x509_crt_init(x509_pm->x509_crt);
  437. ret = mbedtls_x509_crt_parse(x509_pm->x509_crt, load_buf, len + 1);
  438. ssl_mem_free(load_buf);
  439. if (ret) {
  440. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_x509_crt_parse return -0x%x", -ret);
  441. goto failed;
  442. }
  443. return 0;
  444. failed:
  445. mbedtls_x509_crt_free(x509_pm->x509_crt);
  446. ssl_mem_free(x509_pm->x509_crt);
  447. x509_pm->x509_crt = NULL;
  448. no_mem:
  449. return -1;
  450. }
  451. int pkey_pm_new(EVP_PKEY *pk, EVP_PKEY *m_pkey)
  452. {
  453. struct pkey_pm *pkey_pm;
  454. pkey_pm = ssl_mem_zalloc(sizeof(struct pkey_pm));
  455. if (!pkey_pm)
  456. return -1;
  457. pk->pkey_pm = pkey_pm;
  458. if (m_pkey) {
  459. struct pkey_pm *m_pkey_pm = (struct pkey_pm *)m_pkey->pkey_pm;
  460. pkey_pm->ex_pkey = m_pkey_pm->pkey;
  461. }
  462. return 0;
  463. }
  464. void pkey_pm_free(EVP_PKEY *pk)
  465. {
  466. struct pkey_pm *pkey_pm = (struct pkey_pm *)pk->pkey_pm;
  467. if (pkey_pm->pkey) {
  468. mbedtls_pk_free(pkey_pm->pkey);
  469. ssl_mem_free(pkey_pm->pkey);
  470. pkey_pm->pkey = NULL;
  471. }
  472. ssl_mem_free(pk->pkey_pm);
  473. pk->pkey_pm = NULL;
  474. }
  475. int pkey_pm_load(EVP_PKEY *pk, const unsigned char *buffer, int len)
  476. {
  477. int ret;
  478. unsigned char *load_buf;
  479. struct pkey_pm *pkey_pm = (struct pkey_pm *)pk->pkey_pm;
  480. if (pkey_pm->pkey)
  481. mbedtls_pk_free(pkey_pm->pkey);
  482. if (!pkey_pm->pkey) {
  483. pkey_pm->pkey = ssl_mem_malloc(sizeof(mbedtls_pk_context));
  484. if (!pkey_pm->pkey) {
  485. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (pkey_pm->pkey)");
  486. goto no_mem;
  487. }
  488. }
  489. load_buf = ssl_mem_malloc(len + 1);
  490. if (!load_buf) {
  491. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "no enough memory > (load_buf)");
  492. goto failed;
  493. }
  494. ssl_memcpy(load_buf, buffer, len);
  495. load_buf[len] = '\0';
  496. mbedtls_pk_init(pkey_pm->pkey);
  497. ret = mbedtls_pk_parse_key(pkey_pm->pkey, load_buf, len + 1, NULL, 0);
  498. ssl_mem_free(load_buf);
  499. if (ret) {
  500. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_pk_parse_key return -0x%x", -ret);
  501. goto failed;
  502. }
  503. return 0;
  504. failed:
  505. mbedtls_pk_free(pkey_pm->pkey);
  506. ssl_mem_free(pkey_pm->pkey);
  507. pkey_pm->pkey = NULL;
  508. no_mem:
  509. return -1;
  510. }
  511. void ssl_pm_set_bufflen(SSL *ssl, int len)
  512. {
  513. max_content_len = len;
  514. }
  515. long ssl_pm_get_verify_result(const SSL *ssl)
  516. {
  517. uint32_t ret;
  518. long verify_result;
  519. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  520. ret = mbedtls_ssl_get_verify_result(&ssl_pm->ssl);
  521. if (ret) {
  522. SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL, "mbedtls_ssl_get_verify_result() return 0x%x", ret);
  523. verify_result = X509_V_ERR_UNSPECIFIED;
  524. } else
  525. verify_result = X509_V_OK;
  526. return verify_result;
  527. }
  528. /**
  529. * @brief set expected hostname on peer cert CN
  530. */
  531. int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
  532. const char *name, size_t namelen)
  533. {
  534. SSL *ssl = (SSL *)((char *)param - offsetof(SSL, param));
  535. struct ssl_pm *ssl_pm = (struct ssl_pm *)ssl->ssl_pm;
  536. char *name_cstr = NULL;
  537. if (namelen) {
  538. name_cstr = malloc(namelen + 1);
  539. if (!name_cstr) {
  540. return 0;
  541. }
  542. memcpy(name_cstr, name, namelen);
  543. name_cstr[namelen] = '\0';
  544. name = name_cstr;
  545. }
  546. mbedtls_ssl_set_hostname(&ssl_pm->ssl, name);
  547. if (namelen) {
  548. free(name_cstr);
  549. }
  550. return 1;
  551. }