open62541 1.3.12
Open source implementation of OPC UA
Loading...
Searching...
No Matches
securitypolicy.h
Go to the documentation of this file.
1/** This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 *
5 * Copyright 2017-2018 (c) Mark Giraud, Fraunhofer IOSB
6 * Copyright 2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
7 * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
8 */
9
10#ifndef UA_PLUGIN_SECURITYPOLICY_H_
11#define UA_PLUGIN_SECURITYPOLICY_H_
12
13#include <open62541/util.h>
16
18
20
23
24
25
26typedef struct {
28
29 /* Verifies the signature of the message using the provided keys in the context.
30 *
31 * @param channelContext the channelContext that contains the key to verify
32 * the supplied message with.
33 * @param message the message to which the signature is supposed to belong.
34 * @param signature the signature of the message, that should be verified. */
35 UA_StatusCode (*verify)(void *channelContext, const UA_ByteString *message,
36 const UA_ByteString *signature);
37
38 /* Signs the given message using this policys signing algorithm and the
39 * provided keys in the context.
40 *
41 * @param channelContext the channelContext that contains the key to sign
42 * the supplied message with.
43 * @param message the message to sign.
44 * @param signature an output buffer to which the signature is written. The
45 * buffer needs to be allocated by the caller. The
46 * necessary size can be acquired with the signatureSize
47 * attribute of this module. */
48 UA_StatusCode (*sign)(void *channelContext, const UA_ByteString *message,
49 UA_ByteString *signature);
50
51 /* Gets the signature size that depends on the local (private) key.
52 *
53 * @param channelContext the channelContext that contains the
54 * certificate/key.
55 * @return the size of the local signature. Returns 0 if no local
56 * certificate was set. */
57 size_t (*getLocalSignatureSize)(const void *channelContext);
58
59 /* Gets the signature size that depends on the remote (public) key.
60 *
61 * @param channelContext the context to retrieve data from.
62 * @return the size of the remote signature. Returns 0 if no
63 * remote certificate was set previousely. */
64 size_t (*getRemoteSignatureSize)(const void *channelContext);
65
66 /* Gets the local signing key length.
67 *
68 * @param channelContext the context to retrieve data from.
69 * @return the length of the signing key in bytes. Returns 0 if no length can be found.
70 */
71 size_t (*getLocalKeyLength)(const void *channelContext);
72
73 /* Gets the local signing key length.
74 *
75 * @param channelContext the context to retrieve data from.
76 * @return the length of the signing key in bytes. Returns 0 if no length can be found.
77 */
78 size_t (*getRemoteKeyLength)(const void *channelContext);
80
81typedef struct {
83
84 /* Encrypt the given data in place. For asymmetric encryption, the block
85 * size for plaintext and cypher depend on the remote key (certificate).
86 *
87 * @param channelContext the channelContext which contains information about
88 * the keys to encrypt data.
89 * @param data the data that is encrypted. The encrypted data will overwrite
90 * the data that was supplied. */
91 UA_StatusCode (*encrypt)(void *channelContext,
92 UA_ByteString *data);
93
94 /* Decrypts the given ciphertext in place. For asymmetric encryption, the
95 * block size for plaintext and cypher depend on the local private key.
96 *
97 * @param channelContext the channelContext which contains information about
98 * the keys needed to decrypt the message.
99 * @param data the data to decrypt. The decryption is done in place. */
100 UA_StatusCode (*decrypt)(void *channelContext,
101 UA_ByteString *data);
102
103 /* Returns the length of the key used to encrypt messages in bits. For
104 * asymmetric encryption the key length is for the local private key.
105 *
106 * @param channelContext the context to retrieve data from.
107 * @return the length of the local key. Returns 0 if no
108 * key length is known. */
109 size_t (*getLocalKeyLength)(const void *channelContext);
110
111 /* Returns the length of the key to encrypt messages in bits. Depends on the
112 * key (certificate) from the remote side.
113 *
114 * @param channelContext the context to retrieve data from.
115 * @return the length of the remote key. Returns 0 if no
116 * key length is known. */
117 size_t (*getRemoteKeyLength)(const void *channelContext);
118
119 /* Returns the size of encrypted blocks for sending. For asymmetric
120 * encryption this depends on the remote key (certificate). For symmetric
121 * encryption the local and remote encrypted block size are identical.
122 *
123 * @param channelContext the context to retrieve data from.
124 * @return the size of encrypted blocks in bytes. Returns 0 if no key length is known.
125 */
126 size_t (*getRemoteBlockSize)(const void *channelContext);
127
128 /* Returns the size of plaintext blocks for sending. For asymmetric
129 * encryption this depends on the remote key (certificate). For symmetric
130 * encryption the local and remote plaintext block size are identical.
131 *
132 * @param channelContext the context to retrieve data from.
133 * @return the size of plaintext blocks in bytes. Returns 0 if no key length is known.
134 */
135 size_t (*getRemotePlainTextBlockSize)(const void *channelContext);
137
138typedef struct {
139 /* The algorithm used to sign and verify certificates. */
141
142 /* The algorithm used to encrypt and decrypt messages. */
144
146
147typedef struct {
148 /* Generates a thumbprint for the specified certificate.
149 *
150 * @param certificate the certificate to make a thumbprint of.
151 * @param thumbprint an output buffer for the resulting thumbprint. Always
152 * has the length specified in the thumbprintLength in the
153 * asymmetricModule. */
154 UA_StatusCode (*makeCertificateThumbprint)(const UA_SecurityPolicy *securityPolicy,
155 const UA_ByteString *certificate,
156 UA_ByteString *thumbprint)
157 ;
158
159 /* Compares the supplied certificate with the certificate in the endpoint context.
160 *
161 * @param securityPolicy the policy data that contains the certificate
162 * to compare to.
163 * @param certificateThumbprint the certificate thumbprint to compare to the
164 * one stored in the context.
165 * @return if the thumbprints match UA_STATUSCODE_GOOD is returned. If they
166 * don't match or an error occurred an error code is returned. */
167 UA_StatusCode (*compareCertificateThumbprint)(const UA_SecurityPolicy *securityPolicy,
168 const UA_ByteString *certificateThumbprint)
169 ;
170
173
174typedef struct {
175 /* Pseudo random function that is used to generate the symmetric keys.
176 *
177 * For information on what parameters this function receives in what situation,
178 * refer to the OPC UA specification 1.03 Part6 Table 33
179 *
180 * @param policyContext The context of the policy instance
181 * @param secret
182 * @param seed
183 * @param out an output to write the data to. The length defines the maximum
184 * number of output bytes that are produced. */
185 UA_StatusCode (*generateKey)(void *policyContext, const UA_ByteString *secret,
186 const UA_ByteString *seed, UA_ByteString *out)
187 ;
188
189 /* Random generator for generating nonces.
190 *
191 * @param policyContext The context of the policy instance
192 * @param out pointer to a buffer to store the nonce in. Needs to be
193 * allocated by the caller. The buffer is filled with random
194 * data. */
195 UA_StatusCode (*generateNonce)(void *policyContext, UA_ByteString *out)
196 ;
197
198 /*
199 * The length of the nonce used in the SecureChannel as specified in the standard.
200 */
202
205
206typedef struct {
207 /* This method creates a new context data object.
208 *
209 * The caller needs to call delete on the received object to free allocated
210 * memory. Memory is only allocated if the function succeeds so there is no
211 * need to manually free the memory pointed to by *channelContext or to
212 * call delete in case of failure.
213 *
214 * @param securityPolicy the policy context of the endpoint that is connected
215 * to. It will be stored in the channelContext for
216 * further access by the policy.
217 * @param remoteCertificate the remote certificate contains the remote
218 * asymmetric key. The certificate will be verified
219 * and then stored in the context so that its
220 * details may be accessed.
221 * @param channelContext the initialized channelContext that is passed to
222 * functions that work on a context. */
223 UA_StatusCode (*newContext)(const UA_SecurityPolicy *securityPolicy,
224 const UA_ByteString *remoteCertificate,
225 void **channelContext)
226 ;
227
228 /* Deletes the the security context. */
229 void (*deleteContext)(void *channelContext);
230
231 /* Sets the local encrypting key in the supplied context.
232 *
233 * @param channelContext the context to work on.
234 * @param key the local encrypting key to store in the context. */
235 UA_StatusCode (*setLocalSymEncryptingKey)(void *channelContext,
236 const UA_ByteString *key)
237 ;
238
239 /* Sets the local signing key in the supplied context.
240 *
241 * @param channelContext the context to work on.
242 * @param key the local signing key to store in the context. */
243 UA_StatusCode (*setLocalSymSigningKey)(void *channelContext,
244 const UA_ByteString *key)
245 ;
246
247 /* Sets the local initialization vector in the supplied context.
248 *
249 * @param channelContext the context to work on.
250 * @param iv the local initialization vector to store in the context. */
251 UA_StatusCode (*setLocalSymIv)(void *channelContext,
252 const UA_ByteString *iv)
253 ;
254
255 /* Sets the remote encrypting key in the supplied context.
256 *
257 * @param channelContext the context to work on.
258 * @param key the remote encrypting key to store in the context. */
259 UA_StatusCode (*setRemoteSymEncryptingKey)(void *channelContext,
260 const UA_ByteString *key)
261 ;
262
263 /* Sets the remote signing key in the supplied context.
264 *
265 * @param channelContext the context to work on.
266 * @param key the remote signing key to store in the context. */
267 UA_StatusCode (*setRemoteSymSigningKey)(void *channelContext,
268 const UA_ByteString *key)
269 ;
270
271 /* Sets the remote initialization vector in the supplied context.
272 *
273 * @param channelContext the context to work on.
274 * @param iv the remote initialization vector to store in the context. */
275 UA_StatusCode (*setRemoteSymIv)(void *channelContext,
276 const UA_ByteString *iv)
277 ;
278
279 /* Compares the supplied certificate with the certificate in the channel
280 * context.
281 *
282 * @param channelContext the channel context data that contains the
283 * certificate to compare to.
284 * @param certificate the certificate to compare to the one stored in the context.
285 * @return if the certificates match UA_STATUSCODE_GOOD is returned. If they
286 * don't match or an errror occurred an error code is returned. */
287 UA_StatusCode (*compareCertificate)(const void *channelContext,
288 const UA_ByteString *certificate)
289 ;
291
293 /* Additional data */
295
296 /* The policy uri that identifies the implemented algorithms */
298
299 /* The local certificate is specific for each SecurityPolicy since it
300 * depends on the used key length. */
302
303 /* Function pointers grouped into modules */
308
310
311 /* Updates the ApplicationInstanceCertificate and the corresponding private
312 * key at runtime. */
314 const UA_ByteString newCertificate,
315 const UA_ByteString newPrivateKey);
316
317 /* Deletes the dynamic content of the policy */
318 void (*clear)(UA_SecurityPolicy *policy);
319};
320
321
322
323#ifdef UA_ENABLE_PUBSUB_ENCRYPTION
324struct UA_PubSubSecurityPolicy;
325typedef struct UA_PubSubSecurityPolicy UA_PubSubSecurityPolicy;
326
327struct UA_PubSubSecurityPolicy {
328 UA_String policyUri; /* The policy uri that identifies the implemented
329 * algorithms */
330 UA_SecurityPolicySymmetricModule symmetricModule;
331
332 /* Create the context for the WriterGroup. The keys and nonce can be NULL
333 * here. Then they have to be set before the first encryption or signing
334 * operation. */
336 (*newContext)(void *policyContext,
337 const UA_ByteString *signingKey,
338 const UA_ByteString *encryptingKey,
339 const UA_ByteString *keyNonce,
340 void **wgContext);
341
342 /* Delete the WriterGroup SecurityPolicy context */
343 void (*deleteContext)(void *wgContext);
344
345 /* Set the keys and nonce for the WriterGroup. This is returned from the
346 * GetSecurityKeys method of a Security Key Service (SKS). Otherwise, set
347 * manually via out-of-band transmission of the keys. */
349 (*setSecurityKeys)(void *wgContext,
350 const UA_ByteString *signingKey,
351 const UA_ByteString *encryptingKey,
352 const UA_ByteString *keyNonce)
353 ;
354
355 /* The nonce is contained in the NetworkMessage SecurityHeader. Set before
356 * each en-/decryption step. */
358 (*setMessageNonce)(void *wgContext,
359 const UA_ByteString *nonce)
360 ;
361
362 const UA_Logger *logger;
363
364 /* Deletes the dynamic content of the policy */
365 void (*clear)(UA_PubSubSecurityPolicy *policy);
366 void *policyContext;
367};
368
369#endif
370
372
373#endif /* UA_PLUGIN_SECURITYPOLICY_H_ */
#define _UA_BEGIN_DECLS
#undef UA_DEBUG_DUMP_PKGS
Definition config.h:89
#define _UA_END_DECLS
Definition config.h:96
_UA_BEGIN_DECLS const UA_String UA_SECURITY_POLICY_NONE_URI
This Source Code Form is subject to the terms of the Mozilla Public License, v.
UA_SecurityPolicyCryptoModule cryptoModule
UA_SecurityPolicyEncryptionAlgorithm encryptionAlgorithm
UA_SecurityPolicySignatureAlgorithm signatureAlgorithm
UA_SecurityPolicyCryptoModule cryptoModule
UA_SecurityPolicyChannelModule channelModule
UA_SecurityPolicySignatureAlgorithm certificateSigningAlgorithm
UA_ByteString localCertificate
void(* clear)(UA_SecurityPolicy *policy)
UA_SecurityPolicySymmetricModule symmetricModule
UA_SecurityPolicyAsymmetricModule asymmetricModule
UA_StatusCode(* updateCertificateAndPrivateKey)(UA_SecurityPolicy *policy, const UA_ByteString newCertificate, const UA_ByteString newPrivateKey)
const UA_Logger * logger
uint32_t UA_StatusCode
Definition types.h:77