ua_config_default.c 27 KB

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