Add a check to verify the name on the cert
[public/dnssec-swede-utility.git] / swede
diff --git a/swede b/swede
index d2f5d0e2744fd11586276c60c98af4c2562635d5..63137a41dfdefe3b56a18cdff063a6d9401d38d7 100755 (executable)
--- a/swede
+++ b/swede
@@ -17,6 +17,7 @@
 
 import sys
 import os
+import socket
 import unbound
 import re
 from M2Crypto import X509, SSL
@@ -25,7 +26,7 @@ from hashlib import sha256, sha512
 from ipaddr import IPv4Address, IPv6Address
 
 
-def genTLSA(hostname, protocol, port, certificate, output='draft', usage=1, selector=0, mtype=1):
+def genTLSA(hostname, protocol, port, certificate, output='generic', usage=1, selector=0, mtype=1):
        """This function generates a TLSARecord object using the data passed in the parameters,
        it then validates the record and returns the RR as a string.
        """
@@ -53,8 +54,8 @@ def genTLSA(hostname, protocol, port, certificate, output='draft', usage=1, sele
 
        record.isValid(raiseException=True)
 
-       if output == 'draft':
-               return record.getRecord(draft=True)
+       if output == 'generic':
+               return record.getRecord(generic=True)
        return record.getRecord()
 
 def getA(hostname, secure=True):
@@ -170,7 +171,7 @@ def getHash(certificate, mtype):
 def getTLSA(hostname, port=443, protocol='tcp', secure=True):
        """
        This function tries to do a secure lookup of the TLSA record.
-       At the moment it requests the TYPE65468 record and parses it into a 'valid' TLSA record
+       At the moment it requests the TYPE52 record and parses it into a 'valid' TLSA record
        It returns a list of TLSARecord objects
        """
        if hostname[-1] != '.':
@@ -180,9 +181,9 @@ def getTLSA(hostname, port=443, protocol='tcp', secure=True):
                raise Exception('Error: unknown protocol: %s. Should be one of tcp, udp or sctp' % protocol)
        try:
                if port == '*':
-                       records = getRecords('*._%s.%s' % (protocol.lower(), hostname), rrtype=65468, secure=secure)
+                       records = getRecords('*._%s.%s' % (protocol.lower(), hostname), rrtype=52, secure=secure)
                else:
-                       records = getRecords('_%s._%s.%s' % (port, protocol.lower(), hostname), rrtype=65468, secure=secure)
+                       records = getRecords('_%s._%s.%s' % (port, protocol.lower(), hostname), rrtype=52, secure=secure)
        except InsecureLookupException, e:
                print str(e)
                sys.exit(1)
@@ -233,6 +234,28 @@ def verifyCertMatch(record, cert):
        else:
                return False
 
+def verifyCertNameWithHostName(cert, hostname, with_msg=False):
+       """Verify the name on the certificate with a hostname, we need this because we get the cert based on IP address and thusly cannot rely on M2Crypto to verify this"""
+       if not isinstance(cert, X509.X509):
+               return
+       if not isinstance(hostname, str):
+               return
+
+       if hostname[-1] == '.':
+               hostname = hostname[0:-1]
+
+       # Ugly string comparison to see if the name on the ee-cert matches with the name provided on the commandline
+       try:
+               altnames_on_cert = cert.get_ext('subjectAltName').get_value()
+       except:
+               altnames_on_cert = ''
+       if hostname in (str(cert.get_subject()) + altnames_on_cert):
+               return True
+       else:
+               if with_msg:
+                       print 'WARNING: Name on the certificate (Subject: %s, SubjectAltName: %s) doesn\'t match requested hostname (%s).' % (str(cert.get_subject()), altnames_on_cert, hostname)
+               return False
+
 class TLSARecord:
        """When instanciated, this class contains all the fields of a TLSA record.
        """
@@ -242,7 +265,7 @@ class TLSARecord:
                cert should be a hexidecimal string representing the certificate to be matched field
                """
                try:
-                       self.rrtype = 65468 # TLSA provisional
+                       self.rrtype = 52    # TLSA per https://www.iana.org/assignments/dns-parameters
                        self.rrclass = 1    # IN
                        self.name = str(name)
                        self.usage = int(usage)
@@ -252,10 +275,10 @@ class TLSARecord:
                except:
                        raise Exception('Invalid value passed, unable to create a TLSARecord')
 
-       def getRecord(self, draft=False):
-               """Returns the RR string of this TLSARecord, either in rfc (default) or draft format"""
-               if draft:
-                       return '%s IN TYPE65468 \# %s %s%s%s%s' % (self.name, (len(self.cert)/2)+3 , self._toHex(self.usage), self._toHex(self.selector), self._toHex(self.mtype), self.cert)
+       def getRecord(self, generic=False):
+               """Returns the RR string of this TLSARecord, either in rfc (default) or generic format"""
+               if generic:
+                       return '%s IN TYPE52 \# %s %s%s%s%s' % (self.name, (len(self.cert)/2)+3 , self._toHex(self.usage), self._toHex(self.selector), self._toHex(self.mtype), self.cert)
                return '%s IN TLSA %s %s %s %s' % (self.name, self.usage, self.selector, self.mtype, self.cert)
 
        def _toHex(self, val):
@@ -332,6 +355,7 @@ class AAAARecord:
        """An object representing an AAAA Record (IPv6 address)"""
        def __init__(self, hostname, address):
                self.rrtype = 28
+               self.hostname = hostname
                self.address = address
 
        def __str__(self):
@@ -357,7 +381,7 @@ class DNSLookupError(Exception):
 if __name__ == '__main__':
        import argparse
        # create the parser
-       parser = argparse.ArgumentParser(description='Create and verify DANE records.', epilog='This tool has a few limitations: it only IPv4 for SSL connections.')
+       parser = argparse.ArgumentParser(description='Create and verify DANE records.', epilog='This tool has a few limitations')
 
        subparsers = parser.add_subparsers(title='Functions', help='Available functions, see %(prog)s function -h for function-specific help')
        parser_verify = subparsers.add_parser('verify', help='Verify a TLSA record, exit 0 when all TLSA records are matched, exit 2 when a record does not match the received certificate, exit 1 on error.', epilog='Caveat: For TLSA validation, this program chases through the certificate chain offered by the server, not its local certificates.')
@@ -381,7 +405,7 @@ if __name__ == '__main__':
        parser_create.add_argument('--port', '-p', action='store', type=int, default=443, help='The port where running TLS is located (default: %(default)s).')
        parser_create.add_argument('--protocol', action='store', choices=['tcp','udp','sctp'], default='tcp', help='The protocol the TLS service is using (default: %(default)s).')
        parser_create.add_argument('--certificate', '-c', help='The certificate used for the host. If certificate is empty, the certificate will be downloaded from the server')
-       parser_create.add_argument('--output', '-o', action='store', default='draft', choices=['draft','rfc','both'], help='The type of output. Draft (private RRtype, 65468), RFC (TLSA) or both (default: %(default)s).')
+       parser_create.add_argument('--output', '-o', action='store', default='generic', choices=['generic','rfc','both'], help='The type of output. Generic (RFC 3597, TYPE52), RFC (TLSA) or both (default: %(default)s).')
 
        # Usage of the certificate
        parser_create.add_argument('--usage', '-u', action='store', type=int, default=1, choices=[0,1,2,3], help='The Usage of the Certificate for Association. \'0\' for CA, \'1\' for End Entity, \'2\' for trust-anchor, \'3\' for ONLY End-Entity match (default: %(default)s).')
@@ -441,7 +465,7 @@ if __name__ == '__main__':
 
                        if not args.quiet:
                                print 'Attempting to verify the record with the TLS service...'
-                       addresses = getA(args.host, secure=secure)
+                       addresses = getA(args.host, secure=secure) + getAAAA(args.host, secure=secure)
                        for address in addresses:
                                if not args.quiet:
                                        print 'Got the following IP: %s' % str(address)
@@ -456,16 +480,28 @@ if __name__ == '__main__':
                                        sys.exit(1)
                                # Don't error when the verification fails in the SSL handshake
                                ctx.set_verify(SSL.verify_none, depth=9)
-                               connection = SSL.Connection(ctx)
+                               if isinstance(address, AAAARecord):
+                                       sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
+                                       sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+                               else:
+                                       sock = None
+                               connection = SSL.Connection(ctx, sock=sock)
                                try:
                                        connection.connect((str(address), int(args.port)))
                                except SSL.Checker.WrongHost, e:
                                        # The name on the remote cert doesn't match the hostname because we connect on IP, not hostname (as we want secure lookup)
                                        pass
+                               except socket.error, e:
+                                       print 'Cannot connect to %s: %s' % (address, str(e))
+                                       continue
                                chain = connection.get_peer_cert_chain()
                                verify_result = connection.get_verify_result()
 
                                # Good, now let's verify
+                               if not verifyCertNameWithHostName(cert=chain[0], hostname=str(args.host), with_msg=True):
+                                       # The name on the cert doesn't match the hostname... we don't verify the TLSA record
+                                       print 'Not checking the TLSA record.'
+                                       continue
                                if record.usage == 1: # End-host cert
                                        cert = chain[0]
                                        if verifyCertMatch(record, cert):
@@ -563,9 +599,9 @@ if __name__ == '__main__':
                                                        input_ok = True
                                        except:
                                                sys.stdout.write('Port %s not numerical or within correct range (1 <= port <= 65535), try again (hit enter for default 443): ' % user_input)
-                       # Get the A records for the host
+                       # Get the address records for the host
                        try:
-                               addresses = getA(args.host, secure=secure)
+                               addresses = getA(args.host, secure=secure) + getAAAA(args.host, secure=secure)
                        except InsecureLookupException, e:
                                print >> sys.stderr, str(e)
                                sys.exit(1)
@@ -575,11 +611,19 @@ if __name__ == '__main__':
                                # We do the certificate handling here, as M2Crypto keeps segfaulting when try to do stuff with the cert if we don't
                                ctx = SSL.Context()
                                ctx.set_verify(SSL.verify_none, depth=9)
-                               connection = SSL.Connection(ctx)
+                               if isinstance(address, AAAARecord):
+                                       sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
+                                       sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+                               else:
+                                       sock = None
+                               connection = SSL.Connection(ctx, sock=sock)
                                try:
                                        connection.connect((str(address), int(connection_port)))
                                except SSL.Checker.WrongHost:
                                        pass
+                               except socket.error, e:
+                                       print 'Cannot connect to %s: %s' % (address, str(e))
+                                       continue
 
                                chain = connection.get_peer_cert_chain()
                                for chaincert in chain: