/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos

  • Committer: Teddy Hogeborn
  • Date: 2021-03-21 20:46:40 UTC
  • Revision ID: teddy@recompile.se-20210321204640-lpsyen8jr9lw1jma
Some cleanup of GnuTLS interface

Rename opaque internal GnuTLS structures named *_int to also start
with underscore (_), as is the custom in Python programs.

Decode byte strings from UTF-8 where needed.  (Fixing, among other
things, all "DEBUG: GnuTLS" lines having a "b'" prefix in Python 3.)

Simplify calling C functions by:
1. Using the "_as_parameter_" attribute to store the ctypes object.
2. Creating and using helper classes to automatically create pointers
   or cast typed pointers to pointers to void.
3. Providing the "from_param()" method on relevant classes.

Remove "restype" attribute on C functions where "errcheck" attribute
is already set.

* mandos (gnutls.session_int): Rename to start with "_".
  (gnutls.openpgp_crt_int): - '' -
  (gnutls.Error.__init__): Decode byte string from gnutls.strerror().
  (gnutls.PointerTo): New helper class.
  (gnutls.CastToVoidPointer): - '' -
  (gnutls.With_from_param): - '' -
  (gnutls.Credentials): Inherit from "With_from_param" and store the
  ctypes object in the "_as_parameter_" attribute instead of
  "_c_object".
  (gnutls._error_code): Use "gnutls.E_SUCCESS" instead of the unadorned
  numerical constant "0".
  (gnutls._retry_on_error): - '' -
  (gnutls.priority_set_direct.argtypes): Use "ClientSession" instead
  of "session_t", and change all callers to match.
  (gnutls.init.argtypes): Use "PointerTo(ClientSession)" instead of
  "ctypes.POINTER(session_t)", and change all callers to match.
  (gnutls.set_default_priority.argtypes): Use "ClientSession" instead
  of "session_t", and change all callers to match.
  (gnutls.record_send.argtypes): - '' -
  (gnutls.certificate_allocate_credentials.argtypes): Use
  "PointerTo(Credentials)" instead of
  "ctypes.POINTER(certificate_credentials_t)", and change all callers
  to match.
  (gnutls.certificate_free_credentials.argtypes): Use "Credentials"
  instead of "certificate_credentials_t", and change all callers to
  match.
  (gnutls.handshake_set_private_extensions.argtypes): Use
  "ClientSession" instead of "session_t", and change all callers to
  match.
  (gnutls.credentials_set.argtypes): Use
  "CastToVoidPointer(Credentials)" instead of "ctypes.c_void_p", and
  change all callers to match.
  (gnutls.certificate_type_get.argtypes): Use "ClientSession" instead
  of "session_t", and change all callers to match.
  (gnutls.certificate_get_peers.argtypes): - '' -
  (gnutls.deinit.argtypes): - '' -
  (gnutls.handshake.argtypes): - '' -
  (gnutls.handshake.restype): Change from "_error_code" to
  "ctypes.c_int".
  (gnutls.transport_set_ptr.argtypes): Use "ClientSession" instead of
  "session_t", and change all callers to match.
  (gnutls.bye.argtypes): - '' -
  (gnutls.bye.restype): Change from "_error_code" to "ctypes.c_int".
  (gnutls.certificate_type_get2.argtypes): Use "ClientSession" instead
  of "session_t", and change all callers to match.
  (ClientHandler.handle): Decode "key_id" bytes to string before
  logging it in the debug log.
  (main.debug_gnutls): Decode GnuTLS log message from bytes to string
  before logging it in the debug log.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
# "AvahiService" class, and some lines in "main".
12
12
#
13
13
# Everything else is
14
 
# Copyright © 2008-2019 Teddy Hogeborn
15
 
# Copyright © 2008-2019 Björn Påhlsson
 
14
# Copyright © 2008-2020 Teddy Hogeborn
 
15
# Copyright © 2008-2020 Björn Påhlsson
16
16
#
17
17
# This file is part of Mandos.
18
18
#
143
143
if sys.version_info < (3, 2):
144
144
    configparser.Configparser = configparser.SafeConfigParser
145
145
 
146
 
version = "1.8.9"
 
146
version = "1.8.14"
147
147
stored_state_file = "clients.pickle"
148
148
 
149
149
logger = logging.getLogger()
524
524
class AvahiServiceToSyslog(AvahiService):
525
525
    def rename(self, *args, **kwargs):
526
526
        """Add the new name to the syslog messages"""
527
 
        ret = super(AvahiServiceToSyslog, self).rename(*args, **kwargs)
 
527
        ret = super(AvahiServiceToSyslog, self).rename(*args,
 
528
                                                       **kwargs)
