5
5
import asyncio
6
6
import logging
7
7
import math
8
- from cryptography import x509
9
8
from datetime import timedelta , datetime
10
9
import socket
11
10
from urllib .parse import urlparse
@@ -113,7 +112,6 @@ def __init__(self, iserver: InternalServer = None, user_manager=None):
113
112
]
114
113
# allow all certificates by default
115
114
self ._permission_ruleset = SimpleRoleRuleset ()
116
- self .certificate : Optional [x509 .Certificate ] = None
117
115
# Use acceptable limits
118
116
buffer_sz = 65535
119
117
max_msg_sz = 100 * 1024 * 1024 # 100mb
@@ -233,7 +231,7 @@ async def load_certificate(self, path_or_content: Union[str, bytes, Path], forma
233
231
"""
234
232
load server certificate from file, either pem or der
235
233
"""
236
- self .certificate = await uacrypto .load_certificate (path_or_content , format )
234
+ self .iserver . certificate = await uacrypto .load_certificate (path_or_content , format )
237
235
238
236
async def load_private_key (self , path_or_content : Union [str , Path , bytes ], password = None , format = None ):
239
237
self .iserver .private_key = await uacrypto .load_private_key (path_or_content , password , format )
@@ -385,7 +383,7 @@ async def _setup_server_nodes(self):
385
383
for policy_type in self ._security_policy :
386
384
policy , mode , level = security_policies .SECURITY_POLICY_TYPE_MAP [policy_type ]
387
385
if policy is not security_policies .SecurityPolicyNone and not (
388
- self .certificate and self .iserver .private_key
386
+ self .iserver . certificate and self .iserver .private_key
389
387
):
390
388
no_cert = True
391
389
continue
@@ -394,7 +392,7 @@ async def _setup_server_nodes(self):
394
392
security_policies .SecurityPolicyFactory (
395
393
policy ,
396
394
mode ,
397
- self .certificate ,
395
+ self .iserver . certificate ,
398
396
self .iserver .private_key ,
399
397
permission_ruleset = self ._permission_ruleset ,
400
398
)
@@ -417,9 +415,21 @@ def _set_endpoints(self, policy, mode, level):
417
415
idtoken = ua .UserTokenPolicy ()
418
416
idtoken .PolicyId = "certificate"
419
417
idtoken .TokenType = ua .UserTokenType .Certificate
420
- idtoken .SecurityPolicyUri = policy .URI
421
- # TODO request signing if mode == ua.MessageSecurityMode.None_ (also need to verify signature then)
422
- idtokens .append (idtoken )
418
+ # always request signing
419
+ if mode == ua .MessageSecurityMode .None_ :
420
+ # find first policy with signing
421
+ for token_policy_type in self ._security_policy :
422
+ token_policy , token_mode , _ = security_policies .SECURITY_POLICY_TYPE_MAP [token_policy_type ]
423
+ if token_mode == ua .MessageSecurityMode .None_ :
424
+ continue
425
+ idtoken .SecurityPolicyUri = token_policy .URI
426
+ idtokens .append (idtoken )
427
+ break
428
+ else :
429
+ _logger .warning ("No signing policy available, user certificate cannot get verified" )
430
+ else :
431
+ idtoken .SecurityPolicyUri = policy .URI
432
+ idtokens .append (idtoken )
423
433
424
434
if ua .UserNameIdentityToken in tokens :
425
435
idtoken = ua .UserTokenPolicy ()
@@ -432,7 +442,7 @@ def _set_endpoints(self, policy, mode, level):
432
442
# use same policy for encryption
433
443
idtoken .SecurityPolicyUri = policy .URI
434
444
# try to avoid plaintext password, find first policy with encryption
435
- elif self .certificate and self .iserver .private_key :
445
+ elif self .iserver . certificate and self .iserver .private_key :
436
446
for token_policy_type in self ._security_policy :
437
447
token_policy , token_mode , _ = security_policies .SECURITY_POLICY_TYPE_MAP [token_policy_type ]
438
448
if token_mode != ua .MessageSecurityMode .SignAndEncrypt :
@@ -457,8 +467,8 @@ def _set_endpoints(self, policy, mode, level):
457
467
edp = ua .EndpointDescription ()
458
468
edp .EndpointUrl = self .endpoint .geturl ()
459
469
edp .Server = appdesc
460
- if self .certificate :
461
- edp .ServerCertificate = uacrypto .der_from_x509 (self .certificate )
470
+ if self .iserver . certificate :
471
+ edp .ServerCertificate = uacrypto .der_from_x509 (self .iserver . certificate )
462
472
edp .SecurityMode = mode
463
473
edp .SecurityPolicyUri = policy .URI
464
474
edp .UserIdentityTokens = idtokens
@@ -473,9 +483,9 @@ async def start(self):
473
483
"""
474
484
Start to listen on network
475
485
"""
476
- if self .certificate is not None :
486
+ if self .iserver . certificate is not None :
477
487
# Log warnings about the certificate
478
- uacrypto .check_certificate (self .certificate , self ._application_uri , socket .gethostname ())
488
+ uacrypto .check_certificate (self .iserver . certificate , self ._application_uri , socket .gethostname ())
479
489
await self ._setup_server_nodes ()
480
490
await self .iserver .start ()
481
491
try :
0 commit comments