Astarte Device SDK ESP32
ESP32 device SDK for the Astarte platform
astarte_credentials.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2018-2023, SECO Mind Srl
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later OR Apache-2.0
5  */
6 
12 #ifndef _ASTARTE_CREDENTIALS_H_
13 #define _ASTARTE_CREDENTIALS_H_
14 
15 #include "astarte.h"
16 
17 #include <stdbool.h>
18 #include <string.h>
19 
20 #define CERT_LENGTH 4096
21 #define CN_LENGTH 512
22 
23 #define ASTARTE_CREDENTIALS_DEFAULT_NVS_PARTITION NULL
24 
25 enum credential_type_t
26 {
27  ASTARTE_CREDENTIALS_CSR = 1,
28  ASTARTE_CREDENTIALS_KEY,
29  ASTARTE_CREDENTIALS_CERTIFICATE
30 } __attribute__((deprecated("Please use the typedef credential_type_t")));
31 
32 #pragma GCC diagnostic push
33 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
34 typedef enum credential_type_t credential_type_t;
35 #pragma GCC diagnostic pop
36 
37 typedef astarte_err_t (*astarte_credentials_store_t)(
38  void *opaque, credential_type_t cred_type, const void *credential, size_t length);
39 typedef astarte_err_t (*astarte_credentials_fetch_t)(
40  void *opaque, credential_type_t cred_type, char *out, size_t length);
41 typedef bool (*astarte_credentials_exists_t)(void *opaque, credential_type_t cred_type);
42 typedef astarte_err_t (*astarte_credentials_remove_t)(void *opaque, credential_type_t cred_type);
43 
44 typedef struct
45 {
46  astarte_credentials_store_t astarte_credentials_store;
47  astarte_credentials_fetch_t astarte_credentials_fetch;
48  astarte_credentials_exists_t astarte_credentials_exists;
49  astarte_credentials_remove_t astarte_credentials_remove;
51 
52 typedef struct
53 {
55  void *opaque;
57 
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 
70 
82 
91 
98 
109 
120 
131 
141 
151 astarte_err_t astarte_credentials_get_csr(char *out, size_t length);
152 
163 
175  const char *cert_pem, char *out, size_t length);
176 
186 astarte_err_t astarte_credentials_get_key(char *out, size_t length);
187 
199 
210 
221 
229 
237 
245 
246 /*
247  * @brief store a credential using filesystem storage
248  *
249  * @details this API might change in future versions.
250  */
251 astarte_err_t astarte_credentials_store(
252  void *opaque, credential_type_t cred_type, const void *credential, size_t length);
253 
254 /*
255  * @brief fetch a credential using filesystem storage
256  *
257  * @details this API might change in future versions.
258  */
259 astarte_err_t astarte_credentials_fetch(
260  void *opaque, credential_type_t cred_type, char *out, size_t length);
261 
262 /*
263  * @brief return true whether a credential exists on fileystem storage
264  *
265  * @details this API might change in future versions.
266  */
267 bool astarte_credentials_exists(void *opaque, credential_type_t cred_type);
268 
269 /*
270  * @brief remove a credential from filesystem storage
271  *
272  * @details this API might change in future versions.
273  */
274 astarte_err_t astarte_credentials_remove(void *opaque, credential_type_t cred_type);
275 
276 /*
277  * @brief store a credential using NVS
278  *
279  * @details this API might change in future versions.
280  */
281 astarte_err_t astarte_credentials_nvs_store(
282  void *opaque, credential_type_t cred_type, const void *credential, size_t length);
283 
284 /*
285  * @brief fetch a credential using NVS
286  *
287  * @details this API might change in future versions.
288  */
289 astarte_err_t astarte_credentials_nvs_fetch(
290  void *opaque, credential_type_t cred_type, char *out, size_t length);
291 
292 /*
293  * @brief return true whether a credential exists on NVS
294  *
295  * @details this API might change in future versions.
296  */
297 bool astarte_credentials_nvs_exists(void *opaque, credential_type_t cred_type);
298 
299 /*
300  * @brief remove a credential from NVS
301  *
302  * @details this API might change in future versions.
303  */
304 astarte_err_t astarte_credentials_nvs_remove(void *opaque, credential_type_t cred_type);
305 
306 #ifdef __cplusplus
307 }
308 #endif
309 
310 #endif
Astarte types and defines.
astarte_err_t
Astarte return codes.
Definition: astarte.h:28
astarte_err_t astarte_credentials_get_csr(char *out, size_t length)
get the saved CSR
bool astarte_credentials_has_certificate()
check if the certificate exists
astarte_err_t astarte_credentials_create_key()
create Astarte private key.
astarte_err_t astarte_credentials_get_key(char *out, size_t length)
get the private key to connect with the Astarte MQTT v1 protocol
astarte_err_t astarte_credentials_create_csr()
create Astarte CSR to be sent to Pairing API.
bool astarte_credentials_has_key()
check if the private key exists
astarte_err_t astarte_credentials_set_storage_context(astarte_credentials_context_t *creds_context)
replace credentials context.
astarte_err_t astarte_credentials_get_stored_credentials_secret(char *out, size_t length)
get the stored credentials_secret
bool astarte_credentials_has_csr()
check if the CSR exists
astarte_err_t astarte_credentials_set_stored_credentials_secret(const char *credentials_secret)
save the credentials_secret in the NVS
astarte_err_t astarte_credentials_delete_certificate()
delets the saved certificate used to connect with the Astarte MQTT v1 protocol
astarte_err_t astarte_credentials_save_certificate(const char *cert_pem)
save the certificate to connect with the Astarte MQTT v1 protocol
astarte_err_t astarte_credentials_init()
initialize Astarte credentials.
astarte_err_t astarte_credentials_get_certificate(char *out, size_t length)
get the certificate to connect with the Astarte MQTT v1 protocol
astarte_err_t astarte_credentials_erase_stored_credentials_secret()
delete the credentials_secret from the NVS
astarte_err_t astarte_credentials_use_nvs_storage(const char *partition_label)
use a NVS partition as credentials context.
astarte_err_t astarte_credentials_get_certificate_common_name(const char *cert_pem, char *out, size_t length)
get the certificate Common Name
bool astarte_credentials_is_initialized()
check if Astarte credentials are initialized.
Definition: astarte_credentials.h:53
Definition: astarte_credentials.h:45