[go: up one dir, main page]

Menu

[ebf9b6]: / src / flom_tls.h  Maximize  Restore  History

Download this file

408 lines (308 with data), 10.1 kB

  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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Copyright (c) 2013-2024, Christian Ferrari <tiian@users.sourceforge.net>
* All rights reserved.
*
* This file is part of FLoM.
*
* FLoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* FLoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FLoM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FLOM_TLS_H
# define FLOM_TLS_H
#include "config.h"
#ifdef HAVE_DBUS_DBUS_H
# include <dbus/dbus.h>
#endif
#ifdef HAVE_OPENSSL_SSL_H
# include <openssl/ssl.h>
#endif
#ifdef HAVE_OPENSSL_ERR_H
# include <openssl/err.h>
#endif
#ifdef HAVE_OPENSSL_X509V3_H
# include <openssl/x509v3.h>
#endif
/* save old FLOM_TRACE_MODULE and set a new value */
#ifdef FLOM_TRACE_MODULE
# define FLOM_TRACE_MODULE_SAVE FLOM_TRACE_MODULE
# undef FLOM_TRACE_MODULE
#else
# undef FLOM_TRACE_MODULE_SAVE
#endif /* FLOM_TRACE_MODULE */
#define FLOM_TRACE_MODULE FLOM_TRACE_MOD_TLS
/**
* Maximum depth for the certificate chain verification
*/
#define FLOM_TLS_MAX_DEPTH_CERT_CHAIN_VERIF 3
/**
* Number of characters of a unique ID value
*/
#define FLOM_TLS_UNIQUE_ID_LENGHT 32
/**
* Custom data passed to verify callback OpenSSL function
*/
struct flom_tls_callback_data_s {
/**
* Certification chain verification depth
*/
int depth;
/**
* Don't stop, do another step
*/
int dont_stop;
};
/**
* Breakdown structure used to store all the strings related to a certificate
*/
struct flom_tls_cert_s {
/**
* Country Name string
*/
gchar *c_str;
/**
* State or Province Name string
*/
gchar *st_str;
/**
* Locality Name string
*/
gchar *l_str;
/**
* Organization Name string
*/
gchar *o_str;
/**
* Organizational Unit Name NID
*/
gchar *ou_str;
/**
* E-mail address string
*/
gchar *e_str;
/**
* Common Name string
*/
gchar *cn_str;
};
/**
* Object to store the metadata strings that can be retrieved by a "standard"
* X.509 certificate for issuer and subject
*/
typedef struct {
struct flom_tls_cert_s issuer;
struct flom_tls_cert_s subject;
} flom_tls_cert_t;
/**
* Object used to manage a TLS connection: it contains all the necessary data
* and avoid the usage of static data
*/
typedef struct {
/**
* SSL context
*/
SSL_CTX *ctx;
/**
* SSL structure
*/
SSL *ssl;
/**
* Boolean: is the TLS connection related to the client side (TRUE) or
* the server side (FALSE)
*/
int client;
/**
* Maximum depth for the certificate chain verification
*/
int depth;
/**
* Certificate returned by the peer
*/
flom_tls_cert_t *cert;
/**
* Struct used to pass custom data to callback function
*/
struct flom_tls_callback_data_s callback_data;
/**
* Index to custom callback data
*/
int callback_data_index;
} flom_tls_t;
/**
* Status of OpenSSL library initialization: TRUE = initialized,
* FALSE = not initialized
*/
extern int flom_tls_initialized;
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/**
* Unique ID of the FLoM installation
* @return the FLoM unique id string that must be released with g_free
* function or NULL in case of error
*/
gchar *flom_tls_get_unique_id();
/**
* TLS supported protocol(s)
* @return a string containing the protocol(s) supported by the build;
* the value depends from the target system
*/
static inline const char *flom_tls_get_protocol() {
#ifdef HAVE_TLS_METHOD
const char *tls_protocol = "All TLS protocols";
#elif HAVE_TLSV1_METHOD
const char *tls_protocol = "TLSv1";
#elif HAVE_TLSV1_1_METHOD
const char *tls_protocol = "TLSv1.1";
#elif HAVE_TLSV1_2_METHOD
const char *tls_protocol = "TLSv1.2";
#endif
return tls_protocol;
}
/**
* Numeric/string conversion for some SSL errors...
* Why is not supplied by the library?!?! Where am I wrong?!?!
* @return a human readable label
*/
const char *flom_tls_get_error_label(int error);
/**
* Create a new TLS/SSL object and,
* initialize (OpenSSL) TLS/SSL library if necessary
* @param client IN the TLS connection is related to the client side
* (TRUE) or to the server side (FALSE)
*/
flom_tls_t *flom_tls_new(int client);
/**
* Release the TLS/SSL object created and encapsulated by flom_tls_t;
* NOTE: the object must be initialized before this method can be applied
* @param obj IN/OUT TLS object
*/
void flom_tls_delete(flom_tls_t *obj);
/**
* Create a new TLS/SSL context
* @param obj IN/OUT connection object reference
* @return a reason code
*/
int flom_tls_context(flom_tls_t *obj);
/**
* Verify callback function
* @param preverify_ok IN indicates, whether the verification of the
* certificate in question was passed (preverify_ok=1) or not
* (preverify_ok=0)
* @param x509_ctx IN is a pointer to the complete context used for the
* certificate chain verification
* @return a boolean value
*/
int flom_tls_callback(int preverify_ok, X509_STORE_CTX *x509_ctx);
/**
* Set local certificate and private key from files
* @param obj IN/OUT connection object reference
* @param cert_file IN local certificate filename
* @param priv_key_file IN private key filename
* @param ca_cert_file IN ca certificate filename
* @return a reason code
*/
int flom_tls_set_cert(flom_tls_t *obj, const char *cert_file,
const char *priv_key_file, const char *ca_cert_file);
/**
* Prepare a standard TCP connection to be switched to a TLS connection
* @param obj IN/OUT TLS object
* @param sockfd IN file descriptor of the socket already connected
* @return a reason code
*/
int flom_tls_prepare(flom_tls_t *obj, int sockfd);
/**
* Switch a standard TCP client connection to a TLS client connection
* @param obj IN/OUT TLS object
* @param sockfd IN file descriptor of the socket already connected
* @return a reason code
*/
int flom_tls_connect(flom_tls_t *obj, int sockfd);
/**
* Switch a standard TCP server connection to a TLS server connection
* @param obj IN/OUT TLS object
* @param sockfd IN file descriptor of the socket already connected
* @return a reason code
*/
int flom_tls_accept(flom_tls_t *obj, int sockfd);
/**
* Send a buffer using TLS over TCP/IP
* @param obj IN/OUT TLS object
* @param buf IN buffer to send
* @param len IN buffer lenght
* @return a reason code
*/
int flom_tls_send(flom_tls_t *obj, const void *buf, size_t len);
/**
* Receive an XML message using TLS over TCP/IP
* @param obj IN/OUT TLS object
* @param buf OUT buffer to send
* @param len IN buffer lenght
* @param received OUT number of read bytes
* @return a reason code
*/
int flom_tls_recv_msg(flom_tls_t *obj, char *buf, size_t len,
size_t *received);
/**
* Create a new object of type flom_tls_cert_t
* @return a new object or NULL if any error occurred
*/
static inline flom_tls_cert_t *flom_tls_cert_new() {
return (flom_tls_cert_t *)g_try_malloc0(sizeof(flom_tls_cert_t));
}
/**
* Fills in all the strings related to the struct
* @param s IN/OUT struct names
* @param nid IN field identificator
* @param str IN value associated to the field
* @return a reason code
*/
int flom_tls_cert_struct_fill(struct flom_tls_cert_s *s, int nid,
const unsigned char *str);
/**
* Release all the strings related to the struct
* @param s IN struct names
*/
void flom_tls_cert_struct_delete(struct flom_tls_cert_s *s);
/**
* Delete an object previously created with @ref flom_tls_cert_new
* @param obj IN TLS object
*/
void flom_tls_cert_delete(flom_tls_cert_t *obj);
/**
* Parse the X.509 certificate passed by the peer and extract all the
* "standard" metada for "issuer" and "subject"
* @param obj IN/OUT TLS object
* @param ssl IN SSL object (as in OpenSSL library)
* @return a reason code
*/
int flom_tls_cert_parse(flom_tls_cert_t *obj, SSL *ssl);
/**
* Check the certificate presented by the peer: the CN in the certificate
* must match the unique id of the peer; @ref flom_tls_cert_parse must be
* called before this function
* @param obj IN/OUT TLS object
* @param peer_unique_id IN unique id presented by the peer
* @param peer_address IN network address of the peer (source) connection
* @return a reason code
*/
int flom_tls_cert_check(flom_tls_t *obj, const gchar *peer_unique_id,
const gchar *peer_address);
#ifdef __cplusplus
}
#endif /* __cplusplus */
/* restore old value of FLOM_TRACE_MODULE */
#ifdef FLOM_TRACE_MODULE_SAVE
# undef FLOM_TRACE_MODULE
# define FLOM_TRACE_MODULE FLOM_TRACE_MODULE_SAVE
# undef FLOM_TRACE_MODULE_SAVE
#endif /* FLOM_TRACE_MODULE_SAVE */
#endif /* FLOM_TLS_H */