transport.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. generic transport class for handling OpenDroneID messages
  3. */
  4. #include <Arduino.h>
  5. #include "transport.h"
  6. #include "parameters.h"
  7. #include "util.h"
  8. #include "monocypher.h"
  9. const char *Transport::parse_fail = "uninitialised";
  10. uint32_t Transport::last_location_ms;
  11. uint32_t Transport::last_basic_id_ms;
  12. uint32_t Transport::last_self_id_ms;
  13. uint32_t Transport::last_operator_id_ms;
  14. uint32_t Transport::last_system_ms;
  15. uint32_t Transport::last_system_timestamp;
  16. float Transport::last_location_timestamp;
  17. mavlink_open_drone_id_location_t Transport::location;
  18. mavlink_open_drone_id_basic_id_t Transport::basic_id;
  19. mavlink_open_drone_id_authentication_t Transport::authentication;
  20. mavlink_open_drone_id_self_id_t Transport::self_id;
  21. mavlink_open_drone_id_system_t Transport::system;
  22. mavlink_open_drone_id_operator_id_t Transport::operator_id;
  23. Transport::Transport()
  24. {
  25. }
  26. /*
  27. check we are OK to arm
  28. */
  29. uint8_t Transport::arm_status_check(const char *&reason)
  30. {
  31. const uint32_t max_age_location_ms = 3000;
  32. const uint32_t max_age_other_ms = 22000;
  33. const uint32_t now_ms = millis();
  34. uint8_t status = MAV_ODID_PRE_ARM_FAIL_GENERIC;
  35. //return status OK if we have enabled the force arm option
  36. if (g.options & OPTIONS_FORCE_ARM_OK) {
  37. status = MAV_ODID_GOOD_TO_ARM;
  38. return status;
  39. }
  40. if (last_location_ms == 0 || now_ms - last_location_ms > max_age_location_ms) {
  41. reason = "missing location message";
  42. } else if (!g.have_basic_id_info() && (last_basic_id_ms == 0 || now_ms - last_basic_id_ms > max_age_other_ms)) {
  43. reason = "missing basic_id message";
  44. } else if (last_self_id_ms == 0 || now_ms - last_self_id_ms > max_age_other_ms) {
  45. reason = "missing self_id message";
  46. } else if (last_operator_id_ms == 0 || now_ms - last_operator_id_ms > max_age_other_ms) {
  47. reason = "missing operator_id message";
  48. } else if (last_system_ms == 0 || now_ms - last_system_ms > max_age_location_ms) {
  49. // we use location age limit for system as the operator location needs to come in as fast
  50. // as the vehicle location for FAA standard
  51. reason = "missing system message";
  52. } else if (location.latitude == 0 && location.longitude == 0) {
  53. reason = "Bad location";
  54. } else if (system.operator_latitude == 0 && system.operator_longitude == 0) {
  55. reason = "Bad operator location";
  56. } else if (reason == nullptr) {
  57. status = MAV_ODID_GOOD_TO_ARM;
  58. }
  59. return status;
  60. }
  61. /*
  62. make a session key
  63. */
  64. void Transport::make_session_key(uint8_t key[8]) const
  65. {
  66. struct {
  67. uint32_t time_us;
  68. uint8_t mac[8];
  69. uint32_t rand;
  70. } data {};
  71. static_assert(sizeof(data) % 4 == 0, "data must be multiple of 4 bytes");
  72. esp_efuse_mac_get_default(data.mac);
  73. data.time_us = micros();
  74. data.rand = random(0xFFFFFFFF);
  75. const uint64_t c64 = crc_crc64((const uint32_t *)&data, sizeof(data)/sizeof(uint32_t));
  76. memcpy(key, (uint8_t *)&c64, 8);
  77. }
  78. /*
  79. check signature in a command against public keys
  80. */
  81. bool Transport::check_signature(uint8_t sig_length, uint8_t data_len, uint32_t sequence, uint32_t operation,
  82. const uint8_t *data)
  83. {
  84. if (g.no_public_keys()) {
  85. // allow through if no keys are setup
  86. return true;
  87. }
  88. if (sig_length != 64) {
  89. // monocypher signatures are 64 bytes
  90. return false;
  91. }
  92. /*
  93. loop over all public keys, if one matches then we are OK
  94. */
  95. for (uint8_t i=0; i<MAX_PUBLIC_KEYS; i++) {
  96. uint8_t key[32];
  97. if (!g.get_public_key(i, key)) {
  98. continue;
  99. }
  100. crypto_check_ctx ctx {};
  101. crypto_check_ctx_abstract *actx = (crypto_check_ctx_abstract*)&ctx;
  102. crypto_check_init(actx, &data[data_len], key);
  103. crypto_check_update(actx, (const uint8_t*)&sequence, sizeof(sequence));
  104. crypto_check_update(actx, (const uint8_t*)&operation, sizeof(operation));
  105. crypto_check_update(actx, data, data_len);
  106. if (operation != SECURE_COMMAND_GET_SESSION_KEY &&
  107. operation != SECURE_COMMAND_GET_REMOTEID_SESSION_KEY) {
  108. crypto_check_update(actx, session_key, sizeof(session_key));
  109. }
  110. if (crypto_check_final(actx) == 0) {
  111. // good signature
  112. return true;
  113. }
  114. }
  115. return false;
  116. }