IdentificationCertificate.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_IDENTIFICATIONCERTIFICATE_HPP
  14. #define ZT_IDENTIFICATIONCERTIFICATE_HPP
  15. #include "Constants.hpp"
  16. #include "SHA512.hpp"
  17. #include "C25519.hpp"
  18. #include "ECC384.hpp"
  19. #include "SharedPtr.hpp"
  20. #include "Identity.hpp"
  21. #include "Locator.hpp"
  22. #include "Dictionary.hpp"
  23. #include "Utils.hpp"
  24. #include "Containers.hpp"
  25. namespace ZeroTier {
  26. /**
  27. * Certificate identifying the real world owner of an identity or network.
  28. *
  29. * This is a wrapper around the straight C ZT_IdentificationCertificate and
  30. * handles allocating memory for objects and disposing of it on GC. If filling
  31. * out a ZT_IdentificationCertificate structure, identities and other objects
  32. * should be attached via the addXXX() methods rather than by directly setting
  33. * the pointers in the C structure.
  34. *
  35. * If identities and similar objects are NOT added via the addXXX() methods,
  36. * this will not take care of de-allocating them when destroyed.
  37. *
  38. * The serialNo field is filled in automatically by sign() and decode(), so
  39. * it can be left undefined when building certificates.
  40. */
  41. class IdentificationCertificate : public ZT_IdentificationCertificate
  42. {
  43. public:
  44. ZT_INLINE IdentificationCertificate() noexcept
  45. { Utils::zero< sizeof(ZT_IdentificationCertificate) >((ZT_IdentificationCertificate *)this); }
  46. ZT_INLINE IdentificationCertificate(const ZT_IdentificationCertificate &apiCert)
  47. { Utils::copy< sizeof(ZT_IdentificationCertificate) >((ZT_IdentificationCertificate *)this, &apiCert); }
  48. ZT_INLINE IdentificationCertificate(const IdentificationCertificate &cert)
  49. { *this = cert; }
  50. IdentificationCertificate &operator=(const ZT_IdentificationCertificate &apiCert);
  51. IdentificationCertificate &operator=(const IdentificationCertificate &cert);
  52. /**
  53. * Add a subject node/identity without a locator
  54. *
  55. * @param id Identity
  56. * @return Pointer to C struct
  57. */
  58. ZT_IdentificationCertificate_Node *addSubjectNode(const Identity &id);
  59. /**
  60. * Add a subject node/identity with a locator
  61. *
  62. * @param id Identity
  63. * @param loc Locator signed by identity (signature is NOT checked here)
  64. * @return Pointer to C struct
  65. */
  66. ZT_IdentificationCertificate_Node *addSubjectNode(const Identity &id, const Locator &loc);
  67. /**
  68. * Add a subject network
  69. *
  70. * @param id Network ID
  71. * @param controller Network controller's full fingerprint
  72. * @return Pointer to C struct
  73. */
  74. ZT_IdentificationCertificate_Network *addSubjectNetwork(const uint64_t id, const ZT_Fingerprint &controller);
  75. /**
  76. * Marshal this certificate in binary form
  77. *
  78. * The internal encoding used here is Dictionary to permit easy
  79. * extensibility.
  80. *
  81. * @param omitSignature If true omit the signature field (for signing and verification, default is false)
  82. * @return Marshaled certificate
  83. */
  84. Vector< uint8_t > encode(bool omitSignature = false) const;
  85. /**
  86. * Decode this certificate from marshaled bytes.
  87. *
  88. * @param data Marshalled certificate
  89. * @return True if input is valid and was unmarshalled (signature is NOT checked)
  90. */
  91. bool decode(const Vector< uint8_t > &data);
  92. /**
  93. * Sign this certificate (and also fill in serialNo).
  94. *
  95. * @param issuer Issuer identity (must have secret key)
  96. * @return True on success
  97. */
  98. bool sign(const Identity &issuer);
  99. /**
  100. * Verify certificate signature against the issuer contained therein
  101. *
  102. * @return True if certificate is signed and signature is valid
  103. */
  104. bool verify() const;
  105. private:
  106. // These hold any identity or locator objects that are owned by and should
  107. // be deleted with this certificate. Lists are used so the pointers never
  108. // change.
  109. List< Identity > m_identities;
  110. List< Locator > m_locators;
  111. // These are stored in a vector because the memory needs to be contiguous.
  112. Vector< ZT_IdentificationCertificate_Node > m_nodes;
  113. Vector< ZT_IdentificationCertificate_Network > m_networks;
  114. };
  115. } // namespace ZeroTier
  116. #endif