ua_plugin_securitypolicy.h 14 KB

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