528
529
        syslogger.setFormatter(logging.Formatter(
529
530
            'Mandos ({}) [%(process)d]: %(levelname)s: %(message)s'
530
531
            .format(self.name)))
562
563
    OPENPGP_FMT_RAW = 0         # gnutls/openpgp.h
563
564
 
564
565
    # Types
565
 
    class session_int(ctypes.Structure):
 
566
    class _session_int(ctypes.Structure):
566
567
        _fields_ = []
567
 
    session_t = ctypes.POINTER(session_int)
 
568
    session_t = ctypes.POINTER(_session_int)
568
569
 
569
570
    class certificate_credentials_st(ctypes.Structure):
570
571
        _fields_ = []
576
577
        _fields_ = [('data', ctypes.POINTER(ctypes.c_ubyte)),
577
578
                    ('size', ctypes.c_uint)]
578
579
 
579
 
    class openpgp_crt_int(ctypes.Structure):
 
580
    class _openpgp_crt_int(ctypes.Structure):
580
581
        _fields_ = []
581
 
    openpgp_crt_t = ctypes.POINTER(openpgp_crt_int)
 
582
    openpgp_crt_t = ctypes.POINTER(_openpgp_crt_int)
582
583
    openpgp_crt_fmt_t = ctypes.c_int  # gnutls/openpgp.h
583
584
    log_func = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p)
584
585
    credentials_type_t = ctypes.c_int
593
594
            # gnutls.strerror()
594
595
            self.code = code
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__(
598
600
                message, *args)
599
601
 
600
602
    class CertificateSecurityError(Error):
601
603
        pass
602
604
 
 
605
    class PointerTo:
 
606
        def __init__(self, cls):
 
607
            self.cls = cls
 
608
 
 
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))
 
614
 
 
615
    class CastToVoidPointer:
 
616
        def __init__(self, cls):
 
617
            self.cls = cls
 
618
 
 
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)
 
624
 
 
625
    class With_from_param:
 
626
        @classmethod
 
627
        def from_param(cls, obj):
 
628
            return obj._as_parameter_
 
629
 
603
630
    # Classes
604
 
    class Credentials:
 
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
610
636
 
611
637
        def __del__(self):
612
 
            gnutls.certificate_free_credentials(self._c_object)
 
638
            gnutls.certificate_free_credentials(self)
613
639
 
614
 
    class ClientSession:
 
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)
623
649
            del 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,
627
 
                                                    True)
 
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,
633
 
                                               ctypes.c_void_p))
 
656
            gnutls.credentials_set(self, credentials.type,
 
657
                                   credentials)
634
658
            self.credentials = credentials
635
659
 
636
660
        def __del__(self):
637
 
            gnutls.deinit(self._c_object)
 
661
            gnutls.deinit(self)
638
662
 
639
663
        def handshake(self):
640
 
            return gnutls.handshake(self._c_object)
 
664
            return gnutls.handshake(self)
641
665
 
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,
647
 
                                               data[-data_len:],
 
670
                data_len -= gnutls.record_send(self, data[-data_len:],
648
671
                                               data_len)
649
672
 
650
673
        def bye(self):
651
 
            return gnutls.bye(self._c_object, gnutls.SHUT_RDWR)
 
674
            return gnutls.bye(self, gnutls.SHUT_RDWR)
652
675
 
653
676
    # Error handling functions
654
677
    def _error_code(result):
655
678
        """A function to raise exceptions on errors, suitable
656
679
        for the 'restype' attribute on ctypes functions"""
657
 
        if result >= 0:
 
680
        if result >= gnutls.E_SUCCESS:
658
681
            return result
659
682
        if result == gnutls.E_NO_CERTIFICATE_FOUND:
660
683
            raise gnutls.CertificateSecurityError(code=result)
661
684
        raise gnutls.Error(code=result)
