ssl_options.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*******************************************************************************
  2. * Copyright (c) 2016 Guilherme Ferreira <guilherme.maciel.ferreira@gmail.com>
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Guilherme Ferreira - initial implementation and documentation
  15. * Frank Pagliughi - added copy & move operations
  16. *******************************************************************************/
  17. #include "mqtt/ssl_options.h"
  18. #include <utility>
  19. #include <cstring>
  20. namespace mqtt {
  21. /////////////////////////////////////////////////////////////////////////////
  22. const MQTTAsync_SSLOptions ssl_options::DFLT_C_STRUCT = MQTTAsync_SSLOptions_initializer;
  23. ssl_options::ssl_options() : opts_(DFLT_C_STRUCT)
  24. {
  25. }
  26. ssl_options::ssl_options(const string& trustStore, const string& keyStore,
  27. const string& privateKey, const string& privateKeyPassword,
  28. const string& enabledCipherSuites, bool enableServerCertAuth)
  29. : opts_(DFLT_C_STRUCT), trustStore_(trustStore), keyStore_(keyStore),
  30. privateKey_(privateKey), privateKeyPassword_(privateKeyPassword),
  31. enabledCipherSuites_(enabledCipherSuites)
  32. {
  33. update_c_struct();
  34. opts_.enableServerCertAuth = enableServerCertAuth;
  35. }
  36. ssl_options::ssl_options(const ssl_options& opt)
  37. : opts_(opt.opts_), trustStore_(opt.trustStore_), keyStore_(opt.keyStore_),
  38. privateKey_(opt.privateKey_), privateKeyPassword_(opt.privateKeyPassword_),
  39. enabledCipherSuites_(opt.enabledCipherSuites_)
  40. {
  41. update_c_struct();
  42. }
  43. ssl_options::ssl_options(ssl_options&& opt)
  44. : opts_(opt.opts_), trustStore_(std::move(opt.trustStore_)),
  45. keyStore_(std::move(opt.keyStore_)), privateKey_(std::move(opt.privateKey_)),
  46. privateKeyPassword_(std::move(opt.privateKeyPassword_)),
  47. enabledCipherSuites_(std::move(opt.enabledCipherSuites_))
  48. {
  49. update_c_struct();
  50. }
  51. void ssl_options::update_c_struct()
  52. {
  53. opts_.trustStore = c_str(trustStore_);
  54. opts_.keyStore = c_str(keyStore_);
  55. opts_.privateKey = c_str(privateKey_);
  56. opts_.privateKeyPassword = c_str(privateKeyPassword_);
  57. opts_.enabledCipherSuites = c_str(enabledCipherSuites_);
  58. }
  59. ssl_options& ssl_options::operator=(const ssl_options& rhs)
  60. {
  61. if (&rhs == this)
  62. return *this;
  63. opts_ = rhs.opts_;
  64. trustStore_ = rhs.trustStore_;
  65. keyStore_ = rhs.keyStore_;
  66. privateKey_ = rhs.privateKey_;
  67. privateKeyPassword_ = rhs.privateKeyPassword_;
  68. enabledCipherSuites_ = rhs.enabledCipherSuites_;
  69. update_c_struct();
  70. return *this;
  71. }
  72. ssl_options& ssl_options::operator=(ssl_options&& rhs)
  73. {
  74. if (&rhs == this)
  75. return *this;
  76. opts_ = rhs.opts_;
  77. trustStore_ = std::move(rhs.trustStore_);
  78. keyStore_ = std::move(rhs.keyStore_);
  79. privateKey_ = std::move(rhs.privateKey_);
  80. privateKeyPassword_ = std::move(rhs.privateKeyPassword_);
  81. enabledCipherSuites_ = std::move(rhs.enabledCipherSuites_);
  82. update_c_struct();
  83. return *this;
  84. }
  85. void ssl_options::set_trust_store(const string& trustStore)
  86. {
  87. trustStore_ = trustStore;
  88. opts_.trustStore = c_str(trustStore_);
  89. }
  90. void ssl_options::set_key_store(const string& keyStore)
  91. {
  92. keyStore_ = keyStore;
  93. opts_.keyStore = c_str(keyStore_);
  94. }
  95. void ssl_options::set_private_key(const string& privateKey)
  96. {
  97. privateKey_ = privateKey;
  98. opts_.privateKey = c_str(privateKey_);
  99. }
  100. void ssl_options::set_private_key_password(const string& privateKeyPassword)
  101. {
  102. privateKeyPassword_ = privateKeyPassword;
  103. opts_.privateKeyPassword = c_str(privateKeyPassword_);
  104. }
  105. void ssl_options::set_enabled_cipher_suites(const string& enabledCipherSuites)
  106. {
  107. enabledCipherSuites_ = enabledCipherSuites;
  108. opts_.enabledCipherSuites = c_str(enabledCipherSuites_);
  109. }
  110. void ssl_options::set_enable_server_cert_auth(bool enableServerCertAuth)
  111. {
  112. opts_.enableServerCertAuth = to_int(enableServerCertAuth);
  113. }
  114. void ssl_options::ca_path(const string& path)
  115. {
  116. caPath_ = path;
  117. opts_.CApath = c_str(caPath_);
  118. }
  119. /////////////////////////////////////////////////////////////////////////////
  120. } // end namespace mqtt