CertificateOfOwnership.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include "CertificateOfOwnership.hpp"
  14. namespace ZeroTier {
  15. void CertificateOfOwnership::addThing(const InetAddress &ip)
  16. {
  17. if (_thingCount >= ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS)
  18. return;
  19. if (ip.family() == AF_INET) {
  20. _thingTypes[_thingCount] = THING_IPV4_ADDRESS;
  21. Utils::copy<4>(_thingValues[_thingCount],&(reinterpret_cast<const struct sockaddr_in *>(&ip)->sin_addr.s_addr));
  22. ++_thingCount;
  23. } else if (ip.family() == AF_INET6) {
  24. _thingTypes[_thingCount] = THING_IPV6_ADDRESS;
  25. Utils::copy<16>(_thingValues[_thingCount],reinterpret_cast<const struct sockaddr_in6 *>(&ip)->sin6_addr.s6_addr);
  26. ++_thingCount;
  27. }
  28. }
  29. void CertificateOfOwnership::addThing(const MAC &mac)
  30. {
  31. if (_thingCount >= ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS)
  32. return;
  33. _thingTypes[_thingCount] = THING_MAC_ADDRESS;
  34. mac.copyTo(_thingValues[_thingCount]);
  35. ++_thingCount;
  36. }
  37. bool CertificateOfOwnership::sign(const Identity &signer)
  38. {
  39. uint8_t buf[ZT_CERTIFICATEOFOWNERSHIP_MARSHAL_SIZE_MAX + 16];
  40. if (signer.hasPrivate()) {
  41. _signedBy = signer.address();
  42. _signatureLength = signer.sign(buf,(unsigned int)marshal(buf,true),_signature,sizeof(_signature));
  43. return true;
  44. }
  45. return false;
  46. }
  47. int CertificateOfOwnership::marshal(uint8_t data[ZT_CERTIFICATEOFOWNERSHIP_MARSHAL_SIZE_MAX],bool forSign) const noexcept
  48. {
  49. int p = 0;
  50. if (forSign) {
  51. for(int k=0;k<16;++k)
  52. data[p++] = 0x7f;
  53. }
  54. Utils::storeBigEndian<uint64_t>(data + p,_networkId);
  55. Utils::storeBigEndian<uint64_t>(data + p + 8,(uint64_t)_ts);
  56. Utils::storeBigEndian<uint64_t>(data + p + 16,_flags);
  57. Utils::storeBigEndian<uint32_t>(data + p + 24,_id);
  58. Utils::storeBigEndian<uint16_t>(data + p + 28,(uint16_t)_thingCount);
  59. p += 30;
  60. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  61. data[p++] = _thingTypes[i];
  62. Utils::copy<ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE>(data + p,_thingValues[i]);
  63. p += ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE;
  64. }
  65. _issuedTo.copyTo(data + p); p += ZT_ADDRESS_LENGTH;
  66. _signedBy.copyTo(data + p); p += ZT_ADDRESS_LENGTH;
  67. if (!forSign) {
  68. data[p++] = 1;
  69. Utils::storeBigEndian<uint16_t>(data + p,(uint16_t)_signatureLength); p += 2;
  70. Utils::copy(data + p,_signature,_signatureLength); p += (int)_signatureLength;
  71. }
  72. data[p++] = 0;
  73. data[p++] = 0;
  74. if (forSign) {
  75. for(int k=0;k<16;++k)
  76. data[p++] = 0x7f;
  77. }
  78. return p;
  79. }
  80. int CertificateOfOwnership::unmarshal(const uint8_t *data,int len) noexcept
  81. {
  82. if (len < 30)
  83. return -1;
  84. _networkId = Utils::loadBigEndian<uint64_t>(data);
  85. _ts = (int64_t)Utils::loadBigEndian<uint64_t>(data + 8);
  86. _flags = Utils::loadBigEndian<uint64_t>(data + 16);
  87. _id = Utils::loadBigEndian<uint32_t>(data + 24);
  88. _thingCount = Utils::loadBigEndian<uint16_t>(data + 28);
  89. if (_thingCount > ZT_CERTIFICATEOFOWNERSHIP_MAX_THINGS)
  90. return -1;
  91. int p = 30;
  92. for(unsigned int i=0,j=_thingCount;i<j;++i) {
  93. if ((p + 1 + ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE) > len)
  94. return -1;
  95. _thingTypes[i] = data[p++];
  96. Utils::copy<ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE>(_thingValues[i],data + p);
  97. p += ZT_CERTIFICATEOFOWNERSHIP_MAX_THING_VALUE_SIZE;
  98. }
  99. if ((p + ZT_ADDRESS_LENGTH + ZT_ADDRESS_LENGTH + 1 + 2) > len)
  100. return -1;
  101. _issuedTo.setTo(data + p); p += ZT_ADDRESS_LENGTH;
  102. _signedBy.setTo(data + p); p += ZT_ADDRESS_LENGTH + 1;
  103. p += 2 + Utils::loadBigEndian<uint16_t>(data + p);
  104. if (p > len)
  105. return -1;
  106. return p;
  107. }
  108. } // namespace ZeroTier