def serialize_certificates(g):
    for (common_name, path, buf, cert, signed, expires) in g():
        try:
            tags = []
            for tag in getxattr(path, 'user.xdg.tags').decode('utf-8').split(','):
                if ('=' in tag):
                    (k, v) = tag.split('=', 1)
                else:
                    (k, v) = ('other', tag)
                tags.append(dict(id=tag, key=k, value=v))
        except IOError:
            tags = None
        attributes = {}
        for key in listxattr(path):
            if key.startswith(b'user.machine.'):
                attributes[key[13:].decode('ascii')] = getxattr(path, key).decode('ascii')
        try:
            last_seen = datetime.strptime(getxattr(path, 'user.lease.last_seen').decode('ascii'), '%Y-%m-%dT%H:%M:%S.%fZ')
            lease = dict(inner_address=getxattr(path, 'user.lease.inner_address').decode('ascii'), outer_address=getxattr(path, 'user.lease.outer_address').decode('ascii'), last_seen=last_seen, age=(datetime.utcnow() - last_seen))
        except IOError:
            lease = None
        try:
            signer_username = getxattr(path, 'user.signature.username').decode('ascii')
        except IOError:
            signer_username = None
        serialized = dict(serial=('%x' % cert.serial_number), organizational_unit=cert.subject.native.get('organizational_unit_name'), common_name=common_name, signed=signed, expires=expires, sha256sum=hashlib.sha256(buf).hexdigest(), signer=signer_username, lease=lease, tags=tags, attributes=(attributes or None), responder_url=None)
        for e in cert['tbs_certificate']['extensions'].native:
            if (e['extn_id'] == 'key_usage'):
                serialized['key_usage'] = e['extn_value']
            elif (e['extn_id'] == 'extended_key_usage'):
                serialized['extended_key_usage'] = e['extn_value']
            elif (e['extn_id'] == 'basic_constraints'):
                serialized['basic_constraints'] = e['extn_value']
            elif (e['extn_id'] == 'crl_distribution_points'):
                for c in e['extn_value']:
                    serialized['revoked_url'] = c['distribution_point']
                    break
                serialized['extended_key_usage'] = e['extn_value']
            elif (e['extn_id'] == 'authority_information_access'):
                for a in e['extn_value']:
                    if (a['access_method'] == 'ocsp'):
                        serialized['responder_url'] = a['access_location']
                    else:
                        raise NotImplementedError(("Don't know how to handle AIA access method %s" % a['access_method']))
            elif (e['extn_id'] == 'authority_key_identifier'):
                pass
            elif (e['extn_id'] == 'key_identifier'):
                pass
            elif (e['extn_id'] == 'subject_alt_name'):
                (serialized['subject_alt_name'],) = e['extn_value']
            else:
                raise NotImplementedError(("Don't know how to handle extension %s" % e['extn_id']))
        (yield serialized)
def serialize_certificates(g):
    for (common_name, path, buf, cert, signed, expires) in g():
        try:
            tags = []
            for tag in getxattr(path, 'user.xdg.tags').decode('utf-8').split(','):
                if ('=' in tag):
                    (k, v) = tag.split('=', 1)
                else:
                    (k, v) = ('other', tag)
                tags.append(dict(id=tag, key=k, value=v))
        except IOError:
            tags = None
        attributes = {}
        for key in listxattr(path):
            if key.startswith(b'user.machine.'):
                attributes[key[13:].decode('ascii')] = getxattr(path, key).decode('ascii')
        try:
            last_seen = datetime.strptime(getxattr(path, 'user.lease.last_seen').decode('ascii'), '%Y-%m-%dT%H:%M:%S.%fZ')
            lease = dict(inner_address=getxattr(path, 'user.lease.inner_address').decode('ascii'), outer_address=getxattr(path, 'user.lease.outer_address').decode('ascii'), last_seen=last_seen, age=(datetime.utcnow() - last_seen))
        except IOError:
            lease = None
        try:
            signer_username = getxattr(path, 'user.signature.username').decode('ascii')
        except IOError:
            signer_username = None
        serialized = dict(serial=('%x' % cert.serial_number), organizational_unit=cert.subject.native.get('organizational_unit_name'), common_name=common_name, signed=signed, expires=expires, sha256sum=hashlib.sha256(buf).hexdigest(), signer=signer_username, lease=lease, tags=tags, attributes=(attributes or None), responder_url=None)
        for e in cert['tbs_certificate']['extensions'].native:
            if (e['extn_id'] == 'key_usage'):
                serialized['key_usage'] = e['extn_value']
            elif (e['extn_id'] == 'extended_key_usage'):
                serialized['extended_key_usage'] = e['extn_value']
            elif (e['extn_id'] == 'basic_constraints'):
                serialized['basic_constraints'] = e['extn_value']
            elif (e['extn_id'] == 'crl_distribution_points'):
                for c in e['extn_value']:
                    serialized['revoked_url'] = c['distribution_point']
                    break
                serialized['extended_key_usage'] = e['extn_value']
            elif (e['extn_id'] == 'authority_information_access'):
                for a in e['extn_value']:
                    if (a['access_method'] == 'ocsp'):
                        serialized['responder_url'] = a['access_location']
                    else:
                        raise NotImplementedError(("Don't know how to handle AIA access method %s" % a['access_method']))
            elif (e['extn_id'] == 'authority_key_identifier'):
                pass
            elif (e['extn_id'] == 'key_identifier'):
                pass
            elif (e['extn_id'] == 'subject_alt_name'):
                (serialized['subject_alt_name'],) = e['extn_value']
            else:
                raise NotImplementedError(("Don't know how to handle extension %s" % e['extn_id']))
        (yield serialized)