// Regression: an ObjectIdentifier must marshal to the real base-128 DER, and a // string field must marshal as a string. // // oidEncoder.Encode used the Go idiom // dst = appendBase128Int(dst[:0], int64(...)) // Moxie has no aliasing - a subslice is a COPY - so dst[:0] was a fresh buffer. // appendBase128Int appended into that fresh buffer and reassigned the local // dst, which Encode then discarded (it has no result). The caller's dst kept // whatever it had, so every ObjectIdentifier in every marshalled structure // came out 00 00 00 and all x509 DER was corrupt. // // Fixed in: src/encoding/asn1/marshal.mx (writeBase128Int writes in place // through a cursor; the encoder contract gives dst exactly Len() bytes). // // Still open in the same package: a plain []byte field marshals as a // UTF8String (0c) instead of an OCTET STRING (04), because the unified // string=[]byte type reaches asn1 as one descriptor kind (akBytes) and cannot // tell the two spellings apart. See the work log; that one is asserted // only for the string side here. package main import ( "encoding/asn1" "encoding/hex" "fmt" "unsafe" ) type strWrap struct { Name string `asn1:"string"` } // Without the tag a plain []byte field is an OCTET STRING. Moxie unifies string // and []byte into ONE type descriptor (akBytes), so asn1 cannot tell the // spellings apart; the byte meaning is the default because PKCS#8 and x509 // SubjectPublicKey need it, and a character string is requested explicitly. type bytesWrap struct { Key []byte } func chk(what string, got, want string) { if got != want { println("FAIL: " | what | " got " | got | " want " | want) panic("mismatch") } } func main() { // Termination: a batch program must still be stoppable, so the entry // handles the channel the runtime closes on SIGINT/SIGTERM. select { case <-shutdown: return default: } // 1.2.840.113549.1.1.1 (rsaEncryption). oid := asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1} b, err := asn1.Marshal(unsafe.TypeOf(oid), unsafe.Pointer(&oid)) if err != nil { println("FAIL: oid marshal: " | err.String()) panic("marshal") } chk("oid", string(hex.EncodeToString(b)), "06092a864886f70d010101") // A small OID whose first two arcs and one long arc exercise the cursor: // 2.5.4.3 (commonName) -> 06 03 55 04 03, and 1.3.6.1.4.1.311 (a long arc). oid2 := asn1.ObjectIdentifier{2, 5, 4, 3} b2, err2 := asn1.Marshal(unsafe.TypeOf(oid2), unsafe.Pointer(&oid2)) if err2 != nil { println("FAIL: oid2 marshal: " | err2.String()) panic("marshal") } chk("oid2", string(hex.EncodeToString(b2)), "0603550403") oid3 := asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311} b3, err3 := asn1.Marshal(unsafe.TypeOf(oid3), unsafe.Pointer(&oid3)) if err3 != nil { println("FAIL: oid3 marshal: " | err3.String()) panic("marshal") } chk("oid3", string(hex.EncodeToString(b3)), "06072b060104018237") // An asn1:"string" field is a PrintableString(13) when its content is // printable: 30 03 13 01 78. sw := strWrap{Name: "x"} sb, serr := asn1.Marshal(unsafe.TypeOf(sw), unsafe.Pointer(&sw)) if serr != nil { println("FAIL: string marshal: " | serr.String()) panic("marshal") } chk("string", string(hex.EncodeToString(sb)), "3003130178") // A plain []byte field is an OCTET STRING(04): 30 05 04 03 61 62 63. bw := bytesWrap{Key: []byte("abc")} bb, berr := asn1.Marshal(unsafe.TypeOf(bw), unsafe.Pointer(&bw)) if berr != nil { println("FAIL: []byte marshal: " | berr.String()) panic("marshal") } chk("[]byte", string(hex.EncodeToString(bb)), "30050403616263") // Non-UTF-8 bytes must marshal too. Before the flip this failed outright // with "asn1: string not valid UTF-8", which is what blocked every DER // structure carrying a raw key: 30 06 04 04 01 ff 80 00. nb := bytesWrap{Key: []byte{0x01, 0xff, 0x80, 0x00}} nbb, nberr := asn1.Marshal(unsafe.TypeOf(nb), unsafe.Pointer(&nb)) if nberr != nil { println("FAIL: raw []byte marshal: " | nberr.String()) panic("marshal") } chk("raw []byte", string(hex.EncodeToString(nbb)), "3006040401ff8000") fmt.Println("asn1 oid der ok") }