ua_config_default.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  3. *
  4. * Copyright 2017 (c) Julius Pfrommer, Fraunhofer IOSB
  5. * Copyright 2017 (c) Julian Grothoff
  6. * Copyright 2017-2018 (c) Mark Giraud, Fraunhofer IOSB
  7. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  8. * Copyright 2017 (c) Thomas Stalder, Blue Time Concept SA
  9. * Copyright 2018 (c) Daniel Feist, Precitec GmbH & Co. KG
  10. */
  11. #include "ua_plugin_securitypolicy.h"
  12. #include "ua_config_default.h"
  13. #include "ua_log_stdout.h"
  14. #include "ua_network_tcp.h"
  15. #include "ua_accesscontrol_default.h"
  16. #include "ua_pki_certificate.h"
  17. #include "ua_nodestore_default.h"
  18. #include "ua_types_generated.h"
  19. #include "ua_securitypolicy_none.h"
  20. #ifdef UA_ENABLE_ENCRYPTION
  21. #include "ua_securitypolicy_basic128rsa15.h"
  22. #include "ua_securitypolicy_basic256sha256.h"
  23. #endif
  24. #include "ua_types.h"
  25. #include "ua_types_generated_handling.h"
  26. #include "ua_client_subscriptions.h"
  27. /* Struct initialization works across ANSI C/C99/C++ if it is done when the
  28. * variable is first declared. Assigning values to existing structs is
  29. * heterogeneous across the three. */
  30. static UA_INLINE UA_UInt32Range
  31. UA_UINT32RANGE(UA_UInt32 min, UA_UInt32 max) {
  32. UA_UInt32Range range = {min, max};
  33. return range;
  34. }
  35. static UA_INLINE UA_DurationRange
  36. UA_DURATIONRANGE(UA_Duration min, UA_Duration max) {
  37. UA_DurationRange range = {min, max};
  38. return range;
  39. }
  40. /*******************************/
  41. /* Default Connection Settings */
  42. /*******************************/
  43. const UA_ConnectionConfig UA_ConnectionConfig_default = {
  44. 0, /* .protocolVersion */
  45. 65535, /* .sendBufferSize, 64k per chunk */
  46. 65535, /* .recvBufferSize, 64k per chunk */
  47. 0, /* .maxMessageSize, 0 -> unlimited */
  48. 0 /* .maxChunkCount, 0 -> unlimited */
  49. };
  50. /***************************/
  51. /* Default Server Settings */
  52. /***************************/
  53. #define MANUFACTURER_NAME "open62541"
  54. #define PRODUCT_NAME "open62541 OPC UA Server"
  55. #define PRODUCT_URI "http://open62541.org"
  56. #define APPLICATION_NAME "open62541-based OPC UA Application"
  57. #define APPLICATION_URI "urn:unconfigured:application"
  58. #define STRINGIFY(arg) #arg
  59. #define VERSION(MAJOR, MINOR, PATCH, LABEL) \
  60. STRINGIFY(MAJOR) "." STRINGIFY(MINOR) "." STRINGIFY(PATCH) LABEL
  61. static UA_StatusCode
  62. createSecurityPolicyNoneEndpoint(UA_ServerConfig *conf, UA_Endpoint *endpoint,
  63. const UA_ByteString localCertificate) {
  64. UA_EndpointDescription_init(&endpoint->endpointDescription);
  65. UA_SecurityPolicy_None(&endpoint->securityPolicy, NULL, localCertificate, conf->logger);
  66. endpoint->endpointDescription.securityMode = UA_MESSAGESECURITYMODE_NONE;
  67. endpoint->endpointDescription.securityPolicyUri =
  68. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#None");
  69. endpoint->endpointDescription.transportProfileUri =
  70. UA_STRING_ALLOC("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary");
  71. /* Enable all login mechanisms from the access control plugin */
  72. UA_StatusCode retval = UA_Array_copy(conf->accessControl.userTokenPolicies,
  73. conf->accessControl.userTokenPoliciesSize,
  74. (void **)&endpoint->endpointDescription.userIdentityTokens,
  75. &UA_TYPES[UA_TYPES_USERTOKENPOLICY]);
  76. if(retval != UA_STATUSCODE_GOOD)
  77. return retval;
  78. endpoint->endpointDescription.userIdentityTokensSize =
  79. conf->accessControl.userTokenPoliciesSize;
  80. UA_String_copy(&localCertificate, &endpoint->endpointDescription.serverCertificate);
  81. UA_ApplicationDescription_copy(&conf->applicationDescription,
  82. &endpoint->endpointDescription.server);
  83. return UA_STATUSCODE_GOOD;
  84. }
  85. void
  86. UA_ServerConfig_set_customHostname(UA_ServerConfig *config, const UA_String customHostname) {
  87. if(!config)
  88. return;
  89. UA_String_deleteMembers(&config->customHostname);
  90. UA_String_copy(&customHostname, &config->customHostname);
  91. }
  92. #ifdef UA_ENABLE_ENCRYPTION
  93. static UA_StatusCode
  94. createSecurityPolicyBasic128Rsa15Endpoint(UA_ServerConfig *const conf,
  95. UA_Endpoint *endpoint,
  96. UA_MessageSecurityMode securityMode,
  97. const UA_ByteString localCertificate,
  98. const UA_ByteString localPrivateKey) {
  99. UA_EndpointDescription_init(&endpoint->endpointDescription);
  100. UA_StatusCode retval =
  101. UA_SecurityPolicy_Basic128Rsa15(&endpoint->securityPolicy, &conf->certificateVerification,
  102. localCertificate, localPrivateKey, conf->logger);
  103. if(retval != UA_STATUSCODE_GOOD) {
  104. endpoint->securityPolicy.deleteMembers(&endpoint->securityPolicy);
  105. return retval;
  106. }
  107. endpoint->endpointDescription.securityMode = securityMode;
  108. endpoint->endpointDescription.securityPolicyUri =
  109. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15");
  110. endpoint->endpointDescription.transportProfileUri =
  111. UA_STRING_ALLOC("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary");
  112. /* Enable all login mechanisms from the access control plugin */
  113. retval = UA_Array_copy(conf->accessControl.userTokenPolicies,
  114. conf->accessControl.userTokenPoliciesSize,
  115. (void **)&endpoint->endpointDescription.userIdentityTokens,
  116. &UA_TYPES[UA_TYPES_USERTOKENPOLICY]);
  117. if(retval != UA_STATUSCODE_GOOD)
  118. return retval;
  119. endpoint->endpointDescription.userIdentityTokensSize =
  120. conf->accessControl.userTokenPoliciesSize;
  121. UA_String_copy(&localCertificate, &endpoint->endpointDescription.serverCertificate);
  122. UA_ApplicationDescription_copy(&conf->applicationDescription,
  123. &endpoint->endpointDescription.server);
  124. return UA_STATUSCODE_GOOD;
  125. }
  126. static UA_StatusCode
  127. createSecurityPolicyBasic256Sha256Endpoint(UA_ServerConfig *const conf,
  128. UA_Endpoint *endpoint,
  129. UA_MessageSecurityMode securityMode,
  130. const UA_ByteString localCertificate,
  131. const UA_ByteString localPrivateKey) {
  132. UA_EndpointDescription_init(&endpoint->endpointDescription);
  133. UA_StatusCode retval =
  134. UA_SecurityPolicy_Basic256Sha256(&endpoint->securityPolicy, &conf->certificateVerification, localCertificate,
  135. localPrivateKey, conf->logger);
  136. if(retval != UA_STATUSCODE_GOOD) {
  137. endpoint->securityPolicy.deleteMembers(&endpoint->securityPolicy);
  138. return retval;
  139. }
  140. endpoint->endpointDescription.securityMode = securityMode;
  141. endpoint->endpointDescription.securityPolicyUri =
  142. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256");
  143. endpoint->endpointDescription.transportProfileUri =
  144. UA_STRING_ALLOC("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary");
  145. /* Enable all login mechanisms from the access control plugin */
  146. retval = UA_Array_copy(conf->accessControl.userTokenPolicies,
  147. conf->accessControl.userTokenPoliciesSize,
  148. (void **)&endpoint->endpointDescription.userIdentityTokens,
  149. &UA_TYPES[UA_TYPES_USERTOKENPOLICY]);
  150. if(retval != UA_STATUSCODE_GOOD)
  151. return retval;
  152. endpoint->endpointDescription.userIdentityTokensSize =
  153. conf->accessControl.userTokenPoliciesSize;
  154. UA_String_copy(&localCertificate, &endpoint->endpointDescription.serverCertificate);
  155. UA_ApplicationDescription_copy(&conf->applicationDescription,
  156. &endpoint->endpointDescription.server);
  157. UA_String_copy(&localCertificate, &endpoint->endpointDescription.serverCertificate);
  158. UA_ApplicationDescription_copy(&conf->applicationDescription,
  159. &endpoint->endpointDescription.server);
  160. return UA_STATUSCODE_GOOD;
  161. }
  162. #endif
  163. const size_t usernamePasswordsSize = 2;
  164. UA_UsernamePasswordLogin usernamePasswords[2] = {
  165. {UA_STRING_STATIC("user1"), UA_STRING_STATIC("password")},
  166. {UA_STRING_STATIC("user2"), UA_STRING_STATIC("password1")}};
  167. static UA_ServerConfig *
  168. createDefaultConfig(void) {
  169. UA_ServerConfig *conf = (UA_ServerConfig *)UA_malloc(sizeof(UA_ServerConfig));
  170. if(!conf)
  171. return NULL;
  172. /* Zero out.. All members have a valid initial value */
  173. memset(conf, 0, sizeof(UA_ServerConfig));
  174. /* --> Start setting the default static config <-- */
  175. conf->nThreads = 1;
  176. conf->logger = UA_Log_Stdout;
  177. /* Server Description */
  178. conf->buildInfo.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  179. conf->buildInfo.manufacturerName = UA_STRING_ALLOC(MANUFACTURER_NAME);
  180. conf->buildInfo.productName = UA_STRING_ALLOC(PRODUCT_NAME);
  181. conf->buildInfo.softwareVersion =
  182. UA_STRING_ALLOC(VERSION(UA_OPEN62541_VER_MAJOR, UA_OPEN62541_VER_MINOR,
  183. UA_OPEN62541_VER_PATCH, UA_OPEN62541_VER_LABEL));
  184. conf->buildInfo.buildNumber = UA_STRING_ALLOC(__DATE__
  185. " "
  186. __TIME__);
  187. conf->buildInfo.buildDate = 0;
  188. conf->applicationDescription.applicationUri = UA_STRING_ALLOC(APPLICATION_URI);
  189. conf->applicationDescription.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  190. conf->applicationDescription.applicationName =
  191. UA_LOCALIZEDTEXT_ALLOC("en", APPLICATION_NAME);
  192. conf->applicationDescription.applicationType = UA_APPLICATIONTYPE_SERVER;
  193. /* conf->applicationDescription.gatewayServerUri = UA_STRING_NULL; */
  194. /* conf->applicationDescription.discoveryProfileUri = UA_STRING_NULL; */
  195. /* conf->applicationDescription.discoveryUrlsSize = 0; */
  196. /* conf->applicationDescription.discoveryUrls = NULL; */
  197. #ifdef UA_ENABLE_DISCOVERY
  198. /* conf->mdnsServerName = UA_STRING_NULL; */
  199. /* conf->serverCapabilitiesSize = 0; */
  200. /* conf->serverCapabilities = NULL; */
  201. #endif
  202. /* Custom DataTypes */
  203. /* conf->customDataTypesSize = 0; */
  204. /* conf->customDataTypes = NULL; */
  205. /* Networking */
  206. /* conf->networkLayersSize = 0; */
  207. /* conf->networkLayers = NULL; */
  208. /* conf->customHostname = UA_STRING_NULL; */
  209. /* Endpoints */
  210. /* conf->endpoints = {0, NULL}; */
  211. /* Certificate Verification that accepts every certificate. Can be
  212. * overwritten when the policy is specialized. */
  213. UA_CertificateVerification_AcceptAll(&conf->certificateVerification);
  214. /* Global Node Lifecycle */
  215. conf->nodeLifecycle.constructor = NULL;
  216. conf->nodeLifecycle.destructor = NULL;
  217. /* Access Control. Anonymous Login only. */
  218. conf->accessControl = UA_AccessControl_default(true, usernamePasswordsSize, usernamePasswords);
  219. /* Limits for SecureChannels */
  220. conf->maxSecureChannels = 40;
  221. conf->maxSecurityTokenLifetime = 10 * 60 * 1000; /* 10 minutes */
  222. /* Limits for Sessions */
  223. conf->maxSessions = 100;
  224. conf->maxSessionTimeout = 60.0 * 60.0 * 1000.0; /* 1h */
  225. /* Limits for Subscriptions */
  226. conf->publishingIntervalLimits = UA_DURATIONRANGE(100.0, 3600.0 * 1000.0);
  227. conf->lifeTimeCountLimits = UA_UINT32RANGE(3, 15000);
  228. conf->keepAliveCountLimits = UA_UINT32RANGE(1, 100);
  229. conf->maxNotificationsPerPublish = 1000;
  230. conf->maxRetransmissionQueueSize = 0; /* unlimited */
  231. /* Limits for MonitoredItems */
  232. conf->samplingIntervalLimits = UA_DURATIONRANGE(50.0, 24.0 * 3600.0 * 1000.0);
  233. conf->queueSizeLimits = UA_UINT32RANGE(1, 100);
  234. #ifdef UA_ENABLE_DISCOVERY
  235. conf->discoveryCleanupTimeout = 60 * 60;
  236. #endif
  237. /* --> Finish setting the default static config <-- */
  238. return conf;
  239. }
  240. static UA_StatusCode
  241. addDefaultNetworkLayers(UA_ServerConfig *conf, UA_UInt16 portNumber) {
  242. /* Add a network layer */
  243. conf->networkLayers = (UA_ServerNetworkLayer *)
  244. UA_malloc(sizeof(UA_ServerNetworkLayer));
  245. if(!conf->networkLayers)
  246. return UA_STATUSCODE_BADOUTOFMEMORY;
  247. conf->networkLayers[0] =
  248. UA_ServerNetworkLayerTCP(UA_ConnectionConfig_default, portNumber, conf->logger);
  249. conf->networkLayersSize = 1;
  250. return UA_STATUSCODE_GOOD;
  251. }
  252. UA_ServerConfig *
  253. UA_ServerConfig_new_minimal(UA_UInt16 portNumber,
  254. const UA_ByteString *certificate) {
  255. UA_ServerConfig *conf = createDefaultConfig();
  256. UA_StatusCode retval = UA_Nodestore_default_new(&conf->nodestore);
  257. if(retval != UA_STATUSCODE_GOOD) {
  258. UA_ServerConfig_delete(conf);
  259. return NULL;
  260. }
  261. if(addDefaultNetworkLayers(conf, portNumber) != UA_STATUSCODE_GOOD) {
  262. UA_ServerConfig_delete(conf);
  263. return NULL;
  264. }
  265. /* Allocate the endpoint */
  266. conf->endpointsSize = 1;
  267. conf->endpoints = (UA_Endpoint *)UA_malloc(sizeof(UA_Endpoint));
  268. if(!conf->endpoints) {
  269. UA_ServerConfig_delete(conf);
  270. return NULL;
  271. }
  272. /* Populate the endpoint */
  273. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  274. if(certificate)
  275. localCertificate = *certificate;
  276. retval =
  277. createSecurityPolicyNoneEndpoint(conf, &conf->endpoints[0], localCertificate);
  278. if(retval != UA_STATUSCODE_GOOD) {
  279. UA_ServerConfig_delete(conf);
  280. return NULL;
  281. }
  282. return conf;
  283. }
  284. #ifdef UA_ENABLE_ENCRYPTION
  285. UA_ServerConfig *
  286. UA_ServerConfig_new_basic128rsa15(UA_UInt16 portNumber,
  287. const UA_ByteString *certificate,
  288. const UA_ByteString *privateKey,
  289. const UA_ByteString *trustList,
  290. size_t trustListSize,
  291. const UA_ByteString *revocationList,
  292. size_t revocationListSize) {
  293. UA_ServerConfig *conf = createDefaultConfig();
  294. UA_StatusCode retval = UA_CertificateVerification_Trustlist(&conf->certificateVerification,
  295. trustList, trustListSize,
  296. revocationList, revocationListSize);
  297. if(retval != UA_STATUSCODE_GOOD) {
  298. UA_ServerConfig_delete(conf);
  299. return NULL;
  300. }
  301. retval = UA_Nodestore_default_new(&conf->nodestore);
  302. if(retval != UA_STATUSCODE_GOOD) {
  303. UA_ServerConfig_delete(conf);
  304. return NULL;
  305. }
  306. if(addDefaultNetworkLayers(conf, portNumber) != UA_STATUSCODE_GOOD) {
  307. UA_ServerConfig_delete(conf);
  308. return NULL;
  309. }
  310. if(trustListSize == 0)
  311. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  312. "No CA trust-list provided. Any remote certificate will be accepted.");
  313. /* Allocate the endpoints */
  314. conf->endpointsSize = 0;
  315. conf->endpoints = (UA_Endpoint *)UA_malloc(sizeof(UA_Endpoint) * 3);
  316. if(!conf->endpoints) {
  317. UA_ServerConfig_delete(conf);
  318. return NULL;
  319. }
  320. /* Populate the endpoints */
  321. ++conf->endpointsSize;
  322. retval = createSecurityPolicyNoneEndpoint(conf, &conf->endpoints[0], *certificate);
  323. if(retval != UA_STATUSCODE_GOOD) {
  324. UA_ServerConfig_delete(conf);
  325. return NULL;
  326. }
  327. ++conf->endpointsSize;
  328. retval = createSecurityPolicyBasic128Rsa15Endpoint(conf, &conf->endpoints[1],
  329. UA_MESSAGESECURITYMODE_SIGN, *certificate,
  330. *privateKey);
  331. if(retval != UA_STATUSCODE_GOOD) {
  332. UA_ServerConfig_delete(conf);
  333. return NULL;
  334. }
  335. ++conf->endpointsSize;
  336. retval = createSecurityPolicyBasic128Rsa15Endpoint(conf, &conf->endpoints[2],
  337. UA_MESSAGESECURITYMODE_SIGNANDENCRYPT, *certificate,
  338. *privateKey);
  339. if(retval != UA_STATUSCODE_GOOD) {
  340. UA_ServerConfig_delete(conf);
  341. return NULL;
  342. }
  343. return conf;
  344. }
  345. UA_ServerConfig *
  346. UA_ServerConfig_new_basic256sha256(UA_UInt16 portNumber,
  347. const UA_ByteString *certificate,
  348. const UA_ByteString *privateKey,
  349. const UA_ByteString *trustList,
  350. size_t trustListSize,
  351. const UA_ByteString *revocationList,
  352. size_t revocationListSize) {
  353. UA_ServerConfig *conf = createDefaultConfig();
  354. UA_StatusCode retval = UA_CertificateVerification_Trustlist(&conf->certificateVerification,
  355. trustList, trustListSize,
  356. revocationList, revocationListSize);
  357. if(retval != UA_STATUSCODE_GOOD) {
  358. UA_ServerConfig_delete(conf);
  359. return NULL;
  360. }
  361. retval = UA_Nodestore_default_new(&conf->nodestore);
  362. if(retval != UA_STATUSCODE_GOOD) {
  363. UA_ServerConfig_delete(conf);
  364. return NULL;
  365. }
  366. if(addDefaultNetworkLayers(conf, portNumber) != UA_STATUSCODE_GOOD) {
  367. UA_ServerConfig_delete(conf);
  368. return NULL;
  369. }
  370. if(trustListSize == 0)
  371. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  372. "No CA trust-list provided. Any remote certificate will be accepted.");
  373. /* Allocate the endpoints */
  374. conf->endpointsSize = 0;
  375. conf->endpoints = (UA_Endpoint *)UA_malloc(sizeof(UA_Endpoint) * 3);
  376. if(!conf->endpoints) {
  377. UA_ServerConfig_delete(conf);
  378. return NULL;
  379. }
  380. /* Populate the endpoints */
  381. ++conf->endpointsSize;
  382. retval = createSecurityPolicyNoneEndpoint(conf, &conf->endpoints[0], *certificate);
  383. if(retval != UA_STATUSCODE_GOOD) {
  384. UA_ServerConfig_delete(conf);
  385. return NULL;
  386. }
  387. ++conf->endpointsSize;
  388. retval = createSecurityPolicyBasic256Sha256Endpoint(conf, &conf->endpoints[1],
  389. UA_MESSAGESECURITYMODE_SIGN, *certificate,
  390. *privateKey);
  391. if(retval != UA_STATUSCODE_GOOD) {
  392. UA_ServerConfig_delete(conf);
  393. return NULL;
  394. }
  395. ++conf->endpointsSize;
  396. retval = createSecurityPolicyBasic256Sha256Endpoint(conf, &conf->endpoints[2],
  397. UA_MESSAGESECURITYMODE_SIGNANDENCRYPT, *certificate,
  398. *privateKey);
  399. if(retval != UA_STATUSCODE_GOOD) {
  400. UA_ServerConfig_delete(conf);
  401. return NULL;
  402. }
  403. return conf;
  404. }
  405. UA_ServerConfig *
  406. UA_ServerConfig_new_allSecurityPolicies(UA_UInt16 portNumber,
  407. const UA_ByteString *certificate,
  408. const UA_ByteString *privateKey,
  409. const UA_ByteString *trustList,
  410. size_t trustListSize,
  411. const UA_ByteString *revocationList,
  412. size_t revocationListSize) {
  413. UA_ServerConfig *conf = createDefaultConfig();
  414. UA_StatusCode retval = UA_CertificateVerification_Trustlist(&conf->certificateVerification,
  415. trustList, trustListSize,
  416. revocationList, revocationListSize);
  417. if(retval != UA_STATUSCODE_GOOD) {
  418. UA_ServerConfig_delete(conf);
  419. return NULL;
  420. }
  421. retval = UA_Nodestore_default_new(&conf->nodestore);
  422. if(retval != UA_STATUSCODE_GOOD) {
  423. UA_ServerConfig_delete(conf);
  424. return NULL;
  425. }
  426. if(addDefaultNetworkLayers(conf, portNumber) != UA_STATUSCODE_GOOD) {
  427. UA_ServerConfig_delete(conf);
  428. return NULL;
  429. }
  430. if(trustListSize == 0)
  431. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  432. "No CA trust-list provided. Any remote certificate will be accepted.");
  433. /* Allocate the endpoints */
  434. conf->endpointsSize = 0;
  435. conf->endpoints = (UA_Endpoint *)UA_malloc(sizeof(UA_Endpoint) * 5);
  436. if(!conf->endpoints) {
  437. UA_ServerConfig_delete(conf);
  438. return NULL;
  439. }
  440. /* Populate the endpoints */
  441. retval = createSecurityPolicyNoneEndpoint(conf, &conf->endpoints[conf->endpointsSize], *certificate);
  442. ++conf->endpointsSize;
  443. if(retval != UA_STATUSCODE_GOOD) {
  444. UA_ServerConfig_delete(conf);
  445. return NULL;
  446. }
  447. retval = createSecurityPolicyBasic128Rsa15Endpoint(conf, &conf->endpoints[conf->endpointsSize],
  448. UA_MESSAGESECURITYMODE_SIGN, *certificate,
  449. *privateKey);
  450. ++conf->endpointsSize;
  451. if(retval != UA_STATUSCODE_GOOD) {
  452. UA_ServerConfig_delete(conf);
  453. return NULL;
  454. }
  455. retval = createSecurityPolicyBasic128Rsa15Endpoint(conf, &conf->endpoints[conf->endpointsSize],
  456. UA_MESSAGESECURITYMODE_SIGNANDENCRYPT, *certificate,
  457. *privateKey);
  458. ++conf->endpointsSize;
  459. if(retval != UA_STATUSCODE_GOOD) {
  460. UA_ServerConfig_delete(conf);
  461. return NULL;
  462. }
  463. retval = createSecurityPolicyBasic256Sha256Endpoint(conf, &conf->endpoints[conf->endpointsSize],
  464. UA_MESSAGESECURITYMODE_SIGN, *certificate,
  465. *privateKey);
  466. ++conf->endpointsSize;
  467. if(retval != UA_STATUSCODE_GOOD) {
  468. UA_ServerConfig_delete(conf);
  469. return NULL;
  470. }
  471. retval = createSecurityPolicyBasic256Sha256Endpoint(conf, &conf->endpoints[conf->endpointsSize],
  472. UA_MESSAGESECURITYMODE_SIGNANDENCRYPT, *certificate,
  473. *privateKey);
  474. ++conf->endpointsSize;
  475. if(retval != UA_STATUSCODE_GOOD) {
  476. UA_ServerConfig_delete(conf);
  477. return NULL;
  478. }
  479. return conf;
  480. }
  481. #endif
  482. void
  483. UA_ServerConfig_delete(UA_ServerConfig *config) {
  484. if(!config)
  485. return;
  486. /* Server Description */
  487. UA_BuildInfo_deleteMembers(&config->buildInfo);
  488. UA_ApplicationDescription_deleteMembers(&config->applicationDescription);
  489. #ifdef UA_ENABLE_DISCOVERY
  490. UA_String_deleteMembers(&config->mdnsServerName);
  491. UA_Array_delete(config->serverCapabilities, config->serverCapabilitiesSize,
  492. &UA_TYPES[UA_TYPES_STRING]);
  493. config->serverCapabilities = NULL;
  494. config->serverCapabilitiesSize = 0;
  495. #endif
  496. /* Nodestore */
  497. if(config->nodestore.deleteNodestore)
  498. config->nodestore.deleteNodestore(config->nodestore.context);
  499. /* Custom DataTypes */
  500. for(size_t i = 0; i < config->customDataTypesSize; ++i)
  501. UA_free(config->customDataTypes[i].members);
  502. UA_free(config->customDataTypes);
  503. config->customDataTypes = NULL;
  504. config->customDataTypesSize = 0;
  505. /* Networking */
  506. for(size_t i = 0; i < config->networkLayersSize; ++i)
  507. config->networkLayers[i].deleteMembers(&config->networkLayers[i]);
  508. UA_free(config->networkLayers);
  509. config->networkLayers = NULL;
  510. config->networkLayersSize = 0;
  511. UA_String_deleteMembers(&config->customHostname);
  512. config->customHostname = UA_STRING_NULL;
  513. for(size_t i = 0; i < config->endpointsSize; ++i) {
  514. UA_SecurityPolicy *policy = &config->endpoints[i].securityPolicy;
  515. policy->deleteMembers(policy);
  516. UA_EndpointDescription_deleteMembers(&config->endpoints[i].endpointDescription);
  517. }
  518. UA_free(config->endpoints);
  519. config->endpoints = NULL;
  520. config->endpointsSize = 0;
  521. /* Certificate Validation */
  522. config->certificateVerification.deleteMembers(&config->certificateVerification);
  523. /* Access Control */
  524. config->accessControl.deleteMembers(&config->accessControl);
  525. UA_free(config);
  526. }
  527. /***************************/
  528. /* Default Client Settings */
  529. /***************************/
  530. const UA_ClientConfig UA_ClientConfig_default = {
  531. 5000, /* .timeout, 5 seconds */
  532. 10 * 60 * 1000, /* .secureChannelLifeTime, 10 minutes */
  533. UA_Log_Stdout, /* .logger */
  534. { /* .localConnectionConfig */
  535. 0, /* .protocolVersion */
  536. 65535, /* .sendBufferSize, 64k per chunk */
  537. 65535, /* .recvBufferSize, 64k per chunk */
  538. 0, /* .maxMessageSize, 0 -> unlimited */
  539. 0 /* .maxChunkCount, 0 -> unlimited */
  540. },
  541. UA_ClientConnectionTCP, /* .connectionFunc */
  542. 0, /* .customDataTypesSize */
  543. NULL, /*.customDataTypes */
  544. NULL, /*.stateCallback */
  545. NULL, /*.subscriptionInactivityCallback */
  546. NULL, /*.clientContext */
  547. 10 /* .outStandingPublishRequests */
  548. };