ua_plugin_securitypolicy.h 13 KB

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