ssl_pm.c 22 KB

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