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, Ispirata Srl, info@ispirata.com.
3  *
4  * SPDX-License-Identifier: LGPL-2.1+ 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 };
31 
32 typedef astarte_err_t (*astarte_credentials_store_t)(
33  void *opaque, enum credential_type_t cred_type, const void *credential, size_t length);
34 typedef astarte_err_t (*astarte_credentials_fetch_t)(
35  void *opaque, enum credential_type_t cred_type, char *out, size_t length);
36 typedef bool (*astarte_credentials_exists_t)(void *opaque, enum credential_type_t cred_type);
37 typedef astarte_err_t (*astarte_credentials_remove_t)(
38  void *opaque, enum credential_type_t cred_type);
39 
40 typedef struct
41 {
42  astarte_credentials_store_t astarte_credentials_store;
43  astarte_credentials_fetch_t astarte_credentials_fetch;
44  astarte_credentials_exists_t astarte_credentials_exists;
45  astarte_credentials_remove_t astarte_credentials_remove;
47 
48 typedef struct
49 {
51  void *opaque;
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
66 
78 
87 
94 
105 
116 
127 
137 
147 astarte_err_t astarte_credentials_get_csr(char *out, size_t length);
148 
159 
171  const char *cert_pem, char *out, size_t length);
172 
182 astarte_err_t astarte_credentials_get_key(char *out, size_t length);
183 
195 
206 
217 
225 
233 
241 
242 /*
243  * @brief store a credential using filesystem storage
244  *
245  * @details this API might change in future versions.
246  */
247 astarte_err_t astarte_credentials_store(
248  void *opaque, enum credential_type_t cred_type, const void *credential, size_t length);
249 
250 /*
251  * @brief fetch a credential using filesystem storage
252  *
253  * @details this API might change in future versions.
254  */
255 astarte_err_t astarte_credentials_fetch(
256  void *opaque, enum credential_type_t cred_type, char *out, size_t length);
257 
258 /*
259  * @brief return true whether a credential exists on fileystem storage
260  *
261  * @details this API might change in future versions.
262  */
263 bool astarte_credentials_exists(void *opaque, enum credential_type_t cred_type);
264 
265 /*
266  * @brief remove a credential from filesystem storage
267  *
268  * @details this API might change in future versions.
269  */
270 astarte_err_t astarte_credentials_remove(void *opaque, enum credential_type_t cred_type);
271 
272 /*
273  * @brief store a credential using NVS
274  *
275  * @details this API might change in future versions.
276  */
277 astarte_err_t astarte_credentials_nvs_store(
278  void *opaque, enum credential_type_t cred_type, const void *credential, size_t length);
279 
280 /*
281  * @brief fetch a credential using NVS
282  *
283  * @details this API might change in future versions.
284  */
285 astarte_err_t astarte_credentials_nvs_fetch(
286  void *opaque, enum credential_type_t cred_type, char *out, size_t length);
287 
288 /*
289  * @brief return true whether a credential exists on NVS
290  *
291  * @details this API might change in future versions.
292  */
293 bool astarte_credentials_nvs_exists(void *opaque, enum credential_type_t cred_type);
294 
295 /*
296  * @brief remove a credential from NVS
297  *
298  * @details this API might change in future versions.
299  */
300 astarte_err_t astarte_credentials_nvs_remove(void *opaque, enum credential_type_t cred_type);
301 
302 #ifdef __cplusplus
303 }
304 #endif
305 
306 #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:49
Definition: astarte_credentials.h:41