ua_config_default.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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) Fraunhofer IOSB (Author: Julius Pfrommer)
  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. * Copyright 2018 (c) Fabian Arndt, Root-Core
  11. * Copyright 2019 (c) Kalycito Infotech Private Limited
  12. */
  13. #include <open62541/client_config_default.h>
  14. #include <open62541/network_tcp.h>
  15. #include <open62541/plugin/accesscontrol_default.h>
  16. #include <open62541/plugin/log_stdout.h>
  17. #include <open62541/plugin/pki_default.h>
  18. #include <open62541/plugin/securitypolicy_default.h>
  19. #include <open62541/server_config_default.h>
  20. /* Struct initialization works across ANSI C/C99/C++ if it is done when the
  21. * variable is first declared. Assigning values to existing structs is
  22. * heterogeneous across the three. */
  23. static UA_INLINE UA_UInt32Range
  24. UA_UINT32RANGE(UA_UInt32 min, UA_UInt32 max) {
  25. UA_UInt32Range range = {min, max};
  26. return range;
  27. }
  28. static UA_INLINE UA_DurationRange
  29. UA_DURATIONRANGE(UA_Duration min, UA_Duration max) {
  30. UA_DurationRange range = {min, max};
  31. return range;
  32. }
  33. /*******************************/
  34. /* Default Connection Settings */
  35. /*******************************/
  36. const UA_ConnectionConfig UA_ConnectionConfig_default = {
  37. 0, /* .protocolVersion */
  38. 65535, /* .sendBufferSize, 64k per chunk */
  39. 65535, /* .recvBufferSize, 64k per chunk */
  40. 0, /* .maxMessageSize, 0 -> unlimited */
  41. 0 /* .maxChunkCount, 0 -> unlimited */
  42. };
  43. /***************************/
  44. /* Default Server Settings */
  45. /***************************/
  46. #define MANUFACTURER_NAME "open62541"
  47. #define PRODUCT_NAME "open62541 OPC UA Server"
  48. #define PRODUCT_URI "http://open62541.org"
  49. #define APPLICATION_NAME "open62541-based OPC UA Application"
  50. #define APPLICATION_URI "urn:unconfigured:application"
  51. #define APPLICATION_URI_SERVER "urn:open62541.server.application"
  52. #define STRINGIFY(arg) #arg
  53. #define VERSION(MAJOR, MINOR, PATCH, LABEL) \
  54. STRINGIFY(MAJOR) "." STRINGIFY(MINOR) "." STRINGIFY(PATCH) LABEL
  55. static UA_StatusCode
  56. createEndpoint(UA_ServerConfig *conf, UA_EndpointDescription *endpoint,
  57. const UA_SecurityPolicy *securityPolicy,
  58. UA_MessageSecurityMode securityMode) {
  59. UA_EndpointDescription_init(endpoint);
  60. endpoint->securityMode = securityMode;
  61. UA_String_copy(&securityPolicy->policyUri, &endpoint->securityPolicyUri);
  62. endpoint->transportProfileUri =
  63. UA_STRING_ALLOC("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary");
  64. /* Add security level value for the corresponding message security mode */
  65. endpoint->securityLevel = (UA_Byte) securityMode;
  66. /* Enable all login mechanisms from the access control plugin */
  67. UA_StatusCode retval = UA_Array_copy(conf->accessControl.userTokenPolicies,
  68. conf->accessControl.userTokenPoliciesSize,
  69. (void **)&endpoint->userIdentityTokens,
  70. &UA_TYPES[UA_TYPES_USERTOKENPOLICY]);
  71. if(retval != UA_STATUSCODE_GOOD)
  72. return retval;
  73. endpoint->userIdentityTokensSize = conf->accessControl.userTokenPoliciesSize;
  74. UA_String_copy(&securityPolicy->localCertificate, &endpoint->serverCertificate);
  75. UA_ApplicationDescription_copy(&conf->applicationDescription, &endpoint->server);
  76. return UA_STATUSCODE_GOOD;
  77. }
  78. static const size_t usernamePasswordsSize = 2;
  79. static UA_UsernamePasswordLogin usernamePasswords[2] = {
  80. {UA_STRING_STATIC("user1"), UA_STRING_STATIC("password")},
  81. {UA_STRING_STATIC("user2"), UA_STRING_STATIC("password1")}};
  82. static UA_StatusCode
  83. setDefaultConfig(UA_ServerConfig *conf) {
  84. if (!conf)
  85. return UA_STATUSCODE_BADINVALIDARGUMENT;
  86. /* Zero out.. All members have a valid initial value */
  87. UA_ServerConfig_clean(conf);
  88. memset(conf, 0, sizeof(UA_ServerConfig));
  89. /* --> Start setting the default static config <-- */
  90. conf->nThreads = 1;
  91. conf->logger = UA_Log_Stdout_;
  92. conf->shutdownDelay = 0.0;
  93. /* Server Description */
  94. conf->buildInfo.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  95. conf->buildInfo.manufacturerName = UA_STRING_ALLOC(MANUFACTURER_NAME);
  96. conf->buildInfo.productName = UA_STRING_ALLOC(PRODUCT_NAME);
  97. conf->buildInfo.softwareVersion =
  98. UA_STRING_ALLOC(VERSION(UA_OPEN62541_VER_MAJOR, UA_OPEN62541_VER_MINOR,
  99. UA_OPEN62541_VER_PATCH, UA_OPEN62541_VER_LABEL));
  100. #ifdef UA_PACK_DEBIAN
  101. conf->buildInfo.buildNumber = UA_STRING_ALLOC("deb");
  102. #else
  103. conf->buildInfo.buildNumber = UA_STRING_ALLOC(__DATE__ " " __TIME__);
  104. #endif
  105. conf->buildInfo.buildDate = UA_DateTime_now();
  106. conf->applicationDescription.applicationUri = UA_STRING_ALLOC(APPLICATION_URI_SERVER);
  107. conf->applicationDescription.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  108. conf->applicationDescription.applicationName =
  109. UA_LOCALIZEDTEXT_ALLOC("en", APPLICATION_NAME);
  110. conf->applicationDescription.applicationType = UA_APPLICATIONTYPE_SERVER;
  111. /* conf->applicationDescription.gatewayServerUri = UA_STRING_NULL; */
  112. /* conf->applicationDescription.discoveryProfileUri = UA_STRING_NULL; */
  113. /* conf->applicationDescription.discoveryUrlsSize = 0; */
  114. /* conf->applicationDescription.discoveryUrls = NULL; */
  115. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  116. UA_MdnsDiscoveryConfiguration_init(&conf->discovery.mdns);
  117. conf->discovery.mdnsInterfaceIP = UA_STRING_NULL;
  118. #endif
  119. /* Custom DataTypes */
  120. /* conf->customDataTypesSize = 0; */
  121. /* conf->customDataTypes = NULL; */
  122. /* Networking */
  123. /* conf->networkLayersSize = 0; */
  124. /* conf->networkLayers = NULL; */
  125. /* conf->customHostname = UA_STRING_NULL; */
  126. /* Endpoints */
  127. /* conf->endpoints = {0, NULL}; */
  128. /* Certificate Verification that accepts every certificate. Can be
  129. * overwritten when the policy is specialized. */
  130. UA_CertificateVerification_AcceptAll(&conf->certificateVerification);
  131. /* Global Node Lifecycle */
  132. conf->nodeLifecycle.constructor = NULL;
  133. conf->nodeLifecycle.destructor = NULL;
  134. conf->nodeLifecycle.createOptionalChild = NULL;
  135. conf->nodeLifecycle.generateChildNodeId = NULL;
  136. /* Relax constraints for the InformationModel */
  137. conf->relaxEmptyValueConstraint = true; /* Allow empty values */
  138. /* Limits for SecureChannels */
  139. conf->maxSecureChannels = 40;
  140. conf->maxSecurityTokenLifetime = 10 * 60 * 1000; /* 10 minutes */
  141. /* Limits for Sessions */
  142. conf->maxSessions = 100;
  143. conf->maxSessionTimeout = 60.0 * 60.0 * 1000.0; /* 1h */
  144. /* Limits for Subscriptions */
  145. conf->publishingIntervalLimits = UA_DURATIONRANGE(100.0, 3600.0 * 1000.0);
  146. conf->lifeTimeCountLimits = UA_UINT32RANGE(3, 15000);
  147. conf->keepAliveCountLimits = UA_UINT32RANGE(1, 100);
  148. conf->maxNotificationsPerPublish = 1000;
  149. conf->enableRetransmissionQueue = true;
  150. conf->maxRetransmissionQueueSize = 0; /* unlimited */
  151. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  152. conf->maxEventsPerNode = 0; /* unlimited */
  153. #endif
  154. /* Limits for MonitoredItems */
  155. conf->samplingIntervalLimits = UA_DURATIONRANGE(50.0, 24.0 * 3600.0 * 1000.0);
  156. conf->queueSizeLimits = UA_UINT32RANGE(1, 100);
  157. #ifdef UA_ENABLE_DISCOVERY
  158. conf->discovery.cleanupTimeout = 60 * 60;
  159. #endif
  160. #ifdef UA_ENABLE_HISTORIZING
  161. /* conf->accessHistoryDataCapability = UA_FALSE; */
  162. /* conf->maxReturnDataValues = 0; */
  163. /* conf->accessHistoryEventsCapability = UA_FALSE; */
  164. /* conf->maxReturnEventValues = 0; */
  165. /* conf->insertDataCapability = UA_FALSE; */
  166. /* conf->insertEventCapability = UA_FALSE; */
  167. /* conf->insertAnnotationsCapability = UA_FALSE; */
  168. /* conf->replaceDataCapability = UA_FALSE; */
  169. /* conf->replaceEventCapability = UA_FALSE; */
  170. /* conf->updateDataCapability = UA_FALSE; */
  171. /* conf->updateEventCapability = UA_FALSE; */
  172. /* conf->deleteRawCapability = UA_FALSE; */
  173. /* conf->deleteEventCapability = UA_FALSE; */
  174. /* conf->deleteAtTimeDataCapability = UA_FALSE; */
  175. #endif
  176. /* --> Finish setting the default static config <-- */
  177. return UA_STATUSCODE_GOOD;
  178. }
  179. UA_EXPORT UA_StatusCode
  180. UA_ServerConfig_setBasics(UA_ServerConfig* conf) {
  181. return setDefaultConfig(conf);
  182. }
  183. static UA_StatusCode
  184. addDefaultNetworkLayers(UA_ServerConfig *conf, UA_UInt16 portNumber,
  185. UA_UInt32 sendBufferSize, UA_UInt32 recvBufferSize) {
  186. return UA_ServerConfig_addNetworkLayerTCP(conf, portNumber, sendBufferSize, recvBufferSize);
  187. }
  188. UA_EXPORT UA_StatusCode
  189. UA_ServerConfig_addNetworkLayerTCP(UA_ServerConfig *conf, UA_UInt16 portNumber,
  190. UA_UInt32 sendBufferSize, UA_UInt32 recvBufferSize) {
  191. /* Add a network layer */
  192. UA_ServerNetworkLayer *tmp = (UA_ServerNetworkLayer *)
  193. UA_realloc(conf->networkLayers, sizeof(UA_ServerNetworkLayer) * (1 + conf->networkLayersSize));
  194. if(!tmp)
  195. return UA_STATUSCODE_BADOUTOFMEMORY;
  196. conf->networkLayers = tmp;
  197. UA_ConnectionConfig config = UA_ConnectionConfig_default;
  198. if (sendBufferSize > 0)
  199. config.sendBufferSize = sendBufferSize;
  200. if (recvBufferSize > 0)
  201. config.recvBufferSize = recvBufferSize;
  202. conf->networkLayers[conf->networkLayersSize] =
  203. UA_ServerNetworkLayerTCP(config, portNumber, &conf->logger);
  204. if (!conf->networkLayers[conf->networkLayersSize].handle)
  205. return UA_STATUSCODE_BADOUTOFMEMORY;
  206. conf->networkLayersSize++;
  207. return UA_STATUSCODE_GOOD;
  208. }
  209. UA_EXPORT UA_StatusCode
  210. UA_ServerConfig_addSecurityPolicyNone(UA_ServerConfig *config,
  211. const UA_ByteString *certificate) {
  212. UA_StatusCode retval;
  213. /* Allocate the SecurityPolicies */
  214. UA_SecurityPolicy *tmp = (UA_SecurityPolicy *)
  215. UA_realloc(config->securityPolicies, sizeof(UA_SecurityPolicy) * (1 + config->securityPoliciesSize));
  216. if(!tmp)
  217. return UA_STATUSCODE_BADOUTOFMEMORY;
  218. config->securityPolicies = tmp;
  219. /* Populate the SecurityPolicies */
  220. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  221. if(certificate)
  222. localCertificate = *certificate;
  223. retval = UA_SecurityPolicy_None(&config->securityPolicies[config->securityPoliciesSize], NULL,
  224. localCertificate, &config->logger);
  225. if(retval != UA_STATUSCODE_GOOD)
  226. return retval;
  227. config->securityPoliciesSize++;
  228. return UA_STATUSCODE_GOOD;
  229. }
  230. UA_EXPORT UA_StatusCode
  231. UA_ServerConfig_addEndpoint(UA_ServerConfig *config, const UA_String securityPolicyUri,
  232. UA_MessageSecurityMode securityMode)
  233. {
  234. UA_StatusCode retval;
  235. /* Allocate the endpoint */
  236. UA_EndpointDescription * tmp = (UA_EndpointDescription *)
  237. UA_realloc(config->endpoints, sizeof(UA_EndpointDescription) * (1 + config->endpointsSize));
  238. if(!tmp) {
  239. return UA_STATUSCODE_BADOUTOFMEMORY;
  240. }
  241. config->endpoints = tmp;
  242. /* Lookup the security policy */
  243. const UA_SecurityPolicy *policy = NULL;
  244. for (size_t i = 0; i < config->securityPoliciesSize; ++i) {
  245. if (UA_String_equal(&securityPolicyUri, &config->securityPolicies[i].policyUri)) {
  246. policy = &config->securityPolicies[i];
  247. break;
  248. }
  249. }
  250. if (!policy)
  251. return UA_STATUSCODE_BADINVALIDARGUMENT;
  252. /* Populate the endpoint */
  253. retval = createEndpoint(config, &config->endpoints[config->endpointsSize],
  254. policy, securityMode);
  255. if(retval != UA_STATUSCODE_GOOD)
  256. return retval;
  257. config->endpointsSize++;
  258. return UA_STATUSCODE_GOOD;
  259. }
  260. UA_EXPORT UA_StatusCode
  261. UA_ServerConfig_addAllEndpoints(UA_ServerConfig *config) {
  262. UA_StatusCode retval;
  263. /* Allocate the endpoints */
  264. UA_EndpointDescription * tmp = (UA_EndpointDescription *)
  265. UA_realloc(config->endpoints, sizeof(UA_EndpointDescription) * (2 * config->securityPoliciesSize + config->endpointsSize));
  266. if(!tmp) {
  267. return UA_STATUSCODE_BADOUTOFMEMORY;
  268. }
  269. config->endpoints = tmp;
  270. /* Populate the endpoints */
  271. for (size_t i = 0; i < config->securityPoliciesSize; ++i) {
  272. if (UA_String_equal(&UA_SECURITY_POLICY_NONE_URI, &config->securityPolicies[i].policyUri)) {
  273. retval = createEndpoint(config, &config->endpoints[config->endpointsSize],
  274. &config->securityPolicies[i], UA_MESSAGESECURITYMODE_NONE);
  275. if(retval != UA_STATUSCODE_GOOD)
  276. return retval;
  277. config->endpointsSize++;
  278. } else {
  279. retval = createEndpoint(config, &config->endpoints[config->endpointsSize],
  280. &config->securityPolicies[i], UA_MESSAGESECURITYMODE_SIGN);
  281. if(retval != UA_STATUSCODE_GOOD)
  282. return retval;
  283. config->endpointsSize++;
  284. retval = createEndpoint(config, &config->endpoints[config->endpointsSize],
  285. &config->securityPolicies[i], UA_MESSAGESECURITYMODE_SIGNANDENCRYPT);
  286. if(retval != UA_STATUSCODE_GOOD)
  287. return retval;
  288. config->endpointsSize++;
  289. }
  290. }
  291. return UA_STATUSCODE_GOOD;
  292. }
  293. UA_EXPORT UA_StatusCode
  294. UA_ServerConfig_setMinimalCustomBuffer(UA_ServerConfig *config, UA_UInt16 portNumber,
  295. const UA_ByteString *certificate,
  296. UA_UInt32 sendBufferSize,
  297. UA_UInt32 recvBufferSize) {
  298. if (!config)
  299. return UA_STATUSCODE_BADINVALIDARGUMENT;
  300. UA_StatusCode retval = setDefaultConfig(config);
  301. if(retval != UA_STATUSCODE_GOOD) {
  302. UA_ServerConfig_clean(config);
  303. return retval;
  304. }
  305. retval = addDefaultNetworkLayers(config, portNumber, sendBufferSize, recvBufferSize);
  306. if(retval != UA_STATUSCODE_GOOD) {
  307. UA_ServerConfig_clean(config);
  308. return retval;
  309. }
  310. /* Allocate the SecurityPolicies */
  311. retval = UA_ServerConfig_addSecurityPolicyNone(config, certificate);
  312. if(retval != UA_STATUSCODE_GOOD) {
  313. UA_ServerConfig_clean(config);
  314. return retval;
  315. }
  316. /* Initialize the Access Control plugin */
  317. retval = UA_AccessControl_default(config, true,
  318. &config->securityPolicies[config->securityPoliciesSize-1].policyUri,
  319. usernamePasswordsSize, usernamePasswords);
  320. if(retval != UA_STATUSCODE_GOOD) {
  321. UA_ServerConfig_clean(config);
  322. return retval;
  323. }
  324. /* Allocate the endpoint */
  325. retval = UA_ServerConfig_addEndpoint(config, UA_SECURITY_POLICY_NONE_URI, UA_MESSAGESECURITYMODE_NONE);
  326. if(retval != UA_STATUSCODE_GOOD) {
  327. UA_ServerConfig_clean(config);
  328. return retval;
  329. }
  330. return UA_STATUSCODE_GOOD;
  331. }
  332. #ifdef UA_ENABLE_ENCRYPTION
  333. UA_EXPORT UA_StatusCode
  334. UA_ServerConfig_addSecurityPolicyBasic128Rsa15(UA_ServerConfig *config,
  335. const UA_ByteString *certificate,
  336. const UA_ByteString *privateKey) {
  337. UA_StatusCode retval;
  338. /* Allocate the SecurityPolicies */
  339. UA_SecurityPolicy *tmp = (UA_SecurityPolicy *)
  340. UA_realloc(config->securityPolicies, sizeof(UA_SecurityPolicy) * (1 + config->securityPoliciesSize));
  341. if(!tmp)
  342. return UA_STATUSCODE_BADOUTOFMEMORY;
  343. config->securityPolicies = tmp;
  344. /* Populate the SecurityPolicies */
  345. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  346. UA_ByteString localPrivateKey = UA_BYTESTRING_NULL;
  347. if(certificate)
  348. localCertificate = *certificate;
  349. if(privateKey)
  350. localPrivateKey = *privateKey;
  351. retval = UA_SecurityPolicy_Basic128Rsa15(&config->securityPolicies[config->securityPoliciesSize],
  352. &config->certificateVerification,
  353. localCertificate, localPrivateKey, &config->logger);
  354. if(retval != UA_STATUSCODE_GOOD)
  355. return retval;
  356. config->securityPoliciesSize++;
  357. return UA_STATUSCODE_GOOD;
  358. }
  359. UA_EXPORT UA_StatusCode
  360. UA_ServerConfig_addSecurityPolicyBasic256(UA_ServerConfig *config,
  361. const UA_ByteString *certificate,
  362. const UA_ByteString *privateKey) {
  363. UA_StatusCode retval;
  364. /* Allocate the SecurityPolicies */
  365. UA_SecurityPolicy *tmp = (UA_SecurityPolicy *)
  366. UA_realloc(config->securityPolicies, sizeof(UA_SecurityPolicy) * (1 + config->securityPoliciesSize));
  367. if(!tmp)
  368. return UA_STATUSCODE_BADOUTOFMEMORY;
  369. config->securityPolicies = tmp;
  370. /* Populate the SecurityPolicies */
  371. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  372. UA_ByteString localPrivateKey = UA_BYTESTRING_NULL;
  373. if(certificate)
  374. localCertificate = *certificate;
  375. if(privateKey)
  376. localPrivateKey = *privateKey;
  377. retval = UA_SecurityPolicy_Basic256(&config->securityPolicies[config->securityPoliciesSize],
  378. &config->certificateVerification,
  379. localCertificate, localPrivateKey, &config->logger);
  380. if(retval != UA_STATUSCODE_GOOD)
  381. return retval;
  382. config->securityPoliciesSize++;
  383. return UA_STATUSCODE_GOOD;
  384. }
  385. UA_EXPORT UA_StatusCode
  386. UA_ServerConfig_addSecurityPolicyBasic256Sha256(UA_ServerConfig *config,
  387. const UA_ByteString *certificate,
  388. const UA_ByteString *privateKey) {
  389. UA_StatusCode retval;
  390. /* Allocate the SecurityPolicies */
  391. UA_SecurityPolicy *tmp = (UA_SecurityPolicy *)
  392. UA_realloc(config->securityPolicies, sizeof(UA_SecurityPolicy) * (1 + config->securityPoliciesSize));
  393. if(!tmp)
  394. return UA_STATUSCODE_BADOUTOFMEMORY;
  395. config->securityPolicies = tmp;
  396. /* Populate the SecurityPolicies */
  397. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  398. UA_ByteString localPrivateKey = UA_BYTESTRING_NULL;
  399. if(certificate)
  400. localCertificate = *certificate;
  401. if(privateKey)
  402. localPrivateKey = *privateKey;
  403. retval = UA_SecurityPolicy_Basic256Sha256(&config->securityPolicies[config->securityPoliciesSize],
  404. &config->certificateVerification,
  405. localCertificate, localPrivateKey, &config->logger);
  406. if(retval != UA_STATUSCODE_GOOD)
  407. return retval;
  408. config->securityPoliciesSize++;
  409. return UA_STATUSCODE_GOOD;
  410. }
  411. UA_EXPORT UA_StatusCode
  412. UA_ServerConfig_addAllSecurityPolicies(UA_ServerConfig *config,
  413. const UA_ByteString *certificate,
  414. const UA_ByteString *privateKey) {
  415. UA_StatusCode retval;
  416. /* Allocate the SecurityPolicies */
  417. UA_SecurityPolicy *tmp = (UA_SecurityPolicy *)
  418. UA_realloc(config->securityPolicies, sizeof(UA_SecurityPolicy) * (4 + config->securityPoliciesSize));
  419. if(!tmp)
  420. return UA_STATUSCODE_BADOUTOFMEMORY;
  421. config->securityPolicies = tmp;
  422. /* Populate the SecurityPolicies */
  423. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  424. UA_ByteString localPrivateKey = UA_BYTESTRING_NULL;
  425. if(certificate)
  426. localCertificate = *certificate;
  427. if(privateKey)
  428. localPrivateKey = *privateKey;
  429. retval = UA_SecurityPolicy_None(&config->securityPolicies[config->securityPoliciesSize], NULL,
  430. localCertificate, &config->logger);
  431. if(retval != UA_STATUSCODE_GOOD)
  432. return retval;
  433. config->securityPoliciesSize++;
  434. retval = UA_SecurityPolicy_Basic128Rsa15(&config->securityPolicies[config->securityPoliciesSize],
  435. &config->certificateVerification,
  436. localCertificate, localPrivateKey, &config->logger);
  437. if(retval != UA_STATUSCODE_GOOD)
  438. return retval;
  439. config->securityPoliciesSize++;
  440. retval = UA_SecurityPolicy_Basic256(&config->securityPolicies[config->securityPoliciesSize],
  441. &config->certificateVerification,
  442. localCertificate, localPrivateKey, &config->logger);
  443. if(retval != UA_STATUSCODE_GOOD)
  444. return retval;
  445. config->securityPoliciesSize++;
  446. retval = UA_SecurityPolicy_Basic256Sha256(&config->securityPolicies[config->securityPoliciesSize],
  447. &config->certificateVerification,
  448. localCertificate, localPrivateKey, &config->logger);
  449. if(retval != UA_STATUSCODE_GOOD)
  450. return retval;
  451. config->securityPoliciesSize++;
  452. return retval;
  453. }
  454. UA_EXPORT UA_StatusCode
  455. UA_ServerConfig_setDefaultWithSecurityPolicies(UA_ServerConfig *conf,
  456. UA_UInt16 portNumber,
  457. const UA_ByteString *certificate,
  458. const UA_ByteString *privateKey,
  459. const UA_ByteString *trustList,
  460. size_t trustListSize,
  461. const UA_ByteString *issuerList,
  462. size_t issuerListSize,
  463. const UA_ByteString *revocationList,
  464. size_t revocationListSize) {
  465. UA_StatusCode retval = setDefaultConfig(conf);
  466. if(retval != UA_STATUSCODE_GOOD) {
  467. UA_ServerConfig_clean(conf);
  468. return retval;
  469. }
  470. retval = UA_CertificateVerification_Trustlist(&conf->certificateVerification,
  471. trustList, trustListSize,
  472. issuerList, issuerListSize,
  473. revocationList, revocationListSize);
  474. if (retval != UA_STATUSCODE_GOOD)
  475. return retval;
  476. if(trustListSize == 0)
  477. UA_LOG_WARNING(&conf->logger, UA_LOGCATEGORY_USERLAND,
  478. "No CA trust-list provided. "
  479. "Any remote certificate will be accepted.");
  480. retval = addDefaultNetworkLayers(conf, portNumber, 0, 0);
  481. if(retval != UA_STATUSCODE_GOOD) {
  482. UA_ServerConfig_clean(conf);
  483. return retval;
  484. }
  485. retval = UA_ServerConfig_addAllSecurityPolicies(conf, certificate, privateKey);
  486. if(retval != UA_STATUSCODE_GOOD) {
  487. UA_ServerConfig_clean(conf);
  488. return retval;
  489. }
  490. retval = UA_AccessControl_default(conf, true,
  491. &conf->securityPolicies[conf->securityPoliciesSize-1].policyUri,
  492. usernamePasswordsSize, usernamePasswords);
  493. if(retval != UA_STATUSCODE_GOOD) {
  494. UA_ServerConfig_clean(conf);
  495. return retval;
  496. }
  497. retval = UA_ServerConfig_addAllEndpoints(conf);
  498. if(retval != UA_STATUSCODE_GOOD) {
  499. UA_ServerConfig_clean(conf);
  500. return retval;
  501. }
  502. return UA_STATUSCODE_GOOD;
  503. }
  504. #endif
  505. /***************************/
  506. /* Default Client Settings */
  507. /***************************/
  508. static UA_INLINE void
  509. UA_ClientConnectionTCP_poll_callback(UA_Client *client, void *data) {
  510. UA_ClientConnectionTCP_poll(client, data);
  511. }
  512. UA_StatusCode
  513. UA_ClientConfig_setDefault(UA_ClientConfig *config) {
  514. config->timeout = 5000;
  515. config->secureChannelLifeTime = 10 * 60 * 1000; /* 10 minutes */
  516. config->logger.log = UA_Log_Stdout_log;
  517. config->logger.context = NULL;
  518. config->logger.clear = UA_Log_Stdout_clear;
  519. config->localConnectionConfig = UA_ConnectionConfig_default;
  520. /* Certificate Verification that accepts every certificate. Can be
  521. * overwritten when the policy is specialized. */
  522. UA_CertificateVerification_AcceptAll(&config->certificateVerification);
  523. /* With encryption enabled, the applicationUri needs to match the URI from
  524. * the certificate */
  525. config->clientDescription.applicationUri = UA_STRING_ALLOC(APPLICATION_URI);
  526. config->clientDescription.applicationType = UA_APPLICATIONTYPE_CLIENT;
  527. if(config->securityPoliciesSize > 0) {
  528. UA_LOG_ERROR(&config->logger, UA_LOGCATEGORY_NETWORK,
  529. "Could not initialize a config that already has SecurityPolicies");
  530. return UA_STATUSCODE_BADINTERNALERROR;
  531. }
  532. config->securityPolicies = (UA_SecurityPolicy*)UA_malloc(sizeof(UA_SecurityPolicy));
  533. if(!config->securityPolicies)
  534. return UA_STATUSCODE_BADOUTOFMEMORY;
  535. UA_StatusCode retval = UA_SecurityPolicy_None(config->securityPolicies, NULL,
  536. UA_BYTESTRING_NULL, &config->logger);
  537. if(retval != UA_STATUSCODE_GOOD) {
  538. UA_free(config->securityPolicies);
  539. config->securityPolicies = NULL;
  540. return retval;
  541. }
  542. config->securityPoliciesSize = 1;
  543. config->connectionFunc = UA_ClientConnectionTCP;
  544. config->initConnectionFunc = UA_ClientConnectionTCP_init; /* for async client */
  545. config->pollConnectionFunc = UA_ClientConnectionTCP_poll_callback; /* for async connection */
  546. config->customDataTypes = NULL;
  547. config->stateCallback = NULL;
  548. config->connectivityCheckInterval = 0;
  549. config->requestedSessionTimeout = 1200000; /* requestedSessionTimeout */
  550. config->inactivityCallback = NULL;
  551. config->clientContext = NULL;
  552. #ifdef UA_ENABLE_SUBSCRIPTIONS
  553. config->outStandingPublishRequests = 10;
  554. config->subscriptionInactivityCallback = NULL;
  555. #endif
  556. return UA_STATUSCODE_GOOD;
  557. }
  558. #ifdef UA_ENABLE_ENCRYPTION
  559. UA_StatusCode
  560. UA_ClientConfig_setDefaultEncryption(UA_ClientConfig *config,
  561. UA_ByteString localCertificate, UA_ByteString privateKey,
  562. const UA_ByteString *trustList, size_t trustListSize,
  563. const UA_ByteString *revocationList, size_t revocationListSize) {
  564. UA_StatusCode retval = UA_ClientConfig_setDefault(config);
  565. if(retval != UA_STATUSCODE_GOOD)
  566. return retval;
  567. retval = UA_CertificateVerification_Trustlist(&config->certificateVerification,
  568. trustList, trustListSize,
  569. NULL, 0,
  570. revocationList, revocationListSize);
  571. if(retval != UA_STATUSCODE_GOOD)
  572. return retval;
  573. /* Populate SecurityPolicies */
  574. UA_SecurityPolicy *sp = (UA_SecurityPolicy*)
  575. UA_realloc(config->securityPolicies, sizeof(UA_SecurityPolicy) * 4);
  576. if(!sp)
  577. return UA_STATUSCODE_BADOUTOFMEMORY;
  578. config->securityPolicies = sp;
  579. retval = UA_SecurityPolicy_Basic128Rsa15(&config->securityPolicies[1],
  580. &config->certificateVerification,
  581. localCertificate, privateKey, &config->logger);
  582. if(retval != UA_STATUSCODE_GOOD)
  583. return retval;
  584. ++config->securityPoliciesSize;
  585. retval = UA_SecurityPolicy_Basic256(&config->securityPolicies[2],
  586. &config->certificateVerification,
  587. localCertificate, privateKey, &config->logger);
  588. if(retval != UA_STATUSCODE_GOOD)
  589. return retval;
  590. ++config->securityPoliciesSize;
  591. retval = UA_SecurityPolicy_Basic256Sha256(&config->securityPolicies[3],
  592. &config->certificateVerification,
  593. localCertificate, privateKey, &config->logger);
  594. if(retval != UA_STATUSCODE_GOOD)
  595. return retval;
  596. ++config->securityPoliciesSize;
  597. return UA_STATUSCODE_GOOD;
  598. }
  599. #endif