ua_config_default.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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/plugin/accesscontrol_default.h>
  14. #include <open62541/plugin/log_stdout.h>
  15. #include <open62541/plugin/nodestore_default.h>
  16. #include <open62541/plugin/pki_default.h>
  17. #include <open62541/plugin/securitypolicy_default.h>
  18. #include <open62541/server_config_default.h>
  19. #include "ua_network_tcp.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, 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->securityPolicies = (UA_SecurityPolicy *)UA_malloc(sizeof(UA_SecurityPolicy));
  227. if(!conf->securityPolicies) {
  228. UA_ServerConfig_delete(conf);
  229. return NULL;
  230. }
  231. conf->securityPoliciesSize = 1;
  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) * 4);
  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_Basic256(&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. ++conf->securityPoliciesSize;
  502. retval =
  503. UA_SecurityPolicy_Basic256Sha256(&conf->securityPolicies[3], &conf->certificateVerification,
  504. localCertificate, localPrivateKey, &conf->logger);
  505. if(retval != UA_STATUSCODE_GOOD) {
  506. UA_ServerConfig_delete(conf);
  507. return NULL;
  508. }
  509. /* Allocate the endpoints */
  510. conf->endpointsSize = 0;
  511. conf->endpoints = (UA_EndpointDescription *)UA_malloc(sizeof(UA_EndpointDescription) * 7);
  512. if(!conf->endpoints) {
  513. UA_ServerConfig_delete(conf);
  514. return NULL;
  515. }
  516. /* Populate the endpoints */
  517. retval = createEndpoint(conf, &conf->endpoints[conf->endpointsSize], &conf->securityPolicies[0],
  518. UA_MESSAGESECURITYMODE_NONE);
  519. ++conf->endpointsSize;
  520. if(retval != UA_STATUSCODE_GOOD) {
  521. UA_ServerConfig_delete(conf);
  522. return NULL;
  523. }
  524. retval = createEndpoint(conf, &conf->endpoints[conf->endpointsSize], &conf->securityPolicies[1],
  525. UA_MESSAGESECURITYMODE_SIGN);
  526. ++conf->endpointsSize;
  527. if(retval != UA_STATUSCODE_GOOD) {
  528. UA_ServerConfig_delete(conf);
  529. return NULL;
  530. }
  531. retval = createEndpoint(conf, &conf->endpoints[conf->endpointsSize], &conf->securityPolicies[1],
  532. UA_MESSAGESECURITYMODE_SIGNANDENCRYPT);
  533. ++conf->endpointsSize;
  534. if(retval != UA_STATUSCODE_GOOD) {
  535. UA_ServerConfig_delete(conf);
  536. return NULL;
  537. }
  538. retval = createEndpoint(conf, &conf->endpoints[conf->endpointsSize], &conf->securityPolicies[2],
  539. UA_MESSAGESECURITYMODE_SIGN);
  540. ++conf->endpointsSize;
  541. if(retval != UA_STATUSCODE_GOOD) {
  542. UA_ServerConfig_delete(conf);
  543. return NULL;
  544. }
  545. retval = createEndpoint(conf, &conf->endpoints[conf->endpointsSize], &conf->securityPolicies[2],
  546. UA_MESSAGESECURITYMODE_SIGNANDENCRYPT);
  547. ++conf->endpointsSize;
  548. if(retval != UA_STATUSCODE_GOOD) {
  549. UA_ServerConfig_delete(conf);
  550. return NULL;
  551. }
  552. retval = createEndpoint(conf, &conf->endpoints[conf->endpointsSize], &conf->securityPolicies[3],
  553. UA_MESSAGESECURITYMODE_SIGN);
  554. ++conf->endpointsSize;
  555. if(retval != UA_STATUSCODE_GOOD) {
  556. UA_ServerConfig_delete(conf);
  557. return NULL;
  558. }
  559. retval = createEndpoint(conf, &conf->endpoints[conf->endpointsSize], &conf->securityPolicies[3],
  560. UA_MESSAGESECURITYMODE_SIGNANDENCRYPT);
  561. ++conf->endpointsSize;
  562. if(retval != UA_STATUSCODE_GOOD) {
  563. UA_ServerConfig_delete(conf);
  564. return NULL;
  565. }
  566. return conf;
  567. }
  568. #endif
  569. void
  570. UA_ServerConfig_delete(UA_ServerConfig *config) {
  571. if(!config)
  572. return;
  573. /* Server Description */
  574. UA_BuildInfo_deleteMembers(&config->buildInfo);
  575. UA_ApplicationDescription_deleteMembers(&config->applicationDescription);
  576. #ifdef UA_ENABLE_DISCOVERY
  577. UA_String_deleteMembers(&config->mdnsServerName);
  578. UA_Array_delete(config->serverCapabilities, config->serverCapabilitiesSize,
  579. &UA_TYPES[UA_TYPES_STRING]);
  580. config->serverCapabilities = NULL;
  581. config->serverCapabilitiesSize = 0;
  582. #endif
  583. /* Nodestore */
  584. if(config->nodestore.deleteNodestore)
  585. config->nodestore.deleteNodestore(config->nodestore.context);
  586. /* Custom DataTypes */
  587. /* nothing to do */
  588. /* Networking */
  589. for(size_t i = 0; i < config->networkLayersSize; ++i)
  590. config->networkLayers[i].deleteMembers(&config->networkLayers[i]);
  591. UA_free(config->networkLayers);
  592. config->networkLayers = NULL;
  593. config->networkLayersSize = 0;
  594. UA_String_deleteMembers(&config->customHostname);
  595. config->customHostname = UA_STRING_NULL;
  596. for(size_t i = 0; i < config->securityPoliciesSize; ++i) {
  597. UA_SecurityPolicy *policy = &config->securityPolicies[i];
  598. policy->deleteMembers(policy);
  599. }
  600. UA_free(config->securityPolicies);
  601. config->securityPolicies = NULL;
  602. config->securityPoliciesSize = 0;
  603. for(size_t i = 0; i < config->endpointsSize; ++i)
  604. UA_EndpointDescription_deleteMembers(&config->endpoints[i]);
  605. UA_free(config->endpoints);
  606. config->endpoints = NULL;
  607. config->endpointsSize = 0;
  608. /* Certificate Validation */
  609. config->certificateVerification.deleteMembers(&config->certificateVerification);
  610. /* Access Control */
  611. config->accessControl.deleteMembers(&config->accessControl);
  612. /* Historical data */
  613. #ifdef UA_ENABLE_HISTORIZING
  614. if(config->historyDatabase.deleteMembers)
  615. config->historyDatabase.deleteMembers(&config->historyDatabase);
  616. #endif
  617. /* Logger */
  618. if(config->logger.clear)
  619. config->logger.clear(config->logger.context);
  620. UA_free(config);
  621. }
  622. #ifdef UA_ENABLE_PUBSUB /* conditional compilation */
  623. /**
  624. * Add a pubsubTransportLayer to the configuration.
  625. * Memory is reallocated on demand */
  626. UA_StatusCode
  627. UA_ServerConfig_addPubSubTransportLayer(UA_ServerConfig *config,
  628. UA_PubSubTransportLayer *pubsubTransportLayer) {
  629. if(config->pubsubTransportLayersSize == 0) {
  630. config->pubsubTransportLayers = (UA_PubSubTransportLayer *)
  631. UA_malloc(sizeof(UA_PubSubTransportLayer));
  632. } else {
  633. config->pubsubTransportLayers = (UA_PubSubTransportLayer*)
  634. UA_realloc(config->pubsubTransportLayers,
  635. sizeof(UA_PubSubTransportLayer) * (config->pubsubTransportLayersSize + 1));
  636. }
  637. if (config->pubsubTransportLayers == NULL) {
  638. return UA_STATUSCODE_BADOUTOFMEMORY;
  639. }
  640. memcpy(&config->pubsubTransportLayers[config->pubsubTransportLayersSize],
  641. pubsubTransportLayer, sizeof(UA_PubSubTransportLayer));
  642. config->pubsubTransportLayersSize++;
  643. return UA_STATUSCODE_GOOD;
  644. }
  645. #endif /* UA_ENABLE_PUBSUB */
  646. /***************************/
  647. /* Default Client Settings */
  648. /***************************/
  649. static UA_INLINE void UA_ClientConnectionTCP_poll_callback(UA_Client *client, void *data) {
  650. UA_ClientConnectionTCP_poll(client, data);
  651. }
  652. UA_StatusCode
  653. UA_ClientConfig_setDefault(UA_ClientConfig *config) {
  654. config->timeout = 5000;
  655. config->secureChannelLifeTime = 10 * 60 * 1000; /* 10 minutes */
  656. config->logger.log = UA_Log_Stdout_log;
  657. config->logger.context = NULL;
  658. config->logger.clear = UA_Log_Stdout_clear;
  659. config->localConnectionConfig = UA_ConnectionConfig_default;
  660. /* Certificate Verification that accepts every certificate. Can be
  661. * overwritten when the policy is specialized. */
  662. UA_CertificateVerification_AcceptAll(&config->certificateVerification);
  663. /* With encryption enabled, the applicationUri needs to match the URI from
  664. * the certificate */
  665. config->clientDescription.applicationUri = UA_STRING_ALLOC(APPLICATION_URI);
  666. config->clientDescription.applicationType = UA_APPLICATIONTYPE_CLIENT;
  667. if(config->securityPoliciesSize > 0) {
  668. UA_LOG_ERROR(&config->logger, UA_LOGCATEGORY_NETWORK,
  669. "Could not initialize a config that already has SecurityPolicies");
  670. return UA_STATUSCODE_BADINTERNALERROR;
  671. }
  672. config->securityPolicies = (UA_SecurityPolicy*)malloc(sizeof(UA_SecurityPolicy));
  673. if(!config->securityPolicies)
  674. return UA_STATUSCODE_BADOUTOFMEMORY;
  675. UA_StatusCode retval = UA_SecurityPolicy_None(config->securityPolicies, NULL,
  676. UA_BYTESTRING_NULL, &config->logger);
  677. if(retval != UA_STATUSCODE_GOOD) {
  678. free(config->securityPolicies);
  679. config->securityPolicies = NULL;
  680. return retval;
  681. }
  682. config->securityPoliciesSize = 1;
  683. config->connectionFunc = UA_ClientConnectionTCP;
  684. config->initConnectionFunc = UA_ClientConnectionTCP_init; /* for async client */
  685. config->pollConnectionFunc = UA_ClientConnectionTCP_poll_callback; /* for async connection */
  686. config->customDataTypes = NULL;
  687. config->stateCallback = NULL;
  688. config->connectivityCheckInterval = 0;
  689. config->requestedSessionTimeout = 1200000; /* requestedSessionTimeout */
  690. config->inactivityCallback = NULL;
  691. config->clientContext = NULL;
  692. #ifdef UA_ENABLE_SUBSCRIPTIONS
  693. config->outStandingPublishRequests = 10;
  694. config->subscriptionInactivityCallback = NULL;
  695. #endif
  696. return UA_STATUSCODE_GOOD;
  697. }
  698. #ifdef UA_ENABLE_ENCRYPTION
  699. UA_StatusCode
  700. UA_ClientConfig_setDefaultEncryption(UA_ClientConfig *config,
  701. UA_ByteString localCertificate, UA_ByteString privateKey,
  702. const UA_ByteString *trustList, size_t trustListSize,
  703. const UA_ByteString *revocationList, size_t revocationListSize) {
  704. UA_StatusCode retval = UA_ClientConfig_setDefault(config);
  705. if(retval != UA_STATUSCODE_GOOD)
  706. return retval;
  707. retval = UA_CertificateVerification_Trustlist(&config->certificateVerification,
  708. trustList, trustListSize,
  709. revocationList, revocationListSize);
  710. if(retval != UA_STATUSCODE_GOOD)
  711. return retval;
  712. /* Populate SecurityPolicies */
  713. UA_SecurityPolicy *sp = (UA_SecurityPolicy*)
  714. realloc(config->securityPolicies, sizeof(UA_SecurityPolicy) * 4);
  715. if(!sp)
  716. return UA_STATUSCODE_BADOUTOFMEMORY;
  717. config->securityPolicies = sp;
  718. retval = UA_SecurityPolicy_Basic128Rsa15(&config->securityPolicies[1],
  719. &config->certificateVerification,
  720. localCertificate, privateKey, &config->logger);
  721. if(retval != UA_STATUSCODE_GOOD)
  722. return retval;
  723. ++config->securityPoliciesSize;
  724. retval = UA_SecurityPolicy_Basic256(&config->securityPolicies[2],
  725. &config->certificateVerification,
  726. localCertificate, privateKey, &config->logger);
  727. if(retval != UA_STATUSCODE_GOOD)
  728. return retval;
  729. ++config->securityPoliciesSize;
  730. retval = UA_SecurityPolicy_Basic256Sha256(&config->securityPolicies[3],
  731. &config->certificateVerification,
  732. localCertificate, privateKey, &config->logger);
  733. if(retval != UA_STATUSCODE_GOOD)
  734. return retval;
  735. ++config->securityPoliciesSize;
  736. return UA_STATUSCODE_GOOD;
  737. }
  738. #endif