ua_plugin_securitypolicy.h 15 KB

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