【问题标题】:Powershell: How to grab base64 value from attribute of SearchResultEntry object?Powershell:如何从 SearchResultEntry 对象的属性中获取 base64 值?
【发布时间】:2020-07-27 02:28:25
【问题描述】:

我的目标是从 LDAP 连接中获取 CA 的已发布 CRL。 我有一个搜索 LDAP(不是 Active Directory!)的功能,它按预期返回 System.DirectoryServices.Protocols.SearchResultEntryCollection

$results = LDAPsearch "$_LDAP_server`:$_LDAP_searchPort" "cn=$CA,$_LDAP_searchBase" '(&(certificateRevocationList=*))'
ForEach ($element in $results){
    $element.Attributes['cn'].GetValues('string')
    $element.Attributes['certificateRevocationList;binary'].GetValues('string')
}

上面正确读取了返回的每个元素的cn 属性值,但是certificateRevocationList 以一种奇怪的格式返回,它与我期望的 Base64 字符串完全不对应(例如,如果您将数据导出到 LDIF 文件,或者如果您使用 Linux ldapsearch 命令)...

我怎样才能得到实际的 Base64 值?

不幸的是,您只能将 'byte[]' 或 'string' 作为参数传递给 GetValues 方法(此处的 'Base64String' 选项对我很有用,但是...)。

下面的当前输出(其中cn 值正确写入但certificateRevocationList 不正确):

【问题讨论】:

    标签: powershell ldap base64 pki tobase64string


    【解决方案1】:

    将原始 CRL 检索为 byte[],然后自己转换为 base64:

    $crlBin = $element.Attributes['certificateRevocationList;binary'].GetValues('byte[]')
    $crlB64 = [Convert]::ToBase64String($crlBin)
    

    【讨论】:

    • 谢谢。我只需要管道| %{$_},因为GetValues('byte[]') 返回一个数组(我只需要一个值)......或者只需选择第一个对象:GetValues('byte[]')[0]。这样就可以了。
    【解决方案2】:

    只需添加我最终使用的选项,因为我的最终目标实际上是将 CRL 保存为文件(并能够解析它),所以要容易得多:

    $crlBin = $element.Attributes['certificateRevocationList;binary'].GetValues('byte[]')[0]
    [IO.File]::WriteAllBytes("$_localDir\CRL_$CA.crl",$crlBin)
    

    这会以 DER 格式写入实际的 CRL 文件(如果需要,可以使用 certutilopenssl 轻松切换到 PEM)。我最初的想法是从提取的 Base64 值构建一个 PEM 格式的 CRL 文件,但我看得太远了......

    我将 Mathias 的回答作为答案,因为他实际上最好地回答了我的问题;最后我没有指定我想要一个 CRL 文件。

    【讨论】:

      猜你喜欢
      • 2016-11-21
      • 1970-01-01
      • 2021-06-17
      • 2016-09-27
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 2022-09-29
      • 2012-08-26
      相关资源
      最近更新 更多