ssl_pm.c 18 KB

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