无法在golang中为asn解码定义正确的结构

时间:2017-01-31 09:25:22

标签: go asn.1

我有以下ASN1 structure使用golang进行解码

SEQUENCE(2 elem)
     SEQUENCE(2 elem)
          OBJECT IDENTIFIER1.2.840.113549.1.1.1
          NULL
     BIT STRING(1 elem)
          SEQUENCE(2 elem)
               INTEGER(2048 bit) 20832…
               INTEGER  65537

我正在使用以下结构来存储解码数据:

type OidAndNullSET struct {
    OID  asn1.ObjectIdentifier
    Null asn1.RawValue
}

type Seq struct {
    Set    OidAndNullSET
    BitStr asn1.BitString
}

func main() {
    mdata2 := []byte("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhxW+fVIU3KiVWyHy5RZ6jMQrXbrpUNOrz8V29qhZ98G53o6yKeUc2dOXC/dX2w8lEXjf+Hys9BLAJJZc6dlV1vrVZM5C9fvCAMHlAcMW2AADLuG+SruM6URBedSAxMFVwAzsSApLEQSlfGyMvjT+UOrHEjBUMn4+IPiLW2G0o1pHxCkrUxub/RWpl5qO7BbuEQj4flbUGpOpFW+XOuYu78MRmEvl/E9SX8b04RrXZTxPMAqxAl/zRA7VgIVzwtcm6xjzFw8kvr7H4B/zb7Jvl32FhniXMOrPfSGI2xhrr92DTOaPXuPFH2DywbNj/O21fenykWYsB/bA8vH7/EmQdwIDAQAB")


    var n Seq
    _, err1 := asn1.Unmarshal(mdata2, &n)
    checkError(err1)

    fmt.Println("After unmarshal: ", n)
}

func checkError(err error) {
    if err != nil {
        fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
        os.Exit(1)
    }
}

我找不到合适的结构来存储SEQUENCE *并收到跟踪错误

  

asn1:结构错误:标签不匹配(16 vs {class:1 tag:13 length:73 isCompound:false})。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您定义了base64编码的ASN.1流,需要在传递给Unmarshal之前对其进行解码。考虑下一个定义:

mdata2, _ := base64.StdEncoding.DecodeString("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhxW+fVIU3KiVWyHy5RZ6jMQrXbrpUNOrz8V29qhZ98G53o6yKeUc2dOXC/dX2w8lEXjf+Hys9BLAJJZc6dlV1vrVZM5C9fvCAMHlAcMW2AADLuG+SruM6URBedSAxMFVwAzsSApLEQSlfGyMvjT+UOrHEjBUMn4+IPiLW2G0o1pHxCkrUxub/RWpl5qO7BbuEQj4flbUGpOpFW+XOuYu78MRmEvl/E9SX8b04RrXZTxPMAqxAl/zRA7VgIVzwtcm6xjzFw8kvr7H4B/zb7Jvl32FhniXMOrPfSGI2xhrr92DTOaPXuPFH2DywbNj/O21fenykWYsB/bA8vH7/EmQdwIDAQAB")

然后,在将"encoding/base64"添加到导入后,它应该可以正常工作。