662
685
 
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
689
        for the 'errcheck' attribute on ctypes functions"""
666
 
        while result < 0:
 
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)
674
698
 
675
699
    # Functions
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
680
704
 
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
684
708
 
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
688
712
 
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,
691
715
                            ctypes.c_size_t]
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
700
724
 
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
706
729
 
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,
710
733
                                                 ctypes.c_int]
711
734
    handshake_set_private_extensions.restype = None
712
735
 
713
736
    credentials_set = _library.gnutls_credentials_set
714
 
    credentials_set.argtypes = [session_t, credentials_type_t,
715
 
                                ctypes.c_void_p]
 
737
    credentials_set.argtypes = [ClientSession, credentials_type_t,
 
738
                                CastToVoidPointer(Credentials)]
716
739
    credentials_set.restype = _error_code
717
740
 
718
741
    strerror = _library.gnutls_strerror
720
743
    strerror.restype = ctypes.c_char_p
721
744
 
722
745
    certificate_type_get = _library.gnutls_certificate_type_get
723
 
    certificate_type_get.argtypes = [session_t]
 
746
    certificate_type_get.argtypes = [ClientSession]
724
747
    certificate_type_get.restype = _error_code
725
748
 
726
749
    certificate_get_peers = _library.gnutls_certificate_get_peers
727
 
    certificate_get_peers.argtypes = [session_t,
 
750
    certificate_get_peers.argtypes = [ClientSession,
728
751
                                      ctypes.POINTER(ctypes.c_uint)]
729
752
    certificate_get_peers.restype = ctypes.POINTER(datum_t)
730
753
 
737
760
    global_set_log_function.restype = None
738
761
 
739
762
    deinit = _library.gnutls_deinit
740
 
    deinit.argtypes = [session_t]
 
763
    deinit.argtypes = [ClientSession]
741
764
    deinit.restype = None
742
765
 
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
747
770
 
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
751
774
 
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
756
779
 
757
780
    check_version = _library.gnutls_check_version
774
797
 
775
798
        x509_crt_fmt_t = ctypes.c_int
776
799
 
777
 
        # All the function declarations below are from gnutls/abstract.h
 
800
        # All the function declarations below are from
 
801
        # gnutls/abstract.h
778
802
        pubkey_init = _library.gnutls_pubkey_init
779
803
        pubkey_init.argtypes = [ctypes.POINTER(pubkey_t)]
780
804
        pubkey_init.restype = _error_code
794
818
        pubkey_deinit.argtypes = [pubkey_t]
795
819
        pubkey_deinit.restype = None
796
820
    else:
797
 
        # All the function declarations below are from gnutls/openpgp.h
 
821
        # All the function declarations below are from
 
822
        # gnutls/openpgp.h
798
823
 
799
824
        openpgp_crt_init = _library.gnutls_openpgp_crt_init
800
825
        openpgp_crt_init.argtypes = [ctypes.POINTER(openpgp_crt_t)]
806
831
                                       openpgp_crt_fmt_t]
807
832
        openpgp_crt_import.restype = _error_code
808
833
 
809
 
        openpgp_crt_verify_self = _library.gnutls_openpgp_crt_verify_self
810
 
        openpgp_crt_verify_self.argtypes = [openpgp_crt_t, ctypes.c_uint,
811
 
                                            ctypes.POINTER(ctypes.c_uint)]
 
834
        openpgp_crt_verify_self = \
 
835
            _library.gnutls_openpgp_crt_verify_self
 
836
        openpgp_crt_verify_self.argtypes = [
 
837
            openpgp_crt_t,
 
838
            ctypes.c_uint,
 
839
            ctypes.POINTER(ctypes.c_uint),
 
840
        ]
812
841
        openpgp_crt_verify_self.restype = _error_code
813
842
 
814
843
        openpgp_crt_deinit = _library.gnutls_openpgp_crt_deinit
825
854
 
826
855
    if check_version(b"3.6.4"):
827
856
        certificate_type_get2 = _library.gnutls_certificate_type_get2
828
 
        certificate_type_get2.argtypes = [session_t, ctypes.c_int]
 
857
        certificate_type_get2.argtypes = [ClientSession, ctypes.c_int]
829
858
        certificate_type_get2.restype = _error_code
830
859
 
831
860
    # Remove non-public functions
2291
2320
            priority = self.server.gnutls_priority
2292
2321
            if priority is None:
2293
2322
                priority = "NORMAL"
2294
 
            gnutls.priority_set_direct(session._c_object,
2295
 
                                       priority.encode("utf-8"),
2296
 
                                       None)
 
2323
            gnutls.priority_set_direct(session,
 
2324
                                       priority.encode("utf-8"), None)
2297
2325
 
2298
2326
            # Start communication using the Mandos protocol
2299
2327
            # Get protocol number
2326
2354
                    except (TypeError, gnutls.Error) as error:
2327
2355
                        logger.warning("Bad certificate: %s", error)
2328
2356
                        return
2329
 
                    logger.debug("Key ID: %s", key_id)
 
2357
                    logger.debug("Key ID: %s",
 
2358
                                 key_id.decode("utf-8",
 
2359
                                               errors="replace"))
2330
2360
 
2331
2361
                else:
2332
2362
                    key_id = b""
2424
2454
    def peer_certificate(session):
2425
2455
        "Return the peer's certificate as a bytestring"
2426
2456
        try:
2427
 
            cert_type = gnutls.certificate_type_get2(session._c_object,
2428
 
                                                     gnutls.CTYPE_PEERS)
 
2457
            cert_type = gnutls.certificate_type_get2(
 
2458
                session, gnutls.CTYPE_PEERS)
2429
2459
        except AttributeError:
2430
 
            cert_type = gnutls.certificate_type_get(session._c_object)
 
2460
            cert_type = gnutls.certificate_type_get(session)
2431
2461
        if gnutls.has_rawpk:
2432
2462
            valid_cert_types = frozenset((gnutls.CRT_RAWPK,))
2433
2463
        else:
2440
2470
            return b""
2441
2471
        list_size = ctypes.c_uint(1)
2442
2472
        cert_list = (gnutls.certificate_get_peers
2443
 
                     (session._c_object, ctypes.byref(list_size)))
 
2473
                     (session, ctypes.byref(list_size)))
2444
2474
        if not bool(cert_list) and list_size.value != 0:
2445
2475
            raise gnutls.Error("error getting peer certificate")
2446
2476
        if list_size.value == 0:
2468
2498
        buf = ctypes.create_string_buffer(32)
2469
2499
        buf_len = ctypes.c_size_t(len(buf))
2470
2500
        # Get the key ID from the raw public key into the buffer
2471
 
        gnutls.pubkey_get_key_id(pubkey,
2472
 
                                 gnutls.KEYID_USE_SHA256,
2473
 
                                 ctypes.cast(ctypes.byref(buf),
2474
 
                                             ctypes.POINTER(ctypes.c_ubyte)),
2475
 
                                 ctypes.byref(buf_len))
 
2501
        gnutls.pubkey_get_key_id(
 
2502
            pubkey,
 
2503
            gnutls.KEYID_USE_SHA256,
 
2504
            ctypes.cast(ctypes.byref(buf),
 
2505
                        ctypes.POINTER(ctypes.c_ubyte)),
 
2506
            ctypes.byref(buf_len))
2476
2507
        # Deinit the certificate
2477
2508
        gnutls.pubkey_deinit(pubkey)
2478
2509
 
2723
2754
            address = request[3]
2724
2755
 
2725
2756
            for c in self.clients.values():
2726
 
                if key_id == "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855":
 
2757
                if key_id == ("E3B0C44298FC1C149AFBF4C8996FB924"
 
2758
                              "27AE41E4649B934CA495991B7852B855"):
2727
2759
                    continue
2728
2760
                if key_id and c.key_id == key_id:
2729
2761
                    client = c
2781
2813
def rfc3339_duration_to_delta(duration):
2782
2814
    """Parse an RFC 3339 "duration" and return a datetime.timedelta
