ua_config_default.c 30 KB

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