ua_config_default.c 32 KB

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