This repository was archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathiotc_bsp_tls.h
175 lines (154 loc) · 5.9 KB
/
iotc_bsp_tls.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Copyright 2018-2020 Google LLC
*
* This is part of the Google Cloud IoT Device SDK for Embedded C.
* It is licensed under the BSD 3-Clause license; you may not use this file
* except in compliance with the License.
*
* You may obtain a copy of the License at:
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __IOTC_BSP_TLS_H__
#define __IOTC_BSP_TLS_H__
/**
* @file iotc_bsp_tls.h
* @brief Implements Transport Layer Security (TLS).
*
* @details All TLS functions are non-blocking.
*/
#include <stddef.h>
#include <stdint.h>
/**
* @typedef iotc_bsp_io_net_state_t
* @brief The TLS function states.
*
* @see iotc_bsp_io_net_state_e
*/
typedef enum iotc_bsp_tls_state_e {
/** The TLS function succeeded. */
IOTC_BSP_TLS_STATE_OK = 0,
/** Can't initialize TLS library. */
IOTC_BSP_TLS_STATE_INIT_ERROR = 1,
/** Can't validate CA certificate. */
IOTC_BSP_TLS_STATE_CERT_ERROR = 2,
/** Can't complete TLS handshake. */
IOTC_BSP_TLS_STATE_CONNECT_ERROR = 3,
/** TLS handshake is partially complete. Run the function again to read the
remaining data. */
IOTC_BSP_TLS_STATE_WANT_READ = 4,
/** TLS handshake is partially complete. Run the function again to write the
remaining data. */
IOTC_BSP_TLS_STATE_WANT_WRITE = 5,
/** Can't read data. */
IOTC_BSP_TLS_STATE_READ_ERROR = 6,
/** Can't write data. */
IOTC_BSP_TLS_STATE_WRITE_ERROR = 7,
} iotc_bsp_tls_state_t;
/**
* @typedef iotc_bsp_tls_init_params_t
* @brief The TLS context parameters.
* @see #iotc_bsp_tls_init_params_s
*
* @struct iotc_bsp_tls_init_params_s
* @brief The TLS context parameters.
*/
typedef struct iotc_bsp_tls_init_params_s {
/** Callback context. */
void* libiotc_io_callback_context;
/** A pointer to a buffer with root CA PEM certificates. */
uint8_t* ca_cert_pem_buf;
/** The length, in bytes, of ca_cert_pem_buf. */
size_t ca_cert_pem_buf_length;
/** A pointer to the client application's memory allocation function. */
void* (*fp_libiotc_alloc)(size_t);
/** A pointer to the client application's array allocation function. */
void* (*fp_libiotc_calloc)(size_t, size_t);
/** A pointer to the client application's reallocation function. */
void* (*fp_libiotc_realloc)(void*, size_t);
/** A pointer to the client application's free memory function. */
void (*fp_libiotc_free)(void*);
/** A pointer to the host's domain name. The host's domain name must be a
* null-terminated string. */
const char* domain_name;
} iotc_bsp_tls_init_params_t;
/**
* @typedef iotc_bsp_tls_context_t
* @brief The TLS context.
*/
typedef void iotc_bsp_tls_context_t;
/**
* @brief Initializes a TLS library and creates a TLS context.
*
* The SDK calls the function and then deletes init_params, so store persistant
* data outside the function scope.
*
* @param [out] tls_context A pointer to
* {@link ::iotc_bsp_tls_context_t the TLS context}.
* @param [in] init_params The
* {@link ::iotc_bsp_tls_init_params_t TLS context parameters}.
*/
iotc_bsp_tls_state_t iotc_bsp_tls_init(iotc_bsp_tls_context_t** tls_context,
iotc_bsp_tls_init_params_t* init_params);
/**
* @brief Frees a TLS context from memory and deletes any associated data.
*
* @param [out] tls_context A pointer to
* {@link ::iotc_bsp_tls_context_t the TLS context}.
*
* @retval IOTC_BSP_TLS_STATE_OK TLS context successfully freed.
*/
iotc_bsp_tls_state_t iotc_bsp_tls_cleanup(iotc_bsp_tls_context_t** tls_context);
/**
* @brief Starts a TLS handshake.
*
* @param [out] tls_context A pointer to
* {@link ::iotc_bsp_tls_context_t the TLS context}.
*/
iotc_bsp_tls_state_t iotc_bsp_tls_connect(iotc_bsp_tls_context_t* tls_context);
/**
* @brief Reads data on a socket.
*
* @param [out] tls_context A pointer to
* {@link ::iotc_bsp_tls_context_t the TLS context}.
* @param [in] data_ptr A pointer to a buffer to store data that is read.
* @param [in] data_size The size, in bytes, of the buffer to which data_ptr
* points.
* @param [out] bytes_read The number of bytes read.
*/
iotc_bsp_tls_state_t iotc_bsp_tls_read(iotc_bsp_tls_context_t* tls_context,
uint8_t* data_ptr, size_t data_size,
int* bytes_read);
/**
* @brief Gets the pending readable bytes.
*
* @param [out] tls_context A pointer to
* {@link ::iotc_bsp_tls_context_t the TLS context}.
*/
int iotc_bsp_tls_pending(iotc_bsp_tls_context_t* tls_context);
/**
* @brief Write data to a socket.
*
* @param [out] tls_context A pointer to
* {@link ::iotc_bsp_tls_context_t the TLS context}.
* @param [in] data_ptr A pointer to a buffer with the data to be sent.
* @param [in] data_size The size, in bytes, of the buffer to which
* {@code data_ptr} points.
* @param [out] bytes_written The number of bytes written.
*/
iotc_bsp_tls_state_t iotc_bsp_tls_write(iotc_bsp_tls_context_t* tls_context,
uint8_t* data_ptr, size_t data_size,
int* bytes_written);
/** @details Notifies the client application to read data on a socket.
* Implemented in the SDK; don't modify this function. */
iotc_bsp_tls_state_t iotc_bsp_tls_recv_callback(char* buf, int sz,
void* context, int* bytes_sent);
/** @details Notifies the client application to write data to a socket.
* Implemented in the SDK; don't modify this function. */
iotc_bsp_tls_state_t iotc_bsp_tls_send_callback(char* buf, int sz,
void* context, int* bytes_sent);
#endif /* __IOTC_BSP_TLS_H__ */