X509Bundle.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #pragma once
  2. #include <mbedtls/x509_crt.h> // for mbedtls_x509_crt
  3. #include <stddef.h> // for size_t
  4. #include <cstdint> // for uint8_t, uint16_t, uint32_t
  5. #include <vector> // for vector
  6. #include "mbedtls/ssl.h" // for mbedtls_ssl_config
  7. namespace bell::X509Bundle {
  8. typedef struct crt_bundle_t {
  9. const uint8_t** crts;
  10. uint16_t num_certs;
  11. size_t x509_crt_bundle_len;
  12. } crt_bundle_t;
  13. static crt_bundle_t s_crt_bundle;
  14. static std::vector<uint8_t> bundleBytes;
  15. static constexpr auto TAG = "X509Bundle";
  16. static constexpr auto CRT_HEADER_OFFSET = 4;
  17. static constexpr auto BUNDLE_HEADER_OFFSET = 2;
  18. int crtCheckCertificate(mbedtls_x509_crt* child, const uint8_t* pub_key_buf,
  19. size_t pub_key_len);
  20. /* This callback is called for every certificate in the chain. If the chain
  21. * is proper each intermediate certificate is validated through its parent
  22. * in the x509_crt_verify_chain() function. So this callback should
  23. * only verify the first untrusted link in the chain is signed by the
  24. * root certificate in the trusted bundle
  25. */
  26. int crtVerifyCallback(void* buf, mbedtls_x509_crt* crt, int depth,
  27. uint32_t* flags);
  28. /* Initialize the bundle into an array so we can do binary search for certs,
  29. the bundle generated by the python utility is already presorted by subject name
  30. */
  31. void init(const uint8_t* x509_bundle, size_t bundle_size);
  32. void attach(mbedtls_ssl_config* conf);
  33. bool shouldVerify();
  34. }; // namespace bell::X509Bundle