594
578
# gnutls.strerror()
596
580
if message is None and code is not None:
597
message = gnutls.strerror(code).decode(
598
"utf-8", errors="replace")
581
message = gnutls.strerror(code)
599
582
return super(gnutls.Error, self).__init__(
602
585
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):
632
590
def __init__(self):
633
self._as_parameter_ = gnutls.certificate_credentials_t()
634
gnutls.certificate_allocate_credentials(self)
591
self._c_object = gnutls.certificate_credentials_t()
592
gnutls.certificate_allocate_credentials(
593
ctypes.byref(self._c_object))
635
594
self.type = gnutls.CRD_CERTIFICATE
637
596
def __del__(self):
638
gnutls.certificate_free_credentials(self)
597
gnutls.certificate_free_credentials(self._c_object)
640
class ClientSession(With_from_param):
641
600
def __init__(self, socket, credentials=None):
642
self._as_parameter_ = gnutls.session_t()
601
self._c_object = gnutls.session_t()
643
602
gnutls_flags = gnutls.CLIENT
644
603
if gnutls.check_version(b"3.5.6"):
645
604
gnutls_flags |= gnutls.NO_TICKETS
646
605
if gnutls.has_rawpk:
647
606
gnutls_flags |= gnutls.ENABLE_RAWPK
648
gnutls.init(self, gnutls_flags)
607
gnutls.init(ctypes.byref(self._c_object), gnutls_flags)
650
gnutls.set_default_priority(self)
651
gnutls.transport_set_ptr(self, socket.fileno())
652
gnutls.handshake_set_private_extensions(self, True)
609
gnutls.set_default_priority(self._c_object)
610
gnutls.transport_set_ptr(self._c_object, socket.fileno())
611
gnutls.handshake_set_private_extensions(self._c_object,
653
613
self.socket = socket
654
614
if credentials is None:
655
615
credentials = gnutls.Credentials()
656
gnutls.credentials_set(self, credentials.type,
616
gnutls.credentials_set(self._c_object, credentials.type,
617
ctypes.cast(credentials._c_object,
658
619
self.credentials = credentials
660
621
def __del__(self):
622
gnutls.deinit(self._c_object)
663
624
def handshake(self):
664
return gnutls.handshake(self)
625
return gnutls.handshake(self._c_object)
666
627
def send(self, data):
667
628
data = bytes(data)
668
629
data_len = len(data)
669
630
while data_len > 0:
670
data_len -= gnutls.record_send(self, data[-data_len:],
631
data_len -= gnutls.record_send(self._c_object,
674
return gnutls.bye(self, gnutls.SHUT_RDWR)
636
return gnutls.bye(self._c_object, gnutls.SHUT_RDWR)
676
638
# Error handling functions
677
639
def _error_code(result):
678
640
"""A function to raise exceptions on errors, suitable
679
for the "restype" attribute on ctypes functions"""
680
if result >= gnutls.E_SUCCESS:
641
for the 'restype' attribute on ctypes functions"""
682
644
if result == gnutls.E_NO_CERTIFICATE_FOUND:
683
645
raise gnutls.CertificateSecurityError(code=result)
684
646
raise gnutls.Error(code=result)
686
def _retry_on_error(result, func, arguments,
687
_error_code=_error_code):
648
def _retry_on_error(result, func, arguments):
688
649
"""A function to retry on some errors, suitable
689
for the "errcheck" attribute on ctypes functions"""
690
while result < gnutls.E_SUCCESS:
650
for the 'errcheck' attribute on ctypes functions"""
691
652
if result not in (gnutls.E_INTERRUPTED, gnutls.E_AGAIN):
692
653
return _error_code(result)
693
654
result = func(*arguments)
700
661
priority_set_direct = _library.gnutls_priority_set_direct
701
priority_set_direct.argtypes = [ClientSession, ctypes.c_char_p,
662
priority_set_direct.argtypes = [session_t, ctypes.c_char_p,
702
663
ctypes.POINTER(ctypes.c_char_p)]
703
664
priority_set_direct.restype = _error_code
705
666
init = _library.gnutls_init
706
init.argtypes = [PointerTo(ClientSession), ctypes.c_int]
667
init.argtypes = [ctypes.POINTER(session_t), ctypes.c_int]
707
668
init.restype = _error_code
709
670
set_default_priority = _library.gnutls_set_default_priority
710
set_default_priority.argtypes = [ClientSession]
671
set_default_priority.argtypes = [session_t]
711
672
set_default_priority.restype = _error_code
713
674
record_send = _library.gnutls_record_send
714
record_send.argtypes = [ClientSession, ctypes.c_void_p,
675
record_send.argtypes = [session_t, ctypes.c_void_p,
716
677
record_send.restype = ctypes.c_ssize_t
717
678
record_send.errcheck = _retry_on_error
719
680
certificate_allocate_credentials = (
720
681
_library.gnutls_certificate_allocate_credentials)
721
682
certificate_allocate_credentials.argtypes = [
722
PointerTo(Credentials)]
683
ctypes.POINTER(certificate_credentials_t)]
723
684
certificate_allocate_credentials.restype = _error_code
725
686
certificate_free_credentials = (
726
687
_library.gnutls_certificate_free_credentials)
727
certificate_free_credentials.argtypes = [Credentials]
688
certificate_free_credentials.argtypes = [
689
certificate_credentials_t]
728
690
certificate_free_credentials.restype = None
730
692
handshake_set_private_extensions = (
731
693
_library.gnutls_handshake_set_private_extensions)
732
handshake_set_private_extensions.argtypes = [ClientSession,
694
handshake_set_private_extensions.argtypes = [session_t,
734
696
handshake_set_private_extensions.restype = None
736
698
credentials_set = _library.gnutls_credentials_set
737
credentials_set.argtypes = [ClientSession, credentials_type_t,
738
CastToVoidPointer(Credentials)]
699
credentials_set.argtypes = [session_t, credentials_type_t,
739
701
credentials_set.restype = _error_code
741
703
strerror = _library.gnutls_strerror
760
722
global_set_log_function.restype = None
762
724
deinit = _library.gnutls_deinit
763
deinit.argtypes = [ClientSession]
725
deinit.argtypes = [session_t]
764
726
deinit.restype = None
766
728
handshake = _library.gnutls_handshake
767
handshake.argtypes = [ClientSession]
768
handshake.restype = ctypes.c_int
729
handshake.argtypes = [session_t]
730
handshake.restype = _error_code
769
731
handshake.errcheck = _retry_on_error
771
733
transport_set_ptr = _library.gnutls_transport_set_ptr
772
transport_set_ptr.argtypes = [ClientSession, transport_ptr_t]
734
transport_set_ptr.argtypes = [session_t, transport_ptr_t]
773
735
transport_set_ptr.restype = None
775
737
bye = _library.gnutls_bye
776
bye.argtypes = [ClientSession, close_request_t]
777
bye.restype = ctypes.c_int
738
bye.argtypes = [session_t, close_request_t]
739
bye.restype = _error_code
778
740
bye.errcheck = _retry_on_error
780
742
check_version = _library.gnutls_check_version
2272
2228
class ProxyClient:
2273
2229
def __init__(self, child_pipe, key_id, fpr, address):
2274
2230
self._pipe = child_pipe
2275
self._pipe.send(("init", key_id, fpr, address))
2231
self._pipe.send(('init', key_id, fpr, address))
2276
2232
if not self._pipe.recv():
2277
2233
raise KeyError(key_id or fpr)
2279
2235
def __getattribute__(self, name):
2281
2237
return super(ProxyClient, self).__getattribute__(name)
2282
self._pipe.send(("getattr", name))
2238
self._pipe.send(('getattr', name))
2283
2239
data = self._pipe.recv()
2284
if data[0] == "data":
2240
if data[0] == 'data':
2286
if data[0] == "function":
2242
if data[0] == 'function':
2288
2244
def func(*args, **kwargs):
2289
self._pipe.send(("funcall", name, args, kwargs))
2245
self._pipe.send(('funcall', name, args, kwargs))
2290
2246
return self._pipe.recv()[1]
2294
2250
def __setattr__(self, name, value):
2296
2252
return super(ProxyClient, self).__setattr__(name, value)
2297
self._pipe.send(("setattr", name, value))
2253
self._pipe.send(('setattr', name, value))
2300
2256
class ClientHandler(socketserver.BaseRequestHandler, object):
2784
2737
# remove the old hook in favor of the new above hook on
2787
if command == "funcall":
2740
if command == 'funcall':
2788
2741
funcname = request[1]
2789
2742
args = request[2]
2790
2743
kwargs = request[3]
2792
parent_pipe.send(("data", getattr(client_object,
2745
parent_pipe.send(('data', getattr(client_object,
2793
2746
funcname)(*args,
2796
if command == "getattr":
2749
if command == 'getattr':
2797
2750
attrname = request[1]
2798
2751
if isinstance(client_object.__getattribute__(attrname),
2799
collections.abc.Callable):
2800
parent_pipe.send(("function", ))
2752
collections.Callable):
2753
parent_pipe.send(('function', ))
2802
2755
parent_pipe.send((
2803
"data", client_object.__getattribute__(attrname)))
2756
'data', client_object.__getattribute__(attrname)))
2805
if command == "setattr":
2758
if command == 'setattr':
2806
2759
attrname = request[1]
2807
2760
value = request[2]
2808
2761
setattr(client_object, attrname, value)
2813
2766
def rfc3339_duration_to_delta(duration):
2814
2767
"""Parse an RFC 3339 "duration" and return a datetime.timedelta
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)
2769
>>> rfc3339_duration_to_delta("P7D") == datetime.timedelta(7)
2771
>>> rfc3339_duration_to_delta("PT60S") == datetime.timedelta(0, 60)
2773
>>> rfc3339_duration_to_delta("PT60M") == datetime.timedelta(0, 3600)
2775
>>> rfc3339_duration_to_delta("PT24H") == datetime.timedelta(1)
2777
>>> rfc3339_duration_to_delta("P1W") == datetime.timedelta(7)
2779
>>> rfc3339_duration_to_delta("PT5M30S") == datetime.timedelta(0, 330)
2781
>>> rfc3339_duration_to_delta("P1DT3M20S") == datetime.timedelta(1, 200)
2834
2785
# Parsing an RFC 3339 duration with regular expressions is not
2914
2865
def string_to_delta(interval):
2915
2866
"""Parse a string and return a datetime.timedelta
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)
2868
>>> string_to_delta('7d') == datetime.timedelta(7)
2870
>>> string_to_delta('60s') == datetime.timedelta(0, 60)
2872
>>> string_to_delta('60m') == datetime.timedelta(0, 3600)
2874
>>> string_to_delta('24h') == datetime.timedelta(1)
2876
>>> string_to_delta('1w') == datetime.timedelta(7)
2878
>>> string_to_delta('5m 30s') == datetime.timedelta(0, 330)