ssl_pm.c 16 KB

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