Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup #4

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion include/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 0 additions & 5 deletions include/tb_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
137 changes: 6 additions & 131 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
Expand All @@ -285,29 +271,16 @@ 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)
{
printf("SHA 1 error: -0x%04x\n\n", (unsigned int)-return_code);
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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}

Expand All @@ -458,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);
}

/*
Expand Down Expand Up @@ -506,13 +441,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;
Expand All @@ -536,7 +464,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);
Expand All @@ -546,14 +474,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;
Expand All @@ -565,7 +486,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);
Expand All @@ -574,15 +495,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;
Expand Down Expand Up @@ -717,63 +631,44 @@ 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);
output_buffer[index++] = Signatures_Tag_TAG_PERSONALIZATION;
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;
Expand All @@ -786,14 +681,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;
}

Expand Down Expand Up @@ -821,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)
{
Expand Down Expand Up @@ -918,7 +802,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)
{
Expand All @@ -938,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;
Expand All @@ -949,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;
Expand Down Expand Up @@ -1031,13 +913,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(
Expand Down
5 changes: 0 additions & 5 deletions src/peer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include <chrono>
#include <cstdio>
#include <cstring>
#include <pb.h>

#include "peer.h"
Expand All @@ -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)
Expand Down
Loading