ssl_options.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file ssl_options.h
  3. /// Declaration of MQTT ssl_options class
  4. /// @date Jul 7, 2016
  5. /// @author Guilherme Ferreira
  6. /////////////////////////////////////////////////////////////////////////////
  7. /*******************************************************************************
  8. * Copyright (c) 2016 Guilherme Ferreira <guilherme.maciel.ferreira@gmail.com>
  9. * Copyright (c) 2016-2019 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 v1.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-v10.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 <vector>
  32. namespace mqtt {
  33. /////////////////////////////////////////////////////////////////////////////
  34. /**
  35. * Holds the set of SSL options for connection.
  36. */
  37. class ssl_options
  38. {
  39. /** The default C struct */
  40. static const MQTTAsync_SSLOptions DFLT_C_STRUCT ;
  41. /** The underlying C SSL options */
  42. MQTTAsync_SSLOptions opts_;
  43. /**
  44. * The file containing the public digital certificates trusted by
  45. * the client.
  46. */
  47. string trustStore_;
  48. /** The file containing the public certificate chain of the client. */
  49. string keyStore_;
  50. /** The file containing the client's private key. */
  51. string privateKey_;
  52. /** The password to load the client's privateKey if encrypted. */
  53. string privateKeyPassword_;
  54. /** Path to a directory containing CA certificates in PEM format */
  55. string caPath_;
  56. /**
  57. * The list of cipher suites that the client will present to the
  58. * server during the SSL handshake.
  59. */
  60. string enabledCipherSuites_;
  61. /** The connect options has special access */
  62. friend class connect_options;
  63. friend class connect_options_test;
  64. friend class ssl_options_test;
  65. /**
  66. * Gets a pointer to the C-language NUL-terminated strings for the
  67. * struct.
  68. * @note In the SSL options, by default, the Paho C treats nullptr char
  69. * arrays as unset values, so we keep that semantic and only set those
  70. * char arrays if the string is non-empty.
  71. * @param str The C++ string object.
  72. * @return Pointer to a NUL terminated string. This is only valid until
  73. * the next time the string is updated.
  74. */
  75. const char* c_str(const string& str) {
  76. return str.empty() ? nullptr : str.c_str();
  77. }
  78. /**
  79. * Updates the underlying C structure to match our strings.
  80. */
  81. void update_c_struct();
  82. public:
  83. /** Smart/shared pointer to an object of this class. */
  84. using ptr_t = std::shared_ptr<ssl_options>;
  85. /** Smart/shared pointer to a const object of this class. */
  86. using const_ptr_t = std::shared_ptr<const ssl_options>;
  87. /**
  88. * Constructs a new MqttConnectOptions object using the default values.
  89. */
  90. ssl_options();
  91. /**
  92. * Argument constructor.
  93. * @param trustStore The file containing the public digital certificates
  94. * trusted by the client.
  95. * @param keyStore The file containing the public certificate chain of the
  96. * client.
  97. * @param privateKey The file containing the client's private key.
  98. * @param privateKeyPassword The password to load the client's privateKey
  99. * if encrypted.
  100. * @param enabledCipherSuites The list of cipher suites that the client
  101. * will present to the server during the SSL handshake.
  102. * @param enableServerCertAuth True/False option to enable verification of
  103. * the server certificate
  104. */
  105. ssl_options(const string& trustStore, const string& keyStore,
  106. const string& privateKey, const string& privateKeyPassword,
  107. const string& enabledCipherSuites, bool enableServerCertAuth);
  108. /**
  109. * Copy constructor.
  110. * @param opt The other options to copy.
  111. */
  112. ssl_options(const ssl_options& opt);
  113. /**
  114. * Move constructor.
  115. * @param opt The other options to move to this one.
  116. */
  117. ssl_options(ssl_options&& opt);
  118. /**
  119. * Copy assignment.
  120. * @param opt The other options to copy.
  121. * @return A reference to this object.
  122. */
  123. ssl_options& operator=(const ssl_options& opt);
  124. /**
  125. * Move assignment.
  126. * @param opt The other options to move to this one.
  127. * @return A reference to this object.
  128. */
  129. ssl_options& operator=(ssl_options&& opt);
  130. /**
  131. * Returns the file containing the public digital certificates trusted by
  132. * the client.
  133. * @return string
  134. */
  135. string get_trust_store() const { return trustStore_; }
  136. /**
  137. * Returns the file containing the public certificate chain of the client.
  138. * @return string
  139. */
  140. string get_key_store() const { return keyStore_; }
  141. /**
  142. * Returns the file containing the client's private key.
  143. * @return string
  144. */
  145. string get_private_key() const { return privateKey_; }
  146. /**
  147. * Returns the password to load the client's privateKey if encrypted.
  148. * @return string
  149. */
  150. string get_private_key_password() const { return privateKeyPassword_; }
  151. /**
  152. * Returns the list of cipher suites that the client will present to the
  153. * server during the SSL handshake.
  154. * @return string
  155. */
  156. string get_enabled_cipher_suites() const { return enabledCipherSuites_; }
  157. /**
  158. * Returns the true/false to enable verification of the server certificate .
  159. * @return bool
  160. */
  161. bool get_enable_server_cert_auth() const {
  162. return to_bool(opts_.enableServerCertAuth);
  163. }
  164. /**
  165. * Sets the file containing the public digital certificates trusted by
  166. * the client.
  167. * @param trustStore The file in PEM format containing the public
  168. * digital certificates trusted by the client.
  169. */
  170. void set_trust_store(const string& trustStore);
  171. /**
  172. * Sets the file containing the public certificate chain of the client.
  173. * @param keyStore The file in PEM format containing the public
  174. * certificate chain of the client. It may also include
  175. * the client's private key.
  176. */
  177. void set_key_store(const string& keyStore);
  178. /**
  179. * Sets the file containing the client's private key.
  180. * @param privateKey If not included in the sslKeyStore, this is the
  181. * file in PEM format containing the client's private
  182. * key.
  183. */
  184. void set_private_key(const string& privateKey);
  185. /**
  186. * Sets the password to load the client's privateKey if encrypted.
  187. * @param privateKeyPassword The password to load the privateKey if
  188. * encrypted.
  189. */
  190. void set_private_key_password(const string& privateKeyPassword);
  191. /**
  192. * Sets the list of cipher suites that the client will present to the server
  193. * during the SSL handshake.
  194. * @param enabledCipherSuites The list of cipher suites that the client
  195. * will present to the server during the SSL
  196. * handshake. For a full explanation of the
  197. * cipher list format, please see the OpenSSL
  198. * on-line documentation:
  199. * http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT
  200. * If this setting is ommitted, its default
  201. * value will be "ALL", that is, all the
  202. * cipher suites -excluding those offering no
  203. * encryption- will be considered. This
  204. * setting can be used to set an SSL
  205. * anonymous connection (empty string value,
  206. * for instance).
  207. */
  208. void set_enabled_cipher_suites(const string& enabledCipherSuites);
  209. /**
  210. * Enables or disables verification of the server certificate.
  211. * @param enablServerCertAuth enable/disable verification of the server
  212. * certificate
  213. */
  214. void set_enable_server_cert_auth(bool enablServerCertAuth);
  215. /**
  216. * Gets the requested SSL/TLS version.
  217. * @return The requested SSL/TLS version.
  218. */
  219. int get_ssl_version() const { return opts_.sslVersion; }
  220. /**
  221. * Set the SSL/TLS version to use.
  222. *
  223. * @param ver The desired SSL/TLS version. Specify one of:
  224. * @li MQTT_SSL_VERSION_DEFAULT (0)
  225. * @li MQTT_SSL_VERSION_TLS_1_0 (1)
  226. * @li MQTT_SSL_VERSION_TLS_1_1 (2)
  227. * @li MQTT_SSL_VERSION_TLS_1_2 (3)
  228. */
  229. void set_ssl_version(int ver) { opts_.sslVersion = ver; }
  230. /**
  231. * Determines whether it will carry out post-connect checks, including
  232. * that a certificate matches the given host name.
  233. * @return Whether it will carry out post-connect checks.
  234. */
  235. bool get_verify() const { return to_bool(opts_.verify); }
  236. /**
  237. * Sets whether it should carry out post-connect checks, including that
  238. * a certificate matches the given host name.
  239. * @param v Whether it should carry out post-connect checks.
  240. */
  241. void set_verify(bool v) { opts_.verify = to_int(v); }
  242. /**
  243. * Gets the path to a directory containing CA certificates in PEM
  244. * format.
  245. *
  246. * @return Path to a directory containing CA certificates in PEM format,
  247. * if set. If this isn't set, returns an empty string.
  248. */
  249. string ca_path() const { return caPath_; }
  250. /**
  251. * Sets the path to a directory containing CA certificates in PEM
  252. * format.
  253. *
  254. * @param path Path to a directory containing CA certificates in PEM
  255. * format.
  256. */
  257. void ca_path(const string& path);
  258. };
  259. /**
  260. * Shared pointer to the ssl options class.
  261. */
  262. using ssl_options_ptr = ssl_options::ptr_t;
  263. /////////////////////////////////////////////////////////////////////////////
  264. // end namespace mqtt
  265. }
  266. #endif // __mqtt_ssl_options_h