CryptoTransform.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*!
  15. * @file Authentication.h
  16. */
  17. #ifndef _FASTDDS_RTPS_SECURITY_CRYPTOGRAPHY_CRYPTOTRANSFORM_H_
  18. #define _FASTDDS_RTPS_SECURITY_CRYPTOGRAPHY_CRYPTOTRANSFORM_H_
  19. #include <fastdds/rtps/security/cryptography/CryptoTypes.h>
  20. #include <fastdds/rtps/common/CDRMessage_t.h>
  21. #include <fastdds/rtps/common/SerializedPayload.h>
  22. namespace eprosima {
  23. namespace fastrtps {
  24. namespace rtps {
  25. namespace security {
  26. class CryptoTransform
  27. {
  28. public:
  29. virtual ~CryptoTransform(){}
  30. /**
  31. * Serializes the payload sent by the user with a Datawriter.
  32. * @param encoded_payload (out) Result of the encryption
  33. * @param extra_inline_qos (out) Contains additional parameters to be added to the inlineQos of the submessage
  34. * @param payload Plain input buffer
  35. * @param sending_datawriter_crypto Returned by a prior call to register_local_datawriter
  36. * @param exception (out) Security exception
  37. * @return TRUE if successful
  38. */
  39. virtual bool encode_serialized_payload(
  40. SerializedPayload_t& encoded_payload,
  41. std::vector<uint8_t>& extra_inline_qos,
  42. const SerializedPayload_t& payload,
  43. DatawriterCryptoHandle& sending_datawriter_crypto,
  44. SecurityException& exception) = 0;
  45. /**
  46. * Encodes a Data, DataFrag, Gap, Heartbeat or HeartBeatFrag
  47. * @param encoded_rtps_submessage (out) Result of the encryption
  48. * @param plain_rtps_submessage Plain input buffer
  49. * @param sending_datawriter_crypto Crypto of the datawriter that sends the message
  50. * @param receiving_datareader_crypto_list Crypto of the datareaders the message is aimed at
  51. * @param exception (out) Security exception
  52. * @return TRUE is successful
  53. */
  54. virtual bool encode_datawriter_submessage(
  55. CDRMessage_t& encoded_rtps_submessage,
  56. const CDRMessage_t& plain_rtps_submessage,
  57. DatawriterCryptoHandle& sending_datawriter_crypto,
  58. std::vector<DatareaderCryptoHandle*>& receiving_datareader_crypto_list,
  59. SecurityException& exception) = 0;
  60. /**
  61. * Encodes an AckNack or NackFrag
  62. * @param encoded_rtps_submessage (out) Result of the encryption
  63. * @param plain_rtps_submessage Plain input buffer
  64. * @param sending_datareader_crypto Crypto of the sending datareader
  65. * @param receiving_datawriter_crypto_list List with Crypto of the intended datawriter recipients
  66. * @param exception (out) Security exception
  67. * @return TRUE if successful
  68. */
  69. virtual bool encode_datareader_submessage(
  70. CDRMessage_t& encoded_rtps_submessage,
  71. const CDRMessage_t& plain_rtps_submessage,
  72. DatareaderCryptoHandle& sending_datareader_crypto,
  73. std::vector<DatawriterCryptoHandle*>& receiving_datawriter_crypto_list,
  74. SecurityException& exception) = 0;
  75. /**
  76. * Encodes a full rtps message
  77. * @param encoded_rtps_message (out) Result of the encryption
  78. * @param plain_rtps_message Plain input buffer
  79. * @param sending_crypto Crypto of the Participant where the message originates from
  80. * @param receiving_crypto_list Crypto of the Partipants the message is intended towards
  81. * @param exception (out) Security expcetion
  82. * @return TRUE if successful
  83. */
  84. virtual bool encode_rtps_message(
  85. CDRMessage_t& encoded_rtps_message,
  86. const CDRMessage_t& plain_rtps_message,
  87. ParticipantCryptoHandle &sending_crypto,
  88. std::vector<ParticipantCryptoHandle*> &receiving_crypto_list,
  89. SecurityException &exception) = 0;
  90. /**
  91. * Reverses the transformation performed by encode_rtps_message. Decrypts the contents and veryfies MACs or digital signatures.
  92. * @param plain_buffer (out) Decoded message
  93. * @param encoded_buffer Encoded message
  94. * @param receiving_crypto Crypto of the Participant that receives the message
  95. * @param sending_crypto Crypto of the Participant that wrote the message
  96. * @param exception (out) Security exception
  97. * @return TRUE is successful
  98. */
  99. virtual bool decode_rtps_message(
  100. CDRMessage_t& plain_buffer,
  101. const CDRMessage_t& encoded_buffer,
  102. const ParticipantCryptoHandle &receiving_crypto,
  103. const ParticipantCryptoHandle &sending_crypto,
  104. SecurityException &exception) = 0;
  105. /**
  106. * Determines whether the secure submessage comes from a datawriter or a data reader and extracts the required CryptoHandle to decode it.
  107. * @param datawriter_crypto (out) Crypto of the sending datawriter, if applicable
  108. * @param datareader_crypto (out) Crypto of the sending datareader, if applicable
  109. * @param secure_submessage_category (out) Specifies wether the message comes from a datawriter or from a datareader
  110. * @param encoded_rtps_submessage encoded input submessage
  111. * @param receiving_crypto Crypto of the Participant that receives the message
  112. * @param sending_crypto Crypto of the Participant that sent the message
  113. * @param exception (out) Security exception
  114. * @return TRUE if successful
  115. */
  116. virtual bool preprocess_secure_submsg(
  117. DatawriterCryptoHandle **datawriter_crypto,
  118. DatareaderCryptoHandle **datareader_crypto,
  119. SecureSubmessageCategory_t &secure_submessage_category,
  120. const CDRMessage_t& encoded_rtps_submessage,
  121. ParticipantCryptoHandle &receiving_crypto,
  122. ParticipantCryptoHandle &sending_crypto,
  123. SecurityException &exception) = 0;
  124. /**
  125. * Called after prprocess_secure_submessage when the submessage category is DATAWRITER_SUBMESSAGE
  126. * @param plain_rtps_submessage (out) Result of the decryption
  127. * @param encoded_rtps_submessage Encoded message
  128. * @param receiving_datareader_crypto Crypto of the target datareader
  129. * @param sending_datawriter_crypto Crypto of the datawriter that sent the message
  130. * @param exception (out) Security exception
  131. * @return TRUE if successful
  132. */
  133. virtual bool decode_datawriter_submessage(
  134. CDRMessage_t& plain_rtps_submessage,
  135. CDRMessage_t& encoded_rtps_submessage,
  136. DatareaderCryptoHandle &receiving_datareader_crypto,
  137. DatawriterCryptoHandle &sending_datawriter_crypto,
  138. SecurityException &exception) = 0;
  139. /**
  140. * Called after preprocess_secure_submessage when the submessage category is DATAREADER_SUBMESSAGE
  141. * @param plain_rtps_submessage (out) Result of the decryption
  142. * @param encoded_rtps_submessage Encoded message
  143. * @param receiving_datawriter_crypto Crypto of the target datawriter
  144. * @param sending_datareader_crypto Crypto of the datareader that sent the message
  145. * @param exception (out) Security exception
  146. * @return TRUE if successful
  147. */
  148. virtual bool decode_datareader_submessage(
  149. CDRMessage_t& plain_rtps_submessage,
  150. CDRMessage_t& encoded_rtps_submessage,
  151. DatawriterCryptoHandle &receiving_datawriter_crypto,
  152. DatareaderCryptoHandle &sending_datareader_crypto,
  153. SecurityException &exception) = 0;
  154. /**
  155. * Undoes the decryption transformation made on the writer side.
  156. * @param plain_payload (out) Result of the decryption
  157. * @param encoded_payload Encoded input buffer
  158. * @param inline_qos Coming from the data message that carries the target payload
  159. * @param receiving_datareader_crypto Crypto of the target datareader
  160. * @param sending_datawriter_crypto Crypto of the datawriter that sent the message
  161. * @param exception (out) Security exception
  162. * @return TRUE if successful
  163. */
  164. virtual bool decode_serialized_payload(
  165. SerializedPayload_t& plain_payload,
  166. const SerializedPayload_t& encoded_payload,
  167. const std::vector<uint8_t>& inline_qos,
  168. DatareaderCryptoHandle& receiving_datareader_crypto,
  169. DatawriterCryptoHandle& sending_datawriter_crypto,
  170. SecurityException& exception) = 0;
  171. virtual uint32_t calculate_extra_size_for_rtps_message(uint32_t number_discovered_participants) const = 0;
  172. virtual uint32_t calculate_extra_size_for_rtps_submessage(uint32_t number_discovered_readers) const = 0;
  173. virtual uint32_t calculate_extra_size_for_encoded_payload(uint32_t number_discovered_readers) const = 0;
  174. };
  175. } //namespace eprosima
  176. } //namespace fastrtps
  177. } //namespace rtps
  178. } //namespace security
  179. #endif //_FASTDDS_RTPS_SECURITY_CRYPTOGRAPHY_CRYPTOTRANSFORM_H_