ua_config_default.c 30 KB

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