def _escape_dn_value(val):
    """Escape special characters in RFC4514 Distinguished Name value."""

    # See https://tools.ietf.org/html/rfc4514#section-2.4
    val = val.replace('\\', '\\\\')
    val = val.replace('"', '\\"')
    val = val.replace('+', '\\+')
    val = val.replace(',', '\\,')
    val = val.replace(';', '\\;')
    val = val.replace('<', '\\<')
    val = val.replace('>', '\\>')
    val = val.replace('\0', '\\00')

    if val[0] in ('#', ' '):
        val = '\\' + val
    if val[-1] == ' ':
        val = val[:-1] + '\\ '

    return val
Escape special characters in RFC4514 Distinguished Name value.
stackoverflow/venv/lib/python3.6/site-packages/cryptography/x509/name.py