578
594
# gnutls.strerror()
580
596
if message is None and code is not None:
581
message = gnutls.strerror(code)
597
message = gnutls.strerror(code).decode(
598
"utf-8", errors="replace")
582
599
return super(gnutls.Error, self).__init__(
585
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):
590
632
def __init__(self):
591
self._c_object = gnutls.certificate_credentials_t()
592
gnutls.certificate_allocate_credentials(
593
ctypes.byref(self._c_object))
633
self._as_parameter_ = gnutls.certificate_credentials_t()
634
gnutls.certificate_allocate_credentials(self)
594
635
self.type = gnutls.CRD_CERTIFICATE
596
637
def __del__(self):
597
gnutls.certificate_free_credentials(self._c_object)
638
gnutls.certificate_free_credentials(self)
640
class ClientSession(With_from_param):
600
641
def __init__(self, socket, credentials=None):
601
self._c_object = gnutls.session_t()
642
self._as_parameter_ = gnutls.session_t()
602
643
gnutls_flags = gnutls.CLIENT
603
644
if gnutls.check_version(b"3.5.6"):
604
645
gnutls_flags |= gnutls.NO_TICKETS
605
646
if gnutls.has_rawpk:
606
647
gnutls_flags |= gnutls.ENABLE_RAWPK
607
gnutls.init(ctypes.byref(self._c_object), gnutls_flags)
648
gnutls.init(self, gnutls_flags)
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,
650
gnutls.set_default_priority(self)
651
gnutls.transport_set_ptr(self, socket.fileno())
652
gnutls.handshake_set_private_extensions(self, True)
613
653
self.socket = socket
614
654
if credentials is None:
615
655
credentials = gnutls.Credentials()
616
gnutls.credentials_set(self._c_object, credentials.type,
617
ctypes.cast(credentials._c_object,
656
gnutls.credentials_set(self, credentials.type,
619
658
self.credentials = credentials
621
660
def __del__(self):
622
gnutls.deinit(self._c_object)
624
663
def handshake(self):
625
return gnutls.handshake(self._c_object)
664
return gnutls.handshake(self)
627
666
def send(self, data):
628
667
data = bytes(data)
629
668
data_len = len(data)
630
669
while data_len > 0:
631
data_len -= gnutls.record_send(self._c_object,
670
data_len -= gnutls.record_send(self, data[-data_len:],
636
return gnutls.bye(self._c_object, gnutls.SHUT_RDWR)
674
return gnutls.bye(self, gnutls.SHUT_RDWR)
638
676
# Error handling functions
639
677
def _error_code(result):
640
678
"""A function to raise exceptions on errors, suitable
641
679
for the 'restype' attribute on ctypes functions"""
680
if result >= gnutls.E_SUCCESS:
644
682
if result == gnutls.E_NO_CERTIFICATE_FOUND:
645
683
raise gnutls.CertificateSecurityError(code=result)
646
684
raise gnutls.Error(code=result)
648
def _retry_on_error(result, func, arguments):
686
def _retry_on_error(result, func, arguments,
687
_error_code=_error_code):
649
688
"""A function to retry on some errors, suitable
650
689
for the 'errcheck' attribute on ctypes functions"""
690
while result < gnutls.E_SUCCESS:
652
691
if result not in (gnutls.E_INTERRUPTED, gnutls.E_AGAIN):
653
692
return _error_code(result)
654
693
result = func(*arguments)
661
700
priority_set_direct = _library.gnutls_priority_set_direct
662
priority_set_direct.argtypes = [session_t, ctypes.c_char_p,
701
priority_set_direct.argtypes = [ClientSession, ctypes.c_char_p,
663
702
ctypes.POINTER(ctypes.c_char_p)]
664
703
priority_set_direct.restype = _error_code
666
705
init = _library.gnutls_init
667
init.argtypes = [ctypes.POINTER(session_t), ctypes.c_int]
706
init.argtypes = [PointerTo(ClientSession), ctypes.c_int]
668
707
init.restype = _error_code
670
709
set_default_priority = _library.gnutls_set_default_priority
671
set_default_priority.argtypes = [session_t]
710
set_default_priority.argtypes = [ClientSession]
672
711
set_default_priority.restype = _error_code
674
713
record_send = _library.gnutls_record_send
675
record_send.argtypes = [session_t, ctypes.c_void_p,
714
record_send.argtypes = [ClientSession, ctypes.c_void_p,
677
716
record_send.restype = ctypes.c_ssize_t
678
717
record_send.errcheck = _retry_on_error
680
719
certificate_allocate_credentials = (
681
720
_library.gnutls_certificate_allocate_credentials)
682
721
certificate_allocate_credentials.argtypes = [
683
ctypes.POINTER(certificate_credentials_t)]
722
PointerTo(Credentials)]
684
723
certificate_allocate_credentials.restype = _error_code
686
725
certificate_free_credentials = (
687
726
_library.gnutls_certificate_free_credentials)
688
certificate_free_credentials.argtypes = [
689
certificate_credentials_t]
727
certificate_free_credentials.argtypes = [Credentials]
690
728
certificate_free_credentials.restype = None
692
730
handshake_set_private_extensions = (
693
731
_library.gnutls_handshake_set_private_extensions)
694
handshake_set_private_extensions.argtypes = [session_t,
732
handshake_set_private_extensions.argtypes = [ClientSession,
696
734
handshake_set_private_extensions.restype = None
698
736
credentials_set = _library.gnutls_credentials_set
699
credentials_set.argtypes = [session_t, credentials_type_t,
737
credentials_set.argtypes = [ClientSession, credentials_type_t,
738
CastToVoidPointer(Credentials)]
701
739
credentials_set.restype = _error_code
703
741
strerror = _library.gnutls_strerror
722
760
global_set_log_function.restype = None
724
762
deinit = _library.gnutls_deinit
725
deinit.argtypes = [session_t]
763
deinit.argtypes = [ClientSession]
726
764
deinit.restype = None
728
766
handshake = _library.gnutls_handshake
729
handshake.argtypes = [session_t]
730
handshake.restype = _error_code
767
handshake.argtypes = [ClientSession]
768
handshake.restype = ctypes.c_int
731
769
handshake.errcheck = _retry_on_error
733
771
transport_set_ptr = _library.gnutls_transport_set_ptr
734
transport_set_ptr.argtypes = [session_t, transport_ptr_t]
772
transport_set_ptr.argtypes = [ClientSession, transport_ptr_t]
735
773
transport_set_ptr.restype = None
737
775
bye = _library.gnutls_bye
738
bye.argtypes = [session_t, close_request_t]
739
bye.restype = _error_code
776
bye.argtypes = [ClientSession, close_request_t]
777
bye.restype = ctypes.c_int
740
778
bye.errcheck = _retry_on_error
742
780
check_version = _library.gnutls_check_version
2766
2813
def rfc3339_duration_to_delta(duration):
2767
2814
"""Parse an RFC 3339 "duration" and return a datetime.timedelta
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)
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)
2785
2834
# Parsing an RFC 3339 duration with regular expressions is not