ua_config_default.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. /* Server Description */
  93. conf->buildInfo.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  94. conf->buildInfo.manufacturerName = UA_STRING_ALLOC(MANUFACTURER_NAME);
  95. conf->buildInfo.productName = UA_STRING_ALLOC(PRODUCT_NAME);
  96. conf->buildInfo.softwareVersion =
  97. UA_STRING_ALLOC(VERSION(UA_OPEN62541_VER_MAJOR, UA_OPEN62541_VER_MINOR,
  98. UA_OPEN62541_VER_PATCH, UA_OPEN62541_VER_LABEL));
  99. #ifdef UA_PACK_DEBIAN
  100. conf->buildInfo.buildNumber = UA_STRING_ALLOC("deb");
  101. #else
  102. conf->buildInfo.buildNumber = UA_STRING_ALLOC(__DATE__ " " __TIME__);
  103. #endif
  104. conf->buildInfo.buildDate = UA_DateTime_now();
  105. conf->applicationDescription.applicationUri = UA_STRING_ALLOC(APPLICATION_URI_SERVER);
  106. conf->applicationDescription.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  107. conf->applicationDescription.applicationName =
  108. UA_LOCALIZEDTEXT_ALLOC("en", APPLICATION_NAME);
  109. conf->applicationDescription.applicationType = UA_APPLICATIONTYPE_SERVER;
  110. /* conf->applicationDescription.gatewayServerUri = UA_STRING_NULL; */
  111. /* conf->applicationDescription.discoveryProfileUri = UA_STRING_NULL; */
  112. /* conf->applicationDescription.discoveryUrlsSize = 0; */
  113. /* conf->applicationDescription.discoveryUrls = NULL; */
  114. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  115. UA_MdnsDiscoveryConfiguration_init(&conf->discovery.mdns);
  116. conf->discovery.mdnsInterfaceIP = UA_STRING_NULL;
  117. #endif
  118. /* Custom DataTypes */
  119. /* conf->customDataTypesSize = 0; */
  120. /* conf->customDataTypes = NULL; */
  121. /* Networking */
  122. /* conf->networkLayersSize = 0; */
  123. /* conf->networkLayers = NULL; */
  124. /* conf->customHostname = UA_STRING_NULL; */
  125. /* Endpoints */
  126. /* conf->endpoints = {0, NULL}; */
  127. /* Certificate Verification that accepts every certificate. Can be
  128. * overwritten when the policy is specialized. */
  129. UA_CertificateVerification_AcceptAll(&conf->certificateVerification);
  130. /* Global Node Lifecycle */
  131. conf->nodeLifecycle.constructor = NULL;
  132. conf->nodeLifecycle.destructor = NULL;
  133. /* Relax constraints for the InformationModel */
  134. conf->relaxEmptyValueConstraint = true; /* Allow empty values */
  135. /* Limits for SecureChannels */
  136. conf->maxSecureChannels = 40;
  137. conf->maxSecurityTokenLifetime = 10 * 60 * 1000; /* 10 minutes */
  138. /* Limits for Sessions */
  139. conf->maxSessions = 100;
  140. conf->maxSessionTimeout = 60.0 * 60.0 * 1000.0; /* 1h */
  141. /* Limits for Subscriptions */
  142. conf->publishingIntervalLimits = UA_DURATIONRANGE(100.0, 3600.0 * 1000.0);
  143. conf->lifeTimeCountLimits = UA_UINT32RANGE(3, 15000);
  144. conf->keepAliveCountLimits = UA_UINT32RANGE(1, 100);
  145. conf->maxNotificationsPerPublish = 1000;
  146. conf->enableRetransmissionQueue = true;
  147. conf->maxRetransmissionQueueSize = 0; /* unlimited */
  148. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  149. conf->maxEventsPerNode = 0; /* unlimited */
  150. #endif
  151. /* Limits for MonitoredItems */
  152. conf->samplingIntervalLimits = UA_DURATIONRANGE(50.0, 24.0 * 3600.0 * 1000.0);
  153. conf->queueSizeLimits = UA_UINT32RANGE(1, 100);
  154. #ifdef UA_ENABLE_DISCOVERY
  155. conf->discovery.cleanupTimeout = 60 * 60;
  156. #endif
  157. #ifdef UA_ENABLE_HISTORIZING
  158. /* conf->accessHistoryDataCapability = UA_FALSE; */
  159. /* conf->maxReturnDataValues = 0; */
  160. /* conf->accessHistoryEventsCapability = UA_FALSE; */
  161. /* conf->maxReturnEventValues = 0; */
  162. /* conf->insertDataCapability = UA_FALSE; */
  163. /* conf->insertEventCapability = UA_FALSE; */
  164. /* conf->insertAnnotationsCapability = UA_FALSE; */
  165. /* conf->replaceDataCapability = UA_FALSE; */
  166. /* conf->replaceEventCapability = UA_FALSE; */
  167. /* conf->updateDataCapability = UA_FALSE; */
  168. /* conf->updateEventCapability = UA_FALSE; */
  169. /* conf->deleteRawCapability = UA_FALSE; */
  170. /* conf->deleteEventCapability = UA_FALSE; */
  171. /* conf->deleteAtTimeDataCapability = UA_FALSE; */
  172. #endif
  173. /* --> Finish setting the default static config <-- */
  174. return UA_STATUSCODE_GOOD;
  175. }
  176. UA_EXPORT UA_StatusCode
  177. UA_ServerConfig_setBasics(UA_ServerConfig* conf) {
  178. return setDefaultConfig(conf);
  179. }
  180. static UA_StatusCode
  181. addDefaultNetworkLayers(UA_ServerConfig *conf, UA_UInt16 portNumber,
  182. UA_UInt32 sendBufferSize, UA_UInt32 recvBufferSize) {
  183. return UA_ServerConfig_addNetworkLayerTCP(conf, portNumber, sendBufferSize, recvBufferSize);
  184. }
  185. UA_EXPORT UA_StatusCode
  186. UA_ServerConfig_addNetworkLayerTCP(UA_ServerConfig *conf, UA_UInt16 portNumber,
  187. UA_UInt32 sendBufferSize, UA_UInt32 recvBufferSize) {
  188. /* Add a network layer */
  189. UA_ServerNetworkLayer *tmp = (UA_ServerNetworkLayer *)
  190. UA_realloc(conf->networkLayers, sizeof(UA_ServerNetworkLayer) * (1 + conf->networkLayersSize));
  191. if(!tmp)
  192. return UA_STATUSCODE_BADOUTOFMEMORY;
  193. conf->networkLayers = tmp;
  194. UA_ConnectionConfig config = UA_ConnectionConfig_default;
  195. if (sendBufferSize > 0)
  196. config.sendBufferSize = sendBufferSize;
  197. if (recvBufferSize > 0)
  198. config.recvBufferSize = recvBufferSize;
  199. conf->networkLayers[conf->networkLayersSize] =
  200. UA_ServerNetworkLayerTCP(config, portNumber, &conf->logger);
  201. if (!conf->networkLayers[conf->networkLayersSize].handle)
  202. return UA_STATUSCODE_BADOUTOFMEMORY;
  203. conf->networkLayersSize++;
  204. return UA_STATUSCODE_GOOD;
  205. }
  206. UA_EXPORT UA_StatusCode
  207. UA_ServerConfig_addSecurityPolicyNone(UA_ServerConfig *config,
  208. const UA_ByteString *certificate) {
  209. UA_StatusCode retval;
  210. /* Allocate the SecurityPolicies */
  211. UA_SecurityPolicy *tmp = (UA_SecurityPolicy *)
  212. UA_realloc(config->securityPolicies, sizeof(UA_SecurityPolicy) * (1 + config->securityPoliciesSize));
  213. if(!tmp)
  214. return UA_STATUSCODE_BADOUTOFMEMORY;
  215. config->securityPolicies = tmp;
  216. /* Populate the SecurityPolicies */
  217. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  218. if(certificate)
  219. localCertificate = *certificate;
  220. retval = UA_SecurityPolicy_None(&config->securityPolicies[config->securityPoliciesSize], NULL,
  221. localCertificate, &config->logger);
  222. if(retval != UA_STATUSCODE_GOOD)
  223. return retval;
  224. config->securityPoliciesSize++;
  225. return UA_STATUSCODE_GOOD;
  226. }
  227. UA_EXPORT UA_StatusCode
  228. UA_ServerConfig_addEndpoint(UA_ServerConfig *config, const UA_String securityPolicyUri,
  229. UA_MessageSecurityMode securityMode)
  230. {
  231. UA_StatusCode retval;
  232. /* Allocate the endpoint */
  233. UA_EndpointDescription * tmp = (UA_EndpointDescription *)
  234. UA_realloc(config->endpoints, sizeof(UA_EndpointDescription) * (1 + config->endpointsSize));
  235. if(!tmp) {
  236. return UA_STATUSCODE_BADOUTOFMEMORY;
  237. }
  238. config->endpoints = tmp;
  239. /* Lookup the security policy */
  240. const UA_SecurityPolicy *policy = NULL;
  241. for (size_t i = 0; i < config->securityPoliciesSize; ++i) {
  242. if (UA_String_equal(&securityPolicyUri, &config->securityPolicies[i].policyUri)) {
  243. policy = &config->securityPolicies[i];
  244. break;
  245. }
  246. }
  247. if (!policy)
  248. return UA_STATUSCODE_BADINVALIDARGUMENT;
  249. /* Populate the endpoint */
  250. retval = createEndpoint(config, &config->endpoints[config->endpointsSize],
  251. policy, securityMode);
  252. if(retval != UA_STATUSCODE_GOOD)
  253. return retval;
  254. config->endpointsSize++;
  255. return UA_STATUSCODE_GOOD;
  256. }
  257. UA_EXPORT UA_StatusCode
  258. UA_ServerConfig_addAllEndpoints(UA_ServerConfig *config) {
  259. UA_StatusCode retval;
  260. /* Allocate the endpoints */
  261. UA_EndpointDescription * tmp = (UA_EndpointDescription *)
  262. UA_realloc(config->endpoints, sizeof(UA_EndpointDescription) * (2 * config->securityPoliciesSize + config->endpointsSize));
  263. if(!tmp) {
  264. return UA_STATUSCODE_BADOUTOFMEMORY;
  265. }
  266. config->endpoints = tmp;
  267. /* Populate the endpoints */
  268. for (size_t i = 0; i < config->securityPoliciesSize; ++i) {
  269. if (UA_String_equal(&UA_SECURITY_POLICY_NONE_URI, &config->securityPolicies[i].policyUri)) {
  270. retval = createEndpoint(config, &config->endpoints[config->endpointsSize],
  271. &config->securityPolicies[i], UA_MESSAGESECURITYMODE_NONE);
  272. if(retval != UA_STATUSCODE_GOOD)
  273. return retval;
  274. config->endpointsSize++;
  275. } else {
  276. retval = createEndpoint(config, &config->endpoints[config->endpointsSize],
  277. &config->securityPolicies[i], UA_MESSAGESECURITYMODE_SIGN);
  278. if(retval != UA_STATUSCODE_GOOD)
  279. return retval;
  280. config->endpointsSize++;
  281. retval = createEndpoint(config, &config->endpoints[config->endpointsSize],
  282. &config->securityPolicies[i], UA_MESSAGESECURITYMODE_SIGNANDENCRYPT);
  283. if(retval != UA_STATUSCODE_GOOD)
  284. return retval;
  285. config->endpointsSize++;
  286. }
  287. }
  288. return UA_STATUSCODE_GOOD;
  289. }
  290. UA_EXPORT UA_StatusCode
  291. UA_ServerConfig_setMinimalCustomBuffer(UA_ServerConfig *config, UA_UInt16 portNumber,
  292. const UA_ByteString *certificate,
  293. UA_UInt32 sendBufferSize,
  294. UA_UInt32 recvBufferSize) {
  295. if (!config)
  296. return UA_STATUSCODE_BADINVALIDARGUMENT;
  297. UA_StatusCode retval = setDefaultConfig(config);
  298. if(retval != UA_STATUSCODE_GOOD) {
  299. UA_ServerConfig_clean(config);
  300. return retval;
  301. }
  302. retval = addDefaultNetworkLayers(config, portNumber, sendBufferSize, recvBufferSize);
  303. if(retval != UA_STATUSCODE_GOOD) {
  304. UA_ServerConfig_clean(config);
  305. return retval;
  306. }
  307. /* Allocate the SecurityPolicies */
  308. retval = UA_ServerConfig_addSecurityPolicyNone(config, certificate);
  309. if(retval != UA_STATUSCODE_GOOD) {
  310. UA_ServerConfig_clean(config);
  311. return retval;
  312. }
  313. /* Initialize the Access Control plugin */
  314. retval = UA_AccessControl_default(config, true,
  315. &config->securityPolicies[config->securityPoliciesSize-1].policyUri,
  316. usernamePasswordsSize, usernamePasswords);
  317. if(retval != UA_STATUSCODE_GOOD) {
  318. UA_ServerConfig_clean(config);
  319. return retval;
  320. }
  321. /* Allocate the endpoint */
  322. retval = UA_ServerConfig_addEndpoint(config, UA_SECURITY_POLICY_NONE_URI, UA_MESSAGESECURITYMODE_NONE);
  323. if(retval != UA_STATUSCODE_GOOD) {
  324. UA_ServerConfig_clean(config);
  325. return retval;
  326. }
  327. return UA_STATUSCODE_GOOD;
  328. }
  329. #ifdef UA_ENABLE_ENCRYPTION
  330. UA_EXPORT UA_StatusCode
  331. UA_ServerConfig_addSecurityPolicyBasic128Rsa15(UA_ServerConfig *config,
  332. const UA_ByteString *certificate,
  333. const UA_ByteString *privateKey) {
  334. UA_StatusCode retval;
  335. /* Allocate the SecurityPolicies */
  336. UA_SecurityPolicy *tmp = (UA_SecurityPolicy *)
  337. UA_realloc(config->securityPolicies, sizeof(UA_SecurityPolicy) * (1 + config->securityPoliciesSize));
  338. if(!tmp)
  339. return UA_STATUSCODE_BADOUTOFMEMORY;
  340. config->securityPolicies = tmp;
  341. /* Populate the SecurityPolicies */
  342. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  343. UA_ByteString localPrivateKey = UA_BYTESTRING_NULL;
  344. if(certificate)
  345. localCertificate = *certificate;
  346. if(privateKey)
  347. localPrivateKey = *privateKey;
  348. retval = UA_SecurityPolicy_Basic128Rsa15(&config->securityPolicies[config->securityPoliciesSize],
  349. &config->certificateVerification,
  350. localCertificate, localPrivateKey, &config->logger);
  351. if(retval != UA_STATUSCODE_GOOD)
  352. return retval;
  353. config->securityPoliciesSize++;
  354. return UA_STATUSCODE_GOOD;
  355. }
  356. UA_EXPORT UA_StatusCode
  357. UA_ServerConfig_addSecurityPolicyBasic256(UA_ServerConfig *config,
  358. const UA_ByteString *certificate,
  359. const UA_ByteString *privateKey) {
  360. UA_StatusCode retval;
  361. /* Allocate the SecurityPolicies */
  362. UA_SecurityPolicy *tmp = (UA_SecurityPolicy *)
  363. UA_realloc(config->securityPolicies, sizeof(UA_SecurityPolicy) * (1 + config->securityPoliciesSize));
  364. if(!tmp)
  365. return UA_STATUSCODE_BADOUTOFMEMORY;
  366. config->securityPolicies = tmp;
  367. /* Populate the SecurityPolicies */
  368. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  369. UA_ByteString localPrivateKey = UA_BYTESTRING_NULL;
  370. if(certificate)
  371. localCertificate = *certificate;
  372. if(privateKey)
  373. localPrivateKey = *privateKey;
  374. retval = UA_SecurityPolicy_Basic256(&config->securityPolicies[config->securityPoliciesSize],
  375. &config->certificateVerification,
  376. localCertificate, localPrivateKey, &config->logger);
  377. if(retval != UA_STATUSCODE_GOOD)
  378. return retval;
  379. config->securityPoliciesSize++;
  380. return UA_STATUSCODE_GOOD;
  381. }
  382. UA_EXPORT UA_StatusCode
  383. UA_ServerConfig_addSecurityPolicyBasic256Sha256(UA_ServerConfig *config,
  384. const UA_ByteString *certificate,
  385. const UA_ByteString *privateKey) {
  386. UA_StatusCode retval;
  387. /* Allocate the SecurityPolicies */
  388. UA_SecurityPolicy *tmp = (UA_SecurityPolicy *)
  389. UA_realloc(config->securityPolicies, sizeof(UA_SecurityPolicy) * (1 + config->securityPoliciesSize));
  390. if(!tmp)
  391. return UA_STATUSCODE_BADOUTOFMEMORY;
  392. config->securityPolicies = tmp;
  393. /* Populate the SecurityPolicies */
  394. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  395. UA_ByteString localPrivateKey = UA_BYTESTRING_NULL;
  396. if(certificate)
  397. localCertificate = *certificate;
  398. if(privateKey)
  399. localPrivateKey = *privateKey;
  400. retval = UA_SecurityPolicy_Basic256Sha256(&config->securityPolicies[config->securityPoliciesSize],
  401. &config->certificateVerification,
  402. localCertificate, localPrivateKey, &config->logger);
  403. if(retval != UA_STATUSCODE_GOOD)
  404. return retval;
  405. config->securityPoliciesSize++;
  406. return UA_STATUSCODE_GOOD;
  407. }
  408. UA_EXPORT UA_StatusCode
  409. UA_ServerConfig_addAllSecurityPolicies(UA_ServerConfig *config,
  410. const UA_ByteString *certificate,
  411. const UA_ByteString *privateKey) {
  412. UA_StatusCode retval;
  413. /* Allocate the SecurityPolicies */
  414. UA_SecurityPolicy *tmp = (UA_SecurityPolicy *)
  415. UA_realloc(config->securityPolicies, sizeof(UA_SecurityPolicy) * (4 + config->securityPoliciesSize));
  416. if(!tmp)
  417. return UA_STATUSCODE_BADOUTOFMEMORY;
  418. config->securityPolicies = tmp;
  419. /* Populate the SecurityPolicies */
  420. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  421. UA_ByteString localPrivateKey = UA_BYTESTRING_NULL;
  422. if(certificate)
  423. localCertificate = *certificate;
  424. if(privateKey)
  425. localPrivateKey = *privateKey;
  426. retval = UA_SecurityPolicy_None(&config->securityPolicies[config->securityPoliciesSize], NULL,
  427. localCertificate, &config->logger);
  428. if(retval != UA_STATUSCODE_GOOD)
  429. return retval;
  430. config->securityPoliciesSize++;
  431. retval = UA_SecurityPolicy_Basic128Rsa15(&config->securityPolicies[config->securityPoliciesSize],
  432. &config->certificateVerification,
  433. localCertificate, localPrivateKey, &config->logger);
  434. if(retval != UA_STATUSCODE_GOOD)
  435. return retval;
  436. config->securityPoliciesSize++;
  437. retval = UA_SecurityPolicy_Basic256(&config->securityPolicies[config->securityPoliciesSize],
  438. &config->certificateVerification,
  439. localCertificate, localPrivateKey, &config->logger);
  440. if(retval != UA_STATUSCODE_GOOD)
  441. return retval;
  442. config->securityPoliciesSize++;
  443. retval = UA_SecurityPolicy_Basic256Sha256(&config->securityPolicies[config->securityPoliciesSize],
  444. &config->certificateVerification,
  445. localCertificate, localPrivateKey, &config->logger);
  446. if(retval != UA_STATUSCODE_GOOD)
  447. return retval;
  448. config->securityPoliciesSize++;
  449. return retval;
  450. }
  451. UA_EXPORT UA_StatusCode
  452. UA_ServerConfig_setDefaultWithSecurityPolicies(UA_ServerConfig *conf,
  453. UA_UInt16 portNumber,
  454. const UA_ByteString *certificate,
  455. const UA_ByteString *privateKey,
  456. const UA_ByteString *trustList,
  457. size_t trustListSize,
  458. const UA_ByteString *issuerList,
  459. size_t issuerListSize,
  460. const UA_ByteString *revocationList,
  461. size_t revocationListSize) {
  462. UA_StatusCode retval = setDefaultConfig(conf);
  463. if(retval != UA_STATUSCODE_GOOD) {
  464. UA_ServerConfig_clean(conf);
  465. return retval;
  466. }
  467. retval = UA_CertificateVerification_Trustlist(&conf->certificateVerification,
  468. trustList, trustListSize,
  469. issuerList, issuerListSize,
  470. revocationList, revocationListSize);
  471. if (retval != UA_STATUSCODE_GOOD)
  472. return retval;
  473. if(trustListSize == 0)
  474. UA_LOG_WARNING(&conf->logger, UA_LOGCATEGORY_USERLAND,
  475. "No CA trust-list provided. "
  476. "Any remote certificate will be accepted.");
  477. retval = addDefaultNetworkLayers(conf, portNumber, 0, 0);
  478. if(retval != UA_STATUSCODE_GOOD) {
  479. UA_ServerConfig_clean(conf);
  480. return retval;
  481. }
  482. retval = UA_ServerConfig_addAllSecurityPolicies(conf, certificate, privateKey);
  483. if(retval != UA_STATUSCODE_GOOD) {
  484. UA_ServerConfig_clean(conf);
  485. return retval;
  486. }
  487. retval = UA_AccessControl_default(conf, true,
  488. &conf->securityPolicies[conf->securityPoliciesSize-1].policyUri,
  489. usernamePasswordsSize, usernamePasswords);
  490. if(retval != UA_STATUSCODE_GOOD) {
  491. UA_ServerConfig_clean(conf);
  492. return retval;
  493. }
  494. retval = UA_ServerConfig_addAllEndpoints(conf);
  495. if(retval != UA_STATUSCODE_GOOD) {
  496. UA_ServerConfig_clean(conf);
  497. return retval;
  498. }
  499. return UA_STATUSCODE_GOOD;
  500. }
  501. #endif
  502. /***************************/
  503. /* Default Client Settings */
  504. /***************************/
  505. static UA_INLINE void
  506. UA_ClientConnectionTCP_poll_callback(UA_Client *client, void *data) {
  507. UA_ClientConnectionTCP_poll(client, data);
  508. }
  509. UA_StatusCode
  510. UA_ClientConfig_setDefault(UA_ClientConfig *config) {
  511. config->timeout = 5000;
  512. config->secureChannelLifeTime = 10 * 60 * 1000; /* 10 minutes */
  513. config->logger.log = UA_Log_Stdout_log;
  514. config->logger.context = NULL;
  515. config->logger.clear = UA_Log_Stdout_clear;
  516. config->localConnectionConfig = UA_ConnectionConfig_default;
  517. /* Certificate Verification that accepts every certificate. Can be
  518. * overwritten when the policy is specialized. */
  519. UA_CertificateVerification_AcceptAll(&config->certificateVerification);
  520. /* With encryption enabled, the applicationUri needs to match the URI from
  521. * the certificate */
  522. config->clientDescription.applicationUri = UA_STRING_ALLOC(APPLICATION_URI);
  523. config->clientDescription.applicationType = UA_APPLICATIONTYPE_CLIENT;
  524. if(config->securityPoliciesSize > 0) {
  525. UA_LOG_ERROR(&config->logger, UA_LOGCATEGORY_NETWORK,
  526. "Could not initialize a config that already has SecurityPolicies");
  527. return UA_STATUSCODE_BADINTERNALERROR;
  528. }
  529. config->securityPolicies = (UA_SecurityPolicy*)UA_malloc(sizeof(UA_SecurityPolicy));
  530. if(!config->securityPolicies)
  531. return UA_STATUSCODE_BADOUTOFMEMORY;
  532. UA_StatusCode retval = UA_SecurityPolicy_None(config->securityPolicies, NULL,
  533. UA_BYTESTRING_NULL, &config->logger);
  534. if(retval != UA_STATUSCODE_GOOD) {
  535. UA_free(config->securityPolicies);
  536. config->securityPolicies = NULL;
  537. return retval;
  538. }
  539. config->securityPoliciesSize = 1;
  540. config->connectionFunc = UA_ClientConnectionTCP;
  541. config->initConnectionFunc = UA_ClientConnectionTCP_init; /* for async client */
  542. config->pollConnectionFunc = UA_ClientConnectionTCP_poll_callback; /* for async connection */
  543. config->customDataTypes = NULL;
  544. config->stateCallback = NULL;
  545. config->connectivityCheckInterval = 0;
  546. config->requestedSessionTimeout = 1200000; /* requestedSessionTimeout */
  547. config->inactivityCallback = NULL;
  548. config->clientContext = NULL;
  549. #ifdef UA_ENABLE_SUBSCRIPTIONS
  550. config->outStandingPublishRequests = 10;
  551. config->subscriptionInactivityCallback = NULL;
  552. #endif
  553. return UA_STATUSCODE_GOOD;
  554. }
  555. #ifdef UA_ENABLE_ENCRYPTION
  556. UA_StatusCode
  557. UA_ClientConfig_setDefaultEncryption(UA_ClientConfig *config,
  558. UA_ByteString localCertificate, UA_ByteString privateKey,
  559. const UA_ByteString *trustList, size_t trustListSize,
  560. const UA_ByteString *revocationList, size_t revocationListSize) {
  561. UA_StatusCode retval = UA_ClientConfig_setDefault(config);
  562. if(retval != UA_STATUSCODE_GOOD)
  563. return retval;
  564. retval = UA_CertificateVerification_Trustlist(&config->certificateVerification,
  565. trustList, trustListSize,
  566. NULL, 0,
  567. revocationList, revocationListSize);
  568. if(retval != UA_STATUSCODE_GOOD)
  569. return retval;
  570. /* Populate SecurityPolicies */
  571. UA_SecurityPolicy *sp = (UA_SecurityPolicy*)
  572. UA_realloc(config->securityPolicies, sizeof(UA_SecurityPolicy) * 4);
  573. if(!sp)
  574. return UA_STATUSCODE_BADOUTOFMEMORY;
  575. config->securityPolicies = sp;
  576. retval = UA_SecurityPolicy_Basic128Rsa15(&config->securityPolicies[1],
  577. &config->certificateVerification,
  578. localCertificate, privateKey, &config->logger);
  579. if(retval != UA_STATUSCODE_GOOD)
  580. return retval;
  581. ++config->securityPoliciesSize;
  582. retval = UA_SecurityPolicy_Basic256(&config->securityPolicies[2],
  583. &config->certificateVerification,
  584. localCertificate, privateKey, &config->logger);
  585. if(retval != UA_STATUSCODE_GOOD)
  586. return retval;
  587. ++config->securityPoliciesSize;
  588. retval = UA_SecurityPolicy_Basic256Sha256(&config->securityPolicies[3],
  589. &config->certificateVerification,
  590. localCertificate, privateKey, &config->logger);
  591. if(retval != UA_STATUSCODE_GOOD)
  592. return retval;
  593. ++config->securityPoliciesSize;
  594. return UA_STATUSCODE_GOOD;
  595. }
  596. #endif