在go中解析证书字符串
Parsing a certificate string in go
我正在使用 ssldump 提取通信中的证书。当我解析结果时,我在 go 中获得了一个字符串,定义为:
var string certStr
certStr = "30 82 06 9f...."
如何将其解析为 X509 证书?
已更新
我试过直接解析:
certSlc := []byte(certStr)
cert,err := x509.ParseCertificates(certSlc)
但结果是:
Error:asn1: structure error: tags don't match (16 vs {class:0 tag:19 length:48 isCompound:true}) {optional:false explicit:false application:false defaultValue:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false}
我应该做另一种转换吗?也许是字符串不完整或证书类型错误?
我发现了错误。问题出在源头上。
正如我所解释的,我的证书字符串是“30 82 06 09 ...”。此源必须解码为:
hex.DecodeString(certStr)
问题是十六进制解码不适用于此格式。我得到的错误是:
encoding/hex: invalid byte: U+0020 ' '
因此,删除原始字符串中的空格和回车 returns 是使其正常工作的解决方案。
在字节切片中解码后,可以毫无问题地创建 X509 证书。
我正在使用 ssldump 提取通信中的证书。当我解析结果时,我在 go 中获得了一个字符串,定义为:
var string certStr
certStr = "30 82 06 9f...."
如何将其解析为 X509 证书?
已更新
我试过直接解析:
certSlc := []byte(certStr)
cert,err := x509.ParseCertificates(certSlc)
但结果是:
Error:asn1: structure error: tags don't match (16 vs {class:0 tag:19 length:48 isCompound:true}) {optional:false explicit:false application:false defaultValue:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false}
我应该做另一种转换吗?也许是字符串不完整或证书类型错误?
我发现了错误。问题出在源头上。 正如我所解释的,我的证书字符串是“30 82 06 09 ...”。此源必须解码为:
hex.DecodeString(certStr)
问题是十六进制解码不适用于此格式。我得到的错误是:
encoding/hex: invalid byte: U+0020 ' '
因此,删除原始字符串中的空格和回车 returns 是使其正常工作的解决方案。
在字节切片中解码后,可以毫无问题地创建 X509 证书。