ua_config_default.c 27 KB

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