Skip to content
This repository was archived by the owner on Jul 22, 2022. It is now read-only.

While Generating SAS token issue #140

Open
VamsidharP39 opened this issue Jul 5, 2022 · 1 comment
Open

While Generating SAS token issue #140

VamsidharP39 opened this issue Jul 5, 2022 · 1 comment

Comments

@VamsidharP39
Copy link

While Generating the SAS token, the variable sas_token is not fully populated.
Even i kept delay to populate the variable sas_token

Using ESP8266: 2.7.4, Azure SDk for C 1.0.0-beta 4,

I am getting the variable not printed or populated fully.

static int generateSasToken(char* sas_token, size_t size, const char* device_key_d)
{
az_span signature_span = az_span_create((uint8_t*)signature, sizeofarray(signature));
az_span out_signature_span;
Serial.print(F("Free heap:"));
Serial.println(ESP.getFreeHeap());
az_span encrypted_signature_span
= az_span_create((uint8_t*)encrypted_signature, sizeofarray(encrypted_signature));
delay(5000);
printf("\n SAS token0: %s", encrypted_signature_span);
uint32_t expiration = getSecondsSinceEpoch() + ONE_HOUR_IN_SECS;

// Get signature
if (az_result_failed(az_iot_hub_client_sas_get_signature(
&client, expiration, signature_span, &out_signature_span)))
{
Serial.println("Failed getting SAS signature");
return 1;
}
delay(5000);
printf("\n SAS token1: %s", out_signature_span);
// Base64-decode device key
int base64_decoded_device_key_length
= base64_decode_chars(device_key_d, strlen(device_key_d), base64_decoded_device_key);

if (base64_decoded_device_key_length == 0)
{
Serial.println("Failed base64 decoding device key");
return 1;
}

// SHA-256 encrypt
br_hmac_key_context kc;
br_hmac_key_init(
&kc, &br_sha256_vtable, base64_decoded_device_key, base64_decoded_device_key_length);

br_hmac_context hmac_ctx;
br_hmac_init(&hmac_ctx, &kc, 32);
br_hmac_update(&hmac_ctx, az_span_ptr(out_signature_span), az_span_size(out_signature_span));
br_hmac_out(&hmac_ctx, encrypted_signature);

// Base64 encode encrypted signature
String b64enc_hmacsha256_signature = base64::encode(encrypted_signature, br_hmac_size(&hmac_ctx));

az_span b64enc_hmacsha256_signature_span = az_span_create(
(uint8_t*)b64enc_hmacsha256_signature.c_str(), b64enc_hmacsha256_signature.length());

// URl-encode base64 encoded encrypted signature
if (az_result_failed(az_iot_hub_client_sas_get_password(
&client,
expiration,
b64enc_hmacsha256_signature_span,
AZ_SPAN_EMPTY,
sas_token,
size,
NULL)))
{
Serial.println("Failed getting SAS token");
return 1;
}
delay(5000);
printf("\n SAS token2: %s", sas_token);
Serial.println("SAS token generated!! ");
return 0;
}

IP address:
192.168.1.20
Setting time using SNTP...............pm open,type:2 0
............done!
client initialize done!!
Free heap:12328

SAS token0:
SAS token1: SmartStokIotHub2018Aug21.azure-devices.net%2Fdevices%2Fcalibration
1657001749
SAS token2: SharedAccessSignature sr=SmartStokIotHub2018Aug21.azure-devices.net%2Fdevices%2Fcalibration&sig=L5XIcs20mDZ2hxuVW0KSAS token generated!!
In else before calling connectToAzureIoTHub
Client ID: calibration
Username: SmartStokIotHub2018Aug21.azure-devices.net/calibration/?api-version=2020-09-30&DeviceClientType=c/1.3.1(ard;esp8266)
MQTT connecting ... ebPjQn%2F9Cb7Niv6wcoa3AnXI%3D&se=1657001749
SAS token: SharedAccessSignature sr=SmartStokIotHub2018Aug21.azure-devices.net%2Fdevices%2Fcalibration&sig=L5XIcs20mDZ2hxuVW0Ke

@VamsidharP39
Copy link
Author

Closing the issue as it got resolved.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant