title: Why is unmarshalling of a DER ASN.1 large integer limited to SEQUENCE in Golang?
body: <p>I would like to be able to unmarshal a large integer from a DER file using the encoding/asn1 package, but it looks like it only works for a SEQUENCE of integers.</p>
  
  <p>For example, this does not work, which is strange because the marshaling of the Big Int works well (<a href="https://play.golang.org/p/Wkj0jAA6bpq" rel="noreferrer">https://play.golang.org/p/Wkj0jAA6bpq</a>) :</p>
  
  <pre><code>package main
  
  import (
      "encoding/asn1"
      "fmt"
      "math/big"
  )
  
  
  func main() {
          r, _ := new(big.Int).SetString("481B6A0913BD8024CA5D22F014F3", 16);
          encoding, _  := asn1.Marshal(r) 
          fmt.Printf("% X\n", encoding)               // Displayed as an integer: 02 0E 48 1B 6A 09 13 BD 80 24 CA 5D 22 F0 14 F3
          var t big.Int
          _, err := asn1.Unmarshal(encoding, &amp;t)
          if err != nil {
              fmt.Println("Erreur unmarshalling -", err)  // asn1: structure error: tags don't match
          }   
          fmt.Println(t.String())
  } 
  </code></pre>
  
  <p>It looks like I can unmarshall a large integer only if it's part of a SEQUENCE like in this example : <a href="https://play.golang.org/p/J93afvbk41L" rel="noreferrer">https://play.golang.org/p/J93afvbk41L</a></p>
  
  <pre><code>package main
  
  import (
      "encoding/asn1"
      "fmt"
      "math/big"
  )
  
  type TestBigInt struct {                    // From encoding/asn1/asn1_test.go
      X *big.Int
  }
  
  func main() {
          r, _ := new(big.Int).SetString("481B6A0913BD8024CA5D22F014F3", 16);
          encoding, _  := asn1.Marshal(TestBigInt{r}) 
          fmt.Printf("% X\n", encoding)               // Displayed as a sequence of an integer: 30 10 02 0E 48 1B 6A 09 13 BD 80 24 CA 5D 22 F0 14 F3
          var t TestBigInt
          _, err := asn1.Unmarshal(encoding, &amp;t)
          if err != nil {
              fmt.Println("Erreur unmarshalling -", err)  
          }   
          fmt.Println(t.X.String())
  } 
  </code></pre>
  
  <p>However, it works well for 32 bits of 64 bits integer like in this example : <a href="https://play.golang.org/p/vcIgWrI-sk-" rel="noreferrer">https://play.golang.org/p/vcIgWrI-sk-</a></p>
  
  <pre><code>package main
  
  import (
      "encoding/asn1"
      "fmt"
  )
  
  func main() {
          r := 12234
          encoding, _ := asn1.Marshal(r)
          fmt.Printf("% X\n", encoding)
          var t int
          _, err := asn1.Unmarshal(encoding, &amp;t)
          if err != nil {
              fmt.Println("Erreur unmarshalling -", err)
          }   
          fmt.Println(t)
  }
  </code></pre>
  
  <p>Is this a known limitation of the encoding/asn1 package ? Any known solution to unmarshal a large integer outside a SEQUENCE ?</p>