ua_config_default.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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) Julius Pfrommer, Fraunhofer IOSB
  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. */
  10. #include "ua_plugin_securitypolicy.h"
  11. #include "ua_config_default.h"
  12. #include "ua_log_stdout.h"
  13. #include "ua_network_tcp.h"
  14. #include "ua_accesscontrol_default.h"
  15. #include "ua_pki_certificate.h"
  16. #include "ua_nodestore_default.h"
  17. #include "ua_types_generated.h"
  18. #include "ua_securitypolicy_none.h"
  19. #ifdef UA_ENABLE_ENCRYPTION
  20. #include "ua_securitypolicy_basic128rsa15.h"
  21. #endif
  22. #include "ua_types.h"
  23. #include "ua_types_generated_handling.h"
  24. #include "ua_client_subscriptions.h"
  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. #endif
  125. const size_t usernamePasswordsSize = 2;
  126. UA_UsernamePasswordLogin usernamePasswords[2] = {
  127. { UA_STRING_STATIC("user1"), UA_STRING_STATIC("password") },
  128. { UA_STRING_STATIC("user2"), UA_STRING_STATIC("password1") } };
  129. static UA_ServerConfig *
  130. createDefaultConfig(void) {
  131. UA_ServerConfig *conf = (UA_ServerConfig *) UA_malloc(sizeof(UA_ServerConfig));
  132. if(!conf)
  133. return NULL;
  134. /* Zero out.. All members have a valid initial value */
  135. memset(conf, 0, sizeof(UA_ServerConfig));
  136. /* --> Start setting the default static config <-- */
  137. conf->nThreads = 1;
  138. conf->logger = UA_Log_Stdout;
  139. /* Server Description */
  140. conf->buildInfo.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  141. conf->buildInfo.manufacturerName = UA_STRING_ALLOC(MANUFACTURER_NAME);
  142. conf->buildInfo.productName = UA_STRING_ALLOC(PRODUCT_NAME);
  143. conf->buildInfo.softwareVersion =
  144. UA_STRING_ALLOC(VERSION(UA_OPEN62541_VER_MAJOR, UA_OPEN62541_VER_MINOR,
  145. UA_OPEN62541_VER_PATCH, UA_OPEN62541_VER_LABEL));
  146. conf->buildInfo.buildNumber = UA_STRING_ALLOC(__DATE__ " " __TIME__);
  147. conf->buildInfo.buildDate = 0;
  148. conf->applicationDescription.applicationUri = UA_STRING_ALLOC(APPLICATION_URI);
  149. conf->applicationDescription.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  150. conf->applicationDescription.applicationName =
  151. UA_LOCALIZEDTEXT_ALLOC("en", APPLICATION_NAME);
  152. conf->applicationDescription.applicationType = UA_APPLICATIONTYPE_SERVER;
  153. /* conf->applicationDescription.gatewayServerUri = UA_STRING_NULL; */
  154. /* conf->applicationDescription.discoveryProfileUri = UA_STRING_NULL; */
  155. /* conf->applicationDescription.discoveryUrlsSize = 0; */
  156. /* conf->applicationDescription.discoveryUrls = NULL; */
  157. #ifdef UA_ENABLE_DISCOVERY
  158. /* conf->mdnsServerName = UA_STRING_NULL; */
  159. /* conf->serverCapabilitiesSize = 0; */
  160. /* conf->serverCapabilities = NULL; */
  161. #endif
  162. /* Custom DataTypes */
  163. /* conf->customDataTypesSize = 0; */
  164. /* conf->customDataTypes = NULL; */
  165. /* Networking */
  166. /* conf->networkLayersSize = 0; */
  167. /* conf->networkLayers = NULL; */
  168. /* conf->customHostname = UA_STRING_NULL; */
  169. /* Endpoints */
  170. /* conf->endpoints = {0, NULL}; */
  171. /* Certificate Verification that accepts every certificate. Can be
  172. * overwritten when the policy is specialized. */
  173. UA_CertificateVerification_AcceptAll(&conf->certificateVerification);
  174. /* Global Node Lifecycle */
  175. conf->nodeLifecycle.constructor = NULL;
  176. conf->nodeLifecycle.destructor = NULL;
  177. /* Access Control. Anonymous Login only. */
  178. conf->accessControl = UA_AccessControl_default(true, usernamePasswordsSize, usernamePasswords);
  179. /* Limits for SecureChannels */
  180. conf->maxSecureChannels = 40;
  181. conf->maxSecurityTokenLifetime = 10 * 60 * 1000; /* 10 minutes */
  182. /* Limits for Sessions */
  183. conf->maxSessions = 100;
  184. conf->maxSessionTimeout = 60.0 * 60.0 * 1000.0; /* 1h */
  185. /* Limits for Subscriptions */
  186. conf->publishingIntervalLimits = UA_DURATIONRANGE(100.0, 3600.0 * 1000.0);
  187. conf->lifeTimeCountLimits = UA_UINT32RANGE(3, 15000);
  188. conf->keepAliveCountLimits = UA_UINT32RANGE(1, 100);
  189. conf->maxNotificationsPerPublish = 1000;
  190. conf->maxRetransmissionQueueSize = 0; /* unlimited */
  191. /* Limits for MonitoredItems */
  192. conf->samplingIntervalLimits = UA_DURATIONRANGE(50.0, 24.0 * 3600.0 * 1000.0);
  193. conf->queueSizeLimits = UA_UINT32RANGE(1, 100);
  194. #ifdef UA_ENABLE_DISCOVERY
  195. conf->discoveryCleanupTimeout = 60 * 60;
  196. #endif
  197. /* --> Finish setting the default static config <-- */
  198. return conf;
  199. }
  200. static UA_StatusCode
  201. addDefaultNetworkLayers(UA_ServerConfig *conf, UA_UInt16 portNumber) {
  202. /* Add a network layer */
  203. conf->networkLayers = (UA_ServerNetworkLayer *)
  204. UA_malloc(sizeof(UA_ServerNetworkLayer));
  205. if(!conf->networkLayers)
  206. return UA_STATUSCODE_BADOUTOFMEMORY;
  207. conf->networkLayers[0] =
  208. UA_ServerNetworkLayerTCP(UA_ConnectionConfig_default, portNumber, conf->logger);
  209. conf->networkLayersSize = 1;
  210. return UA_STATUSCODE_GOOD;
  211. }
  212. UA_ServerConfig *
  213. UA_ServerConfig_new_minimal(UA_UInt16 portNumber,
  214. const UA_ByteString *certificate) {
  215. UA_ServerConfig *conf = createDefaultConfig();
  216. UA_StatusCode retval = UA_Nodestore_default_new(&conf->nodestore);
  217. if(retval != UA_STATUSCODE_GOOD) {
  218. UA_ServerConfig_delete(conf);
  219. return NULL;
  220. }
  221. if(addDefaultNetworkLayers(conf, portNumber) != UA_STATUSCODE_GOOD) {
  222. UA_ServerConfig_delete(conf);
  223. return NULL;
  224. }
  225. /* Allocate the endpoint */
  226. conf->endpointsSize = 1;
  227. conf->endpoints = (UA_Endpoint *) UA_malloc(sizeof(UA_Endpoint));
  228. if(!conf->endpoints) {
  229. UA_ServerConfig_delete(conf);
  230. return NULL;
  231. }
  232. /* Populate the endpoint */
  233. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  234. if(certificate)
  235. localCertificate = *certificate;
  236. retval =
  237. createSecurityPolicyNoneEndpoint(conf, &conf->endpoints[0], localCertificate);
  238. if(retval != UA_STATUSCODE_GOOD) {
  239. UA_ServerConfig_delete(conf);
  240. return NULL;
  241. }
  242. return conf;
  243. }
  244. #ifdef UA_ENABLE_ENCRYPTION
  245. UA_ServerConfig *
  246. UA_ServerConfig_new_basic128rsa15(UA_UInt16 portNumber,
  247. const UA_ByteString *certificate,
  248. const UA_ByteString *privateKey,
  249. const UA_ByteString *trustList,
  250. size_t trustListSize,
  251. const UA_ByteString *revocationList,
  252. size_t revocationListSize) {
  253. UA_ServerConfig *conf = createDefaultConfig();
  254. UA_StatusCode retval = UA_CertificateVerification_Trustlist(&conf->certificateVerification,
  255. trustList, trustListSize,
  256. revocationList, revocationListSize);
  257. if(retval != UA_STATUSCODE_GOOD) {
  258. UA_ServerConfig_delete(conf);
  259. return NULL;
  260. }
  261. retval = UA_Nodestore_default_new(&conf->nodestore);
  262. if(retval != UA_STATUSCODE_GOOD) {
  263. UA_ServerConfig_delete(conf);
  264. return NULL;
  265. }
  266. if(addDefaultNetworkLayers(conf, portNumber) != UA_STATUSCODE_GOOD) {
  267. UA_ServerConfig_delete(conf);
  268. return NULL;
  269. }
  270. if(trustListSize == 0)
  271. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  272. "No CA trust-list provided. Any remote certificate will be accepted.");
  273. /* Allocate the endpoints */
  274. conf->endpointsSize = 0;
  275. conf->endpoints = (UA_Endpoint *) UA_malloc(sizeof(UA_Endpoint) * 3);
  276. if(!conf->endpoints) {
  277. UA_ServerConfig_delete(conf);
  278. return NULL;
  279. }
  280. /* Populate the endpoints */
  281. ++conf->endpointsSize;
  282. retval = createSecurityPolicyNoneEndpoint(conf, &conf->endpoints[0], *certificate);
  283. if(retval != UA_STATUSCODE_GOOD) {
  284. UA_ServerConfig_delete(conf);
  285. return NULL;
  286. }
  287. ++conf->endpointsSize;
  288. retval = createSecurityPolicyBasic128Rsa15Endpoint(conf, &conf->endpoints[1],
  289. UA_MESSAGESECURITYMODE_SIGN, *certificate,
  290. *privateKey);
  291. if(retval != UA_STATUSCODE_GOOD) {
  292. UA_ServerConfig_delete(conf);
  293. return NULL;
  294. }
  295. ++conf->endpointsSize;
  296. retval = createSecurityPolicyBasic128Rsa15Endpoint(conf, &conf->endpoints[2],
  297. UA_MESSAGESECURITYMODE_SIGNANDENCRYPT, *certificate,
  298. *privateKey);
  299. if(retval != UA_STATUSCODE_GOOD) {
  300. UA_ServerConfig_delete(conf);
  301. return NULL;
  302. }
  303. return conf;
  304. }
  305. #endif
  306. void
  307. UA_ServerConfig_delete(UA_ServerConfig *config) {
  308. if(!config)
  309. return;
  310. /* Server Description */
  311. UA_BuildInfo_deleteMembers(&config->buildInfo);
  312. UA_ApplicationDescription_deleteMembers(&config->applicationDescription);
  313. #ifdef UA_ENABLE_DISCOVERY
  314. UA_String_deleteMembers(&config->mdnsServerName);
  315. UA_Array_delete(config->serverCapabilities, config->serverCapabilitiesSize,
  316. &UA_TYPES[UA_TYPES_STRING]);
  317. config->serverCapabilities = NULL;
  318. config->serverCapabilitiesSize = 0;
  319. #endif
  320. /* Nodestore */
  321. if(config->nodestore.deleteNodestore)
  322. config->nodestore.deleteNodestore(config->nodestore.context);
  323. /* Custom DataTypes */
  324. for(size_t i = 0; i < config->customDataTypesSize; ++i)
  325. UA_free(config->customDataTypes[i].members);
  326. UA_free(config->customDataTypes);
  327. config->customDataTypes = NULL;
  328. config->customDataTypesSize = 0;
  329. /* Networking */
  330. for(size_t i = 0; i < config->networkLayersSize; ++i)
  331. config->networkLayers[i].deleteMembers(&config->networkLayers[i]);
  332. UA_free(config->networkLayers);
  333. config->networkLayers = NULL;
  334. config->networkLayersSize = 0;
  335. UA_String_deleteMembers(&config->customHostname);
  336. config->customHostname = UA_STRING_NULL;
  337. for(size_t i = 0; i < config->endpointsSize; ++i) {
  338. UA_SecurityPolicy *policy = &config->endpoints[i].securityPolicy;
  339. policy->deleteMembers(policy);
  340. UA_EndpointDescription_deleteMembers(&config->endpoints[i].endpointDescription);
  341. }
  342. UA_free(config->endpoints);
  343. config->endpoints = NULL;
  344. config->endpointsSize = 0;
  345. /* Certificate Validation */
  346. config->certificateVerification.deleteMembers(&config->certificateVerification);
  347. /* Access Control */
  348. config->accessControl.deleteMembers(&config->accessControl);
  349. UA_free(config);
  350. }
  351. /***************************/
  352. /* Default Client Settings */
  353. /***************************/
  354. const UA_ClientConfig UA_ClientConfig_default = {
  355. 5000, /* .timeout, 5 seconds */
  356. 10 * 60 * 1000, /* .secureChannelLifeTime, 10 minutes */
  357. UA_Log_Stdout, /* .logger */
  358. { /* .localConnectionConfig */
  359. 0, /* .protocolVersion */
  360. 65535, /* .sendBufferSize, 64k per chunk */
  361. 65535, /* .recvBufferSize, 64k per chunk */
  362. 0, /* .maxMessageSize, 0 -> unlimited */
  363. 0 /* .maxChunkCount, 0 -> unlimited */
  364. },
  365. UA_ClientConnectionTCP, /* .connectionFunc */
  366. 0, /* .customDataTypesSize */
  367. NULL, /*.customDataTypes */
  368. NULL, /*.stateCallback */
  369. NULL, /*.subscriptionInactivityCallback */
  370. NULL, /*.clientContext */
  371. 10 /* .outStandingPublishRequests */
  372. };