check_securechannel.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <src_generated/ua_types_generated.h>
  7. #include <testing_networklayers.h>
  8. #include <ua_types_encoding_binary.h>
  9. #include <src_generated/ua_transport_generated_encoding_binary.h>
  10. #include <src_generated/ua_transport_generated.h>
  11. #include <ua_types.h>
  12. #include <src_generated/ua_types_generated_encoding_binary.h>
  13. #include <ua_plugin_securitypolicy.h>
  14. #include <src_generated/ua_transport_generated_handling.h>
  15. #include "testing_policy.h"
  16. #include "ua_securechannel.h"
  17. #include "check.h"
  18. #define UA_BYTESTRING_STATIC(s) {sizeof(s)-1, (UA_Byte*)(s)}
  19. // Some default testing sizes. Can be overwritten in testing functions.
  20. #define DEFAULT_SYM_ENCRYPTION_BLOCK_SIZE 2
  21. #define DEFAULT_SYM_SIGNING_KEY_LENGTH 3
  22. #define DEFAULT_SYM_ENCRYPTION_KEY_LENGTH 5
  23. #define DEFAULT_ASYM_REMOTE_SIGNATURE_SIZE 7
  24. #define DEFAULT_ASYM_LOCAL_SIGNATURE_SIZE 11
  25. #define DEFAULT_ASYM_REMOTE_PLAINTEXT_BLOCKSIZE 256
  26. UA_SecureChannel testChannel;
  27. UA_ByteString dummyCertificate = UA_BYTESTRING_STATIC("DUMMY CERTIFICATE DUMMY CERTIFICATE DUMMY CERTIFICATE");
  28. UA_SecurityPolicy dummyPolicy;
  29. UA_Connection testingConnection;
  30. UA_ByteString sentData;
  31. static funcs_called fCalled;
  32. static key_sizes keySizes;
  33. static void
  34. setup_secureChannel(void) {
  35. TestingPolicy(&dummyPolicy, dummyCertificate, &fCalled, &keySizes);
  36. UA_SecureChannel_init(&testChannel, &dummyPolicy, &dummyCertificate);
  37. testingConnection = createDummyConnection(&sentData);
  38. UA_Connection_attachSecureChannel(&testingConnection, &testChannel);
  39. testChannel.connection = &testingConnection;
  40. }
  41. static void
  42. teardown_secureChannel(void) {
  43. UA_SecureChannel_deleteMembersCleanup(&testChannel);
  44. dummyPolicy.deleteMembers(&dummyPolicy);
  45. memset(&testingConnection, 0, sizeof(UA_Connection));
  46. }
  47. static void
  48. setup_funcs_called(void) {
  49. memset(&fCalled, 0, sizeof(struct funcs_called));
  50. }
  51. static void
  52. teardown_funcs_called(void) {
  53. memset(&fCalled, 0, sizeof(struct funcs_called));
  54. }
  55. static void
  56. setup_key_sizes(void) {
  57. memset(&keySizes, 0, sizeof(struct key_sizes));
  58. keySizes.sym_sig_keyLen = DEFAULT_SYM_SIGNING_KEY_LENGTH;
  59. keySizes.sym_enc_blockSize = DEFAULT_SYM_ENCRYPTION_BLOCK_SIZE;
  60. keySizes.sym_enc_keyLen = DEFAULT_SYM_ENCRYPTION_KEY_LENGTH;
  61. keySizes.asym_lcl_sig_size = DEFAULT_ASYM_LOCAL_SIGNATURE_SIZE;
  62. keySizes.asym_rmt_sig_size = DEFAULT_ASYM_REMOTE_SIGNATURE_SIZE;
  63. keySizes.asym_rmt_ptext_blocksize = DEFAULT_ASYM_REMOTE_PLAINTEXT_BLOCKSIZE;
  64. keySizes.asym_rmt_enc_key_size = 2048;
  65. keySizes.asym_lcl_enc_key_size = 1024;
  66. }
  67. static void
  68. teardown_key_sizes(void) {
  69. memset(&keySizes, 0, sizeof(struct key_sizes));
  70. }
  71. /*
  72. static void
  73. setup_dummyPolicy(void) {
  74. TestingPolicy(&dummyPolicy, dummyCertificate, &fCalled);
  75. }
  76. static void
  77. teardown_dummyPolicy(void) {
  78. dummyPolicy.deleteMembers(&dummyPolicy);
  79. }*/
  80. START_TEST(SecureChannel_initAndDelete)
  81. {
  82. TestingPolicy(&dummyPolicy, dummyCertificate, &fCalled, &keySizes);
  83. UA_StatusCode retval;
  84. UA_SecureChannel channel;
  85. retval = UA_SecureChannel_init(&channel, &dummyPolicy, &dummyCertificate);
  86. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Expected StatusCode to be good");
  87. ck_assert_msg(channel.state == UA_SECURECHANNELSTATE_FRESH, "Expected state to be fresh");
  88. ck_assert_msg(fCalled.newContext, "Expected newContext to have been called");
  89. ck_assert_msg(fCalled.makeCertificateThumbprint, "Expected makeCertificateThumbprint to have been called");
  90. ck_assert_msg(channel.securityPolicy == &dummyPolicy, "SecurityPolicy not set correctly");
  91. UA_SecureChannel_deleteMembersCleanup(&channel);
  92. ck_assert_msg(fCalled.deleteContext, "Expected deleteContext to have been called");
  93. dummyPolicy.deleteMembers(&dummyPolicy);
  94. }
  95. END_TEST
  96. START_TEST(SecureChannel_initAndDelete_invalidParameters)
  97. {
  98. UA_StatusCode retval = UA_SecureChannel_init(NULL, NULL, NULL);
  99. ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected init to fail");
  100. UA_SecureChannel channel;
  101. retval = UA_SecureChannel_init(&channel, &dummyPolicy, NULL);
  102. ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected init to fail");
  103. retval = UA_SecureChannel_init(&channel, NULL, &dummyCertificate);
  104. ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected init to fail");
  105. retval = UA_SecureChannel_init(NULL, &dummyPolicy, &dummyCertificate);
  106. ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected init to fail");
  107. UA_SecureChannel_deleteMembersCleanup(NULL);
  108. }
  109. END_TEST
  110. START_TEST(SecureChannel_generateNewKeys)
  111. {
  112. UA_StatusCode retval = UA_SecureChannel_generateNewKeys(&testChannel);
  113. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Expected Statuscode to be good");
  114. ck_assert_msg(fCalled.generateKey, "Expected generateKey to have been called");
  115. ck_assert_msg(fCalled.setLocalSymEncryptingKey, "Expected setLocalSymEncryptingKey to have been called");
  116. ck_assert_msg(fCalled.setLocalSymSigningKey, "Expected setLocalSymSigningKey to have been called");
  117. ck_assert_msg(fCalled.setLocalSymIv, "Expected setLocalSymIv to have been called");
  118. ck_assert_msg(fCalled.setRemoteSymEncryptingKey, "Expected setRemoteSymEncryptingKey to have been called");
  119. ck_assert_msg(fCalled.setRemoteSymSigningKey, "Expected setRemoteSymSigningKey to have been called");
  120. ck_assert_msg(fCalled.setRemoteSymIv, "Expected setRemoteSymIv to have been called");
  121. retval = UA_SecureChannel_generateNewKeys(NULL);
  122. ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected failure on NULL pointer");
  123. }
  124. END_TEST
  125. START_TEST(SecureChannel_revolveTokens)
  126. {
  127. // Fake that no token was issued by setting 0
  128. testChannel.nextSecurityToken.tokenId = 0;
  129. UA_StatusCode retval = UA_SecureChannel_revolveTokens(&testChannel);
  130. ck_assert_msg(retval == UA_STATUSCODE_BADSECURECHANNELTOKENUNKNOWN,
  131. "Expected failure because tokenId 0 signifies that no token was issued");
  132. // Fake an issued token by setting an id
  133. testChannel.nextSecurityToken.tokenId = 10;
  134. retval = UA_SecureChannel_revolveTokens(&testChannel);
  135. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Expected function to return GOOD");
  136. ck_assert_msg(fCalled.generateKey,
  137. "Expected generateKey to be called because new keys need to be generated,"
  138. "when switching to the next token.");
  139. UA_ChannelSecurityToken testToken;
  140. UA_ChannelSecurityToken_init(&testToken);
  141. ck_assert_msg(memcmp(&testChannel.nextSecurityToken, &testToken, sizeof(UA_ChannelSecurityToken)) == 0,
  142. "Expected the next securityToken to be freshly initialized");
  143. ck_assert_msg(testChannel.securityToken.tokenId == 10, "Expected token to have been copied");
  144. }
  145. END_TEST
  146. static void
  147. createDummyResponse(UA_OpenSecureChannelResponse *response) {
  148. UA_OpenSecureChannelResponse_init(response);
  149. memset(response, 0, sizeof(UA_OpenSecureChannelResponse));
  150. }
  151. START_TEST(SecureChannel_sendAsymmetricOPNMessage_withoutConnection)
  152. {
  153. UA_OpenSecureChannelResponse dummyResponse;
  154. createDummyResponse(&dummyResponse);
  155. testChannel.securityMode = UA_MESSAGESECURITYMODE_NONE;
  156. // Remove connection to provoke error
  157. UA_Connection_detachSecureChannel(testChannel.connection);
  158. testChannel.connection = NULL;
  159. UA_StatusCode retval = UA_SecureChannel_sendAsymmetricOPNMessage(&testChannel,
  160. 42,
  161. &dummyResponse,
  162. &UA_TYPES[UA_TYPES_OPENSECURECHANNELRESPONSE]);
  163. ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected failure without a connection");
  164. }
  165. END_TEST
  166. START_TEST(SecureChannel_sendAsymmetricOPNMessage_invalidParameters)
  167. {
  168. UA_OpenSecureChannelResponse dummyResponse;
  169. createDummyResponse(&dummyResponse);
  170. UA_StatusCode retval = UA_SecureChannel_sendAsymmetricOPNMessage(&testChannel,
  171. 42,
  172. NULL,
  173. &UA_TYPES[UA_TYPES_OPENSECURECHANNELRESPONSE]);
  174. ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected failure");
  175. retval = UA_SecureChannel_sendAsymmetricOPNMessage(&testChannel,
  176. 42,
  177. &dummyResponse,
  178. NULL);
  179. ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected failure");
  180. retval = UA_SecureChannel_sendAsymmetricOPNMessage(NULL,
  181. 42,
  182. &dummyResponse,
  183. &UA_TYPES[UA_TYPES_OPENSECURECHANNELRESPONSE]);
  184. ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected failure");
  185. }
  186. END_TEST
  187. START_TEST(SecureChannel_sendAsymmetricOPNMessage_SecurityModeInvalid)
  188. {
  189. // Configure our channel correctly for OPN messages and setup dummy message
  190. UA_OpenSecureChannelResponse dummyResponse;
  191. createDummyResponse(&dummyResponse);
  192. testChannel.securityMode = UA_MESSAGESECURITYMODE_INVALID;
  193. UA_StatusCode retval = UA_SecureChannel_sendAsymmetricOPNMessage(&testChannel,
  194. 42,
  195. &dummyResponse,
  196. &UA_TYPES[UA_TYPES_OPENSECURECHANNELRESPONSE]);
  197. ck_assert_msg(retval == UA_STATUSCODE_BADSECURITYMODEREJECTED, "Expected SecurityMode rejected error");
  198. }
  199. END_TEST
  200. START_TEST(SecureChannel_sendAsymmetricOPNMessage_SecurityModeNone)
  201. {
  202. // Configure our channel correctly for OPN messages and setup dummy message
  203. UA_OpenSecureChannelResponse dummyResponse;
  204. createDummyResponse(&dummyResponse);
  205. testChannel.securityMode = UA_MESSAGESECURITYMODE_NONE;
  206. UA_StatusCode retval = UA_SecureChannel_sendAsymmetricOPNMessage(&testChannel,
  207. 42,
  208. &dummyResponse,
  209. &UA_TYPES[UA_TYPES_OPENSECURECHANNELRESPONSE]);
  210. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Expected function to succeed");
  211. ck_assert_msg(!fCalled.asym_enc, "Message encryption was called but should not have been");
  212. ck_assert_msg(!fCalled.asym_sign, "Message signing was called but should not have been");
  213. }
  214. END_TEST
  215. START_TEST(SecureChannel_sendAsymmetricOPNMessage_SecurityModeSign)
  216. {
  217. // Configure our channel correctly for OPN messages and setup dummy message
  218. UA_OpenSecureChannelResponse dummyResponse;
  219. createDummyResponse(&dummyResponse);
  220. testChannel.securityMode = UA_MESSAGESECURITYMODE_SIGN;
  221. UA_StatusCode retval = UA_SecureChannel_sendAsymmetricOPNMessage(&testChannel,
  222. 42,
  223. &dummyResponse,
  224. &UA_TYPES[UA_TYPES_OPENSECURECHANNELRESPONSE]);
  225. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Expected function to succeed");
  226. ck_assert_msg(fCalled.asym_enc, "Expected message to have been encrypted but it was not");
  227. ck_assert_msg(fCalled.asym_sign, "Expected message to have been signed but it was not");
  228. }
  229. END_TEST
  230. START_TEST(SecureChannel_sendAsymmetricOPNMessage_SecurityModeSignAndEncrypt)
  231. {
  232. // Configure our channel correctly for OPN messages and setup dummy message
  233. UA_OpenSecureChannelResponse dummyResponse;
  234. createDummyResponse(&dummyResponse);
  235. testChannel.securityMode = UA_MESSAGESECURITYMODE_SIGNANDENCRYPT;
  236. UA_StatusCode retval = UA_SecureChannel_sendAsymmetricOPNMessage(&testChannel,
  237. 42,
  238. &dummyResponse,
  239. &UA_TYPES[UA_TYPES_OPENSECURECHANNELRESPONSE]);
  240. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Expected function to succeed");
  241. ck_assert_msg(fCalled.asym_enc, "Expected message to have been encrypted but it was not");
  242. ck_assert_msg(fCalled.asym_sign, "Expected message to have been signed but it was not");
  243. }
  244. END_TEST
  245. START_TEST(SecureChannel_sendAsymmetricOPNMessage_sentDataIsValid)
  246. {
  247. UA_OpenSecureChannelResponse dummyResponse;
  248. createDummyResponse(&dummyResponse);
  249. testChannel.securityMode = UA_MESSAGESECURITYMODE_SIGNANDENCRYPT;
  250. UA_UInt32 requestId = UA_UInt32_random();
  251. UA_StatusCode retval = UA_SecureChannel_sendAsymmetricOPNMessage(&testChannel,
  252. requestId,
  253. &dummyResponse,
  254. &UA_TYPES[UA_TYPES_OPENSECURECHANNELRESPONSE]);
  255. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Expected function to succeed");
  256. size_t offset = 0;
  257. UA_SecureConversationMessageHeader header;
  258. UA_SecureConversationMessageHeader_decodeBinary(&sentData, &offset, &header);
  259. UA_AsymmetricAlgorithmSecurityHeader asymSecurityHeader;
  260. UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(&sentData, &offset, &asymSecurityHeader);
  261. ck_assert_msg(UA_ByteString_equal(&dummyCertificate, &asymSecurityHeader.senderCertificate),
  262. "Expected the certificate to be equal to the one used by the secureChannel");
  263. ck_assert_msg(UA_ByteString_equal(&testChannel.securityPolicy->policyUri,
  264. &asymSecurityHeader.securityPolicyUri),
  265. "Expected securityPolicyUri to be equal to the one used by the secureChannel");
  266. UA_ByteString thumbPrint = {20, testChannel.remoteCertificateThumbprint};
  267. ck_assert_msg(UA_ByteString_equal(&thumbPrint,
  268. &asymSecurityHeader.receiverCertificateThumbprint),
  269. "Expected receiverCertificateThumbprint to be equal to the one set in the secureChannel");
  270. for(size_t i = offset; i < header.messageHeader.messageSize; ++i) {
  271. sentData.data[i] = (UA_Byte) ((sentData.data[i] - 1) % (UA_BYTE_MAX + 1));
  272. }
  273. UA_SequenceHeader sequenceHeader;
  274. UA_SequenceHeader_decodeBinary(&sentData, &offset, &sequenceHeader);
  275. ck_assert_msg(sequenceHeader.requestId == requestId, "Expected requestId to be %i but was %i",
  276. requestId,
  277. sequenceHeader.requestId);
  278. UA_NodeId original = UA_NODEID_NUMERIC(0, UA_TYPES[UA_TYPES_OPENSECURECHANNELRESPONSE].binaryEncodingId);
  279. UA_NodeId requestTypeId;
  280. UA_NodeId_decodeBinary(&sentData, &offset, &requestTypeId);
  281. ck_assert_msg(UA_NodeId_equal(&original, &requestTypeId), "Expected nodeIds to be equal");
  282. UA_OpenSecureChannelResponse sentResponse;
  283. UA_OpenSecureChannelResponse_decodeBinary(&sentData, &offset, &sentResponse);
  284. ck_assert_msg(memcmp(&sentResponse, &dummyResponse, sizeof(UA_OpenSecureChannelResponse)) == 0,
  285. "Expected the sent response to be equal to the one supplied to the send function");
  286. UA_Byte paddingByte = sentData.data[offset];
  287. size_t paddingSize = (size_t) paddingByte;
  288. for(size_t i = 0; i <= paddingSize; ++i) {
  289. ck_assert_msg(sentData.data[offset + i] == paddingByte,
  290. "Expected padding byte %i to be %i but got value %i",
  291. i,
  292. paddingByte,
  293. sentData.data[offset + i]);
  294. }
  295. ck_assert_msg(sentData.data[offset + paddingSize + 1] == '*', "Expected first byte of signature");
  296. UA_SecureConversationMessageHeader_deleteMembers(&header);
  297. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymSecurityHeader);
  298. UA_SequenceHeader_deleteMembers(&sequenceHeader);
  299. UA_OpenSecureChannelResponse_deleteMembers(&sentResponse);
  300. }
  301. END_TEST
  302. START_TEST(Securechannel_sendAsymmetricOPNMessage_extraPaddingPresentWhenKeyLargerThan2048Bits)
  303. {
  304. keySizes.asym_rmt_enc_key_size = 4096;
  305. keySizes.asym_rmt_ptext_blocksize = 4096;
  306. UA_OpenSecureChannelResponse dummyResponse;
  307. createDummyResponse(&dummyResponse);
  308. testChannel.securityMode = UA_MESSAGESECURITYMODE_SIGNANDENCRYPT;
  309. UA_UInt32 requestId = UA_UInt32_random();
  310. UA_StatusCode retval = UA_SecureChannel_sendAsymmetricOPNMessage(&testChannel,
  311. requestId,
  312. &dummyResponse,
  313. &UA_TYPES[UA_TYPES_OPENSECURECHANNELRESPONSE]);
  314. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Expected function to succeed");
  315. size_t offset = 0;
  316. UA_SecureConversationMessageHeader header;
  317. UA_SecureConversationMessageHeader_decodeBinary(&sentData, &offset, &header);
  318. UA_AsymmetricAlgorithmSecurityHeader asymSecurityHeader;
  319. UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(&sentData, &offset, &asymSecurityHeader);
  320. ck_assert_msg(UA_ByteString_equal(&dummyCertificate, &asymSecurityHeader.senderCertificate),
  321. "Expected the certificate to be equal to the one used by the secureChannel");
  322. ck_assert_msg(UA_ByteString_equal(&testChannel.securityPolicy->policyUri,
  323. &asymSecurityHeader.securityPolicyUri),
  324. "Expected securityPolicyUri to be equal to the one used by the secureChannel");
  325. UA_ByteString thumbPrint = {20, testChannel.remoteCertificateThumbprint};
  326. ck_assert_msg(UA_ByteString_equal(&thumbPrint,
  327. &asymSecurityHeader.receiverCertificateThumbprint),
  328. "Expected receiverCertificateThumbprint to be equal to the one set in the secureChannel");
  329. for(size_t i = offset; i < header.messageHeader.messageSize; ++i) {
  330. sentData.data[i] = (UA_Byte) ((sentData.data[i] - 1) % (UA_BYTE_MAX + 1));
  331. }
  332. UA_SequenceHeader sequenceHeader;
  333. UA_SequenceHeader_decodeBinary(&sentData, &offset, &sequenceHeader);
  334. ck_assert_msg(sequenceHeader.requestId == requestId, "Expected requestId to be %i but was %i",
  335. requestId,
  336. sequenceHeader.requestId);
  337. UA_NodeId original = UA_NODEID_NUMERIC(0, UA_TYPES[UA_TYPES_OPENSECURECHANNELRESPONSE].binaryEncodingId);
  338. UA_NodeId requestTypeId;
  339. UA_NodeId_decodeBinary(&sentData, &offset, &requestTypeId);
  340. ck_assert_msg(UA_NodeId_equal(&original, &requestTypeId), "Expected nodeIds to be equal");
  341. UA_OpenSecureChannelResponse sentResponse;
  342. UA_OpenSecureChannelResponse_decodeBinary(&sentData, &offset, &sentResponse);
  343. ck_assert_msg(memcmp(&sentResponse, &dummyResponse, sizeof(UA_OpenSecureChannelResponse)) == 0,
  344. "Expected the sent response to be equal to the one supplied to the send function");
  345. UA_Byte paddingByte = sentData.data[offset];
  346. UA_Byte extraPaddingByte = sentData.data[sentData.length - keySizes.asym_lcl_sig_size - 1];
  347. size_t paddingSize = (size_t) paddingByte;
  348. paddingSize |= extraPaddingByte << 8;
  349. for(size_t i = 0; i <= paddingSize; ++i) {
  350. ck_assert_msg(sentData.data[offset + i] == paddingByte,
  351. "Expected padding byte %i to be %i but got value %i",
  352. i,
  353. paddingByte,
  354. sentData.data[offset + i]);
  355. }
  356. ck_assert_msg(sentData.data[offset + paddingSize + 1] == extraPaddingByte, "Expected extra padding byte to be "
  357. "%i but got %i",
  358. extraPaddingByte, sentData.data[offset + paddingSize + 1]);
  359. ck_assert_msg(sentData.data[offset + paddingSize + 2] == '*', "Expected first byte 42 of signature but got %i",
  360. sentData.data[offset + paddingSize + 2]);
  361. UA_SecureConversationMessageHeader_deleteMembers(&header);
  362. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymSecurityHeader);
  363. UA_SequenceHeader_deleteMembers(&sequenceHeader);
  364. UA_OpenSecureChannelResponse_deleteMembers(&sentResponse);
  365. }
  366. END_TEST
  367. START_TEST(SecureChannel_generateNonce)
  368. {
  369. UA_ByteString myNonce;
  370. UA_ByteString_init(&myNonce);
  371. for(size_t i = 0; i < 129; ++i) {
  372. i = (i == 128) ? 65536 : i; // large edge case
  373. UA_StatusCode retval = UA_SecureChannel_generateNonce(&testChannel, i, &myNonce);
  374. ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Expected retval to be good");
  375. ck_assert_msg(myNonce.length == i, "Expected nonce length to be %i but was %i", i, myNonce.length);
  376. ck_assert_msg(fCalled.generateNonce, "Expected generateNonce to have been called");
  377. }
  378. UA_ByteString_deleteMembers(&myNonce);
  379. }
  380. END_TEST
  381. START_TEST(SecureChannel_generateNonce_invalidParameters)
  382. {
  383. UA_ByteString myNonce;
  384. UA_ByteString_init(&myNonce);
  385. UA_StatusCode retval = UA_SecureChannel_generateNonce(NULL, 42, NULL);
  386. ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected failure");
  387. retval = UA_SecureChannel_generateNonce(NULL, 42, &myNonce);
  388. ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected failure");
  389. retval = UA_SecureChannel_generateNonce(&testChannel, 42, NULL);
  390. ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected failure");
  391. }
  392. END_TEST
  393. static Suite *
  394. testSuite_SecureChannel(void) {
  395. Suite *s = suite_create("SecureChannel");
  396. TCase *tc_initAndDelete = tcase_create("Initialize and delete Securechannel");
  397. tcase_add_checked_fixture(tc_initAndDelete, setup_funcs_called, teardown_funcs_called);
  398. tcase_add_checked_fixture(tc_initAndDelete, setup_key_sizes, teardown_key_sizes);
  399. tcase_add_test(tc_initAndDelete, SecureChannel_initAndDelete);
  400. tcase_add_test(tc_initAndDelete, SecureChannel_initAndDelete_invalidParameters);
  401. suite_add_tcase(s, tc_initAndDelete);
  402. TCase *tc_generateNewKeys = tcase_create("Test generateNewKeys function");
  403. tcase_add_checked_fixture(tc_generateNewKeys, setup_funcs_called, teardown_funcs_called);
  404. tcase_add_checked_fixture(tc_generateNewKeys, setup_key_sizes, teardown_key_sizes);
  405. tcase_add_checked_fixture(tc_generateNewKeys, setup_secureChannel, teardown_secureChannel);
  406. tcase_add_test(tc_generateNewKeys, SecureChannel_generateNewKeys);
  407. suite_add_tcase(s, tc_generateNewKeys);
  408. TCase *tc_revolveTokens = tcase_create("Test revolveTokens function");
  409. tcase_add_checked_fixture(tc_revolveTokens, setup_funcs_called, teardown_funcs_called);
  410. tcase_add_checked_fixture(tc_revolveTokens, setup_key_sizes, teardown_key_sizes);
  411. tcase_add_checked_fixture(tc_revolveTokens, setup_secureChannel, teardown_secureChannel);
  412. tcase_add_test(tc_revolveTokens, SecureChannel_revolveTokens);
  413. suite_add_tcase(s, tc_revolveTokens);
  414. TCase *tc_sendAsymmetricOPNMessage = tcase_create("Test sendAsymmetricOPNMessage function");
  415. tcase_add_checked_fixture(tc_sendAsymmetricOPNMessage, setup_funcs_called, teardown_funcs_called);
  416. tcase_add_checked_fixture(tc_sendAsymmetricOPNMessage, setup_key_sizes, teardown_key_sizes);
  417. tcase_add_checked_fixture(tc_sendAsymmetricOPNMessage, setup_secureChannel, teardown_secureChannel);
  418. tcase_add_test(tc_sendAsymmetricOPNMessage, SecureChannel_sendAsymmetricOPNMessage_withoutConnection);
  419. tcase_add_test(tc_sendAsymmetricOPNMessage, SecureChannel_sendAsymmetricOPNMessage_invalidParameters);
  420. tcase_add_test(tc_sendAsymmetricOPNMessage, SecureChannel_sendAsymmetricOPNMessage_SecurityModeInvalid);
  421. tcase_add_test(tc_sendAsymmetricOPNMessage, SecureChannel_sendAsymmetricOPNMessage_SecurityModeNone);
  422. tcase_add_test(tc_sendAsymmetricOPNMessage, SecureChannel_sendAsymmetricOPNMessage_SecurityModeSign);
  423. tcase_add_test(tc_sendAsymmetricOPNMessage, SecureChannel_sendAsymmetricOPNMessage_SecurityModeSignAndEncrypt);
  424. tcase_add_test(tc_sendAsymmetricOPNMessage, SecureChannel_sendAsymmetricOPNMessage_sentDataIsValid);
  425. tcase_add_test(tc_sendAsymmetricOPNMessage,
  426. Securechannel_sendAsymmetricOPNMessage_extraPaddingPresentWhenKeyLargerThan2048Bits);
  427. suite_add_tcase(s, tc_sendAsymmetricOPNMessage);
  428. TCase *tc_generateNonce = tcase_create("Test generateNonce function");
  429. tcase_add_checked_fixture(tc_generateNonce, setup_funcs_called, teardown_funcs_called);
  430. tcase_add_checked_fixture(tc_generateNonce, setup_key_sizes, teardown_key_sizes);
  431. tcase_add_checked_fixture(tc_generateNonce, setup_secureChannel, teardown_secureChannel);
  432. tcase_add_test(tc_generateNonce, SecureChannel_generateNonce);
  433. tcase_add_test(tc_generateNonce, SecureChannel_generateNonce_invalidParameters);
  434. suite_add_tcase(s, tc_generateNonce);
  435. return s;
  436. }
  437. int
  438. main(void) {
  439. Suite *s = testSuite_SecureChannel();
  440. SRunner *sr = srunner_create(s);
  441. srunner_set_fork_status(sr, CK_NOFORK);
  442. srunner_run_all(sr, CK_NORMAL);
  443. int number_failed = srunner_ntests_failed(sr);
  444. srunner_free(sr);
  445. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  446. }