v2 / vlib / x / encoding / asn1 / examples / examples0.v
113 lines · 92 sloc · 3.7 KB · 58fc4dead559901cad648fb695f31d0f6de9945a
Raw
1module main
2
3import x.encoding.asn1
4
5// Previously, defined type KerberosString = asn1.GeneralString directly
6// without reduplicating required Element methods, would produces
7// RUNTIME ERROR: invalid memory access.
8// **Updates**
9// Its has been resolved in [22901](https://github.com/vlang/v/issues/22901)
10// Thanks to @felipensp
11type KerberosString = asn1.GeneralString
12
13fn KerberosString.new(s string) !KerberosString {
14 return KerberosString(asn1.GeneralString.new(s)!)
15}
16
17type KerberosStringList = []KerberosString
18
19fn (ksl KerberosStringList) tag() asn1.Tag {
20 return asn1.default_sequence_tag
21}
22
23fn (ksl KerberosStringList) payload() ![]u8 {
24 mut out := []u8{}
25 for item in ksl {
26 // maybe produces x00000000: at ???: RUNTIME ERROR: invalid memory access
27 // `item` cannot be used as interface object outside `unsafe` blocks as it
28 // might be stored on stack. Consider declaring `KerberosString` as `@[heap]`
29 obj := unsafe { item }
30 out << asn1.encode(obj)!
31 }
32 return out
33}
34
35// PrincipalName ::= SEQUENCE {
36// name-type [0] Int32,
37// name-string [1] SEQUENCE OF KerberosString
38// }
39struct PrincipalName {
40 name_type asn1.Integer @[context_specific: 0; explicit; inner: 2] // integer tag = (universal, false, 2)
41
42 // defines name_string as asn1.SequenceOf[KerberosString] would produces unexpected result
43 // when you build payload with `asn1.make_payload`,
44 // see issues at https://github.com/vlang/v/issues/22721
45 // so we build with KerberosStringList
46 name_string KerberosStringList @[context_specific: 1; explicit; inner: 16] // sequence tag = (universal, true, 16)
47}
48
49fn (pn PrincipalName) tag() asn1.Tag {
50 return asn1.default_sequence_tag
51}
52
53fn (pn PrincipalName) payload() ![]u8 {
54 kd := asn1.new_key_default()
55 payload := asn1.make_payload[PrincipalName](pn, kd)!
56 return payload
57}
58
59fn PrincipalName.decode(bytes []u8) !PrincipalName {
60 // decode should produces Sequence type
61 elem := asn1.decode(bytes)!
62 assert elem.tag().equal(asn1.default_sequence_tag)
63
64 // cast it into Sequence type and get the fields
65 seq := elem.into_object[asn1.Sequence]()!
66 fields := seq.fields()
67
68 // every fields of the sequence is raw of wrapped element, so we should unwrap it with
69 // the same options used to wrap in encode step, and turn to the real underlying object.
70 el_name_type := fields[0].unwrap_with_options('context_specific: 0; explicit; inner: 2')!
71 name_type := el_name_type.into_object[asn1.Integer]()!
72
73 // tag 16 is sequence tag, its decoded into Sequence type
74 el_name_string := fields[1].unwrap_with_options('context_specific: 1; explicit; inner: 16')!
75 el_seq := el_name_string.into_object[asn1.Sequence]()!
76
77 // KerberosString string has a general_string tag, so it would be parsed as asn1.GeneralString
78 // so, we transforms it into KerberosString back and build KerberosStringList.
79 mut a := []KerberosString{}
80 for item in el_seq.fields() {
81 gst := item.into_object[asn1.GeneralString]()!
82 obj := KerberosString(gst)
83 a << obj
84 }
85
86 name_string_list := KerberosStringList(a)
87
88 return PrincipalName{
89 name_type: name_type
90 name_string: name_string_list
91 }
92}
93
94fn main() {
95 // Basically this is a Kerberos PrincipalName data
96 data := [u8(0x30), 0x15, 0xa0, 0x03, 0x02, 0x01, 0x01, 0xa1, 0x0e, 0x30, 0x0c, 0x1b, 0x0a,
97 0x62, 0x6f, 0x62, 0x62, 0x61, 0x2d, 0x66, 0x65, 0x74, 0x74]
98 els := [KerberosString.new('bobba-fett')!]
99
100 pn := PrincipalName{
101 name_type: asn1.Integer.from_int(1)
102 name_string: KerberosStringList(els)
103 }
104
105 out1 := asn1.encode(pn)!
106 back := PrincipalName.decode(data)!
107 dump(back == pn) // should assert to true
108 dump(out1.hex() == data.hex()) // should assert to true
109
110 // run this produces:
111 // [examples/examples_0.v:128] back == pn: true
112 // [examples/examples_0.v:129] out1.hex() == data.hex(): true
113}
114