E-MailRelay

gssl_mbedtls_utils.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gssl_mbedtls_utils.h
19///
20
21#ifndef GSSL_MBEDTLS_UTILS_H
22#define GSSL_MBEDTLS_UTILS_H
23
24#include "gdef.h"
25#include "gssl_mbedtls_headers.h"
26#include "gstrmacros.h"
27#include <cstddef> // std::nullptr_t
28
29// macro magic to provide function-name/function-pointer arguments for call()
30//
31// in later versions of mbedtls v2 some functions like foo() have a preferred
32// alternative form foo_ret() that returns an integer result code -- however
33// in mbedtls v3 some of the foo_ret() functions are deprecated and foo()
34// is now preferred
35//
36#ifdef FN
37#undef FN
38#endif
39#define FN( fn ) nullptr,(#fn),(fn)
40#if MBEDTLS_VERSION_NUMBER >= 0x02070000
41#if MBEDTLS_VERSION_MAJOR >= 3
42#define FN_RET( fn ) nullptr,(#fn),(fn)
43#else
44#define FN_RET( fn ) 0,(#fn),(G_STR_PASTE(fn,_ret))
45#endif
46#else
47#define FN_RET( fn ) nullptr,(#fn),(fn)
48#endif
49#define FN_OK( ok , fn ) int(ok),(#fn),(fn)
50
51 namespace GSsl
52{
53 namespace MbedTls
54 {
55 // calls the given function with error checking -- overload for functions returning an integer status value
56 template <typename F, typename... Args>
57 void call( int ok , const char * fname , F fn , Args&&... args )
58 {
59 int rc = fn( std::forward<Args>(args)... ) ;
60 if( rc != ok )
61 throw Error( fname , rc ) ;
62 }
63
64 // calls the given function -- overload for functions returning void
65 template <typename F, typename... Args>
66 void call( std::nullptr_t , const char * /*fname*/ , F fn , Args&&... args )
67 {
68 fn( std::forward<Args>(args)... ) ;
69 }
70
71 // calls mbedtls_pk_parse_key() with or without the new rng parameters
72 using old_fn = int (*)( mbedtls_pk_context * c , const unsigned char * k , std::size_t ks ,
73 const unsigned char * p , std::size_t ps ) ;
74 using new_fn = int (*)( mbedtls_pk_context* c , const unsigned char* k , std::size_t ks ,
75 const unsigned char * p , std::size_t ps ,
76 int (*r)(void*,unsigned char*,std::size_t) , void* rp ) ;
77 inline int call_fn( old_fn fn ,
78 mbedtls_pk_context * c , const unsigned char * k , std::size_t ks ,
79 const unsigned char * p , std::size_t ps ,
80 int (*)(void*,unsigned char*,std::size_t) , void * )
81 {
82 return fn( c , k , ks , p , ps ) ;
83 }
84 inline int call_fn( new_fn fn ,
85 mbedtls_pk_context * c , const unsigned char * k , std::size_t ks ,
86 const unsigned char * p , std::size_t ps ,
87 int (*r)(void*,unsigned char*,std::size_t) , void * rp )
88 {
89 return fn( c , k , ks , p , ps , r , rp ) ;
90 }
91
92 template <typename T>
93 struct X /// Initialises and frees an mbedtls object on construction and destruction.
94 {
95 X( void (*init)(T*) , void (*free)(T*) ) : m_free(free) { init(&x) ; }
96 ~X() { m_free(&x) ; }
97 T * ptr() { return &x ; }
98 const T * ptr() const { return &x ; }
99 T x ;
100 void (*m_free)(T*) ;
101 X * operator&() = delete ;
102 const X * operator&() const = delete ;
103 X( const X<T> & ) = delete ;
104 X( X<T> && ) = delete ;
105 X<T> & operator=( const X<T> & ) = delete ;
106 X<T> & operator=( X<T> && ) = delete ;
107 } ;
108 }
109}
110
111#endif
gssl_mbedtls_headers.h
GSsl
TLS/SSL transport layer security classes.
Definition: gssl.h:36
GSsl::MbedTls::X
Initialises and frees an mbedtls object on construction and destruction.
Definition: gssl_mbedtls_utils.h:94