| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
from typing import Callable, Optional, Type, Union, TYPE_CHECKING, List |
| 21 |
|
| 22 |
from cproton import PN_EOS, PN_OK, PN_SASL_AUTH, PN_SASL_NONE, PN_SASL_OK, PN_SASL_PERM, PN_SASL_SYS, PN_SASL_TEMP, \ |
| 23 |
PN_SSL_ANONYMOUS_PEER, PN_SSL_CERT_SUBJECT_CITY_OR_LOCALITY, PN_SSL_CERT_SUBJECT_COMMON_NAME, \ |
| 24 |
PN_SSL_CERT_SUBJECT_COUNTRY_NAME, PN_SSL_CERT_SUBJECT_ORGANIZATION_NAME, PN_SSL_CERT_SUBJECT_ORGANIZATION_UNIT, \ |
| 25 |
PN_SSL_CERT_SUBJECT_STATE_OR_PROVINCE, PN_SSL_MD5, PN_SSL_MODE_CLIENT, PN_SSL_MODE_SERVER, PN_SSL_RESUME_NEW, \ |
| 26 |
PN_SSL_RESUME_REUSED, PN_SSL_RESUME_UNKNOWN, PN_SSL_SHA1, PN_SSL_SHA256, PN_SSL_SHA512, PN_SSL_VERIFY_PEER, \ |
| 27 |
PN_SSL_VERIFY_PEER_NAME, PN_TRACE_DRV, PN_TRACE_FRM, PN_TRACE_OFF, PN_TRACE_RAW, pn_error_text, pn_sasl, \ |
| 28 |
pn_sasl_allowed_mechs, pn_sasl_config_name, pn_sasl_config_path, pn_sasl_done, pn_sasl_extended, \ |
| 29 |
pn_sasl_get_allow_insecure_mechs, pn_sasl_get_mech, pn_sasl_get_user, pn_sasl_get_authorization, pn_sasl_outcome, \ |
| 30 |
pn_sasl_set_allow_insecure_mechs, pn_ssl, pn_ssl_domain, pn_ssl_domain_allow_unsecured_client, pn_ssl_domain_free, \ |
| 31 |
pn_ssl_domain_set_credentials, pn_ssl_domain_set_peer_authentication, pn_ssl_domain_set_trusted_ca_db, \ |
| 32 |
pn_ssl_get_cert_fingerprint, pn_ssl_get_cipher_name, pn_ssl_get_peer_hostname, pn_ssl_get_protocol_name, \ |
| 33 |
pn_ssl_get_remote_subject, pn_ssl_get_remote_subject_subfield, pn_ssl_init, pn_ssl_present, pn_ssl_resume_status, \ |
| 34 |
pn_ssl_set_peer_hostname, pn_transport, pn_transport_attachments, pn_transport_bind, pn_transport_capacity, \ |
| 35 |
pn_transport_close_head, pn_transport_close_tail, pn_transport_closed, pn_transport_condition, \ |
| 36 |
pn_transport_connection, pn_transport_error, pn_transport_get_channel_max, pn_transport_get_frames_input, \ |
| 37 |
pn_transport_get_frames_output, pn_transport_get_idle_timeout, pn_transport_get_max_frame, \ |
| 38 |
pn_transport_get_pytracer, pn_transport_get_remote_idle_timeout, pn_transport_get_remote_max_frame, \ |
| 39 |
pn_transport_get_user, pn_transport_is_authenticated, pn_transport_is_encrypted, pn_transport_log, \ |
| 40 |
pn_transport_peek, pn_transport_pending, pn_transport_pop, pn_transport_push, pn_transport_remote_channel_max, \ |
| 41 |
pn_transport_require_auth, pn_transport_require_encryption, pn_transport_set_channel_max, \ |
| 42 |
pn_transport_set_idle_timeout, pn_transport_set_max_frame, pn_transport_set_pytracer, pn_transport_set_server, \ |
| 43 |
pn_transport_tick, pn_transport_trace, pn_transport_unbind |
| 44 |
|
| 45 |
from ._common import millis2secs, secs2millis, unicode2utf8, utf82unicode |
| 46 |
from ._condition import cond2obj, obj2cond |
| 47 |
from ._exceptions import EXCEPTIONS, SSLException, SSLUnavailable, SessionException, TransportException |
| 48 |
from ._wrapper import Wrapper |
| 49 |
|
| 50 |
if TYPE_CHECKING: |
| 51 |
from ._condition import Condition |
| 52 |
from ._endpoints import Connection # would produce circular import |
| 53 |
|
| 54 |
|
| 55 |
class TraceAdapter: |
| 56 |
|
| 57 |
def __init__(self, tracer: Callable[['Transport', str], None]) -> None: |
| 58 |
self.tracer = tracer |
| 59 |
|
| 60 |
def __call__(self, trans_impl, message): |
| 61 |
self.tracer(Transport.wrap(trans_impl), message) |
| 62 |
|
| 63 |
|
| 64 |
class Transport(Wrapper): |
| 65 |
""" |
| 66 |
A network channel supporting an AMQP connection. |
| 67 |
""" |
| 68 |
|
| 69 |
TRACE_OFF = PN_TRACE_OFF |
| 70 |
""" Turn logging off entirely. """ |
| 71 |
|
| 72 |
TRACE_DRV = PN_TRACE_DRV |
| 73 |
""" Log driver-related events. """ |
| 74 |
|
| 75 |
TRACE_FRM = PN_TRACE_FRM |
| 76 |
""" Log protocol frames going in and out of the transport. """ |
| 77 |
|
| 78 |
TRACE_RAW = PN_TRACE_RAW |
| 79 |
""" Log raw binary data going in and out of the transport. """ |
| 80 |
|
| 81 |
CLIENT = 1 |
| 82 |
""" Transport mode is as a client. """ |
| 83 |
|
| 84 |
SERVER = 2 |
| 85 |
""" Transport mode is as a server. """ |
| 86 |
|
| 87 |
@staticmethod |
| 88 |
def wrap(impl: Optional[Callable]) -> Optional['Transport']: |
| 89 |
if impl is None: |
| 90 |
return None |
| 91 |
else: |
| 92 |
return Transport(_impl=impl) |
| 93 |
|
| 94 |
def __init__( |
| 95 |
self, |
| 96 |
mode: 'Optional[int]' = None, |
| 97 |
_impl: 'Callable' = pn_transport, |
| 98 |
) -> None: |
| 99 |
Wrapper.__init__(self, _impl, pn_transport_attachments) |
| 100 |
if mode == Transport.SERVER: |
| 101 |
pn_transport_set_server(self._impl) |
| 102 |
elif mode is None or mode == Transport.CLIENT: |
| 103 |
pass |
| 104 |
else: |
| 105 |
raise TransportException("Cannot initialise Transport from mode: %s" % str(mode)) |
| 106 |
|
| 107 |
def _init(self) -> None: |
| 108 |
self._sasl = None |
| 109 |
self._ssl = None |
| 110 |
self._reactor = None |
| 111 |
self._connect_selectable = None |
| 112 |
|
| 113 |
def _check(self, err: int) -> int: |
| 114 |
if err < 0: |
| 115 |
exc = EXCEPTIONS.get(err, TransportException) |
| 116 |
raise exc("[%s]: %s" % (err, pn_error_text(pn_transport_error(self._impl)))) |
| 117 |
else: |
| 118 |
return err |
| 119 |
|
| 120 |
@property |
| 121 |
def tracer(self) -> Optional[Callable[['Transport', str], None]]: |
| 122 |
"""A callback for trace logging. The callback is passed the transport |
| 123 |
and log message. For no tracer callback, value is ``None``. |
| 124 |
""" |
| 125 |
adapter = pn_transport_get_pytracer(self._impl) |
| 126 |
if adapter: |
| 127 |
return adapter.tracer |
| 128 |
else: |
| 129 |
return None |
| 130 |
|
| 131 |
@tracer.setter |
| 132 |
def tracer(self, tracer: Callable[['Transport', str], None]) -> None: |
| 133 |
pn_transport_set_pytracer(self._impl, TraceAdapter(tracer)) |
| 134 |
|
| 135 |
def log(self, message: str) -> None: |
| 136 |
""" |
| 137 |
Log a message using a transport's logging mechanism. |
| 138 |
|
| 139 |
This can be useful in a debugging context as the log message will |
| 140 |
be prefixed with the transport's identifier. |
| 141 |
|
| 142 |
:param message: The message to be logged. |
| 143 |
:type message: ``str`` |
| 144 |
""" |
| 145 |
pn_transport_log(self._impl, message) |
| 146 |
|
| 147 |
def require_auth(self, bool: bool) -> None: |
| 148 |
""" |
| 149 |
Set whether a non-authenticated transport connection is allowed. |
| 150 |
|
| 151 |
There are several ways within the AMQP protocol suite to get |
| 152 |
unauthenticated connections: |
| 153 |
|
| 154 |
- Use no SASL layer (with either no TLS or TLS without client certificates) |
| 155 |
- Use a SASL layer but the ANONYMOUS mechanism |
| 156 |
|
| 157 |
The default if this option is not set is to allow unauthenticated connections. |
| 158 |
|
| 159 |
:param bool: ``True`` when authenticated connections are required. |
| 160 |
""" |
| 161 |
pn_transport_require_auth(self._impl, bool) |
| 162 |
|
| 163 |
@property |
| 164 |
def authenticated(self) -> bool: |
| 165 |
""" |
| 166 |
Indicate whether the transport connection is authenticated. |
| 167 |
|
| 168 |
.. note:: This property may not be stable until the :const:`Event.CONNECTION_REMOTE_OPEN` |
| 169 |
event is received. |
| 170 |
|
| 171 |
:type: ``bool`` |
| 172 |
""" |
| 173 |
return pn_transport_is_authenticated(self._impl) |
| 174 |
|
| 175 |
def require_encryption(self, bool): |
| 176 |
""" |
| 177 |
Set whether a non encrypted transport connection is allowed |
| 178 |
|
| 179 |
There are several ways within the AMQP protocol suite to get encrypted connections: |
| 180 |
|
| 181 |
- Use TLS |
| 182 |
- Use a SASL with a mechanism that supports security layers |
| 183 |
|
| 184 |
The default if this option is not set is to allow unencrypted connections. |
| 185 |
|
| 186 |
:param bool: ``True`` if encryption is required on this transport, ``False`` otherwise. |
| 187 |
:type bool: ``bool`` |
| 188 |
""" |
| 189 |
pn_transport_require_encryption(self._impl, bool) |
| 190 |
|
| 191 |
@property |
| 192 |
def encrypted(self) -> bool: |
| 193 |
""" |
| 194 |
Indicate whether the transport connection is encrypted. |
| 195 |
|
| 196 |
.. note:: This property may not be stable until the :const:`Event.CONNECTION_REMOTE_OPEN` |
| 197 |
event is received. |
| 198 |
""" |
| 199 |
return pn_transport_is_encrypted(self._impl) |
| 200 |
|
| 201 |
@property |
| 202 |
def user(self) -> Optional[str]: |
| 203 |
""" |
| 204 |
The authenticated user. |
| 205 |
|
| 206 |
On the client it will return whatever user was passed in to the |
| 207 |
:attr:`Connection.user` attribute of the bound connection. |
| 208 |
|
| 209 |
The returned value is only reliable after the ``PN_TRANSPORT_AUTHENTICATED`` |
| 210 |
event has been received. |
| 211 |
""" |
| 212 |
return pn_transport_get_user(self._impl) |
| 213 |
|
| 214 |
def bind(self, connection: 'Connection') -> None: |
| 215 |
""" |
| 216 |
Assign a connection to the transport. |
| 217 |
|
| 218 |
:param connection: Connection to which to bind. |
| 219 |
:raise: :exc:`TransportException` if there is any Proton error. |
| 220 |
""" |
| 221 |
self._check(pn_transport_bind(self._impl, connection._impl)) |
| 222 |
|
| 223 |
def bind_nothrow(self, connection: 'Connection') -> None: |
| 224 |
""" |
| 225 |
Assign a connection to the transport. Any failure is |
| 226 |
ignored rather than thrown. |
| 227 |
|
| 228 |
:param connection: Connection to which to bind. |
| 229 |
""" |
| 230 |
pn_transport_bind(self._impl, connection._impl) |
| 231 |
|
| 232 |
def unbind(self) -> None: |
| 233 |
""" |
| 234 |
Unbinds a transport from its AMQP connection. |
| 235 |
|
| 236 |
:raise: :exc:`TransportException` if there is any Proton error. |
| 237 |
""" |
| 238 |
self._check(pn_transport_unbind(self._impl)) |
| 239 |
|
| 240 |
def trace(self, n: int) -> None: |
| 241 |
""" |
| 242 |
Update a transports trace flags. |
| 243 |
|
| 244 |
The trace flags for a transport control what sort of information is |
| 245 |
logged. The value may be :const:`TRACE_OFF` or any combination of |
| 246 |
:const:`TRACE_DRV`, :const:`TRACE_FRM`, :const:`TRACE_RAW` using |
| 247 |
a bitwise or operation. |
| 248 |
|
| 249 |
:param n: Trace flags |
| 250 |
""" |
| 251 |
pn_transport_trace(self._impl, n) |
| 252 |
|
| 253 |
def tick(self, now: float) -> float: |
| 254 |
""" |
| 255 |
Process any pending transport timer events (like heartbeat generation). |
| 256 |
|
| 257 |
This method should be called after all pending input has been |
| 258 |
processed by the transport and before generating output. It returns |
| 259 |
the deadline for the next pending timer event, if any are present. |
| 260 |
|
| 261 |
.. note:: This function does nothing until the first data is read |
| 262 |
from or written to the transport. |
| 263 |
|
| 264 |
:param now: seconds since epoch. |
| 265 |
:return: If non-zero, then the monotonic expiration time of the next |
| 266 |
pending timer event for the transport. The caller must invoke |
| 267 |
:meth:`tick` again at least once at or before this deadline |
| 268 |
occurs. If ``0.0``, then there are no pending events. |
| 269 |
""" |
| 270 |
return millis2secs(pn_transport_tick(self._impl, secs2millis(now))) |
| 271 |
|
| 272 |
def capacity(self) -> int: |
| 273 |
""" |
| 274 |
Get the amount of free space for input following the transport's |
| 275 |
tail pointer. |
| 276 |
|
| 277 |
:return: Available space for input in bytes. |
| 278 |
:raise: :exc:`TransportException` if there is any Proton error. |
| 279 |
""" |
| 280 |
c = pn_transport_capacity(self._impl) |
| 281 |
if c >= PN_EOS: |
| 282 |
return c |
| 283 |
else: |
| 284 |
return self._check(c) |
| 285 |
|
| 286 |
def push(self, binary: bytes) -> None: |
| 287 |
""" |
| 288 |
Pushes the supplied bytes into the tail of the transport. |
| 289 |
Only some of the bytes will be copied if there is insufficient |
| 290 |
capacity available. Use :meth:`capacity` to determine how much |
| 291 |
capacity the transport has. |
| 292 |
|
| 293 |
:param binary: Data to be pushed onto the transport tail. |
| 294 |
:raise: - :exc:`TransportException` if there is any Proton error. |
| 295 |
- ``OverflowError`` if the size of the data exceeds the |
| 296 |
transport capacity. |
| 297 |
""" |
| 298 |
n = self._check(pn_transport_push(self._impl, binary)) |
| 299 |
if n != len(binary): |
| 300 |
raise OverflowError("unable to process all bytes: %s, %s" % (n, len(binary))) |
| 301 |
|
| 302 |
def close_tail(self) -> None: |
| 303 |
""" |
| 304 |
Indicate that the input has reached End Of Stream (EOS). |
| 305 |
|
| 306 |
This tells the transport that no more input will be forthcoming. |
| 307 |
|
| 308 |
:raise: :exc:`TransportException` if there is any Proton error. |
| 309 |
""" |
| 310 |
self._check(pn_transport_close_tail(self._impl)) |
| 311 |
|
| 312 |
def pending(self) -> int: |
| 313 |
""" |
| 314 |
Get the number of pending output bytes following the transport's |
| 315 |
head pointer. |
| 316 |
|
| 317 |
:return: The number of pending output bytes. |
| 318 |
:raise: :exc:`TransportException` if there is any Proton error. |
| 319 |
""" |
| 320 |
p = pn_transport_pending(self._impl) |
| 321 |
if p >= PN_EOS: |
| 322 |
return p |
| 323 |
else: |
| 324 |
return self._check(p) |
| 325 |
|
| 326 |
def peek(self, size: int) -> Optional[bytes]: |
| 327 |
""" |
| 328 |
Returns ``size`` bytes from the head of the transport. |
| 329 |
|
| 330 |
It is an error to call this with a value of ``size`` that |
| 331 |
is greater than the value reported by :meth:`pending`. |
| 332 |
|
| 333 |
:param size: Number of bytes to return. |
| 334 |
:return: ``size`` bytes from the head of the transport, or ``None`` |
| 335 |
if none are available. |
| 336 |
:raise: :exc:`TransportException` if there is any Proton error. |
| 337 |
""" |
| 338 |
cd, out = pn_transport_peek(self._impl, size) |
| 339 |
if cd == PN_EOS: |
| 340 |
return None |
| 341 |
else: |
| 342 |
self._check(cd) |
| 343 |
return out |
| 344 |
|
| 345 |
def pop(self, size: int) -> None: |
| 346 |
""" |
| 347 |
Removes ``size`` bytes of output from the pending output queue |
| 348 |
following the transport's head pointer. |
| 349 |
|
| 350 |
Calls to this function may alter the transport's head pointer as |
| 351 |
well as the number of pending bytes reported by |
| 352 |
:meth:`pending`. |
| 353 |
|
| 354 |
:param size: Number of bytes to remove. |
| 355 |
""" |
| 356 |
pn_transport_pop(self._impl, size) |
| 357 |
|
| 358 |
def close_head(self) -> None: |
| 359 |
""" |
| 360 |
Indicate that the output has closed. |
| 361 |
|
| 362 |
This tells the transport that no more output will be popped. |
| 363 |
|
| 364 |
:raise: :exc:`TransportException` if there is any Proton error. |
| 365 |
""" |
| 366 |
self._check(pn_transport_close_head(self._impl)) |
| 367 |
|
| 368 |
@property |
| 369 |
def closed(self) -> bool: |
| 370 |
""" |
| 371 |
``True`` iff both the transport head and transport tail are closed |
| 372 |
using :meth:`close_head` and :meth:`close_tail` respectively. |
| 373 |
""" |
| 374 |
return pn_transport_closed(self._impl) |
| 375 |
|
| 376 |
# AMQP 1.0 max-frame-size |
| 377 |
@property |
| 378 |
def max_frame_size(self) -> int: |
| 379 |
"""The maximum size for transport frames (in bytes).""" |
| 380 |
return pn_transport_get_max_frame(self._impl) |
| 381 |
|
| 382 |
@max_frame_size.setter |
| 383 |
def max_frame_size(self, value: int) -> None: |
| 384 |
pn_transport_set_max_frame(self._impl, value) |
| 385 |
|
| 386 |
@property |
| 387 |
def remote_max_frame_size(self) -> int: |
| 388 |
""" |
| 389 |
The maximum frame size of a transport's remote peer (in bytes). |
| 390 |
""" |
| 391 |
return pn_transport_get_remote_max_frame(self._impl) |
| 392 |
|
| 393 |
@property |
| 394 |
def channel_max(self) -> int: |
| 395 |
"""The maximum channel number that may be used on this transport. |
| 396 |
|
| 397 |
.. note:: This is the maximum channel number allowed, giving a |
| 398 |
valid channel number range of ``[0 .. channel_max]``. Therefore the |
| 399 |
maximum number of simultaneously active channels will be |
| 400 |
channel_max plus 1. |
| 401 |
|
| 402 |
You can set this more than once to raise and lower |
| 403 |
the limit your application imposes on max channels for this |
| 404 |
transport. However, smaller limits may be imposed by Proton, |
| 405 |
or by the remote peer. |
| 406 |
|
| 407 |
After the ``OPEN`` frame has been sent to the remote peer, |
| 408 |
further calls to this function will have no effect. |
| 409 |
|
| 410 |
:raise: :exc:`SessionException` if the ``OPEN`` frame has already |
| 411 |
been sent. |
| 412 |
""" |
| 413 |
return pn_transport_get_channel_max(self._impl) |
| 414 |
|
| 415 |
@channel_max.setter |
| 416 |
def channel_max(self, value: int) -> None: |
| 417 |
if pn_transport_set_channel_max(self._impl, value): |
| 418 |
raise SessionException("Too late to change channel max.") |
| 419 |
|
| 420 |
@property |
| 421 |
def remote_channel_max(self) -> int: |
| 422 |
""" |
| 423 |
The maximum allowed channel number of a transport's remote peer. |
| 424 |
""" |
| 425 |
return pn_transport_remote_channel_max(self._impl) |
| 426 |
|
| 427 |
# AMQP 1.0 idle-time-out |
| 428 |
@property |
| 429 |
def idle_timeout(self) -> float: |
| 430 |
"""The idle timeout of the connection in seconds. A zero idle |
| 431 |
timeout means heartbeats are disabled. |
| 432 |
""" |
| 433 |
return millis2secs(pn_transport_get_idle_timeout(self._impl)) |
| 434 |
|
| 435 |
@idle_timeout.setter |
| 436 |
def idle_timeout(self, sec: Union[float, int]) -> None: |
| 437 |
pn_transport_set_idle_timeout(self._impl, secs2millis(sec)) |
| 438 |
|
| 439 |
@property |
| 440 |
def remote_idle_timeout(self) -> float: |
| 441 |
""" |
| 442 |
Get the idle timeout for a transport's remote peer in |
| 443 |
seconds. A zero idle timeout means heartbeats are disabled. |
| 444 |
""" |
| 445 |
return millis2secs(pn_transport_get_remote_idle_timeout(self._impl)) |
| 446 |
|
| 447 |
@property |
| 448 |
def frames_output(self) -> int: |
| 449 |
""" |
| 450 |
Get the number of frames output by a transport. |
| 451 |
""" |
| 452 |
return pn_transport_get_frames_output(self._impl) |
| 453 |
|
| 454 |
@property |
| 455 |
def frames_input(self) -> int: |
| 456 |
""" |
| 457 |
Get the number of frames input by a transport. |
| 458 |
""" |
| 459 |
return pn_transport_get_frames_input(self._impl) |
| 460 |
|
| 461 |
def sasl(self) -> 'SASL': |
| 462 |
""" |
| 463 |
Get the :class:`SASL` object associated with this transport. |
| 464 |
|
| 465 |
:return: SASL object associated with this transport. |
| 466 |
""" |
| 467 |
return SASL(self) |
| 468 |
|
| 469 |
def ssl(self, domain: Optional['SSLDomain'] = None, session_details: Optional['SSLSessionDetails'] = None) -> 'SSL': |
| 470 |
""" |
| 471 |
Get the :class:`SSL` session associated with this transport. If |
| 472 |
not set, then a new session will be created using ``domain`` and |
| 473 |
``session_details``. |
| 474 |
|
| 475 |
:param domain: An SSL domain configuration object |
| 476 |
:param session_details: A unique identifier for the SSL session. |
| 477 |
:return: SSL session associated with this transport. |
| 478 |
""" |
| 479 |
# SSL factory (singleton for this transport) |
| 480 |
if not self._ssl: |
| 481 |
self._ssl = SSL(self, domain, session_details) |
| 482 |
return self._ssl |
| 483 |
|
| 484 |
@property |
| 485 |
def condition(self) -> Optional['Condition']: |
| 486 |
"""Get additional information about the condition of the transport. |
| 487 |
|
| 488 |
When a :const:`Event.TRANSPORT_ERROR` event occurs, this operation |
| 489 |
can be used to access the details of the error condition. |
| 490 |
|
| 491 |
See :class:`Condition` for more information. |
| 492 |
""" |
| 493 |
return cond2obj(pn_transport_condition(self._impl)) |
| 494 |
|
| 495 |
@condition.setter |
| 496 |
def condition(self, cond: 'Condition') -> None: |
| 497 |
pn_cond = pn_transport_condition(self._impl) |
| 498 |
obj2cond(cond, pn_cond) |
| 499 |
|
| 500 |
@property |
| 501 |
def connection(self) -> 'Connection': |
| 502 |
"""The connection bound to this transport.""" |
| 503 |
from . import _endpoints |
| 504 |
return _endpoints.Connection.wrap(pn_transport_connection(self._impl)) |
| 505 |
|
| 506 |
|
| 507 |
class SASLException(TransportException): |
| 508 |
pass |
| 509 |
|
| 510 |
|
| 511 |
class SASL(Wrapper): |
| 512 |
""" |
| 513 |
The SASL layer is responsible for establishing an authenticated |
| 514 |
and/or encrypted tunnel over which AMQP frames are passed between |
| 515 |
peers. The peer acting as the SASL Client must provide |
| 516 |
authentication credentials. The peer acting as the SASL Server must |
| 517 |
provide authentication against the received credentials. |
| 518 |
""" |
| 519 |
OK = PN_SASL_OK |
| 520 |
AUTH = PN_SASL_AUTH |
| 521 |
SYS = PN_SASL_SYS |
| 522 |
PERM = PN_SASL_PERM |
| 523 |
TEMP = PN_SASL_TEMP |
| 524 |
|
| 525 |
@staticmethod |
| 526 |
def extended() -> bool: |
| 527 |
""" |
| 528 |
Check for support of extended SASL negotiation. |
| 529 |
|
| 530 |
All implementations of Proton support ``ANONYMOUS`` and ``EXTERNAL`` on both |
| 531 |
client and server sides and ``PLAIN`` on the client side. |
| 532 |
|
| 533 |
Extended SASL implementations use an external library (Cyrus SASL) |
| 534 |
to support other mechanisms beyond these basic ones. |
| 535 |
|
| 536 |
:rtype: ``True`` if we support extended SASL negotiation, ``False`` if |
| 537 |
we only support basic negotiation. |
| 538 |
""" |
| 539 |
return pn_sasl_extended() |
| 540 |
|
| 541 |
def __init__(self, transport: Transport) -> None: |
| 542 |
Wrapper.__init__(self, transport._impl, pn_transport_attachments) |
| 543 |
self._sasl = pn_sasl(transport._impl) |
| 544 |
|
| 545 |
def _check(self, err): |
| 546 |
if err < 0: |
| 547 |
exc = EXCEPTIONS.get(err, SASLException) |
| 548 |
raise exc("[%s]" % (err)) |
| 549 |
else: |
| 550 |
return err |
| 551 |
|
| 552 |
@property |
| 553 |
def user(self) -> Optional[str]: |
| 554 |
""" |
| 555 |
Retrieve the authenticated user. This is usually used at the the |
| 556 |
server end to find the name of the authenticated user. |
| 557 |
|
| 558 |
If :meth:`outcome` returns a value other than :const:`OK`, then |
| 559 |
there will be no user to return. The returned value is only reliable |
| 560 |
after the ``PN_TRANSPORT_AUTHENTICATED`` event has been received. |
| 561 |
|
| 562 |
:rtype: * If the SASL layer was not negotiated then ``None`` is returned. |
| 563 |
* If the ``ANONYMOUS`` mechanism is used then the user will be |
| 564 |
``"anonymous"``. |
| 565 |
* Otherwise a string containing the user is |
| 566 |
returned. |
| 567 |
""" |
| 568 |
return pn_sasl_get_user(self._sasl) |
| 569 |
|
| 570 |
@property |
| 571 |
def authorization(self) -> Optional[str]: |
| 572 |
""" |
| 573 |
Retrieve the requested authorization user. This is usually used at the the |
| 574 |
server end to find the name of any requested authorization user. |
| 575 |
|
| 576 |
If the peer has not requested an authorization user or the SASL mechanism has |
| 577 |
no capability to transport an authorization id this will be the same as the |
| 578 |
authenticated user. |
| 579 |
|
| 580 |
Note that it is the role of the server to ensure that the authenticated user is |
| 581 |
actually allowed to act as the requested authorization user. |
| 582 |
|
| 583 |
If :meth:`outcome` returns a value other than :const:`OK`, then |
| 584 |
there will be no user to return. The returned value is only reliable |
| 585 |
after the ``PN_TRANSPORT_AUTHENTICATED`` event has been received. |
| 586 |
|
| 587 |
:rtype: * If the SASL layer was not negotiated then ``None`` is returned. |
| 588 |
* If the ``ANONYMOUS`` mechanism is used then the user will be |
| 589 |
``"anonymous"``. |
| 590 |
* Otherwise a string containing the user is |
| 591 |
returned. |
| 592 |
""" |
| 593 |
return pn_sasl_get_authorization(self._sasl) |
| 594 |
|
| 595 |
@property |
| 596 |
def mech(self) -> str: |
| 597 |
""" |
| 598 |
Return the selected SASL mechanism. |
| 599 |
|
| 600 |
The returned value is only reliable after the ``PN_TRANSPORT_AUTHENTICATED`` |
| 601 |
event has been received. |
| 602 |
|
| 603 |
:rtype: The authentication mechanism selected by the SASL layer. |
| 604 |
""" |
| 605 |
return pn_sasl_get_mech(self._sasl) |
| 606 |
|
| 607 |
@property |
| 608 |
def outcome(self) -> Optional[int]: |
| 609 |
""" |
| 610 |
Retrieve the outcome of SASL negotiation. |
| 611 |
|
| 612 |
:rtype: * ``None`` if no negotiation has taken place. |
| 613 |
* Otherwise the outcome of the negotiation. |
| 614 |
""" |
| 615 |
outcome = pn_sasl_outcome(self._sasl) |
| 616 |
if outcome == PN_SASL_NONE: |
| 617 |
return None |
| 618 |
else: |
| 619 |
return outcome |
| 620 |
|
| 621 |
def allowed_mechs(self, mechs: Union[str, List[str]]) -> None: |
| 622 |
""" |
| 623 |
SASL mechanisms that are to be considered for authentication. |
| 624 |
|
| 625 |
This can be used on either the client or the server to restrict |
| 626 |
the SASL mechanisms that may be used to the mechanisms on the list. |
| 627 |
|
| 628 |
**NOTE:** By default the ``GSSAPI`` and ``GSS-SPNEGO`` mechanisms |
| 629 |
are not enabled for clients. This is because these mechanisms have |
| 630 |
the problematic behaviour of 'capturing' the client whenever they |
| 631 |
are installed so that they will be used by the client if offered by |
| 632 |
the server even if the client can't successfully authenticate this |
| 633 |
way. This can lead to some very hard to debug failures. |
| 634 |
|
| 635 |
**NOTE:** The ``GSSAPI`` or ``GSS-SPNEGO`` mechanisms need to be |
| 636 |
explicitly enabled if they are required (together with any other |
| 637 |
required mechanisms). |
| 638 |
|
| 639 |
:param mechs: A list of mechanisms that are allowed for authentication, |
| 640 |
either a string containing a space-separated list of mechs |
| 641 |
``"mech1 mech2 ..."``, or a Python list of strings |
| 642 |
``["mech1", "mech2", ...]``. |
| 643 |
""" |
| 644 |
if isinstance(mechs, list): |
| 645 |
mechs = " ".join(mechs) |
| 646 |
pn_sasl_allowed_mechs(self._sasl, unicode2utf8(mechs)) |
| 647 |
|
| 648 |
@property |
| 649 |
def allow_insecure_mechs(self) -> bool: |
| 650 |
"""Allow unencrypted cleartext passwords (PLAIN mech)""" |
| 651 |
return pn_sasl_get_allow_insecure_mechs(self._sasl) |
| 652 |
|
| 653 |
@allow_insecure_mechs.setter |
| 654 |
def allow_insecure_mechs(self, insecure: bool) -> None: |
| 655 |
pn_sasl_set_allow_insecure_mechs(self._sasl, insecure) |
| 656 |
|
| 657 |
def done(self, outcome): |
| 658 |
""" |
| 659 |
Set the outcome of SASL negotiation. Used by the server to set the |
| 660 |
result of the negotiation process. |
| 661 |
""" |
| 662 |
pn_sasl_done(self._sasl, outcome) |
| 663 |
|
| 664 |
def config_name(self, name: str): |
| 665 |
""" |
| 666 |
Set the SASL configuration name. This is used to construct the SASL |
| 667 |
configuration filename. In the current implementation ``".conf"`` is |
| 668 |
added to the name and the file is looked for in the configuration |
| 669 |
directory. |
| 670 |
|
| 671 |
If not set it will default to ``"proton-server"`` for a sasl server |
| 672 |
and ``"proton-client"`` for a client. |
| 673 |
|
| 674 |
:param name: The configuration name. |
| 675 |
""" |
| 676 |
pn_sasl_config_name(self._sasl, name) |
| 677 |
|
| 678 |
def config_path(self, path: str): |
| 679 |
""" |
| 680 |
Set the SASL configuration path. This is used to tell SASL where |
| 681 |
to look for the configuration file. In the current implementation |
| 682 |
it can be a colon separated list of directories. |
| 683 |
|
| 684 |
The environment variable ``PN_SASL_CONFIG_PATH`` can also be used |
| 685 |
to set this path, but if both methods are used then this |
| 686 |
:meth:`config_path` will take precedence. |
| 687 |
|
| 688 |
If not set, the underlying implementation default will be used. |
| 689 |
|
| 690 |
:param path: The configuration path, may contain colon-separated list |
| 691 |
if more than one path is specified. |
| 692 |
""" |
| 693 |
pn_sasl_config_path(self._sasl, path) |
| 694 |
|
| 695 |
|
| 696 |
class SSLDomain(object): |
| 697 |
""" |
| 698 |
An SSL configuration domain, used to hold the SSL configuration |
| 699 |
for one or more SSL sessions. |
| 700 |
""" |
| 701 |
|
| 702 |
MODE_CLIENT = PN_SSL_MODE_CLIENT |
| 703 |
"""Local connection endpoint is an SSL client.""" |
| 704 |
|
| 705 |
MODE_SERVER = PN_SSL_MODE_SERVER |
| 706 |
"""Local connection endpoint is an SSL server.""" |
| 707 |
|
| 708 |
VERIFY_PEER = PN_SSL_VERIFY_PEER |
| 709 |
"""Require peer to provide a valid identifying certificate.""" |
| 710 |
|
| 711 |
VERIFY_PEER_NAME = PN_SSL_VERIFY_PEER_NAME |
| 712 |
"""Require valid certificate and matching name.""" |
| 713 |
|
| 714 |
ANONYMOUS_PEER = PN_SSL_ANONYMOUS_PEER |
| 715 |
"""Do not require a certificate nor cipher authorization.""" |
| 716 |
|
| 717 |
def __init__(self, mode: int) -> None: |
| 718 |
self._domain = pn_ssl_domain(mode) |
| 719 |
if self._domain is None: |
| 720 |
raise SSLUnavailable() |
| 721 |
|
| 722 |
def _check(self, err: int) -> int: |
| 723 |
if err < 0: |
| 724 |
exc = EXCEPTIONS.get(err, SSLException) |
| 725 |
raise exc("SSL failure.") |
| 726 |
else: |
| 727 |
return err |
| 728 |
|
| 729 |
def set_credentials(self, cert_file: str, key_file: str, password: Optional[str]) -> int: |
| 730 |
""" |
| 731 |
Set the certificate that identifies the local node to the remote. |
| 732 |
|
| 733 |
This certificate establishes the identity for the local node for all :class:`SSL` sessions |
| 734 |
created from this domain. It will be sent to the remote if the remote needs to verify |
| 735 |
the identity of this node. This may be used for both SSL servers and SSL clients (if |
| 736 |
client authentication is required by the server). |
| 737 |
|
| 738 |
.. note:: This setting effects only those :class:`SSL` objects created after this call |
| 739 |
returns. :class:`SSL` objects created before invoking this method will use the domain's |
| 740 |
previous setting. |
| 741 |
|
| 742 |
:param cert_file: Specifier for the file/database containing the identifying |
| 743 |
certificate. For Openssl users, this is a PEM file. For Windows SChannel |
| 744 |
users, this is the PKCS#12 file or system store. |
| 745 |
:param key_file: An optional key to access the identifying certificate. For |
| 746 |
Openssl users, this is an optional PEM file containing the private key |
| 747 |
used to sign the certificate. For Windows SChannel users, this is the |
| 748 |
friendly name of the self-identifying certificate if there are multiple |
| 749 |
certificates in the store. |
| 750 |
:param password: The password used to sign the key, else ``None`` if key is not |
| 751 |
protected. |
| 752 |
:return: 0 on success |
| 753 |
:raise: :exc:`SSLException` if there is any Proton error |
| 754 |
""" |
| 755 |
return self._check(pn_ssl_domain_set_credentials(self._domain, |
| 756 |
cert_file, key_file, |
| 757 |
password)) |
| 758 |
|
| 759 |
def set_trusted_ca_db(self, certificate_db: str) -> int: |
| 760 |
""" |
| 761 |
Configure the set of trusted CA certificates used by this domain to verify peers. |
| 762 |
|
| 763 |
If the local SSL client/server needs to verify the identity of the remote, it must |
| 764 |
validate the signature of the remote's certificate. This function sets the database of |
| 765 |
trusted CAs that will be used to verify the signature of the remote's certificate. |
| 766 |
|
| 767 |
.. note:: This setting effects only those :class:`SSL` objects created after this call |
| 768 |
returns. :class:`SSL` objects created before invoking this method will use the domain's |
| 769 |
previous setting. |
| 770 |
|
| 771 |
.. note:: By default the list of trusted CA certificates will be set to the system default. |
| 772 |
What this is is depends on the OS and the SSL implementation used: For OpenSSL the default |
| 773 |
will depend on how the OS is set up. When using the Windows SChannel implementation the default |
| 774 |
will be the users default trusted certificate store. |
| 775 |
|
| 776 |
:param certificate_db: Database of trusted CAs, used to authenticate the peer. |
| 777 |
:return: 0 on success |
| 778 |
:raise: :exc:`SSLException` if there is any Proton error |
| 779 |
""" |
| 780 |
return self._check(pn_ssl_domain_set_trusted_ca_db(self._domain, |
| 781 |
certificate_db)) |
| 782 |
|
| 783 |
def set_peer_authentication(self, verify_mode: int, trusted_CAs: Optional[str] = None) -> int: |
| 784 |
""" |
| 785 |
This method controls how the peer's certificate is validated, if at all. By default, |
| 786 |
servers do not attempt to verify their peers (PN_SSL_ANONYMOUS_PEER) but |
| 787 |
clients attempt to verify both the certificate and peer name (PN_SSL_VERIFY_PEER_NAME). |
| 788 |
Once certificates and trusted CAs are configured, peer verification can be enabled. |
| 789 |
|
| 790 |
.. note:: In order to verify a peer, a trusted CA must be configured. See |
| 791 |
:meth:`set_trusted_ca_db`. |
| 792 |
|
| 793 |
.. note:: Servers must provide their own certificate when verifying a peer. See |
| 794 |
:meth:`set_credentials`. |
| 795 |
|
| 796 |
.. note:: This setting effects only those :class:`SSL` objects created after this call |
| 797 |
returns. :class:`SSL` objects created before invoking this method will use the domain's |
| 798 |
previous setting. |
| 799 |
|
| 800 |
:param verify_mode: The level of validation to apply to the peer, one of :const:`VERIFY_PEER`, |
| 801 |
:const:`VERIFY_PEER_NAME`, :const:`ANONYMOUS_PEER`, |
| 802 |
:param trusted_CAs: Path to a database of trusted CAs that the server will advertise. |
| 803 |
:return: 0 on success |
| 804 |
:raise: :exc:`SSLException` if there is any Proton error |
| 805 |
""" |
| 806 |
return self._check(pn_ssl_domain_set_peer_authentication(self._domain, |
| 807 |
verify_mode, |
| 808 |
trusted_CAs)) |
| 809 |
|
| 810 |
def allow_unsecured_client(self) -> int: |
| 811 |
""" |
| 812 |
Permit a server to accept connection requests from non-SSL clients. |
| 813 |
|
| 814 |
This configures the server to "sniff" the incoming client data stream, |
| 815 |
and dynamically determine whether SSL/TLS is being used. This option |
| 816 |
is disabled by default: only clients using SSL/TLS are accepted. |
| 817 |
|
| 818 |
:raise: :exc:`SSLException` if there is any Proton error |
| 819 |
""" |
| 820 |
return self._check(pn_ssl_domain_allow_unsecured_client(self._domain)) |
| 821 |
|
| 822 |
def __del__(self) -> None: |
| 823 |
pn_ssl_domain_free(self._domain) |
| 824 |
|
| 825 |
|
| 826 |
class SSL(object): |
| 827 |
""" |
| 828 |
An SSL session associated with a transport. A transport must have |
| 829 |
an SSL object in order to "speak" SSL over its connection. |
| 830 |
""" |
| 831 |
|
| 832 |
@staticmethod |
| 833 |
def present() -> bool: |
| 834 |
""" |
| 835 |
Tests for an SSL implementation being present. |
| 836 |
|
| 837 |
:return: ``True`` if we support SSL, ``False`` if not. |
| 838 |
""" |
| 839 |
return pn_ssl_present() |
| 840 |
|
| 841 |
def _check(self, err: int) -> int: |
| 842 |
if err < 0: |
| 843 |
exc = EXCEPTIONS.get(err, SSLException) |
| 844 |
raise exc("SSL failure.") |
| 845 |
else: |
| 846 |
return err |
| 847 |
|
| 848 |
def __new__( |
| 849 |
cls: Type['SSL'], |
| 850 |
transport: Transport, |
| 851 |
domain: SSLDomain, |
| 852 |
session_details: Optional['SSLSessionDetails'] = None |
| 853 |
) -> 'SSL': |
| 854 |
"""Enforce a singleton SSL object per Transport""" |
| 855 |
if transport._ssl: |
| 856 |
# unfortunately, we've combined the allocation and the configuration in a |
| 857 |
# single step. So catch any attempt by the application to provide what |
| 858 |
# may be a different configuration than the original (hack) |
| 859 |
ssl = transport._ssl |
| 860 |
if (domain and (ssl._domain is not domain) or |
| 861 |
session_details and (ssl._session_details is not session_details)): |
| 862 |
raise SSLException("Cannot re-configure existing SSL object!") |
| 863 |
else: |
| 864 |
obj = super(SSL, cls).__new__(cls) |
| 865 |
obj._domain = domain |
| 866 |
obj._session_details = session_details |
| 867 |
session_id = None |
| 868 |
if session_details: |
| 869 |
session_id = session_details.get_session_id() |
| 870 |
obj._ssl = pn_ssl(transport._impl) |
| 871 |
if obj._ssl is None: |
| 872 |
raise SSLUnavailable() |
| 873 |
if domain: |
| 874 |
pn_ssl_init(obj._ssl, domain._domain, session_id) |
| 875 |
transport._ssl = obj |
| 876 |
return transport._ssl |
| 877 |
|
| 878 |
def cipher_name(self) -> Optional[str]: |
| 879 |
""" |
| 880 |
Get the name of the Cipher that is currently in use. |
| 881 |
|
| 882 |
Gets a text description of the cipher that is currently active, or |
| 883 |
returns ``None`` if SSL is not active (no cipher). |
| 884 |
|
| 885 |
.. note:: The cipher in use may change over time due to renegotiation |
| 886 |
or other changes to the SSL state. |
| 887 |
|
| 888 |
:return: The cypher name, or ``None`` if no cipher in use. |
| 889 |
""" |
| 890 |
rc, name = pn_ssl_get_cipher_name(self._ssl, 128) |
| 891 |
if rc: |
| 892 |
return name |
| 893 |
return None |
| 894 |
|
| 895 |
def protocol_name(self) -> Optional[str]: |
| 896 |
""" |
| 897 |
Get the name of the SSL protocol that is currently in use. |
| 898 |
|
| 899 |
Gets a text description of the SSL protocol that is currently active, |
| 900 |
or returns ``None`` if SSL is not active. |
| 901 |
|
| 902 |
.. note:: The protocol may change over time due to renegotiation. |
| 903 |
|
| 904 |
:return: The protocol name if SSL is active, or ``None`` if SSL connection |
| 905 |
is not ready or active. |
| 906 |
""" |
| 907 |
rc, name = pn_ssl_get_protocol_name(self._ssl, 128) |
| 908 |
if rc: |
| 909 |
return name |
| 910 |
return None |
| 911 |
|
| 912 |
SHA1 = PN_SSL_SHA1 |
| 913 |
"""Produces hash that is 20 bytes long using SHA-1""" |
| 914 |
|
| 915 |
SHA256 = PN_SSL_SHA256 |
| 916 |
"""Produces hash that is 32 bytes long using SHA-256""" |
| 917 |
|
| 918 |
SHA512 = PN_SSL_SHA512 |
| 919 |
"""Produces hash that is 64 bytes long using SHA-512""" |
| 920 |
|
| 921 |
MD5 = PN_SSL_MD5 |
| 922 |
"""Produces hash that is 16 bytes long using MD5""" |
| 923 |
|
| 924 |
CERT_COUNTRY_NAME = PN_SSL_CERT_SUBJECT_COUNTRY_NAME |
| 925 |
"""Certificate country name 2-char ISO code""" |
| 926 |
|
| 927 |
CERT_STATE_OR_PROVINCE = PN_SSL_CERT_SUBJECT_STATE_OR_PROVINCE |
| 928 |
"""Certificate state or province, not abbreviated""" |
| 929 |
|
| 930 |
CERT_CITY_OR_LOCALITY = PN_SSL_CERT_SUBJECT_CITY_OR_LOCALITY |
| 931 |
"""Certificate city or place name, not abbreviated""" |
| 932 |
|
| 933 |
CERT_ORGANIZATION_NAME = PN_SSL_CERT_SUBJECT_ORGANIZATION_NAME |
| 934 |
"""Certificate organization name""" |
| 935 |
|
| 936 |
CERT_ORGANIZATION_UNIT = PN_SSL_CERT_SUBJECT_ORGANIZATION_UNIT |
| 937 |
"""Certificate organization unit or division within organization""" |
| 938 |
|
| 939 |
CERT_COMMON_NAME = PN_SSL_CERT_SUBJECT_COMMON_NAME |
| 940 |
"""Certificate common name or URL""" |
| 941 |
|
| 942 |
def get_cert_subject_subfield(self, subfield_name: int) -> Optional[str]: |
| 943 |
""" |
| 944 |
Returns a string that contains the value of the sub field of |
| 945 |
the subject field in the ssl certificate. The subject field |
| 946 |
usually contains the following values: |
| 947 |
|
| 948 |
* :const:`CERT_COUNTRY_NAME` |
| 949 |
* :const:`CERT_STATE_OR_PROVINCE` |
| 950 |
* :const:`CERT_CITY_OR_LOCALITY` |
| 951 |
* :const:`CERT_ORGANIZATION_NAME` |
| 952 |
* :const:`CERT_ORGANIZATION_UNIT` |
| 953 |
* :const:`CERT_COMMON_NAME` |
| 954 |
|
| 955 |
:param subfield_name: The enumeration representing the required |
| 956 |
sub field listed above |
| 957 |
:return: A string which contains the requested sub field value which |
| 958 |
is valid until the ssl object is destroyed. |
| 959 |
""" |
| 960 |
subfield_value = pn_ssl_get_remote_subject_subfield(self._ssl, subfield_name) |
| 961 |
return subfield_value |
| 962 |
|
| 963 |
def get_cert_subject(self) -> str: |
| 964 |
""" |
| 965 |
Get the subject from the peer's certificate. |
| 966 |
|
| 967 |
:return: A string containing the full subject. |
| 968 |
""" |
| 969 |
subject = pn_ssl_get_remote_subject(self._ssl) |
| 970 |
return subject |
| 971 |
|
| 972 |
def _get_cert_subject_unknown_subfield(self) -> None: |
| 973 |
# Pass in an unhandled enum |
| 974 |
return self.get_cert_subject_subfield(10) |
| 975 |
|
| 976 |
# Convenience functions for obtaining the subfields of the subject field. |
| 977 |
def get_cert_common_name(self) -> str: |
| 978 |
""" |
| 979 |
A convenience method to get a string that contains the :const:`CERT_COMMON_NAME` |
| 980 |
sub field of the subject field in the ssl certificate. |
| 981 |
|
| 982 |
:return: A string containing the :const:`CERT_COMMON_NAME` sub field. |
| 983 |
""" |
| 984 |
return self.get_cert_subject_subfield(SSL.CERT_COMMON_NAME) |
| 985 |
|
| 986 |
def get_cert_organization(self) -> str: |
| 987 |
""" |
| 988 |
A convenience method to get a string that contains the :const:`CERT_ORGANIZATION_NAME` |
| 989 |
sub field of the subject field in the ssl certificate. |
| 990 |
|
| 991 |
:return: A string containing the :const:`CERT_ORGANIZATION_NAME` sub field. |
| 992 |
""" |
| 993 |
return self.get_cert_subject_subfield(SSL.CERT_ORGANIZATION_NAME) |
| 994 |
|
| 995 |
def get_cert_organization_unit(self) -> str: |
| 996 |
""" |
| 997 |
A convenience method to get a string that contains the :const:`CERT_ORGANIZATION_UNIT` |
| 998 |
sub field of the subject field in the ssl certificate. |
| 999 |
|
| 1000 |
:return: A string containing the :const:`CERT_ORGANIZATION_UNIT` sub field. |
| 1001 |
""" |
| 1002 |
return self.get_cert_subject_subfield(SSL.CERT_ORGANIZATION_UNIT) |
| 1003 |
|
| 1004 |
def get_cert_locality_or_city(self) -> str: |
| 1005 |
""" |
| 1006 |
A convenience method to get a string that contains the :const:`CERT_CITY_OR_LOCALITY` |
| 1007 |
sub field of the subject field in the ssl certificate. |
| 1008 |
|
| 1009 |
:return: A string containing the :const:`CERT_CITY_OR_LOCALITY` sub field. |
| 1010 |
""" |
| 1011 |
return self.get_cert_subject_subfield(SSL.CERT_CITY_OR_LOCALITY) |
| 1012 |
|
| 1013 |
def get_cert_country(self) -> str: |
| 1014 |
""" |
| 1015 |
A convenience method to get a string that contains the :const:`CERT_COUNTRY_NAME` |
| 1016 |
sub field of the subject field in the ssl certificate. |
| 1017 |
|
| 1018 |
:return: A string containing the :const:`CERT_COUNTRY_NAME` sub field. |
| 1019 |
""" |
| 1020 |
return self.get_cert_subject_subfield(SSL.CERT_COUNTRY_NAME) |
| 1021 |
|
| 1022 |
def get_cert_state_or_province(self) -> str: |
| 1023 |
""" |
| 1024 |
A convenience method to get a string that contains the :const:`CERT_STATE_OR_PROVINCE` |
| 1025 |
sub field of the subject field in the ssl certificate. |
| 1026 |
|
| 1027 |
:return: A string containing the :const:`CERT_STATE_OR_PROVINCE` sub field. |
| 1028 |
""" |
| 1029 |
return self.get_cert_subject_subfield(SSL.CERT_STATE_OR_PROVINCE) |
| 1030 |
|
| 1031 |
def get_cert_fingerprint(self, fingerprint_length: int, digest_name: int) -> Optional[str]: |
| 1032 |
""" |
| 1033 |
Get the fingerprint of the certificate. The certificate fingerprint |
| 1034 |
(as displayed in the Fingerprints section when looking at a certificate |
| 1035 |
with say the Firefox browser) is the hexadecimal hash of the entire |
| 1036 |
certificate. The fingerprint is not part of the certificate, rather |
| 1037 |
it is computed from the certificate and can be used to uniquely identify |
| 1038 |
a certificate. |
| 1039 |
|
| 1040 |
:param fingerprint_length: Must be :math:`>= 33` for md5, :math:`>= 41` |
| 1041 |
for sha1, :math:`>= 65` for sha256 and :math:`>= 129` |
| 1042 |
for sha512. |
| 1043 |
:param digest_name: The hash algorithm to use. Must be one of :const:`SHA1`, |
| 1044 |
:const:`SHA256`, :const:`SHA512`, :const:`MD5`. |
| 1045 |
:return: Hex fingerprint in a string, or ``None`` if an error occurred. |
| 1046 |
""" |
| 1047 |
rc, fingerprint_str = pn_ssl_get_cert_fingerprint(self._ssl, fingerprint_length, digest_name) |
| 1048 |
if rc == PN_OK: |
| 1049 |
return fingerprint_str |
| 1050 |
return None |
| 1051 |
|
| 1052 |
# Convenience functions for obtaining fingerprint for specific hashing algorithms |
| 1053 |
def _get_cert_fingerprint_unknown_hash_alg(self) -> None: |
| 1054 |
return self.get_cert_fingerprint(41, 10) |
| 1055 |
|
| 1056 |
def get_cert_fingerprint_sha1(self) -> Optional[str]: |
| 1057 |
""" |
| 1058 |
A convenience method to get the :const:`SHA1` fingerprint of the |
| 1059 |
certificate. |
| 1060 |
|
| 1061 |
:return: Hex fingerprint in a string, or ``None`` if an error occurred. |
| 1062 |
""" |
| 1063 |
return self.get_cert_fingerprint(41, SSL.SHA1) |
| 1064 |
|
| 1065 |
def get_cert_fingerprint_sha256(self) -> Optional[str]: |
| 1066 |
""" |
| 1067 |
A convenience method to get the :const:`SHA256` fingerprint of the |
| 1068 |
certificate. |
| 1069 |
|
| 1070 |
:return: Hex fingerprint in a string, or ``None`` if an error occurred. |
| 1071 |
""" |
| 1072 |
# sha256 produces a fingerprint that is 64 characters long |
| 1073 |
return self.get_cert_fingerprint(65, SSL.SHA256) |
| 1074 |
|
| 1075 |
def get_cert_fingerprint_sha512(self) -> Optional[str]: |
| 1076 |
""" |
| 1077 |
A convenience method to get the :const:`SHA512` fingerprint of the |
| 1078 |
certificate. |
| 1079 |
|
| 1080 |
:return: Hex fingerprint in a string, or ``None`` if an error occurred. |
| 1081 |
""" |
| 1082 |
# sha512 produces a fingerprint that is 128 characters long |
| 1083 |
return self.get_cert_fingerprint(129, SSL.SHA512) |
| 1084 |
|
| 1085 |
def get_cert_fingerprint_md5(self) -> Optional[str]: |
| 1086 |
""" |
| 1087 |
A convenience method to get the :const:`MD5` fingerprint of the |
| 1088 |
certificate. |
| 1089 |
|
| 1090 |
:return: Hex fingerprint in a string, or ``None`` if an error occurred. |
| 1091 |
""" |
| 1092 |
return self.get_cert_fingerprint(33, SSL.MD5) |
| 1093 |
|
| 1094 |
@property |
| 1095 |
def remote_subject(self) -> str: |
| 1096 |
""" |
| 1097 |
The subject from the peers certificate. |
| 1098 |
""" |
| 1099 |
return pn_ssl_get_remote_subject(self._ssl) |
| 1100 |
|
| 1101 |
RESUME_UNKNOWN = PN_SSL_RESUME_UNKNOWN |
| 1102 |
"""Session resume state unknown/not supported.""" |
| 1103 |
|
| 1104 |
RESUME_NEW = PN_SSL_RESUME_NEW |
| 1105 |
"""Session renegotiated - not resumed.""" |
| 1106 |
|
| 1107 |
RESUME_REUSED = PN_SSL_RESUME_REUSED |
| 1108 |
"""Session resumed from previous session.""" |
| 1109 |
|
| 1110 |
def resume_status(self) -> int: |
| 1111 |
""" |
| 1112 |
Check whether the state has been resumed. |
| 1113 |
|
| 1114 |
Used for client session resume. When called on an active session, |
| 1115 |
indicates whether the state has been resumed from a previous session. |
| 1116 |
|
| 1117 |
.. note:: This is a best-effort service - there is no guarantee that |
| 1118 |
the remote server will accept the resumed parameters. The remote |
| 1119 |
server may choose to ignore these parameters, and request a |
| 1120 |
re-negotiation instead. |
| 1121 |
|
| 1122 |
:return: Status code indicating whether or not the session has been |
| 1123 |
resumed. One of: |
| 1124 |
* :const:`RESUME_UNKNOWN` |
| 1125 |
* :const:`RESUME_NEW` |
| 1126 |
* :const:`RESUME_REUSED` |
| 1127 |
""" |
| 1128 |
return pn_ssl_resume_status(self._ssl) |
| 1129 |
|
| 1130 |
@property |
| 1131 |
def peer_hostname(self) -> str: |
| 1132 |
"""Manage the expected name of the remote peer. |
| 1133 |
|
| 1134 |
The hostname is used for two purposes: |
| 1135 |
|
| 1136 |
1. when set on an SSL client, it is sent to the server during the |
| 1137 |
handshake (if Server Name Indication is supported) |
| 1138 |
2. it is used to check against the identifying name provided in the |
| 1139 |
peer's certificate. If the supplied name does not exactly match a |
| 1140 |
SubjectAltName (type DNS name), or the CommonName entry in the |
| 1141 |
peer's certificate, the peer is considered unauthenticated |
| 1142 |
(potential imposter), and the SSL connection is aborted. |
| 1143 |
|
| 1144 |
.. note:: Verification of the hostname is only done if |
| 1145 |
:const:`SSLDomain.VERIFY_PEER_NAME` is set using |
| 1146 |
:meth:`SSLDomain.set_peer_authentication`.""" |
| 1147 |
err, name = pn_ssl_get_peer_hostname(self._ssl, 1024) |
| 1148 |
self._check(err) |
| 1149 |
return utf82unicode(name) |
| 1150 |
|
| 1151 |
@peer_hostname.setter |
| 1152 |
def peer_hostname(self, hostname: Optional[str]) -> None: |
| 1153 |
self._check(pn_ssl_set_peer_hostname(self._ssl, unicode2utf8(hostname))) |
| 1154 |
|
| 1155 |
|
| 1156 |
class SSLSessionDetails(object): |
| 1157 |
""" |
| 1158 |
Unique identifier for the SSL session. Used to resume previous |
| 1159 |
session on a new SSL connection. |
| 1160 |
""" |
| 1161 |
|
| 1162 |
def __init__(self, session_id: str) -> None: |
| 1163 |
self._session_id = session_id |
| 1164 |
|
| 1165 |
def get_session_id(self) -> str: |
| 1166 |
""" |
| 1167 |
Get the unique identifier for this SSL session |
| 1168 |
|
| 1169 |
:return: Session identifier |
| 1170 |
""" |
| 1171 |
return self._session_id |