ua_services_session.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #include "ua_types_generated_handling.h"
  18. static UA_StatusCode
  19. signCreateSessionResponse(UA_Server *server, UA_SecureChannel *channel,
  20. const UA_CreateSessionRequest *request,
  21. UA_CreateSessionResponse *response) {
  22. if(channel->securityMode != UA_MESSAGESECURITYMODE_SIGN &&
  23. channel->securityMode != UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  24. return UA_STATUSCODE_GOOD;
  25. const UA_SecurityPolicy *const securityPolicy = channel->securityPolicy;
  26. UA_SignatureData *signatureData = &response->serverSignature;
  27. /* Prepare the signature */
  28. size_t signatureSize = securityPolicy->certificateSigningAlgorithm.
  29. getLocalSignatureSize(securityPolicy, channel->channelContext);
  30. UA_StatusCode retval = UA_String_copy(&securityPolicy->certificateSigningAlgorithm.uri,
  31. &signatureData->algorithm);
  32. retval |= UA_ByteString_allocBuffer(&signatureData->signature, signatureSize);
  33. if(retval != UA_STATUSCODE_GOOD)
  34. return retval;
  35. /* Allocate a temp buffer */
  36. size_t dataToSignSize = request->clientCertificate.length + request->clientNonce.length;
  37. UA_ByteString dataToSign;
  38. retval = UA_ByteString_allocBuffer(&dataToSign, dataToSignSize);
  39. if(retval != UA_STATUSCODE_GOOD)
  40. return retval; /* signatureData->signature is cleaned up with the response */
  41. /* Sign the signature */
  42. memcpy(dataToSign.data, request->clientCertificate.data, request->clientCertificate.length);
  43. memcpy(dataToSign.data + request->clientCertificate.length,
  44. request->clientNonce.data, request->clientNonce.length);
  45. retval = securityPolicy->certificateSigningAlgorithm.
  46. sign(securityPolicy, channel->channelContext, &dataToSign, &signatureData->signature);
  47. /* Clean up */
  48. UA_ByteString_deleteMembers(&dataToSign);
  49. return retval;
  50. }
  51. void
  52. Service_CreateSession(UA_Server *server, UA_SecureChannel *channel,
  53. const UA_CreateSessionRequest *request,
  54. UA_CreateSessionResponse *response) {
  55. if(!channel) {
  56. response->responseHeader.serviceResult = UA_STATUSCODE_BADINTERNALERROR;
  57. return;
  58. }
  59. if(!channel->connection) {
  60. response->responseHeader.serviceResult = UA_STATUSCODE_BADINTERNALERROR;
  61. return;
  62. }
  63. UA_LOG_DEBUG_CHANNEL(&server->config.logger, channel, "Trying to create session");
  64. if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGN ||
  65. channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT) {
  66. /* Compare the clientCertificate with the remoteCertificate of the channel.
  67. * Both the clientCertificate of this request and the remoteCertificate
  68. * of the channel may contain a partial or a complete certificate chain.
  69. * The compareCertificate function of the channelModule will compare the
  70. * first certificate of each chain. The end certificate shall be located
  71. * first in the chain according to the OPC UA specification Part 6 (1.04),
  72. * chapter 6.2.3.*/
  73. if(channel->securityPolicy->channelModule.compareCertificate(channel->channelContext,
  74. &request->clientCertificate) != UA_STATUSCODE_GOOD) {
  75. response->responseHeader.serviceResult = UA_STATUSCODE_BADCERTIFICATEINVALID;
  76. return;
  77. }
  78. }
  79. if(channel->securityToken.channelId == 0) {
  80. response->responseHeader.serviceResult = UA_STATUSCODE_BADSECURECHANNELIDINVALID;
  81. return;
  82. }
  83. if(!UA_ByteString_equal(&channel->securityPolicy->policyUri,
  84. &UA_SECURITY_POLICY_NONE_URI) &&
  85. request->clientNonce.length < 32) {
  86. response->responseHeader.serviceResult = UA_STATUSCODE_BADNONCEINVALID;
  87. return;
  88. }
  89. /* TODO: Compare application URI with certificate uri (decode certificate) */
  90. UA_CertificateVerification *cv = channel->securityPolicy->certificateVerification;
  91. if(cv && cv->verifyApplicationURI) {
  92. response->responseHeader.serviceResult =
  93. cv->verifyApplicationURI(cv->context, &request->clientCertificate,
  94. &request->clientDescription.applicationUri);
  95. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  96. return;
  97. }
  98. UA_Session *newSession = NULL;
  99. response->responseHeader.serviceResult =
  100. UA_SessionManager_createSession(&server->sessionManager, channel, request, &newSession);
  101. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  102. UA_LOG_DEBUG_CHANNEL(&server->config.logger, channel,
  103. "Processing CreateSessionRequest failed");
  104. return;
  105. }
  106. UA_assert(newSession != NULL);
  107. /* Allocate the response */
  108. response->serverEndpoints = (UA_EndpointDescription *)
  109. UA_Array_new(server->config.endpointsSize,
  110. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  111. if(!response->serverEndpoints) {
  112. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  113. UA_SessionManager_removeSession(&server->sessionManager,
  114. &newSession->header.authenticationToken);
  115. return;
  116. }
  117. response->serverEndpointsSize = server->config.endpointsSize;
  118. /* Copy the server's endpointdescriptions into the response */
  119. for(size_t i = 0; i < server->config.endpointsSize; ++i)
  120. response->responseHeader.serviceResult |=
  121. UA_EndpointDescription_copy(&server->config.endpoints[i],
  122. &response->serverEndpoints[i]);
  123. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  124. UA_SessionManager_removeSession(&server->sessionManager,
  125. &newSession->header.authenticationToken);
  126. return;
  127. }
  128. /* Mirror back the endpointUrl */
  129. for(size_t i = 0; i < response->serverEndpointsSize; ++i) {
  130. UA_String_deleteMembers(&response->serverEndpoints[i].endpointUrl);
  131. response->responseHeader.serviceResult |=
  132. UA_String_copy(&request->endpointUrl,
  133. &response->serverEndpoints[i].endpointUrl);
  134. }
  135. /* Attach the session to the channel. But don't activate for now. */
  136. UA_Session_attachToSecureChannel(newSession, channel);
  137. /* Fill the session information */
  138. newSession->maxResponseMessageSize = request->maxResponseMessageSize;
  139. newSession->maxRequestMessageSize =
  140. channel->connection->config.maxMessageSize;
  141. response->responseHeader.serviceResult |=
  142. UA_ApplicationDescription_copy(&request->clientDescription,
  143. &newSession->clientDescription);
  144. /* Prepare the response */
  145. response->sessionId = newSession->sessionId;
  146. response->revisedSessionTimeout = (UA_Double)newSession->timeout;
  147. response->authenticationToken = newSession->header.authenticationToken;
  148. response->responseHeader.serviceResult |=
  149. UA_String_copy(&request->sessionName, &newSession->sessionName);
  150. UA_ByteString_init(&response->serverCertificate);
  151. if(server->config.endpointsSize > 0)
  152. for(size_t i = 0; i < response->serverEndpointsSize; ++i) {
  153. if(response->serverEndpoints[i].securityMode==channel->securityMode &&
  154. UA_ByteString_equal(&response->serverEndpoints[i].securityPolicyUri,
  155. &channel->securityPolicy->policyUri) &&
  156. UA_String_equal(&response->serverEndpoints[i].endpointUrl,
  157. &request->endpointUrl))
  158. {
  159. response->responseHeader.serviceResult |=
  160. UA_ByteString_copy(&response->serverEndpoints[i].serverCertificate,
  161. &response->serverCertificate);
  162. }
  163. }
  164. /* Create a session nonce */
  165. response->responseHeader.serviceResult |= UA_Session_generateNonce(newSession);
  166. response->responseHeader.serviceResult |=
  167. UA_ByteString_copy(&newSession->serverNonce, &response->serverNonce);
  168. /* Sign the signature */
  169. response->responseHeader.serviceResult |=
  170. signCreateSessionResponse(server, channel, request, response);
  171. /* Failure -> remove the session */
  172. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  173. UA_SessionManager_removeSession(&server->sessionManager,
  174. &newSession->header.authenticationToken);
  175. return;
  176. }
  177. UA_LOG_DEBUG_CHANNEL(&server->config.logger, channel,
  178. "Session " UA_PRINTF_GUID_FORMAT " created",
  179. UA_PRINTF_GUID_DATA(newSession->sessionId.identifier.guid));
  180. }
  181. static UA_StatusCode
  182. checkSignature(const UA_Server *server, const UA_SecureChannel *channel,
  183. UA_Session *session, const UA_ActivateSessionRequest *request) {
  184. if(channel->securityMode != UA_MESSAGESECURITYMODE_SIGN &&
  185. channel->securityMode != UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  186. return UA_STATUSCODE_GOOD;
  187. if(!channel->securityPolicy)
  188. return UA_STATUSCODE_BADINTERNALERROR;
  189. const UA_SecurityPolicy *securityPolicy = channel->securityPolicy;
  190. const UA_ByteString *localCertificate = &securityPolicy->localCertificate;
  191. size_t dataToVerifySize = localCertificate->length + session->serverNonce.length;
  192. UA_ByteString dataToVerify;
  193. UA_StatusCode retval = UA_ByteString_allocBuffer(&dataToVerify, dataToVerifySize);
  194. if(retval != UA_STATUSCODE_GOOD)
  195. return retval;
  196. memcpy(dataToVerify.data, localCertificate->data, localCertificate->length);
  197. memcpy(dataToVerify.data + localCertificate->length,
  198. session->serverNonce.data, session->serverNonce.length);
  199. retval = securityPolicy->certificateSigningAlgorithm.verify(securityPolicy, channel->channelContext, &dataToVerify,
  200. &request->clientSignature.signature);
  201. UA_ByteString_deleteMembers(&dataToVerify);
  202. return retval;
  203. }
  204. /* TODO: Check all of the following:
  205. *
  206. * Part 4, §5.6.3: When the ActivateSession Service is called for the first time
  207. * then the Server shall reject the request if the SecureChannel is not same as
  208. * the one associated with the CreateSession request. Subsequent calls to
  209. * ActivateSession may be associated with different SecureChannels. If this is
  210. * the case then the Server shall verify that the Certificate the Client used to
  211. * create the new SecureChannel is the same as the Certificate used to create
  212. * the original SecureChannel. In addition, the Server shall verify that the
  213. * Client supplied a UserIdentityToken that is identical to the token currently
  214. * associated with the Session. Once the Server accepts the new SecureChannel it
  215. * shall reject requests sent via the old SecureChannel. */
  216. void
  217. Service_ActivateSession(UA_Server *server, UA_SecureChannel *channel,
  218. UA_Session *session, const UA_ActivateSessionRequest *request,
  219. UA_ActivateSessionResponse *response) {
  220. UA_LOG_DEBUG_SESSION(&server->config.logger, session, "Execute ActivateSession");
  221. if(session->validTill < UA_DateTime_nowMonotonic()) {
  222. UA_LOG_INFO_SESSION(&server->config.logger, session,
  223. "ActivateSession: SecureChannel %i wants "
  224. "to activate, but the session has timed out",
  225. channel->securityToken.channelId);
  226. response->responseHeader.serviceResult =
  227. UA_STATUSCODE_BADSESSIONIDINVALID;
  228. return;
  229. }
  230. /* Check if the signature corresponds to the ServerNonce that was last sent
  231. * to the client */
  232. response->responseHeader.serviceResult = checkSignature(server, channel, session, request);
  233. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  234. UA_LOG_INFO_SESSION(&server->config.logger, session,
  235. "Signature check failed with status code %s",
  236. UA_StatusCode_name(response->responseHeader.serviceResult));
  237. return;
  238. }
  239. /* Find the matching endpoint */
  240. const UA_EndpointDescription *ed = NULL;
  241. for(size_t i = 0; ed == NULL && i < server->config.endpointsSize; ++i) {
  242. const UA_EndpointDescription *e = &server->config.endpoints[i];
  243. /* Match the Security Mode */
  244. if(e->securityMode != channel->securityMode)
  245. continue;
  246. /* Match the SecurityPolicy */
  247. if(!UA_String_equal(&e->securityPolicyUri,
  248. &channel->securityPolicy->policyUri))
  249. continue;
  250. /* Match the UserTokenType */
  251. for(size_t j = 0; j < e->userIdentityTokensSize; j++) {
  252. const UA_UserTokenPolicy *u = &e->userIdentityTokens[j];
  253. if(u->tokenType == UA_USERTOKENTYPE_ANONYMOUS) {
  254. if(request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_ANONYMOUSIDENTITYTOKEN])
  255. continue;
  256. } else if(u->tokenType == UA_USERTOKENTYPE_USERNAME) {
  257. if(request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_USERNAMEIDENTITYTOKEN])
  258. continue;
  259. } else if(u->tokenType == UA_USERTOKENTYPE_CERTIFICATE) {
  260. if(request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_X509IDENTITYTOKEN])
  261. continue;
  262. } else if(u->tokenType == UA_USERTOKENTYPE_ISSUEDTOKEN) {
  263. if(request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_ISSUEDIDENTITYTOKEN])
  264. continue;
  265. } else {
  266. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  267. return;
  268. }
  269. /* Match found */
  270. ed = e;
  271. break;
  272. }
  273. }
  274. /* No matching endpoint found */
  275. if(!ed) {
  276. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  277. return;
  278. }
  279. /* Callback into userland access control */
  280. response->responseHeader.serviceResult =
  281. server->config.accessControl.activateSession(server, &server->config.accessControl,
  282. ed, &channel->remoteCertificate,
  283. &session->sessionId,
  284. &request->userIdentityToken,
  285. &session->sessionHandle);
  286. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  287. UA_LOG_INFO_SESSION(&server->config.logger, session,
  288. "ActivateSession: The AccessControl plugin "
  289. "denied the access with the status code %s",
  290. UA_StatusCode_name(response->responseHeader.serviceResult));
  291. return;
  292. }
  293. if(session->header.channel && session->header.channel != channel) {
  294. UA_LOG_INFO_SESSION(&server->config.logger, session,
  295. "ActivateSession: Detach from old channel");
  296. /* Detach the old SecureChannel and attach the new */
  297. UA_Session_detachFromSecureChannel(session);
  298. UA_Session_attachToSecureChannel(session, channel);
  299. }
  300. /* Activate the session */
  301. session->activated = true;
  302. UA_Session_updateLifetime(session);
  303. /* Generate a new session nonce for the next time ActivateSession is called */
  304. response->responseHeader.serviceResult = UA_Session_generateNonce(session);
  305. response->responseHeader.serviceResult |=
  306. UA_ByteString_copy(&session->serverNonce, &response->serverNonce);
  307. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  308. UA_Session_detachFromSecureChannel(session);
  309. session->activated = false;
  310. UA_LOG_INFO_SESSION(&server->config.logger, session,
  311. "ActivateSession: Could not generate a server nonce");
  312. return;
  313. }
  314. UA_LOG_INFO_SESSION(&server->config.logger, session,
  315. "ActivateSession: Session activated");
  316. }
  317. void
  318. Service_CloseSession(UA_Server *server, UA_Session *session,
  319. const UA_CloseSessionRequest *request,
  320. UA_CloseSessionResponse *response) {
  321. UA_LOG_INFO_SESSION(&server->config.logger, session, "CloseSession");
  322. /* Callback into userland access control */
  323. server->config.accessControl.closeSession(server, &server->config.accessControl,
  324. &session->sessionId, session->sessionHandle);
  325. response->responseHeader.serviceResult =
  326. UA_SessionManager_removeSession(&server->sessionManager,
  327. &session->header.authenticationToken);
  328. }