server_basic128rsa15.c 3.0 KB

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