ua_config_default.c 26 KB

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