Exemplo n.º 1
0
Arquivo: x509.py Projeto: museack/tate
    def extract_names(self):
        results = {
            'CN': None,
            'DNS': set(),
            'SRV': set(),
            'URI': set(),
            'XMPPAddr': set(),
            'OU': None,
        }

        # Extract the CommonName(s) from the cert.
        for rdnss in self.subject:
            for rdns in rdnss:
                for name in rdns:
                    oid = name.getComponentByName('type')
                    value = name.getComponentByName('value')

                    if oid == COMMON_NAME:
                        value = decoder.decode(value,
                                               asn1Spec=DirectoryString())[0]
                        value = decode_str(value.getComponent())
                        results['CN'] = value

                    elif oid == OU_NAME:
                        value = decoder.decode(value,
                                               asn1Spec=DirectoryString())[0]
                        value = decode_str(value.getComponent())
                        results['OU'] = value

        # Extract the Subject Alternate Names (DNS, SRV, URI, XMPPAddr)
        for extension in self.extensions:
            oid = extension.getComponentByName('extnID')
            if oid != SUBJECT_ALT_NAME:
                continue

            value = decoder.decode(extension.getComponentByName('extnValue'),
                                   asn1Spec=OctetString())[0]
            sa_names = decoder.decode(value, asn1Spec=SubjectAltName())[0]
            for name in sa_names:
                name_type = name.getName()
                if name_type == 'dNSName':
                    results['DNS'].add(decode_str(name.getComponent()))
                if name_type == 'uniformResourceIdentifier':
                    value = decode_str(name.getComponent())
                    if value.startswith('xmpp:'):
                        results['URI'].add(value[5:])
                elif name_type == 'otherName':
                    name = name.getComponent()

                    oid = name.getComponentByName('type-id')
                    value = name.getComponentByName('value')

                    if oid == XMPP_ADDR:
                        value = decoder.decode(value, asn1Spec=UTF8String())[0]
                        results['XMPPAddr'].add(decode_str(value))
                    elif oid == SRV_NAME:
                        value = decoder.decode(value, asn1Spec=IA5String())[0]
                        results['SRV'].add(decode_str(value))
        return results
Exemplo n.º 2
0
 def _decode_subject(self, subject):
     """Load data from a ASN.1 subject.
     """
     self.common_names = []
     subject_name = []
     for rdnss in subject:
         for rdns in rdnss:
             rdnss_list = []
             for nameval in rdns:
                 val_type = nameval.getComponentByName('type')
                 value = nameval.getComponentByName('value')
                 if val_type not in DN_OIDS:
                     logger.debug("OID {0} not supported".format(val_type))
                     continue
                 val_type = DN_OIDS[val_type]
                 value = der_decoder.decode(value,
                                            asn1Spec=DirectoryString())[0]
                 value = value.getComponent()
                 try:
                     value = _decode_asn1_string(value)
                 except UnicodeError:
                     logger.debug(
                         "Cannot decode value: {0!r}".format(value))
                     continue
                 if val_type == u"commonName":
                     self.common_names.append(value)
                 rdnss_list.append((val_type, value))
             subject_name.append(tuple(rdnss_list))
     self.subject_name = tuple(subject_name)