cert_app.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Certificate reading application
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "mbedtls/build_info.h"
  20. #include "mbedtls/platform.h"
  21. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  22. !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
  23. !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) || \
  24. !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
  25. !defined(MBEDTLS_CTR_DRBG_C) || defined(MBEDTLS_X509_REMOVE_INFO)
  26. int main(void)
  27. {
  28. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  29. "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
  30. "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
  31. "MBEDTLS_X509_CRT_PARSE_C and/or MBEDTLS_FS_IO and/or "
  32. "MBEDTLS_CTR_DRBG_C not defined and/or MBEDTLS_X509_REMOVE_INFO defined.\n");
  33. mbedtls_exit(0);
  34. }
  35. #else
  36. #include "mbedtls/entropy.h"
  37. #include "mbedtls/ctr_drbg.h"
  38. #include "mbedtls/net_sockets.h"
  39. #include "mbedtls/ssl.h"
  40. #include "mbedtls/x509.h"
  41. #include "mbedtls/debug.h"
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #define MODE_NONE 0
  46. #define MODE_FILE 1
  47. #define MODE_SSL 2
  48. #define DFL_MODE MODE_NONE
  49. #define DFL_FILENAME "cert.crt"
  50. #define DFL_CA_FILE ""
  51. #define DFL_CRL_FILE ""
  52. #define DFL_CA_PATH ""
  53. #define DFL_SERVER_NAME "localhost"
  54. #define DFL_SERVER_PORT "4433"
  55. #define DFL_DEBUG_LEVEL 0
  56. #define DFL_PERMISSIVE 0
  57. #define USAGE_IO \
  58. " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
  59. " default: \"\" (none)\n" \
  60. " crl_file=%%s The single CRL file you want to use\n" \
  61. " default: \"\" (none)\n" \
  62. " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
  63. " default: \"\" (none) (overrides ca_file)\n"
  64. #define USAGE \
  65. "\n usage: cert_app param=<>...\n" \
  66. "\n acceptable parameters:\n" \
  67. " mode=file|ssl default: none\n" \
  68. " filename=%%s default: cert.crt\n" \
  69. USAGE_IO \
  70. " server_name=%%s default: localhost\n" \
  71. " server_port=%%d default: 4433\n" \
  72. " debug_level=%%d default: 0 (disabled)\n" \
  73. " permissive=%%d default: 0 (disabled)\n" \
  74. "\n"
  75. /*
  76. * global options
  77. */
  78. struct options {
  79. int mode; /* the mode to run the application in */
  80. const char *filename; /* filename of the certificate file */
  81. const char *ca_file; /* the file with the CA certificate(s) */
  82. const char *crl_file; /* the file with the CRL to use */
  83. const char *ca_path; /* the path with the CA certificate(s) reside */
  84. const char *server_name; /* hostname of the server (client only) */
  85. const char *server_port; /* port on which the ssl service runs */
  86. int debug_level; /* level of debugging */
  87. int permissive; /* permissive parsing */
  88. } opt;
  89. static void my_debug(void *ctx, int level,
  90. const char *file, int line,
  91. const char *str)
  92. {
  93. ((void) level);
  94. mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
  95. fflush((FILE *) ctx);
  96. }
  97. static int my_verify(void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
  98. {
  99. char buf[1024];
  100. ((void) data);
  101. mbedtls_printf("\nVerify requested for (Depth %d):\n", depth);
  102. mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
  103. mbedtls_printf("%s", buf);
  104. if ((*flags) == 0) {
  105. mbedtls_printf(" This certificate has no flags\n");
  106. } else {
  107. mbedtls_x509_crt_verify_info(buf, sizeof(buf), " ! ", *flags);
  108. mbedtls_printf("%s\n", buf);
  109. }
  110. return 0;
  111. }
  112. int main(int argc, char *argv[])
  113. {
  114. int ret = 1;
  115. int exit_code = MBEDTLS_EXIT_FAILURE;
  116. mbedtls_net_context server_fd;
  117. unsigned char buf[1024];
  118. mbedtls_entropy_context entropy;
  119. mbedtls_ctr_drbg_context ctr_drbg;
  120. mbedtls_ssl_context ssl;
  121. mbedtls_ssl_config conf;
  122. mbedtls_x509_crt cacert;
  123. mbedtls_x509_crl cacrl;
  124. int i, j;
  125. uint32_t flags;
  126. int verify = 0;
  127. char *p, *q;
  128. const char *pers = "cert_app";
  129. /*
  130. * Set to sane values
  131. */
  132. mbedtls_net_init(&server_fd);
  133. mbedtls_ctr_drbg_init(&ctr_drbg);
  134. mbedtls_ssl_init(&ssl);
  135. mbedtls_ssl_config_init(&conf);
  136. mbedtls_x509_crt_init(&cacert);
  137. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  138. mbedtls_x509_crl_init(&cacrl);
  139. #else
  140. /* Zeroize structure as CRL parsing is not supported and we have to pass
  141. it to the verify function */
  142. memset(&cacrl, 0, sizeof(mbedtls_x509_crl));
  143. #endif
  144. if (argc < 2) {
  145. usage:
  146. mbedtls_printf(USAGE);
  147. goto exit;
  148. }
  149. opt.mode = DFL_MODE;
  150. opt.filename = DFL_FILENAME;
  151. opt.ca_file = DFL_CA_FILE;
  152. opt.crl_file = DFL_CRL_FILE;
  153. opt.ca_path = DFL_CA_PATH;
  154. opt.server_name = DFL_SERVER_NAME;
  155. opt.server_port = DFL_SERVER_PORT;
  156. opt.debug_level = DFL_DEBUG_LEVEL;
  157. opt.permissive = DFL_PERMISSIVE;
  158. for (i = 1; i < argc; i++) {
  159. p = argv[i];
  160. if ((q = strchr(p, '=')) == NULL) {
  161. goto usage;
  162. }
  163. *q++ = '\0';
  164. for (j = 0; p + j < q; j++) {
  165. if (argv[i][j] >= 'A' && argv[i][j] <= 'Z') {
  166. argv[i][j] |= 0x20;
  167. }
  168. }
  169. if (strcmp(p, "mode") == 0) {
  170. if (strcmp(q, "file") == 0) {
  171. opt.mode = MODE_FILE;
  172. } else if (strcmp(q, "ssl") == 0) {
  173. opt.mode = MODE_SSL;
  174. } else {
  175. goto usage;
  176. }
  177. } else if (strcmp(p, "filename") == 0) {
  178. opt.filename = q;
  179. } else if (strcmp(p, "ca_file") == 0) {
  180. opt.ca_file = q;
  181. } else if (strcmp(p, "crl_file") == 0) {
  182. opt.crl_file = q;
  183. } else if (strcmp(p, "ca_path") == 0) {
  184. opt.ca_path = q;
  185. } else if (strcmp(p, "server_name") == 0) {
  186. opt.server_name = q;
  187. } else if (strcmp(p, "server_port") == 0) {
  188. opt.server_port = q;
  189. } else if (strcmp(p, "debug_level") == 0) {
  190. opt.debug_level = atoi(q);
  191. if (opt.debug_level < 0 || opt.debug_level > 65535) {
  192. goto usage;
  193. }
  194. } else if (strcmp(p, "permissive") == 0) {
  195. opt.permissive = atoi(q);
  196. if (opt.permissive < 0 || opt.permissive > 1) {
  197. goto usage;
  198. }
  199. } else {
  200. goto usage;
  201. }
  202. }
  203. /*
  204. * 1.1. Load the trusted CA
  205. */
  206. mbedtls_printf(" . Loading the CA root certificate ...");
  207. fflush(stdout);
  208. if (strlen(opt.ca_path)) {
  209. if ((ret = mbedtls_x509_crt_parse_path(&cacert, opt.ca_path)) < 0) {
  210. mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_path returned -0x%x\n\n",
  211. (unsigned int) -ret);
  212. goto exit;
  213. }
  214. verify = 1;
  215. } else if (strlen(opt.ca_file)) {
  216. if ((ret = mbedtls_x509_crt_parse_file(&cacert, opt.ca_file)) < 0) {
  217. mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
  218. (unsigned int) -ret);
  219. goto exit;
  220. }
  221. verify = 1;
  222. }
  223. mbedtls_printf(" ok (%d skipped)\n", ret);
  224. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  225. if (strlen(opt.crl_file)) {
  226. if ((ret = mbedtls_x509_crl_parse_file(&cacrl, opt.crl_file)) != 0) {
  227. mbedtls_printf(" failed\n ! mbedtls_x509_crl_parse returned -0x%x\n\n",
  228. (unsigned int) -ret);
  229. goto exit;
  230. }
  231. verify = 1;
  232. }
  233. #endif
  234. if (opt.mode == MODE_FILE) {
  235. mbedtls_x509_crt crt;
  236. mbedtls_x509_crt *cur = &crt;
  237. mbedtls_x509_crt_init(&crt);
  238. /*
  239. * 1.1. Load the certificate(s)
  240. */
  241. mbedtls_printf("\n . Loading the certificate(s) ...");
  242. fflush(stdout);
  243. ret = mbedtls_x509_crt_parse_file(&crt, opt.filename);
  244. if (ret < 0) {
  245. mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file returned %d\n\n", ret);
  246. mbedtls_x509_crt_free(&crt);
  247. goto exit;
  248. }
  249. if (opt.permissive == 0 && ret > 0) {
  250. mbedtls_printf(
  251. " failed\n ! mbedtls_x509_crt_parse failed to parse %d certificates\n\n",
  252. ret);
  253. mbedtls_x509_crt_free(&crt);
  254. goto exit;
  255. }
  256. mbedtls_printf(" ok\n");
  257. /*
  258. * 1.2 Print the certificate(s)
  259. */
  260. while (cur != NULL) {
  261. mbedtls_printf(" . Peer certificate information ...\n");
  262. ret = mbedtls_x509_crt_info((char *) buf, sizeof(buf) - 1, " ",
  263. cur);
  264. if (ret == -1) {
  265. mbedtls_printf(" failed\n ! mbedtls_x509_crt_info returned %d\n\n", ret);
  266. mbedtls_x509_crt_free(&crt);
  267. goto exit;
  268. }
  269. mbedtls_printf("%s\n", buf);
  270. cur = cur->next;
  271. }
  272. /*
  273. * 1.3 Verify the certificate
  274. */
  275. if (verify) {
  276. mbedtls_printf(" . Verifying X.509 certificate...");
  277. if ((ret = mbedtls_x509_crt_verify(&crt, &cacert, &cacrl, NULL, &flags,
  278. my_verify, NULL)) != 0) {
  279. char vrfy_buf[512];
  280. mbedtls_printf(" failed\n");
  281. mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), " ! ", flags);
  282. mbedtls_printf("%s\n", vrfy_buf);
  283. } else {
  284. mbedtls_printf(" ok\n");
  285. }
  286. }
  287. mbedtls_x509_crt_free(&crt);
  288. } else if (opt.mode == MODE_SSL) {
  289. /*
  290. * 1. Initialize the RNG and the session data
  291. */
  292. mbedtls_printf("\n . Seeding the random number generator...");
  293. fflush(stdout);
  294. mbedtls_entropy_init(&entropy);
  295. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
  296. (const unsigned char *) pers,
  297. strlen(pers))) != 0) {
  298. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
  299. goto ssl_exit;
  300. }
  301. mbedtls_printf(" ok\n");
  302. #if defined(MBEDTLS_DEBUG_C)
  303. mbedtls_debug_set_threshold(opt.debug_level);
  304. #endif
  305. /*
  306. * 2. Start the connection
  307. */
  308. mbedtls_printf(" . SSL connection to tcp/%s/%s...", opt.server_name,
  309. opt.server_port);
  310. fflush(stdout);
  311. if ((ret = mbedtls_net_connect(&server_fd, opt.server_name,
  312. opt.server_port, MBEDTLS_NET_PROTO_TCP)) != 0) {
  313. mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
  314. goto ssl_exit;
  315. }
  316. /*
  317. * 3. Setup stuff
  318. */
  319. if ((ret = mbedtls_ssl_config_defaults(&conf,
  320. MBEDTLS_SSL_IS_CLIENT,
  321. MBEDTLS_SSL_TRANSPORT_STREAM,
  322. MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
  323. mbedtls_printf(" failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret);
  324. goto exit;
  325. }
  326. if (verify) {
  327. mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED);
  328. mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
  329. mbedtls_ssl_conf_verify(&conf, my_verify, NULL);
  330. } else {
  331. mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE);
  332. }
  333. mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
  334. mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
  335. if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) {
  336. mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
  337. goto ssl_exit;
  338. }
  339. if ((ret = mbedtls_ssl_set_hostname(&ssl, opt.server_name)) != 0) {
  340. mbedtls_printf(" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
  341. goto ssl_exit;
  342. }
  343. mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
  344. /*
  345. * 4. Handshake
  346. */
  347. while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
  348. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  349. mbedtls_printf(" failed\n ! mbedtls_ssl_handshake returned %d\n\n", ret);
  350. goto ssl_exit;
  351. }
  352. }
  353. mbedtls_printf(" ok\n");
  354. /*
  355. * 5. Print the certificate
  356. */
  357. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  358. mbedtls_printf(" . Peer certificate information ... skipped\n");
  359. #else
  360. mbedtls_printf(" . Peer certificate information ...\n");
  361. ret = mbedtls_x509_crt_info((char *) buf, sizeof(buf) - 1, " ",
  362. mbedtls_ssl_get_peer_cert(&ssl));
  363. if (ret == -1) {
  364. mbedtls_printf(" failed\n ! mbedtls_x509_crt_info returned %d\n\n", ret);
  365. goto ssl_exit;
  366. }
  367. mbedtls_printf("%s\n", buf);
  368. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  369. mbedtls_ssl_close_notify(&ssl);
  370. ssl_exit:
  371. mbedtls_ssl_free(&ssl);
  372. mbedtls_ssl_config_free(&conf);
  373. } else {
  374. goto usage;
  375. }
  376. exit_code = MBEDTLS_EXIT_SUCCESS;
  377. exit:
  378. mbedtls_net_free(&server_fd);
  379. mbedtls_x509_crt_free(&cacert);
  380. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  381. mbedtls_x509_crl_free(&cacrl);
  382. #endif
  383. mbedtls_ctr_drbg_free(&ctr_drbg);
  384. mbedtls_entropy_free(&entropy);
  385. mbedtls_exit(exit_code);
  386. }
  387. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
  388. MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
  389. MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */