server_encryption.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. *
  4. * Copyright 2019 (c) Kalycito Infotech Private Limited
  5. *
  6. */
  7. #include <open62541/client_highlevel.h>
  8. #include <open62541/plugin/log_stdout.h>
  9. #include <open62541/plugin/securitypolicy.h>
  10. #include <open62541/server.h>
  11. #include <open62541/server_config_default.h>
  12. #include <signal.h>
  13. #include <stdlib.h>
  14. #include "common.h"
  15. UA_Boolean running = true;
  16. static void stopHandler(int sig) {
  17. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "received ctrl-c");
  18. running = false;
  19. }
  20. int main(int argc, char* argv[]) {
  21. signal(SIGINT, stopHandler);
  22. signal(SIGTERM, stopHandler);
  23. if(argc < 3) {
  24. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  25. "Missing arguments. Arguments are "
  26. "<server-certificate.der> <private-key.der> "
  27. "[<trustlist1.crl>, ...]");
  28. return EXIT_FAILURE;
  29. }
  30. /* Load certificate and private key */
  31. UA_ByteString certificate = loadFile(argv[1]);
  32. UA_ByteString privateKey = loadFile(argv[2]);
  33. /* Load the trustlist */
  34. size_t trustListSize = 0;
  35. if(argc > 3)
  36. trustListSize = (size_t)argc-3;
  37. UA_STACKARRAY(UA_ByteString, trustList, trustListSize);
  38. for(size_t i = 0; i < trustListSize; i++)
  39. trustList[i] = loadFile(argv[i+3]);
  40. /* Loading of a issuer list, not used in this application */
  41. size_t issuerListSize = 0;
  42. UA_ByteString *issuerList = NULL;
  43. /* Loading of a revocation list currently unsupported */
  44. UA_ByteString *revocationList = NULL;
  45. size_t revocationListSize = 0;
  46. UA_Server *server = UA_Server_new();
  47. UA_ServerConfig *config = UA_Server_getConfig(server);
  48. UA_StatusCode retval =
  49. UA_ServerConfig_setDefaultWithSecurityPolicies(config, 4840,
  50. &certificate, &privateKey,
  51. trustList, trustListSize,
  52. issuerList, issuerListSize,
  53. revocationList, revocationListSize);
  54. UA_ByteString_clear(&certificate);
  55. UA_ByteString_clear(&privateKey);
  56. for(size_t i = 0; i < trustListSize; i++)
  57. UA_ByteString_clear(&trustList[i]);
  58. if(retval != UA_STATUSCODE_GOOD)
  59. goto cleanup;
  60. retval = UA_Server_run(server, &running);
  61. cleanup:
  62. UA_Server_delete(server);
  63. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  64. }