ua_services_session.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2014-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2014-2017 (c) Florian Palm
  7. * Copyright 2014-2016 (c) Sten Grüner
  8. * Copyright 2015 (c) Chris Iatrou
  9. * Copyright 2015 (c) Oleksiy Vasylyev
  10. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  11. * Copyright 2017-2018 (c) Mark Giraud, Fraunhofer IOSB
  12. * Copyright 2019 (c) Kalycito Infotech Private Limited
  13. */
  14. #include "ua_services.h"
  15. #include "ua_server_internal.h"
  16. #include "ua_session_manager.h"
  17. static UA_StatusCode
  18. signCreateSessionResponse(UA_Server *server, UA_SecureChannel *channel,
  19. const UA_CreateSessionRequest *request,
  20. UA_CreateSessionResponse *response) {
  21. if(channel->securityMode != UA_MESSAGESECURITYMODE_SIGN &&
  22. channel->securityMode != UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  23. return UA_STATUSCODE_GOOD;
  24. const UA_SecurityPolicy *const securityPolicy = channel->securityPolicy;
  25. UA_SignatureData *signatureData = &response->serverSignature;
  26. /* Prepare the signature */
  27. size_t signatureSize = securityPolicy->certificateSigningAlgorithm.
  28. getLocalSignatureSize(securityPolicy, channel->channelContext);
  29. UA_StatusCode retval = UA_String_copy(&securityPolicy->certificateSigningAlgorithm.uri,
  30. &signatureData->algorithm);
  31. retval |= UA_ByteString_allocBuffer(&signatureData->signature, signatureSize);
  32. if(retval != UA_STATUSCODE_GOOD)
  33. return retval;
  34. /* Allocate a temp buffer */
  35. size_t dataToSignSize = request->clientCertificate.length + request->clientNonce.length;
  36. UA_ByteString dataToSign;
  37. retval = UA_ByteString_allocBuffer(&dataToSign, dataToSignSize);
  38. if(retval != UA_STATUSCODE_GOOD)
  39. return retval; /* signatureData->signature is cleaned up with the response */
  40. /* Sign the signature */
  41. memcpy(dataToSign.data, request->clientCertificate.data, request->clientCertificate.length);
  42. memcpy(dataToSign.data + request->clientCertificate.length,
  43. request->clientNonce.data, request->clientNonce.length);
  44. retval = securityPolicy->certificateSigningAlgorithm.
  45. sign(securityPolicy, channel->channelContext, &dataToSign, &signatureData->signature);
  46. /* Clean up */
  47. UA_ByteString_deleteMembers(&dataToSign);
  48. return retval;
  49. }
  50. void
  51. Service_CreateSession(UA_Server *server, UA_SecureChannel *channel,
  52. const UA_CreateSessionRequest *request,
  53. UA_CreateSessionResponse *response) {
  54. if(!channel) {
  55. response->responseHeader.serviceResult = UA_STATUSCODE_BADINTERNALERROR;
  56. return;
  57. }
  58. if(!channel->connection) {
  59. response->responseHeader.serviceResult = UA_STATUSCODE_BADINTERNALERROR;
  60. return;
  61. }
  62. UA_LOG_DEBUG_CHANNEL(&server->config.logger, channel, "Trying to create session");
  63. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  64. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
  65. /* Compare the clientCertificate with the remoteCertificate of the channel.
  66. * Both the clientCertificate of this request and the remoteCertificate
  67. * of the channel may contain a partial or a complete certificate chain.
  68. * The compareCertificate function of the channelModule will compare the
  69. * first certificate of each chain. The end certificate shall be located
  70. * first in the chain according to the OPC UA specification Part 6 (1.04),
  71. * chapter 6.2.3.*/
  72. UA_StatusCode retval = channel->securityPolicy->channelModule.
  73. compareCertificate(channel->channelContext, &request->clientCertificate);
  74. if(retval != UA_STATUSCODE_GOOD) {
  75. UA_LOG_WARNING_CHANNEL(&server->config.logger, channel,
  76. "The client certificate did not validate");
  77. response->responseHeader.serviceResult = UA_STATUSCODE_BADCERTIFICATEINVALID;
  78. return;
  79. }
  80. }
  81. if(channel->securityToken.channelId == 0) {
  82. response->responseHeader.serviceResult = UA_STATUSCODE_BADSECURECHANNELIDINVALID;
  83. return;
  84. }
  85. if(!UA_ByteString_equal(&channel->securityPolicy->policyUri,
  86. &UA_SECURITY_POLICY_NONE_URI) &&
  87. request->clientNonce.length < 32) {
  88. response->responseHeader.serviceResult = UA_STATUSCODE_BADNONCEINVALID;
  89. return;
  90. }
  91. /* TODO: Compare application URI with certificate uri (decode certificate) */
  92. UA_CertificateVerification *cv = channel->securityPolicy->certificateVerification;
  93. if(cv && cv->verifyApplicationURI) {
  94. response->responseHeader.serviceResult =
  95. cv->verifyApplicationURI(cv->context, &request->clientCertificate,
  96. &request->clientDescription.applicationUri);
  97. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  98. UA_LOG_WARNING_CHANNEL(&server->config.logger, channel,
  99. "The client's ApplicationURI did not match the certificate");
  100. return;
  101. }
  102. }
  103. UA_Session *newSession = NULL;
  104. response->responseHeader.serviceResult =
  105. UA_SessionManager_createSession(&server->sessionManager, channel, request, &newSession);
  106. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  107. UA_LOG_WARNING_CHANNEL(&server->config.logger, channel,
  108. "Processing CreateSessionRequest failed");
  109. return;
  110. }
  111. UA_assert(newSession != NULL);
  112. /* Allocate the response */
  113. response->serverEndpoints = (UA_EndpointDescription *)
  114. UA_Array_new(server->config.endpointsSize,
  115. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  116. if(!response->serverEndpoints) {
  117. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  118. UA_SessionManager_removeSession(&server->sessionManager,
  119. &newSession->header.authenticationToken);
  120. return;
  121. }
  122. response->serverEndpointsSize = server->config.endpointsSize;
  123. /* Copy the server's endpointdescriptions into the response */
  124. for(size_t i = 0; i < server->config.endpointsSize; ++i)
  125. response->responseHeader.serviceResult |=
  126. UA_EndpointDescription_copy(&server->config.endpoints[i],
  127. &response->serverEndpoints[i]);
  128. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  129. UA_SessionManager_removeSession(&server->sessionManager,
  130. &newSession->header.authenticationToken);
  131. return;
  132. }
  133. /* Mirror back the endpointUrl */
  134. for(size_t i = 0; i < response->serverEndpointsSize; ++i) {
  135. UA_String_deleteMembers(&response->serverEndpoints[i].endpointUrl);
  136. response->responseHeader.serviceResult |=
  137. UA_String_copy(&request->endpointUrl,
  138. &response->serverEndpoints[i].endpointUrl);
  139. }
  140. /* Attach the session to the channel. But don't activate for now. */
  141. UA_Session_attachToSecureChannel(newSession, channel);
  142. /* Fill the session information */
  143. newSession->maxResponseMessageSize = request->maxResponseMessageSize;
  144. newSession->maxRequestMessageSize =
  145. channel->connection->config.maxMessageSize;
  146. response->responseHeader.serviceResult |=
  147. UA_ApplicationDescription_copy(&request->clientDescription,
  148. &newSession->clientDescription);
  149. /* Prepare the response */
  150. response->sessionId = newSession->sessionId;
  151. response->revisedSessionTimeout = (UA_Double)newSession->timeout;
  152. response->authenticationToken = newSession->header.authenticationToken;
  153. response->responseHeader.serviceResult |=
  154. UA_String_copy(&request->sessionName, &newSession->sessionName);
  155. UA_ByteString_init(&response->serverCertificate);
  156. if(server->config.endpointsSize > 0)
  157. for(size_t i = 0; i < response->serverEndpointsSize; ++i) {
  158. if(response->serverEndpoints[i].securityMode==channel->securityMode &&
  159. UA_ByteString_equal(&response->serverEndpoints[i].securityPolicyUri,
  160. &channel->securityPolicy->policyUri) &&
  161. UA_String_equal(&response->serverEndpoints[i].endpointUrl,
  162. &request->endpointUrl))
  163. {
  164. response->responseHeader.serviceResult |=
  165. UA_ByteString_copy(&response->serverEndpoints[i].serverCertificate,
  166. &response->serverCertificate);
  167. }
  168. }
  169. /* Create a session nonce */
  170. response->responseHeader.serviceResult |= UA_Session_generateNonce(newSession);
  171. response->responseHeader.serviceResult |=
  172. UA_ByteString_copy(&newSession->serverNonce, &response->serverNonce);
  173. /* Sign the signature */
  174. response->responseHeader.serviceResult |=
  175. signCreateSessionResponse(server, channel, request, response);
  176. /* Failure -> remove the session */
  177. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  178. UA_SessionManager_removeSession(&server->sessionManager,
  179. &newSession->header.authenticationToken);
  180. return;
  181. }
  182. UA_LOG_INFO_CHANNEL(&server->config.logger, channel,
  183. "Session " UA_PRINTF_GUID_FORMAT " created",
  184. UA_PRINTF_GUID_DATA(newSession->sessionId.identifier.guid));
  185. }
  186. static UA_StatusCode
  187. checkSignature(const UA_Server *server, const UA_SecureChannel *channel,
  188. UA_Session *session, const UA_ActivateSessionRequest *request) {
  189. if(channel->securityMode != UA_MESSAGESECURITYMODE_SIGN &&
  190. channel->securityMode != UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  191. return UA_STATUSCODE_GOOD;
  192. /* Check for zero signature length in client signature */
  193. if(request->clientSignature.signature.length == 0) {
  194. return UA_STATUSCODE_BADAPPLICATIONSIGNATUREINVALID;
  195. }
  196. if(!channel->securityPolicy)
  197. return UA_STATUSCODE_BADINTERNALERROR;
  198. const UA_SecurityPolicy *securityPolicy = channel->securityPolicy;
  199. const UA_ByteString *localCertificate = &securityPolicy->localCertificate;
  200. size_t dataToVerifySize = localCertificate->length + session->serverNonce.length;
  201. UA_ByteString dataToVerify;
  202. UA_StatusCode retval = UA_ByteString_allocBuffer(&dataToVerify, dataToVerifySize);
  203. if(retval != UA_STATUSCODE_GOOD)
  204. return retval;
  205. memcpy(dataToVerify.data, localCertificate->data, localCertificate->length);
  206. memcpy(dataToVerify.data + localCertificate->length,
  207. session->serverNonce.data, session->serverNonce.length);
  208. retval = securityPolicy->certificateSigningAlgorithm.verify(securityPolicy, channel->channelContext, &dataToVerify,
  209. &request->clientSignature.signature);
  210. UA_ByteString_deleteMembers(&dataToVerify);
  211. return retval;
  212. }
  213. #ifdef UA_ENABLE_ENCRYPTION
  214. static UA_StatusCode
  215. decryptPassword(UA_SecurityPolicy *securityPolicy, void *tempChannelContext,
  216. const UA_ByteString *serverNonce, UA_UserNameIdentityToken *userToken) {
  217. UA_SecurityPolicyEncryptionAlgorithm *asymEnc =
  218. &securityPolicy->asymmetricModule.cryptoModule.encryptionAlgorithm;
  219. if(!UA_String_equal(&userToken->encryptionAlgorithm, &asymEnc->uri))
  220. return UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  221. UA_UInt32 tokenSecretLength;
  222. UA_ByteString decryptedTokenSecret, tokenServerNonce;
  223. if(UA_ByteString_copy(&userToken->password, &decryptedTokenSecret) != UA_STATUSCODE_GOOD)
  224. return UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  225. UA_StatusCode retval = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  226. if(asymEnc->decrypt(securityPolicy, tempChannelContext,
  227. &decryptedTokenSecret) != UA_STATUSCODE_GOOD)
  228. goto cleanup;
  229. memcpy(&tokenSecretLength, decryptedTokenSecret.data, sizeof(UA_UInt32));
  230. /* The decrypted data must be large enough to include the Encrypted Token
  231. * Secret Format and the length field must indicate enough data to include
  232. * the server nonce. */
  233. if(decryptedTokenSecret.length < sizeof(UA_UInt32) + serverNonce->length ||
  234. decryptedTokenSecret.length < sizeof(UA_UInt32) + tokenSecretLength ||
  235. tokenSecretLength < serverNonce->length)
  236. goto cleanup;
  237. /* If the Encrypted Token Secret contains padding, the padding must be
  238. * zeroes according to the 1.04.1 specification errata, chapter 3. */
  239. for(size_t i = sizeof(UA_UInt32) + tokenSecretLength; i < decryptedTokenSecret.length; i++) {
  240. if(decryptedTokenSecret.data[i] != 0)
  241. goto cleanup;
  242. }
  243. /* The server nonce must match according to the 1.04.1 specification errata,
  244. * chapter 3. */
  245. tokenServerNonce.length = serverNonce->length;
  246. tokenServerNonce.data = &decryptedTokenSecret.data[sizeof(UA_UInt32) + tokenSecretLength - serverNonce->length];
  247. if(!UA_ByteString_equal(serverNonce, &tokenServerNonce))
  248. goto cleanup;
  249. /* The password was decrypted successfully. Replace usertoken with the
  250. * decrypted password. The encryptionAlgorithm and policyId fields are left
  251. * in the UserToken as an indication for the AccessControl plugin that
  252. * evaluates the decrypted content. */
  253. memcpy(userToken->password.data, &decryptedTokenSecret.data[sizeof(UA_UInt32)],
  254. tokenSecretLength - serverNonce->length);
  255. userToken->password.length = tokenSecretLength - serverNonce->length;
  256. retval = UA_STATUSCODE_GOOD;
  257. cleanup:
  258. UA_ByteString_deleteMembers(&decryptedTokenSecret);
  259. return retval;
  260. }
  261. #endif
  262. /* TODO: Check all of the following:
  263. *
  264. * Part 4, §5.6.3: When the ActivateSession Service is called for the first time
  265. * then the Server shall reject the request if the SecureChannel is not same as
  266. * the one associated with the CreateSession request. Subsequent calls to
  267. * ActivateSession may be associated with different SecureChannels. If this is
  268. * the case then the Server shall verify that the Certificate the Client used to
  269. * create the new SecureChannel is the same as the Certificate used to create
  270. * the original SecureChannel. In addition, the Server shall verify that the
  271. * Client supplied a UserIdentityToken that is identical to the token currently
  272. * associated with the Session. Once the Server accepts the new SecureChannel it
  273. * shall reject requests sent via the old SecureChannel. */
  274. void
  275. Service_ActivateSession(UA_Server *server, UA_SecureChannel *channel,
  276. UA_Session *session, const UA_ActivateSessionRequest *request,
  277. UA_ActivateSessionResponse *response) {
  278. UA_LOG_DEBUG_SESSION(&server->config.logger, session, "Execute ActivateSession");
  279. if(session->validTill < UA_DateTime_nowMonotonic()) {
  280. UA_LOG_INFO_SESSION(&server->config.logger, session,
  281. "ActivateSession: SecureChannel %i wants "
  282. "to activate, but the session has timed out",
  283. channel->securityToken.channelId);
  284. response->responseHeader.serviceResult =
  285. UA_STATUSCODE_BADSESSIONIDINVALID;
  286. return;
  287. }
  288. /* Check if the signature corresponds to the ServerNonce that was last sent
  289. * to the client */
  290. response->responseHeader.serviceResult = checkSignature(server, channel, session, request);
  291. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  292. UA_LOG_INFO_SESSION(&server->config.logger, session,
  293. "Signature check failed with status code %s",
  294. UA_StatusCode_name(response->responseHeader.serviceResult));
  295. return;
  296. }
  297. /* Find the matching endpoint */
  298. const UA_EndpointDescription *ed = NULL;
  299. for(size_t i = 0; ed == NULL && i < server->config.endpointsSize; ++i) {
  300. const UA_EndpointDescription *e = &server->config.endpoints[i];
  301. /* Match the Security Mode */
  302. if(e->securityMode != channel->securityMode)
  303. continue;
  304. /* Match the SecurityPolicy */
  305. if(!UA_String_equal(&e->securityPolicyUri, &channel->securityPolicy->policyUri))
  306. continue;
  307. /* Match the UserTokenType */
  308. for(size_t j = 0; j < e->userIdentityTokensSize; j++) {
  309. const UA_UserTokenPolicy *u = &e->userIdentityTokens[j];
  310. if(u->tokenType == UA_USERTOKENTYPE_ANONYMOUS) {
  311. /* Part 4, Section 5.6.3.2, Table 17: A NULL or empty
  312. * UserIdentityToken should be treated as Anonymous */
  313. if(request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_ANONYMOUSIDENTITYTOKEN] &&
  314. request->userIdentityToken.encoding != UA_EXTENSIONOBJECT_ENCODED_NOBODY)
  315. continue;
  316. } else if(u->tokenType == UA_USERTOKENTYPE_USERNAME) {
  317. if(request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_USERNAMEIDENTITYTOKEN])
  318. continue;
  319. } else if(u->tokenType == UA_USERTOKENTYPE_CERTIFICATE) {
  320. if(request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_X509IDENTITYTOKEN])
  321. continue;
  322. } else if(u->tokenType == UA_USERTOKENTYPE_ISSUEDTOKEN) {
  323. if(request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_ISSUEDIDENTITYTOKEN])
  324. continue;
  325. } else {
  326. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  327. return;
  328. }
  329. /* Match found */
  330. ed = e;
  331. break;
  332. }
  333. }
  334. /* No matching endpoint found */
  335. if(!ed) {
  336. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  337. return;
  338. }
  339. #ifdef UA_ENABLE_ENCRYPTION
  340. /* If it is a UserNameIdentityToken, decrypt the password if encrypted */
  341. if((request->userIdentityToken.encoding == UA_EXTENSIONOBJECT_DECODED) &&
  342. (request->userIdentityToken.content.decoded.type == &UA_TYPES[UA_TYPES_USERNAMEIDENTITYTOKEN])) {
  343. UA_UserNameIdentityToken *userToken = (UA_UserNameIdentityToken *)
  344. request->userIdentityToken.content.decoded.data;
  345. /* Find the UserTokenPolicy */
  346. UA_Byte tokenIndex = 0;
  347. for(; tokenIndex < ed->userIdentityTokensSize; tokenIndex++) {
  348. if(ed->userIdentityTokens[tokenIndex].tokenType != UA_USERTOKENTYPE_USERNAME)
  349. continue;
  350. if(UA_String_equal(&userToken->policyId, &ed->userIdentityTokens[tokenIndex].policyId))
  351. break;
  352. }
  353. if(tokenIndex == ed->userIdentityTokensSize) {
  354. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  355. return;
  356. }
  357. /* Get the SecurityPolicy. If the userTokenPolicy doesn't specify a
  358. * security policy the security policy of the secure channel is used. */
  359. UA_SecurityPolicy* securityPolicy;
  360. if(ed->userIdentityTokens[tokenIndex].securityPolicyUri.data == NULL)
  361. securityPolicy = UA_SecurityPolicy_getSecurityPolicyByUri(server, &ed->securityPolicyUri);
  362. else
  363. securityPolicy = UA_SecurityPolicy_getSecurityPolicyByUri(server, &ed->userIdentityTokens[tokenIndex].securityPolicyUri);
  364. if(!securityPolicy) {
  365. response->responseHeader.serviceResult = UA_STATUSCODE_BADINTERNALERROR;
  366. return;
  367. }
  368. /* Encrypted password? */
  369. if(!UA_String_equal(&securityPolicy->policyUri, &UA_SECURITY_POLICY_NONE_URI)) {
  370. /* Test if the encryption algorithm is correctly specified */
  371. if(!UA_String_equal(&userToken->encryptionAlgorithm,
  372. &securityPolicy->asymmetricModule.cryptoModule.
  373. encryptionAlgorithm.uri)) {
  374. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  375. return;
  376. }
  377. /* Create a temporary channel context if a different SecurityPolicy is
  378. * used for the password from the SecureChannel */
  379. void *tempChannelContext = channel->channelContext;
  380. if(securityPolicy != channel->securityPolicy) {
  381. /* TODO: This is a hack. We use our own certificate to create a
  382. * channel context. Because the client does not provide one in a
  383. * #None SecureChannel. We should not need a ChannelContext at all
  384. * for asymmetric decryption where the remote certificate is not
  385. * used. */
  386. response->responseHeader.serviceResult =
  387. securityPolicy->channelModule.newContext(securityPolicy,
  388. &securityPolicy->localCertificate,
  389. &tempChannelContext);
  390. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  391. UA_LOG_WARNING_SESSION(&server->config.logger, session, "ActivateSession: "
  392. "Failed to create a context for the SecurityPolicy %.*s",
  393. (int)securityPolicy->policyUri.length,
  394. securityPolicy->policyUri.data);
  395. return;
  396. }
  397. }
  398. /* Decrypt */
  399. response->responseHeader.serviceResult =
  400. decryptPassword(securityPolicy, tempChannelContext, &session->serverNonce, userToken);
  401. /* Remove the temporary channel context */
  402. if(securityPolicy != channel->securityPolicy)
  403. securityPolicy->channelModule.deleteContext(tempChannelContext);
  404. }
  405. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  406. UA_LOG_INFO_SESSION(&server->config.logger, session, "ActivateSession: "
  407. "Failed to decrypt the password with the status code %s",
  408. UA_StatusCode_name(response->responseHeader.serviceResult));
  409. }
  410. }
  411. #endif
  412. /* Callback into userland access control */
  413. response->responseHeader.serviceResult =
  414. server->config.accessControl.activateSession(server, &server->config.accessControl,
  415. ed, &channel->remoteCertificate,
  416. &session->sessionId,
  417. &request->userIdentityToken,
  418. &session->sessionHandle);
  419. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  420. UA_LOG_INFO_SESSION(&server->config.logger, session,
  421. "ActivateSession: The AccessControl plugin "
  422. "denied the access with the status code %s",
  423. UA_StatusCode_name(response->responseHeader.serviceResult));
  424. return;
  425. }
  426. if(session->header.channel && session->header.channel != channel) {
  427. UA_LOG_INFO_SESSION(&server->config.logger, session,
  428. "ActivateSession: Detach from old channel");
  429. /* Detach the old SecureChannel and attach the new */
  430. UA_Session_detachFromSecureChannel(session);
  431. UA_Session_attachToSecureChannel(session, channel);
  432. }
  433. /* Activate the session */
  434. session->activated = true;
  435. UA_Session_updateLifetime(session);
  436. /* Generate a new session nonce for the next time ActivateSession is called */
  437. response->responseHeader.serviceResult = UA_Session_generateNonce(session);
  438. response->responseHeader.serviceResult |=
  439. UA_ByteString_copy(&session->serverNonce, &response->serverNonce);
  440. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  441. UA_Session_detachFromSecureChannel(session);
  442. session->activated = false;
  443. UA_LOG_INFO_SESSION(&server->config.logger, session,
  444. "ActivateSession: Could not generate a server nonce");
  445. return;
  446. }
  447. UA_LOG_INFO_SESSION(&server->config.logger, session,
  448. "ActivateSession: Session activated");
  449. }
  450. void
  451. Service_CloseSession(UA_Server *server, UA_Session *session,
  452. const UA_CloseSessionRequest *request,
  453. UA_CloseSessionResponse *response) {
  454. UA_LOG_INFO_SESSION(&server->config.logger, session, "CloseSession");
  455. response->responseHeader.serviceResult =
  456. UA_SessionManager_removeSession(&server->sessionManager,
  457. &session->header.authenticationToken);
  458. }