Revocation.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "Revocation.hpp"
  14. namespace ZeroTier {
  15. bool Revocation::sign(const Identity &signer)
  16. {
  17. uint8_t buf[ZT_REVOCATION_MARSHAL_SIZE_MAX+32];
  18. if (signer.hasPrivate()) {
  19. _signedBy = signer.address();
  20. _signatureLength = signer.sign(buf,(unsigned int)marshal(buf,true),_signature,sizeof(_signature));
  21. return true;
  22. }
  23. return false;
  24. }
  25. int Revocation::marshal(uint8_t data[ZT_REVOCATION_MARSHAL_SIZE_MAX],bool forSign) const
  26. {
  27. int p = 0;
  28. if (forSign) {
  29. for(int k=0;k<8;++k)
  30. data[p++] = 0x7f;
  31. }
  32. Utils::storeBigEndian<uint32_t>(data + p,0); p += 4;
  33. Utils::storeBigEndian<uint32_t>(data + p,_id); p += 4;
  34. Utils::storeBigEndian<uint64_t>(data + p,_networkId); p += 8;
  35. Utils::storeBigEndian<uint32_t>(data + p,0); p += 4;
  36. Utils::storeBigEndian<uint32_t>(data + p,_credentialId); p += 4;
  37. Utils::storeBigEndian<uint64_t>(data + p,(uint64_t)_threshold); p += 8;
  38. Utils::storeBigEndian<uint64_t>(data + p,_flags); p += 8;
  39. _target.copyTo(data + p); p += ZT_ADDRESS_LENGTH;
  40. _signedBy.copyTo(data + p); p += ZT_ADDRESS_LENGTH;
  41. data[p++] = (uint8_t)_type;
  42. if (!forSign) {
  43. data[p++] = 1;
  44. Utils::storeBigEndian<uint16_t>(data + p,(uint16_t)_signatureLength);
  45. memcpy(data + p,_signature,_signatureLength);
  46. p += (int)_signatureLength;
  47. }
  48. data[p++] = 0;
  49. data[p++] = 0;
  50. if (forSign) {
  51. for(int k=0;k<8;++k)
  52. data[p++] = 0x7f;
  53. }
  54. return p;
  55. }
  56. int Revocation::unmarshal(const uint8_t *restrict data,const int len)
  57. {
  58. if (len < 54)
  59. return -1;
  60. // 4 bytes reserved
  61. _id = Utils::loadBigEndian<uint32_t>(data + 4);
  62. _networkId = Utils::loadBigEndian<uint64_t>(data + 8);
  63. // 4 bytes reserved
  64. _credentialId = Utils::loadBigEndian<uint32_t>(data + 20);
  65. _threshold = (int64_t)Utils::loadBigEndian<uint64_t>(data + 24);
  66. _flags = Utils::loadBigEndian<uint64_t>(data + 32);
  67. _target.setTo(data + 40);
  68. _signedBy.setTo(data + 45);
  69. _type = (ZT_CredentialType)data[50];
  70. // 1 byte reserved
  71. _signatureLength = Utils::loadBigEndian<uint16_t>(data + 52);
  72. int p = 54 + (int)_signatureLength;
  73. if ((_signatureLength > ZT_SIGNATURE_BUFFER_SIZE)||(p > len))
  74. return -1;
  75. memcpy(_signature,data + 54,_signatureLength);
  76. if ((p + 2) > len)
  77. return -1;
  78. p += 2 + Utils::loadBigEndian<uint16_t>(data + p);
  79. if (p > len)
  80. return -1;
  81. return p;
  82. }
  83. } // namespace ZeroTier