You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python LDAP3模块获取LDAP中ASCII格式的CRL证书

解决LDAP3获取CRL转ASCII格式的问题

你遇到的问题本质是:ldapsearch会自动将LDAP中存储的二进制DER格式CRL做Base64编码,输出成可直接使用的ASCII格式;而ldap3返回的是原始二进制数据(也就是你看到的带\x的字节串),需要手动转成Base64编码并加上PEM格式头尾。

处理步骤:

  • 从返回的条目里提取certificateRevocationList的二进制数据
  • 使用Python的base64模块将二进制数据编码为Base64字符串
  • 添加上X509 CRL的PEM格式首尾标记

修改后的代码:

#!/bin/python3
from ldap3 import Server, Connection, SAFE_SYNC, ALL
import base64

server = Server('ldaps://<HOST>', port=636, use_ssl=True, get_info=ALL)
base_dn = "c=us"

conn = Connection(server, client_strategy=SAFE_SYNC, auto_bind=True, auto_encode=False)
result, results_response, _, _ = conn.search(
    search_base=base_dn,
    search_filter='(ou=rootCA)',
    attributes='certificateRevocationList'
)

# 提取CRL二进制数据并转换为PEM格式
if results_response:
    crl_der = results_response[0]['attributes']['certificateRevocationList']
    crl_b64 = base64.b64encode(crl_der).decode('ascii')
    crl_pem = f"-----BEGIN X509 CRL-----\n{crl_b64}\n-----END X509 CRL-----"
    print(crl_pem)
else:
    print("未找到匹配的CRL条目")

说明:

  • SAFE_SYNC模式下,conn.search()返回的是一个四元组:(结果状态, 条目列表, 响应消息, 响应ID),需要解构拿到条目列表results_response
  • certificateRevocationList属性返回的是bytes类型的DER格式CRL,用base64.b64encode()转成Base64字节串,再用decode('ascii')转成ASCII字符串
  • 添加上PEM头尾后,格式就和ldapsearch输出的完全一致了

内容的提问来源于stack exchange,提问作者Barry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 19:30:00