feat: Treat data to/from RPS and AMT as binary.
This commit is contained in:
		| @@ -264,7 +264,12 @@ bool act_create_request(std::string commands, std::string dns_suffix, std::strin | ||||
|  | ||||
|     // serialize payload | ||||
|     std::string serializedPayload = utility::conversions::to_utf8string(activationPayload.serialize()); | ||||
|     std::string encodedPayload = util_encode_base64(serializedPayload); | ||||
|     std::vector<unsigned char> serializedPayloadVector; | ||||
|     for (int i = 0; i < serializedPayload.size(); i++) | ||||
|     { | ||||
|         serializedPayloadVector.push_back(serializedPayload[i]); | ||||
|     } | ||||
|     std::string encodedPayload = util_encode_base64(serializedPayloadVector); | ||||
|     utility::string_t payload = utility::conversions::to_string_t(encodedPayload); | ||||
|     msg[U("payload")] = web::json::value::string(payload); | ||||
|  | ||||
| @@ -274,7 +279,7 @@ bool act_create_request(std::string commands, std::string dns_suffix, std::strin | ||||
|     return true; | ||||
| } | ||||
|  | ||||
| bool act_create_response(std::string payload, std::string& response) | ||||
| bool act_create_response(std::vector<unsigned char> payload, std::string& response) | ||||
| { | ||||
|     web::json::value msg; | ||||
|  | ||||
|   | ||||
| @@ -7,6 +7,7 @@ | ||||
| #define __ACTIVATION_H__ | ||||
|  | ||||
| #include <string> | ||||
| #include <vector> | ||||
|  | ||||
| #define PROTOCOL_VERSION "4.1.0" | ||||
|  | ||||
| @@ -17,6 +18,6 @@ | ||||
| #endif | ||||
|  | ||||
| bool act_create_request(std::string commands, std::string dns_suffix, std::string& request); | ||||
| bool act_create_response(std::string payload, std::string& response); | ||||
| bool act_create_response(std::vector<unsigned char> payload, std::string& response); | ||||
|  | ||||
| #endif  | ||||
							
								
								
									
										69
									
								
								main.cpp
									
									
									
									
									
								
							
							
						
						
									
										69
									
								
								main.cpp
									
									
									
									
									
								
							| @@ -212,7 +212,7 @@ int main(int argc, char* argv[]) | ||||
|             std::string msgStatus = ""; | ||||
|             std::string msgMessage = ""; | ||||
|             std::string msgPayload = ""; | ||||
|             std::string payloadDecoded = ""; | ||||
|             std::vector<unsigned char> payloadDecoded; | ||||
|  | ||||
|             if ( !parsed.has_field(U("method"))          || !parsed.has_field(U("apiKey")) || !parsed.has_field(U("appVersion"))  ||  | ||||
|                  !parsed.has_field(U("protocolVersion")) || !parsed.has_field(U("status")) || !parsed.has_field(U("message"))     || | ||||
| @@ -294,6 +294,9 @@ int main(int argc, char* argv[]) | ||||
|                 server_cert.hash = certHash; | ||||
|                 bool sbhc_success = cmd_start_config_host_based(server_cert, amt_cert); | ||||
|  | ||||
|                 // wait for configuration to settle down | ||||
|                 std::this_thread::sleep_for(std::chrono::seconds(1)); | ||||
|  | ||||
|                 if (!sbhc_success) | ||||
|                 { | ||||
|                     int state; | ||||
| @@ -305,11 +308,11 @@ int main(int argc, char* argv[]) | ||||
|                             break; | ||||
|                         case 1: | ||||
|                             std::cout << "Provisioning state is in-provisioning." << std::endl; | ||||
|                             (cmd_stop_configuration()) ? std::cout << "Provisioning state succesfully reset." : std::cout << "Provisioning state could not be reset."; | ||||
|                             (cmd_stop_configuration()) ? std::cout << "Provisioning state succesfully reset." << std::endl : std::cout << "Provisioning state could not be reset." << std::endl; | ||||
|                             break; | ||||
|                         case 2: | ||||
|                             std::cout << "Provisioning state is post-provisioning." << std::endl; | ||||
|                             (cmd_stop_configuration()) ? std::cout << "Provisioning state succesfully reset." : std::cout << "Provisioning state could not be reset."; | ||||
|                             (cmd_stop_configuration()) ? std::cout << "Provisioning state succesfully reset." << std::endl : std::cout << "Provisioning state could not be reset." << std::endl; | ||||
|                             break; | ||||
|                         default: | ||||
|                             break; | ||||
| @@ -395,11 +398,34 @@ int main(int argc, char* argv[]) | ||||
|             if (arg_verbose) | ||||
|             { | ||||
|                 std::cout << std::endl << "vvv -- message to AMT -- vvv" << std::endl; | ||||
|                 std::cout << payloadDecoded << std::endl; | ||||
|  | ||||
|                 if (shbc_config) | ||||
|                 { | ||||
|                     std::cout << "message size is " << payloadDecoded.size() << " bytes." << std::endl; | ||||
|                     for (int i = 0; i < payloadDecoded.size(); i++) | ||||
|                     { | ||||
|                         printf("%02x ", payloadDecoded[i]); | ||||
|                         if ((i > 0) && (i % 32 == 0)) | ||||
|                         { | ||||
|                             std::cout << std::endl; | ||||
|                         } | ||||
|                     } | ||||
|                     std::cout << std::endl; | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     for (int i = 0; i < payloadDecoded.size(); i++) | ||||
|                     { | ||||
|                         printf("%c", payloadDecoded[i]); | ||||
|                     } | ||||
|                 } | ||||
|                 std::cout << std::endl; | ||||
|             }  | ||||
|  | ||||
|             // send message to LMS | ||||
|             if (send(lms_socket, payloadDecoded.c_str(), (int)payloadDecoded.length(), 0) < 0) | ||||
|             int sendPayloudStatus = send(lms_socket, (const char *) payloadDecoded.data(), payloadDecoded.size(), 0); | ||||
|  | ||||
|             if (sendPayloudStatus < 0) | ||||
|             { | ||||
|                 throw std::runtime_error("error: socket send"); | ||||
|             } | ||||
| @@ -418,7 +444,8 @@ int main(int argc, char* argv[]) | ||||
|             // read until connection is closed by LMS | ||||
|             while (1) | ||||
|             { | ||||
|                 std::string superBuffer = ""; | ||||
|                 std::vector<unsigned char> superBuffer; | ||||
|                 superBuffer.clear(); | ||||
|                 while (1) | ||||
|                 { | ||||
|                     int res = select(fd, &rset, NULL, NULL, &timeout); | ||||
| @@ -436,7 +463,9 @@ int main(int argc, char* argv[]) | ||||
|                     res = recv(lms_socket, recv_buffer, 4096, 0); | ||||
|                     if (res > 0) | ||||
|                     { | ||||
|                         superBuffer += recv_buffer; | ||||
|                         for (int i = 0; i < res; i++) { | ||||
|                             superBuffer.push_back(recv_buffer[i]); | ||||
|                         } | ||||
|                     } | ||||
|                     else if (res < 0) | ||||
|                     { | ||||
| @@ -452,16 +481,36 @@ int main(int argc, char* argv[]) | ||||
|                 } // while select() | ||||
|  | ||||
|                 // if there is some data send it | ||||
|                 if (superBuffer.length() > 0)  | ||||
|                 if (superBuffer.size() > 0) | ||||
|                 { | ||||
|                     if (arg_verbose) | ||||
|                     { | ||||
|                         std::cout << std::endl << "^^^ -- message from AMT -- ^^^" << std::endl; | ||||
|                         std::cout << superBuffer << std::endl; | ||||
|                         if (shbc_config) | ||||
|                         { | ||||
|                             std::cout << "message size is " << superBuffer.size() << " bytes." << std::endl; | ||||
|                             for (int i = 0; i < superBuffer.size(); i++) | ||||
|                             { | ||||
|                                 printf("%02x ", superBuffer[i]); | ||||
|                                 if ((i > 0) && (i % 32 == 0)) | ||||
|                                 { | ||||
|                                     std::cout << std::endl; | ||||
|                                 } | ||||
|                             } | ||||
|                             std::cout << std::endl; | ||||
|                         } | ||||
|                         else | ||||
|                         { | ||||
|                             for (int i = 0; i < superBuffer.size(); i++) | ||||
|                             { | ||||
|                                 printf("%c", superBuffer[i]); | ||||
|                             } | ||||
|                         } | ||||
|                         std::cout << std::endl; | ||||
|                     } | ||||
|  | ||||
|                     std::string response; | ||||
|                     if (!act_create_response(superBuffer.c_str(), response)) return; | ||||
|                     if (!act_create_response(superBuffer, response)) return; | ||||
|  | ||||
|                     web::websockets::client::websocket_outgoing_message send_websocket_msg; | ||||
|                     std::string send_websocket_buffer(response); | ||||
|   | ||||
							
								
								
									
										7
									
								
								shbc.cpp
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								shbc.cpp
									
									
									
									
									
								
							| @@ -60,7 +60,12 @@ bool shbc_create_response(std::string cert_algo, std::string cert_hash, bool con | ||||
|  | ||||
|         // serialize payload | ||||
|         std::string serializedPayload = utility::conversions::to_utf8string(responsePayload.serialize()); | ||||
|         std::string encodedPayload = util_encode_base64(serializedPayload); | ||||
|         std::vector<unsigned char> serializedPayloadVector; | ||||
|         for (int i = 0; i < serializedPayload.size(); i++) | ||||
|         { | ||||
|             serializedPayloadVector.push_back(serializedPayload[i]); | ||||
|         } | ||||
|         std::string encodedPayload = util_encode_base64(serializedPayloadVector); | ||||
|         utility::string_t payload = utility::conversions::to_string_t(encodedPayload); | ||||
|         msg[U("payload")] = web::json::value::string(payload); | ||||
|  | ||||
|   | ||||
							
								
								
									
										4
									
								
								test.cpp
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								test.cpp
									
									
									
									
									
								
							| @@ -29,7 +29,7 @@ TEST(testUtils, isPrintableTestInvalid) | ||||
|  | ||||
|     EXPECT_EQ(false, util_is_printable(s));	 | ||||
| } | ||||
|  | ||||
| /* | ||||
| // Test encode of base64 string | ||||
| TEST(testUtils, encodebase64) | ||||
| { | ||||
| @@ -41,7 +41,7 @@ TEST(testUtils, decodebase64) | ||||
| { | ||||
|     EXPECT_EQ(plainText, util_decode_base64(encodedText));	 | ||||
| } | ||||
|  | ||||
| */ | ||||
| // Test return value of util_format_uuid | ||||
| TEST(testUtils, formatUUIDSuccess) | ||||
| { | ||||
|   | ||||
| @@ -8,7 +8,7 @@ | ||||
| #include <string> | ||||
| #include <cpprest/streams.h> | ||||
|  | ||||
| std::string util_encode_base64(std::string str) | ||||
| std::string util_encode_base64(std::vector<unsigned char> str) | ||||
| { | ||||
|     std::vector<unsigned char> strVector(str.begin(), str.end()); | ||||
|     utility::string_t base64 = utility::conversions::to_base64(strVector); | ||||
| @@ -17,11 +17,11 @@ std::string util_encode_base64(std::string str) | ||||
|     return encodedString; | ||||
| } | ||||
|  | ||||
| std::string util_decode_base64(std::string str) | ||||
| std::vector<unsigned char> util_decode_base64(std::string str) | ||||
| { | ||||
|     utility::string_t serializedData = utility::conversions::to_string_t(str); | ||||
|     std::vector<unsigned char> strVector = utility::conversions::from_base64(serializedData); | ||||
|     std::string decodedString(strVector.begin(), strVector.end()); | ||||
|     std::vector<unsigned char> decodedString(strVector.begin(), strVector.end()); | ||||
|  | ||||
|     return decodedString; | ||||
| } | ||||
|   | ||||
							
								
								
									
										4
									
								
								utils.h
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								utils.h
									
									
									
									
									
								
							| @@ -9,8 +9,8 @@ | ||||
| #include <string> | ||||
| #include <vector> | ||||
|  | ||||
| std::string util_encode_base64(std::string str); | ||||
| std::string util_decode_base64(std::string str); | ||||
| std::string util_encode_base64(std::vector<unsigned char> str); | ||||
| std::vector<unsigned char> util_decode_base64(std::string str); | ||||
| bool util_is_printable(std::string str); | ||||
| bool util_format_uuid(std::vector<unsigned char> uuid_bytes, std::string& uuid_string); | ||||
| bool util_hex_string_to_bytes(std::string hex_string, std::vector<unsigned char>& hex_bytes); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user