示例#1
0
def get_host_names(certificate):
    """
    Extract the host names from the Pem encoded X509 certificate

    :param certificate: A PEM encoded certificate
    :returns: A dictionary containing the following keys:
    ['cn', 'dns_names']
    where 'cn' is the CN from the SubjectName of the certificate, and
    'dns_names' is a list of dNSNames (possibly empty) from
    the SubjectAltNames of the certificate.
    """

    x509 = _get_x509_from_pem_bytes(certificate)
    hostNames = {}
    if hasattr(x509.get_subject(), 'CN'):
        hostNames['cn'] = x509.get_subject().CN
    hostNames['dns_names'] = []
    num_exts = x509.get_extension_count()
    for i in range(0, num_exts):
        ext = x509.get_extension(i)
        short_name = ext.get_short_name()
        if short_name == six.b('subjectAltName'):
            data = ext.get_data()
            general_names_container = decoder.decode(
                data, asn1Spec=rfc2459.GeneralNames())
            for general_names in general_names_container[0]:
                currName = general_names.getName()
                if currName == 'dNSName':
                    octets = general_names.getComponent().asOctets()
                    decoded = octets.decode("utf-8")
                    hostNames['dns_names'].append(decoded)
    return hostNames
示例#2
0
    def addAuthorityKeyId(self, akiTypes, critical):
        types = [st.strip() for st in akiTypes.split(',')]

        noneSpecified = 0 == len(akiTypes.strip())

        if critical:
            raise UnknownAuthorityKeyIdError(critical)
        hasher = hashlib.sha1()
        hasher.update(self.issuerKey.toDER())
        akiKi = rfc2459.KeyIdentifier().subtype(implicitTag=tag.Tag(
            tag.tagClassContext, tag.tagFormatSimple, 0),
                                                value=hasher.digest())
        aki = rfc2459.AuthorityKeyIdentifier()

        # If the issuerSerialNumber is set, we can add AKI data for Issuer principal and the issuer serial number
        if None != self.issuerSerialNumber:
            issuerName = rfc2459.GeneralNames().subtype(implicitTag=tag.Tag(
                tag.tagClassContext, tag.tagFormatSimple, 1))
            generalName = stringToDN(
                self.issuer,
                tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
            issuerName.setComponentByPosition(0, generalName)
            csn = rfc2459.CertificateSerialNumber().subtype(
                implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple,
                                    2),
                value=decoder.decode(self.issuerSerialNumber)[0])
            if noneSpecified or 'ki' in types:
                aki.setComponentByPosition(0, akiKi)
            if noneSpecified or 'issuer' in types:
                aki.setComponentByPosition(1, issuerName)
            if noneSpecified or 'serialNumber' in types:
                aki.setComponentByPosition(2, csn)
        else:
            if noneSpecified or 'ki' in types:
                aki.setComponentByPosition(0, akiKi)
        self.addExtension(rfc2459.id_ce_authorityKeyIdentifier, aki, critical)
示例#3
0
class _TagList(univ.Sequence):
    """TagList as per GOF 182"""

    componentType = namedtype.NamedTypes(
        namedtype.NamedType("policyAuthority", rfc2459.GeneralNames()),
        namedtype.NamedType("tags", _VOMSTags()))