ssl_publish.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // ssl_publish.cpp
  2. //
  3. // This is a Paho MQTT C++ client, sample application.
  4. //
  5. // It's an example of how to connect to an MQTT broker securely, and then
  6. // send messages as an MQTT publisher using the C++ asynchronous client
  7. // interface.
  8. //
  9. // The sample demonstrates:
  10. // - Connecting to an MQTT server/broker securely
  11. // - Setting SSL/TLS options
  12. // - Last will and testament
  13. // - Publishing messages
  14. // - Using asynchronous tokens
  15. // - Implementing callbacks and action listeners
  16. //
  17. // We can test this using mosquitto configured with certificates in the
  18. // Paho C library. The C library has an SSL/TSL test suite, and we can use
  19. // that to test:
  20. // $ cd paho.mqtt.c
  21. // $ mosquitto -c test/tls-testing/mosquitto.conf
  22. //
  23. // Then use the files "test-root-ca.crt" and "client.pem" from the
  24. // test/ssl directory (paho.mqtt.c/test/ssl) for the trust store and
  25. // key_store, respectively, for this program.
  26. //
  27. /*******************************************************************************
  28. * Copyright (c) 2013-2017 Frank Pagliughi <fpagliughi@mindspring.com>
  29. *
  30. * All rights reserved. This program and the accompanying materials
  31. * are made available under the terms of the Eclipse Public License v1.0
  32. * and Eclipse Distribution License v1.0 which accompany this distribution.
  33. *
  34. * The Eclipse Public License is available at
  35. * http://www.eclipse.org/legal/epl-v10.html
  36. * and the Eclipse Distribution License is available at
  37. * http://www.eclipse.org/org/documents/edl-v10.php.
  38. *
  39. * Contributors:
  40. * Frank Pagliughi - initial implementation and documentation
  41. *******************************************************************************/
  42. #include <iostream>
  43. #include <fstream>
  44. #include <cstdlib>
  45. #include <string>
  46. #include <chrono>
  47. #include <cstring>
  48. #include "mqtt/async_client.h"
  49. const std::string DFLT_SERVER_ADDRESS { "ssl://localhost:18885" };
  50. const std::string DFLT_CLIENT_ID { "ssl_publish_cpp" };
  51. const std::string KEY_STORE { "client.pem" };
  52. const std::string TRUST_STORE { "test-root-ca.crt" };
  53. const std::string LWT_TOPIC { "events/disconnect" };
  54. const std::string LWT_PAYLOAD { "Last will and testament." };
  55. const int QOS = 1;
  56. const auto TIMEOUT = std::chrono::seconds(10);
  57. /////////////////////////////////////////////////////////////////////////////
  58. /**
  59. * A callback class for use with the main MQTT client.
  60. */
  61. class callback : public virtual mqtt::callback
  62. {
  63. public:
  64. void connection_lost(const std::string& cause) override {
  65. std::cout << "\nConnection lost" << std::endl;
  66. if (!cause.empty())
  67. std::cout << "\tcause: " << cause << std::endl;
  68. }
  69. void delivery_complete(mqtt::delivery_token_ptr tok) override {
  70. std::cout << "\tDelivery complete for token: "
  71. << (tok ? tok->get_message_id() : -1) << std::endl;
  72. }
  73. };
  74. /////////////////////////////////////////////////////////////////////////////
  75. using namespace std;
  76. int main(int argc, char* argv[])
  77. {
  78. string address = (argc > 1) ? string(argv[1]) : DFLT_SERVER_ADDRESS,
  79. clientID = (argc > 2) ? string(argv[2]) : DFLT_CLIENT_ID;
  80. // Note that we don't actually need to open the trust or key stores.
  81. // We just need a quick, portable way to check that they exist.
  82. {
  83. ifstream tstore(TRUST_STORE);
  84. if (!tstore) {
  85. cerr << "The trust store file does not exist: " << TRUST_STORE << endl;
  86. cerr << " Get a copy from \"paho.mqtt.c/test/ssl/test-root-ca.crt\"" << endl;;
  87. return 1;
  88. }
  89. ifstream kstore(KEY_STORE);
  90. if (!kstore) {
  91. cerr << "The key store file does not exist: " << KEY_STORE << endl;
  92. cerr << " Get a copy from \"paho.mqtt.c/test/ssl/client.pem\"" << endl;
  93. return 1;
  94. }
  95. }
  96. cout << "Initializing for server '" << address << "'..." << endl;
  97. mqtt::async_client client(address, clientID);
  98. callback cb;
  99. client.set_callback(cb);
  100. mqtt::connect_options connopts("testuser", "testpassword");
  101. mqtt::ssl_options sslopts;
  102. sslopts.set_trust_store(TRUST_STORE);
  103. sslopts.set_key_store(KEY_STORE);
  104. mqtt::message willmsg(LWT_TOPIC, LWT_PAYLOAD, QOS, true);
  105. mqtt::will_options will(willmsg);
  106. connopts.set_will(will);
  107. connopts.set_ssl(sslopts);
  108. cout << " ...OK" << endl;
  109. try {
  110. // Connect using SSL/TLS
  111. cout << "\nConnecting..." << endl;
  112. mqtt::token_ptr conntok = client.connect(connopts);
  113. cout << "Waiting for the connection..." << endl;
  114. conntok->wait();
  115. cout << " ...OK" << endl;
  116. // Send a message
  117. cout << "\nSending message..." << endl;
  118. auto msg = mqtt::make_message("hello", "Hello secure C++ world!", QOS, false);
  119. client.publish(msg)->wait_for(TIMEOUT);
  120. cout << " ...OK" << endl;
  121. // Disconnect
  122. cout << "\nDisconnecting..." << endl;
  123. conntok = client.disconnect();
  124. conntok->wait();
  125. cout << " ...OK" << endl;
  126. }
  127. catch (const mqtt::exception& exc) {
  128. cerr << exc.what() << endl;
  129. return 1;
  130. }
  131. return 0;
  132. }