paho-mqtt-cpp
MQTT C++ Client for POSIX and Windows
Loading...
Searching...
No Matches
ssl_options.h
Go to the documentation of this file.
1
7
8/*******************************************************************************
9 * Copyright (c) 2016 Guilherme Ferreira <guilherme.maciel.ferreira@gmail.com>
10 * Copyright (c) 2016-2021 Frank Pagliughi <fpagliughi@mindspring.com>
11 *
12 * All rights reserved. This program and the accompanying materials
13 * are made available under the terms of the Eclipse Public License v2.0
14 * and Eclipse Distribution License v1.0 which accompany this distribution.
15 *
16 * The Eclipse Public License is available at
17 * http://www.eclipse.org/legal/epl-v20.html
18 * and the Eclipse Distribution License is available at
19 * http://www.eclipse.org/org/documents/edl-v10.php.
20 *
21 * Contributors:
22 * Guilherme Ferreira - initial implementation and documentation
23 * Frank Pagliughi - added copy & move operations
24 * Frank Pagliughi - upgraded compatibility to Paho C 1.3
25 *******************************************************************************/
26
27#ifndef __mqtt_ssl_options_h
28#define __mqtt_ssl_options_h
29
30#include "MQTTAsync.h"
31#include "mqtt/message.h"
32#include "mqtt/topic.h"
33#include "mqtt/types.h"
34#include "mqtt/platform.h"
35#include <vector>
36#include <functional>
37
38namespace mqtt {
39
41
46{
47public:
49 using ptr_t = std::shared_ptr<ssl_options>;
51 using const_ptr_t = std::shared_ptr<const ssl_options>;
53 using unique_ptr_t = std::unique_ptr<ssl_options>;
54
56 using error_handler = std::function<void(const string& errMsg)>;
62 using psk_handler = std::function<unsigned(const string& hint,
63 char *identity, size_t max_identity_len,
64 unsigned char *psk, size_t max_psk_len)>;
65
66private:
68 PAHO_MQTTPP_EXPORT static const MQTTAsync_SSLOptions DFLT_C_STRUCT ;
69
71 MQTTAsync_SSLOptions opts_;
72
77 string trustStore_;
78
80 string keyStore_;
81
83 string privateKey_;
84
86 string privateKeyPassword_;
87
89 string caPath_;
90
95 string enabledCipherSuites_;
96
98 error_handler errHandler_;
99
101 psk_handler pskHandler_;
102
104 std::basic_string<unsigned char> protos_;
105
107 static int on_error(const char *str, size_t len, void *context);
108 static unsigned on_psk(const char *hint, char *identity, unsigned int max_identity_len,
109 unsigned char *psk, unsigned int max_psk_len, void *context);
110
112 friend class connect_options;
113
124 const char* c_str(const string& str) {
125 return str.empty() ? nullptr : str.c_str();
126 }
130 void update_c_struct();
131
132public:
152 ssl_options(const string& trustStore, const string& keyStore,
153 const string& privateKey, const string& privateKeyPassword,
154 const string& enabledCipherSuites, bool enableServerCertAuth,
155 const std::vector<string> alpnProtos=std::vector<string>());
174 ssl_options(const string& trustStore, const string& keyStore,
175 const string& privateKey, const string& privateKeyPassword,
176 const string& caPath,
177 const string& enabledCipherSuites, bool enableServerCertAuth,
178 const std::vector<string> alpnProtos=std::vector<string>());
204 #if defined(UNIT_TESTS)
205 const MQTTAsync_SSLOptions& c_struct() const { return opts_; }
206 #endif
212 string get_trust_store() const { return trustStore_; }
217 string get_key_store() const { return keyStore_; }
222 string get_private_key() const { return privateKey_; }
227 string get_private_key_password() const { return privateKeyPassword_; }
233 string get_enabled_cipher_suites() const { return enabledCipherSuites_; }
239 return to_bool(opts_.enableServerCertAuth);
240 }
247 void set_trust_store(const string& trustStore);
254 void set_key_store(const string& keyStore);
261 void set_private_key(const string& privateKey);
267 void set_private_key_password(const string& privateKeyPassword);
285 void set_enabled_cipher_suites(const string& enabledCipherSuites);
291 void set_enable_server_cert_auth(bool enableServerCertAuth);
296 int get_ssl_version() const { return opts_.sslVersion; }
306 void set_ssl_version(int ver) { opts_.sslVersion = ver; }
312 bool get_verify() const { return to_bool(opts_.verify); }
318 void set_verify(bool v) { opts_.verify = to_int(v); }
326 string get_ca_path() const { return caPath_; }
327 string ca_path() const { return caPath_; }
335 void set_ca_path(const string& path);
336 void ca_path(const string& path) { set_ca_path(path); }
352 std::vector<string> get_alpn_protos() const;
359 void set_alpn_protos(const std::vector<string>& protos);
360};
361
370
371
373
378{
380 ssl_options opts_;
381
382public:
395 auto trust_store(const string& store) -> self& {
396 opts_.set_trust_store(store);
397 return *this;
398 }
405 auto key_store(const string& store) -> self& {
406 opts_.set_key_store(store);
407 return *this;
408 }
414 auto private_key(const string& key) -> self& {
415 opts_.set_private_key(key);
416 return *this;
417 }
422 auto private_keypassword(const string& passwd) -> self& {
423 opts_.set_private_key_password(passwd);
424 return *this;
425 }
440 auto enabled_cipher_suites(const string& suites) -> self& {
441 opts_.set_enabled_cipher_suites(suites);
442 return *this;
443 }
448 auto enable_server_cert_auth(bool on) -> self& {
450 return *this;
451 }
461 auto ssl_version(int ver) -> self& {
462 opts_.set_ssl_version(ver);
463 return *this;
464 }
470 auto verify(bool on=true) -> self& {
471 opts_.set_verify(on);
472 return *this;
473 }
479 auto ca_path(const string& path) -> self& {
480 opts_.ca_path(path);
481 return *this;
482 }
488 opts_.set_error_handler(cb);
489 return *this;
490 }
497 opts_.set_psk_handler(cb);
498 return *this;
499 }
504 auto alpn_protos(const std::vector<string>& protos) -> self& {
505 opts_.set_alpn_protos(protos);
506 return *this;
507 }
512 ssl_options finalize() { return opts_; }
513};
514
516// end namespace mqtt
517}
518
519#endif // __mqtt_ssl_options_h
520
Definition: connect_options.h:49
Definition: ssl_options.h:378
auto verify(bool on=true) -> self &
Definition: ssl_options.h:470
auto ca_path(const string &path) -> self &
Definition: ssl_options.h:479
ssl_options_builder()
Definition: ssl_options.h:388
auto error_handler(ssl_options::error_handler cb) -> self &
Definition: ssl_options.h:487
auto psk_handler(ssl_options::psk_handler cb) -> self &
Definition: ssl_options.h:496
auto private_key(const string &key) -> self &
Definition: ssl_options.h:414
auto trust_store(const string &store) -> self &
Definition: ssl_options.h:395
ssl_options finalize()
Definition: ssl_options.h:512
auto private_keypassword(const string &passwd) -> self &
Definition: ssl_options.h:422
auto enabled_cipher_suites(const string &suites) -> self &
Definition: ssl_options.h:440
auto ssl_version(int ver) -> self &
Definition: ssl_options.h:461
auto alpn_protos(const std::vector< string > &protos) -> self &
Definition: ssl_options.h:504
auto key_store(const string &store) -> self &
Definition: ssl_options.h:405
auto enable_server_cert_auth(bool on) -> self &
Definition: ssl_options.h:448
Definition: ssl_options.h:46
void set_ssl_version(int ver)
Definition: ssl_options.h:306
void set_enabled_cipher_suites(const string &enabledCipherSuites)
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 >())
string get_ca_path() const
Definition: ssl_options.h:326
void set_key_store(const string &keyStore)
std::shared_ptr< const ssl_options > const_ptr_t
Definition: ssl_options.h:51
void set_private_key_password(const string &privateKeyPassword)
string ca_path() const
Definition: ssl_options.h:327
string get_private_key() const
Definition: ssl_options.h:222
void set_alpn_protos(const std::vector< string > &protos)
int get_ssl_version() const
Definition: ssl_options.h:296
void set_enable_server_cert_auth(bool enableServerCertAuth)
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:64
ssl_options & operator=(ssl_options &&opt)
ssl_options(const ssl_options &opt)
std::unique_ptr< ssl_options > unique_ptr_t
Definition: ssl_options.h:53
string get_key_store() const
Definition: ssl_options.h:217
void set_ca_path(const string &path)
string get_trust_store() const
Definition: ssl_options.h:212
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 >())
std::shared_ptr< ssl_options > ptr_t
Definition: ssl_options.h:49
void set_verify(bool v)
Definition: ssl_options.h:318
bool get_verify() const
Definition: ssl_options.h:312
void set_trust_store(const string &trustStore)
std::vector< string > get_alpn_protos() const
string get_private_key_password() const
Definition: ssl_options.h:227
string get_enabled_cipher_suites() const
Definition: ssl_options.h:233
std::function< void(const string &errMsg)> error_handler
Definition: ssl_options.h:56
ssl_options(ssl_options &&opt)
void ca_path(const string &path)
Definition: ssl_options.h:336
void set_psk_handler(psk_handler cb)
void set_error_handler(error_handler cb)
ssl_options & operator=(const ssl_options &opt)
bool get_enable_server_cert_auth() const
Definition: ssl_options.h:238
void set_private_key(const string &privateKey)
#define PAHO_MQTTPP_EXPORT
Definition: export.h:40
Definition: async_client.h:49
bool to_bool(int n)
Definition: types.h:161
ssl_options::unique_ptr_t ssl_options_unique_ptr
Definition: ssl_options.h:369
int to_int(bool b)
Definition: types.h:167
ssl_options::ptr_t ssl_options_ptr
Definition: ssl_options.h:365