server_basic128rsa15.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. UA_ByteString *trustList = NULL;
  50. size_t trustListSize = 0;
  51. if(argc > 3) {
  52. trustListSize = (size_t)argc-3;
  53. trustList = (UA_ByteString*)
  54. UA_alloca(sizeof(UA_ByteString) * trustListSize);
  55. for(size_t i = 0; i < trustListSize; i++)
  56. trustList[i] = loadFile(argv[i+3]);
  57. }
  58. /* Loading of a revocation list currentlu unsupported */
  59. UA_ByteString *revocationList = NULL;
  60. size_t revocationListSize = 0;
  61. UA_ServerConfig *config =
  62. UA_ServerConfig_new_basic128rsa15(4840, &certificate, &privateKey,
  63. trustList, trustListSize,
  64. revocationList, revocationListSize);
  65. UA_ByteString_deleteMembers(&certificate);
  66. UA_ByteString_deleteMembers(&privateKey);
  67. for(size_t i = 0; i < trustListSize; i++)
  68. UA_ByteString_deleteMembers(&trustList[i]);
  69. if(!config) {
  70. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  71. "Could not create the server config");
  72. return 1;
  73. }
  74. UA_Server *server = UA_Server_new(config);
  75. UA_StatusCode retval = UA_Server_run(server, &running);
  76. UA_Server_delete(server);
  77. UA_ServerConfig_delete(config);
  78. return (int)retval;
  79. }