aboutsummaryrefslogtreecommitdiffstats
path: root/src/common.c
blob: 7d378f1139541ef51b66144df42923d7edbccbfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
 * @file  common.c
 * @brief Implements shared helpers declared in common.h.
 */

#include "common.h"

#include <open62541/client_config_default.h>
#include <open62541/plugin/log_stdout.h>
#include <open62541/server_config_default.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* ========================================================================
 * File Loading
 * ======================================================================== */

UA_ByteString
loadFile (const char *const path)
{
  UA_ByteString fileContents = UA_STRING_NULL;

  FILE *fp = fopen (path, "rb");
  if (!fp)
    {
      /* fopen sets errno on failure.  Callers like createSecureServer use
         loadFile for optional trustlist entries where a missing file is not
         an error.  Clear errno so open62541's logging does not pick up
         a stale value and emit misleading error messages. */
      errno = 0;
      return fileContents;
    }

  fseek (fp, 0, SEEK_END);
  fileContents.length = (size_t)ftell (fp);
  fileContents.data
      = (UA_Byte *)UA_malloc (fileContents.length * sizeof (UA_Byte));
  if (fileContents.data)
    {
      fseek (fp, 0, SEEK_SET);
      size_t read = fread (fileContents.data, sizeof (UA_Byte),
                           fileContents.length, fp);
      if (read != fileContents.length)
        UA_ByteString_clear (&fileContents);
    }
  else
    {
      fileContents.length = 0;
    }
  fclose (fp);

  return fileContents;
}

/* ========================================================================
 * Security Helpers
 * ======================================================================== */

int
parseLogLevel (const char *name)
{
  static const struct
  {
    const char *name;
    UA_LogLevel level;
  } levels[] = {
    { "trace", UA_LOGLEVEL_TRACE }, { "debug", UA_LOGLEVEL_DEBUG },
    { "info", UA_LOGLEVEL_INFO },   { "warning", UA_LOGLEVEL_WARNING },
    { "error", UA_LOGLEVEL_ERROR }, { "fatal", UA_LOGLEVEL_FATAL },
  };
  for (size_t i = 0; i < sizeof (levels) / sizeof (levels[0]); i++)
    {
      if (strcmp (name, levels[i].name) == 0)
        return (int)levels[i].level;
    }
  return -1;
}

UA_MessageSecurityMode
parseSecurityMode (const char *name)
{
  if (strcmp (name, "None") == 0)
    return UA_MESSAGESECURITYMODE_NONE;
  if (strcmp (name, "Sign") == 0)
    return UA_MESSAGESECURITYMODE_SIGN;
  if (strcmp (name, "SignAndEncrypt") == 0)
    return UA_MESSAGESECURITYMODE_SIGNANDENCRYPT;
  return UA_MESSAGESECURITYMODE_INVALID;
}

const char *
resolveSecurityPolicyUri (const char *shortName)
{
  static const struct
  {
    const char *name;
    const char *uri;
  } policies[] = {
    { "None", "http://opcfoundation.org/UA/SecurityPolicy#None" },
    { "Basic256Sha256",
      "http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256" },
    { "Aes256_Sha256_RsaPss",
      "http://opcfoundation.org/UA/SecurityPolicy#Aes256_Sha256_RsaPss" },
    { "Aes128_Sha256_RsaOaep",
      "http://opcfoundation.org/UA/SecurityPolicy#Aes128_Sha256_RsaOaep" },
    { "ECC_nistP256",
      "http://opcfoundation.org/UA/SecurityPolicy#ECC_nistP256" },
  };
  for (size_t i = 0; i < sizeof (policies) / sizeof (policies[0]); i++)
    {
      if (strcmp (shortName, policies[i].name) == 0)
        return policies[i].uri;
    }
  return NULL;
}

/* ========================================================================
 * Output Formatting
 * ======================================================================== */

