ua_config_default.c 28 KB

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