Loading...
Searching...
No Matches
ssl_options.h
Go to the documentation of this file.
/*******************************************************************************
* Copyright (c) 2016-2024 Frank Pagliughi <fpagliughi@mindspring.com>
* Copyright (c) 2016 Guilherme Ferreira <guilherme.maciel.ferreira@gmail.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Guilherme Ferreira - initial implementation and documentation
* Frank Pagliughi - added copy & move operations
* Frank Pagliughi - upgraded compatibility to Paho C 1.3
*******************************************************************************/
#ifndef __mqtt_ssl_options_h
#define __mqtt_ssl_options_h
#include <functional>
#include <vector>
#include "MQTTAsync.h"
#include "mqtt/message.h"
#include "mqtt/platform.h"
#include "mqtt/topic.h"
#include "mqtt/types.h"
namespace mqtt {
class ssl_options
{
public:
using ptr_t = std::shared_ptr<ssl_options>;
using const_ptr_t = std::shared_ptr<const ssl_options>;
using unique_ptr_t = std::unique_ptr<ssl_options>;
using error_handler = std::function<void(const string& errMsg)>;
using psk_handler = std::function<unsigned(
const string& hint, char* identity, size_t max_identity_len, unsigned char* psk,
size_t max_psk_len
)>;
private:
static constexpr MQTTAsync_SSLOptions DFLT_C_STRUCT MQTTAsync_SSLOptions_initializer;
MQTTAsync_SSLOptions opts_{DFLT_C_STRUCT};
string trustStore_;
string keyStore_;
string privateKey_;
string privateKeyPassword_;
string caPath_;
string enabledCipherSuites_;
error_handler errHandler_;
psk_handler pskHandler_;
std::vector<unsigned char> protos_;
static int on_error(const char* str, size_t len, void* context);
static unsigned on_psk(
const char* hint, char* identity, unsigned int max_identity_len, unsigned char* psk,
unsigned int max_psk_len, void* context
);
friend class connect_options;
const char* c_str(const string& str) { return str.empty() ? nullptr : str.c_str(); }
void update_c_struct();
public:
ssl_options() {}
ssl_options(
const string& trustStore, const string& keyStore, const string& privateKey,
const string& privateKeyPassword, const string& enabledCipherSuites,
bool enableServerCertAuth,
const std::vector<string> alpnProtos = std::vector<string>()
);
ssl_options(
const string& trustStore, const string& keyStore, const string& privateKey,
const string& privateKeyPassword, const string& caPath,
const string& enabledCipherSuites, bool enableServerCertAuth,
const std::vector<string> alpnProtos = std::vector<string>()
);
ssl_options(const ssl_options& opt);
ssl_options(ssl_options&& opt);
ssl_options& operator=(const ssl_options& opt);
ssl_options& operator=(ssl_options&& opt);
#if defined(UNIT_TESTS)
const MQTTAsync_SSLOptions& c_struct() const { return opts_; }
#endif
string get_trust_store() const { return trustStore_; }
string get_key_store() const { return keyStore_; }
string get_private_key() const { return privateKey_; }
string get_private_key_password() const { return privateKeyPassword_; }
string get_enabled_cipher_suites() const { return enabledCipherSuites_; }
bool get_enable_server_cert_auth() const { return to_bool(opts_.enableServerCertAuth); }
void set_trust_store(const string& trustStore);
void set_key_store(const string& keyStore);
void set_private_key(const string& privateKey);
void set_private_key_password(const string& privateKeyPassword);
void set_enabled_cipher_suites(const string& enabledCipherSuites);
void set_enable_server_cert_auth(bool enableServerCertAuth);
int get_ssl_version() const { return opts_.sslVersion; }
void set_ssl_version(int ver) { opts_.sslVersion = ver; }
bool get_verify() const { return to_bool(opts_.verify); }
void set_verify(bool v) { opts_.verify = to_int(v); }
string get_ca_path() const { return caPath_; }
string ca_path() const { return caPath_; }
void set_ca_path(const string& path);
void ca_path(const string& path) { set_ca_path(path); }
void set_error_handler(error_handler cb);
void set_psk_handler(psk_handler cb);
std::vector<string> get_alpn_protos() const;
void set_alpn_protos(const std::vector<string>& protos);
};
using ssl_options_ptr = ssl_options::ptr_t;
using ssl_options_unique_ptr = ssl_options::unique_ptr_t;
class ssl_options_builder
{
ssl_options opts_;
public:
using self = ssl_options_builder;
ssl_options_builder() {}
auto trust_store(const string& store) -> self& {
opts_.set_trust_store(store);
return *this;
}
auto key_store(const string& store) -> self& {
opts_.set_key_store(store);
return *this;
}
auto private_key(const string& key) -> self& {
opts_.set_private_key(key);
return *this;
}
auto private_keypassword(const string& passwd) -> self& {
opts_.set_private_key_password(passwd);
return *this;
}
auto enabled_cipher_suites(const string& suites) -> self& {
opts_.set_enabled_cipher_suites(suites);
return *this;
}
auto enable_server_cert_auth(bool on) -> self& {
opts_.set_enable_server_cert_auth(on);
return *this;
}
auto ssl_version(int ver) -> self& {
opts_.set_ssl_version(ver);
return *this;
}
auto verify(bool on = true) -> self& {
opts_.set_verify(on);
return *this;
}
auto ca_path(const string& path) -> self& {
opts_.ca_path(path);
return *this;
}
auto error_handler(ssl_options::error_handler cb) -> self& {
opts_.set_error_handler(cb);
return *this;
}
auto psk_handler(ssl_options::psk_handler cb) -> self& {
opts_.set_psk_handler(cb);
return *this;
}
auto alpn_protos(const std::vector<string>& protos) -> self& {
opts_.set_alpn_protos(protos);
return *this;
}
ssl_options finalize() { return opts_; }
};
} // namespace mqtt
#endif // __mqtt_ssl_options_h
mqtt::ssl_options_builder::verify
auto verify(bool on=true) -> self &
Definition ssl_options.h:472
mqtt::ssl_options_builder::ca_path
auto ca_path(const string &path) -> self &
Definition ssl_options.h:481
mqtt::ssl_options_builder::ssl_options_builder
ssl_options_builder()
Definition ssl_options.h:390
mqtt::ssl_options_builder::error_handler
auto error_handler(ssl_options::error_handler cb) -> self &
Definition ssl_options.h:489
mqtt::ssl_options_builder::psk_handler
auto psk_handler(ssl_options::psk_handler cb) -> self &
Definition ssl_options.h:498
mqtt::ssl_options_builder::private_key
auto private_key(const string &key) -> self &
Definition ssl_options.h:416
mqtt::ssl_options_builder::trust_store
auto trust_store(const string &store) -> self &
Definition ssl_options.h:397
mqtt::ssl_options_builder::finalize
ssl_options finalize()
Definition ssl_options.h:514
mqtt::ssl_options_builder::self
ssl_options_builder self
Definition ssl_options.h:386
mqtt::ssl_options_builder::private_keypassword
auto private_keypassword(const string &passwd) -> self &
Definition ssl_options.h:424
mqtt::ssl_options_builder::enabled_cipher_suites
auto enabled_cipher_suites(const string &suites) -> self &
Definition ssl_options.h:442
mqtt::ssl_options_builder::ssl_version
auto ssl_version(int ver) -> self &
Definition ssl_options.h:463
mqtt::ssl_options_builder::alpn_protos
auto alpn_protos(const std::vector< string > &protos) -> self &
Definition ssl_options.h:506
mqtt::ssl_options_builder::key_store
auto key_store(const string &store) -> self &
Definition ssl_options.h:407
mqtt::ssl_options_builder::enable_server_cert_auth
auto enable_server_cert_auth(bool on) -> self &
Definition ssl_options.h:450
mqtt::ssl_options
Definition ssl_options.h:47
mqtt::ssl_options::set_ssl_version
void set_ssl_version(int ver)
Definition ssl_options.h:309
mqtt::ssl_options::set_enabled_cipher_suites
void set_enabled_cipher_suites(const string &enabledCipherSuites)
mqtt::ssl_options::ssl_options
ssl_options(const string &trustStore, const string &keyStore, const string &privateKey, const string &privateKeyPassword, const string &enabledCipherSuites, bool enableServerCertAuth, const std::vector< string > alpnProtos=std::vector< string >())
mqtt::ssl_options::psk_handler
std::function< unsigned( const string &hint, char *identity, size_t max_identity_len, unsigned char *psk, size_t max_psk_len)> psk_handler
Definition ssl_options.h:63
mqtt::ssl_options::get_ca_path
string get_ca_path() const
Definition ssl_options.h:329
mqtt::ssl_options::set_key_store
void set_key_store(const string &keyStore)
mqtt::ssl_options::const_ptr_t
std::shared_ptr< const ssl_options > const_ptr_t
Definition ssl_options.h:52
mqtt::ssl_options::set_private_key_password
void set_private_key_password(const string &privateKeyPassword)
mqtt::ssl_options::ca_path
string ca_path() const
Definition ssl_options.h:330
mqtt::ssl_options::get_private_key
string get_private_key() const
Definition ssl_options.h:227
mqtt::ssl_options::set_alpn_protos
void set_alpn_protos(const std::vector< string > &protos)
mqtt::ssl_options::get_ssl_version
int get_ssl_version() const
Definition ssl_options.h:299
mqtt::ssl_options::set_enable_server_cert_auth
void set_enable_server_cert_auth(bool enableServerCertAuth)
mqtt::ssl_options::operator=
ssl_options & operator=(ssl_options &&opt)
mqtt::ssl_options::ssl_options
ssl_options(const ssl_options &opt)
mqtt::ssl_options::unique_ptr_t
std::unique_ptr< ssl_options > unique_ptr_t
Definition ssl_options.h:54
mqtt::ssl_options::get_key_store
string get_key_store() const
Definition ssl_options.h:222
mqtt::ssl_options::set_ca_path
void set_ca_path(const string &path)
mqtt::ssl_options::get_trust_store
string get_trust_store() const
Definition ssl_options.h:217
mqtt::ssl_options::ssl_options
ssl_options(const string &trustStore, const string &keyStore, const string &privateKey, const string &privateKeyPassword, const string &caPath, const string &enabledCipherSuites, bool enableServerCertAuth, const std::vector< string > alpnProtos=std::vector< string >())
mqtt::ssl_options::ptr_t
std::shared_ptr< ssl_options > ptr_t
Definition ssl_options.h:50
mqtt::ssl_options::set_verify
void set_verify(bool v)
Definition ssl_options.h:321
mqtt::ssl_options::get_verify
bool get_verify() const
Definition ssl_options.h:315
mqtt::ssl_options::set_trust_store
void set_trust_store(const string &trustStore)
mqtt::ssl_options::get_alpn_protos
std::vector< string > get_alpn_protos() const
mqtt::ssl_options::connect_options
friend class connect_options
Definition ssl_options.h:116
mqtt::ssl_options::get_private_key_password
string get_private_key_password() const
Definition ssl_options.h:232
mqtt::ssl_options::get_enabled_cipher_suites
string get_enabled_cipher_suites() const
Definition ssl_options.h:238
mqtt::ssl_options::error_handler
std::function< void(const string &errMsg)> error_handler
Definition ssl_options.h:57
mqtt::ssl_options::ssl_options
ssl_options(ssl_options &&opt)
mqtt::ssl_options::ca_path
void ca_path(const string &path)
Definition ssl_options.h:339
mqtt::ssl_options::ssl_options
ssl_options()
Definition ssl_options.h:138
mqtt::ssl_options::set_psk_handler
void set_psk_handler(psk_handler cb)
mqtt::ssl_options::set_error_handler
void set_error_handler(error_handler cb)
mqtt::ssl_options::operator=
ssl_options & operator=(const ssl_options &opt)
mqtt::ssl_options::get_enable_server_cert_auth
bool get_enable_server_cert_auth() const
Definition ssl_options.h:243
mqtt::ssl_options::set_private_key
void set_private_key(const string &privateKey)
message.h
mqtt
Definition async_client.h:60
mqtt::to_bool
bool to_bool(int n)
Definition types.h:107
mqtt::ssl_options_unique_ptr
ssl_options::unique_ptr_t ssl_options_unique_ptr
Definition ssl_options.h:372
mqtt::ssl_options_ptr
ssl_options::ptr_t ssl_options_ptr
Definition ssl_options.h:368
mqtt::to_int
int to_int(bool b)
Definition types.h:113
platform.h
topic.h
types.h