void
printApplicationDescription (const UA_ApplicationDescription *description,
                             size_t index)
{
  const char *type = "Unknown";
  switch (description->applicationType)
    {
    case UA_APPLICATIONTYPE_SERVER:
      type = "Server";
      break;
    case UA_APPLICATIONTYPE_CLIENT:
      type = "Client";
      break;
    case UA_APPLICATIONTYPE_CLIENTANDSERVER:
      type = "Client and Server";
      break;
    case UA_APPLICATIONTYPE_DISCOVERYSERVER:
      type = "Discovery Server";
      break;
    default:
      break;
    }

  UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION, "Server[%lu]: %.*s",
               (unsigned long)index, (int)description->applicationUri.length,
               description->applicationUri.data);
  UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION, "  Name: %.*s",
               (int)description->applicationName.text.length,
               description->applicationName.text.data);
  UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION,
               "  Application URI: %.*s",
               (int)description->applicationUri.length,
               description->applicationUri.data);
  UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION,
               "  Product URI: %.*s", (int)description->productUri.length,
               description->productUri.data);
  UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION, "  Type: %s", type);
  for (size_t j = 0; j < description->discoveryUrlsSize; j++)
    {
      UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION,
                   "  Discovery URL[%lu]: %.*s", (unsigned long)j,
                   (int)description->discoveryUrls[j].length,
                   description->discoveryUrls[j].data);
    }
}

void
printEndpoint (const UA_EndpointDescription *endpoint, size_t index)
{
  const char *mode = "Unknown";
  switch (endpoint->securityMode)
    {
    case UA_MESSAGESECURITYMODE_NONE:
      mode = "None";
      break;
    case UA_MESSAGESECURITYMODE_SIGN:
      mode = "Sign";
      break;
    case UA_MESSAGESECURITYMODE_SIGNANDENCRYPT:
      mode = "SignAndEncrypt";
      break;
    default:
      break;
    }

  /* Extract policy name after the '#' */
  const char *policy = (const char *)endpoint->securityPolicyUri.data;
  size_t policyLen = endpoint->securityPolicyUri.length;
  for (size_t k = 0; k < endpoint->securityPolicyUri.length; k++)
    {
      if (endpoint->securityPolicyUri.data[k] == '#')
        {
          policy = (const char *)&endpoint->securityPolicyUri.data[k + 1];
          policyLen = endpoint->securityPolicyUri.length - k - 1;
          break;
        }
    }

  UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION,
               "  [%4lu] %.*s | Level: %2d | %-14s | %.*s",
               (unsigned long)index, (int)endpoint->endpointUrl.length,
               endpoint->endpointUrl.data, endpoint->securityLevel, mode,
               (int)policyLen, policy);
}

/* ========================================================================
 * Factory Functions
 * ======================================================================== */

UA_Server *
createSecureServer (UA_UInt16 port, const char *applicationUri,
                    const char *certPath, const char *keyPath,
                    char **trustPaths, size_t trustSize, UA_StatusCode *retval)
{
  UA_ByteString certificate = loadFile (certPath);
  UA_ByteString privateKey = loadFile (keyPath);

  /* +1: UA_STACKARRAY requires a strictly positive size for VLA. */
  UA_STACKARRAY (UA_ByteString, trustList, trustSize + 1);
  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);

  UA_ByteString_clear (&certificate);
  UA_ByteString_clear (&privateKey);
  for (size_t i = 0; i < trustSize; i++)
    UA_ByteString_clear (&trustList[i]);

  if (*retval != UA_STATUSCODE_GOOD)
    {
      UA_Server_delete (server);
      return NULL;
    }

  UA_String_clear (&config->applicationDescription.applicationUri);
  config->applicationDescription.applicationUri
      = UA_String_fromChars (applicationUri);

  return server;
}

UA_StatusCode
createSecureClientConfig (UA_ClientConfig *cc, const char *applicationUri,
                          const char *certPath, const char *keyPath,
                          char **trustPaths, size_t trustSize,
                          UA_MessageSecurityMode securityMode,
                          const char *securityPolicyUri)
{
  UA_ByteString certificate = loadFile (certPath);
  UA_ByteString privateKey = loadFile (keyPath);

  /* +1: UA_STACKARRAY requires a strictly positive size for VLA. */
  UA_STACKARRAY (UA_ByteString, trustList, trustSize + 1);
  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);

  UA_ByteString_clear (&certificate);
  UA_ByteString_clear (&privateKey);
  for (size_t i = 0; i < trustSize; i++)
    UA_ByteString_clear (&trustList[i]);

  if (retval != UA_STATUSCODE_GOOD)
    {
      UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION,
                    "Failed to set client encryption. StatusCode %s",
                    UA_StatusCode_name (retval));
      return retval;
    }

  UA_String_clear (&cc->clientDescription.applicationUri);
  cc->clientDescription.applicationUri = UA_String_fromChars (applicationUri);

  cc->securityMode = securityMode;
  cc->securityPolicyUri = UA_String_fromChars (securityPolicyUri);

  return retval;
}