2783
2815
 
2784
 
    >>> rfc3339_duration_to_delta("P7D") == datetime.timedelta(7)
2785
 
    True
2786
 
    >>> rfc3339_duration_to_delta("PT60S") == datetime.timedelta(0, 60)
2787
 
    True
2788
 
    >>> rfc3339_duration_to_delta("PT60M") == datetime.timedelta(0, 3600)
2789
 
    True
2790
 
    >>> rfc3339_duration_to_delta("PT24H") == datetime.timedelta(1)
2791
 
    True
2792
 
    >>> rfc3339_duration_to_delta("P1W") == datetime.timedelta(7)
2793
 
    True
2794
 
    >>> rfc3339_duration_to_delta("PT5M30S") == datetime.timedelta(0, 330)
2795
 
    True
2796
 
    >>> rfc3339_duration_to_delta("P1DT3M20S") == datetime.timedelta(1, 200)
2797
 
    True
 
2816
    >>> timedelta = datetime.timedelta
 
2817
    >>> rfc3339_duration_to_delta("P7D") == timedelta(7)
 
2818
    True
 
2819
    >>> rfc3339_duration_to_delta("PT60S") == timedelta(0, 60)
 
2820
    True
 
2821
    >>> rfc3339_duration_to_delta("PT60M") == timedelta(0, 3600)
 
2822
    True
 
2823
    >>> rfc3339_duration_to_delta("PT24H") == timedelta(1)
 
2824
    True
 
2825
    >>> rfc3339_duration_to_delta("P1W") == timedelta(7)
 
2826
    True
 
2827
    >>> rfc3339_duration_to_delta("PT5M30S") == timedelta(0, 330)
 
2828
    True
 
2829
    >>> rfc3339_duration_to_delta("P1DT3M20S") == timedelta(1, 200)
 
2830
    True
 
2831
    >>> del timedelta
2798
2832
    """
2799
2833
 
2800
2834
    # Parsing an RFC 3339 duration with regular expressions is not
3167
3201
 
3168
3202
        @gnutls.log_func
3169
3203
        def debug_gnutls(level, string):
3170
 
            logger.debug("GnuTLS: %s", string[:-1])
 
3204
            logger.debug("GnuTLS: %s",
 
3205
                         string[:-1].decode("utf-8",
 
3206
                                            errors="replace"))
3171
3207
 
3172
3208
        gnutls.global_set_log_function(debug_gnutls)
3173
3209