ua_config_default.c 29 KB

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