593
594
# gnutls.strerror()
595
596
if message is None and code is not None:
596
message = gnutls.strerror(code)
597
message = gnutls.strerror(code).decode(
598
"utf-8", errors="replace")
597
599
return super(gnutls.Error, self).__init__(
600
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):
605
632
def __init__(self):
606
self._c_object = gnutls.certificate_credentials_t()
607
gnutls.certificate_allocate_credentials(
608
ctypes.byref(self._c_object))
633
self._as_parameter_ = gnutls.certificate_credentials_t()
634
gnutls.certificate_allocate_credentials(self)
609
635
self.type = gnutls.CRD_CERTIFICATE
611
637
def __del__(self):
612
gnutls.certificate_free_credentials(self._c_object)
638
gnutls.certificate_free_credentials(self)
640
class ClientSession(With_from_param):
615
641
def __init__(self, socket, credentials=None):
616
self._c_object = gnutls.session_t()
642
self._as_parameter_ = gnutls.session_t()
617
643
gnutls_flags = gnutls.CLIENT
618
644
if gnutls.check_version(b"3.5.6"):
619
645
gnutls_flags |= gnutls.NO_TICKETS
620
646
if gnutls.has_rawpk:
621
647
gnutls_flags |= gnutls.ENABLE_RAWPK
622
gnutls.init(ctypes.byref(self._c_object), gnutls_flags)
648
gnutls.init(self, gnutls_flags)
624
gnutls.set_default_priority(self._c_object)
625
gnutls.transport_set_ptr(self._c_object, socket.fileno())
626
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)
628
653
self.socket = socket
629
654
if credentials is None:
630
655
credentials = gnutls.Credentials()
631
gnutls.credentials_set(self._c_object, credentials.type,
632
ctypes.cast(credentials._c_object,
656
gnutls.credentials_set(self, credentials.type,
634
658
self.credentials = credentials
636
660
def __del__(self):
637
gnutls.deinit(self._c_object)
639
663
def handshake(self):
640
return gnutls.handshake(self._c_object)
664
return gnutls.handshake(self)
642
666
def send(self, data):
643
667
data = bytes(data)
644
668
data_len = len(data)
645
669
while data_len > 0:
646
data_len -= gnutls.record_send(self._c_object,
670
data_len -= gnutls.record_send(self, data[-data_len:],
651
return gnutls.bye(self._c_object, gnutls.SHUT_RDWR)
674
return gnutls.bye(self, gnutls.SHUT_RDWR)
653
676
# Error handling functions
654
677
def _error_code(result):
655
678
"""A function to raise exceptions on errors, suitable
656
for the 'restype' attribute on ctypes functions"""
679
for the "restype" attribute on ctypes functions"""
680
if result >= gnutls.E_SUCCESS:
659
682
if result == gnutls.E_NO_CERTIFICATE_FOUND:
660
683
raise gnutls.CertificateSecurityError(code=result)
661
684
raise gnutls.Error(code=result)
663
def _retry_on_error(result, func, arguments):
686
def _retry_on_error(result, func, arguments,
687
_error_code=_error_code):
664
688
"""A function to retry on some errors, suitable
665
for the 'errcheck' attribute on ctypes functions"""
689
for the "errcheck" attribute on ctypes functions"""
690
while result < gnutls.E_SUCCESS:
667
691
if result not in (gnutls.E_INTERRUPTED, gnutls.E_AGAIN):
668
692
return _error_code(result)
669
693
result = func(*arguments)
676
700
priority_set_direct = _library.gnutls_priority_set_direct
677
priority_set_direct.argtypes = [session_t, ctypes.c_char_p,
701
priority_set_direct.argtypes = [ClientSession, ctypes.c_char_p,
678
702
ctypes.POINTER(ctypes.c_char_p)]
679
703
priority_set_direct.restype = _error_code
681
705
init = _library.gnutls_init
682
init.argtypes = [ctypes.POINTER(session_t), ctypes.c_int]
706
init.argtypes = [PointerTo(ClientSession), ctypes.c_int]
683
707
init.restype = _error_code
685
709
set_default_priority = _library.gnutls_set_default_priority
686
set_default_priority.argtypes = [session_t]
710
set_default_priority.argtypes = [ClientSession]
687
711
set_default_priority.restype = _error_code
689
713
record_send = _library.gnutls_record_send
690
record_send.argtypes = [session_t, ctypes.c_void_p,
714
record_send.argtypes = [ClientSession, ctypes.c_void_p,
692
716
record_send.restype = ctypes.c_ssize_t
693
717
record_send.errcheck = _retry_on_error
695
719
certificate_allocate_credentials = (
696
720
_library.gnutls_certificate_allocate_credentials)
697
721
certificate_allocate_credentials.argtypes = [
698
ctypes.POINTER(certificate_credentials_t)]
722
PointerTo(Credentials)]
699
723
certificate_allocate_credentials.restype = _error_code
701
725
certificate_free_credentials = (
702
726
_library.gnutls_certificate_free_credentials)
703
certificate_free_credentials.argtypes = [
704
certificate_credentials_t]
727
certificate_free_credentials.argtypes = [Credentials]
705
728
certificate_free_credentials.restype = None
707
730
handshake_set_private_extensions = (
708
731
_library.gnutls_handshake_set_private_extensions)
709
handshake_set_private_extensions.argtypes = [session_t,
732
handshake_set_private_extensions.argtypes = [ClientSession,
711
734
handshake_set_private_extensions.restype = None
713
736
credentials_set = _library.gnutls_credentials_set
714
credentials_set.argtypes = [session_t, credentials_type_t,
737
credentials_set.argtypes = [ClientSession, credentials_type_t,
738
CastToVoidPointer(Credentials)]
716
739
credentials_set.restype = _error_code
718
741
strerror = _library.gnutls_strerror
737
760
global_set_log_function.restype = None
739
762
deinit = _library.gnutls_deinit
740
deinit.argtypes = [session_t]
763
deinit.argtypes = [ClientSession]
741
764
deinit.restype = None
743
766
handshake = _library.gnutls_handshake
744
handshake.argtypes = [session_t]
745
handshake.restype = _error_code
767
handshake.argtypes = [ClientSession]
768
handshake.restype = ctypes.c_int
746
769
handshake.errcheck = _retry_on_error
748
771
transport_set_ptr = _library.gnutls_transport_set_ptr
749
transport_set_ptr.argtypes = [session_t, transport_ptr_t]
772
transport_set_ptr.argtypes = [ClientSession, transport_ptr_t]
750
773
transport_set_ptr.restype = None
752
775
bye = _library.gnutls_bye
753
bye.argtypes = [session_t, close_request_t]
754
bye.restype = _error_code
776
bye.argtypes = [ClientSession, close_request_t]
777
bye.restype = ctypes.c_int
755
778
bye.errcheck = _retry_on_error
757
780
check_version = _library.gnutls_check_version
2243
2272
class ProxyClient:
2244
2273
def __init__(self, child_pipe, key_id, fpr, address):
2245
2274
self._pipe = child_pipe
2246
self._pipe.send(('init', key_id, fpr, address))
2275
self._pipe.send(("init", key_id, fpr, address))
2247
2276
if not self._pipe.recv():
2248
2277
raise KeyError(key_id or fpr)
2250
2279
def __getattribute__(self, name):
2252
2281
return super(ProxyClient, self).__getattribute__(name)
2253
self._pipe.send(('getattr', name))
2282
self._pipe.send(("getattr", name))
2254
2283
data = self._pipe.recv()
2255
if data[0] == 'data':
2284
if data[0] == "data":
2257
if data[0] == 'function':
2286
if data[0] == "function":
2259
2288
def func(*args, **kwargs):
2260
self._pipe.send(('funcall', name, args, kwargs))
2289
self._pipe.send(("funcall", name, args, kwargs))
2261
2290
return self._pipe.recv()[1]
2265
2294
def __setattr__(self, name, value):
2267
2296
return super(ProxyClient, self).__setattr__(name, value)
2268
self._pipe.send(('setattr', name, value))
2297
self._pipe.send(("setattr", name, value))
2271
2300
class ClientHandler(socketserver.BaseRequestHandler, object):
2752
2784
# remove the old hook in favor of the new above hook on
2755
if command == 'funcall':
2787
if command == "funcall":
2756
2788
funcname = request[1]
2757
2789
args = request[2]
2758
2790
kwargs = request[3]
2760
parent_pipe.send(('data', getattr(client_object,
2792
parent_pipe.send(("data", getattr(client_object,
2761
2793
funcname)(*args,
2764
if command == 'getattr':
2796
if command == "getattr":
2765
2797
attrname = request[1]
2766
2798
if isinstance(client_object.__getattribute__(attrname),
2767
2799
collections.abc.Callable):
2768
parent_pipe.send(('function', ))
2800
parent_pipe.send(("function", ))
2770
2802
parent_pipe.send((
2771
'data', client_object.__getattribute__(attrname)))
2803
"data", client_object.__getattribute__(attrname)))
2773
if command == 'setattr':
2805
if command == "setattr":
2774
2806
attrname = request[1]
2775
2807
value = request[2]
2776
2808
setattr(client_object, attrname, value)
2781
2813
def rfc3339_duration_to_delta(duration):
2782
2814
"""Parse an RFC 3339 "duration" and return a datetime.timedelta
2784
>>> rfc3339_duration_to_delta("P7D") == datetime.timedelta(7)
2786
>>> rfc3339_duration_to_delta("PT60S") == datetime.timedelta(0, 60)
2788
>>> rfc3339_duration_to_delta("PT60M") == datetime.timedelta(0, 3600)
2790
>>> rfc3339_duration_to_delta("PT24H") == datetime.timedelta(1)
2792
>>> rfc3339_duration_to_delta("P1W") == datetime.timedelta(7)
2794
>>> rfc3339_duration_to_delta("PT5M30S") == datetime.timedelta(0, 330)
2796
>>> 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)
2800
2834
# Parsing an RFC 3339 duration with regular expressions is not
2880
2914
def string_to_delta(interval):
2881
2915
"""Parse a string and return a datetime.timedelta
2883
>>> string_to_delta('7d') == datetime.timedelta(7)
2885
>>> string_to_delta('60s') == datetime.timedelta(0, 60)
2887
>>> string_to_delta('60m') == datetime.timedelta(0, 3600)
2889
>>> string_to_delta('24h') == datetime.timedelta(1)
2891
>>> string_to_delta('1w') == datetime.timedelta(7)
2893
>>> 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)