Skip to content

Commit 1e82430

Browse files
committed
tls: use der format certificate
1 parent d241e66 commit 1e82430

File tree

11 files changed

+218
-179
lines changed

11 files changed

+218
-179
lines changed

core/src/mender-api.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,44 +116,58 @@ mender_api_init(mender_api_config_t *config, mender_api_callbacks_t *callbacks)
116116
}
117117

118118
mender_err_t
119-
mender_api_perform_authentication(char *private_key, char *public_key) {
119+
mender_api_perform_authentication(unsigned char *private_key, size_t private_key_length, unsigned char *public_key, size_t public_key_length) {
120120

121121
assert(NULL != private_key);
122122
assert(NULL != public_key);
123123
mender_err_t ret;
124+
char * public_key_pem = NULL;
124125
char * payload = NULL;
125126
char * response = NULL;
126127
char * signature = NULL;
127128
size_t signature_length = 0;
128129
int status = 0;
129130

131+
/* Convert public key to PEM format */
132+
size_t olen = 0;
133+
mender_tls_pem_write_buffer(public_key, public_key_length, NULL, 0, &olen);
134+
if (NULL == (public_key_pem = (char *)malloc(olen))) {
135+
mender_log_error("Unable to allocate memory");
136+
ret = MENDER_FAIL;
137+
goto END;
138+
}
139+
if (MENDER_OK != (ret = mender_tls_pem_write_buffer(public_key, public_key_length, public_key_pem, olen, &olen))) {
140+
mender_log_error("Unable to convert public key");
141+
goto END;
142+
}
143+
130144
/* Format payload */
131145
if (NULL != mender_api_config.tenant_token) {
132146
if (NULL
133147
== (payload = malloc(strlen("{ \"id_data\": \"{ \\\"mac\\\": \\\"\\\"}\", \"pubkey\": \"\", \"tenant_token\": \"\" }")
134-
+ strlen(mender_api_config.mac_address) + strlen(public_key) + strlen(mender_api_config.tenant_token) + 1))) {
148+
+ strlen(mender_api_config.mac_address) + strlen(public_key_pem) + strlen(mender_api_config.tenant_token) + 1))) {
135149
mender_log_error("Unable to allocate memory");
136150
ret = MENDER_FAIL;
137151
goto END;
138152
}
139153
sprintf(payload,
140154
"{ \"id_data\": \"{ \\\"mac\\\": \\\"%s\\\"}\", \"pubkey\": \"%s\", \"tenant_token\": \"%s\" }",
141155
mender_api_config.mac_address,
142-
public_key,
156+
public_key_pem,
143157
mender_api_config.tenant_token);
144158
} else {
145159
if (NULL
146160
== (payload = malloc(strlen("{ \"id_data\": \"{ \\\"mac\\\": \\\"\\\"}\", \"pubkey\": \"\" }") + strlen(mender_api_config.mac_address)
147-
+ strlen(public_key) + 1))) {
161+
+ strlen(public_key_pem) + 1))) {
148162
mender_log_error("Unable to allocate memory");
149163
ret = MENDER_FAIL;
150164
goto END;
151165
}
152-
sprintf(payload, "{ \"id_data\": \"{ \\\"mac\\\": \\\"%s\\\"}\", \"pubkey\": \"%s\" }", mender_api_config.mac_address, public_key);
166+
sprintf(payload, "{ \"id_data\": \"{ \\\"mac\\\": \\\"%s\\\"}\", \"pubkey\": \"%s\" }", mender_api_config.mac_address, public_key_pem);
153167
}
154168

