简体   繁体   中英

Go invalid ASN.1 Object Identifier encoding error

I am trying to create a DER file using encoding/asn1, and I keep getting an invalid Object Identifier runtime error. asn1.ObjectIdentifier is just an []int, so I'm not sure what's invalid.

package main

import (
    "encoding/asn1"
    "fmt"
    "log"
)

type algorithm struct {
    Algo asn1.ObjectIdentifier
    Null asn1.RawValue
}

func main() {
    var myCert algorithm
    myCert.Algo = asn1.ObjectIdentifier{42, 134, 72, 134}
    myCert.Null = asn1.NullRawValue

    mdata, err := asn1.Marshal(myCert)
    if err != nil {
        log.Fatalln(err)
    }

    fmt.Println(mdata)
}

The program exits with the following error: "asn1: structure error: invalid object identifier"

Playground example here

Object identifiers are not straight forward.

There are only 3 top level arcs (the integers that form the object identifier). In other words, first arc (42 in your example) can only be 0, 1 or 2

If your first arc is 0 or 1, then the second arc must be less than 40

Note: This restriction is used by the encoding rules of the object identifier value to pack the 2 first arcs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM