ua_config_default.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. #include "ua_plugin_securitypolicy.h"
  4. #include "ua_config_default.h"
  5. #include "ua_log_stdout.h"
  6. #include "ua_network_tcp.h"
  7. #include "ua_accesscontrol_default.h"
  8. #include "ua_pki_certificate.h"
  9. #include "ua_nodestore_default.h"
  10. #include "ua_types_generated.h"
  11. #include "ua_securitypolicy_none.h"
  12. #ifdef UA_ENABLE_ENCRYPTION
  13. #include "ua_securitypolicy_basic128rsa15.h"
  14. #endif
  15. #include "ua_types.h"
  16. #include "ua_types_generated_handling.h"
  17. #include "ua_client_highlevel.h"
  18. #define ANONYMOUS_POLICY "open62541-anonymous-policy"
  19. #define USERNAME_POLICY "open62541-username-policy"
  20. /* Struct initialization works across ANSI C/C99/C++ if it is done when the
  21. * variable is first declared. Assigning values to existing structs is
  22. * heterogeneous across the three. */
  23. static UA_INLINE UA_UInt32Range
  24. UA_UINT32RANGE(UA_UInt32 min, UA_UInt32 max) {
  25. UA_UInt32Range range = {min, max};
  26. return range;
  27. }
  28. static UA_INLINE UA_DurationRange
  29. UA_DURATIONRANGE(UA_Double min, UA_Double max) {
  30. UA_DurationRange range = {min, max};
  31. return range;
  32. }
  33. /*******************************/
  34. /* Default Connection Settings */
  35. /*******************************/
  36. const UA_ConnectionConfig UA_ConnectionConfig_default = {
  37. 0, /* .protocolVersion */
  38. 65535, /* .sendBufferSize, 64k per chunk */
  39. 65535, /* .recvBufferSize, 64k per chunk */
  40. 0, /* .maxMessageSize, 0 -> unlimited */
  41. 0 /* .maxChunkCount, 0 -> unlimited */
  42. };
  43. /***************************/
  44. /* Default Server Settings */
  45. /***************************/
  46. #define MANUFACTURER_NAME "open62541"
  47. #define PRODUCT_NAME "open62541 OPC UA Server"
  48. #define PRODUCT_URI "http://open62541.org"
  49. #define APPLICATION_NAME "open62541-based OPC UA Application"
  50. #define APPLICATION_URI "urn:unconfigured:application"
  51. #define STRINGIFY(arg) #arg
  52. #define VERSION(MAJOR, MINOR, PATCH, LABEL) \
  53. STRINGIFY(MAJOR) "." STRINGIFY(MINOR) "." STRINGIFY(PATCH) LABEL
  54. static UA_StatusCode
  55. createSecurityPolicyNoneEndpoint(UA_ServerConfig *conf, UA_Endpoint *endpoint,
  56. const UA_ByteString localCertificate) {
  57. UA_EndpointDescription_init(&endpoint->endpointDescription);
  58. UA_SecurityPolicy_None(&endpoint->securityPolicy, localCertificate, conf->logger);
  59. endpoint->endpointDescription.securityMode = UA_MESSAGESECURITYMODE_NONE;
  60. endpoint->endpointDescription.securityPolicyUri =
  61. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#None");
  62. endpoint->endpointDescription.transportProfileUri =
  63. UA_STRING_ALLOC("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary");
  64. /* enable anonymous and username/password */
  65. size_t policies = 2;
  66. endpoint->endpointDescription.userIdentityTokens = (UA_UserTokenPolicy *)
  67. UA_Array_new(policies, &UA_TYPES[UA_TYPES_USERTOKENPOLICY]);
  68. if(!endpoint->endpointDescription.userIdentityTokens)
  69. return UA_STATUSCODE_BADOUTOFMEMORY;
  70. endpoint->endpointDescription.userIdentityTokensSize = policies;
  71. endpoint->endpointDescription.userIdentityTokens[0].tokenType =
  72. UA_USERTOKENTYPE_ANONYMOUS;
  73. endpoint->endpointDescription.userIdentityTokens[0].policyId =
  74. UA_STRING_ALLOC(ANONYMOUS_POLICY);
  75. endpoint->endpointDescription.userIdentityTokens[1].tokenType =
  76. UA_USERTOKENTYPE_USERNAME;
  77. endpoint->endpointDescription.userIdentityTokens[1].policyId =
  78. UA_STRING_ALLOC(USERNAME_POLICY);
  79. UA_String_copy(&localCertificate, &endpoint->endpointDescription.serverCertificate);
  80. UA_ApplicationDescription_copy(&conf->applicationDescription,
  81. &endpoint->endpointDescription.server);
  82. return UA_STATUSCODE_GOOD;
  83. }
  84. void
  85. UA_ServerConfig_set_customHostname(UA_ServerConfig *config, const UA_String customHostname) {
  86. if(!config)
  87. return;
  88. UA_String_deleteMembers(&config->customHostname);
  89. UA_String_copy(&customHostname, &config->customHostname);
  90. }
  91. #ifdef UA_ENABLE_ENCRYPTION
  92. static UA_StatusCode
  93. createSecurityPolicyBasic128Rsa15Endpoint(UA_ServerConfig *const conf,
  94. UA_Endpoint *endpoint,
  95. UA_MessageSecurityMode securityMode,
  96. const UA_ByteString localCertificate,
  97. const UA_ByteString localPrivateKey) {
  98. UA_EndpointDescription_init(&endpoint->endpointDescription);
  99. UA_StatusCode retval =
  100. UA_SecurityPolicy_Basic128Rsa15(&endpoint->securityPolicy, localCertificate,
  101. localPrivateKey, conf->logger);
  102. if(retval != UA_STATUSCODE_GOOD) {
  103. endpoint->securityPolicy.deleteMembers(&endpoint->securityPolicy);
  104. return retval;
  105. }
  106. endpoint->endpointDescription.securityMode = securityMode;
  107. endpoint->endpointDescription.securityPolicyUri =
  108. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15");
  109. endpoint->endpointDescription.transportProfileUri =
  110. UA_STRING_ALLOC("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary");
  111. /* enable anonymous and username/password */
  112. size_t policies = 1;
  113. endpoint->endpointDescription.userIdentityTokens = (UA_UserTokenPolicy *)
  114. UA_Array_new(policies, &UA_TYPES[UA_TYPES_USERTOKENPOLICY]);
  115. if(!endpoint->endpointDescription.userIdentityTokens)
  116. return UA_STATUSCODE_BADOUTOFMEMORY;
  117. endpoint->endpointDescription.userIdentityTokensSize = policies;
  118. endpoint->endpointDescription.userIdentityTokens[0].tokenType =
  119. UA_USERTOKENTYPE_ANONYMOUS;
  120. endpoint->endpointDescription.userIdentityTokens[0].policyId =
  121. UA_STRING_ALLOC(ANONYMOUS_POLICY);
  122. /*
  123. endpoint->endpointDescription.userIdentityTokens[1].tokenType =
  124. UA_USERTOKENTYPE_USERNAME;
  125. endpoint->endpointDescription.userIdentityTokens[1].policyId =
  126. UA_STRING_ALLOC(USERNAME_POLICY);*/
  127. UA_String_copy(&localCertificate, &endpoint->endpointDescription.serverCertificate);
  128. UA_ApplicationDescription_copy(&conf->applicationDescription,
  129. &endpoint->endpointDescription.server);
  130. return UA_STATUSCODE_GOOD;
  131. }
  132. #endif
  133. static UA_ServerConfig *
  134. createDefaultConfig(void) {
  135. UA_ServerConfig *conf = (UA_ServerConfig *) UA_malloc(sizeof(UA_ServerConfig));
  136. if(!conf)
  137. return NULL;
  138. /* Zero out.. All members have a valid initial value */
  139. memset(conf, 0, sizeof(UA_ServerConfig));
  140. /* --> Start setting the default static config <-- */
  141. conf->nThreads = 1;
  142. conf->logger = UA_Log_Stdout;
  143. /* Server Description */
  144. conf->buildInfo.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  145. conf->buildInfo.manufacturerName = UA_STRING_ALLOC(MANUFACTURER_NAME);
  146. conf->buildInfo.productName = UA_STRING_ALLOC(PRODUCT_NAME);
  147. conf->buildInfo.softwareVersion =
  148. UA_STRING_ALLOC(VERSION(UA_OPEN62541_VER_MAJOR, UA_OPEN62541_VER_MINOR,
  149. UA_OPEN62541_VER_PATCH, UA_OPEN62541_VER_LABEL));
  150. conf->buildInfo.buildNumber = UA_STRING_ALLOC(__DATE__ " " __TIME__);
  151. conf->buildInfo.buildDate = 0;
  152. conf->applicationDescription.applicationUri = UA_STRING_ALLOC(APPLICATION_URI);
  153. conf->applicationDescription.productUri = UA_STRING_ALLOC(PRODUCT_URI);
  154. conf->applicationDescription.applicationName =
  155. UA_LOCALIZEDTEXT_ALLOC("en", APPLICATION_NAME);
  156. conf->applicationDescription.applicationType = UA_APPLICATIONTYPE_SERVER;
  157. /* conf->applicationDescription.gatewayServerUri = UA_STRING_NULL; */
  158. /* conf->applicationDescription.discoveryProfileUri = UA_STRING_NULL; */
  159. /* conf->applicationDescription.discoveryUrlsSize = 0; */
  160. /* conf->applicationDescription.discoveryUrls = NULL; */
  161. #ifdef UA_ENABLE_DISCOVERY
  162. /* conf->mdnsServerName = UA_STRING_NULL; */
  163. /* conf->serverCapabilitiesSize = 0; */
  164. /* conf->serverCapabilities = NULL; */
  165. #endif
  166. /* Custom DataTypes */
  167. /* conf->customDataTypesSize = 0; */
  168. /* conf->customDataTypes = NULL; */
  169. /* Networking */
  170. /* conf->networkLayersSize = 0; */
  171. /* conf->networkLayers = NULL; */
  172. /* conf->customHostname = UA_STRING_NULL; */
  173. /* Endpoints */
  174. /* conf->endpoints = {0, NULL}; */
  175. /* Certificate Verification that accepts every certificate. Can be
  176. * overwritten when the policy is specialized. */
  177. UA_CertificateVerification_AcceptAll(&conf->certificateVerification);
  178. /* Global Node Lifecycle */
  179. conf->nodeLifecycle.constructor = NULL;
  180. conf->nodeLifecycle.destructor = NULL;
  181. /* Access Control */
  182. conf->accessControl.enableAnonymousLogin = true;
  183. conf->accessControl.enableUsernamePasswordLogin = true;
  184. conf->accessControl.activateSession = activateSession_default;
  185. conf->accessControl.closeSession = closeSession_default;
  186. conf->accessControl.getUserRightsMask = getUserRightsMask_default;
  187. conf->accessControl.getUserAccessLevel = getUserAccessLevel_default;
  188. conf->accessControl.getUserExecutable = getUserExecutable_default;
  189. conf->accessControl.getUserExecutableOnObject = getUserExecutableOnObject_default;
  190. conf->accessControl.allowAddNode = allowAddNode_default;
  191. conf->accessControl.allowAddReference = allowAddReference_default;
  192. conf->accessControl.allowDeleteNode = allowDeleteNode_default;
  193. conf->accessControl.allowDeleteReference = allowDeleteReference_default;
  194. /* Limits for SecureChannels */
  195. conf->maxSecureChannels = 40;
  196. conf->maxSecurityTokenLifetime = 10 * 60 * 1000; /* 10 minutes */
  197. /* Limits for Sessions */
  198. conf->maxSessions = 100;
  199. conf->maxSessionTimeout = 60.0 * 60.0 * 1000.0; /* 1h */
  200. /* Limits for Subscriptions */
  201. conf->publishingIntervalLimits = UA_DURATIONRANGE(100.0, 3600.0 * 1000.0);
  202. conf->lifeTimeCountLimits = UA_UINT32RANGE(3, 15000);
  203. conf->keepAliveCountLimits = UA_UINT32RANGE(1, 100);
  204. conf->maxNotificationsPerPublish = 1000;
  205. conf->maxRetransmissionQueueSize = 0; /* unlimited */
  206. /* Limits for MonitoredItems */
  207. conf->samplingIntervalLimits = UA_DURATIONRANGE(50.0, 24.0 * 3600.0 * 1000.0);
  208. conf->queueSizeLimits = UA_UINT32RANGE(1, 100);
  209. #ifdef UA_ENABLE_DISCOVERY
  210. conf->discoveryCleanupTimeout = 60 * 60;
  211. #endif
  212. /* --> Finish setting the default static config <-- */
  213. return conf;
  214. }
  215. static UA_StatusCode
  216. addDefaultNetworkLayers(UA_ServerConfig *conf, UA_UInt16 portNumber) {
  217. /* Add a network layer */
  218. conf->networkLayers = (UA_ServerNetworkLayer *)
  219. UA_malloc(sizeof(UA_ServerNetworkLayer));
  220. if(!conf->networkLayers)
  221. return UA_STATUSCODE_BADOUTOFMEMORY;
  222. conf->networkLayers[0] =
  223. UA_ServerNetworkLayerTCP(UA_ConnectionConfig_default, portNumber);
  224. conf->networkLayersSize = 1;
  225. return UA_STATUSCODE_GOOD;
  226. }
  227. UA_ServerConfig *
  228. UA_ServerConfig_new_minimal(UA_UInt16 portNumber,
  229. const UA_ByteString *certificate) {
  230. UA_ServerConfig *conf = createDefaultConfig();
  231. UA_StatusCode retval = UA_Nodestore_default_new(&conf->nodestore);
  232. if(retval != UA_STATUSCODE_GOOD) {
  233. UA_ServerConfig_delete(conf);
  234. return NULL;
  235. }
  236. if(addDefaultNetworkLayers(conf, portNumber) != UA_STATUSCODE_GOOD) {
  237. UA_ServerConfig_delete(conf);
  238. return NULL;
  239. }
  240. /* Allocate the endpoint */
  241. conf->endpointsSize = 1;
  242. conf->endpoints = (UA_Endpoint *) UA_malloc(sizeof(UA_Endpoint));
  243. if(!conf->endpoints) {
  244. UA_ServerConfig_delete(conf);
  245. return NULL;
  246. }
  247. /* Populate the endpoint */
  248. UA_ByteString localCertificate = UA_BYTESTRING_NULL;
  249. if(certificate)
  250. localCertificate = *certificate;
  251. retval =
  252. createSecurityPolicyNoneEndpoint(conf, &conf->endpoints[0], localCertificate);
  253. if(retval != UA_STATUSCODE_GOOD) {
  254. UA_ServerConfig_delete(conf);
  255. return NULL;
  256. }
  257. return conf;
  258. }
  259. #ifdef UA_ENABLE_ENCRYPTION
  260. UA_ServerConfig *
  261. UA_ServerConfig_new_basic128rsa15(UA_UInt16 portNumber,
  262. const UA_ByteString *certificate,
  263. const UA_ByteString *privateKey,
  264. const UA_ByteString *trustList,
  265. size_t trustListSize,
  266. const UA_ByteString *revocationList,
  267. size_t revocationListSize) {
  268. UA_ServerConfig *conf = createDefaultConfig();
  269. UA_StatusCode retval = UA_CertificateVerification_Trustlist(&conf->certificateVerification,
  270. trustList, trustListSize,
  271. revocationList, revocationListSize);
  272. if(retval != UA_STATUSCODE_GOOD) {
  273. UA_ServerConfig_delete(conf);
  274. return NULL;
  275. }
  276. retval = UA_Nodestore_default_new(&conf->nodestore);
  277. if(retval != UA_STATUSCODE_GOOD) {
  278. UA_ServerConfig_delete(conf);
  279. return NULL;
  280. }
  281. if(addDefaultNetworkLayers(conf, portNumber) != UA_STATUSCODE_GOOD) {
  282. UA_ServerConfig_delete(conf);
  283. return NULL;
  284. }
  285. if(trustListSize == 0)
  286. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  287. "No CA trust-list provided. Any remote certificate will be accepted.");
  288. /* Allocate the endpoints */
  289. conf->endpointsSize = 0;
  290. conf->endpoints = (UA_Endpoint *) UA_malloc(sizeof(UA_Endpoint) * 3);
  291. if(!conf->endpoints) {
  292. UA_ServerConfig_delete(conf);
  293. return NULL;
  294. }
  295. /* Populate the endpoints */
  296. ++conf->endpointsSize;
  297. retval = createSecurityPolicyNoneEndpoint(conf, &conf->endpoints[0], *certificate);
  298. if(retval != UA_STATUSCODE_GOOD) {
  299. UA_ServerConfig_delete(conf);
  300. return NULL;
  301. }
  302. ++conf->endpointsSize;
  303. retval = createSecurityPolicyBasic128Rsa15Endpoint(conf, &conf->endpoints[1],
  304. UA_MESSAGESECURITYMODE_SIGN, *certificate,
  305. *privateKey);
  306. if(retval != UA_STATUSCODE_GOOD) {
  307. UA_ServerConfig_delete(conf);
  308. return NULL;
  309. }
  310. ++conf->endpointsSize;
  311. retval = createSecurityPolicyBasic128Rsa15Endpoint(conf, &conf->endpoints[2],
  312. UA_MESSAGESECURITYMODE_SIGNANDENCRYPT, *certificate,
  313. *privateKey);
  314. if(retval != UA_STATUSCODE_GOOD) {
  315. UA_ServerConfig_delete(conf);
  316. return NULL;
  317. }
  318. return conf;
  319. }
  320. #endif
  321. void
  322. UA_ServerConfig_delete(UA_ServerConfig *config) {
  323. if(!config)
  324. return;
  325. /* Server Description */
  326. UA_BuildInfo_deleteMembers(&config->buildInfo);
  327. UA_ApplicationDescription_deleteMembers(&config->applicationDescription);
  328. #ifdef UA_ENABLE_DISCOVERY
  329. UA_String_deleteMembers(&config->mdnsServerName);
  330. UA_Array_delete(config->serverCapabilities, config->serverCapabilitiesSize,
  331. &UA_TYPES[UA_TYPES_STRING]);
  332. config->serverCapabilities = NULL;
  333. config->serverCapabilitiesSize = 0;
  334. #endif
  335. /* Nodestore */
  336. if(config->nodestore.deleteNodestore)
  337. config->nodestore.deleteNodestore(config->nodestore.context);
  338. /* Custom DataTypes */
  339. for(size_t i = 0; i < config->customDataTypesSize; ++i)
  340. UA_free(config->customDataTypes[i].members);
  341. UA_free(config->customDataTypes);
  342. config->customDataTypes = NULL;
  343. config->customDataTypesSize = 0;
  344. /* Networking */
  345. for(size_t i = 0; i < config->networkLayersSize; ++i)
  346. config->networkLayers[i].deleteMembers(&config->networkLayers[i]);
  347. UA_free(config->networkLayers);
  348. config->networkLayers = NULL;
  349. config->networkLayersSize = 0;
  350. UA_String_deleteMembers(&config->customHostname);
  351. config->customHostname = UA_STRING_NULL;
  352. for(size_t i = 0; i < config->endpointsSize; ++i) {
  353. UA_SecurityPolicy *policy = &config->endpoints[i].securityPolicy;
  354. policy->deleteMembers(policy);
  355. UA_EndpointDescription_deleteMembers(&config->endpoints[i].endpointDescription);
  356. }
  357. UA_free(config->endpoints);
  358. config->endpoints = NULL;
  359. config->endpointsSize = 0;
  360. /* Certificate Validation */
  361. config->certificateVerification.deleteMembers(&config->certificateVerification);
  362. UA_free(config);
  363. }
  364. /***************************/
  365. /* Default Client Settings */
  366. /***************************/
  367. const UA_ClientConfig UA_ClientConfig_default = {
  368. 5000, /* .timeout, 5 seconds */
  369. 10 * 60 * 1000, /* .secureChannelLifeTime, 10 minutes */
  370. UA_Log_Stdout, /* .logger */
  371. /* .localConnectionConfig */
  372. {0, /* .protocolVersion */
  373. 65535, /* .sendBufferSize, 64k per chunk */
  374. 65535, /* .recvBufferSize, 64k per chunk */
  375. 0, /* .maxMessageSize, 0 -> unlimited */
  376. 0}, /* .maxChunkCount, 0 -> unlimited */
  377. UA_ClientConnectionTCP, /* .connectionFunc */
  378. 0, /* .customDataTypesSize */
  379. NULL, /*.customDataTypes */
  380. NULL /*.stateCallback */
  381. };
  382. /****************************************/
  383. /* Default Client Subscription Settings */
  384. /****************************************/
  385. #ifdef UA_ENABLE_SUBSCRIPTIONS
  386. const UA_SubscriptionSettings UA_SubscriptionSettings_default = {
  387. 500.0, /* .requestedPublishingInterval */
  388. 10000, /* .requestedLifetimeCount */
  389. 1, /* .requestedMaxKeepAliveCount */
  390. 0, /* .maxNotificationsPerPublish */
  391. true, /* .publishingEnabled */
  392. 0 /* .priority */
  393. };
  394. #endif