ua_config_default.c 27 KB

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