From c027b2f49046689c2f15fde7896c11f668a484b8 Mon Sep 17 00:00:00 2001 From: yoziru Date: Fri, 26 Jul 2024 02:28:39 +0200 Subject: [PATCH 1/4] rename buildEphemeralKeyMessage -> buildSessionInfoRequestMessage --- include/client.h | 2 +- src/client.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/client.h b/include/client.h index e614e14..84d7bce 100644 --- a/include/client.h +++ b/include/client.h @@ -117,7 +117,7 @@ namespace TeslaBLE static int parsePayloadCarServerResponse(UniversalMessage_RoutableMessage_protobuf_message_as_bytes_t *input_buffer, CarServer_Response *output); - int buildEphemeralKeyMessage(UniversalMessage_Domain domain, + int buildSessionInfoRequestMessage(UniversalMessage_Domain domain, pb_byte_t *output_buffer, size_t *output_length); diff --git a/src/client.cpp b/src/client.cpp index 9a6b9bb..5799dd1 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -918,7 +918,7 @@ namespace TeslaBLE * @param output_length Size of the output buffer * @return int result code 0 for successful */ - int Client::buildEphemeralKeyMessage(UniversalMessage_Domain domain, + int Client::buildSessionInfoRequestMessage(UniversalMessage_Domain domain, pb_byte_t *output_buffer, size_t *output_length) { From 4f139a2587887feae87385ea54f6cf1557699d46 Mon Sep 17 00:00:00 2001 From: yoziru Date: Fri, 26 Jul 2024 03:28:10 +0200 Subject: [PATCH 2/4] remove debug logging --- src/client.cpp | 128 ++--------------------------------------------- src/peer.cpp | 5 -- src/tb_utils.cpp | 4 +- 3 files changed, 7 insertions(+), 130 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index 5799dd1..6e09141 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -238,14 +238,6 @@ namespace TeslaBLE return 1; } - // Debug: Print the public key - printf("Public key:\n"); - for (size_t i = 0; i < public_key_size; i++) - { - printf("%02x", public_key_buffer[i]); - } - printf("\n"); - mbedtls_ecdh_init(&this->ecdh_context_); return_code = mbedtls_ecdh_get_params( @@ -257,12 +249,6 @@ namespace TeslaBLE printf("ECDH Get Params (private) error: -0x%04x\n\n", (unsigned int)-return_code); return 1; } - printf("Private key :\n"); - for (size_t i = 0; i < 32; i++) - { - printf("%02lx", mbedtls_pk_ec(this->private_key_context_)->private_d.private_p[i]); - } - printf("\n"); return_code = mbedtls_ecdh_get_params(&this->ecdh_context_, &tesla_key, MBEDTLS_ECDH_THEIRS); @@ -285,15 +271,9 @@ namespace TeslaBLE printf("ECDH calc secret error: -0x%04x\n\n", (unsigned int)-return_code); return 1; } - printf("Shared secret before hash: "); - for (size_t i = 0; i < shared_secret_olen; i++) - { - printf("%02x", shared_secret[i]); - } - printf("\n"); // Now hash the shared secret - printf("shared_secret_olen: %u\n", shared_secret_olen); + // printf("shared_secret_olen: %u\n", shared_secret_olen); return_code = mbedtls_sha1(shared_secret, shared_secret_olen, shared_secret_sha1); if (return_code != 0) { @@ -301,13 +281,6 @@ namespace TeslaBLE return 1; } - printf("Shared secret: "); - for (int i = 0; i < sizeof shared_secret_sha1; i++) - { - printf("%02x", shared_secret_sha1[i]); - } - printf("\n"); - if (isInfotainment) { memcpy(this->shared_secret_infotainment_sha1_, shared_secret_sha1, this->SHARED_KEY_SIZE_BYTES); // we only need the first 16 bytes @@ -352,13 +325,6 @@ namespace TeslaBLE pb_byte_t *shared_secret = domain == UniversalMessage_Domain_DOMAIN_INFOTAINMENT ? this->shared_secret_infotainment_sha1_ : this->shared_secret_vcsec_sha1_; size_t shared_secret_size = this->SHARED_KEY_SIZE_BYTES; - // check SHA-1 shared secret should be 16 bytes - printf("Shared secret: "); - for (size_t i = 0; i < shared_secret_size; i++) - { - printf("%02x", shared_secret[i]); - } - printf("\n"); if (shared_secret_size != this->SHARED_KEY_SIZE_BYTES) { printf("\033[1;31mError: Shared secret SHA1 is not 16 bytes (actual size = %u)\033[0m\n", shared_secret_size); @@ -394,13 +360,6 @@ namespace TeslaBLE // Use the hash as the AAD for AES-GCM mbedtls_gcm_update_ad(&aes_context, ad_hash, sizeof(ad_hash)); - // Encrypt the plaintext - printf("Plaintext: "); - for (size_t i = 0; i < input_buffer_length; i++) - { - printf("%02x", input_buffer[i]); - } - printf("\n"); return_code = mbedtls_gcm_update(&aes_context, input_buffer, input_buffer_length, output_buffer, output_buffer_length, output_length); if (return_code != 0) @@ -423,28 +382,6 @@ namespace TeslaBLE mbedtls_gcm_free(&aes_context); - // Debug output - printf("Nonce: "); - for (int i = 0; i < sizeof(this->nonce_); i++) - { - printf("%02x", this->nonce_[i]); - } - printf("\n"); - - printf("Ciphertext: "); - for (size_t i = 0; i < *output_length; i++) - { - printf("%02x", output_buffer[i]); - } - printf("\n"); - - printf("Tag: "); - for (size_t i = 0; i < tag_length; i++) - { - printf("%02x", signature_buffer[i]); - } - printf("\n"); - return 0; } @@ -506,13 +443,6 @@ namespace TeslaBLE size_t *output_length) { printf("Building whitelist message\n"); - printf("Public key size: %d\n", this->public_key_size_); - printf("Public key: "); - for (int i = 0; i < this->public_key_size_; i++) - { - printf("%02x", this->public_key_[i]); - } - printf("\n"); VCSEC_PermissionChange permissions_action = VCSEC_PermissionChange_init_default; @@ -536,7 +466,7 @@ namespace TeslaBLE VCSEC_UnsignedMessage_WhitelistOperation_tag; payload.sub_message.WhitelistOperation = whitelist; - printf("Encoding whitelist message\n"); + // printf("Encoding whitelist message\n"); pb_byte_t payload_buffer[80]; size_t payload_length; int return_code = pb_encode_fields(payload_buffer, &payload_length, VCSEC_UnsignedMessage_fields, &payload); @@ -546,14 +476,7 @@ namespace TeslaBLE return 1; } - printf("Encoded whitelist message :"); - for (int i = 0; i < payload_length; i++) - { - printf("%02x", payload_buffer[i]); - } - printf("\n"); - - printf("Building VCSEC to VCSEC message\n"); + // printf("Building VCSEC to VCSEC message\n"); VCSEC_ToVCSECMessage vcsec_message = VCSEC_ToVCSECMessage_init_default; VCSEC_SignedMessage signed_message = VCSEC_SignedMessage_init_default; vcsec_message.has_signedMessage = true; @@ -565,7 +488,7 @@ namespace TeslaBLE signed_message.protobufMessageAsBytes.size = payload_length; vcsec_message.signedMessage = signed_message; - printf("Encoding VCSEC to VCSEC message\n"); + // printf("Encoding VCSEC to VCSEC message\n"); pb_byte_t vcsec_encode_buffer[86]; size_t vcsec_encode_buffer_size; return_code = pb_encode_fields(vcsec_encode_buffer, &vcsec_encode_buffer_size, VCSEC_ToVCSECMessage_fields, &vcsec_message); @@ -574,15 +497,8 @@ namespace TeslaBLE printf("Failed to encode VCSEC to VCSEC message\n"); return 1; } - printf("Encoded VCSEC to VCSEC message length: %d\n", vcsec_encode_buffer_size); - printf("Encoded VCSEC to VCSEC message :"); - for (int i = 0; i < vcsec_encode_buffer_size; i++) - { - printf("%02x", vcsec_encode_buffer[i]); - } - printf("\n"); - printf("Prepending length\n"); + // printf("Prepending length\n"); this->prependLength(vcsec_encode_buffer, vcsec_encode_buffer_size, output_buffer, output_length); return 0; @@ -717,28 +633,15 @@ namespace TeslaBLE { size_t index = 0; - // Helper function for debug printing - auto debug_print = [](const char *label, const uint8_t *data, size_t length) - { - printf("%s: ", label); - for (size_t i = 0; i < length; i++) - { - printf("%02x", data[i]); - } - printf("\n"); - }; - // Signature type output_buffer[index++] = Signatures_Tag_TAG_SIGNATURE_TYPE; output_buffer[index++] = 0x01; output_buffer[index++] = signature_type; - debug_print("Signature type", output_buffer, 3); // Domain output_buffer[index++] = Signatures_Tag_TAG_DOMAIN; output_buffer[index++] = 0x01; output_buffer[index++] = domain; - debug_print("Domain", output_buffer + 3, 3); // Personalization (VIN) size_t vin_length = strlen(VIN); @@ -746,34 +649,28 @@ namespace TeslaBLE output_buffer[index++] = vin_length; memcpy(output_buffer + index, VIN, vin_length); index += vin_length; - debug_print("VIN", output_buffer + 6, vin_length + 2); // Epoch output_buffer[index++] = Signatures_Tag_TAG_EPOCH; output_buffer[index++] = 0x10; // Assuming epoch is always 16 bytes memcpy(output_buffer + index, epoch, 16); index += 16; - debug_print("Epoch", output_buffer + index - 18, 18); // Expires at - printf("Expires at: %ld\n", expires_at); output_buffer[index++] = Signatures_Tag_TAG_EXPIRES_AT; output_buffer[index++] = 0x04; output_buffer[index++] = (expires_at >> 24) & 0xFF; output_buffer[index++] = (expires_at >> 16) & 0xFF; output_buffer[index++] = (expires_at >> 8) & 0xFF; output_buffer[index++] = expires_at & 0xFF; - debug_print("Expires at", output_buffer + index - 6, 6); // Counter - printf("Counter: %ld\n", counter); output_buffer[index++] = Signatures_Tag_TAG_COUNTER; output_buffer[index++] = 0x04; output_buffer[index++] = (counter >> 24) & 0xFF; output_buffer[index++] = (counter >> 16) & 0xFF; output_buffer[index++] = (counter >> 8) & 0xFF; output_buffer[index++] = counter & 0xFF; - debug_print("Counter", output_buffer + index - 6, 6); // Terminal byte output_buffer[index++] = Signatures_Tag_TAG_END; @@ -786,14 +683,6 @@ namespace TeslaBLE *output_length = index; - // Final debug output - printf("Complete AD Buffer: "); - for (size_t i = 0; i < index; i++) - { - printf("%02x", output_buffer[i]); - } - printf("\n"); - return 0; } @@ -1031,13 +920,6 @@ namespace TeslaBLE printf("\033[1;31mFailed to encode car action message\033[0m\n"); return 1; } - printf("payload length: %zu\n", payload_length); - printf("payload: "); - for (int i = 0; i < payload_length; i++) - { - printf("%02x", payload_buffer[i]); - } - printf("\n"); // build universal message return this->buildUniversalMessageWithPayload( diff --git a/src/peer.cpp b/src/peer.cpp index 6dba6a4..6190fc6 100644 --- a/src/peer.cpp +++ b/src/peer.cpp @@ -1,6 +1,4 @@ #include -#include -#include #include #include "peer.h" @@ -23,18 +21,15 @@ namespace TeslaBLE } void Peer::setExpiresAt(const uint32_t *expires_at) { - printf("Expires at: %lu\n", *expires_at); this->expires_at_ = *expires_at; } uint32_t Peer::generateExpiresAt(int seconds) { uint32_t expiresAt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now() + std::chrono::seconds(seconds)) - this->time_zero_; - printf("Generated expiration time: %lu\n", expiresAt); return expiresAt; } void Peer::setTimeZero(const uint32_t *time_zero) { - printf("TimeZero: %lu\n", *time_zero); this->time_zero_ = *time_zero; } void Peer::setIsAuthenticated(bool isAuthenticated) diff --git a/src/tb_utils.cpp b/src/tb_utils.cpp index 7ea40a6..d6ee5a4 100644 --- a/src/tb_utils.cpp +++ b/src/tb_utils.cpp @@ -94,7 +94,7 @@ namespace TeslaBLE PB_GET_ERROR(&unsigned_message_size_stream)); return 1; } - printf("Bytes written: %zu\n", unsigned_message_size_stream.bytes_written); + // printf("Bytes written: %zu\n", unsigned_message_size_stream.bytes_written); if (unsigned_message_size_stream.bytes_written == 0) { printf("\033[1;31mNo bytes written\033[0m\n"); @@ -104,7 +104,7 @@ namespace TeslaBLE // printf("Message size: %hhu\n", *output_length); // now encode proper - printf("Encoding message\n"); + // printf("Encoding message\n"); pb_ostream_t unsigned_message_stream = pb_ostream_from_buffer(output_buffer, *output_length); bool status_encode_bytes = pb_encode(&unsigned_message_stream, fields, src_struct); if (!status_encode_bytes) From bd52dc305a3ec930b1929ede3dcc400930e67f12 Mon Sep 17 00:00:00 2001 From: yoziru Date: Fri, 26 Jul 2024 03:31:47 +0200 Subject: [PATCH 3/4] tb_utils: remove unused hex and dump methods --- include/tb_utils.h | 5 ---- src/client.cpp | 7 ----- src/tb_utils.cpp | 72 ---------------------------------------------- 3 files changed, 84 deletions(-) diff --git a/include/tb_utils.h b/include/tb_utils.h index 0181c66..b09c053 100644 --- a/include/tb_utils.h +++ b/include/tb_utils.h @@ -6,11 +6,6 @@ namespace TeslaBLE { - std::string uint8ToHexString(const uint8_t *v, size_t s); - uint8_t *hexStrToUint8(const char *string); - void dumpBuffer(const char *title, pb_byte_t *buf, size_t len); - void dumpHexBuffer(const char *title, pb_byte_t *buf, size_t len); - int pb_encode_fields( pb_byte_t *output_buffer, size_t *output_length, diff --git a/src/client.cpp b/src/client.cpp index 6e09141..eab5d57 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -395,8 +395,6 @@ namespace TeslaBLE mbedtls_ecp_keypair_free(&this->tesla_key_infotainment_); mbedtls_ecdh_free(&this->ecdh_context_); mbedtls_ctr_drbg_free(&this->drbg_context_); - // mbedtls_ecp_point_free(&Q); - // mbedtls_mpi_free(&d); } /* @@ -710,9 +708,6 @@ namespace TeslaBLE universal_message.has_from_destination = true; universal_message.from_destination = from_destination; - // char routing_address[33]; - // strcpy(routing_address, "2c907bd76c640d360b3027dc7404efde"); - universal_message.which_payload = UniversalMessage_RoutableMessage_protobuf_message_as_bytes_tag; if (encryptPayload) { @@ -827,7 +822,6 @@ namespace TeslaBLE universal_message.which_payload = UniversalMessage_RoutableMessage_session_info_request_tag; UniversalMessage_SessionInfoRequest session_info_request = UniversalMessage_SessionInfoRequest_init_default; - // printf("public key size: %d\n", this->public_key_size_); memcpy(session_info_request.public_key.bytes, this->public_key_, this->public_key_size_); session_info_request.public_key.size = this->public_key_size_; universal_message.payload.session_info_request = session_info_request; @@ -838,7 +832,6 @@ namespace TeslaBLE { uuid[i] = rand() % 256; } - // mbedtls_ctr_drbg_random(&this->drbg_context_, uuid, sizeof(uuid)); memcpy(universal_message.uuid, uuid, sizeof(uuid)); size_t universal_encode_buffer_size = this->MAX_BLE_MESSAGE_SIZE - 2; diff --git a/src/tb_utils.cpp b/src/tb_utils.cpp index d6ee5a4..08e087a 100644 --- a/src/tb_utils.cpp +++ b/src/tb_utils.cpp @@ -8,84 +8,12 @@ namespace TeslaBLE { - uint8_t *hexStrToUint8(const char *string) - { - if (string == NULL) - return NULL; - - size_t slength = strlen(string); - if ((slength % 2) != 0) // must be even - return NULL; - - size_t dlength = slength / 2; - uint8_t *data = (uint8_t *)malloc(dlength); - memset(data, 0, dlength); - size_t index = 0; - - while (index < slength) - { - char c = string[index]; - int value = 0; - if (c >= '0' && c <= '9') - value = (c - '0'); - else if (c >= 'A' && c <= 'F') - value = (10 + (c - 'A')); - else if (c >= 'a' && c <= 'f') - value = (10 + (c - 'a')); - else - return NULL; - - data[(index / 2)] += value << (((index + 1) % 2) * 4); - index++; - } - - return data; - } - - std::string uint8ToHexString(const uint8_t *v, size_t s) - { - std::stringstream stream; - stream << std::hex << std::setfill('0'); - for (int i = 0; i < s; i++) - { - stream << std::hex << std::setw(2) << static_cast(v[i]); - } - return stream.str(); - } - - void dumpHexBuffer(const char *title, pb_byte_t *buf, size_t len) - { - size_t i = 0; - printf("\n%s", title); - for (i = 0; i < len; i++) - { - printf("%c%c", "0123456789ABCDEF"[buf[i] / 16], - "0123456789ABCDEF"[buf[i] % 16]); - } - printf("\n"); - } - - void dumpBuffer(const char *title, pb_byte_t *buf, size_t len) - { - size_t i = 0; - printf("\n%s", title); - for (i = 0; i < len; i++) - { - printf("%c", buf[i]); - } - printf("\n"); - } - int pb_encode_fields( pb_byte_t *output_buffer, size_t *output_length, const pb_msgdesc_t *fields, const void *src_struct) { - // pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct) - // first get message length - // printf("Getting message length\n"); - pb_ostream_t unsigned_message_size_stream = {nullptr, 0, 0, 0, nullptr}; bool status_encode_length = pb_encode(&unsigned_message_size_stream, fields, src_struct); if (!status_encode_length) From 4d6ca08e2b1b1a4ba89aec7533449f63017885bf Mon Sep 17 00:00:00 2001 From: yoziru Date: Fri, 26 Jul 2024 11:22:15 +0200 Subject: [PATCH 4/4] bump 2.1.0 --- CMakeLists.txt | 2 +- library.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1470035..d814533 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.22) include(FetchContent) project(TeslaBLE - VERSION 2.0.3 + VERSION 2.1.0 DESCRIPTION "CPP Tesla BLE Library" LANGUAGES CXX C ) diff --git a/library.json b/library.json index 16c0af4..28cd788 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "TeslaBLE", - "version": "2.0.3", + "version": "2.1.0", "description": "This CPP library facilitates direct communication with Tesla vehicles via the BLE API. It offers fundamental features such as unlocking/locking, opening the trunk, and more. The library's capabilities are contingent on the range of actions implemented by Tesla, which is the only limitation at present.", "keywords": "tesla, ble", "repository": {