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

Golang解码BIMI证书ASN.1 Logotype扩展返回空结构问题

BIMI证书PEM文件解码SVG标识时得到空结构的问题

需要从BIMI证书的PEM文件中提取SVG标识,所有数据都包含在文件内,但解码后得到的始终是空结构。已咨询ChatGPT和Copilot,二者均判定代码逻辑正确,但问题仍未解决。


用于解码的类型定义(已精简部分结构)

type LogotypeExt struct {
    CommunityLogos []LogotypeInfo      `asn1:"tag:0,optional,explicit"`
    IssuerLogo     LogotypeInfo        `asn1:"tag:1,optional,explicit"`
    SubjectLogo    LogotypeInfo        `asn1:"tag:2,optional,explicit"`
    OtherLogos     []OtherLogotypeInfo `asn1:"tag:3,optional,explicit"`
}

type LogotypeInfo struct {
    Direct   LogotypeData      `asn1:"tag:0,optional,explicit"`
    Indirect LogotypeReference `asn1:"tag:1,optional,explicit"`
}

type LogotypeData struct {
    Image []LogotypeImage `asn1:"optional"`
    Audio []LogotypeAudio `asn1:"tag:1,optional"`
}

type LogotypeImage struct {
    ImageDetails LogotypeDetails
    ImageInfo    LogotypeImageInfo `asn1:"optional"`
}

type LogotypeDetails struct {
    MediaType    string
    LogotypeHash []HashAlgAndValue `asn1:"SEQUENCE OF"`
    LogotypeURI  []string          `asn1:"SEQUENCE OF"`
}

type LogotypeImageInfo struct {
    Type       int `asn1:"tag:0,default:1"`
    FileSize   int
    XSize      int
    YSize      int
    Resolution LogotypeImageResolution `asn1:"optional"`
    Language   string                  `asn1:"tag:4,omitempty,optional"`
}

type LogotypeImageResolution struct {
    NumBits   int `asn1:"tag:1"`
    TableSize int `asn1:"tag:2"`
}

type OtherLogotypeInfo struct {
    LogotypeType string
    Info         LogotypeInfo
}

type LogotypeReference struct {
    RefStructHash []HashAlgAndValue `asn1:"SEQUENCE OF"`
    RefStructURI  []string          `asn1:"SEQUENCE OF"`
}

证书解码与解析函数

func (c *Checker) CheckPemFile(file string) (data []byte, err error) {
    data, err = os.ReadFile(file)
    if err != nil {
        return nil, err
    }

    block, _ := pem.Decode(data)
    if block == nil {
        return nil, fmt.Errorf("failed to decode PEM block")
    }

    cert, err := x509.ParseCertificate(block.Bytes)
    if err != nil {
        return nil, fmt.Errorf("failed to parse certificate: %w", err)
    }

    svgData, found := extractSVGFromLogotype(cert.Extensions)
    if !found {
        return nil, fmt.Errorf("certificate does not have a Logotype extension with an SVG")
    }

    return svgData, nil
}

SVG提取函数

func extractSVGFromLogotype(exts []pkix.Extension) (string, bool) {
    for _, ext := range exts {
        if ext.Id.Equal([]int{1, 3, 6, 1, 5, 5, 7, 1, 12}) { // Logotype扩展OID

            fmt.Println("Encoded data:", hex.EncodeToString(ext.Value))

            var logotypeData LogotypeExt
            rest, err := asn1.Unmarshal(ext.Value, &logotypeData)
            if err != nil {
                fmt.Printf("error decoding Logotype extension: %v\n", err)
                return "", false
            }
            if len(rest) > 0 {
                fmt.Printf("unprocessed data after unmarshaling: %v\n", rest)
            }
            // 后续处理逻辑省略
...
}

核心问题

尽管ext.Value中存在有效数据,但logotypeData始终被解码为空结构。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 00:06:02