ua_config_default.c 30 KB

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