server_basic128rsa15.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <signal.h>
  4. #include <stdio.h>
  5. #include <errno.h>
  6. #include "open62541.h"
  7. static UA_ByteString loadFile(const char *const path) {
  8. UA_ByteString fileContents = UA_STRING_NULL;
  9. /* Open the file */
  10. FILE *fp = fopen(path, "rb");
  11. if(!fp) {
  12. errno = 0; /* We read errno also from the tcp layer... */
  13. return fileContents;
  14. }
  15. /* Get the file length, allocate the data and read */
  16. fseek(fp, 0, SEEK_END);
  17. fileContents.length = (size_t)ftell(fp);
  18. fileContents.data = (UA_Byte*)UA_malloc(fileContents.length * sizeof(UA_Byte));
  19. if(fileContents.data) {
  20. fseek(fp, 0, SEEK_SET);
  21. size_t read = fread(fileContents.data, sizeof(UA_Byte), fileContents.length, fp);
  22. if(read != fileContents.length)
  23. UA_ByteString_deleteMembers(&fileContents);
  24. } else {
  25. fileContents.length = 0;
  26. }
  27. fclose(fp);
  28. return fileContents;
  29. }
  30. UA_Boolean running = true;
  31. static void stopHandler(int sig) {
  32. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "received ctrl-c");
  33. running = false;
  34. }
  35. int main(int argc, char* argv[]) {
  36. signal(SIGINT, stopHandler);
  37. signal(SIGTERM, stopHandler);
  38. if(argc < 3) {
  39. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  40. "Missing arguments. Arguments are "
  41. "<server-certificate.der> <private-key.der> "
  42. "[<trustlist1.crl>, ...]");
  43. return 1;
  44. }
  45. /* Load certificate and private key */
  46. UA_ByteString certificate = loadFile(argv[1]);
  47. UA_ByteString privateKey = loadFile(argv[2]);
  48. /* Load the trustlist */
  49. size_t trustListSize = 0;
  50. if(argc > 3)
  51. trustListSize = (size_t)argc-3;
  52. UA_STACKARRAY(UA_ByteString, trustList, trustListSize);
  53. for(size_t i = 0; i < trustListSize; i++)
  54. trustList[i] = loadFile(argv[i+3]);
  55. /* Loading of a revocation list currentlu unsupported */
  56. UA_ByteString *revocationList = NULL;
  57. size_t revocationListSize = 0;
  58. UA_ServerConfig *config =
  59. UA_ServerConfig_new_basic128rsa15(4840, &certificate, &privateKey,
  60. trustList, trustListSize,
  61. revocationList, revocationListSize);
  62. UA_ByteString_deleteMembers(&certificate);
  63. UA_ByteString_deleteMembers(&privateKey);
  64. for(size_t i = 0; i < trustListSize; i++)
  65. UA_ByteString_deleteMembers(&trustList[i]);
  66. if(!config) {
  67. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  68. "Could not create the server config");
  69. return 1;
  70. }
  71. UA_Server *server = UA_Server_new(config);
  72. UA_StatusCode retval = UA_Server_run(server, &running);
  73. UA_Server_delete(server);
  74. UA_ServerConfig_delete(config);
  75. return (int)retval;
  76. }