testing_policy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  5. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  6. #include <ua_types.h>
  7. #include "ua_securitypolicy_none.h"
  8. #include "ua_types_generated_handling.h"
  9. #include "testing_policy.h"
  10. #include "check.h"
  11. #define SET_CALLED(func) funcsCalled->func = true
  12. #define SYM_ENCRYPTION_BLOCK_SIZE 2
  13. #define SYM_SIGNING_KEY_LENGTH 3
  14. #define SYM_ENCRYPTION_KEY_LENGTH 5
  15. funcs_called *funcsCalled;
  16. static UA_StatusCode
  17. verify_testing(const UA_SecurityPolicy *securityPolicy,
  18. const void *channelContext,
  19. const UA_ByteString *message,
  20. const UA_ByteString *signature) {
  21. return UA_STATUSCODE_GOOD;
  22. }
  23. static UA_StatusCode
  24. sign_testing(const UA_SecurityPolicy *securityPolicy,
  25. const void *channelContext,
  26. const UA_ByteString *message,
  27. UA_ByteString *signature) {
  28. return UA_STATUSCODE_GOOD;
  29. }
  30. static size_t
  31. asym_getLocalSignatureSize_testing(const UA_SecurityPolicy *securityPolicy,
  32. const void *channelContext) {
  33. return 0;
  34. }
  35. static size_t
  36. asym_getRemoteSignatureSize_testing(const UA_SecurityPolicy *securityPolicy,
  37. const void *channelContext) {
  38. return 0;
  39. }
  40. static size_t
  41. asym_getLocalEncryptionKeyLength_testing(const UA_SecurityPolicy *securityPolicy,
  42. const void *channelContext) {
  43. return 0;
  44. }
  45. static size_t
  46. asym_getRemoteEncryptionKeyLength_testing(const UA_SecurityPolicy *securityPolicy,
  47. const void *channelContext) {
  48. return 0;
  49. }
  50. static size_t
  51. sym_getLocalSignatureSize_testing(const UA_SecurityPolicy *securityPolicy,
  52. const void *channelContext) {
  53. return 0;
  54. }
  55. static size_t
  56. sym_getRemoteSignatureSize_testing(const UA_SecurityPolicy *securityPolicy,
  57. const void *channelContext) {
  58. return 0;
  59. }
  60. static size_t
  61. sym_getLocalEncryptionKeyLength_testing(const UA_SecurityPolicy *securityPolicy,
  62. const void *channelContext) {
  63. ck_assert(securityPolicy);
  64. ck_assert(channelContext);
  65. return SYM_ENCRYPTION_KEY_LENGTH;
  66. }
  67. static size_t
  68. sym_getRemoteEncryptionKeyLength_testing(const UA_SecurityPolicy *securityPolicy,
  69. const void *channelContext) {
  70. return SYM_ENCRYPTION_KEY_LENGTH;
  71. }
  72. static UA_StatusCode
  73. encrypt_testing(const UA_SecurityPolicy *securityPolicy,
  74. const void *channelContext,
  75. UA_ByteString *data) {
  76. return UA_STATUSCODE_GOOD;
  77. }
  78. static UA_StatusCode
  79. decrypt_testing(const UA_SecurityPolicy *securityPolicy,
  80. const void *channelContext,
  81. UA_ByteString *data) {
  82. return UA_STATUSCODE_GOOD;
  83. }
  84. static UA_StatusCode
  85. makeThumbprint_testing(const UA_SecurityPolicy *securityPolicy,
  86. const UA_ByteString *certificate,
  87. UA_ByteString *thumbprint) {
  88. SET_CALLED(makeCertificateThumbprint);
  89. return UA_STATUSCODE_GOOD;
  90. }
  91. static UA_StatusCode
  92. compareThumbprint_testing(const UA_SecurityPolicy *securityPolicy,
  93. const UA_ByteString *certificateThumbprint) {
  94. return UA_STATUSCODE_GOOD;
  95. }
  96. static UA_StatusCode
  97. generateKey_testing(const UA_SecurityPolicy *securityPolicy,
  98. const UA_ByteString *secret,
  99. const UA_ByteString *seed,
  100. UA_ByteString *out) {
  101. SET_CALLED(generateKey);
  102. ck_assert(securityPolicy);
  103. ck_assert(secret);
  104. ck_assert(seed);
  105. ck_assert(out);
  106. return UA_STATUSCODE_GOOD;
  107. }
  108. static UA_StatusCode
  109. generateNonce_testing(const UA_SecurityPolicy *securityPolicy,
  110. UA_ByteString *out) {
  111. out->data = UA_Byte_new();
  112. if(!out->data)
  113. return UA_STATUSCODE_BADOUTOFMEMORY;
  114. out->length = 1;
  115. out->data[0] = 'a';
  116. return UA_STATUSCODE_GOOD;
  117. }
  118. static UA_StatusCode
  119. newContext_testing(const UA_SecurityPolicy *securityPolicy,
  120. const UA_ByteString *remoteCertificate,
  121. void **channelContext) {
  122. SET_CALLED(newContext);
  123. ck_assert(securityPolicy);
  124. ck_assert(remoteCertificate);
  125. ck_assert(channelContext);
  126. *channelContext = (void *) funcsCalled;
  127. return UA_STATUSCODE_GOOD;
  128. }
  129. static void
  130. deleteContext_testing(void *channelContext) {
  131. SET_CALLED(deleteContext);
  132. ck_assert(channelContext);
  133. }
  134. static UA_StatusCode
  135. setLocalSymEncryptingKey_testing(void *channelContext,
  136. const UA_ByteString *val) {
  137. SET_CALLED(setLocalSymEncryptingKey);
  138. ck_assert(channelContext);
  139. ck_assert(val);
  140. ck_assert(val->data);
  141. ck_assert_msg(val->length == SYM_ENCRYPTION_KEY_LENGTH,
  142. "Expected length to be %i but got %i",
  143. SYM_ENCRYPTION_KEY_LENGTH,
  144. val->length);
  145. return UA_STATUSCODE_GOOD;
  146. }
  147. static UA_StatusCode
  148. setLocalSymSigningKey_testing(void *channelContext,
  149. const UA_ByteString *val) {
  150. SET_CALLED(setLocalSymSigningKey);
  151. ck_assert(channelContext);
  152. ck_assert(val);
  153. ck_assert(val->data);
  154. ck_assert_msg(val->length == SYM_SIGNING_KEY_LENGTH,
  155. "Expected length to be %i but got %i",
  156. SYM_SIGNING_KEY_LENGTH,
  157. val->length);
  158. return UA_STATUSCODE_GOOD;
  159. }
  160. static UA_StatusCode
  161. setLocalSymIv_testing(void *channelContext,
  162. const UA_ByteString *val) {
  163. SET_CALLED(setLocalSymIv);
  164. ck_assert(channelContext);
  165. ck_assert(val);
  166. ck_assert(val->data);
  167. ck_assert_msg(val->length == SYM_ENCRYPTION_BLOCK_SIZE,
  168. "Expected length to be %i but got %i",
  169. SYM_ENCRYPTION_BLOCK_SIZE,
  170. val->length);
  171. return UA_STATUSCODE_GOOD;
  172. }
  173. static UA_StatusCode
  174. setRemoteSymEncryptingKey_testing(void *channelContext,
  175. const UA_ByteString *val) {
  176. SET_CALLED(setRemoteSymEncryptingKey);
  177. ck_assert(channelContext);
  178. ck_assert(val);
  179. ck_assert(val->data);
  180. ck_assert_msg(val->length == SYM_ENCRYPTION_KEY_LENGTH,
  181. "Expected length to be %i but got %i",
  182. SYM_ENCRYPTION_KEY_LENGTH,
  183. val->length);
  184. return UA_STATUSCODE_GOOD;
  185. }
  186. static UA_StatusCode
  187. setRemoteSymSigningKey_testing(void *channelContext,
  188. const UA_ByteString *val) {
  189. SET_CALLED(setRemoteSymSigningKey);
  190. ck_assert(channelContext);
  191. ck_assert(val);
  192. ck_assert(val->data);
  193. ck_assert_msg(val->length == SYM_SIGNING_KEY_LENGTH,
  194. "Expected length to be %i but got %i",
  195. SYM_SIGNING_KEY_LENGTH,
  196. val->length);
  197. return UA_STATUSCODE_GOOD;
  198. }
  199. static UA_StatusCode
  200. setRemoteSymIv_testing(void *channelContext,
  201. const UA_ByteString *val) {
  202. SET_CALLED(setRemoteSymIv);
  203. ck_assert(channelContext);
  204. ck_assert(val);
  205. ck_assert(val->data);
  206. ck_assert_msg(val->length == SYM_ENCRYPTION_BLOCK_SIZE,
  207. "Expected length to be %i but got %i",
  208. SYM_ENCRYPTION_BLOCK_SIZE,
  209. val->length);
  210. return UA_STATUSCODE_GOOD;
  211. }
  212. static size_t
  213. getRemoteAsymPlainTextBlockSize_testing(const void *channelContext) {
  214. return 5;
  215. }
  216. static size_t
  217. getRemoteAsymEncryptionBufferLengthOverhead_testing(const void *channelContext,
  218. size_t maxEncryptionLength) {
  219. return 0;
  220. }
  221. static UA_StatusCode
  222. compareCertificate_testing(const void *channelContext,
  223. const UA_ByteString *certificate) {
  224. return UA_STATUSCODE_GOOD;
  225. }
  226. static void
  227. policy_deletemembers_testing(UA_SecurityPolicy *policy) {
  228. UA_ByteString_deleteMembers(&policy->localCertificate);
  229. }
  230. UA_StatusCode
  231. TestingPolicy(UA_SecurityPolicy *policy, const UA_ByteString localCertificate,
  232. funcs_called *fCalled) {
  233. funcsCalled = fCalled;
  234. policy->policyContext = (void *) funcsCalled;
  235. policy->policyUri = UA_STRING("http://opcfoundation.org/UA/SecurityPolicy#Testing");
  236. policy->logger = NULL;
  237. UA_ByteString_copy(&localCertificate, &policy->localCertificate);
  238. policy->asymmetricModule.makeCertificateThumbprint = makeThumbprint_testing;
  239. policy->asymmetricModule.compareCertificateThumbprint = compareThumbprint_testing;
  240. policy->asymmetricModule.cryptoModule.signatureAlgorithmUri = UA_STRING_NULL;
  241. policy->asymmetricModule.cryptoModule.verify = verify_testing;
  242. policy->asymmetricModule.cryptoModule.sign = sign_testing;
  243. policy->asymmetricModule.cryptoModule.getLocalSignatureSize = asym_getLocalSignatureSize_testing;
  244. policy->asymmetricModule.cryptoModule.getRemoteSignatureSize = asym_getRemoteSignatureSize_testing;
  245. policy->asymmetricModule.cryptoModule.encrypt = encrypt_testing;
  246. policy->asymmetricModule.cryptoModule.decrypt = decrypt_testing;
  247. policy->asymmetricModule.cryptoModule.getLocalEncryptionKeyLength = asym_getLocalEncryptionKeyLength_testing;
  248. policy->asymmetricModule.cryptoModule.getRemoteEncryptionKeyLength = asym_getRemoteEncryptionKeyLength_testing;
  249. policy->symmetricModule.generateKey = generateKey_testing;
  250. policy->symmetricModule.generateNonce = generateNonce_testing;
  251. policy->symmetricModule.cryptoModule.signatureAlgorithmUri = UA_STRING_NULL;
  252. policy->symmetricModule.cryptoModule.verify = verify_testing;
  253. policy->symmetricModule.cryptoModule.sign = sign_testing;
  254. policy->symmetricModule.cryptoModule.getLocalSignatureSize = sym_getLocalSignatureSize_testing;
  255. policy->symmetricModule.cryptoModule.getRemoteSignatureSize = sym_getRemoteSignatureSize_testing;
  256. policy->symmetricModule.cryptoModule.encrypt = encrypt_testing;
  257. policy->symmetricModule.cryptoModule.decrypt = decrypt_testing;
  258. policy->symmetricModule.cryptoModule.getLocalEncryptionKeyLength = sym_getLocalEncryptionKeyLength_testing;
  259. policy->symmetricModule.cryptoModule.getRemoteEncryptionKeyLength = sym_getRemoteEncryptionKeyLength_testing;
  260. policy->symmetricModule.encryptionBlockSize = SYM_ENCRYPTION_BLOCK_SIZE;
  261. policy->symmetricModule.signingKeyLength = SYM_SIGNING_KEY_LENGTH;
  262. policy->channelModule.newContext = newContext_testing;
  263. policy->channelModule.deleteContext = deleteContext_testing;
  264. policy->channelModule.setLocalSymEncryptingKey = setLocalSymEncryptingKey_testing;
  265. policy->channelModule.setLocalSymSigningKey = setLocalSymSigningKey_testing;
  266. policy->channelModule.setLocalSymIv = setLocalSymIv_testing;
  267. policy->channelModule.setRemoteSymEncryptingKey = setRemoteSymEncryptingKey_testing;
  268. policy->channelModule.setRemoteSymSigningKey = setRemoteSymSigningKey_testing;
  269. policy->channelModule.setRemoteSymIv = setRemoteSymIv_testing;
  270. policy->channelModule.compareCertificate = compareCertificate_testing;
  271. policy->channelModule.getRemoteAsymPlainTextBlockSize = getRemoteAsymPlainTextBlockSize_testing;
  272. policy->channelModule.getRemoteAsymEncryptionBufferLengthOverhead =
  273. getRemoteAsymEncryptionBufferLengthOverhead_testing;
  274. policy->deleteMembers = policy_deletemembers_testing;
  275. return UA_STATUSCODE_GOOD;
  276. }