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