testing_policy.c 14 KB

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