32 #include <mbedtls/ssl.h>
33 #include <mbedtls/ssl_ciphersuites.h>
34 #include <mbedtls/entropy.h>
35 #include <mbedtls/ctr_drbg.h>
36 #include <mbedtls/certs.h>
37 #include <mbedtls/error.h>
38 #include <mbedtls/version.h>
39 #include <mbedtls/pem.h>
40 #include <mbedtls/base64.h>
41 #include <mbedtls/debug.h>
52 explicit Cleanup(
void (*fn)(T*) ) : m_p(nullptr) , m_fn(fn) {}
53 ~Cleanup() { reset() ; }
54 void reset( T * p =
nullptr ) {
if(m_p) m_fn(m_p) ; m_p = p ; }
55 void release() { m_p = nullptr ; }
61 GSsl::MbedTls::LibraryImp::LibraryImp(
G::StringArray & library_config , Library::LogFn log_fn ,
bool verbose ) :
63 m_config(library_config)
65 mbedtls_debug_set_threshold( verbose ? 3 : 1 ) ;
68 GSsl::MbedTls::LibraryImp::~LibraryImp()
78 const std::string & key_file ,
const std::string & cert_file ,
const std::string & ca_file ,
79 const std::string & default_peer_certificate_name ,
const std::string & default_peer_host_name ,
80 const std::string & profile_config )
82 shared_ptr<ProfileImp> profile_ptr(
83 new ProfileImp(*
this,is_server_profile,key_file,cert_file,ca_file,
84 default_peer_certificate_name,default_peer_host_name,profile_config) ) ;
85 m_profile_map.insert( Map::value_type(profile_name,profile_ptr) ) ;
90 Map::const_iterator p = m_profile_map.find( profile_name ) ;
91 return p != m_profile_map.end() ;
96 Map::const_iterator p = m_profile_map.find( profile_name ) ;
97 if( p == m_profile_map.end() )
throw Error(
"no such profile: [" + profile_name +
"]" ) ;
98 return *(*p).second.get() ;
101 GSsl::Library::LogFn GSsl::MbedTls::LibraryImp::log()
const
106 std::string GSsl::MbedTls::LibraryImp::sid()
108 std::vector<char> buffer( 100U ) ;
109 G_ASSERT( buffer.size() >= 18U ) ;
110 mbedtls_version_get_string_full( &buffer[0] ) ;
111 buffer[buffer.size()-1U] =
'\0' ;
125 std::string GSsl::MbedTls::LibraryImp::credit(
const std::string & prefix ,
const std::string & eol ,
const std::string & eot )
127 std::ostringstream ss ;
129 << prefix <<
"mbed TLS: Copy" <<
"right (C) 2006-2016, ARM Limited" << eol
137 result.push_back(
"MD5" ) ;
138 result.push_back(
"SHA1" ) ;
139 result.push_back(
"SHA256" ) ;
151 m_noverify(consume(config,
"noverify")),
155 static const int SSL_v3 = MBEDTLS_SSL_MINOR_VERSION_0 ;
156 static const int TLS_v1_0 = MBEDTLS_SSL_MINOR_VERSION_1 ;
157 static const int TLS_v1_1 = MBEDTLS_SSL_MINOR_VERSION_2 ;
158 static const int TLS_v1_2 = MBEDTLS_SSL_MINOR_VERSION_3 ;
160 G_ASSERT( SSL_v3 >= 0 ) ;
161 G_ASSERT( TLS_v1_0 >= 0 ) ;
162 G_ASSERT( TLS_v1_1 >= 0 ) ;
163 G_ASSERT( TLS_v1_2 >= 0 ) ;
165 if(
consume(config,
"sslv3") ) m_min = SSL_v3 ;
166 if(
consume(config,
"tlsv1.0") ) m_min = TLS_v1_0 ;
167 if(
consume(config,
"tlsv1.1") ) m_min = TLS_v1_1 ;
168 if(
consume(config,
"tlsv1.2") ) m_min = TLS_v1_2 ;
170 if(
consume(config,
"-sslv3") ) m_max = SSL_v3 ;
171 if(
consume(config,
"-tlsv1.0") ) m_max = TLS_v1_0 ;
172 if(
consume(config,
"-tlsv1.1") ) m_max = TLS_v1_1 ;
173 if(
consume(config,
"-tlsv1.2") ) m_max = TLS_v1_2 ;
176 int GSsl::MbedTls::Config::min_()
const
181 int GSsl::MbedTls::Config::max_()
const
186 bool GSsl::MbedTls::Config::noverify()
const
191 bool GSsl::MbedTls::Config::consume(
G::StringArray & list ,
const std::string & item )
198 GSsl::MbedTls::DigesterImp::DigesterImp(
const std::string & hash_type ,
const std::string & state ) :
199 m_hash_type(
G::Str::upper(hash_type))
201 if( m_hash_type ==
"MD5" )
203 mbedtls_md5_init( &m_md5 ) ;
205 mbedtls_md5_starts( &m_md5 ) ;
209 else if( m_hash_type ==
"SHA1" )
211 mbedtls_sha1_init( &m_sha1 ) ;
213 mbedtls_sha1_starts( &m_sha1 ) ;
217 else if( m_hash_type ==
"SHA256" )
219 mbedtls_sha256_init( &m_sha256 ) ;
221 mbedtls_sha256_starts( &m_sha256 , 0 ) ;
227 GSsl::MbedTls::DigesterImp::~DigesterImp()
229 if( m_hash_type ==
"MD5" )
230 mbedtls_md5_free( &m_md5 ) ;
231 else if( m_hash_type ==
"SHA1" )
232 mbedtls_sha1_free( &m_sha1 ) ;
233 else if( m_hash_type ==
"SHA256" )
234 mbedtls_sha256_free( &m_sha256 ) ;
239 if( m_hash_type ==
"MD5" )
240 mbedtls_md5_update( &m_md5 , reinterpret_cast<const unsigned char*>(s.data()) , s.size() ) ;
241 else if( m_hash_type ==
"SHA1" )
242 mbedtls_sha1_update( &m_sha1 , reinterpret_cast<const unsigned char*>(s.data()) , s.size() ) ;
243 else if( m_hash_type ==
"SHA256" )
244 mbedtls_sha256_update( &m_sha256 , reinterpret_cast<const unsigned char*>(s.data()) , s.size() ) ;
249 if( m_hash_type ==
"MD5" )
251 unsigned char buffer[16] ;
252 mbedtls_md5_finish( &m_md5 , buffer ) ;
253 return std::string( reinterpret_cast<const char*>(buffer) ,
sizeof(buffer) ) ;
255 else if( m_hash_type ==
"SHA1" )
257 unsigned char buffer[20] ;
258 mbedtls_sha1_finish( &m_sha1 , buffer ) ;
259 return std::string( reinterpret_cast<const char*>(buffer) ,
sizeof(buffer) ) ;
261 else if( m_hash_type ==
"SHA256" )
263 unsigned char buffer[32] ;
264 mbedtls_sha256_finish( &m_sha256 , buffer ) ;
265 return std::string( reinterpret_cast<const char*>(buffer) ,
sizeof(buffer) ) ;
269 return std::string() ;
275 if( m_hash_type ==
"MD5" )
277 else if( m_hash_type ==
"SHA1" )
279 else if( m_hash_type ==
"SHA256" )
282 return std::string() ;
292 if( m_hash_type ==
"MD5" )
294 else if( m_hash_type ==
"SHA1" )
296 else if( m_hash_type ==
"SHA256" )
304 return valuesize() + 4U ;
309 GSsl::MbedTls::ProfileImp::ProfileImp(
const LibraryImp & library_imp ,
bool is_server_profile ,
310 const std::string & key_file ,
const std::string & cert_file ,
const std::string & ca_path ,
311 const std::string & default_peer_certificate_name ,
const std::string & default_peer_host_name ,
312 const std::string & profile_config ) :
313 m_library_imp(library_imp) ,
314 m_default_peer_certificate_name(default_peer_certificate_name) ,
315 m_default_peer_host_name(default_peer_host_name)
317 Cleanup<mbedtls_ssl_config> cleanup( mbedtls_ssl_config_free ) ;
320 Config extra_config = library_imp.config() ;
321 if( !profile_config.empty() )
324 extra_config =
Config( profile_config_list ) ;
325 if( !profile_config_list.empty() )
326 G_WARNING(
"GSsl::MbedTls::ProfileImp::ctor: tls-config: tls " << (is_server_profile?
"server":
"client")
327 <<
" profile configuration ignored: [" <<
G::Str::join(
",",profile_config_list) <<
"]" ) ;
332 static mbedtls_ssl_config config_zero ;
333 m_config = config_zero ;
334 mbedtls_ssl_config_init( &m_config ) ;
335 cleanup.reset( &m_config ) ;
336 int rc = mbedtls_ssl_config_defaults( &m_config ,
337 is_server_profile ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT ,
338 MBEDTLS_SSL_TRANSPORT_STREAM ,
339 MBEDTLS_SSL_PRESET_DEFAULT ) ;
340 if( rc )
throw Error(
"mbedtls_ssl_config_defaults" , rc ) ;
344 if( !key_file.empty() )
345 m_pk.load( key_file ) ;
346 if( !cert_file.empty() )
347 m_certificate.load( cert_file ) ;
350 if( m_certificate.loaded() )
352 int rc = mbedtls_ssl_conf_own_cert( &m_config , m_certificate.ptr() , m_pk.ptr() ) ;
354 throw Error(
"mbedtls_ssl_conf_own_cert" , rc ) ;
359 std::string ca_path_default =
"/etc/ssl/certs/ca-certificates.crt" ;
360 if( ca_path.empty() )
363 mbedtls_ssl_conf_authmode( &m_config , MBEDTLS_SSL_VERIFY_OPTIONAL ) ;
365 else if( ca_path ==
"<none>" )
368 mbedtls_ssl_conf_authmode( &m_config , MBEDTLS_SSL_VERIFY_NONE ) ;
370 else if( ca_path ==
"<default>" )
373 m_ca_list.load( ca_path_default ) ;
374 bool no_verify = extra_config.noverify() ;
375 mbedtls_ssl_conf_ca_chain( &m_config , m_ca_list.ptr() , crl() ) ;
376 mbedtls_ssl_conf_authmode( &m_config , no_verify ? MBEDTLS_SSL_VERIFY_OPTIONAL : MBEDTLS_SSL_VERIFY_REQUIRED ) ;
381 m_ca_list.load( ca_path ) ;
382 bool no_verify = extra_config.noverify() ;
383 mbedtls_ssl_conf_ca_chain( &m_config , m_ca_list.ptr() , crl() ) ;
384 mbedtls_ssl_conf_authmode( &m_config , no_verify ? MBEDTLS_SSL_VERIFY_OPTIONAL : MBEDTLS_SSL_VERIFY_REQUIRED ) ;
390 if( extra_config.min_() >= 0 )
391 mbedtls_ssl_conf_min_version( &m_config , MBEDTLS_SSL_MAJOR_VERSION_3 , extra_config.min_() ) ;
392 if( extra_config.max_() >= 0 )
393 mbedtls_ssl_conf_max_version( &m_config , MBEDTLS_SSL_MAJOR_VERSION_3 , extra_config.max_() ) ;
398 mbedtls_ssl_conf_rng( &m_config , mbedtls_ctr_drbg_random , m_library_imp.rng().ptr() ) ;
399 mbedtls_ssl_conf_dbg( &m_config , onDebug ,
this ) ;
404 mbedtls_ssl_conf_renegotiation( &m_config , MBEDTLS_SSL_RENEGOTIATION_DISABLED ) ;
409 GSsl::MbedTls::ProfileImp::~ProfileImp()
411 mbedtls_ssl_config_free( &m_config ) ;
415 const std::string & peer_host_name )
const
419 peer_certificate_name.empty()?defaultPeerCertificateName():peer_certificate_name ,
420 peer_host_name.empty()?defaultPeerHostName():peer_host_name ) ;
423 mbedtls_x509_crl * GSsl::MbedTls::ProfileImp::crl()
const
429 const mbedtls_ssl_config * GSsl::MbedTls::ProfileImp::config()
const
434 void GSsl::MbedTls::ProfileImp::onDebug(
void * This ,
int level_in ,
const char * file ,
int line ,
const char * message )
438 reinterpret_cast<ProfileImp*
>(This)->doDebug( level_in , file , line , message ) ;
445 void GSsl::MbedTls::ProfileImp::doDebug(
int level_in ,
const char * file ,
int line ,
const char * message )
459 int level_out = level_in >= 4 ? 0 : ( level_in >= 2 ? 1 : 3 ) ;
460 if( m_library_imp.log() && level_out )
462 std::ostringstream ss ;
463 G::Path path( file ? std::string(file) : std::string() ) ;
464 ss << path.basename() <<
"(" << line <<
"): " << (message?message:
"") ;
465 logAt( level_out , ss.str() ) ;
469 void GSsl::MbedTls::ProfileImp::logAt(
int level_out , std::string s )
const
471 Library::LogFn log_fn = m_library_imp.log() ;
476 (*log_fn)( level_out , s ) ;
480 const std::string & GSsl::MbedTls::ProfileImp::defaultPeerCertificateName()
const
482 return m_default_peer_certificate_name ;
485 const std::string & GSsl::MbedTls::ProfileImp::defaultPeerHostName()
const
487 return m_default_peer_host_name ;
492 GSsl::MbedTls::ProtocolImp::ProtocolImp(
const ProfileImp & profile ,
const std::string & required_peer_certificate_name ,
493 const std::string & target_peer_host_name ) :
496 m_ssl(profile.config()) ,
499 mbedtls_ssl_set_bio( m_ssl.ptr() , this , doSend , doRecv ,
nullptr ) ;
506 std::string name = target_peer_host_name.empty() ? required_peer_certificate_name : target_peer_host_name ;
508 mbedtls_ssl_set_hostname( m_ssl.ptr() , name.c_str() ) ;
511 GSsl::MbedTls::ProtocolImp::~ProtocolImp()
517 int rc = mbedtls_ssl_read( m_ssl.ptr() ,
reinterpret_cast<unsigned char*
>(buffer) , buffer_size_in ) ;
518 data_size_out = rc < 0 ? 0 : rc ;
519 if( rc == 0 )
return Protocol::Result_error ;
520 size_t available = rc > 0 ? mbedtls_ssl_get_bytes_avail( m_ssl.ptr() ) : 0U ;
521 return convert(
"mbedtls_ssl_read" , rc , available > 0U ) ;
525 ssize_t & data_size_out )
527 const unsigned char * p =
reinterpret_cast<const unsigned char *
>(buffer_in) ;
528 ssize_t n =
static_cast<ssize_t
>(data_size_in) ; G_ASSERT( n >= 0 ) ;
531 int rc = mbedtls_ssl_write( m_ssl.ptr() , p , n ) ;
534 data_size_out =
static_cast<ssize_t
>(data_size_in) ;
535 return Protocol::Result_ok ;
544 return convert(
"mbedtls_ssl_write" , rc ) ;
551 return Protocol::Result_ok ;
554 int GSsl::MbedTls::ProtocolImp::doRecvTimeout(
void * This ,
unsigned char * p ,
size_t n , uint32_t timeout_ms )
558 return doRecv( This , p , n ) ;
561 int GSsl::MbedTls::ProtocolImp::doRecv(
void * This ,
unsigned char * p ,
size_t n )
563 G::ReadWrite * io =
reinterpret_cast<ProtocolImp*
>(This)->m_io ;
564 G_ASSERT( io !=
nullptr ) ;
565 ssize_t rc = io->
read( reinterpret_cast<char*>(p) , n ) ;
566 if( rc < 0 && io->eWouldBlock() )
return MBEDTLS_ERR_SSL_WANT_READ ;
567 if( rc < 0 )
return MBEDTLS_ERR_NET_RECV_FAILED ;
571 int GSsl::MbedTls::ProtocolImp::doSend(
void * This ,
const unsigned char * p ,
size_t n )
573 G::ReadWrite * io =
reinterpret_cast<ProtocolImp*
>(This)->m_io ;
574 G_ASSERT( io !=
nullptr ) ;
575 ssize_t rc = io->
write( reinterpret_cast<const char*>(p) , n ) ;
576 if( rc < 0 && io->eWouldBlock() )
return MBEDTLS_ERR_SSL_WANT_WRITE ;
577 if( rc < 0 )
return MBEDTLS_ERR_NET_SEND_FAILED ;
581 GSsl::Protocol::Result GSsl::MbedTls::ProtocolImp::convert(
const char * fnname ,
int rc ,
bool more )
584 if( rc == MBEDTLS_ERR_SSL_WANT_READ )
return Protocol::Result_read ;
585 if( rc == MBEDTLS_ERR_SSL_WANT_WRITE )
return Protocol::Result_write ;
586 if( rc == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
return Protocol::Result_error ;
589 throw Error( fnname , rc , verifyResultString(rc) ) ;
591 return more ? Protocol::Result_more : Protocol::Result_ok ;
594 std::string GSsl::MbedTls::ProtocolImp::verifyResultString(
int rc )
596 if( rc == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
598 int verify_result = mbedtls_ssl_get_verify_result( m_ssl.ptr() ) ;
599 std::vector<char> buffer( 1024U ) ;
600 mbedtls_x509_crt_verify_info( &buffer[0] , buffer.size() ,
"" , verify_result ) ;
601 buffer[buffer.size()-1U] =
'\0' ;
606 return std::string() ;
622 GSsl::Protocol::Result GSsl::MbedTls::ProtocolImp::handshake()
624 int rc = mbedtls_ssl_handshake( m_ssl.ptr() ) ;
625 Result result = convert(
"mbedtls_ssl_handshake" , rc ) ;
626 if( result == Protocol::Result_ok )
628 const char * vstr =
"" ;
629 if( m_ssl.ptr()->conf->authmode == MBEDTLS_SSL_VERIFY_NONE )
632 vstr =
"peer certificate not verified" ;
634 else if( m_ssl.ptr()->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
636 int v = mbedtls_ssl_get_verify_result( m_ssl.ptr() ) ;
637 m_verified = v == 0 ;
638 vstr = v == 0 ?
"peer certificate verified" :
"peer certificate failed to verify" ;
639 if( v & MBEDTLS_X509_BADCERT_EXPIRED ) vstr =
"peer certificate has expired" ;
640 if( v & MBEDTLS_X509_BADCERT_REVOKED ) vstr =
"peer certificate has been revoked" ;
641 if( v & MBEDTLS_X509_BADCERT_NOT_TRUSTED ) vstr =
"peer certificate not signed by a trusted ca" ;
642 if( v & MBEDTLS_X509_BADCERT_MISSING ) vstr =
"peer certificate missing" ;
643 if( v & MBEDTLS_X509_BADCERT_SKIP_VERIFY ) vstr =
"peer certificate verification was skipped" ;
648 vstr =
"peer certificate verified" ;
651 m_peer_certificate = getPeerCertificate() ;
652 m_peer_certificate_chain = m_peer_certificate ;
654 m_profile.logAt( 2 , std::string() +
"protocol=" + protocol() +
", cipher=" + cipher() +
", verification=[" + vstr +
"]" ) ;
659 std::string GSsl::MbedTls::ProtocolImp::protocol()
const
661 const char * p = mbedtls_ssl_get_version( m_ssl.ptr() ) ;
665 std::string GSsl::MbedTls::ProtocolImp::cipher()
const
667 const char * p = mbedtls_ssl_get_ciphersuite( m_ssl.ptr() ) ;
671 const GSsl::Profile & GSsl::MbedTls::ProtocolImp::profile()
const
676 std::string GSsl::MbedTls::ProtocolImp::getPeerCertificate()
679 const mbedtls_x509_crt * certificate = mbedtls_ssl_get_peer_cert( m_ssl.ptr() ) ;
680 if( certificate !=
nullptr )
682 const char * head =
"-----BEGIN CERTIFICATE-----\n" ;
683 const char * tail =
"-----END CERTIFICATE-----\n" ;
686 unsigned char c =
'\0' ;
687 int rc = mbedtls_pem_write_buffer( head , tail , certificate->raw.p , certificate->raw.len , &c , 0 , &n ) ;
688 if( n == 0U || rc != MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL )
689 throw Error(
"certificate error" ) ;
692 std::vector<unsigned char> buffer( n ) ;
693 rc = mbedtls_pem_write_buffer( head , tail , certificate->raw.p , certificate->raw.len ,
694 &buffer[0] , buffer.size() , &n ) ;
695 if( n == 0 || rc != 0 )
696 throw Error(
"certificate error" ) ;
698 result = std::string( reinterpret_cast<const char *>(&buffer[0]) , n-1U ) ;
699 if( std::string(result.c_str()) != result || result.find(tail) == std::string::npos )
700 throw Error(
"certificate error" ) ;
707 return m_peer_certificate ;
712 return m_peer_certificate_chain ;
722 GSsl::MbedTls::Context::Context(
const mbedtls_ssl_config * config_p )
724 static mbedtls_ssl_context x_zero ;
726 mbedtls_ssl_init( &x ) ;
728 int rc = mbedtls_ssl_setup( &x , config_p ) ;
729 if( rc )
throw Error(
"mbedtls_ssl_setup" , rc ) ;
732 GSsl::MbedTls::Context::~Context()
734 mbedtls_ssl_free( &x ) ;
737 mbedtls_ssl_context * GSsl::MbedTls::Context::ptr()
742 mbedtls_ssl_context * GSsl::MbedTls::Context::ptr()
const
744 return const_cast<mbedtls_ssl_context*
>(&x) ;
749 GSsl::MbedTls::Rng::Rng()
751 static mbedtls_entropy_context entropy_zero ;
752 entropy = entropy_zero ;
753 mbedtls_entropy_init( &entropy ) ;
755 static mbedtls_ctr_drbg_context x_zero ;
757 mbedtls_ctr_drbg_init( &x ) ;
759 unsigned char extra[] =
"sdflkjsdlkjsdfkljxmvnxcvmxmncvx" ;
760 int rc = mbedtls_ctr_drbg_seed( &x , mbedtls_entropy_func , &entropy , extra ,
sizeof(extra) ) ;
763 mbedtls_entropy_free( &entropy ) ;
764 throw Error(
"mbedtls_ctr_drbg_init" , rc ) ;
768 GSsl::MbedTls::Rng::~Rng()
770 mbedtls_ctr_drbg_free( &x ) ;
771 mbedtls_entropy_free( &entropy ) ;
774 mbedtls_ctr_drbg_context * GSsl::MbedTls::Rng::ptr()
779 mbedtls_ctr_drbg_context * GSsl::MbedTls::Rng::ptr()
const
781 return const_cast<mbedtls_ctr_drbg_context*
>(&x) ;
786 static void scrub(
unsigned char *p_in ,
size_t n )
789 volatile unsigned char *p = p_in ;
794 GSsl::MbedTls::SecureFile::SecureFile(
const std::string & path ,
bool with_nul )
797 std::filebuf * fp = nullptr ;
800 const char * path_p = path.c_str() ;
803 fp = f.open( path_p , std::ios_base::in | std::ios_base::binary ) ;
805 std::streamoff n = 0 ;
806 bool ok = fp != nullptr ;
807 if( ok ) n = fp->pubseekoff( 0 , std::ios_base::end , std::ios_base::in ) ;
808 if( ok ) m_buffer.resize( static_cast<size_t>(n) ) ;
809 if( ok ) fp->pubseekpos( 0 , std::ios_base::in ) ;
810 if( ok ) ok = fp->sgetn( &m_buffer[0] , m_buffer.size() ) == static_cast<std::streamsize>(m_buffer.size()) ;
811 if( !ok ) scrub( pu() , size() ) ;
812 if( fp ) { fp->close() ; fp = nullptr ; }
813 if( ok && with_nul ) m_buffer.push_back(
'\0' ) ;
817 scrub( pu() , size() ) ;
818 if( fp ) fp->close() ;
823 GSsl::MbedTls::SecureFile::~SecureFile()
825 scrub( pu() , size() ) ;
828 const char * GSsl::MbedTls::SecureFile::p()
const
830 static char c =
'\0' ;
831 return m_buffer.size() ? &m_buffer[0] : &c ;
834 const unsigned char * GSsl::MbedTls::SecureFile::pu()
const
836 return reinterpret_cast<const unsigned char*
>(p()) ;
839 unsigned char * GSsl::MbedTls::SecureFile::pu()
841 return const_cast<unsigned char*
>(
reinterpret_cast<const unsigned char*
>(p())) ;
844 size_t GSsl::MbedTls::SecureFile::size()
const
846 return m_buffer.size() ;
849 bool GSsl::MbedTls::SecureFile::empty()
const
851 return size() == 0U ;
856 GSsl::MbedTls::Key::Key()
858 mbedtls_pk_init( &x ) ;
861 GSsl::MbedTls::Key::~Key()
863 mbedtls_pk_free( &x ) ;
866 void GSsl::MbedTls::Key::load(
const std::string & pem_file )
868 SecureFile file( pem_file ,
true ) ;
870 throw Error(
"cannot load private key from " + pem_file ) ;
872 int rc = mbedtls_pk_parse_key( &x , file.pu() , file.size() , nullptr , 0 ) ;
874 throw Error(
"mbedtls_pk_parse_key" , rc ) ;
879 mbedtls_pk_context * GSsl::MbedTls::Key::ptr()
884 mbedtls_pk_context * GSsl::MbedTls::Key::ptr()
const
886 return const_cast<mbedtls_pk_context*
>(&x) ;
891 GSsl::MbedTls::Certificate::Certificate() :
894 mbedtls_x509_crt_init( &x ) ;
897 GSsl::MbedTls::Certificate::~Certificate()
899 mbedtls_x509_crt_free( &x ) ;
902 void GSsl::MbedTls::Certificate::load(
const std::string & path )
904 SecureFile file( path ,
true ) ;
906 throw Error(
"cannot load certificates from " + path ) ;
908 int rc = mbedtls_x509_crt_parse( &x , file.pu() , file.size() ) ;
910 throw Error(
"mbedtls_x509_crt_parse" , rc ) ;
912 throw Error(
"mbedtls_x509_crt_parse" ) ;
917 bool GSsl::MbedTls::Certificate::loaded()
const
922 mbedtls_x509_crt * GSsl::MbedTls::Certificate::ptr()
924 return loaded() ? &x : nullptr ;
927 mbedtls_x509_crt * GSsl::MbedTls::Certificate::ptr()
const
930 return const_cast<mbedtls_x509_crt*
>(&x) ;
937 GSsl::MbedTls::Error::Error(
const std::string & s ) :
938 m_what(
"tls error: "+s)
942 GSsl::MbedTls::Error::Error(
const std::string & fnname ,
int rc ,
const std::string & more ) :
943 m_what(
"tls error: "+fnname+
"()")
945 char buffer[200] = {
'\0' } ;
946 mbedtls_strerror( rc , buffer ,
sizeof(buffer) ) ;
947 buffer[
sizeof(buffer)-1] =
'\0' ;
949 std::ostringstream ss ;
952 ss <<
" [" << more <<
"]" ;
954 m_what.append( ss.str() ) ;
957 GSsl::MbedTls::Error::~Error() throw ()
961 const char * GSsl::MbedTls::Error::what()
const throw ()
963 return m_what.c_str() ;
A base interface for profile classes that work with concrete classes derived from GSsl::LibraryImpBas...
An interface to an underlying TLS library.
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
An abstract interface for reading and writing from a non-blocking i/o channel.
A base interface for GSsl::Protocol pimple classes.
virtual ssize_type write(const char *buf, size_type len)=0
Sends data.
virtual std::string id() const override
Implements Library::id().
Holds protocol version information, etc.
An implementation of the GSsl::LibraryImpBase interface for mbedtls.
An exception class for GSsl::MbedTls classes.
virtual ProtocolImpBase * newProtocol(const std::string &, const std::string &) const override
Factory method for a new Protocol object on the heap.
static void decode(const std::string &, uint_type *, size_type &)
Converts an encode()d string back into a hash state of N integers and a data size returned by referen...
std::vector< std::string > StringArray
A std::vector of std::strings.
A class which acquires the process's special privileges on construction and releases them on destruct...
static void splitIntoTokens(const std::string &in, StringArray &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
virtual const Profile & profile(const std::string &profile_name) const override
Implements Library::profile().
virtual size_t blocksize() const override
Implements Digester::blocksize().
virtual Result connect(G::ReadWrite &) override
Implements Protocol::connect().
Holds a mbedtls_ctr_drbg_context structure.
virtual void add(const std::string &) override
Implements Digester::add().
An implementation of the GSsl::DigesterImpBase interface for MbedTls.
An implementation of the GSsl::ProtocolImpBase interface for mbedtls.
virtual ssize_type read(char *buffer, size_type buffer_length)=0
Reads data.
virtual Result read(char *buffer, size_t buffer_size_in, ssize_t &data_size_out) override
Implements Protocol::read().
virtual bool verified() const override
Implements Protocol::verified().
virtual bool hasProfile(const std::string &profile_name) const override
Implements Library::hasProfile().
Returns an object that can perform a cryptographic hash.
static bool consume(G::StringArray &list, const std::string &item)
A convenience function that removes the item from the list and returns true iff is was removed...
virtual std::string peerCertificate() const override
Implements Protocol::peerCertificate().
An implementation of the GSsl::Profile interface for mbedtls.
virtual Result accept(G::ReadWrite &) override
Implements Protocol::accept().
virtual size_t statesize() const override
Implements Digester::statesize().
static std::string trimmed(const std::string &s, const std::string &ws)
Returns a trim()med version of s.
virtual Result write(const char *buffer, size_t data_size_in, ssize_t &data_size_out) override
Implements Protocol::write().
virtual std::string state() override
Implements Digester::state().
virtual std::string peerCertificateChain() const override
Implements Protocol::peerCertificateChain().
virtual std::string value() override
Implements Digester::value().
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings with separators.
virtual Result stop() override
Implements Protocol::stop().
virtual size_t valuesize() const override
Implements Digester::valuesize().
static std::string encode(const uint_type *)
Returns the hash state as a string, typically containing non-printing characters. ...
virtual G::StringArray digesters(bool) const override
Implements Library::digesters().
virtual Digester digester(const std::string &, const std::string &) const override
Implements Library::digester().
virtual void addProfile(const std::string &profile_name, bool is_server_profile, const std::string &key_file, const std::string &cert_file, const std::string &ca_file, const std::string &default_peer_certificate_name, const std::string &default_peer_host_name, const std::string &profile_config) override
Implements Library::addProfile().
A Path object represents a file system path.
static std::string ws()
Returns a string of standard whitespace characters.