X509Bundle.cpp 5.8 KB

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