ua_config_default.c 25 KB

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