forked from milanbella/gocosem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasn1.go
3260 lines (2796 loc) · 75 KB
/
asn1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gocosem
import (
"bytes"
"fmt"
"io"
"math"
"time"
)
const DEBUG_ASN1 = false
// asn1 simple types
type tAsn1BitString struct {
buf []uint8
bitsUnused int
}
type tAsn1IA5String string
type tAsn1Integer int32
type tAsn1Integer8 int8
type tAsn1Integer16 int16
type tAsn1Integer32 int32
type tAsn1Long64 int64
type tAsn1Unsigned8 uint8
type tAsn1Unsigned16 uint16
type tAsn1Unsigned32 uint32
type tAsn1UnsignedLong64 uint64
type tAsn1Float float32
type tAsn1Float32 float32
type tAsn1Float64 float64
type tAsn1ObjectIdentifier []uint32
type tAsn1OctetString []uint8
type tAsn1PrintableString string
type tAsn1VisibleString []byte
type tAsn1T61String string
type tAsn1UTCTime time.Time
type tAsn1GraphicString []byte
type tAsn1Any []byte
type tAsn1Null int
type tAsn1Boolean bool
type tAsn1DateTime []byte
type tAsn1Date []byte
type tAsn1Time []byte
type tAsn1Choice struct {
tag int
val interface{}
}
type tAsn1CosemAuthenticationValueOther struct {
otherMechanismName tAsn1ObjectIdentifier
otherMechanismValue tAsn1OctetString
}
func (ch *tAsn1Choice) setVal(tag int, val interface{}) {
ch.tag = tag
ch.val = val
}
func (ch *tAsn1Choice) getTag() int {
return ch.tag
}
func (ch *tAsn1Choice) getVal() interface{} {
return ch.val
}
type tAuthenticationValueOther struct {
otherMechanismName tAsn1ObjectIdentifier
otherMechanismValue tAsn1Any
}
type AARQapdu struct {
//protocol-version [0] IMPLICIT T-protocol-version DEFAULT {version1},
protocolVersion *tAsn1BitString
//application-context-name [1] Application-context-name,
applicationContextName tAsn1ObjectIdentifier
//called-AP-title [2] AP-title OPTIONAL,
calledAPtitle *tAsn1OctetString
//called-AE-qualifier [3] AE-qualifier OPTIONAL,
calledAEqualifier *tAsn1OctetString
//called-AP-invocation-id [4] AP-invocation-identifier OPTIONAL,
calledAPinvocationId *tAsn1Integer
//called-AE-invocation-id [5] AE-invocation-identifier OPTIONAL,
calledAEinvocationId *tAsn1Integer
//calling-AP-title [6] AP-title OPTIONAL,
callingAPtitle *tAsn1OctetString
//calling-AE-qualifier [7] AE-qualifier OPTIONAL,
callingAEqualifier *tAsn1OctetString
//calling-AP-invocation-id [8] AP-invocation-identifier OPTIONAL,
callingAPinvocationId *tAsn1Integer
//calling-AE-invocation-id [9] AE-invocation-identifier OPTIONAL,
callingAEinvocationId *tAsn1Integer
//-- The following field shall not be present if only the kernel is used.
//sender-acse-requirements [10] IMPLICIT ACSE-requirements OPTIONAL,
senderAcseRequirements *tAsn1BitString
//-- The following field shall only be present if the authentication functional unit is selected.
//mechanism-name [11] IMPLICIT Mechanism-name OPTIONAL,
mechanismName *tAsn1ObjectIdentifier
//-- The following field shall only be present if the authentication functional unit is selected.
//calling-authentication-value [12] EXPLICIT Authentication-value OPTIONAL,
callingAuthenticationValue *tAsn1Choice
//implementation-information [29] IMPLICIT Implementation-data OPTIONAL,
implementationInformation *tAsn1GraphicString
//user-information [30] EXPLICIT Association-information OPTIONAL
userInformation *tAsn1OctetString
}
//AARE-apdu ::= [APPLICATION 1] IMPLICIT SEQUENCE
type AAREapdu struct {
//-- [APPLICATION 1] == [ 61H ] = [ 97 ]
//protocol-version [0] IMPLICIT T-protocol-version DEFAULT {version1},
protocolVersion *tAsn1BitString
//application-context-name [1] Application-context-name,
applicationContextName tAsn1ObjectIdentifier
//result [2] Association-result,
result tAsn1Integer
//result-source-diagnostic [3] Associate-source-diagnostic,
resultSourceDiagnostic tAsn1Choice
//responding-AP-title [4] AP-title OPTIONAL,
respondingAPtitle *tAsn1OctetString
//responding-AE-qualifier [5] AE-qualifier OPTIONAL,
respondingAEqualifier *tAsn1OctetString
//responding-AP-invocation-id [6] AP-invocation-identifier OPTIONAL,
respondingAPinvocationId *tAsn1Integer
//responding-AE-invocation-id [7] AE-invocation-identifier OPTIONAL,
respondingAEinvocationId *tAsn1Integer
//-- The following field shall not be present if only the kernel is used.
//responder-acse-requirements [8] IMPLICIT ACSE-requirements OPTIONAL,
responderAcseRequirements *tAsn1BitString
//-- The following field shall only be present if the authentication functional unit is selected.
//mechanism-name [9] IMPLICIT Mechanism-name OPTIONAL,
mechanismName *tAsn1ObjectIdentifier
//-- The following field shall only be present if the authentication functional unit is selected.
//responding-authentication-value [10] EXPLICIT Authentication-value OPTIONAL,
respondingAuthenticationValue *tAsn1Choice
//implementation-information [29] IMPLICIT Implementation-data OPTIONAL,
implementationInformation *tAsn1GraphicString
//user-information [30] EXPLICIT Association-information OPTIONAL
userInformation *tAsn1OctetString
}
const ASN1_CLASS_UNIVERSAL = 0x00
const ASN1_CLASS_APPLICATION = 0x01
const ASN1_CLASS_CONTEXT_SPECIFIC = 0x02
const ASN1_CLASS_PRIVATE = 0x04
const BER_ENCODING_PRIMITIVE = 0x00
const BER_ENCODING_CONSTRUCTED = 0x01
type t_der_encode_contect_fn func(buf bytes.Buffer, val interface{}) (err error)
type t_der_decode_contect_fn func(r io.Reader, ch *t_der_chunk, val interface{}) (err error)
type t_der_chunk struct {
asn1_class uint8
encoding uint8
asn1_tag uint32
content []byte
length uint32
}
func der_print_chunk(ch *t_der_chunk) string {
var asn1_class string
if ASN1_CLASS_UNIVERSAL == ch.asn1_class {
asn1_class = "u"
} else if ASN1_CLASS_APPLICATION == ch.asn1_class {
asn1_class = "a"
} else if ASN1_CLASS_CONTEXT_SPECIFIC == ch.asn1_class {
asn1_class = "c"
} else if ASN1_CLASS_PRIVATE == ch.asn1_class {
asn1_class = "p"
} else {
asn1_class = "u"
}
var encoding string
if BER_ENCODING_PRIMITIVE == ch.encoding {
encoding = "p"
} else if BER_ENCODING_CONSTRUCTED == ch.encoding {
encoding = "c"
} else {
encoding = "u"
}
tag := fmt.Sprintf("%3d", ch.asn1_tag)
length := fmt.Sprintf("%5d", ch.length)
content := fmt.Sprintf("%X", ch.content)
chunk := fmt.Sprintf("%s %s %s %s %s", asn1_class, encoding, tag, length, content)
return chunk
}
func encode_uint32_base128(w io.Writer, val uint32) (err error) {
b := make([]uint8, 1)
if val <= uint32(0x7f) {
b[0] = uint8(val & 0x7f)
_, err := w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
} else if val <= uint32(0x3fff) {
b[0] = uint8((val&0x3f80)>>7) | 0x80
_, err := w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
b[0] = uint8(val & 0x7f)
_, err = w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
} else if val <= uint32(0x1fffff) {
b[0] = uint8((val&0x1fc000)>>14) | 0x80
_, err := w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
b[0] = uint8((val&0x3f80)>>7) | 0x80
_, err = w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
b[0] = uint8(val & 0x7f)
_, err = w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
} else if val <= uint32(0x0fffffff) {
b[0] = uint8((val&0xfe00000)>>21) | 0x80
_, err := w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
b[0] = uint8((val&0x1fc000)>>14) | 0x80
_, err = w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
b[0] = uint8((val&0x3f80)>>7) | 0x80
_, err = w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
b[0] = uint8(val & 0x7f)
_, err = w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
} else if val <= uint32(0xffffffff) {
b[0] = uint8(val&0x00000000>>28) | 0x80
_, err := w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
b[0] = uint8(val&0xfe00000>>21) | 0x80
_, err = w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
b[0] = uint8(val&0x1fc000>>14) | 0x80
_, err = w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
b[0] = uint8(val&0x3f80>>7) | 0x80
_, err = w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
b[0] = uint8(val & 0x7f)
_, err = w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
} else {
errorLog("asserion failed", err)
return err
}
return nil
}
func decode_uint32_base128(r io.Reader) (err error, val uint32) {
b := make([]byte, 1)
_, err = r.Read(b)
if nil != err {
errorLog("io.Read(): %v", err)
return err, 0
}
val = uint32(0x7f & b[0])
if b[0]&0x80 > 0 {
_, err = r.Read(b)
if nil != err {
errorLog("io.Read(): %v", err)
return err, val
}
val = (val << 7) | uint32(0x7f&b[0])
if uint32(b[0]&0x80) > 0 {
_, err = r.Read(b)
if nil != err {
errorLog("io.Read(): %v", err)
return err, val
}
val = (val << 7) | uint32(0x7f&b[0])
if b[0]&0x80 > 0 {
_, err = r.Read(b)
if nil != err {
errorLog("io.Read(): %v", err)
return err, val
}
val = (val << 7) | uint32(0x7f&b[0])
if b[0]&0x80 > 0 {
_, err = r.Read(b)
if nil != err {
errorLog("io.Read(): %v", err)
return err, val
}
if b[0] > 0x0f {
err = fmt.Errorf("value of tag exceeds limit: %v", math.MaxUint32)
errorLog("%s", err)
return err, val
}
val = (val << 4) | uint32(0x7f&b[0])
}
}
}
}
return err, val
}
func der_encode_Integer(i tAsn1Integer) (err error, content []uint8) {
var _i int64 = int64(i)
// compute minimum number of bytes needed for encoding integer
n := 1
for _i > 127 { // minimum bytes if integer is positive
n++
_i >>= 8
}
for i < -128 { // minimum bytes if integer is negative
n++
i >>= 8
}
content = make([]uint8, n)
for j := 0; j < n; j++ {
content[j] = uint8(_i >> uint((n-1-j)*8))
}
return nil, content
}
func der_decode_Integer(content []uint8) (err error, i tAsn1Integer) {
if len(content) < 1 {
err = fmt.Errorf("decoding error: empty integer")
errorLog("%s", err)
return err, 0
}
if len(content) > 1 {
if (content[0] == 0 && content[1]&0x80 == 0) || (content[0] == 0xff && content[1]&0x80 == 0x80) {
err = fmt.Errorf("decoding error: not minimally encoded integer")
errorLog("%s", err)
return err, 0
}
}
var _i int64
if len(content) > 8 {
err = fmt.Errorf("decoding error: integer is too big")
errorLog("%s", err)
return err, 0
}
for j := 0; j < len(content); j++ {
_i <<= 8
_i |= int64(content[j])
}
// shift left and right to sign extend
// https://en.wikipedia.org/wiki/Sign_extension
_i <<= 64 - uint8(len(content))*8
_i >>= 64 - uint8(len(content))*8
if _i != int64(int32(_i)) {
err = fmt.Errorf("decoding error: integer is too big")
errorLog("%s", err)
return err, 0
}
return nil, tAsn1Integer(_i)
}
func der_encode_BitString(bs *tAsn1BitString) (err error, content []uint8) {
if nil == bs || nil == bs.buf {
content = make([]uint8, 0)
return nil, content
}
if 0 == len(bs.buf) {
content = make([]uint8, 1)
content[0] = 0
return nil, content
}
content = make([]uint8, len(bs.buf)+1)
if bs.bitsUnused > 7 {
err = fmt.Errorf("wrong count of unused bits")
errorLog("%v", err)
return err, nil
}
content[0] = uint8(bs.bitsUnused)
for i := 0; i < len(bs.buf); i++ {
content[i+1] = bs.buf[i]
}
return nil, content
}
func der_decode_BitString(content []uint8) (err error, bs *tAsn1BitString) {
if len(content) < 1 {
return nil, nil
}
var _bs tAsn1BitString
_bs.buf = make([]byte, len(content)-1)
_bs.bitsUnused = int(content[0])
for i := 0; i < len(content)-1; i++ {
_bs.buf[i] = content[i+1]
}
return nil, &_bs
}
func der_encode_ObjectIdentifier(oi *tAsn1ObjectIdentifier) (err error, content []uint8) {
if nil == oi {
content = make([]uint8, 0)
return nil, content
}
_oi := ([]uint32)(*oi)
if 0 == len(_oi) {
content = make([]uint8, 0)
return nil, content
}
if len(_oi) < 2 {
panic("object identifier must contain at least 2 components")
}
if _oi[0] > 2 {
panic("first component value must be in range 0-2")
}
if _oi[0] < 2 {
if _oi[1] > 39 {
panic("second component must be in range 0-39")
}
}
var buf bytes.Buffer
err = encode_uint32_base128(&buf, 40*_oi[0]+_oi[1])
if nil != err {
panic(err)
}
for i := 2; i < len(_oi); i++ {
err := encode_uint32_base128(&buf, _oi[i])
if nil != err {
return err, nil
}
}
content = buf.Bytes()
return nil, content
}
func der_decode_ObjectIdentifier(content []uint8) (err error, oi *tAsn1ObjectIdentifier) {
if len(content) < 1 {
return nil, nil
}
buf := bytes.NewReader(content)
COMPONENTS_BUFFER_SIZE := 100
components := make([]uint32, COMPONENTS_BUFFER_SIZE)
var i int
var component uint32
for i = 0; i < len(components); i++ {
if buf.Len() > 0 {
err, component = decode_uint32_base128(buf)
if nil != err {
return err, nil
}
} else {
break
}
if 0 == i {
if (0 >= component) && (39 <= component) {
components[0] = 0
components[1] = component
} else if (40 >= component) && (79 <= component) {
components[0] = 1
components[1] = component - 40
} else {
components[0] = 2
components[1] = component - 80
}
i = 1
} else {
components[i] = component
}
}
if i == len(components) {
if buf.Len() > 0 { // there are still some components remaining to be read
err = fmt.Errorf("COMPONENTS_BUFFER_SIZE small")
errorLog("%v", err)
return err, nil
}
}
components = components[0:i]
return nil, (*tAsn1ObjectIdentifier)(&components)
}
func der_encode_chunk(w io.Writer, ch *t_der_chunk) (err error) {
if DEBUG_ASN1 {
fmt.Printf("%s\n", der_print_chunk(ch))
}
b := make([]byte, 1)
// class
b[0] = ch.asn1_class << 6
// encoding
b[0] |= ch.encoding << 5
// tag
if ch.asn1_tag < 31 {
b[0] |= uint8(ch.asn1_tag)
_, err := w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
} else {
b[0] |= 0x1f
_, err := w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
err = encode_uint32_base128(w, ch.asn1_tag)
if nil != err {
return err
}
}
length := uint64(len(ch.content))
// length
if length <= 0x7f {
b[0] = uint8(length)
_, err := w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
} else {
var m int
if length <= 0xff {
m = 1
} else if length <= 0xffff {
m = 2
} else if length <= 0xffffff {
m = 3
} else if length <= 0xffffffff {
m = 4
} else if length <= 0xffffffffff {
m = 5
} else if length <= 0xffffffffffff {
m = 6
} else if length <= 0xffffffffffffff {
m = 7
} else if length <= 0xffffffffffffffff {
m = 8
} else {
err = fmt.Errorf("asserion failed")
errorLog("%v", err)
return err
}
b[0] = uint8(m) | 0x10
_, err := w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
for i := 0; i < m; i++ {
b[0] = uint8((length >> uint(i)) & 0xff)
_, err := w.Write(b)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
}
}
_, err = w.Write(ch.content)
if nil != err {
errorLog("io.Write(): %v", err)
return err
}
return err
}
func der_decode_chunk(r io.Reader) (err error, _ch *t_der_chunk) {
var m int
var ch t_der_chunk
b := make([]byte, 1)
if DEBUG_ASN1 {
// print bytes to be parsed
var _err error
var pbuf bytes.Buffer
for {
_, _err = r.Read(b)
if io.EOF == _err {
break
}
if nil != _err {
errorLog("io.Read(): %v", err)
return _err, nil
}
_, _err = pbuf.Write(b)
if nil != _err {
errorLog("pbuf.Write(): %v", err)
return _err, nil
}
}
readerContent := pbuf.Bytes()
fmt.Printf("%X\n", readerContent)
r = bytes.NewReader(readerContent)
}
_, err = r.Read(b)
if nil != err {
errorLog("io.Read(): %v", err)
return err, nil
}
ch.length++
// class
ch.asn1_class = (b[0] & 0xc0) >> 6
// encoding
ch.encoding = (b[0] & 0x20) >> 5
// tag
if 0x1f == b[0]&0x1F {
err, ch.asn1_tag = decode_uint32_base128(r)
if nil != err {
return err, nil
}
if ch.asn1_tag < 31 {
err = fmt.Errorf("wrong tag value")
errorLog("%s", err)
return err, nil
}
} else {
ch.asn1_tag = uint32(0x1f & b[0])
if ch.asn1_tag > 30 {
err = fmt.Errorf("wrong tag value")
errorLog("%s", err)
return err, nil
}
}
// length
var length uint64
_, err = r.Read(b)
if nil != err {
errorLog("io.Read(): %v", err)
return err, nil
}
ch.length++
if 0x80 == b[0] {
err = fmt.Errorf("wrog encoding: length 0x80")
errorLog("%s", err)
return err, nil
} else if 0x00 == b[0]&0x80 {
length = uint64(b[0] & 0x7f)
} else {
m = int(0x7f & b[0])
if m > 8 {
err = fmt.Errorf("value of length exceeds limit")
errorLog("%s", err)
return err, nil
}
_, err := r.Read(b)
if nil != err {
errorLog("io.Read(): %v", err)
return err, nil
}
ch.length++
length = uint64(b[0])
for i := 0; i < m-1; m-- {
_, err := r.Read(b)
if nil != err {
errorLog("io.Read(): %v", err)
return err, nil
}
ch.length++
length = (length << 8) | uint64(b[0])
}
}
if length > 1024 { // guard against allocating too much
err = fmt.Errorf("content too long: %v bytes", length)
errorLog("%s", err)
return err, nil
}
ch.content = make([]byte, length)
_, err = r.Read(ch.content)
if nil != err {
errorLog("io.Read(): %v", err)
return err, nil
}
ch.length += uint32(len(ch.content))
if DEBUG_ASN1 {
fmt.Printf("%s\n", der_print_chunk(&ch))
}
return nil, &ch
}
func encode_AARQapdu(w io.Writer, aarq *AARQapdu) (err error) {
var ch, ch1, ch2, ch3, chAARQ *t_der_chunk
var buf, bufAARQ *bytes.Buffer
if nil == aarq {
return nil
}
// AARQ-apdu ::= [APPLICATION 0] IMPLICIT SEQUENCE
chAARQ = new(t_der_chunk)
chAARQ.asn1_class = ASN1_CLASS_APPLICATION
chAARQ.encoding = BER_ENCODING_CONSTRUCTED
chAARQ.asn1_tag = 0
bufAARQ = new(bytes.Buffer)
// protocol-version [0] IMPLICIT T-protocol-version DEFAULT {version1}
if nil != aarq.protocolVersion {
ch = new(t_der_chunk)
ch.asn1_class = ASN1_CLASS_CONTEXT_SPECIFIC
ch.encoding = BER_ENCODING_PRIMITIVE
ch.asn1_tag = 0
err, ch.content = der_encode_BitString(aarq.protocolVersion)
if nil != err {
return err
}
err = der_encode_chunk(bufAARQ, ch)
if nil != err {
return err
}
}
// application-context-name [1] Application-context-name,
if nil != aarq.applicationContextName {
ch = new(t_der_chunk)
ch.asn1_class = ASN1_CLASS_CONTEXT_SPECIFIC
ch.encoding = BER_ENCODING_CONSTRUCTED
ch.asn1_tag = 1
ch1 = new(t_der_chunk)
ch1.asn1_class = ASN1_CLASS_UNIVERSAL
ch1.encoding = BER_ENCODING_PRIMITIVE
ch1.asn1_tag = 6
err, ch1.content = der_encode_ObjectIdentifier(&aarq.applicationContextName)
if nil != err {
return err
}
buf = new(bytes.Buffer)
err = der_encode_chunk(buf, ch1)
if nil != err {
return err
}
ch.content = buf.Bytes()
err = der_encode_chunk(bufAARQ, ch)
if nil != err {
return err
}
}
// called-AP-title [2] AP-title OPTIONAL,
if nil != aarq.calledAPtitle {
ch = new(t_der_chunk)
ch.asn1_class = ASN1_CLASS_CONTEXT_SPECIFIC
ch.encoding = BER_ENCODING_CONSTRUCTED
ch.asn1_tag = 2
ch1 = new(t_der_chunk)
ch1.asn1_class = ASN1_CLASS_UNIVERSAL
ch1.encoding = BER_ENCODING_PRIMITIVE
ch1.asn1_tag = 4
octetString := ([]uint8)(*aarq.calledAPtitle)
ch1.content = make([]uint8, len(octetString))
copy(ch1.content, octetString)
buf = new(bytes.Buffer)
err = der_encode_chunk(buf, ch1)
if nil != err {
return err
}
ch.content = buf.Bytes()
err = der_encode_chunk(bufAARQ, ch)
if nil != err {
return err
}
}
// called-AE-qualifier [3] AE-qualifier OPTIONAL,
if nil != aarq.calledAEqualifier {
ch = new(t_der_chunk)
ch.asn1_class = ASN1_CLASS_CONTEXT_SPECIFIC
ch.encoding = BER_ENCODING_CONSTRUCTED
ch.asn1_tag = 3
ch1 = new(t_der_chunk)
ch1.asn1_class = ASN1_CLASS_UNIVERSAL
ch1.encoding = BER_ENCODING_PRIMITIVE
ch1.asn1_tag = 4
octetString := ([]uint8)(*aarq.calledAEqualifier)
ch1.content = make([]uint8, len(octetString))
copy(ch1.content, octetString)
buf = new(bytes.Buffer)
err = der_encode_chunk(buf, ch1)
if nil != err {
return err
}
ch.content = buf.Bytes()
err = der_encode_chunk(bufAARQ, ch)
if nil != err {
return err
}
}
// called-AP-invocation-id [4] AP-invocation-identifier OPTIONAL,
if nil != aarq.calledAPinvocationId {
ch = new(t_der_chunk)
ch.asn1_class = ASN1_CLASS_CONTEXT_SPECIFIC
ch.encoding = BER_ENCODING_CONSTRUCTED
ch.asn1_tag = 4
ch1 = new(t_der_chunk)
ch1.asn1_class = ASN1_CLASS_UNIVERSAL
ch1.encoding = BER_ENCODING_PRIMITIVE
ch1.asn1_tag = 2
err, ch1.content = der_encode_Integer(*aarq.calledAPinvocationId)
if nil != err {
return err
}
buf = new(bytes.Buffer)
err = der_encode_chunk(buf, ch1)
if nil != err {
return err
}
ch.content = buf.Bytes()
err = der_encode_chunk(bufAARQ, ch)
if nil != err {
return err
}
}
// called-AE-invocation-id [5] AE-invocation-identifier OPTIONAL,
if nil != aarq.calledAEinvocationId {
ch = new(t_der_chunk)
ch.asn1_class = ASN1_CLASS_CONTEXT_SPECIFIC
ch.encoding = BER_ENCODING_CONSTRUCTED
ch.asn1_tag = 5
ch1 = new(t_der_chunk)
ch1.asn1_class = ASN1_CLASS_UNIVERSAL
ch1.encoding = BER_ENCODING_PRIMITIVE
ch1.asn1_tag = 2
err, ch1.content = der_encode_Integer(*aarq.calledAEinvocationId)
if nil != err {
return err
}
buf = new(bytes.Buffer)
err = der_encode_chunk(buf, ch1)
if nil != err {
return err
}
ch.content = buf.Bytes()
err = der_encode_chunk(bufAARQ, ch)
if nil != err {
return err
}
}