1 // Regression: an ObjectIdentifier must marshal to the real base-128 DER, and a
2 // string field must marshal as a string.
3 //
4 // oidEncoder.Encode used the Go idiom
5 // dst = appendBase128Int(dst[:0], int64(...))
6 // Moxie has no aliasing - a subslice is a COPY - so dst[:0] was a fresh buffer.
7 // appendBase128Int appended into that fresh buffer and reassigned the local
8 // dst, which Encode then discarded (it has no result). The caller's dst kept
9 // whatever it had, so every ObjectIdentifier in every marshalled structure
10 // came out 00 00 00 and all x509 DER was corrupt.
11 //
12 // Fixed in: src/encoding/asn1/marshal.mx (writeBase128Int writes in place
13 // through a cursor; the encoder contract gives dst exactly Len() bytes).
14 //
15 // Still open in the same package: a plain []byte field marshals as a
16 // UTF8String (0c) instead of an OCTET STRING (04), because the unified
17 // string=[]byte type reaches asn1 as one descriptor kind (akBytes) and cannot
18 // tell the two spellings apart. See the work log; that one is asserted
19 // only for the string side here.
20 package main
21 22 import (
23 "encoding/asn1"
24 "encoding/hex"
25 "fmt"
26 "unsafe"
27 )
28 29 type strWrap struct {
30 Name string `asn1:"string"`
31 }
32 33 // Without the tag a plain []byte field is an OCTET STRING. Moxie unifies string
34 // and []byte into ONE type descriptor (akBytes), so asn1 cannot tell the
35 // spellings apart; the byte meaning is the default because PKCS#8 and x509
36 // SubjectPublicKey need it, and a character string is requested explicitly.
37 type bytesWrap struct {
38 Key []byte
39 }
40 41 func chk(what string, got, want string) {
42 if got != want {
43 println("FAIL: " | what | " got " | got | " want " | want)
44 panic("mismatch")
45 }
46 }
47 48 func main() {
49 // Termination: a batch program must still be stoppable, so the entry
50 // handles the channel the runtime closes on SIGINT/SIGTERM.
51 select {
52 case <-shutdown:
53 return
54 default:
55 }
56 57 // 1.2.840.113549.1.1.1 (rsaEncryption).
58 oid := asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
59 b, err := asn1.Marshal(unsafe.TypeOf(oid), unsafe.Pointer(&oid))
60 if err != nil {
61 println("FAIL: oid marshal: " | err.String())
62 panic("marshal")
63 }
64 chk("oid", string(hex.EncodeToString(b)), "06092a864886f70d010101")
65 66 // A small OID whose first two arcs and one long arc exercise the cursor:
67 // 2.5.4.3 (commonName) -> 06 03 55 04 03, and 1.3.6.1.4.1.311 (a long arc).
68 oid2 := asn1.ObjectIdentifier{2, 5, 4, 3}
69 b2, err2 := asn1.Marshal(unsafe.TypeOf(oid2), unsafe.Pointer(&oid2))
70 if err2 != nil {
71 println("FAIL: oid2 marshal: " | err2.String())
72 panic("marshal")
73 }
74 chk("oid2", string(hex.EncodeToString(b2)), "0603550403")
75 76 oid3 := asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311}
77 b3, err3 := asn1.Marshal(unsafe.TypeOf(oid3), unsafe.Pointer(&oid3))
78 if err3 != nil {
79 println("FAIL: oid3 marshal: " | err3.String())
80 panic("marshal")
81 }
82 chk("oid3", string(hex.EncodeToString(b3)), "06072b060104018237")
83 84 // An asn1:"string" field is a PrintableString(13) when its content is
85 // printable: 30 03 13 01 78.
86 sw := strWrap{Name: "x"}
87 sb, serr := asn1.Marshal(unsafe.TypeOf(sw), unsafe.Pointer(&sw))
88 if serr != nil {
89 println("FAIL: string marshal: " | serr.String())
90 panic("marshal")
91 }
92 chk("string", string(hex.EncodeToString(sb)), "3003130178")
93 94 // A plain []byte field is an OCTET STRING(04): 30 05 04 03 61 62 63.
95 bw := bytesWrap{Key: []byte("abc")}
96 bb, berr := asn1.Marshal(unsafe.TypeOf(bw), unsafe.Pointer(&bw))
97 if berr != nil {
98 println("FAIL: []byte marshal: " | berr.String())
99 panic("marshal")
100 }
101 chk("[]byte", string(hex.EncodeToString(bb)), "30050403616263")
102 103 // Non-UTF-8 bytes must marshal too. Before the flip this failed outright
104 // with "asn1: string not valid UTF-8", which is what blocked every DER
105 // structure carrying a raw key: 30 06 04 04 01 ff 80 00.
106 nb := bytesWrap{Key: []byte{0x01, 0xff, 0x80, 0x00}}
107 nbb, nberr := asn1.Marshal(unsafe.TypeOf(nb), unsafe.Pointer(&nb))
108 if nberr != nil {
109 println("FAIL: raw []byte marshal: " | nberr.String())
110 panic("marshal")
111 }
112 chk("raw []byte", string(hex.EncodeToString(nbb)), "3006040401ff8000")
113 114 fmt.Println("asn1 oid der ok")
115 }
116