ua_stack_channel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * ua_stack_channel.c
  3. *
  4. * Created on: 09.05.2014
  5. * Author: root
  6. */
  7. #include "ua_stack_channel.h"
  8. typedef struct SL_Channel1 {
  9. SL_channelState state;
  10. UA_UInt32 channelId;
  11. //TL_Connection* tlConnection;
  12. UA_TL_Connection1 connection;
  13. UA_UInt32 requestId;
  14. //UA_UInt32 lastRequestId;
  15. UA_UInt32 sequenceNumber;
  16. //UA_UInt32 lastSequenceNumber;
  17. UA_AsymmetricAlgorithmSecurityHeader remoteAsymAlgSettings;
  18. UA_AsymmetricAlgorithmSecurityHeader localAsymAlgSettings;
  19. UA_ChannelSecurityToken securityToken;
  20. UA_MessageSecurityMode securityMode;
  21. UA_ByteString remoteNonce;
  22. UA_ByteString localNonce;
  23. SL_ChannelSecurityTokenProvider tokenProvider;
  24. SL_ChannelIdProvider channelIdProvider;
  25. }SL_Channel1;
  26. UA_Int32 SL_Channel_registerTokenProvider(SL_secureChannel channel, SL_ChannelSecurityTokenProvider provider)
  27. {
  28. ((SL_Channel1*)channel)->tokenProvider = provider;
  29. return UA_SUCCESS;
  30. }
  31. UA_Int32 SL_Channel_getRemainingLifetime(SL_secureChannel channel, UA_Int32 *lifetime)
  32. {
  33. if(channel)
  34. {
  35. *lifetime = UA_DateTime_difference_ms(((SL_Channel1*)channel)->securityToken.createdAt,UA_DateTime_now());
  36. return UA_SUCCESS;
  37. }
  38. else
  39. {
  40. printf("SL_Channel_getRemainingLifetime - no valid channel object, null pointer");
  41. return UA_ERROR;
  42. }
  43. }
  44. UA_Int32 SL_Channel_getChannelId(SL_secureChannel channel, UA_UInt32 *channelId)
  45. {
  46. if(channel)
  47. {
  48. *channelId = ((SL_Channel1*)channel)->channelId;
  49. return UA_SUCCESS;
  50. }
  51. return UA_ERROR;
  52. }
  53. UA_Int32 SL_Channel_getTokenId(SL_secureChannel channel, UA_UInt32 *tokenId)
  54. {
  55. if(channel)
  56. {
  57. *tokenId = ((SL_Channel1*)channel)->securityToken.tokenId;
  58. return UA_SUCCESS;
  59. }
  60. return UA_ERROR;
  61. }
  62. UA_Int32 SL_Channel_getLocalAsymAlgSettings(SL_secureChannel channel, UA_AsymmetricAlgorithmSecurityHeader **asymAlgSettings)
  63. {
  64. UA_Int32 retval = 0;
  65. retval |= UA_alloc((void**)asymAlgSettings,UA_AsymmetricAlgorithmSecurityHeader_calcSize(UA_NULL));
  66. retval |= UA_ByteString_copy(&(channel->localAsymAlgSettings.receiverCertificateThumbprint),
  67. &(*asymAlgSettings)->receiverCertificateThumbprint);
  68. retval |= UA_ByteString_copy(&(channel->localAsymAlgSettings.securityPolicyUri),
  69. &(*asymAlgSettings)->securityPolicyUri);
  70. retval |= UA_ByteString_copy(&(channel->localAsymAlgSettings.senderCertificate),
  71. &(*asymAlgSettings)->senderCertificate);
  72. return UA_SUCCESS;
  73. }
  74. UA_Int32 SL_Channel_getConnection(SL_secureChannel channel, UA_TL_Connection1 *connection)
  75. {
  76. if(channel)
  77. {
  78. *connection = ((SL_Channel1*)channel)->connection;
  79. return UA_SUCCESS;
  80. }
  81. return UA_ERROR;
  82. }
  83. UA_Int32 SL_Channel_getRequestId(SL_secureChannel channel, UA_UInt32 *requestId)
  84. {
  85. if(channel)
  86. {
  87. *requestId = ((SL_Channel1*)channel)->requestId;
  88. return UA_SUCCESS;
  89. }
  90. return UA_ERROR;
  91. }
  92. UA_Int32 SL_Channel_getSequenceNumber(SL_secureChannel channel, UA_UInt32 *sequenceNumber)
  93. {
  94. if(channel)
  95. {
  96. *sequenceNumber = ((SL_Channel1*)channel)->sequenceNumber;
  97. return UA_SUCCESS;
  98. }
  99. return UA_ERROR;
  100. }
  101. UA_Int32 SL_Channel_getState(SL_secureChannel channel,SL_channelState *channelState)
  102. {
  103. if(channel)
  104. {
  105. *channelState = ((SL_Channel1*)channel)->state;
  106. return UA_SUCCESS;
  107. }
  108. return UA_ERROR;
  109. }
  110. UA_Int32 SL_Channel_getRevisedLifetime(SL_secureChannel channel, UA_UInt32 *revisedLifetime)
  111. {
  112. if(channel)
  113. {
  114. *revisedLifetime = ((SL_Channel1*)channel)->securityToken.revisedLifetime;
  115. return UA_SUCCESS;
  116. }
  117. return UA_ERROR;
  118. }
  119. //setters
  120. UA_Int32 SL_Channel_setId(SL_secureChannel channel, UA_UInt32 id)
  121. {
  122. ((SL_Channel1*)channel)->channelId = id;
  123. return UA_SUCCESS;
  124. }
  125. //private function
  126. UA_Int32 SL_Channel_setState(SL_secureChannel channel,SL_channelState channelState)
  127. {
  128. ((SL_Channel1*)channel)->state = channelState;
  129. return UA_SUCCESS;
  130. }
  131. UA_Int32 SL_Channel_init(SL_secureChannel channel,
  132. UA_ByteString *receiverCertificateThumbprint,
  133. UA_ByteString *securityPolicyUri,
  134. UA_ByteString *senderCertificate,
  135. UA_MessageSecurityMode securityMode)
  136. {
  137. UA_Int32 retval = UA_SUCCESS;
  138. retval |= UA_ByteString_copy(receiverCertificateThumbprint,
  139. &((SL_Channel1*)channel)->localAsymAlgSettings.receiverCertificateThumbprint);
  140. retval |= UA_ByteString_copy(securityPolicyUri,
  141. &((SL_Channel1*)channel)->localAsymAlgSettings.securityPolicyUri);
  142. retval |= UA_ByteString_copy(senderCertificate,
  143. &((SL_Channel1*)channel)->localAsymAlgSettings.senderCertificate);
  144. ((SL_Channel1*)channel)->state = UA_SL_CHANNEL_CLOSED;
  145. return retval;
  146. }
  147. //TODO implement real nonce generator - DUMMY function
  148. UA_Int32 SL_Channel_generateNonce(UA_ByteString *nonce)
  149. {
  150. UA_ByteString_new(&nonce);
  151. UA_alloc((void**)&(nonce->data),1);
  152. nonce->length = 1;
  153. nonce->data[0] = 'a';
  154. return UA_SUCCESS;
  155. }
  156. _Bool SL_Channel_equal(void* channel1, void* channel2)
  157. {
  158. return (((SL_Channel1*)channel1)->channelId == ((SL_Channel1*)channel2)->channelId);
  159. }
  160. UA_Int32 SL_Channel_new(SL_secureChannel **channel,
  161. SL_ChannelIdProvider channelIdProvider,
  162. SL_ChannelSecurityTokenProvider tokenProvider,
  163. UA_ByteString *receiverCertificateThumbprint,
  164. UA_ByteString *securityPolicyUri,
  165. UA_ByteString *senderCertificate,
  166. UA_MessageSecurityMode securityMode)
  167. {
  168. UA_Int32 retval = UA_SUCCESS;
  169. retval |= UA_alloc((void**)channel,sizeof(SL_secureChannel));
  170. SL_Channel1 *thisChannel = UA_NULL;
  171. retval |= UA_alloc((void**)&thisChannel,sizeof(SL_Channel1));
  172. thisChannel->channelIdProvider = channelIdProvider;
  173. thisChannel->tokenProvider = tokenProvider;
  174. retval |= UA_ByteString_copy(receiverCertificateThumbprint,
  175. &thisChannel->localAsymAlgSettings.receiverCertificateThumbprint);
  176. retval |= UA_ByteString_copy(securityPolicyUri,
  177. &thisChannel->localAsymAlgSettings.securityPolicyUri);
  178. retval |= UA_ByteString_copy(senderCertificate,
  179. &thisChannel->localAsymAlgSettings.senderCertificate);
  180. thisChannel->state = UA_SL_CHANNEL_CLOSED;
  181. **channel = (SL_secureChannel)thisChannel;
  182. return UA_SUCCESS;
  183. }
  184. UA_Int32 SL_Channel_initByRequest(SL_secureChannel channel,
  185. UA_TL_Connection1 connection,
  186. const UA_ByteString* msg,
  187. UA_Int32* pos)
  188. {
  189. UA_SequenceHeader *sequenceHeader;
  190. UA_SequenceHeader_new(&sequenceHeader);
  191. UA_SequenceHeader_init(sequenceHeader);
  192. ((SL_Channel1*)channel)->channelIdProvider(&((SL_Channel1*)channel)->channelId);
  193. ((SL_Channel1*)channel)->connection = connection;
  194. // channel->createdAt = UA_DateTime_now();
  195. // channel->localAsymAlgSettings.receiverCertificateThumbprint
  196. SL_Channel_generateNonce(&channel->localNonce);
  197. *pos += 4; // skip the securechannelid
  198. UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(msg, pos,
  199. &channel->remoteAsymAlgSettings);
  200. UA_SequenceHeader_decodeBinary(msg, pos,
  201. sequenceHeader);
  202. //init last requestId and sequenceNumber
  203. //channel->lastRequestId = sequenceHeader->requestId;
  204. //channel->lastSequenceNumber = sequenceHeader->sequenceNumber;
  205. channel->requestId = sequenceHeader->requestId;//channel->lastRequestId;
  206. channel->sequenceNumber = sequenceHeader->sequenceNumber;//channel->lastSequenceNumber;
  207. channel->state = UA_SL_CHANNEL_CLOSED;
  208. return UA_SUCCESS;
  209. }
  210. UA_Int32 SL_Channel_delete(SL_secureChannel channel)
  211. {
  212. //TODO implement me
  213. //((SL_Channel1*)channel)->
  214. return UA_SUCCESS;
  215. }
  216. UA_Int32 SL_Channel_deleteMembers(SL_secureChannel channel)
  217. {
  218. //TODO implement me!
  219. return UA_SUCCESS;
  220. }
  221. UA_Int32 SL_Channel_processTokenRequest(SL_secureChannel channel,UA_UInt32 requestedLifetime, UA_SecurityTokenRequestType requestType)
  222. {
  223. if(((SL_Channel1*)channel)->tokenProvider)
  224. {
  225. return ((SL_Channel1*)channel)->tokenProvider(channel,requestedLifetime,requestType, &((SL_Channel1*)channel)->securityToken);
  226. }
  227. printf("SL_Channel_processTokenRequest - no Token provider registered");
  228. return UA_ERROR;
  229. }
  230. UA_Int32 SL_Channel_renewToken(SL_secureChannel channel, UA_UInt32 tokenId,UA_DateTime revisedLifetime, UA_DateTime createdAt)
  231. {
  232. ((SL_Channel1*)channel)->securityToken.tokenId = tokenId;
  233. ((SL_Channel1*)channel)->securityToken.createdAt = createdAt;
  234. ((SL_Channel1*)channel)->securityToken.revisedLifetime = revisedLifetime;
  235. return UA_SUCCESS;
  236. }
  237. UA_Int32 SL_Channel_checkSequenceNumber(SL_secureChannel channel, UA_UInt32 sequenceNumber)
  238. {
  239. if(((SL_Channel1*)channel)->sequenceNumber+1 == sequenceNumber){
  240. ((SL_Channel1*)channel)->sequenceNumber++;
  241. return UA_SUCCESS;
  242. }
  243. printf("SL_Channel_checkSequenceNumber - ERROR, wrong SequenceNumber expected: %i, received: %i",
  244. ((SL_Channel1*)channel)->sequenceNumber+1,sequenceNumber);
  245. return UA_ERROR;
  246. }
  247. UA_Int32 SL_Channel_checkRequestId(SL_secureChannel channel, UA_UInt32 requestId)
  248. {
  249. if(((SL_Channel1*)channel)->requestId+1 == requestId){
  250. ((SL_Channel1*)channel)->requestId++;
  251. return UA_SUCCESS;
  252. }
  253. printf("SL_Channel_requestId - ERROR, wrong requestId expected: %i, received: %i",
  254. ((SL_Channel1*)channel)->requestId+1,requestId);
  255. return UA_ERROR;
  256. }
  257. UA_Int32 SL_Channel_processOpenRequest(SL_secureChannel channel,
  258. const UA_OpenSecureChannelRequest* request, UA_OpenSecureChannelResponse* response)
  259. {
  260. UA_UInt32 protocolVersion;
  261. SL_Channel1 *thisChannel = (SL_Channel1*)channel;
  262. UA_Int32 retval = UA_SUCCESS;
  263. UA_TL_Connection_getProtocolVersion(thisChannel->connection, &protocolVersion);
  264. if (request->clientProtocolVersion
  265. != protocolVersion)
  266. {
  267. printf("SL_Channel_processOpenRequest - error protocol version \n");
  268. //TODO ERROR_Bad_ProtocolVersionUnsupported
  269. }
  270. switch (request->requestType)
  271. {
  272. case UA_SECURITYTOKEN_ISSUE:
  273. if (thisChannel->state == UA_SL_CHANNEL_OPEN)
  274. {
  275. printf("SL_Channel_processOpenRequest - multiple security token request");
  276. //TODO return ERROR
  277. retval = UA_ERROR;
  278. break;
  279. }
  280. printf(
  281. "SL_Channel_processOpenRequest - creating new token for a new SecureChannel\n");
  282. SL_Channel_processTokenRequest(channel,request->requestedLifetime, request->requestType);
  283. // SL_createNewToken(connection);
  284. break;
  285. case UA_SECURITYTOKEN_RENEW:
  286. if (thisChannel->state == UA_SL_CHANNEL_CLOSED)
  287. {
  288. printf(
  289. "SL_Channel_processOpenRequest - renew token request received, but no secureChannel was established before");
  290. //TODO return ERROR
  291. retval = UA_ERROR;
  292. break;
  293. }
  294. else
  295. {
  296. //generate new SecurityToken
  297. retval = SL_Channel_processTokenRequest(channel,request->requestedLifetime, request->requestType);
  298. if (retval != UA_SUCCESS)
  299. {
  300. printf(
  301. "SL_Channel_processOpenRequest - creating new token for an existing SecureChannel\n");
  302. }
  303. else
  304. {
  305. printf(
  306. "SL_Channel_processOpenRequest - cannot create new token for an existing SecureChannel\n");
  307. }
  308. break;
  309. }
  310. }
  311. switch (request->securityMode)
  312. {
  313. case UA_SECURITYMODE_INVALID:
  314. thisChannel->remoteNonce.data = UA_NULL;
  315. thisChannel->remoteNonce.length = -1;
  316. printf("SL_Channel_processOpenRequest - client demands no security \n");
  317. break;
  318. case UA_SECURITYMODE_SIGN:
  319. printf("SL_Channel_processOpenRequest - client demands signed \n");
  320. //TODO check if senderCertificate and ReceiverCertificateThumbprint are present
  321. break;
  322. case UA_SECURITYMODE_SIGNANDENCRYPT:
  323. printf("SL_Channel_processOpenRequest - client demands signed & encrypted \n");
  324. //TODO check if senderCertificate and ReceiverCertificateThumbprint are present
  325. break;
  326. }
  327. thisChannel->state = CONNECTIONSTATE_ESTABLISHED;
  328. if (request->requestHeader.returnDiagnostics != 0)
  329. {
  330. printf("SL_openSecureChannel - diagnostics demanded by the client\n");
  331. printf(
  332. "SL_openSecureChannel - retrieving diagnostics not implemented!\n");
  333. //TODO fill with demanded information part 4, 7.8 - Table 123
  334. response->responseHeader.serviceDiagnostics.encodingMask = 0;
  335. }
  336. else
  337. {
  338. response->responseHeader.serviceDiagnostics.encodingMask = 0;
  339. }
  340. response->serverProtocolVersion = protocolVersion;
  341. UA_ChannelSecurityToken_copy(&((SL_Channel1*)(channel))->securityToken, &(response->securityToken));
  342. UA_ByteString_copy(&thisChannel->localNonce, &response->serverNonce);
  343. return retval;
  344. }
  345. UA_Int32 SL_Channel_processCloseRequest(SL_secureChannel channel,
  346. const UA_CloseSecureChannelRequest* request)
  347. {
  348. SL_Channel_setState(channel,UA_SL_CHANNEL_CLOSED);
  349. return UA_SUCCESS;
  350. }