ua_config_default.c 30 KB

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