ua_config_default.c 30 KB

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