E-MailRelay
gssl_mbedtls_utils.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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"
26#include "gstrmacros.h"
27
28// macro magic to provide function-name/function-pointer arguments for call()
29#ifdef FN
30#undef FN
31#endif
32#define FN( fn ) (#fn),(fn)
33
34// in newer versions of the mbedtls library hashing functions like mbed_whatever_ret()
35// returning an integer are preferred, compared to mbed_whatever() returning void --
36// except in version 3 some functions go back to a void return
37#if MBEDTLS_VERSION_NUMBER >= 0x02070000
38#define FN_RET( fn ) (#fn),(G_STR_PASTE(fn,_ret))
39#if MBEDTLS_VERSION_MAJOR >= 3
40#define FN_RETv3( fn ) (#fn),(fn)
41#else
42#define FN_RETv3( fn ) (#fn),(G_STR_PASTE(fn,_ret))
43#endif
44#else
45#define FN_RET( fn ) (#fn),(fn)
46#define FN_RETv3( fn ) (#fn),(fn)
47#endif
48
49namespace GSsl
50{
51 namespace MbedTls
52 {
53 // calls the given function with error checking -- overload for functions returning int
54 template <typename F, typename... Args>
55 typename std::enable_if< !std::is_same<void,typename std::result_of<F(Args...)>::type>::value >::type
56 call( const char * fname , F fn , Args&&... args )
57 {
58 int rc = fn( std::forward<Args>(args)... ) ;
59 if( rc )
60 throw Error( fname , rc ) ;
61 }
62
63 // calls the given function -- overload for functions returning void
64 template <typename F, typename... Args>
65 typename std::enable_if< std::is_same<void,typename std::result_of<F(Args...)>::type>::value >::type
66 call( const char * , 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
TLS/SSL transport layer security classes.
Definition: gssl.h:36
Initialises and frees an mbedtls object on construction and destruction.