ssl_options.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file ssl_options.h
  3. /// Declaration of MQTT ssl_options class
  4. /// @date Jul 7, 2016
  5. /// @author Frank Pagliughi, Guilherme Ferreira
  6. /////////////////////////////////////////////////////////////////////////////
  7. /*******************************************************************************
  8. * Copyright (c) 2016 Guilherme Ferreira <guilherme.maciel.ferreira@gmail.com>
  9. * Copyright (c) 2016-2021 Frank Pagliughi <fpagliughi@mindspring.com>
  10. *
  11. * All rights reserved. This program and the accompanying materials
  12. * are made available under the terms of the Eclipse Public License v2.0
  13. * and Eclipse Distribution License v1.0 which accompany this distribution.
  14. *
  15. * The Eclipse Public License is available at
  16. * http://www.eclipse.org/legal/epl-v20.html
  17. * and the Eclipse Distribution License is available at
  18. * http://www.eclipse.org/org/documents/edl-v10.php.
  19. *
  20. * Contributors:
  21. * Guilherme Ferreira - initial implementation and documentation
  22. * Frank Pagliughi - added copy & move operations
  23. * Frank Pagliughi - upgraded compatibility to Paho C 1.3
  24. *******************************************************************************/
  25. #ifndef __mqtt_ssl_options_h
  26. #define __mqtt_ssl_options_h
  27. #include "MQTTAsync.h"
  28. #include "mqtt/message.h"
  29. #include "mqtt/topic.h"
  30. #include "mqtt/types.h"
  31. #include "mqtt/platform.h"
  32. #include <vector>
  33. #include <functional>
  34. namespace mqtt {
  35. /////////////////////////////////////////////////////////////////////////////
  36. /**
  37. * Holds the set of SSL options for connection.
  38. */
  39. class ssl_options
  40. {
  41. public:
  42. /** Smart/shared pointer to an object of this class. */
  43. using ptr_t = std::shared_ptr<ssl_options>;
  44. /** Smart/shared pointer to a const object of this class. */
  45. using const_ptr_t = std::shared_ptr<const ssl_options>;
  46. /** Unique pointer to an object of this class. */
  47. using unique_ptr_t = std::unique_ptr<ssl_options>;
  48. /** Handler type for error message callbacks */
  49. using error_handler = std::function<void(const string& errMsg)>;
  50. /**
  51. * Handler type for TLS-PSK option callback.
  52. * On success, the callback should return the length of the PSK (in
  53. * bytes). On failure, it should throw or return zero.
  54. */
  55. using psk_handler = std::function<unsigned(const string& hint,
  56. char *identity, size_t max_identity_len,
  57. unsigned char *psk, size_t max_psk_len)>;
  58. private:
  59. /** The default C struct */
  60. PAHO_MQTTPP_EXPORT static const MQTTAsync_SSLOptions DFLT_C_STRUCT ;
  61. /** The underlying C SSL options */
  62. MQTTAsync_SSLOptions opts_;
  63. /**
  64. * The file containing the public digital certificates trusted by
  65. * the client.
  66. */
  67. string trustStore_;
  68. /** The file containing the public certificate chain of the client. */
  69. string keyStore_;
  70. /** The file containing the client's private key. */
  71. string privateKey_;
  72. /** The password to load the client's privateKey if encrypted. */
  73. string privateKeyPassword_;
  74. /** Path to a directory containing CA certificates in PEM format */
  75. string caPath_;
  76. /**
  77. * The list of cipher suites that the client will present to the
  78. * server during the SSL handshake.
  79. */
  80. string enabledCipherSuites_;
  81. /** Error message callback handler */
  82. error_handler errHandler_;
  83. /** PSK callback handler */
  84. psk_handler pskHandler_;
  85. /** ALPN protocol list, in wire format */
  86. std::basic_string<unsigned char> protos_;
  87. /** Callbacks from the C library */
  88. static int on_error(const char *str, size_t len, void *context);
  89. static unsigned on_psk(const char *hint, char *identity, unsigned int max_identity_len,
  90. unsigned char *psk, unsigned int max_psk_len, void *context);
  91. /** The connect options has special access */
  92. friend class connect_options;
  93. /**
  94. * Gets a pointer to the C-language NUL-terminated strings for the
  95. * struct.
  96. * @note In the SSL options, by default, the Paho C treats nullptr char
  97. * arrays as unset values, so we keep that semantic and only set those
  98. * char arrays if the string is non-empty.
  99. * @param str The C++ string object.
  100. * @return Pointer to a NUL terminated string. This is only valid until
  101. * the next time the string is updated.
  102. */
  103. const char* c_str(const string& str) {
  104. return str.empty() ? nullptr : str.c_str();
  105. }
  106. /**
  107. * Updates the underlying C structure to match our strings.
  108. */
  109. void update_c_struct();
  110. public:
  111. /**
  112. * Constructs a new MqttConnectOptions object using the default values.
  113. */
  114. ssl_options();
  115. /**
  116. * Argument constructor.
  117. * @param trustStore The file containing the public digital certificates
  118. * trusted by the client.
  119. * @param keyStore The file containing the public certificate chain of the
  120. * client.
  121. * @param privateKey The file containing the client's private key.
  122. * @param privateKeyPassword The password to load the client's privateKey
  123. * if encrypted.
  124. * @param enabledCipherSuites The list of cipher suites that the client
  125. * will present to the server during the SSL handshake.
  126. * @param enableServerCertAuth True/False option to enable verification of
  127. * the server certificate
  128. * @param alpnProtos The ALPN protocols to try.
  129. */
  130. ssl_options(const string& trustStore, const string& keyStore,
  131. const string& privateKey, const string& privateKeyPassword,
  132. const string& enabledCipherSuites, bool enableServerCertAuth,
  133. const std::vector<string> alpnProtos=std::vector<string>());
  134. /**
  135. * Argument constructor.
  136. * @param trustStore The file containing the public digital certificates
  137. * trusted by the client.
  138. * @param keyStore The file containing the public certificate chain of
  139. * the client.
  140. * @param privateKey The file containing the client's private key.
  141. * @param privateKeyPassword The password to load the client's
  142. * privateKey if encrypted.
  143. * @param caPath The name of a directory containing CA certificates in
  144. * PEM format.
  145. * @param enabledCipherSuites The list of cipher suites that the client
  146. * will present to the server during the SSL
  147. * handshake.
  148. * @param enableServerCertAuth True/False option to enable verification
  149. * of the server certificate
  150. * @param alpnProtos The ALPN protocols to try.
  151. */
  152. ssl_options(const string& trustStore, const string& keyStore,
  153. const string& privateKey, const string& privateKeyPassword,
  154. const string& caPath,
  155. const string& enabledCipherSuites, bool enableServerCertAuth,
  156. const std::vector<string> alpnProtos=std::vector<string>());
  157. /**
  158. * Copy constructor.
  159. * @param opt The other options to copy.
  160. */
  161. ssl_options(const ssl_options& opt);
  162. /**
  163. * Move constructor.
  164. * @param opt The other options to move to this one.
  165. */
  166. ssl_options(ssl_options&& opt);
  167. /**
  168. * Copy assignment.
  169. * @param opt The other options to copy.
  170. * @return A reference to this object.
  171. */
  172. ssl_options& operator=(const ssl_options& opt);
  173. /**
  174. * Move assignment.
  175. * @param opt The other options to move to this one.
  176. * @return A reference to this object.
  177. */
  178. ssl_options& operator=(ssl_options&& opt);
  179. /**
  180. * Expose the underlying C struct for the unit tests.
  181. */
  182. #if defined(UNIT_TESTS)
  183. const MQTTAsync_SSLOptions& c_struct() const { return opts_; }
  184. #endif
  185. /**
  186. * Returns the file containing the public digital certificates trusted by
  187. * the client.
  188. * @return string
  189. */
  190. string get_trust_store() const { return trustStore_; }
  191. /**
  192. * Returns the file containing the public certificate chain of the client.
  193. * @return string
  194. */
  195. string get_key_store() const { return keyStore_; }
  196. /**
  197. * Gets the name of file containing the client's private key.
  198. * @return The name of file containing the client's private key.
  199. */
  200. string get_private_key() const { return privateKey_; }
  201. /**
  202. * Gets the password to load the client's privateKey if encrypted.
  203. * @return The password to load the client's privateKey if encrypted.
  204. */
  205. string get_private_key_password() const { return privateKeyPassword_; }
  206. /**
  207. * Returns the list of cipher suites that the client will present to the
  208. * server during the SSL handshake.
  209. * @return string
  210. */
  211. string get_enabled_cipher_suites() const { return enabledCipherSuites_; }
  212. /**
  213. * Returns the true/false to enable verification of the server certificate .
  214. * @return bool
  215. */
  216. bool get_enable_server_cert_auth() const {
  217. return to_bool(opts_.enableServerCertAuth);
  218. }
  219. /**
  220. * Sets the file containing the public digital certificates trusted by
  221. * the client.
  222. * @param trustStore The file in PEM format containing the public
  223. * digital certificates trusted by the client.
  224. */
  225. void set_trust_store(const string& trustStore);
  226. /**
  227. * Sets the file containing the public certificate chain of the client.
  228. * @param keyStore The file in PEM format containing the public
  229. * certificate chain of the client. It may also include
  230. * the client's private key.
  231. */
  232. void set_key_store(const string& keyStore);
  233. /**
  234. * Sets the file containing the client's private key.
  235. * @param privateKey If not included in the sslKeyStore, this is the
  236. * file in PEM format containing the client's private
  237. * key.
  238. */
  239. void set_private_key(const string& privateKey);
  240. /**
  241. * Sets the password to load the client's privateKey if encrypted.
  242. * @param privateKeyPassword The password to load the privateKey if
  243. * encrypted.
  244. */
  245. void set_private_key_password(const string& privateKeyPassword);
  246. /**
  247. * Sets the list of cipher suites that the client will present to the server
  248. * during the SSL handshake.
  249. * @param enabledCipherSuites The list of cipher suites that the client
  250. * will present to the server during the SSL
  251. * handshake. For a full explanation of the
  252. * cipher list format, please see the OpenSSL
  253. * on-line documentation:
  254. * https://www.openssl.org/docs/manmaster/man1/openssl-ciphers.html
  255. * If this setting is omitted, its default
  256. * value will be "ALL", that is, all the
  257. * cipher suites -excluding those offering no
  258. * encryption- will be considered. This
  259. * setting can be used to set an SSL
  260. * anonymous connection (empty string value,
  261. * for instance).
  262. */
  263. void set_enabled_cipher_suites(const string& enabledCipherSuites);
  264. /**
  265. * Enables or disables verification of the server certificate.
  266. * @param enableServerCertAuth enable/disable verification of the server
  267. * certificate
  268. */
  269. void set_enable_server_cert_auth(bool enableServerCertAuth);
  270. /**
  271. * Gets the requested SSL/TLS version.
  272. * @return The requested SSL/TLS version.
  273. */
  274. int get_ssl_version() const { return opts_.sslVersion; }
  275. /**
  276. * Set the SSL/TLS version to use.
  277. *
  278. * @param ver The desired SSL/TLS version. Specify one of:
  279. * @li MQTT_SSL_VERSION_DEFAULT (0)
  280. * @li MQTT_SSL_VERSION_TLS_1_0 (1)
  281. * @li MQTT_SSL_VERSION_TLS_1_1 (2)
  282. * @li MQTT_SSL_VERSION_TLS_1_2 (3)
  283. */
  284. void set_ssl_version(int ver) { opts_.sslVersion = ver; }
  285. /**
  286. * Determines whether it will carry out post-connect checks, including
  287. * that a certificate matches the given host name.
  288. * @return Whether it will carry out post-connect checks.
  289. */
  290. bool get_verify() const { return to_bool(opts_.verify); }
  291. /**
  292. * Sets whether it should carry out post-connect checks, including that
  293. * a certificate matches the given host name.
  294. * @param v Whether it should carry out post-connect checks.
  295. */
  296. void set_verify(bool v) { opts_.verify = to_int(v); }
  297. /**
  298. * Gets the path to a directory containing CA certificates in PEM
  299. * format.
  300. *
  301. * @return Path to a directory containing CA certificates in PEM format,
  302. * if set. If this isn't set, returns an empty string.
  303. */
  304. string get_ca_path() const { return caPath_; }
  305. string ca_path() const { return caPath_; }
  306. /**
  307. * Sets the path to a directory containing CA certificates in PEM
  308. * format.
  309. *
  310. * @param path Path to a directory containing CA certificates in PEM
  311. * format.
  312. */
  313. void set_ca_path(const string& path);
  314. void ca_path(const string& path) { set_ca_path(path); }
  315. /**
  316. * Registers the error message callback handler.
  317. * @param cb The callback to receive error messages.
  318. */
  319. void set_error_handler(error_handler cb);
  320. /**
  321. * Registers a callback handler to set the TLS-PSK options.
  322. * See: OpenSSL SSL_CTX_set_psk_client_callback()
  323. * @param cb The callback.
  324. */
  325. void set_psk_handler(psk_handler cb);
  326. /**
  327. * Gets the list of supported ALPN protocols.
  328. * @return A vector containing the supported ALPN protocols.
  329. */
  330. std::vector<string> get_alpn_protos() const;
  331. /**
  332. * Sets the list of supported ALPN protocols.
  333. * See:
  334. * https://www.openssl.org/docs/man1.1.0/man3/SSL_CTX_set_alpn_protos.html
  335. * @param protos The list of ALPN protocols to be negotiated.
  336. */
  337. void set_alpn_protos(const std::vector<string>& protos);
  338. };
  339. /**
  340. * Shared pointer to the ssl options class.
  341. */
  342. using ssl_options_ptr = ssl_options::ptr_t;
  343. /**
  344. * Unique pointer to the ssl options class.
  345. */
  346. using ssl_options_unique_ptr = ssl_options::unique_ptr_t;
  347. /////////////////////////////////////////////////////////////////////////////
  348. /**
  349. * Class to build the SSL options for connections.
  350. */
  351. class ssl_options_builder
  352. {
  353. /** The underlying options */
  354. ssl_options opts_;
  355. public:
  356. /** This class */
  357. using self = ssl_options_builder;
  358. /**
  359. * Default constructor.
  360. */
  361. ssl_options_builder() {}
  362. /**
  363. * Sets the file containing the public digital certificates trusted by
  364. * the client.
  365. * @param store The file in PEM format containing the public digital
  366. * certificates trusted by the client.
  367. */
  368. auto trust_store(const string& store) -> self& {
  369. opts_.set_trust_store(store);
  370. return *this;
  371. }
  372. /**
  373. * Sets the file containing the public certificate chain of the client.
  374. * @param store The file in PEM format containing the public certificate
  375. * chain of the client. It may also include the client's
  376. * private key.
  377. */
  378. auto key_store(const string& store) -> self& {
  379. opts_.set_key_store(store);
  380. return *this;
  381. }
  382. /**
  383. * Sets the file containing the client's private key.
  384. * @param key If not included in the sslKeyStore, this is the file in
  385. * PEM format containing the client's private key.
  386. */
  387. auto private_key(const string& key) -> self& {
  388. opts_.set_private_key(key);
  389. return *this;
  390. }
  391. /**
  392. * Sets the password to load the client's privateKey if encrypted.
  393. * @param passwd The password to load the privateKey if encrypted.
  394. */
  395. auto private_keypassword(const string& passwd) -> self& {
  396. opts_.set_private_key_password(passwd);
  397. return *this;
  398. }
  399. /**
  400. * Sets the list of cipher suites that the client will present to the server
  401. * during the SSL handshake.
  402. * @param suites The list of cipher suites that the client will present to
  403. * the server during the SSL handshake. For a full
  404. * explanation of the cipher list format, please see the
  405. * OpenSSL on-line documentation:
  406. * http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT
  407. * If this setting is omitted, its default value will be
  408. * "ALL", that is, all the cipher suites -excluding those
  409. * offering no encryption- will be considered. This setting
  410. * can be used to set an SSL anonymous connection (empty
  411. * string value, for instance).
  412. */
  413. auto enabled_cipher_suites(const string& suites) -> self& {
  414. opts_.set_enabled_cipher_suites(suites);
  415. return *this;
  416. }
  417. /**
  418. * Enables or disables verification of the server certificate.
  419. * @param on enable/disable verification of the server certificate
  420. */
  421. auto enable_server_cert_auth(bool on) -> self& {
  422. opts_.set_enable_server_cert_auth(on);
  423. return *this;
  424. }
  425. /**
  426. * Set the SSL/TLS version to use.
  427. *
  428. * @param ver The desired SSL/TLS version. Specify one of:
  429. * @li MQTT_SSL_VERSION_DEFAULT (0)
  430. * @li MQTT_SSL_VERSION_TLS_1_0 (1)
  431. * @li MQTT_SSL_VERSION_TLS_1_1 (2)
  432. * @li MQTT_SSL_VERSION_TLS_1_2 (3)
  433. */
  434. auto ssl_version(int ver) -> self& {
  435. opts_.set_ssl_version(ver);
  436. return *this;
  437. }
  438. /**
  439. * Sets whether it should carry out post-connect checks, including that
  440. * a certificate matches the given host name.
  441. * @param on Whether it should carry out post-connect checks.
  442. */
  443. auto verify(bool on=true) -> self& {
  444. opts_.set_verify(on);
  445. return *this;
  446. }
  447. /**
  448. * Sets the path to a directory containing CA certificates in PEM format.
  449. * @param path Path to a directory containing CA certificates in PEM
  450. * format.
  451. */
  452. auto ca_path(const string& path) -> self& {
  453. opts_.ca_path(path);
  454. return *this;
  455. }
  456. /**
  457. * Registers an error callback handler.
  458. * @param cb The callback to receive error messages.
  459. */
  460. auto error_handler(ssl_options::error_handler cb) -> self& {
  461. opts_.set_error_handler(cb);
  462. return *this;
  463. }
  464. /**
  465. * Registers a callback handler to set the TLS-PSK options.
  466. * See: OpenSSL SSL_CTX_set_psk_client_callback()
  467. * @param cb The callback.
  468. */
  469. auto psk_handler(ssl_options::psk_handler cb) -> self& {
  470. opts_.set_psk_handler(cb);
  471. return *this;
  472. }
  473. /**
  474. * Sets the list of supported ALPN protocols.
  475. * @param protos The list of ALPN protocols to be negotiated.
  476. */
  477. auto alpn_protos(const std::vector<string>& protos) -> self& {
  478. opts_.set_alpn_protos(protos);
  479. return *this;
  480. }
  481. /**
  482. * Finish building the options and return them.
  483. * @return The option struct as built.
  484. */
  485. ssl_options finalize() { return opts_; }
  486. };
  487. /////////////////////////////////////////////////////////////////////////////
  488. // end namespace mqtt
  489. }
  490. #endif // __mqtt_ssl_options_h