576
594
# gnutls.strerror()
578
596
if message is None and code is not None:
579
message = gnutls.strerror(code)
597
message = gnutls.strerror(code).decode(
598
"utf-8", errors="replace")
580
599
return super(gnutls.Error, self).__init__(
583
602
class CertificateSecurityError(Error):
606
def __init__(self, cls):
609
def from_param(self, obj):
610
if not isinstance(obj, self.cls):
611
raise TypeError("Not of type {}: {!r}"
612
.format(self.cls.__name__, obj))
613
return ctypes.byref(obj.from_param(obj))
615
class CastToVoidPointer:
616
def __init__(self, cls):
619
def from_param(self, obj):
620
if not isinstance(obj, self.cls):
621
raise TypeError("Not of type {}: {!r}"
622
.format(self.cls.__name__, obj))
623
return ctypes.cast(obj.from_param(obj), ctypes.c_void_p)
625
class With_from_param:
627
def from_param(cls, obj):
628
return obj._as_parameter_
631
class Credentials(With_from_param):
588
632
def __init__(self):
589
self._c_object = gnutls.certificate_credentials_t()
590
gnutls.certificate_allocate_credentials(
591
ctypes.byref(self._c_object))
633
self._as_parameter_ = gnutls.certificate_credentials_t()
634
gnutls.certificate_allocate_credentials(self)
592
635
self.type = gnutls.CRD_CERTIFICATE
594
637
def __del__(self):
595
gnutls.certificate_free_credentials(self._c_object)
638
gnutls.certificate_free_credentials(self)
640
class ClientSession(With_from_param):
598
641
def __init__(self, socket, credentials=None):
599
self._c_object = gnutls.session_t()
642
self._as_parameter_ = gnutls.session_t()
600
643
gnutls_flags = gnutls.CLIENT
601
644
if gnutls.check_version(b"3.5.6"):
602
645
gnutls_flags |= gnutls.NO_TICKETS
603
646
if gnutls.has_rawpk:
604
647
gnutls_flags |= gnutls.ENABLE_RAWPK
605
gnutls.init(ctypes.byref(self._c_object), gnutls_flags)
648
gnutls.init(self, gnutls_flags)
607
gnutls.set_default_priority(self._c_object)
608
gnutls.transport_set_ptr(self._c_object, socket.fileno())
609
gnutls.handshake_set_private_extensions(self._c_object,
650
gnutls.set_default_priority(self)
651
gnutls.transport_set_ptr(self, socket.fileno())
652
gnutls.handshake_set_private_extensions(self, True)
611
653
self.socket = socket
612
654
if credentials is None:
613
655
credentials = gnutls.Credentials()
614
gnutls.credentials_set(self._c_object, credentials.type,
615
ctypes.cast(credentials._c_object,
656
gnutls.credentials_set(self, credentials.type,
617
658
self.credentials = credentials
619
660
def __del__(self):
620
gnutls.deinit(self._c_object)
622
663
def handshake(self):
623
return gnutls.handshake(self._c_object)
664
return gnutls.handshake(self)
625
666
def send(self, data):
626
667
data = bytes(data)
627
668
data_len = len(data)
628
669
while data_len > 0:
629
data_len -= gnutls.record_send(self._c_object,
670
data_len -= gnutls.record_send(self, data[-data_len:],
634
return gnutls.bye(self._c_object, gnutls.SHUT_RDWR)
674
return gnutls.bye(self, gnutls.SHUT_RDWR)
636
676
# Error handling functions
637
677
def _error_code(result):
638
678
"""A function to raise exceptions on errors, suitable
639
for the 'restype' attribute on ctypes functions"""
679
for the "restype" attribute on ctypes functions"""
680
if result >= gnutls.E_SUCCESS:
642
682
if result == gnutls.E_NO_CERTIFICATE_FOUND:
643
683
raise gnutls.CertificateSecurityError(code=result)
644
684
raise gnutls.Error(code=result)
646
def _retry_on_error(result, func, arguments):
686
def _retry_on_error(result, func, arguments,
687
_error_code=_error_code):
647
688
"""A function to retry on some errors, suitable
648
for the 'errcheck' attribute on ctypes functions"""
689
for the "errcheck" attribute on ctypes functions"""
690
while result < gnutls.E_SUCCESS:
650
691
if result not in (gnutls.E_INTERRUPTED, gnutls.E_AGAIN):
651
692
return _error_code(result)
652
693
result = func(*arguments)
659
700
priority_set_direct = _library.gnutls_priority_set_direct
660
priority_set_direct.argtypes = [session_t, ctypes.c_char_p,
701
priority_set_direct.argtypes = [ClientSession, ctypes.c_char_p,
661
702
ctypes.POINTER(ctypes.c_char_p)]
662
703
priority_set_direct.restype = _error_code
664
705
init = _library.gnutls_init
665
init.argtypes = [ctypes.POINTER(session_t), ctypes.c_int]
706
init.argtypes = [PointerTo(ClientSession), ctypes.c_int]
666
707
init.restype = _error_code
668
709
set_default_priority = _library.gnutls_set_default_priority
669
set_default_priority.argtypes = [session_t]
710
set_default_priority.argtypes = [ClientSession]
670
711
set_default_priority.restype = _error_code
672
713
record_send = _library.gnutls_record_send
673
record_send.argtypes = [session_t, ctypes.c_void_p,
714
record_send.argtypes = [ClientSession, ctypes.c_void_p,
675
716
record_send.restype = ctypes.c_ssize_t
676
717
record_send.errcheck = _retry_on_error
678
719
certificate_allocate_credentials = (
679
720
_library.gnutls_certificate_allocate_credentials)
680
721
certificate_allocate_credentials.argtypes = [
681
ctypes.POINTER(certificate_credentials_t)]
722
PointerTo(Credentials)]
682
723
certificate_allocate_credentials.restype = _error_code
684
725
certificate_free_credentials = (
685
726
_library.gnutls_certificate_free_credentials)
686
certificate_free_credentials.argtypes = [
687
certificate_credentials_t]
727
certificate_free_credentials.argtypes = [Credentials]
688
728
certificate_free_credentials.restype = None
690
730
handshake_set_private_extensions = (
691
731
_library.gnutls_handshake_set_private_extensions)
692
handshake_set_private_extensions.argtypes = [session_t,
732
handshake_set_private_extensions.argtypes = [ClientSession,
694
734
handshake_set_private_extensions.restype = None
696
736
credentials_set = _library.gnutls_credentials_set
697
credentials_set.argtypes = [session_t, credentials_type_t,
737
credentials_set.argtypes = [ClientSession, credentials_type_t,
738
CastToVoidPointer(Credentials)]
699
739
credentials_set.restype = _error_code
701
741
strerror = _library.gnutls_strerror
720
760
global_set_log_function.restype = None
722
762
deinit = _library.gnutls_deinit
723
deinit.argtypes = [session_t]
763
deinit.argtypes = [ClientSession]
724
764
deinit.restype = None
726
766
handshake = _library.gnutls_handshake
727
handshake.argtypes = [session_t]
728
handshake.restype = _error_code
767
handshake.argtypes = [ClientSession]
768
handshake.restype = ctypes.c_int
729
769
handshake.errcheck = _retry_on_error
731
771
transport_set_ptr = _library.gnutls_transport_set_ptr
732
transport_set_ptr.argtypes = [session_t, transport_ptr_t]
772
transport_set_ptr.argtypes = [ClientSession, transport_ptr_t]
733
773
transport_set_ptr.restype = None
735
775
bye = _library.gnutls_bye
736
bye.argtypes = [session_t, close_request_t]
737
bye.restype = _error_code
776
bye.argtypes = [ClientSession, close_request_t]
777
bye.restype = ctypes.c_int
738
778
bye.errcheck = _retry_on_error
740
780
check_version = _library.gnutls_check_version
2225
2272
class ProxyClient:
2226
2273
def __init__(self, child_pipe, key_id, fpr, address):
2227
2274
self._pipe = child_pipe
2228
self._pipe.send(('init', key_id, fpr, address))
2275
self._pipe.send(("init", key_id, fpr, address))
2229
2276
if not self._pipe.recv():
2230
2277
raise KeyError(key_id or fpr)
2232
2279
def __getattribute__(self, name):
2234
2281
return super(ProxyClient, self).__getattribute__(name)
2235
self._pipe.send(('getattr', name))
2282
self._pipe.send(("getattr", name))
2236
2283
data = self._pipe.recv()
2237
if data[0] == 'data':
2284
if data[0] == "data":
2239
if data[0] == 'function':
2286
if data[0] == "function":
2241
2288
def func(*args, **kwargs):
2242
self._pipe.send(('funcall', name, args, kwargs))
2289
self._pipe.send(("funcall", name, args, kwargs))
2243
2290
return self._pipe.recv()[1]
2247
2294
def __setattr__(self, name, value):
2249
2296
return super(ProxyClient, self).__setattr__(name, value)
2250
self._pipe.send(('setattr', name, value))
2297
self._pipe.send(("setattr", name, value))
2253
2300
class ClientHandler(socketserver.BaseRequestHandler, object):
2734
2784
# remove the old hook in favor of the new above hook on
2737
if command == 'funcall':
2787
if command == "funcall":
2738
2788
funcname = request[1]
2739
2789
args = request[2]
2740
2790
kwargs = request[3]
2742
parent_pipe.send(('data', getattr(client_object,
2792
parent_pipe.send(("data", getattr(client_object,
2743
2793
funcname)(*args,
2746
if command == 'getattr':
2796
if command == "getattr":
2747
2797
attrname = request[1]
2748
2798
if isinstance(client_object.__getattribute__(attrname),
2749
collections.Callable):
2750
parent_pipe.send(('function', ))
2799
collections.abc.Callable):
2800
parent_pipe.send(("function", ))
2752
2802
parent_pipe.send((
2753
'data', client_object.__getattribute__(attrname)))
2803
"data", client_object.__getattribute__(attrname)))
2755
if command == 'setattr':
2805
if command == "setattr":
2756
2806
attrname = request[1]
2757
2807
value = request[2]
2758
2808
setattr(client_object, attrname, value)
2763
2813
def rfc3339_duration_to_delta(duration):
2764
2814
"""Parse an RFC 3339 "duration" and return a datetime.timedelta
2766
>>> rfc3339_duration_to_delta("P7D") == datetime.timedelta(7)
2768
>>> rfc3339_duration_to_delta("PT60S") == datetime.timedelta(0, 60)
2770
>>> rfc3339_duration_to_delta("PT60M") == datetime.timedelta(0, 3600)
2772
>>> rfc3339_duration_to_delta("PT24H") == datetime.timedelta(1)
2774
>>> rfc3339_duration_to_delta("P1W") == datetime.timedelta(7)
2776
>>> rfc3339_duration_to_delta("PT5M30S") == datetime.timedelta(0, 330)
2778
>>> rfc3339_duration_to_delta("P1DT3M20S") == datetime.timedelta(1, 200)
2816
>>> timedelta = datetime.timedelta
2817
>>> rfc3339_duration_to_delta("P7D") == timedelta(7)
2819
>>> rfc3339_duration_to_delta("PT60S") == timedelta(0, 60)
2821
>>> rfc3339_duration_to_delta("PT60M") == timedelta(0, 3600)
2823
>>> rfc3339_duration_to_delta("PT24H") == timedelta(1)
2825
>>> rfc3339_duration_to_delta("P1W") == timedelta(7)
2827
>>> rfc3339_duration_to_delta("PT5M30S") == timedelta(0, 330)
2829
>>> rfc3339_duration_to_delta("P1DT3M20S") == timedelta(1, 200)
2782
2834
# Parsing an RFC 3339 duration with regular expressions is not
2862
2914
def string_to_delta(interval):
2863
2915
"""Parse a string and return a datetime.timedelta
2865
>>> string_to_delta('7d') == datetime.timedelta(7)
2867
>>> string_to_delta('60s') == datetime.timedelta(0, 60)
2869
>>> string_to_delta('60m') == datetime.timedelta(0, 3600)
2871
>>> string_to_delta('24h') == datetime.timedelta(1)
2873
>>> string_to_delta('1w') == datetime.timedelta(7)
2875
>>> string_to_delta('5m 30s') == datetime.timedelta(0, 330)
2917
>>> string_to_delta("7d") == datetime.timedelta(7)
2919
>>> string_to_delta("60s") == datetime.timedelta(0, 60)
2921
>>> string_to_delta("60m") == datetime.timedelta(0, 3600)
2923
>>> string_to_delta("24h") == datetime.timedelta(1)
2925
>>> string_to_delta("1w") == datetime.timedelta(7)
2927
>>> string_to_delta("5m 30s") == datetime.timedelta(0, 330)