ua_config_default.c 29 KB

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