Mstdlib-1.24.0
m_tls.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2017 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_TLS_H__
25#define __M_TLS_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/base/m_defs.h>
30#include <mstdlib/base/m_types.h>
31
32/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
33
34__BEGIN_DECLS
35
36/*! \addtogroup m_tls_funcs TLS functions
37 * \ingroup m_tls
38 *
39 * TLS functions
40 *
41 * @{
42 */
43
44struct M_tls_clientctx;
45typedef struct M_tls_clientctx M_tls_clientctx_t;
46
47struct M_tls_serverctx;
48typedef struct M_tls_serverctx M_tls_serverctx_t;
49
50
51/*! Supported TLS protocols. */
52typedef enum {
53 M_TLS_PROTOCOL_INVALID = -1, /*!< Invalid protocol. */
58 M_TLS_PROTOCOL_DEFAULT = (M_TLS_PROTOCOL_TLSv1_0 | M_TLS_PROTOCOL_TLSv1_1 | M_TLS_PROTOCOL_TLSv1_2 | M_TLS_PROTOCOL_TLSv1_3) /*!< While not a define passing 0 to a function that takes a protocol will be treated as default. */
60
61
62/*! Certificate verification level.
63 *
64 * Used by client connections to control how they decide to trust
65 * the certificate presented by the server.
66 *
67 */
68typedef enum {
69 M_TLS_VERIFY_NONE = 0, /*!< Do not verify the certificate or hostname. */
70 M_TLS_VERIFY_CERT_ONLY = 1, /*!< Only verify the certificate. The domain name is not checked. */
71 M_TLS_VERIFY_CERT_FUZZY = 2, /*!< Verify the certificate and that the base domain name matches.
72 Use this for servers that don't properly have a wild card cert
73 but still use a sub domain. */
74 M_TLS_VERIFY_FULL = 3 /*!< Default. Verify the certificate and full domain name matches */
75 /* XXX: OCSP, CRL? */
77
78
79/*! How the TLS stack was/is initialized.
80 *
81 * The TLS system uses OpenSSL as its back ends. It has global initialization
82 * and can only be initialized once. Inform the TLS system if it has already
83 * been initialized.
84 */
85typedef enum {
86 M_TLS_INIT_NORMAL = 1, /*!< Fully initialize the TLS (OpenSSL stack) */
87 M_TLS_INIT_EXTERNAL = 2 /*!< TLS initialization is handled externally (use with caution) */
89
90
91/*! Initialize the TLS library.
92 *
93 * If a TLS function is used without calling this function it
94 * will be auto initialized using the NORMAL type.
95 *
96 * \param[in] type Type of initialization.
97 */
98M_API void M_tls_init(M_tls_init_t type);
99
100
101/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102 * Client Support
103 */
104
105
106/*! Create a client TLS context.
107 *
108 * \return Client context.
109 */
111
112
113/*! Increment reference counters.
114 *
115 * Intended for APIs that might take ownership. Can only be
116 * Dereferenced via M_tls_clientctx_destroy()
117 *
118 *\param[in] ctx Client context.
119 */
121
122
123/*! Destroy a client context.
124 *
125 * Client CTXs use reference counters, and will delay destruction until after last consumer is destroyed.
126 *
127 *\param[in] ctx Client context.
128 */
130
131
132/*! Set the TLS protocols that the context should use.
133 *
134 * \param[in] ctx Client context.
135 * \param[in] protocols M_tls_protocols_t bitmap of TLS protocols that should be supported.
136 * Protocols are treated as min and max. For example if TLSv1.0 and
137 * TLSv1.2 are enabled, then TLSv1.1 will be enabled even if not
138 * explicitly set.
139 *
140 * \return M_TRUE on success, otherwise M_FALSE on error.
141 */
142M_API M_bool M_tls_clientctx_set_protocols(M_tls_clientctx_t *ctx, int protocols);
143
144
145/*! Set the ciphers that the context should support.
146 *
147 * A default list of secure ciphers is used if it is not explicitly changed
148 * by this function.
149 *
150 * \param[in] ctx Client context.
151 * \param[in] ciphers OpenSSL cipher string.
152 *
153 * \see M_tls_clientctx_get_cipherlist
154 */
155M_API M_bool M_tls_clientctx_set_ciphers(M_tls_clientctx_t *ctx, const char *ciphers);
156
157
158/* Set a certificate that will be sent to the server for the server to validate.
159 *
160 * Keys must be RSA or DSA.
161 * Certificates must be X509 and can be PEM, or DER encoded.
162 *
163 * The intermediate file can have multiple certificates comprising a certificate chain.
164 * Certificates in the chain should be ordered by signing with the certificate that signed
165 * the client certificate on top. The root CA file should not be in this chain.
166 *
167 * Intermediate example:
168 *
169 * crt (not part of intermediate file)
170 * Intermediate 1 that signed crt
171 * Intermediate 2 that signed intermediate 1
172 * Intermediate 3 that signed intermediate 2
173 * CA that signed intermediate 3 (not part of intermediate file).
174 *
175 * \param[in] ctx Client context.
176 * \param[in] key Private key associated with certificate.
177 * \param[in] key_len Length of private key.
178 * \param[in] crt Certificate.
179 * \param[in] crt_len Length of certificate.
180 * \param[in] intermediate Intermediate certificate chain.
181 * \param[in] intermediate_len Length of intermediate certificate chain.
182 *
183 * \return M_TRUE on success, otherwise M_FALSE on error.
184 *
185 * \see M_tls_clientctx_set_cert_files
186 */
187M_API M_bool M_tls_clientctx_set_cert(M_tls_clientctx_t *ctx, const unsigned char *key, size_t key_len, const unsigned char *crt, size_t crt_len, const unsigned char *intermediate, size_t intermediate_len);
188
189
190/* Set a certificate that will be sent to the server for the server to validate from a file.
191 *
192 * Requirements are the same as M_tls_clientctx_set_cert
193 *
194 * \param[in] ctx Client context.
195 * \param[in] keypath Path to key file.
196 * \param[in] crtpath Path to certificate file.
197 * \param[in] intermediatepath Path to intermediate certificate file.
198 *
199 * \return M_TRUE on success, otherwise M_FALSE on error.
200 *
201 * \see M_tls_clientctx_set_cert
202 */
203M_API M_bool M_tls_clientctx_set_cert_files(M_tls_clientctx_t *ctx, const char *keypath, const char *crtpath, const char *intermediatepath);
204
205
206/*! Load the OS CA trust list for validating the certificate presented by the server.
207 *
208 * This will not clear existing CAs that were already loaded.
209 *
210 * \param[in] ctx Client context.
211 *
212 * \return M_TRUE on success, otherwise M_FALSE on error.
213 */
215
216
217/*! Load a CA certificate for validating the certificate presented by the server.
218 *
219 * This will not clear existing CAs that were already loaded.
220 *
221 * \param[in] ctx Client context.
222 * \param[in] ca CA data.
223 * \param[in] len CA length.
224 *
225 * \return M_TRUE on success, otherwise M_FALSE on error.
226 */
227M_API M_bool M_tls_clientctx_set_trust_ca(M_tls_clientctx_t *ctx, const unsigned char *ca, size_t len);
228
229
230/*! Load a CA certificate from a file for validating the certificate presented by the server.
231 *
232 * This will not clear existing CAs that were already loaded.
233 *
234 * \param[in] ctx Client context.
235 * \param[in] path
236 *
237 * \return M_TRUE on success, otherwise M_FALSE on error.
238 */
239M_API M_bool M_tls_clientctx_set_trust_ca_file(M_tls_clientctx_t *ctx, const char *path);
240
241
242/*! Load CA certificates found in a directory for validating the certificate presented by the server.
243 *
244 * Files must be PEM encoded and use the ".pem" extension.
245 *
246 * This will not clear existing CAs that were already loaded.
247 *
248 * \param[in] ctx Client context.
249 * \param[in] path Path to CA file.
250 *
251 * \return M_TRUE on success, otherwise M_FALSE on error.
252 *
253 * \see M_tls_clientctx_set_trust_ca
254 */
255M_API M_bool M_tls_clientctx_set_trust_ca_dir(M_tls_clientctx_t *ctx, const char *path);
256
257
258/*! Load a certificate for validation of the certificate presented by the server.
259 *
260 * This is for loading intermediate certificate used as part of the trust chain.
261 *
262 * This will not clear existing certificates that were already loaded.
263 *
264 * \param[in] ctx Client context.
265 * \param[in] crt Certificate.
266 * \param[in] len Certificate length.
267 *
268 * \return M_TRUE on success, otherwise M_FALSE on error.
269 */
270M_API M_bool M_tls_clientctx_set_trust_cert(M_tls_clientctx_t *ctx, const unsigned char *crt, size_t len);
271
272
273/*! Load a certificate from a file for validation of the certificate presented by the server.
274 *
275 * This is for loading intermediate certificate used as part of the trust chain.
276 *
277 * This will not clear existing certificates that were already loaded.
278 *
279 * \param[in] ctx Client context.
280 * \param[in] path Path to certificate file.
281 *
282 * \return M_TRUE on success, otherwise M_FALSE on error.
283 *
284 * \see M_tls_clientctx_set_trust_cert
285 */
286M_API M_bool M_tls_clientctx_set_trust_cert_file(M_tls_clientctx_t *ctx, const char *path);
287
288
289/* Set how the server certificate should be verified.
290 *
291 * \param[in] ctx Client context.
292 * \param[in] level Verification level.
293 *
294 * \return M_TRUE on success, otherwise M_FALSE on error.
295 */
297
298
299/*! Enable or disable session resumption.
300 *
301 * Session resumption is enabled by default.
302 *
303 * \param[in] ctx Client context.
304 * \param[in] enable M_TRUE to enable. M_FALSE to disable.
305 *
306 * \return M_TRUE on success, otherwise M_FALSE on error.
307 */
309
310
311/*! Retrieves a colon separated list of ciphers that are enabled.
312 *
313 * \param[in] ctx Client context.
314 *
315 * \return String.
316 */
318
319
320/*! Set ALPN supported applications.
321 *
322 * \param[in] ctx Client context.
323 * \param[in] applications List of supported applications.
324 *
325 * \return M_TRUE on success, otherwise M_FALSE on error.
326 */
328
329
330/*! Set the negotiation timeout.
331 *
332 * How long the client should wait to establish a connection.
333 *
334 * \param[in] ctx Client context.
335 * \param[in] timeout_ms Time in milliseconds.
336 *
337 * \return M_TRUE on success, otherwise M_FALSE on error.
338 */
339M_API M_bool M_tls_clientctx_set_negotiation_timeout_ms(M_tls_clientctx_t *ctx, M_uint64 timeout_ms);
340
341
342/*! Wrap existing IO channel with TLS.
343 *
344 * \param[in] io io object.
345 * \param[in] ctx Client context.
346 * \param[in] hostname Hostname is optional if wrapping an outbound network connection where
347 * it can be retrieved from the lower layer
348 * \param[out] layer_id Layer id this is added at.
349 *
350 * \return Result.
351 */
352M_API M_io_error_t M_io_tls_client_add(M_io_t *io, M_tls_clientctx_t *ctx, const char *hostname, size_t *layer_id);
353
354
355/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
356 * ServerSupport
357 */
358
359
360/*! Create a server TLS context.
361 *
362 * \param[in] key Private key associated with certificate.
363 * \param[in] key_len Length of private key.
364 * \param[in] crt Certificate.
365 * \param[in] crt_len Length of certificate.
366 * \param[in] intermediate Intermediate certificate chain. Can be NULL.
367 * \param[in] intermediate_len Length of intermediate certificate chain.
368 *
369 * \return Server context.
370 */
371M_API M_tls_serverctx_t *M_tls_serverctx_create(const unsigned char *key, size_t key_len, const unsigned char *crt, size_t crt_len, const unsigned char *intermediate, size_t intermediate_len);
372
373
374/*! Create a server TLS context from files.
375 *
376 * \param[in] keypath Path to key file.
377 * \param[in] crtpath Path to certificate file.
378 * \param[in] intermediatepath Path to intermediate certificate file. Can be NULL.
379 *
380 * \return Server context.
381 */
382M_API M_tls_serverctx_t *M_tls_serverctx_create_from_files(const char *keypath, const char *crtpath, const char *intermediatepath);
383
384/*! Increment reference counters.
385 *
386 * Intended for APIs that might take ownership. Can only be
387 * Dereferenced via M_tls_serverctx_destroy()
388 *
389 * \param[in] ctx Server context.
390 *
391 * \return M_TRUE on success, otherwise M_FALSE on error.
392 */
394
395
396/*! Destroy a server context.
397 *
398 * Server CTXs use reference counters, and will delay destruction until after last consumer is destroyed.
399 *
400 * \param[in] ctx Server context.
401 */
403
404
405/*! Add a sub context under this one to allow multiple certificates to be used with SNI.
406 *
407 * For SNI support, if a certificate does not list a subject alt name, a server context
408 * needs to be created for each certificate. The certificate to be used as the
409 * default when the client does not support SNI will be the parent context. All
410 * of the additional contexts are added to this one.
411 *
412 * This is not necessary if a certificate lists all expected host names as subject alt names.
413 *
414 * \param[in] ctx Server context.
415 * \param[in] child Child server context.
416 *
417 * \return M_TRUE on success, otherwise M_FALSE on error.
418 */
420
421
422/*! Number of child contexts associated with this server context used for SNI.
423 *
424 * \param[in] ctx Server context.
425 *
426 * \return Count.
427 *
428 * \see M_tls_serverctx_SNI_at
429 */
431
432
433/*! Get a child SNI context from a context based on host name.
434 *
435 * \param[in] ctx Server context.
436 * \param[in] hostname Host name to look for.
437 *
438 * \return Server context on success, otherwise NULL if not found.
439 */
441
442
443/*! Get a child SNI context from a context at a given index.
444 *
445 * \param[in] ctx Server context.
446 * \param[in] idx Index.
447 *
448 * \return Server context on success, otherwise NULL on error.
449 *
450 * \see M_tls_serverctx_SNI_count
451 */
453
454
455/* Get the X509 certificate for a server context.
456 *
457 * \param[in] ctx Server context.
458 *
459 * \return PEM encoded certificate
460 */
462
463
464/*! Set the TLS protocols that the context should use.
465 *
466 * \param[in] ctx Server context.
467 * \param[in] protocols M_tls_protocols_t bitmap of TLS protocols that should be supported.
468 * Protocols are treated as min and max. For example if TLSv1.0 and
469 * TLSv1.2 are enabled, then TLSv1.1 will be enabled even if not
470 * explicitly set.
471 *
472 * \return M_TRUE on success, otherwise M_FALSE on error.
473 */
474M_API M_bool M_tls_serverctx_set_protocols(M_tls_serverctx_t *ctx, int protocols);
475
476
477/*! Set the ciphers that the context should support.
478 *
479 * A default list of secure ciphers is used if it is not explicitly changed
480 * by this function.
481 *
482 * \param[in] ctx Server context.
483 * \param[in] ciphers OpenSSL cipher string.
484 *
485 * \see M_tls_clientctx_get_cipherlist
486 */
487M_API M_bool M_tls_serverctx_set_ciphers(M_tls_serverctx_t *ctx, const char *ciphers);
488
489
490/*! Set the server to prefer its own cipher order rather than the client.
491 *
492 * By default, the client cipher order is preferred, this is recommended as a
493 * client may be a mobile device where a cipher like TLS_CHACHA20_POLY1305_SHA256
494 * is more efficient than TLS_AES_256_GCM_SHA384 and will provide a better
495 * customer experience. However a desktop client may prefer TLS_AES_256_GCM_SHA384
496 * as it supports AES-NI instruction helpers or similar. Since the server is
497 * often more powerful than the client, it is better suited to the additional
498 * compute.
499 *
500 * Assuming the server is configured to only allow strong ciphers, there should
501 * be no security risk in allowing the client to decide the most efficient.
502 *
503 * \param[in] ctx Server context.
504 * \param[in] tf M_TRUE to enable server preference, M_FALSE to disable.
505 *
506 * \see M_tls_clientctx_get_cipherlist
507 */
509
510
511
512/*! Load a CA certificate for validating the certificate presented by the client.
513 *
514 * If set the client will be required to present a certificate.
515 *
516 * This will not clear existing CAs that were already loaded.
517 *
518 * \param[in] ctx Server context.
519 * \param[in] ca CA data.
520 * \param[in] len CA length.
521 *
522 * \return M_TRUE on success, otherwise M_FALSE on error.
523 */
524M_API M_bool M_tls_serverctx_set_trust_ca(M_tls_serverctx_t *ctx, const unsigned char *ca, size_t len);
525
526
527/*! Load a CA certificate from a file for validating the certificate presented by the client.
528 *
529 * This will not clear existing CAs that were already loaded.
530 *
531 * \param[in] ctx Server context.
532 * \param[in] path Path to CA file.
533 *
534 * \return M_TRUE on success, otherwise M_FALSE on error.
535 *
536 * \see M_tls_serverctx_set_trust_ca
537 */
538M_API M_bool M_tls_serverctx_set_trust_ca_file(M_tls_serverctx_t *ctx, const char *path);
539
540
541/*! Load a certificate for validation of the certificate presented by the client.
542 *
543 * This is for loading intermediate certificate used as part of the trust chain.
544 *
545 * This will not clear existing certificates that were already loaded.
546 *
547 * \param[in] ctx Server context.
548 * \param[in] path Path to CA directory.
549 *
550 * \return M_TRUE on success, otherwise M_FALSE on error.
551 *
552 * \see M_tls_serverctx_set_trust_ca
553 */
554M_API M_bool M_tls_serverctx_set_trust_ca_dir(M_tls_serverctx_t *ctx, const char *path);
555
556
557/*! Load a certificate for validation of the certificate presented by the client.
558 *
559 * This is for loading intermediate certificate used as part of the trust chain.
560 *
561 * This will not clear existing certificates that were already loaded.
562 *
563 * \param[in] ctx Server context.
564 * \param[in] crt Certificate.
565 * \param[in] len Certificate length.
566 *
567 * \return M_TRUE on success, otherwise M_FALSE on error.
568 */
569M_API M_bool M_tls_serverctx_set_trust_cert(M_tls_serverctx_t *ctx, const unsigned char *crt, size_t len);
570
571
572/*! Load a certificate from a file for validation of the certificate presented by the client.
573 *
574 * This is for loading intermediate certificate used as part of the trust chain.
575 *
576 * This will not clear existing certificates that were already loaded.
577 *
578 * \param[in] ctx Server context.
579 * \param[in] path Path to certificate file.
580 *
581 * \return M_TRUE on success, otherwise M_FALSE on error.
582 *
583 * \see M_tls_serverctx_set_trust_cert
584 */
585M_API M_bool M_tls_serverctx_set_trust_cert_file(M_tls_serverctx_t *ctx, const char *path);
586
587
588/*! Load a certificate revocation list to validate the certificate presented by the client.
589 *
590 * This will not clear existing revocations already loaded.
591 *
592 * \param[in] ctx Server context.
593 * \param[in] crl CRL.
594 * \param[in] len CRL Length.
595 *
596 * \return M_TRUE on success, otherwise M_FALSE on error.
597 */
598M_API M_bool M_tls_serverctx_add_trust_crl(M_tls_serverctx_t *ctx, const unsigned char *crl, size_t len);
599
600
601/*! Load a certificate revocation from a file list to validate the certificate presented by the client.
602 *
603 * This will not clear existing revocations already loaded.
604 *
605 * \param[in] ctx Server context.
606 * \param[in] path Path to certificate revocation list file.
607 *
608 * \return M_TRUE on success, otherwise M_FALSE on error.
609 *
610 * \see M_tls_serverctx_set_trust_cert_file
611 */
612M_API M_bool M_tls_serverctx_add_trust_crl_file(M_tls_serverctx_t *ctx, const char *path);
613
614
615/*! Set the dhparam for the context.
616 *
617 * If not set, uses internal 2236 dhparam.
618 * DHparam data must be PEM-encoded.
619 *
620 * \param[in] ctx Server context.
621 * \param[in] dhparam DHparam data. If dhparam is NULL, disables the use of DHE negotiation.
622 * \param[in] dhparam_len Length of data.
623 *
624 * \return M_TRUE on success, otherwise M_FALSE on error.
625 */
626M_API M_bool M_tls_serverctx_set_dhparam(M_tls_serverctx_t *ctx, const unsigned char *dhparam, size_t dhparam_len);
627
628
629/*! Set the dhparam for the context from a file.
630 *
631 * If not set, uses internal 2236 dhparam.
632 * DHparam data must be PEM-encoded.
633 *
634 * \param[in] ctx Server context.
635 * \param[in] dhparam_path Path to DHparam data.
636 *
637 * \return M_TRUE on success, otherwise M_FALSE on error.
638 *
639 * \see M_tls_serverctx_set_dhparam
640 */
641M_API M_bool M_tls_serverctx_set_dhparam_file(M_tls_serverctx_t *ctx, const char *dhparam_path);
642
643
644/*! Enable or disable session resumption.
645 *
646 * Session resumption is enabled by default.
647 *
648 * \param[in] ctx Server context.
649 * \param[in] enable M_TRUE to enable. M_FALSE to disable.
650 *
651 * \return M_TRUE on success, otherwise M_FALSE on error.
652 */
654
655
656/*! Retrieves a colon separated list of ciphers that are enabled.
657 *
658 * \param[in] ctx Server context.
659 *
660 * \return String.
661 */
663
664/*! Set ALPN supported applications.
665 *
666 * \param[in] ctx Server context.
667 * \param[in] applications List of supported applications.
668 *
669 * \return M_TRUE on success, otherwise M_FALSE on error.
670 */
672
673
674/*! Set the negotiation timeout.
675 *
676 * How long the server should wait to establish a connection.
677 *
678 * \param[in] ctx Server context.
679 * \param[in] timeout_ms Time in milliseconds.
680 *
681 * \return M_TRUE on success, otherwise M_FALSE on error.
682 */
683M_API M_bool M_tls_serverctx_set_negotiation_timeout_ms(M_tls_serverctx_t *ctx, M_uint64 timeout_ms);
684
685/*! Wrap existing IO channel with TLS.
686 *
687 * \param[in] io io object.
688 * \param[in] ctx Server context.
689 * \param[out] layer_id Layer id this is added at.
690 *
691 * \return Result.
692 */
693M_API M_io_error_t M_io_tls_server_add(M_io_t *io, M_tls_serverctx_t *ctx, size_t *layer_id);
694
695
696/*! Get the host name the connected client requested.
697 *
698 * \param[in] io io object.
699 * \param[in] id Layer id.
700 *
701 * \return String.
702 */
703M_API const char *M_tls_server_get_hostname(M_io_t *io, size_t id);
704
705
706/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
707 * Common to both server and client
708 */
709
710
711/*! Get the protocol the connection was establish with.
712 *
713 * \param[in] io io object.
714 * \param[in] id Layer id.
715 *
716 * \return Protocol.
717 */
719
720
721/*! Was the session for this connection reused from a previous connection?
722 *
723 * \param[in] io io object.
724 * \param[in] id Layer id.
725 *
726 * \return M_TRUE if reused, otherwise M_FALSE.
727 */
728M_API M_bool M_tls_get_sessionreused(M_io_t *io, size_t id);
729
730
731/*! Get the cipher negotiated.
732 *
733 * \param[in] io io object.
734 * \param[in] id Layer id.
735 *
736 * \return String.
737 */
738M_API const char *M_tls_get_cipher(M_io_t *io, size_t id);
739
740
741/*! Get the application negotiated.
742 *
743 * \param[in] io io object.
744 * \param[in] id Layer id.
745 *
746 * \return NULL if there is no ALPN support or on error, or will return the application.
747 */
748M_API char *M_tls_get_application(M_io_t *io, size_t id);
749
750
751/*! Get the certificate presented by the other end.
752 *
753 * \param[in] io io object.
754 * \param[in] id Layer id.
755 *
756 * \return X509 PEM encoded certificate.
757 */
758M_API char *M_tls_get_peer_cert(M_io_t *io, size_t id);
759
760
761/*! How long negotiated took.
762 *
763 * \param[in] io io object.
764 * \param[in] id Layer id.
765 *
766 * \return Negotiation time (success or fail) in ms */
767M_API M_uint64 M_tls_get_negotiation_time_ms(M_io_t *io, size_t id);
768
769
770/*! Convert a protocol to string.
771 *
772 * Only single protocol should be specified. If multiple are provided
773 * it is undefined which will be returned. Used primarily for logging to
774 * print what protocol a connection is using.
775 *
776 * \param[in] protocol
777 *
778 * \return String.
779 */
780M_API const char *M_tls_protocols_to_str(M_tls_protocols_t protocol);
781
782
783/*! Convert a string to protocols bitmap
784 *
785 * The value for this field is a space separated list of protocols. Valid
786 * protocols are: tlsv1, tlsv1.0, tlsv1.1, tlsv1.2, tlsv1.3.
787 *
788 * Entry tlsv1 implies all tls 1.y protocols.
789 *
790 * If the protocol is appended with a plus (+) sign, then it means that protocol
791 * version or higher, for instance, "tlsv1.1+" implies "tlsv1.1 tlsv1.2 tlsv1.3"
792 *
793 * Protocols are treated as min and max. Enabling protocols with
794 * version gaps will result in the gaps being enabled. E.g. specifying
795 * "tlsv1.0 tlsv1.2" will enable tlsv1.0, _tlsv1.1_, and tlsv1.2.
796 *
797 * Unknown entries will be ignored. Protocols that are not supported
798 * by the backend will be removed from the list of returned protocols.
799 *
800 * \param[in] protocols_str String of protocols
801 *
802 * \return Protocol bitmap. M_TLS_PROTOCOL_INVALID on error.
803 */
804M_API M_tls_protocols_t M_tls_protocols_from_str(const char *protocols_str);
805
806/*! @} */
807
808__END_DECLS
809
810#endif /* __M_TLS_H__ */
enum M_io_error M_io_error_t
Definition: m_io.h:93
struct M_io M_io_t
Definition: m_io.h:59
struct M_list_str M_list_str_t
Definition: m_list_str.h:80
M_bool M_tls_clientctx_set_default_trust(M_tls_clientctx_t *ctx)
M_bool M_tls_serverctx_set_dhparam(M_tls_serverctx_t *ctx, const unsigned char *dhparam, size_t dhparam_len)
M_bool M_tls_serverctx_add_trust_crl_file(M_tls_serverctx_t *ctx, const char *path)
M_bool M_tls_serverctx_set_protocols(M_tls_serverctx_t *ctx, int protocols)
M_bool M_tls_clientctx_set_ciphers(M_tls_clientctx_t *ctx, const char *ciphers)
M_bool M_tls_serverctx_set_server_preference(M_tls_serverctx_t *ctx, M_bool tf)
M_bool M_tls_serverctx_set_trust_cert_file(M_tls_serverctx_t *ctx, const char *path)
M_bool M_tls_serverctx_upref(M_tls_serverctx_t *ctx)
size_t M_tls_serverctx_SNI_count(M_tls_serverctx_t *ctx)
const char * M_tls_get_cipher(M_io_t *io, size_t id)
const char * M_tls_protocols_to_str(M_tls_protocols_t protocol)
char * M_tls_get_application(M_io_t *io, size_t id)
M_bool M_tls_serverctx_SNI_ctx_add(M_tls_serverctx_t *ctx, M_tls_serverctx_t *child)
M_bool M_tls_clientctx_set_verify_level(M_tls_clientctx_t *ctx, M_tls_verify_level_t level)
M_bool M_tls_get_sessionreused(M_io_t *io, size_t id)
M_uint64 M_tls_get_negotiation_time_ms(M_io_t *io, size_t id)
M_bool M_tls_clientctx_set_trust_cert(M_tls_clientctx_t *ctx, const unsigned char *crt, size_t len)
M_bool M_tls_clientctx_set_cert_files(M_tls_clientctx_t *ctx, const char *keypath, const char *crtpath, const char *intermediatepath)
M_tls_serverctx_t * M_tls_serverctx_create_from_files(const char *keypath, const char *crtpath, const char *intermediatepath)
M_bool M_tls_serverctx_set_trust_ca(M_tls_serverctx_t *ctx, const unsigned char *ca, size_t len)
M_bool M_tls_clientctx_set_applications(M_tls_clientctx_t *ctx, M_list_str_t *applications)
M_bool M_tls_clientctx_set_negotiation_timeout_ms(M_tls_clientctx_t *ctx, M_uint64 timeout_ms)
M_bool M_tls_clientctx_upref(M_tls_clientctx_t *ctx)
M_tls_serverctx_t * M_tls_serverctx_SNI_at(M_tls_serverctx_t *ctx, size_t idx)
void M_tls_serverctx_destroy(M_tls_serverctx_t *ctx)
M_bool M_tls_serverctx_set_negotiation_timeout_ms(M_tls_serverctx_t *ctx, M_uint64 timeout_ms)
M_tls_clientctx_t * M_tls_clientctx_create(void)
M_bool M_tls_clientctx_set_cert(M_tls_clientctx_t *ctx, const unsigned char *key, size_t key_len, const unsigned char *crt, size_t crt_len, const unsigned char *intermediate, size_t intermediate_len)
M_tls_verify_level_t
Definition: m_tls.h:68
M_bool M_tls_clientctx_set_trust_ca_file(M_tls_clientctx_t *ctx, const char *path)
const char * M_tls_server_get_hostname(M_io_t *io, size_t id)
M_bool M_tls_serverctx_set_ciphers(M_tls_serverctx_t *ctx, const char *ciphers)
M_bool M_tls_serverctx_set_trust_ca_file(M_tls_serverctx_t *ctx, const char *path)
M_io_error_t M_io_tls_server_add(M_io_t *io, M_tls_serverctx_t *ctx, size_t *layer_id)
struct M_tls_clientctx M_tls_clientctx_t
Definition: m_tls.h:45
char * M_tls_serverctx_get_cert(M_tls_serverctx_t *ctx)
M_bool M_tls_clientctx_set_trust_ca(M_tls_clientctx_t *ctx, const unsigned char *ca, size_t len)
M_bool M_tls_serverctx_add_trust_crl(M_tls_serverctx_t *ctx, const unsigned char *crl, size_t len)
M_bool M_tls_clientctx_set_trust_ca_dir(M_tls_clientctx_t *ctx, const char *path)
char * M_tls_serverctx_get_cipherlist(M_tls_serverctx_t *ctx)
struct M_tls_serverctx M_tls_serverctx_t
Definition: m_tls.h:48
M_bool M_tls_clientctx_set_session_resumption(M_tls_clientctx_t *ctx, M_bool enable)
M_tls_protocols_t M_tls_protocols_from_str(const char *protocols_str)
M_tls_protocols_t M_tls_get_protocol(M_io_t *io, size_t id)
M_tls_protocols_t
Definition: m_tls.h:52
M_bool M_tls_serverctx_set_trust_ca_dir(M_tls_serverctx_t *ctx, const char *path)
M_tls_init_t
Definition: m_tls.h:85
M_io_error_t M_io_tls_client_add(M_io_t *io, M_tls_clientctx_t *ctx, const char *hostname, size_t *layer_id)
M_bool M_tls_serverctx_set_session_resumption(M_tls_serverctx_t *ctx, M_bool enable)
M_tls_serverctx_t * M_tls_serverctx_SNI_lookup(M_tls_serverctx_t *ctx, const char *hostname)
M_bool M_tls_clientctx_set_protocols(M_tls_clientctx_t *ctx, int protocols)
char * M_tls_get_peer_cert(M_io_t *io, size_t id)
M_bool M_tls_serverctx_set_dhparam_file(M_tls_serverctx_t *ctx, const char *dhparam_path)
void M_tls_init(M_tls_init_t type)
char * M_tls_clientctx_get_cipherlist(M_tls_clientctx_t *ctx)
M_bool M_tls_clientctx_set_trust_cert_file(M_tls_clientctx_t *ctx, const char *path)
void M_tls_clientctx_destroy(M_tls_clientctx_t *ctx)
M_bool M_tls_serverctx_set_trust_cert(M_tls_serverctx_t *ctx, const unsigned char *crt, size_t len)
M_bool M_tls_serverctx_set_applications(M_tls_serverctx_t *ctx, M_list_str_t *applications)
M_tls_serverctx_t * M_tls_serverctx_create(const unsigned char *key, size_t key_len, const unsigned char *crt, size_t crt_len, const unsigned char *intermediate, size_t intermediate_len)
@ M_TLS_VERIFY_NONE
Definition: m_tls.h:69
@ M_TLS_VERIFY_CERT_FUZZY
Definition: m_tls.h:71
@ M_TLS_VERIFY_FULL
Definition: m_tls.h:74
@ M_TLS_VERIFY_CERT_ONLY
Definition: m_tls.h:70
@ M_TLS_PROTOCOL_TLSv1_2
Definition: m_tls.h:56
@ M_TLS_PROTOCOL_TLSv1_1
Definition: m_tls.h:55
@ M_TLS_PROTOCOL_INVALID
Definition: m_tls.h:53
@ M_TLS_PROTOCOL_TLSv1_3
Definition: m_tls.h:57
@ M_TLS_PROTOCOL_DEFAULT
Definition: m_tls.h:58
@ M_TLS_PROTOCOL_TLSv1_0
Definition: m_tls.h:54
@ M_TLS_INIT_EXTERNAL
Definition: m_tls.h:87
@ M_TLS_INIT_NORMAL
Definition: m_tls.h:86