testing_policy.c 12 KB

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