def stringToDN(string, tag=None): """Takes a string representing a distinguished name or directory name and returns a Name for use by pyasn1. See the documentation for the issuer and subject fields for more details. Takes an optional implicit tag in cases where the Name needs to be tagged differently.""" if '/' not in string: string = '/CN=%s' % string rdns = rfc2459.RDNSequence() pos = 0 pattern = '/(C|ST|L|O|OU|CN|emailAddress)=' split = re.split(pattern, string) # split should now be [[encoding], <type>, <value>, <type>, <value>, ...] if split[0]: encoding = split[0] else: encoding = 'utf8String' for (nameType, value) in zip(split[1::2], split[2::2]): ava = rfc2459.AttributeTypeAndValue() if nameType == 'C': ava.setComponentByName('type', rfc2459.id_at_countryName) nameComponent = rfc2459.X520countryName(value) elif nameType == 'ST': ava.setComponentByName('type', rfc2459.id_at_stateOrProvinceName) nameComponent = rfc2459.X520StateOrProvinceName() elif nameType == 'L': ava.setComponentByName('type', rfc2459.id_at_localityName) nameComponent = rfc2459.X520LocalityName() elif nameType == 'O': ava.setComponentByName('type', rfc2459.id_at_organizationName) nameComponent = rfc2459.X520OrganizationName() elif nameType == 'OU': ava.setComponentByName('type', rfc2459.id_at_organizationalUnitName) nameComponent = rfc2459.X520OrganizationalUnitName() elif nameType == 'CN': ava.setComponentByName('type', rfc2459.id_at_commonName) nameComponent = rfc2459.X520CommonName() elif nameType == 'emailAddress': ava.setComponentByName('type', rfc2459.emailAddress) nameComponent = rfc2459.Pkcs9email(value) else: raise UnknownDNTypeError(nameType) if not nameType == 'C' and not nameType == 'emailAddress': # The value may have things like '\0' (i.e. a slash followed by # the number zero) that have to be decoded into the resulting # '\x00' (i.e. a byte with value zero). nameComponent.setComponentByName( encoding, value.decode(encoding='string_escape')) ava.setComponentByName('value', nameComponent) rdn = rfc2459.RelativeDistinguishedName() rdn.setComponentByPosition(0, ava) rdns.setComponentByPosition(pos, rdn) pos = pos + 1 if tag: name = rfc2459.Name().subtype(implicitTag=tag) else: name = rfc2459.Name() name.setComponentByPosition(0, rdns) return name
def stringToDN(string, tag=None): """Takes a string representing a distinguished name or directory name and returns a Name for use by pyasn1. See the documentation for the issuer and subject fields for more details. Takes an optional implicit tag in cases where the Name needs to be tagged differently.""" if string and "/" not in string: string = "/CN=%s" % string rdns = rfc2459.RDNSequence() pattern = "/(C|ST|L|O|OU|CN|emailAddress)=" split = re.split(pattern, string) # split should now be [[encoding], <type>, <value>, <type>, <value>, ...] if split[0]: encoding = split[0] else: encoding = "utf8String" for pos, (nameType, value) in enumerate(zip(split[1::2], split[2::2])): ava = rfc2459.AttributeTypeAndValue() if nameType == "C": ava["type"] = rfc2459.id_at_countryName nameComponent = rfc2459.X520countryName(value) elif nameType == "ST": ava["type"] = rfc2459.id_at_stateOrProvinceName nameComponent = rfc2459.X520StateOrProvinceName() elif nameType == "L": ava["type"] = rfc2459.id_at_localityName nameComponent = rfc2459.X520LocalityName() elif nameType == "O": ava["type"] = rfc2459.id_at_organizationName nameComponent = rfc2459.X520OrganizationName() elif nameType == "OU": ava["type"] = rfc2459.id_at_organizationalUnitName nameComponent = rfc2459.X520OrganizationalUnitName() elif nameType == "CN": ava["type"] = rfc2459.id_at_commonName nameComponent = rfc2459.X520CommonName() elif nameType == "emailAddress": ava["type"] = rfc2459.emailAddress nameComponent = rfc2459.Pkcs9email(value) else: raise UnknownDNTypeError(nameType) if not nameType == "C" and not nameType == "emailAddress": # The value may have things like '\0' (i.e. a slash followed by # the number zero) that have to be decoded into the resulting # '\x00' (i.e. a byte with value zero). nameComponent[encoding] = six.ensure_binary(value).decode( encoding="unicode_escape" ) ava["value"] = nameComponent rdn = rfc2459.RelativeDistinguishedName() rdn.setComponentByPosition(0, ava) rdns.setComponentByPosition(pos, rdn) if tag: name = rfc2459.Name().subtype(implicitTag=tag) else: name = rfc2459.Name() name.setComponentByPosition(0, rdns) return name
def DNToString(dn): rdns = dn.getComponent() ret = '' for rdn in rdns: ret += '[' for attr in rdn: attrType = attr.getComponentByName('type') if attrType == rfc2459.emailAddress: val, rest = decoder.decode(attr.getComponentByName('value'), asn1Spec=rfc2459.Pkcs9email()) assert rest == "" # Strictly speaking, this is IA5, not ASCII. val = str(val).decode('ascii') else: val, rest = decoder.decode(attr.getComponentByName('value'), asn1Spec=rfc2459.X520name()) assert rest == "" valt = val.getName() val = val.getComponent() if valt == 'printableString': val = str(val) elif valt == 'teletexString': # Strictly this is a T.61 string. T.61 no longer exists as a # standard and some certs mark ISO 8859-1 as # teletexString. And we should never see this, but we do. val = str(val).decode('iso8859-1') elif valt == 'utf8String': val = str(val) else: print valt assert False assert val is not None ret += '/' + str(attrType) + '=' + val ret += ']' return ret