155169
/* Sign payload */
156-
if (MENDER_OK != (ret = mender_tls_sign_payload(private_key, payload, &signature, &signature_length))) {
170+
if (MENDER_OK != (ret = mender_tls_sign_payload(private_key, private_key_length, payload, &signature, &signature_length))) {
157171
mender_log_error("Unable to sign payload");
158172
goto END;
159173
}
@@ -205,6 +219,9 @@ mender_api_perform_authentication(char *private_key, char *public_key) {
205219
if (NULL != payload) {
206220
free(payload);
207221
}
222+
if (NULL != public_key_pem) {
223+
free(public_key_pem);
224+
}
208225

209226
return ret;
210227
}

core/src/mender-client.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ static mender_client_callbacks_t mender_client_callbacks;
8585
/**
8686
* @brief Authentication keys
8787
*/
88-
static char *mender_client_private_key = NULL;
89-
static char *mender_client_public_key = NULL;
88+
static unsigned char *mender_client_private_key = NULL;
89+
static size_t mender_client_private_key_length = 0;
90+
static unsigned char *mender_client_public_key = NULL;
91+
static size_t mender_client_public_key_length = 0;
9092

9193
/**
9294
* @brief OTA ID and artifact name, used to report OTA status after rebooting
@@ -319,10 +321,12 @@ mender_client_exit(void) {
319321
free(mender_client_private_key);
320322
mender_client_private_key = NULL;
321323
}
324+
mender_client_private_key_length = 0;
322325
if (NULL != mender_client_public_key) {
323326
free(mender_client_public_key);
324327
mender_client_public_key = NULL;
325328
}
329+
mender_client_public_key_length = 0;
326330
if (NULL != mender_client_ota_id) {
327331
free(mender_client_ota_id);
328332
mender_client_ota_id = NULL;
@@ -365,34 +369,28 @@ mender_client_task(void *arg) {
365369
}
366370

367371
/* Retrieve or generate authentication keys if not allready done */
368-
size_t private_key_length = 0;
369-
size_t public_key_length = 0;
370-
if (MENDER_OK != mender_storage_get_authentication_keys(&mender_client_private_key, &private_key_length, &mender_client_public_key, &public_key_length)) {
372+
if (MENDER_OK
373+
!= mender_storage_get_authentication_keys(
374+
&mender_client_private_key, &mender_client_private_key_length, &mender_client_public_key, &mender_client_public_key_length)) {
371375

372376
/* Generate authentication keys */
373377
mender_log_info("Generating authentication keys...");
374378
if (MENDER_OK
375-
!= mender_tls_generate_authentication_keys(&mender_client_private_key, &private_key_length, &mender_client_public_key, &public_key_length)) {
379+
!= mender_tls_generate_authentication_keys(
380+
&mender_client_private_key, &mender_client_private_key_length, &mender_client_public_key, &mender_client_public_key_length)) {
376381
mender_log_error("Unable to generate authentication keys");
377382
goto END;
378383
}
379384

380385
/* Record keys */
381-
if (MENDER_OK != mender_storage_set_authentication_keys(mender_client_private_key, mender_client_public_key)) {
386+
if (MENDER_OK
387+
!= mender_storage_set_authentication_keys(
388+
mender_client_private_key, mender_client_private_key_length, mender_client_public_key, mender_client_public_key_length)) {
382389
mender_log_error("Unable to record authentication keys");
383390
goto END;
384391
}
385392
}
386393

387-
/* Convert public key for usage with mender HTTP API */
388-
char *tmp = mender_utils_str_replace(mender_client_public_key, "\n", "\\n");
389-
if (NULL == tmp) {
390-
mender_log_error("Unable to convert public key");
391-
goto END;
392-
}
393-
free(mender_client_public_key);
394-
mender_client_public_key = tmp;
395-
396394
/* Retrieve OTA ID if it is found (following an update) */
397395
mender_err_t ret;
398396
size_t ota_id_length = 0;
@@ -411,7 +409,9 @@ mender_client_task(void *arg) {
411409
while (true) {
412410

413411
/* Perform authentication with the mender server */
414-
if (MENDER_OK != mender_api_perform_authentication(mender_client_private_key, mender_client_public_key)) {
412+
if (MENDER_OK
413+
!= mender_api_perform_authentication(
414+
mender_client_private_key, mender_client_private_key_length, mender_client_public_key, mender_client_public_key_length)) {
415415

416416
/* Invoke authentication error callback */
417417
if (NULL != mender_client_callbacks.authentication_failure) {

core/src/mender-utils.c

100644100755
Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -25,106 +25,9 @@
2525
* SOFTWARE.
2626
*/
2727

28-
#include <sys/types.h>
29-
#include <regex.h>
3028
#include "mender-log.h"
3129
#include "mender-utils.h"
3230

33-
char *
34-
mender_utils_str_replace(char *input, char *search, char *replace) {
35-
36-
assert(NULL != input);
37-
assert(NULL != search);
38-
assert(NULL != replace);
39-
40-
regex_t regex;
41-
regmatch_t match;
42-
char * str = input;
43-
char * output = NULL;
44-
size_t index = 0;
45-
int previous_match_finish = 0;
46-
47-
/* Compile expression */
48-
if (0 != regcomp(&regex, search, REG_EXTENDED)) {
49-
/* Unable to compile expression */
50-
mender_log_error("Unable to compile expression '%s'", search);
51-
return NULL;
52-
}
53-
54-
/* Loop until all search string are replaced */
55-
bool loop = true;
56-
while (true == loop) {
57-
58-
/* Search wanted string */
59-
if (0 != regexec(&regex, str, 1, &match, 0)) {
60-
/* No more string to be replaced */
61-
loop = false;
62-
} else {
63-
if (match.rm_so != -1) {
64-
65-
/* Beginning and ending offset of the match */
66-
int current_match_start = (int)(match.rm_so + (str - input));
67-
int current_match_finish = (int)(match.rm_eo + (str - input));
68-
69-
/* Reallocate output memory */
70-
char *tmp = (char *)realloc(output, index + (current_match_start - previous_match_finish) + 1);
71-
if (NULL == tmp) {
72-
/* Unable to allocate memory */
73-
regfree(&regex);
74-
free(output);
75-
mender_log_error("Unable to allocate memory");
76-
return NULL;
77-
}
78-
output = tmp;
79-
80-
/* Copy string from previous match to the beginning of the current match */
81-
memcpy(&output[index], &input[previous_match_finish], current_match_start - previous_match_finish);
82-
index += (current_match_start - previous_match_finish);
83-
output[index] = 0;
84-
85-
/* Reallocate output memory */
86-
if (NULL == (tmp = (char *)realloc(output, index + strlen(replace) + 1))) {
87-
/* Unable to allocate memory */
88-
regfree(&regex);
89-
free(output);
90-
mender_log_error(NULL, "Unable to allocate memory");
91-
return NULL;
92-
}
93-
output = tmp;
94-
95-
/* Copy replace string to the output */
96-
strcat(output, replace);
97-
index += strlen(replace);
98-
99-
/* Update previous match ending value */
100-
previous_match_finish = current_match_finish;
101-
}
102-
str += match.rm_eo;
103-
}
104-
}
105-
106-
/* Reallocate output memory */
107-
char *tmp = (char *)realloc(output, index + (strlen(input) - previous_match_finish) + 1);
108-
if (NULL == tmp) {
109-
/* Unable to allocate memory */
110-
regfree(&regex);
111-
free(output);
112-
mender_log_error(NULL, "Unable to allocate memory");
113-
return NULL;
114-
}
115-
output = tmp;
116-
117-
/* Copy the end of the string after the latest match */
118-
memcpy(&output[index], &input[previous_match_finish], strlen(input) - previous_match_finish);
119-
index += (strlen(input) - previous_match_finish);
120-
output[index] = 0;
121-
122-
/* Release regex */
123-
regfree(&regex);
124-
125-
return output;
126-
}
127-
12831
char *
12932
mender_utils_http_status_to_string(int status) {
13033

include/mender-api.h

100644100755
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ mender_err_t mender_api_init(mender_api_config_t *config, mender_api_callbacks_t
6262
/**
6363
* @brief Perform authentication of the device, retrieve token from mender-server used for the next requests
6464
* @param private_key Client private key used for authentication
65+
* @param private_key_length Private key length
6566
* @param public_key Client public key used for authentication
67+
* @param public_key_length Public key length
6668
* @return MENDER_OK if the function succeeds, error code otherwise
6769
*/
68-
mender_err_t mender_api_perform_authentication(char *private_key, char *public_key);
70+
mender_err_t mender_api_perform_authentication(unsigned char *private_key, size_t private_key_length, unsigned char *public_key, size_t public_key_length);
6971

7072
/**
7173
* @brief Publish inventory data of the device to the mender-server

include/mender-storage.h

100755100644
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mender_err_t mender_storage_init(void);
3838

3939
/**
4040
* @brief Erase authentication keys
41-
* @return ESP_OK if the function succeeds, error code otherwise
41+
* @return MENDER_OK if the function succeeds, error code otherwise
4242
*/
4343
mender_err_t mender_storage_erase_authentication_keys(void);
4444

@@ -50,15 +50,20 @@ mender_err_t mender_storage_erase_authentication_keys(void);
5050
* @param public_key_lentgh Public key length from storage, 0 if not found
5151
* @return MENDER_OK if the function succeeds, error code otherwise
5252
*/
53-
mender_err_t mender_storage_get_authentication_keys(char **private_key, size_t *private_key_length, char **public_key, size_t *public_key_length);
53+
mender_err_t mender_storage_get_authentication_keys(unsigned char **private_key,
54+
size_t * private_key_length,
55+
unsigned char **public_key,
56+
size_t * public_key_length);
5457

5558
/**
5659
* @brief Set authentication keys
5760
* @param private_key Private key to store
61+
* @param private_key_lentgh Private key length
5862
* @param public_key Public key to store
63+
* @param public_key_lentgh Public key length
5964
* @return MENDER_OK if the function succeeds, error code otherwise
6065
*/
61-
mender_err_t mender_storage_set_authentication_keys(char *private_key, char *public_key);
66+
mender_err_t mender_storage_set_authentication_keys(unsigned char *private_key, size_t private_key_length, unsigned char *public_key, size_t public_key_length);
6267

6368
/**
6469
* @brief Get OTA deployment

include/mender-tls.h

100755100644
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,35 @@ mender_err_t mender_tls_init(void);
4242
* @param private_key_length Private key lenght
4343
* @param public_key Public key generated
4444
* @param public_key_length Public key lenght
45-
* @return ESP_OK if the function succeeds, error code otherwise
45+
* @return MENDER_OK if the function succeeds, error code otherwise
46+
*/
47+
mender_err_t mender_tls_generate_authentication_keys(unsigned char **private_key,
48+
size_t * private_key_length,
49+
unsigned char **public_key,
50+
size_t * public_key_length);
51+
52+
/**
53+
* @brief Write a buffer of PEM information from a DER encoded buffer
54+
* @note This function is derived from mbedtls_pem_write_buffer with const header and footer, and line feed is "\\n"
55+
* @param der_data The DER data to encode
56+
* @param der_len The length of the DER data
57+
* @param buf The buffer to write to
58+
* @param buf_len The length of the output buffer
59+
* @param olen The address at which to store the total length written or required output buffer length is not enough
60+
* @return MENDER_OK if the function succeeds, error code otherwise
4661
*/
47-
mender_err_t mender_tls_generate_authentication_keys(char **private_key, size_t *private_key_length, char **public_key, size_t *public_key_length);
62+
mender_err_t mender_tls_pem_write_buffer(const unsigned char *der_data, size_t der_len, char *buf, size_t buf_len, size_t *olen);
4863

4964
/**
5065
* @brief Sign payload
5166
* @param private_key Private key
67+
* @param private_key_length Private key length
5268
* @param payload Payload to sign
5369
* @param signature Signature of the payload
5470
* @param signature_length Length of the signature buffer, updated to the length of the signature
55-
* @return ESP_OK if the function succeeds, error code otherwise
71+
* @return MENDER_OK if the function succeeds, error code otherwise
5672
*/
57-
mender_err_t mender_tls_sign_payload(char *private_key, char *payload, char **signature, size_t *signature_length);
73+
mender_err_t mender_tls_sign_payload(unsigned char *private_key, size_t private_key_length, char *payload, char **signature, size_t *signature_length);
5874

5975
/**
6076
* @brief Release mender TLS

include/mender-utils.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,6 @@
3030

3131
#include "mender-common.h"
3232

33-
/**
34-
* @brief Function used to replace a string in the input buffer
35-
* @param input Input buffer
36-
* @param search String to be replaced or regex expression
37-
* @param replace Replacement string
38-
* @return New string with replacements if the function succeeds, NULL otherwise
39-
*/
40-
char *mender_utils_str_replace(char *input, char *search, char *replace);
41-
4233
/**
4334
* @brief Function used to print HTTP status as string
4435
* @param status HTTP status code

0 commit comments

Comments
 (0)