testing_policy.c 14 KB

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