ua_plugin_securitypolicy.h 14 KB

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