client_basic128rsa15.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 2018 (c) Kalycito Infotech Private Limited
  6. */
  7. #include <stdio.h>
  8. #include <errno.h>
  9. #include "open62541.h"
  10. #define MIN_ARGS 3
  11. #define FAILURE 1
  12. #define CONNECTION_STRING "opc.tcp://localhost:4840"
  13. /* loadFile parses the certificate file.
  14. *
  15. * @param path specifies the file name given in argv[]
  16. * @return Returns the file content after parsing */
  17. static UA_ByteString loadFile(const char *const path) {
  18. UA_ByteString fileContents = UA_BYTESTRING_NULL;
  19. if(path == NULL)
  20. return fileContents;
  21. /* Open the file */
  22. FILE *fp = fopen(path, "rb");
  23. if(!fp) {
  24. errno = 0; /* We read errno also from the tcp layer */
  25. return fileContents;
  26. }
  27. /* Get the file length, allocate the data and read */
  28. fseek(fp, 0, SEEK_END);
  29. fileContents.length = (size_t)ftell(fp);
  30. fileContents.data = (UA_Byte*)UA_malloc(fileContents.length * sizeof(UA_Byte));
  31. if(fileContents.data) {
  32. fseek(fp, 0, SEEK_SET);
  33. size_t read = fread(fileContents.data, sizeof(UA_Byte), fileContents.length, fp);
  34. if(read != fileContents.length)
  35. UA_ByteString_deleteMembers(&fileContents);
  36. } else {
  37. fileContents.length = 0;
  38. }
  39. fclose(fp);
  40. return fileContents;
  41. }
  42. /* cleanupClient deletes the memory allocated for client configuration.
  43. *
  44. * @param client client configuration that need to be deleted
  45. * @param remoteCertificate server certificate */
  46. static void cleanupClient(UA_Client* client, UA_ByteString* remoteCertificate) {
  47. UA_ByteString_delete(remoteCertificate); /* Dereference the memory */
  48. UA_Client_delete(client); /* Disconnects the client internally */
  49. }
  50. /* main function for secure client implementation.
  51. *
  52. * @param argc count of command line variable provided
  53. * @param argv[] array of strings include certificate, private key,
  54. * trust list and revocation list
  55. * @return Return an integer representing success or failure of application */
  56. int main(int argc, char* argv[]) {
  57. UA_Client* client = NULL;
  58. UA_ByteString* remoteCertificate = NULL;
  59. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  60. UA_ByteString* trustList = NULL;
  61. size_t trustListSize = 0;
  62. UA_ByteString* revocationList = NULL;
  63. size_t revocationListSize = 0;
  64. /* endpointArray is used to hold the available endpoints in the server
  65. * endpointArraySize is used to hold the number of endpoints available */
  66. UA_EndpointDescription* endpointArray = NULL;
  67. size_t endpointArraySize = 0;
  68. /* Load certificate and private key */
  69. UA_ByteString certificate = loadFile(argv[1]);
  70. UA_ByteString privateKey = loadFile(argv[2]);
  71. if(argc < MIN_ARGS) {
  72. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  73. "The Certificate and key is missing."
  74. "The required arguments are "
  75. "<client-certificate.der> <client-private-key.der> "
  76. "[<trustlist1.crl>, ...]");
  77. return FAILURE;
  78. }
  79. /* The Get endpoint (discovery service) is done with
  80. * security mode as none to see the server's capability
  81. * and certificate */
  82. client = UA_Client_new(UA_ClientConfig_default);
  83. remoteCertificate = UA_ByteString_new();
  84. retval = UA_Client_getEndpoints(client, CONNECTION_STRING,
  85. &endpointArraySize, &endpointArray);
  86. if(retval != UA_STATUSCODE_GOOD) {
  87. UA_Array_delete(endpointArray, endpointArraySize,
  88. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  89. cleanupClient(client, remoteCertificate);
  90. return (int)retval;
  91. }
  92. printf("%i endpoints found\n", (int)endpointArraySize);
  93. for(size_t endPointCount = 0; endPointCount < endpointArraySize; endPointCount++) {
  94. printf("URL of endpoint %i is %.*s\n", (int)endPointCount,
  95. (int)endpointArray[endPointCount].endpointUrl.length,
  96. endpointArray[endPointCount].endpointUrl.data);
  97. if(endpointArray[endPointCount].securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  98. UA_ByteString_copy(&endpointArray[endPointCount].serverCertificate, remoteCertificate);
  99. }
  100. if(UA_ByteString_equal(remoteCertificate, &UA_BYTESTRING_NULL)) {
  101. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  102. "Server does not support Security Mode of"
  103. " UA_MESSAGESECURITYMODE_SIGNANDENCRYPT");
  104. cleanupClient(client, remoteCertificate);
  105. return FAILURE;
  106. }
  107. UA_Array_delete(endpointArray, endpointArraySize,
  108. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  109. /* Load the trustList. Load revocationList is not supported now */
  110. if(argc > MIN_ARGS) {
  111. trustListSize = (size_t)argc-MIN_ARGS;
  112. retval = UA_ByteString_allocBuffer(trustList, trustListSize);
  113. if(retval != UA_STATUSCODE_GOOD) {
  114. cleanupClient(client, remoteCertificate);
  115. return (int)retval;
  116. }
  117. for(size_t trustListCount = 0; trustListCount < trustListSize; trustListCount++) {
  118. trustList[trustListCount] = loadFile(argv[trustListCount+3]);
  119. }
  120. }
  121. /* Secure client initialization */
  122. client = UA_Client_secure_new(UA_ClientConfig_default,
  123. certificate, privateKey,
  124. remoteCertificate,
  125. trustList, trustListSize,
  126. revocationList, revocationListSize);
  127. if(client == NULL) {
  128. UA_ByteString_delete(remoteCertificate); /* Dereference the memory */
  129. return FAILURE;
  130. }
  131. UA_ByteString_deleteMembers(&certificate);
  132. UA_ByteString_deleteMembers(&privateKey);
  133. for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++) {
  134. UA_ByteString_deleteMembers(&trustList[deleteCount]);
  135. }
  136. if(!client) {
  137. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  138. "Could not create the server config");
  139. cleanupClient(client, remoteCertificate);
  140. return FAILURE;
  141. }
  142. /* Secure client connect */
  143. retval = UA_Client_connect(client, CONNECTION_STRING);
  144. if(retval != UA_STATUSCODE_GOOD) {
  145. cleanupClient(client, remoteCertificate);
  146. return (int)retval;
  147. }
  148. UA_Variant value;
  149. UA_Variant_init(&value);
  150. /* NodeId of the variable holding the current time */
  151. const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  152. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  153. if(retval == UA_STATUSCODE_GOOD &&
  154. UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
  155. UA_DateTime raw_date = *(UA_DateTime *) value.data;
  156. UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
  157. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "date is: %u-%u-%u %u:%u:%u.%03u\n",
  158. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  159. }
  160. /* Clean up */
  161. UA_Variant_deleteMembers(&value);
  162. cleanupClient(client, remoteCertificate);
  163. return (int)retval;
  164. }