ua_plugin_securitypolicy.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2017 (c) Mark Giraud, Fraunhofer IOSB
  6. * Copyright 2017 (c) Julius Pfrommer, Fraunhofer IOSB
  7. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  8. */
  9. #ifndef UA_PLUGIN_SECURITYPOLICY_H_
  10. #define UA_PLUGIN_SECURITYPOLICY_H_
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #include "ua_types.h"
  15. #include "ua_types_generated.h"
  16. #include "ua_plugin_log.h"
  17. extern const UA_ByteString UA_SECURITY_POLICY_NONE_URI;
  18. struct UA_SecurityPolicy;
  19. typedef struct UA_SecurityPolicy UA_SecurityPolicy;
  20. typedef struct {
  21. UA_String signatureAlgorithmUri;
  22. /* Verifies the signature of the message using the provided keys in the context.
  23. *
  24. * @param securityPolicy the securityPolicy the function is invoked on.
  25. * @param channelContext the channelContext that contains the key to verify
  26. * the supplied message with.
  27. * @param message the message to which the signature is supposed to belong.
  28. * @param signature the signature of the message, that should be verified. */
  29. UA_StatusCode (*verify)(const UA_SecurityPolicy *securityPolicy,
  30. void *channelContext, const UA_ByteString *message,
  31. const UA_ByteString *signature) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  32. /* Signs the given message using this policys signing algorithm and the
  33. * provided keys in the context.
  34. *
  35. * @param securityPolicy the securityPolicy the function is invoked on.
  36. * @param channelContext the channelContext that contains the key to sign
  37. * the supplied message with.
  38. * @param message the message to sign.
  39. * @param signature an output buffer to which the signature is written. The
  40. * buffer needs to be allocated by the caller. The
  41. * necessary size can be acquired with the signatureSize
  42. * attribute of this module. */
  43. UA_StatusCode (*sign)(const UA_SecurityPolicy *securityPolicy,
  44. void *channelContext, const UA_ByteString *message,
  45. UA_ByteString *signature) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  46. /* Gets the signature size that depends on the local (private) key.
  47. *
  48. * @param securityPolicy the securityPolicy the function is invoked on.
  49. * @param channelContext the channelContext that contains the
  50. * certificate/key.
  51. * @return the size of the local signature. Returns 0 if no local
  52. * certificate was set. */
  53. size_t (*getLocalSignatureSize)(const UA_SecurityPolicy *securityPolicy,
  54. const void *channelContext);
  55. /* Gets the signature size that depends on the remote (public) key.
  56. *
  57. * @param securityPolicy the securityPolicy the function is invoked on.
  58. * @param channelContext the context to retrieve data from.
  59. * @return the size of the remote signature. Returns 0 if no
  60. * remote certificate was set previousely. */
  61. size_t (*getRemoteSignatureSize)(const UA_SecurityPolicy *securityPolicy,
  62. const void *channelContext);
  63. UA_String encryptionAlgorithmUri;
  64. /* Encrypt the given data in place using an asymmetric algorithm and keys.
  65. *
  66. * @param securityPolicy the securityPolicy the function is invoked on.
  67. * @param channelContext the channelContext which contains information about
  68. * the keys to encrypt data.
  69. * @param data the data that is encrypted. The encrypted data will overwrite
  70. * the data that was supplied. */
  71. UA_StatusCode(*encrypt)(const UA_SecurityPolicy *securityPolicy,
  72. void *channelContext,
  73. UA_ByteString *data) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  74. /* Decrypts the given ciphertext in place using an asymmetric algorithm and
  75. * key.
  76. *
  77. * @param securityPolicy the securityPolicy the function is invoked on.
  78. * @param channelContext the channelContext which contains information about
  79. * the keys needed to decrypt the message.
  80. * @param data the data to decrypt. The decryption is done in place. */
  81. UA_StatusCode(*decrypt)(const UA_SecurityPolicy *securityPolicy,
  82. void *channelContext,
  83. UA_ByteString *data) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  84. /* Returns the length of the key used locally to encrypt messages in bits
  85. *
  86. * @param securityPolicy the securityPolicy the function is invoked on.
  87. * @param channelContext the context to retrieve data from.
  88. * @return the length of the local key. Returns 0 if no
  89. * key length is known. */
  90. size_t (*getLocalEncryptionKeyLength)(const UA_SecurityPolicy *securityPolicy,
  91. const void *channelContext);
  92. /* Returns the length of the key used remotely to encrypt messages in bits
  93. *
  94. * @param securityPolicy the securityPolicy the function is invoked on.
  95. * @param channelContext the context to retrieve data from.
  96. * @return the length of the remote key. Returns 0 if no
  97. * key length is known. */
  98. size_t (*getRemoteEncryptionKeyLength)(const UA_SecurityPolicy *securityPolicy,
  99. const void *channelContext);
  100. } UA_SecurityPolicyCryptoModule;
  101. typedef struct {
  102. /* Generates a thumbprint for the specified certificate.
  103. *
  104. * @param securityPolicy the securityPolicy the function is invoked on.
  105. * @param certificate the certificate to make a thumbprint of.
  106. * @param thumbprint an output buffer for the resulting thumbprint. Always
  107. * has the length specified in the thumbprintLength in the
  108. * asymmetricModule. */
  109. UA_StatusCode (*makeCertificateThumbprint)(const UA_SecurityPolicy *securityPolicy,
  110. const UA_ByteString *certificate,
  111. UA_ByteString *thumbprint)
  112. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  113. /* Compares the supplied certificate with the certificate in the endpoit context.
  114. *
  115. * @param securityPolicy the policy data that contains the certificate
  116. * to compare to.
  117. * @param certificateThumbprint the certificate thumbprint to compare to the
  118. * one stored in the context.
  119. * @return if the thumbprints match UA_STATUSCODE_GOOD is returned. If they
  120. * don't match or an error occurred an error code is returned. */
  121. UA_StatusCode (*compareCertificateThumbprint)(const UA_SecurityPolicy *securityPolicy,
  122. const UA_ByteString *certificateThumbprint)
  123. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  124. UA_SecurityPolicyCryptoModule cryptoModule;
  125. } UA_SecurityPolicyAsymmetricModule;
  126. typedef struct {
  127. /* Pseudo random function that is used to generate the symmetric keys.
  128. *
  129. * For information on what parameters this function receives in what situation,
  130. * refer to the OPC UA specification 1.03 Part6 Table 33
  131. *
  132. * @param securityPolicy the securityPolicy the function is invoked on.
  133. * @param secret
  134. * @param seed
  135. * @param out an output to write the data to. The length defines the maximum
  136. * number of output bytes that are produced. */
  137. UA_StatusCode (*generateKey)(const UA_SecurityPolicy *securityPolicy,
  138. const UA_ByteString *secret,
  139. const UA_ByteString *seed, UA_ByteString *out)
  140. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  141. /* Random generator for generating nonces.
  142. *
  143. * @param securityPolicy the securityPolicy this function is invoked on.
  144. * Example: myPolicy->generateNonce(myPolicy,
  145. * &outBuff);
  146. * @param out pointer to a buffer to store the nonce in. Needs to be
  147. * allocated by the caller. The buffer is filled with random
  148. * data. */
  149. UA_StatusCode (*generateNonce)(const UA_SecurityPolicy *securityPolicy,
  150. UA_ByteString *out)
  151. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  152. UA_SecurityPolicyCryptoModule cryptoModule;
  153. size_t encryptionBlockSize;
  154. size_t signingKeyLength;
  155. } UA_SecurityPolicySymmetricModule;
  156. typedef struct {
  157. /* This method creates a new context data object.
  158. *
  159. * The caller needs to call delete on the received object to free allocated
  160. * memory. Memory is only allocated if the function succeeds so there is no
  161. * need to manually free the memory pointed to by *channelContext or to
  162. * call delete in case of failure.
  163. *
  164. * @param securityPolicy the policy context of the endpoint that is connected
  165. * to. It will be stored in the channelContext for
  166. * further access by the policy.
  167. * @param remoteCertificate the remote certificate contains the remote
  168. * asymmetric key. The certificate will be verified
  169. * and then stored in the context so that its
  170. * details may be accessed.
  171. * @param channelContext the initialized channelContext that is passed to
  172. * functions that work on a context. */
  173. UA_StatusCode (*newContext)(const UA_SecurityPolicy *securityPolicy,
  174. const UA_ByteString *remoteCertificate,
  175. void **channelContext)
  176. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  177. /* Deletes the the security context. */
  178. void (*deleteContext)(void *channelContext);
  179. /* Sets the local encrypting key in the supplied context.
  180. *
  181. * @param channelContext the context to work on.
  182. * @param key the local encrypting key to store in the context. */
  183. UA_StatusCode (*setLocalSymEncryptingKey)(void *channelContext,
  184. const UA_ByteString *key)
  185. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  186. /* Sets the local signing key in the supplied context.
  187. *
  188. * @param channelContext the context to work on.
  189. * @param key the local signing key to store in the context. */
  190. UA_StatusCode (*setLocalSymSigningKey)(void *channelContext,
  191. const UA_ByteString *key)
  192. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  193. /* Sets the local initialization vector in the supplied context.
  194. *
  195. * @param channelContext the context to work on.
  196. * @param iv the local initialization vector to store in the context. */
  197. UA_StatusCode (*setLocalSymIv)(void *channelContext,
  198. const UA_ByteString *iv)
  199. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  200. /* Sets the remote encrypting key in the supplied context.
  201. *
  202. * @param channelContext the context to work on.
  203. * @param key the remote encrypting key to store in the context. */
  204. UA_StatusCode (*setRemoteSymEncryptingKey)(void *channelContext,
  205. const UA_ByteString *key)
  206. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  207. /* Sets the remote signing key in the supplied context.
  208. *
  209. * @param channelContext the context to work on.
  210. * @param key the remote signing key to store in the context. */
  211. UA_StatusCode (*setRemoteSymSigningKey)(void *channelContext,
  212. const UA_ByteString *key)
  213. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  214. /* Sets the remote initialization vector in the supplied context.
  215. *
  216. * @param channelContext the context to work on.
  217. * @param iv the remote initialization vector to store in the context. */
  218. UA_StatusCode (*setRemoteSymIv)(void *channelContext,
  219. const UA_ByteString *iv)
  220. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  221. /* Compares the supplied certificate with the certificate in the channel
  222. * context.
  223. *
  224. * @param channelContext the channel context data that contains the
  225. * certificate to compare to.
  226. * @param certificate the certificate to compare to the one stored in the context.
  227. * @return if the certificates match UA_STATUSCODE_GOOD is returned. If they
  228. * don't match or an errror occurred an error code is returned. */
  229. UA_StatusCode (*compareCertificate)(const void *channelContext,
  230. const UA_ByteString *certificate)
  231. UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  232. /* Gets the plaintext block size that depends on the remote public key.
  233. *
  234. * @param channelContext the context to retrieve data from.
  235. * @return the size of the plain text block size when encrypting with the
  236. * remote public key. Returns 0 as long as no remote certificate was
  237. * set previousely. */
  238. size_t (*getRemoteAsymPlainTextBlockSize)(const void *channelContext);
  239. /* Gets the number of bytes that are needed by the encryption function in
  240. * addition to the length of the plaintext message. This is needed, since
  241. * most RSA encryption methods have their own padding mechanism included.
  242. * This makes the encrypted message larger than the plainText, so we need to
  243. * have enough room in the buffer for the overhead.
  244. *
  245. * @param channelContext the retrieve data from.
  246. * @param maxEncryptionLength the maximum number of bytes that the data to
  247. * encrypt can be. */
  248. size_t (*getRemoteAsymEncryptionBufferLengthOverhead)(const void *channelContext,
  249. size_t maxEncryptionLength);
  250. } UA_SecurityPolicyChannelModule;
  251. struct UA_SecurityPolicy {
  252. /* Additional data */
  253. void *policyContext;
  254. /* The policy uri that identifies the implemented algorithms */
  255. UA_ByteString policyUri;
  256. /* The local certificate is specific for each SecurityPolicy since it
  257. * depends on the used key length. */
  258. UA_ByteString localCertificate;
  259. /* Function pointers grouped into modules */
  260. UA_SecurityPolicyAsymmetricModule asymmetricModule;
  261. UA_SecurityPolicySymmetricModule symmetricModule;
  262. UA_SecurityPolicyChannelModule channelModule;
  263. UA_Logger logger;
  264. /* Deletes the dynamic content of the policy */
  265. void (*deleteMembers)(UA_SecurityPolicy *policy);
  266. };
  267. typedef struct {
  268. UA_SecurityPolicy securityPolicy;
  269. UA_EndpointDescription endpointDescription;
  270. } UA_Endpoint;
  271. #ifdef __cplusplus
  272. }
  273. #endif
  274. #endif /* UA_PLUGIN_SECURITYPOLICY_H_ */