I have Golang structure:
type Dog struct {
Name string
UUID *big.Int
}
Then I asn1.Marshal it:
dog := Dog{Name: "Rex", UUID: new(big.Int).SetBytes([]byte{0, 0, 7})}
dogAsn, _ := asn1.Marshal(dog)
When I look at the ASN.1 structure of "dogAsn" (using dumpasn1 Linux utility)I see:
SEQUENCE {
PrintableString 'Rex'
INTEGER
00 00 00 07
}
I wish NOT to have "PrintableString" there, but instead "GeneralString":
(Desired output):
SEQUENCE {
GeneralString 'Rex'
INTEGER
00 00 00 07
}
Adding asn1:"tag:27"
to the "Name" field:
type Dog struct {
Name string `asn1:"tag:27"`
...
}
Doesn't solve my issue. Any good ideas? Thanks in advance!
CodePudding user response:
> Solution has been found!
==============================
If you need to create an ASN.1 GeneralString in a Golang, then do this:
1.
type Dog struct {
Name asn1.RawValue // previously was "string"
UUID *big.Int
}
dogGeneralStr := asn1.RawValue{Tag: asn1.TagGeneralString, Bytes: []byte("Rex")}
dog := Dog{Name: dogGeneralStr, UUID: new(big.Int).SetBytes([]byte{0, 0,7})}
dogAsn, _ := asn1.Marshal(dog)
- SEQUENCE { GeneralString 'Rex' INTEGER 00 00 00 07 }
P.S Then inside your program, whenever you need this dog.Name in a []byte or string form you just do: dogGeneralStr.Bytes or string(dogGeneralStr.Bytes) respectively.
CodePudding user response:
You can't do this with the built in library. The marshalling logic allows you to pick between IA5String
, PrintableString
, NumericString
or UTF8String
.
I found an alternative ASN.1 library: https://github.com/Logicalis/asn1
But is is no longer maintained and also encodes all strings as OctetString
So you best bet is to copy the source code of encoding/asn1
and add a custom tag to indicate GeneralString
while encoding. Perhaps you could even make a PR out of it.