2
0

X509Bundle.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "X509Bundle.h"
  2. #include <mbedtls/md.h> // for mbedtls_md, mbedtls_md_get_size
  3. #include <mbedtls/pk.h> // for mbedtls_pk_can_do, mbedtls_pk_pa...
  4. #include <mbedtls/ssl.h> // for mbedtls_ssl_conf_ca_chain, mbedt...
  5. #include <mbedtls/x509.h> // for mbedtls_x509_buf, MBEDTLS_ERR_X5...
  6. #include <stdlib.h> // for free, calloc
  7. #include <string.h> // for memcmp, memcpy
  8. #include <stdexcept> // for runtime_error
  9. #include "BellLogger.h" // for AbstractLogger, BELL_LOG
  10. using namespace bell::X509Bundle;
  11. static mbedtls_x509_crt s_dummy_crt;
  12. static bool s_should_verify_certs = false;
  13. #ifndef MBEDTLS_PRIVATE
  14. #define MBEDTLS_PRIVATE(member) member
  15. #endif
  16. int bell::X509Bundle::crtCheckCertificate(mbedtls_x509_crt* child,
  17. const uint8_t* pub_key_buf,
  18. size_t pub_key_len) {
  19. int ret = 0;
  20. mbedtls_x509_crt parent;
  21. const mbedtls_md_info_t* md_info;
  22. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  23. mbedtls_x509_crt_init(&parent);
  24. if ((ret = mbedtls_pk_parse_public_key(&parent.pk, pub_key_buf,
  25. pub_key_len)) != 0) {
  26. BELL_LOG(error, TAG, "PK parse failed with error 0x%04x, key len = %d", ret,
  27. pub_key_len);
  28. goto cleanup;
  29. }
  30. // Fast check to avoid expensive computations when not necessary
  31. if (!mbedtls_pk_can_do(&parent.pk, child->MBEDTLS_PRIVATE(sig_pk))) {
  32. BELL_LOG(error, TAG, "Simple compare failed");
  33. ret = -1;
  34. goto cleanup;
  35. }
  36. md_info = mbedtls_md_info_from_type(child->MBEDTLS_PRIVATE(sig_md));
  37. if ((ret = mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash)) != 0) {
  38. BELL_LOG(error, TAG, "Internal mbedTLS error %X", ret);
  39. goto cleanup;
  40. }
  41. if ((ret = mbedtls_pk_verify_ext(
  42. child->MBEDTLS_PRIVATE(sig_pk), child->MBEDTLS_PRIVATE(sig_opts),
  43. &parent.pk, child->MBEDTLS_PRIVATE(sig_md), hash,
  44. mbedtls_md_get_size(md_info), child->MBEDTLS_PRIVATE(sig).p,
  45. child->MBEDTLS_PRIVATE(sig).len)) != 0) {
  46. BELL_LOG(error, TAG, "PK verify failed with error %X", ret);
  47. goto cleanup;
  48. }
  49. cleanup:
  50. mbedtls_x509_crt_free(&parent);
  51. return ret;
  52. }
  53. /* This callback is called for every certificate in the chain. If the chain
  54. * is proper each intermediate certificate is validated through its parent
  55. * in the x509_crt_verify_chain() function. So this callback should
  56. * only verify the first untrusted link in the chain is signed by the
  57. * root certificate in the trusted bundle
  58. */
  59. int bell::X509Bundle::crtVerifyCallback(void* buf, mbedtls_x509_crt* crt,
  60. int depth, uint32_t* flags) {
  61. mbedtls_x509_crt* child = crt;
  62. /* It's OK for a trusted cert to have a weak signature hash alg.
  63. as we already trust this certificate */
  64. uint32_t flags_filtered = *flags & ~(MBEDTLS_X509_BADCERT_BAD_MD);
  65. if (flags_filtered != MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
  66. return 0;
  67. }
  68. if (s_crt_bundle.crts == NULL) {
  69. BELL_LOG(error, TAG, "No certificates in bundle");
  70. return MBEDTLS_ERR_X509_FATAL_ERROR;
  71. }
  72. BELL_LOG(debug, TAG, "%d certificates in bundle", s_crt_bundle.num_certs);
  73. size_t name_len = 0;
  74. const uint8_t* crt_name;
  75. bool crt_found = false;
  76. int start = 0;
  77. int end = s_crt_bundle.num_certs - 1;
  78. int middle = (end - start) / 2;
  79. /* Look for the certificate using binary search on subject name */
  80. while (start <= end) {
  81. name_len = s_crt_bundle.crts[middle][0] << 8 | s_crt_bundle.crts[middle][1];
  82. crt_name = s_crt_bundle.crts[middle] + CRT_HEADER_OFFSET;
  83. int cmp_res = memcmp(child->issuer_raw.p, crt_name, name_len);
  84. if (cmp_res == 0) {
  85. crt_found = true;
  86. break;
  87. } else if (cmp_res < 0) {
  88. end = middle - 1;
  89. } else {
  90. start = middle + 1;
  91. }
  92. middle = (start + end) / 2;
  93. }
  94. int ret = MBEDTLS_ERR_X509_FATAL_ERROR;
  95. if (crt_found) {
  96. size_t key_len =
  97. s_crt_bundle.crts[middle][2] << 8 | s_crt_bundle.crts[middle][3];
  98. ret = crtCheckCertificate(
  99. child, s_crt_bundle.crts[middle] + CRT_HEADER_OFFSET + name_len,
  100. key_len);
  101. } else {
  102. BELL_LOG(error, TAG, "Certificate not found in bundle");
  103. }
  104. if (ret == 0) {
  105. BELL_LOG(info, TAG, "Certificate validated");
  106. *flags = 0;
  107. return 0;
  108. }
  109. BELL_LOG(info, TAG, "Failed to verify certificate");
  110. return MBEDTLS_ERR_X509_FATAL_ERROR;
  111. }
  112. /* Initialize the bundle into an array so we can do binary search for certs,
  113. the bundle generated by the python utility is already presorted by subject name
  114. */
  115. void bell::X509Bundle::init(const uint8_t* x509_bundle, size_t bundle_size) {
  116. if (bundle_size < BUNDLE_HEADER_OFFSET + CRT_HEADER_OFFSET) {
  117. throw std::runtime_error("Invalid certificate bundle");
  118. }
  119. uint16_t num_certs = (x509_bundle[0] << 8) | x509_bundle[1];
  120. const uint8_t** crts =
  121. (const uint8_t**)calloc(num_certs, sizeof(x509_bundle));
  122. if (crts == NULL) {
  123. throw std::runtime_error("Unable to allocate memory for bundle");
  124. }
  125. bundleBytes.resize(bundle_size);
  126. memcpy(bundleBytes.data(), x509_bundle, bundle_size);
  127. const uint8_t* cur_crt;
  128. /* This is the maximum region that is allowed to access */
  129. const uint8_t* bundle_end = bundleBytes.data() + bundle_size;
  130. cur_crt = bundleBytes.data() + BUNDLE_HEADER_OFFSET;
  131. for (int i = 0; i < num_certs; i++) {
  132. crts[i] = cur_crt;
  133. if (cur_crt + CRT_HEADER_OFFSET > bundle_end) {
  134. free(crts);
  135. throw std::runtime_error("Invalid certificate bundle");
  136. }
  137. size_t name_len = cur_crt[0] << 8 | cur_crt[1];
  138. size_t key_len = cur_crt[2] << 8 | cur_crt[3];
  139. cur_crt = cur_crt + CRT_HEADER_OFFSET + name_len + key_len;
  140. }
  141. if (cur_crt > bundle_end) {
  142. free(crts);
  143. throw std::runtime_error("Invalid certificate bundle");
  144. }
  145. /* The previous crt bundle is only updated when initialization of the
  146. * current crt_bundle is successful */
  147. /* Free previous crt_bundle */
  148. free(s_crt_bundle.crts);
  149. s_crt_bundle.num_certs = num_certs;
  150. s_crt_bundle.crts = crts;
  151. // Enable certificate verification
  152. s_should_verify_certs = true;
  153. }
  154. void bell::X509Bundle::attach(mbedtls_ssl_config* conf) {
  155. /* point to a dummy certificate
  156. * This is only required so that the
  157. * cacert_ptr passes non-NULL check during handshake
  158. */
  159. mbedtls_ssl_config* ssl_conf = (mbedtls_ssl_config*)conf;
  160. mbedtls_x509_crt_init(&s_dummy_crt);
  161. mbedtls_ssl_conf_ca_chain(ssl_conf, &s_dummy_crt, NULL);
  162. mbedtls_ssl_conf_verify(ssl_conf, crtVerifyCallback, NULL);
  163. }
  164. bool bell::X509Bundle::shouldVerify() {
  165. return s_should_verify_certs;
  166. }