123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- * Copyright (c) 2016 Guilherme Ferreira <guilherme.maciel.ferreira@gmail.com>
- * Copyright (c) 2016-2021 Frank Pagliughi <fpagliughi@mindspring.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:
- * and the Eclipse Distribution License is available at
- * http:
- *
- * 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 "MQTTAsync.h"
- #include "mqtt/message.h"
- #include "mqtt/topic.h"
- #include "mqtt/types.h"
- #include "mqtt/platform.h"
- #include <vector>
- #include <functional>
- namespace mqtt {
- * Holds the set of SSL options for connection.
- */
- 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:
-
- PAHO_MQTTPP_EXPORT static const MQTTAsync_SSLOptions DFLT_C_STRUCT ;
-
- MQTTAsync_SSLOptions opts_;
-
- string trustStore_;
-
- string keyStore_;
-
- string privateKey_;
-
- string privateKeyPassword_;
-
- string caPath_;
-
- string enabledCipherSuites_;
-
- error_handler errHandler_;
-
- psk_handler pskHandler_;
-
- std::basic_string<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;
- /**
- * Gets a pointer to the C-language NUL-terminated strings for the
- * struct.
- * @note In the SSL options, by default, the Paho C treats nullptr char
- * arrays as unset values, so we keep that semantic and only set those
- * char arrays if the string is non-empty.
- * @param str The C++ string object.
- * @return Pointer to a NUL terminated string. This is only valid until
- * the next time the string is updated.
- */
- 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 to build the SSL options for connections.
- */
- 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_; }
- };
- }
- #endif
|