testing_policy.c 14 KB

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