| 1 |
/* |
| 2 |
* Copyright(c) 2006 to 2021 ZettaScale Technology and others |
| 3 |
* |
| 4 |
* This program and the accompanying materials are made available under the |
| 5 |
* terms of the Eclipse Public License v. 2.0 which is available at |
| 6 |
* http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License |
| 7 |
* v. 1.0 which is available at |
| 8 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 |
* |
| 10 |
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 11 |
*/ |
| 12 |
#include <string.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include "dds/dds.h" |
| 15 |
#include "dds/ddsrt/heap.h" |
| 16 |
#include "dds/ddsrt/sync.h" |
| 17 |
#include "dds/ddsrt/string.h" |
| 18 |
#include "dds/ddsi/ddsi_domaingv.h" |
| 19 |
#include "dds/security/dds_security_api.h" |
| 20 |
#include "dds/security/core/dds_security_utils.h" |
| 21 |
#include "authentication_wrapper.h" |
| 22 |
#include "test_identity.h" |
| 23 |
#include "plugin_wrapper_msg_q.h" |
| 24 |
|
| 25 |
int init_authentication(const char *argument, void **context, struct ddsi_domaingv *gv); |
| 26 |
int finalize_authentication(void *context); |
| 27 |
|
| 28 |
enum auth_plugin_mode { |
| 29 |
PLUGIN_MODE_ALL_OK, |
| 30 |
PLUGIN_MODE_WRAPPED, |
| 31 |
PLUGIN_MODE_MISSING_FUNC, |
| 32 |
PLUGIN_MODE_INIT_FAIL |
| 33 |
}; |
| 34 |
|
| 35 |
/** |
| 36 |
* Implementation structure for storing encapsulated members of the instance |
| 37 |
* while giving only the interface definition to user |
| 38 |
*/ |
| 39 |
struct dds_security_authentication_impl |
| 40 |
{ |
| 41 |
dds_security_authentication base; |
| 42 |
dds_security_authentication *instance; |
| 43 |
struct message_queue msg_queue; |
| 44 |
enum auth_plugin_mode mode; |
| 45 |
}; |
| 46 |
|
| 47 |
static struct dds_security_authentication_impl **auth_impl; |
| 48 |
static size_t auth_impl_idx = 0; |
| 49 |
static size_t auth_impl_use = 0; |
| 50 |
|
| 51 |
static const char *test_identity_certificate = TEST_IDENTITY_CERTIFICATE_DUMMY; |
| 52 |
static const char *test_private_key = TEST_IDENTITY_PRIVATE_KEY_DUMMY; |
| 53 |
static const char *test_ca_certificate = TEST_IDENTITY_CA_CERTIFICATE_DUMMY; |
| 54 |
|
| 55 |
static DDS_Security_ValidationResult_t test_validate_local_identity_all_ok( |
| 56 |
DDS_Security_GUID_t *adjusted_participant_guid, |
| 57 |
const DDS_Security_Qos *participant_qos, |
| 58 |
const DDS_Security_GUID_t *candidate_participant_guid) |
| 59 |
{ |
| 60 |
DDS_Security_ValidationResult_t result = DDS_SECURITY_VALIDATION_OK; |
| 61 |
char *identity_ca = NULL; |
| 62 |
char *identity_certificate = NULL; |
| 63 |
char *private_key = NULL; |
| 64 |
|
| 65 |
memcpy(adjusted_participant_guid, candidate_participant_guid, sizeof(*adjusted_participant_guid)); |
| 66 |
for (unsigned i = 0; i < participant_qos->property.value._length; i++) |
| 67 |
{ |
| 68 |
printf("%s\n", participant_qos->property.value._buffer[i].name); |
| 69 |
if (!strcmp(participant_qos->property.value._buffer[i].name, DDS_SEC_PROP_AUTH_PRIV_KEY)) |
| 70 |
private_key = participant_qos->property.value._buffer[i].value; |
| 71 |
else if (!strcmp(participant_qos->property.value._buffer[i].name, DDS_SEC_PROP_AUTH_IDENTITY_CA)) |
| 72 |
identity_ca = participant_qos->property.value._buffer[i].value; |
| 73 |
else if (!strcmp(participant_qos->property.value._buffer[i].name, DDS_SEC_PROP_AUTH_IDENTITY_CERT)) |
| 74 |
identity_certificate = participant_qos->property.value._buffer[i].value; |
| 75 |
} |
| 76 |
|
| 77 |
assert(identity_certificate != NULL); |
| 78 |
assert(identity_ca != NULL); |
| 79 |
assert(private_key != NULL); |
| 80 |
|
| 81 |
if (strcmp(identity_certificate, test_identity_certificate)) |
| 82 |
{ |
| 83 |
printf("identity received=%s\n", identity_certificate); |
| 84 |
printf("identity expected=%s\n", test_identity_certificate); |
| 85 |
result = DDS_SECURITY_VALIDATION_FAILED; |
| 86 |
printf("FAILED: Could not get identity_certificate value properly\n"); |
| 87 |
} |
| 88 |
else if (strcmp(identity_ca, test_ca_certificate)) |
| 89 |
{ |
| 90 |
printf("ca received=%s\n", identity_ca); |
| 91 |
printf("ca expected=%s\n", test_ca_certificate); |
| 92 |
printf("FAILED: Could not get identity_ca value properly\n"); |
| 93 |
result = DDS_SECURITY_VALIDATION_FAILED; |
| 94 |
} |
| 95 |
else if (strcmp(private_key, test_private_key)) |
| 96 |
{ |
| 97 |
printf("FAILED: Could not get private_key value properly\n"); |
| 98 |
result = DDS_SECURITY_VALIDATION_FAILED; |
| 99 |
} |
| 100 |
if (result == DDS_SECURITY_VALIDATION_OK) |
| 101 |
printf("DDS_SECURITY_VALIDATION_OK\n"); |
| 102 |
|
| 103 |
return result; |
| 104 |
} |
| 105 |
|
| 106 |
static DDS_Security_ValidationResult_t test_validate_local_identity( |
| 107 |
dds_security_authentication *instance, |
| 108 |
DDS_Security_IdentityHandle *local_identity_handle, |
| 109 |
DDS_Security_GUID_t *adjusted_participant_guid, |
| 110 |
const DDS_Security_DomainId domain_id, |
| 111 |
const DDS_Security_Qos *participant_qos, |
| 112 |
const DDS_Security_GUID_t *candidate_participant_guid, |
| 113 |
DDS_Security_SecurityException *ex) |
| 114 |
{ |
| 115 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 116 |
switch (impl->mode) |
| 117 |
{ |
| 118 |
case PLUGIN_MODE_WRAPPED: |
| 119 |
{ |
| 120 |
DDS_Security_ValidationResult_t result = impl->instance->validate_local_identity( |
| 121 |
impl->instance, local_identity_handle, adjusted_participant_guid, domain_id, participant_qos, candidate_participant_guid, ex); |
| 122 |
add_message(&impl->msg_queue, MESSAGE_KIND_VALIDATE_LOCAL_IDENTITY, *local_identity_handle, |
| 123 |
0, 0, adjusted_participant_guid, NULL, result, ex ? ex->message : "", NULL, instance); |
| 124 |
return result; |
| 125 |
} |
| 126 |
case PLUGIN_MODE_ALL_OK: |
| 127 |
default: |
| 128 |
return test_validate_local_identity_all_ok(adjusted_participant_guid, participant_qos, candidate_participant_guid); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
static DDS_Security_boolean test_get_identity_token(dds_security_authentication *instance, |
| 133 |
DDS_Security_IdentityToken *identity_token, |
| 134 |
const DDS_Security_IdentityHandle handle, |
| 135 |
DDS_Security_SecurityException *ex) |
| 136 |
{ |
| 137 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 138 |
switch (impl->mode) |
| 139 |
{ |
| 140 |
case PLUGIN_MODE_WRAPPED: |
| 141 |
return impl->instance->get_identity_token(impl->instance, identity_token, handle, ex); |
| 142 |
|
| 143 |
case PLUGIN_MODE_ALL_OK: |
| 144 |
default: |
| 145 |
{ |
| 146 |
memset(identity_token, 0, sizeof(*identity_token)); |
| 147 |
identity_token->class_id = ddsrt_strdup(""); |
| 148 |
return true; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
static DDS_Security_boolean test_get_identity_status_token( |
| 154 |
dds_security_authentication *instance, |
| 155 |
DDS_Security_IdentityStatusToken *identity_status_token, |
| 156 |
const DDS_Security_IdentityHandle handle, |
| 157 |
DDS_Security_SecurityException *ex) |
| 158 |
{ |
| 159 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 160 |
switch (impl->mode) |
| 161 |
{ |
| 162 |
case PLUGIN_MODE_WRAPPED: |
| 163 |
return impl->instance->get_identity_status_token(impl->instance, identity_status_token, handle, ex); |
| 164 |
case PLUGIN_MODE_ALL_OK: |
| 165 |
default: |
| 166 |
return true; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static DDS_Security_boolean test_set_permissions_credential_and_token( |
| 171 |
dds_security_authentication *instance, |
| 172 |
const DDS_Security_IdentityHandle handle, |
| 173 |
const DDS_Security_PermissionsCredentialToken *permissions_credential, |
| 174 |
const DDS_Security_PermissionsToken *permissions_token, |
| 175 |
DDS_Security_SecurityException *ex) |
| 176 |
{ |
| 177 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 178 |
switch (impl->mode) |
| 179 |
{ |
| 180 |
case PLUGIN_MODE_WRAPPED: |
| 181 |
return impl->instance->set_permissions_credential_and_token(impl->instance, handle, permissions_credential, permissions_token, ex); |
| 182 |
case PLUGIN_MODE_ALL_OK: |
| 183 |
default: |
| 184 |
return true; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
static DDS_Security_ValidationResult_t test_validate_remote_identity( |
| 189 |
dds_security_authentication *instance, |
| 190 |
DDS_Security_IdentityHandle *remote_identity_handle, |
| 191 |
DDS_Security_AuthRequestMessageToken *local_auth_request_token, |
| 192 |
const DDS_Security_AuthRequestMessageToken *remote_auth_request_token, |
| 193 |
const DDS_Security_IdentityHandle local_identity_handle, |
| 194 |
const DDS_Security_IdentityToken *remote_identity_token, |
| 195 |
const DDS_Security_GUID_t *remote_participant_guid, |
| 196 |
DDS_Security_SecurityException *ex) |
| 197 |
{ |
| 198 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 199 |
switch (impl->mode) |
| 200 |
{ |
| 201 |
case PLUGIN_MODE_WRAPPED: |
| 202 |
{ |
| 203 |
DDS_Security_ValidationResult_t result = impl->instance->validate_remote_identity( |
| 204 |
impl->instance, remote_identity_handle, local_auth_request_token, remote_auth_request_token, |
| 205 |
local_identity_handle, remote_identity_token, remote_participant_guid, ex); |
| 206 |
add_message(&impl->msg_queue, MESSAGE_KIND_VALIDATE_REMOTE_IDENTITY, local_identity_handle, |
| 207 |
*remote_identity_handle, 0, NULL, remote_participant_guid, result, ex ? ex->message : "", local_auth_request_token, instance); |
| 208 |
return result; |
| 209 |
} |
| 210 |
|
| 211 |
case PLUGIN_MODE_ALL_OK: |
| 212 |
default: |
| 213 |
return DDS_SECURITY_VALIDATION_OK; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
static DDS_Security_ValidationResult_t test_begin_handshake_request( |
| 218 |
dds_security_authentication *instance, |
| 219 |
DDS_Security_HandshakeHandle *handshake_handle, |
| 220 |
DDS_Security_HandshakeMessageToken *handshake_message, |
| 221 |
const DDS_Security_IdentityHandle initiator_identity_handle, |
| 222 |
const DDS_Security_IdentityHandle replier_identity_handle, |
| 223 |
const DDS_Security_OctetSeq *serialized_local_participant_data, |
| 224 |
DDS_Security_SecurityException *ex) |
| 225 |
{ |
| 226 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 227 |
switch (impl->mode) |
| 228 |
{ |
| 229 |
case PLUGIN_MODE_WRAPPED: |
| 230 |
{ |
| 231 |
DDS_Security_ValidationResult_t result = impl->instance->begin_handshake_request( |
| 232 |
impl->instance, handshake_handle, handshake_message, initiator_identity_handle, |
| 233 |
replier_identity_handle, serialized_local_participant_data, ex); |
| 234 |
add_message(&impl->msg_queue, MESSAGE_KIND_BEGIN_HANDSHAKE_REQUEST, initiator_identity_handle, |
| 235 |
replier_identity_handle, *handshake_handle, NULL, NULL, result, ex ? ex->message : "", handshake_message, instance); |
| 236 |
return result; |
| 237 |
} |
| 238 |
|
| 239 |
case PLUGIN_MODE_ALL_OK: |
| 240 |
default: |
| 241 |
return DDS_SECURITY_VALIDATION_OK; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
static DDS_Security_ValidationResult_t test_begin_handshake_reply( |
| 246 |
dds_security_authentication *instance, |
| 247 |
DDS_Security_HandshakeHandle *handshake_handle, |
| 248 |
DDS_Security_HandshakeMessageToken *handshake_message_out, |
| 249 |
const DDS_Security_HandshakeMessageToken *handshake_message_in, |
| 250 |
const DDS_Security_IdentityHandle initiator_identity_handle, |
| 251 |
const DDS_Security_IdentityHandle replier_identity_handle, |
| 252 |
const DDS_Security_OctetSeq *serialized_local_participant_data, |
| 253 |
DDS_Security_SecurityException *ex) |
| 254 |
{ |
| 255 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 256 |
switch (impl->mode) |
| 257 |
{ |
| 258 |
case PLUGIN_MODE_WRAPPED: |
| 259 |
{ |
| 260 |
DDS_Security_ValidationResult_t result = impl->instance->begin_handshake_reply( |
| 261 |
impl->instance, handshake_handle, handshake_message_out, handshake_message_in, |
| 262 |
initiator_identity_handle, replier_identity_handle, serialized_local_participant_data, ex); |
| 263 |
add_message(&impl->msg_queue, MESSAGE_KIND_BEGIN_HANDSHAKE_REPLY, replier_identity_handle, |
| 264 |
initiator_identity_handle, *handshake_handle, NULL, NULL, result, ex ? ex->message : "", handshake_message_out, instance); |
| 265 |
return result; |
| 266 |
} |
| 267 |
|
| 268 |
case PLUGIN_MODE_ALL_OK: |
| 269 |
default: |
| 270 |
return DDS_SECURITY_VALIDATION_OK; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
static DDS_Security_ValidationResult_t test_process_handshake( |
| 275 |
dds_security_authentication *instance, |
| 276 |
DDS_Security_HandshakeMessageToken *handshake_message_out, |
| 277 |
const DDS_Security_HandshakeMessageToken *handshake_message_in, |
| 278 |
const DDS_Security_HandshakeHandle handshake_handle, |
| 279 |
DDS_Security_SecurityException *ex) |
| 280 |
{ |
| 281 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 282 |
switch (impl->mode) |
| 283 |
{ |
| 284 |
case PLUGIN_MODE_WRAPPED: |
| 285 |
{ |
| 286 |
DDS_Security_ValidationResult_t result = impl->instance->process_handshake(impl->instance, handshake_message_out, handshake_message_in, handshake_handle, ex); |
| 287 |
add_message(&impl->msg_queue, MESSAGE_KIND_PROCESS_HANDSHAKE, 0, 0, handshake_handle, |
| 288 |
NULL, NULL, result, ex ? ex->message : "", handshake_message_out, instance); |
| 289 |
return result; |
| 290 |
} |
| 291 |
|
| 292 |
case PLUGIN_MODE_ALL_OK: |
| 293 |
default: |
| 294 |
return DDS_SECURITY_VALIDATION_OK; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
static DDS_Security_SharedSecretHandle test_get_shared_secret( |
| 299 |
dds_security_authentication *instance, |
| 300 |
const DDS_Security_HandshakeHandle handshake_handle, |
| 301 |
DDS_Security_SecurityException *ex) |
| 302 |
{ |
| 303 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 304 |
switch (impl->mode) |
| 305 |
{ |
| 306 |
case PLUGIN_MODE_WRAPPED: |
| 307 |
return impl->instance->get_shared_secret(impl->instance, handshake_handle, ex); |
| 308 |
case PLUGIN_MODE_ALL_OK: |
| 309 |
default: |
| 310 |
return 0; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
static DDS_Security_boolean test_get_authenticated_peer_credential_token( |
| 315 |
dds_security_authentication *instance, |
| 316 |
DDS_Security_AuthenticatedPeerCredentialToken *peer_credential_token, |
| 317 |
const DDS_Security_HandshakeHandle handshake_handle, |
| 318 |
DDS_Security_SecurityException *ex) |
| 319 |
{ |
| 320 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 321 |
switch (impl->mode) |
| 322 |
{ |
| 323 |
case PLUGIN_MODE_WRAPPED: |
| 324 |
return impl->instance->get_authenticated_peer_credential_token(impl->instance, peer_credential_token, handshake_handle, ex); |
| 325 |
case PLUGIN_MODE_ALL_OK: |
| 326 |
default: |
| 327 |
return true; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
static DDS_Security_boolean test_set_listener(dds_security_authentication *instance, |
| 332 |
const dds_security_authentication_listener *listener, |
| 333 |
DDS_Security_SecurityException *ex) |
| 334 |
{ |
| 335 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 336 |
switch (impl->mode) |
| 337 |
{ |
| 338 |
case PLUGIN_MODE_WRAPPED: |
| 339 |
return impl->instance->set_listener(impl->instance, listener, ex); |
| 340 |
case PLUGIN_MODE_ALL_OK: |
| 341 |
default: |
| 342 |
return true; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
static DDS_Security_boolean test_return_identity_token(dds_security_authentication *instance, |
| 347 |
const DDS_Security_IdentityToken *token, |
| 348 |
DDS_Security_SecurityException *ex) |
| 349 |
{ |
| 350 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 351 |
switch (impl->mode) |
| 352 |
{ |
| 353 |
case PLUGIN_MODE_WRAPPED: |
| 354 |
return impl->instance->return_identity_token(impl->instance, token, ex); |
| 355 |
case PLUGIN_MODE_ALL_OK: |
| 356 |
default: |
| 357 |
if (token->class_id) |
| 358 |
ddsrt_free (token->class_id); |
| 359 |
return true; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
static DDS_Security_boolean test_return_identity_status_token( |
| 364 |
dds_security_authentication *instance, |
| 365 |
const DDS_Security_IdentityStatusToken *token, |
| 366 |
DDS_Security_SecurityException *ex) |
| 367 |
{ |
| 368 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 369 |
switch (impl->mode) |
| 370 |
{ |
| 371 |
case PLUGIN_MODE_WRAPPED: |
| 372 |
return impl->instance->return_identity_status_token(impl->instance, token, ex); |
| 373 |
case PLUGIN_MODE_ALL_OK: |
| 374 |
default: |
| 375 |
return true; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
static DDS_Security_boolean test_return_authenticated_peer_credential_token( |
| 380 |
dds_security_authentication *instance, |
| 381 |
const DDS_Security_AuthenticatedPeerCredentialToken *peer_credential_token, |
| 382 |
DDS_Security_SecurityException *ex) |
| 383 |
{ |
| 384 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 385 |
switch (impl->mode) |
| 386 |
{ |
| 387 |
case PLUGIN_MODE_WRAPPED: |
| 388 |
return impl->instance->return_authenticated_peer_credential_token(impl->instance, peer_credential_token, ex); |
| 389 |
case PLUGIN_MODE_ALL_OK: |
| 390 |
default: |
| 391 |
return true; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
static DDS_Security_boolean test_return_handshake_handle(dds_security_authentication *instance, |
| 396 |
const DDS_Security_HandshakeHandle handshake_handle, |
| 397 |
DDS_Security_SecurityException *ex) |
| 398 |
{ |
| 399 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 400 |
switch (impl->mode) |
| 401 |
{ |
| 402 |
case PLUGIN_MODE_WRAPPED: |
| 403 |
return impl->instance->return_handshake_handle(impl->instance, handshake_handle, ex); |
| 404 |
case PLUGIN_MODE_ALL_OK: |
| 405 |
default: |
| 406 |
return true; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
static DDS_Security_boolean test_return_identity_handle( |
| 411 |
dds_security_authentication *instance, |
| 412 |
const DDS_Security_IdentityHandle identity_handle, |
| 413 |
DDS_Security_SecurityException *ex) |
| 414 |
{ |
| 415 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 416 |
switch (impl->mode) |
| 417 |
{ |
| 418 |
case PLUGIN_MODE_WRAPPED: |
| 419 |
return impl->instance->return_identity_handle(impl->instance, identity_handle, ex); |
| 420 |
default: |
| 421 |
return true; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
static DDS_Security_boolean test_return_sharedsecret_handle( |
| 426 |
dds_security_authentication *instance, |
| 427 |
const DDS_Security_SharedSecretHandle sharedsecret_handle, |
| 428 |
DDS_Security_SecurityException *ex) |
| 429 |
{ |
| 430 |
struct dds_security_authentication_impl *impl = (struct dds_security_authentication_impl *)instance; |
| 431 |
switch (impl->mode) |
| 432 |
{ |
| 433 |
case PLUGIN_MODE_WRAPPED: |
| 434 |
return impl->instance->return_sharedsecret_handle(impl->instance, sharedsecret_handle, ex); |
| 435 |
case PLUGIN_MODE_ALL_OK: |
| 436 |
default: |
| 437 |
return true; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
static struct dds_security_authentication_impl * get_impl_for_domain(dds_domainid_t domain_id) |
| 442 |
{ |
| 443 |
for (size_t i = 0; i < auth_impl_idx; i++) |
| 444 |
{ |
| 445 |
if (auth_impl[i] && auth_impl[i]->instance->gv->config.domainId == domain_id) |
| 446 |
{ |
| 447 |
return auth_impl[i]; |
| 448 |
} |
| 449 |
} |
| 450 |
return NULL; |
| 451 |
} |
| 452 |
|
| 453 |
enum take_message_result test_authentication_plugin_take_msg(dds_domainid_t domain_id, message_kind_t kind, DDS_Security_IdentityHandle lidHandle, DDS_Security_IdentityHandle ridHandle, DDS_Security_IdentityHandle hsHandle, dds_time_t abstimeout, struct message **msg) |
| 454 |
{ |
| 455 |
struct dds_security_authentication_impl *impl = get_impl_for_domain(domain_id); |
| 456 |
assert(impl); |
| 457 |
return take_message(&impl->msg_queue, kind, lidHandle, ridHandle, hsHandle, abstimeout, msg); |
| 458 |
} |
| 459 |
|
| 460 |
void test_authentication_plugin_release_msg(struct message *msg) |
| 461 |
{ |
| 462 |
delete_message(msg); |
| 463 |
} |
| 464 |
|
| 465 |
static struct dds_security_authentication_impl * init_test_authentication_common(void) |
| 466 |
{ |
| 467 |
struct dds_security_authentication_impl * impl = ddsrt_malloc(sizeof(*impl)); |
| 468 |
memset(impl, 0, sizeof(*impl)); |
| 469 |
impl->base.validate_local_identity = &test_validate_local_identity; |
| 470 |
impl->base.get_identity_token = &test_get_identity_token; |
| 471 |
impl->base.get_identity_status_token = &test_get_identity_status_token; |
| 472 |
impl->base.set_permissions_credential_and_token = &test_set_permissions_credential_and_token; |
| 473 |
impl->base.validate_remote_identity = &test_validate_remote_identity; |
| 474 |
impl->base.begin_handshake_request = &test_begin_handshake_request; |
| 475 |
impl->base.begin_handshake_reply = &test_begin_handshake_reply; |
| 476 |
impl->base.process_handshake = &test_process_handshake; |
| 477 |
impl->base.get_shared_secret = &test_get_shared_secret; |
| 478 |
impl->base.get_authenticated_peer_credential_token = &test_get_authenticated_peer_credential_token; |
| 479 |
impl->base.set_listener = &test_set_listener; |
| 480 |
impl->base.return_identity_token = &test_return_identity_token; |
| 481 |
impl->base.return_identity_status_token = &test_return_identity_status_token; |
| 482 |
impl->base.return_authenticated_peer_credential_token = &test_return_authenticated_peer_credential_token; |
| 483 |
impl->base.return_handshake_handle = &test_return_handshake_handle; |
| 484 |
impl->base.return_identity_handle = &test_return_identity_handle; |
| 485 |
impl->base.return_sharedsecret_handle = &test_return_sharedsecret_handle; |
| 486 |
return impl; |
| 487 |
} |
| 488 |
|
| 489 |
int init_test_authentication_all_ok(const char *argument, void **context, struct ddsi_domaingv *gv) |
| 490 |
{ |
| 491 |
DDSRT_UNUSED_ARG(argument); |
| 492 |
DDSRT_UNUSED_ARG(context); |
| 493 |
DDSRT_UNUSED_ARG(gv); |
| 494 |
struct dds_security_authentication_impl *impl = init_test_authentication_common(); |
| 495 |
impl->mode = PLUGIN_MODE_ALL_OK; |
| 496 |
*context = impl; |
| 497 |
return 0; |
| 498 |
} |
| 499 |
|
| 500 |
int finalize_test_authentication_all_ok(void *context) |
| 501 |
{ |
| 502 |
assert(((struct dds_security_authentication_impl *)context)->mode == PLUGIN_MODE_ALL_OK); |
| 503 |
ddsrt_free(context); |
| 504 |
return 0; |
| 505 |
} |
| 506 |
|
| 507 |
int init_test_authentication_missing_func(const char *argument, void **context, struct ddsi_domaingv *gv) |
| 508 |
{ |
| 509 |
DDSRT_UNUSED_ARG(argument); |
| 510 |
DDSRT_UNUSED_ARG(context); |
| 511 |
DDSRT_UNUSED_ARG(gv); |
| 512 |
struct dds_security_authentication_impl *impl = init_test_authentication_common(); |
| 513 |
impl->base.get_shared_secret = NULL; |
| 514 |
impl->mode = PLUGIN_MODE_MISSING_FUNC; |
| 515 |
*context = impl; |
| 516 |
return 0; |
| 517 |
} |
| 518 |
|
| 519 |
int finalize_test_authentication_missing_func(void *context) |
| 520 |
{ |
| 521 |
assert(((struct dds_security_authentication_impl *)context)->mode == PLUGIN_MODE_MISSING_FUNC); |
| 522 |
ddsrt_free(context); |
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
int init_test_authentication_init_error(const char *argument, void **context, struct ddsi_domaingv *gv) |
| 527 |
{ |
| 528 |
DDSRT_UNUSED_ARG(argument); |
| 529 |
DDSRT_UNUSED_ARG(context); |
| 530 |
DDSRT_UNUSED_ARG(gv); |
| 531 |
return 1; |
| 532 |
} |
| 533 |
|
| 534 |
int finalize_test_authentication_init_error(void *context) |
| 535 |
{ |
| 536 |
DDSRT_UNUSED_ARG(context); |
| 537 |
return 0; |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* Init and fini functions for using wrapped mode for the authentication plugin. |
| 542 |
* These functions assumes that there are no concurrent calls, as the static |
| 543 |
* variables used here are not protected by a lock. */ |
| 544 |
int init_test_authentication_wrapped(const char *argument, void **context, struct ddsi_domaingv *gv) |
| 545 |
{ |
| 546 |
int ret; |
| 547 |
struct dds_security_authentication_impl *impl = init_test_authentication_common(); |
| 548 |
impl->mode = PLUGIN_MODE_WRAPPED; |
| 549 |
|
| 550 |
init_message_queue(&impl->msg_queue); |
| 551 |
ret = init_authentication(argument, (void **)&impl->instance, gv); |
| 552 |
auth_impl_idx++; |
| 553 |
auth_impl = ddsrt_realloc(auth_impl, auth_impl_idx * sizeof(*auth_impl)); |
| 554 |
auth_impl[auth_impl_idx - 1] = impl; |
| 555 |
auth_impl_use++; |
| 556 |
*context = impl; |
| 557 |
return ret; |
| 558 |
} |
| 559 |
|
| 560 |
int finalize_test_authentication_wrapped(void *context) |
| 561 |
{ |
| 562 |
int ret; |
| 563 |
struct dds_security_authentication_impl *impl = context; |
| 564 |
assert(impl->mode == PLUGIN_MODE_WRAPPED); |
| 565 |
deinit_message_queue(&impl->msg_queue); |
| 566 |
ret = finalize_authentication(impl->instance); |
| 567 |
|
| 568 |
size_t idx; |
| 569 |
for (idx = 0; idx < auth_impl_idx; idx++) |
| 570 |
if (auth_impl[idx] == impl) |
| 571 |
break; |
| 572 |
assert (idx < auth_impl_idx); |
| 573 |
auth_impl[idx] = NULL; |
| 574 |
|
| 575 |
ddsrt_free(context); |
| 576 |
|
| 577 |
if (--auth_impl_use == 0) |
| 578 |
{ |
| 579 |
ddsrt_free (auth_impl); |
| 580 |
auth_impl = NULL; |
| 581 |
auth_impl_idx = 0; |
| 582 |
} |
| 583 |
|
| 584 |
return ret; |
| 585 |
} |
| 586 |
|