server_encryption.c 2.3 KB

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