aboutsummaryrefslogtreecommitdiffstats
path: root/src/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c114
1 files changed, 100 insertions, 14 deletions
diff --git a/src/common.c b/src/common.c
index 7d378f1..568e4d0 100644
--- a/src/common.c
+++ b/src/common.c
@@ -9,6 +9,7 @@
#include <open62541/plugin/log_stdout.h>
#include <open62541/server_config_default.h>
+#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@@ -56,6 +57,102 @@ loadFile (const char *const path)
}
/* ========================================================================
+ * Trust Store
+ * ======================================================================== */
+
+int
+loadTrustStore (const char *dirPath, char ***outPaths, size_t *outSize)
+{
+ *outPaths = NULL;
+ *outSize = 0;
+
+ DIR *dir = opendir (dirPath);
+ if (!dir)
+ {
+ UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
+ "Cannot open trust store directory '%s'", dirPath);
+ return -1;
+ }
+
+ size_t capacity = 8;
+ size_t count = 0;
+ char **paths = malloc (capacity * sizeof (char *));
+ if (!paths)
+ {
+ UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
+ "loadTrustStore: out of memory");
+ closedir (dir);
+ return -1;
+ }
+
+ struct dirent *entry;
+ while ((entry = readdir (dir)) != NULL)
+ {
+ const char *name = entry->d_name;
+ size_t nameLen = strlen (name);
+ /* Skip entries that are not *.der files. 5 = strlen("x.der"). */
+ if (nameLen < 5 || strcmp (name + nameLen - 4, ".der") != 0)
+ continue;
+
+ /* Build full path: dirPath/name */
+ size_t dirLen = strlen (dirPath);
+ size_t fullLen = dirLen + 1 + nameLen + 1;
+ char *full = malloc (fullLen);
+ if (!full)
+ {
+ UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
+ "loadTrustStore: out of memory");
+ for (size_t i = 0; i < count; i++)
+ free (paths[i]);
+ free (paths);
+ closedir (dir);
+ return -1;
+ }
+ snprintf (full, fullLen, "%s/%s", dirPath, name);
+
+ if (count == capacity)
+ {
+ capacity *= 2;
+ char **tmp = realloc (paths, capacity * sizeof (char *));
+ if (!tmp)
+ {
+ UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
+ "loadTrustStore: out of memory");
+ free (full);
+ for (size_t i = 0; i < count; i++)
+ free (paths[i]);
+ free (paths);
+ closedir (dir);
+ return -1;
+ }
+ paths = tmp;
+ }
+
+ paths[count++] = full;
+ }
+
+ closedir (dir);
+
+ if (count == 0)
+ {
+ free (paths);
+ return 0;
+ }
+
+ *outPaths = paths;
+ *outSize = count;
+ return 0;
+}
+
+void
+freeTrustStore (char **paths, size_t size)
+{
+ for (size_t i = 0; i < size; i++)
+ free (paths[i]);
+ free (paths);
+}
+
+/* ========================================================================
* Security Helpers
* ======================================================================== */
@@ -223,18 +320,12 @@ createSecureServer (UA_UInt16 port, const char *applicationUri,
for (size_t i = 0; i < trustSize; i++)
trustList[i] = loadFile (trustPaths[i]);
- /* Issuer and revocation lists are unused in this demo. */
- size_t issuerListSize = 0;
- UA_ByteString *issuerList = NULL;
- UA_ByteString *revocationList = NULL;
- size_t revocationListSize = 0;
-
UA_Server *server = UA_Server_new ();
UA_ServerConfig *config = UA_Server_getConfig (server);
*retval = UA_ServerConfig_setDefaultWithSecurityPolicies (
- config, port, &certificate, &privateKey, trustList, trustSize,
- issuerList, issuerListSize, revocationList, revocationListSize);
+ config, port, &certificate, &privateKey, trustList, trustSize, NULL, 0,
+ NULL, 0);
UA_ByteString_clear (&certificate);
UA_ByteString_clear (&privateKey);
@@ -269,13 +360,8 @@ createSecureClientConfig (UA_ClientConfig *cc, const char *applicationUri,
for (size_t i = 0; i < trustSize; i++)
trustList[i] = loadFile (trustPaths[i]);
- /* Revocation list is unused in this demo. */
- UA_ByteString *revocationList = NULL;
- size_t revocationListSize = 0;
-
UA_StatusCode retval = UA_ClientConfig_setDefaultEncryption (
- cc, certificate, privateKey, trustList, trustSize, revocationList,
- revocationListSize);
+ cc, certificate, privateKey, trustList, trustSize, NULL, 0);
UA_ByteString_clear (&certificate);
UA_ByteString_clear (&privateKey);