PahoMqttCpp
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-2024 Frank Pagliughi <fpagliughi@mindspring.com>
10 * Copyright (c) 2016 Guilherme Ferreira <guilherme.maciel.ferreira@gmail.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 <functional>
31#include <vector>
32
33#include "MQTTAsync.h"
34#include "mqtt/message.h"
35#include "mqtt/platform.h"
36#include "mqtt/topic.h"
37#include "mqtt/types.h"
38
39namespace mqtt {
40
42
47{
48public:
50 using ptr_t = std::shared_ptr<ssl_options>;
52 using const_ptr_t = std::shared_ptr<const ssl_options>;
54 using unique_ptr_t = std::unique_ptr<ssl_options>;
55
57 using error_handler = std::function<void(const string& errMsg)>;
63 using psk_handler = std::function<unsigned(
64 const string& hint, char* identity, size_t max_identity_len, unsigned char* psk,
65 size_t max_psk_len
66 )>;
67
68private:
70 static constexpr MQTTAsync_SSLOptions DFLT_C_STRUCT MQTTAsync_SSLOptions_initializer;
71
73 MQTTAsync_SSLOptions opts_{DFLT_C_STRUCT};
74
79 string trustStore_;
80
82 string keyStore_;
83
85 string privateKey_;
86
88 string privateKeyPassword_;
89
91 string caPath_;
92
97 string enabledCipherSuites_;
98
100 error_handler errHandler_;
101
103 psk_handler pskHandler_;
104
106 std::basic_string<unsigned char> protos_;
107
109 static int on_error(const char* str, size_t len, void* context);
110 static unsigned on_psk(
111 const char* hint, char* identity, unsigned int max_identity_len, unsigned char* psk,
112 unsigned int max_psk_len, void* context
113 );
114
116 friend class connect_options;
117
128 const char* c_str(const string& str) { return str.empty() ? nullptr : str.c_str(); }
132 void update_c_struct();
133
134public:
155 const string& trustStore, const string& keyStore, const string& privateKey,
156 const string& privateKeyPassword, const string& enabledCipherSuites,
157 bool enableServerCertAuth,
158 const std::vector<string> alpnProtos = std::vector<string>()
159 );
179 const string& trustStore, const string& keyStore, const string& privateKey,
180 const string& privateKeyPassword, const string& caPath,
181 const string& enabledCipherSuites, bool enableServerCertAuth,
182 const std::vector<string> alpnProtos = std::vector<string>()
183 );
209#if defined(UNIT_TESTS)
210 const MQTTAsync_SSLOptions& c_struct() const { return opts_; }
211#endif
217 string get_trust_store() const { return trustStore_; }
222 string get_key_store() const { return keyStore_; }
227 string get_private_key() const { return privateKey_; }
232 string get_private_key_password() const { return privateKeyPassword_; }
238 string get_enabled_cipher_suites() const { return enabledCipherSuites_; }
243 bool get_enable_server_cert_auth() const { return to_bool(opts_.enableServerCertAuth); }
250 void set_trust_store(const string& trustStore);
257 void set_key_store(const string& keyStore);
264 void set_private_key(const string& privateKey);
270 void set_private_key_password(const string& privateKeyPassword);
288 void set_enabled_cipher_suites(const string& enabledCipherSuites);
294 void set_enable_server_cert_auth(bool enableServerCertAuth);
299 int get_ssl_version() const { return opts_.sslVersion; }
309 void set_ssl_version(int ver) { opts_.sslVersion = ver; }
315 bool get_verify() const { return to_bool(opts_.verify); }
321 void set_verify(bool v) { opts_.verify = to_int(v); }
329 string get_ca_path() const { return caPath_; }
330 string ca_path() const { return caPath_; }
338 void set_ca_path(const string& path);
339 void ca_path(const string& path) { set_ca_path(path); }
355 std::vector<string> get_alpn_protos() const;
362 void set_alpn_protos(const std::vector<string>& protos);
363};
364
373
375
380{
382 ssl_options opts_;
383
384public:
397 auto trust_store(const string& store) -> self& {
398 opts_.set_trust_store(store);
399 return *this;
400 }
407 auto key_store(const string& store) -> self& {
408 opts_.set_key_store(store);
409 return *this;
410 }
416 auto private_key(const string& key) -> self& {
417 opts_.set_private_key(key);
418 return *this;
419 }
424 auto private_keypassword(const string& passwd) -> self& {
425 opts_.set_private_key_password(passwd);
426 return *this;
427 }
442 auto enabled_cipher_suites(const string& suites) -> self& {
443 opts_.set_enabled_cipher_suites(suites);
444 return *this;
445 }
450 auto enable_server_cert_auth(bool on) -> self& {
452 return *this;
453 }
463 auto ssl_version(int ver) -> self& {
464 opts_.set_ssl_version(ver);
465 return *this;
466 }
472 auto verify(bool on = true) -> self& {
473 opts_.set_verify(on);
474 return *this;
475 }
481 auto ca_path(const string& path) -> self& {
482 opts_.ca_path(path);
483 return *this;
484 }
490 opts_.set_error_handler(cb);
491 return *this;
492 }
499 opts_.set_psk_handler(cb);
500 return *this;
501 }
506 auto alpn_protos(const std::vector<string>& protos) -> self& {
507 opts_.set_alpn_protos(protos);
508 return *this;
509 }
514 ssl_options finalize() { return opts_; }
515};
516
518} // namespace mqtt
519
520#endif // __mqtt_ssl_options_h
Definition connect_options.h:50
Definition ssl_options.h:380
auto verify(bool on=true) -> self &
Definition ssl_options.h:472
auto ca_path(const string &path) -> self &
Definition ssl_options.h:481
ssl_options_builder()
Definition ssl_options.h:390
auto error_handler(ssl_options::error_handler cb) -> self &
Definition ssl_options.h:489
auto psk_handler(ssl_options::psk_handler cb) -> self &
Definition ssl_options.h:498
auto private_key(const string &key) -> self &
Definition ssl_options.h:416
auto trust_store(const string &store) -> self &
Definition ssl_options.h:397
ssl_options finalize()
Definition ssl_options.h:514
auto private_keypassword(const string &passwd) -> self &
Definition ssl_options.h:424
auto enabled_cipher_suites(const string &suites) -> self &
Definition ssl_options.h:442
auto ssl_version(int ver) -> self &
Definition ssl_options.h:463
auto alpn_protos(const std::vector< string > &protos) -> self &
Definition ssl_options.h:506
auto key_store(const string &store) -> self &
Definition ssl_options.h:407
auto enable_server_cert_auth(bool on) -> self &
Definition ssl_options.h:450
Definition ssl_options.h:47
void set_ssl_version(int ver)
Definition ssl_options.h:309
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 >())
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
string get_ca_path() const
Definition ssl_options.h:329
void set_key_store(const string &keyStore)
std::shared_ptr< const ssl_options > const_ptr_t
Definition ssl_options.h:52
void set_private_key_password(const string &privateKeyPassword)
string ca_path() const
Definition ssl_options.h:330
string get_private_key() const
Definition ssl_options.h:227
void set_alpn_protos(const std::vector< string > &protos)
int get_ssl_version() const
Definition ssl_options.h:299
void set_enable_server_cert_auth(bool enableServerCertAuth)
ssl_options & operator=(ssl_options &&opt)
ssl_options(const ssl_options &opt)
std::unique_ptr< ssl_options > unique_ptr_t
Definition ssl_options.h:54
string get_key_store() const
Definition ssl_options.h:222
void set_ca_path(const string &path)
string get_trust_store() const
Definition ssl_options.h:217
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:50
void set_verify(bool v)
Definition ssl_options.h:321
bool get_verify() const
Definition ssl_options.h:315
void set_trust_store(const string &trustStore)
std::vector< string > get_alpn_protos() const
string get_private_key_password() const
Definition ssl_options.h:232
string get_enabled_cipher_suites() const
Definition ssl_options.h:238
std::function< void(const string &errMsg)> error_handler
Definition ssl_options.h:57
ssl_options(ssl_options &&opt)
void ca_path(const string &path)
Definition ssl_options.h:339
ssl_options()
Definition ssl_options.h:138
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:243
void set_private_key(const string &privateKey)
Definition async_client.h:60
bool to_bool(int n)
Definition types.h:107
ssl_options::unique_ptr_t ssl_options_unique_ptr
Definition ssl_options.h:372
ssl_options::ptr_t ssl_options_ptr
Definition ssl_options.h:368
int to_int(bool b)
Definition types.h:113