blob: e4519966ac834b7de90fb734ebc64941f5818a57 [file] [log] [blame]
Lev Walkin84291e02004-06-03 03:49:45 +00001#LyX 1.3 created this file. For more info see http://www.lyx.org/
2\lyxformat 221
3\textclass book
4\language english
5\inputencoding latin1
6\fontscheme ae
7\graphics default
8\paperfontsize default
9\spacing single
10\papersize Default
11\paperpackage a4
12\use_geometry 0
13\use_amsmath 0
14\use_natbib 0
15\use_numerical_citations 0
16\paperorientation portrait
17\secnumdepth 3
18\tocdepth 3
19\paragraph_separation indent
20\defskip medskip
21\quotes_language swedish
22\quotes_times 2
23\papercolumns 1
24\papersides 1
25\paperpagestyle default
26
27\layout Title
28
29ASN.1 Compiler: Usage
30\layout Author
31
32Lev Walkin <
33\begin_inset LatexCommand \url{vlm@lionet.info}
34
35\end_inset
36
37>
38\layout Standard
39
40
41\begin_inset LatexCommand \tableofcontents{}
42
43\end_inset
44
45
46\layout Chapter
47
48Basics of ASN.1
49\layout Standard
50
51
52\emph on
53This chapter defines some basic ASN.1 concepts and describes several most
54 widely used types.
55 It is by no means an authoritative or complete reference.
56 For more complete ASN.1 description, please refer to Olivier Dubuisson's
57 book
58\begin_inset LatexCommand \cite{Dub00}
59
60\end_inset
61
62 or the ASN.1 standard itself
63\begin_inset LatexCommand \cite{ITU-T/ASN.1}
64
65\end_inset
66
67.
68\layout Standard
69
70The Abstract Syntax Notation One is used to formally describe the semantics
71 of data transmitted across the network.
72 Two communicating parties may have different formats of their native data
73 types (i.e.
74 number of bits in the integer type), thus it is important to have a way
75 to describe the data in a manner which is independent from the particular
76 machine's representation.
77 The ASN.1 specifications is used to achieve one or more of the following:
78\layout Itemize
79
80The specification expressed in the ASN.1 notation is a formal and precise
81 way to communicate the data semantics to human readers;
82\layout Itemize
83
84The ASN.1 specifications may be used as input for automatic compilers which
85 produce the code for some target language (C, C++, Java, etc) to encode
86 and decode the data according to some encoding rules (which are also defined
87 by the ASN.1 standard).
88\layout Standard
89
90Consider the following example:
91\layout LyX-Code
92
93Rectangle ::= SEQUENCE {
94\layout LyX-Code
95
96 height INTEGER,
97\layout LyX-Code
98
99 width INTEGER
100\layout LyX-Code
101
102}
103\layout Standard
104
105This ASN.1 specification describes a constructed type,
106\emph on
107Rectangle
108\emph default
109, containing two integer fields.
110 This specification may tell the reader that there is this kind of data
111 structure and that some entity may be prepared to send or receive it.
112 The question on
113\emph on
114how
115\emph default
116 that entity is going to send or receive the
117\emph on
118encoded data
119\emph default
120 is outside the scope of ASN.1.
121 For example, this data structure may be encoded according to some encoding
122 rules and sent to the destination using the TCP protocol.
123 The ASN.1 specifies several ways of encoding (or
124\begin_inset Quotes sld
125\end_inset
126
127serializing
128\begin_inset Quotes srd
129\end_inset
130
131, or
132\begin_inset Quotes sld
133\end_inset
134
135marshaling
136\begin_inset Quotes srd
137\end_inset
138
139) the data: BER, CER, DER and XER, some of them which will be described
140 later.
141\layout Standard
142
143The complete specification must be wrapped in a module, which looks like
144 this:
145\layout LyX-Code
146
147UsageExampleModule1
148\layout LyX-Code
149
150 { iso org(3) dod(6) internet(1) private(4)
151\layout LyX-Code
152
153 enterprise(1) spelio(9363) software(1)
154\layout LyX-Code
155
156 asn1c(5) docs(2) usage(1) 1 }
157\layout LyX-Code
158
159 AUTOMATIC TAGS DEFINITIONS ::=
160\layout LyX-Code
161
162BEGIN
163\layout LyX-Code
164
165
166\layout LyX-Code
167
168-- This is a comment which describes nothing.
169\layout LyX-Code
170
171Rectangle ::= SEQUENCE {
172\layout LyX-Code
173
174 height INTEGER, -- Height of the rectangle
175\layout LyX-Code
176
177 width INTEGER, -- Width of the rectangle
178\layout LyX-Code
179
180}
181\layout LyX-Code
182
183
184\layout LyX-Code
185
186END
187\layout Standard
188
189The module header consists of module name (UsageExampleModule1), the module
190 object identifier ({...}), some flags (AUTOMATIC TAGS) and
191\begin_inset Quotes sld
192\end_inset
193
194DEFINITIONS ::= BEGIN
195\begin_inset Quotes srd
196\end_inset
197
198.
199 The module ends with an
200\begin_inset Quotes sld
201\end_inset
202
203END
204\begin_inset Quotes srd
205\end_inset
206
207 statement.
208\layout Section
209
210Some of the ASN.1 Basic Types
211\layout Subsection
212
213The BOOLEAN type
214\layout Standard
215
216The BOOLEAN type models the simple binary TRUE/FALSE, YES/NO, ON/OFF or
217 a similar kind of two-way choice.
218\layout Subsection
219
220The INTEGER type
221\layout Standard
222
223The INTEGER type is a signed natural number type without any restrictions
224 on its size.
225 If the automatic checking on INTEGER value bounds are necessary, the subtype
226 constraints must be used.
227\layout LyX-Code
228
229SimpleInteger ::= INTEGER
230\layout LyX-Code
231
232-- An integer with a very limited range
233\layout LyX-Code
234
235SmallInt ::= INTEGER (0..127)
236\layout LyX-Code
237
238-- Integer, negative
239\layout LyX-Code
240
241NegativeInt ::= INTEGER (MIN..0)
242\layout Subsection
243
244The ENUMERATED type
245\layout Standard
246
247The ENUMERATED type is semantically equivalent to the INTEGER type with
248 some integer values explicitly named.
249\layout LyX-Code
250
251FruitId ::= ENUMERATED { apple(1), orange(2) }
252\layout LyX-Code
253
254-- The numbers in braces are optional,
255\layout LyX-Code
256
257-- the enumeration may be performed
258\layout LyX-Code
259
260-- automatically by the compiler
261\layout LyX-Code
262
263ComputerOSType ::= ENUMERATED {
264\layout LyX-Code
265
266 FreeBSD, -- will be 0
267\layout LyX-Code
268
269 Windows, -- will be 1
270\layout LyX-Code
271
272 Solaris(5), -- will remain 5
273\layout LyX-Code
274
275 Linux, -- will be 6
276\layout LyX-Code
277
278 MacOS -- will be 7
279\layout LyX-Code
280
281}
282\layout Subsection
283
284The OCTET STRING type
285\layout Standard
286
287This type models the sequence of 8-bit bytes.
288 This may be used to transmit some opaque data or data serialized by other
289 types of encoders (i.e.
290 video file, photo picture, etc).
291\layout Subsection
292
293The OBJECT IDENTIFIER type
294\layout Standard
295
296The OBJECT IDENTIFIER is used to represent the unique identifier of any
297 object, starting from the very root of the registration tree.
298 If your organization needs to uniquely identify something (a router, a
299 room, a person, a standard, or whatever), you are encouraged to get your
300 own identification subtree at
301\begin_inset LatexCommand \htmlurl{http://www.iana.org/protocols/forms.htm}
302
303\end_inset
304
305.
306\layout Standard
307
308For example, the very first ASN.1 module in this document has the following
309 OBJECT IDENTIFIER: 1 3 6 1 4 1 9363 1 5 2 1 1.
310\layout LyX-Code
311
312ExampleOID ::= OBJECT IDENTIFIER
313\layout LyX-Code
314
315usageExampleModule1-oid ExampleOID
316\layout LyX-Code
317
318 ::= { 1 3 6 1 4 1 9363 1 5 2 1 1 }
319\layout LyX-Code
320
321-- An identifier of the Internet.
322\layout LyX-Code
323
324internet-id OBJECT IDENTIFIER
325\layout LyX-Code
326
327 ::= { iso(1) identified-organization(3)
328\layout LyX-Code
329
330 dod(6) internet(1) }
331\layout Standard
332
333As you see, names are optional.
334\layout Subsection
335
336The RELATIVE-OID type
337\layout Standard
338
339The RELATIVE-OID type has the semantics of a subtree of an OBJECT IDENTIFIER.
340 There may be no need to repeat the whole sequence of numbers from the root
341 of the registration tree where the only thing of interest is some of the
342 tree's subsequence.
343\layout LyX-Code
344
345this-document RELATIVE-OID ::= { docs(2) usage(1) }
346\layout LyX-Code
347
348this-example RELATIVE-OID ::= {
349\layout LyX-Code
350
351 this-document assorted-examples(0) this-example(1) }
352\layout Section
353
354Some of the ASN.1 String Types
355\layout Subsection
356
357The IA5String type
358\layout Standard
359
360This is essentially the ASCII, with 128 character codes available (7 lower
361 bits of 8-bit byte).
362\layout Subsection
363
364The UTF8String type
365\layout Standard
366
367This is the character string which encodes the full Unicode range (4 bytes)
368 using multibyte character sequences.
369\layout Subsection
370
371The NumericString type
372\layout Standard
373
374This type represents the character string with the alphabet consisting of
375 numbers (
376\begin_inset Quotes sld
377\end_inset
378
3790
380\begin_inset Quotes srd
381\end_inset
382
383 to
384\begin_inset Quotes sld
385\end_inset
386
3879
388\begin_inset Quotes srd
389\end_inset
390
391) and a space.
392\layout Subsection
393
394The PrintableString type
395\layout Standard
396
397The character string with the following alphabet: space,
398\begin_inset Quotes sld
399\end_inset
400
401
402\series bold
403'
404\series default
405
406\begin_inset Quotes srd
407\end_inset
408
409 (single quote),
410\begin_inset Quotes sld
411\end_inset
412
413
414\series bold
415(
416\series default
417
418\begin_inset Quotes sld
419\end_inset
420
421,
422\begin_inset Quotes sld
423\end_inset
424
425
426\series bold
427)
428\series default
429
430\begin_inset Quotes srd
431\end_inset
432
433,
434\begin_inset Quotes sld
435\end_inset
436
437
438\series bold
439+
440\series default
441
442\begin_inset Quotes srd
443\end_inset
444
445,
446\begin_inset Quotes sld
447\end_inset
448
449,
450\begin_inset Quotes srd
451\end_inset
452
453 (comma),
454\begin_inset Quotes sld
455\end_inset
456
457
458\series bold
459-
460\series default
461
462\begin_inset Quotes srd
463\end_inset
464
465,
466\begin_inset Quotes sld
467\end_inset
468
469
470\series bold
471.
472\series default
473
474\begin_inset Quotes srd
475\end_inset
476
477,
478\begin_inset Quotes sld
479\end_inset
480
481
482\series bold
483/
484\series default
485
486\begin_inset Quotes srd
487\end_inset
488
489, digits (
490\begin_inset Quotes sld
491\end_inset
492
4930
494\begin_inset Quotes srd
495\end_inset
496
497 to
498\begin_inset Quotes sld
499\end_inset
500
5019
502\begin_inset Quotes srd
503\end_inset
504
505),
506\begin_inset Quotes sld
507\end_inset
508
509
510\series bold
511:
512\series default
513
514\begin_inset Quotes srd
515\end_inset
516
517,
518\begin_inset Quotes sld
519\end_inset
520
521
522\series bold
523=
524\series default
525
526\begin_inset Quotes srd
527\end_inset
528
529,
530\begin_inset Quotes sld
531\end_inset
532
533
534\series bold
535?
536\series default
537
538\begin_inset Quotes srd
539\end_inset
540
541, upper-case and lower-case letters (
542\begin_inset Quotes sld
543\end_inset
544
545A
546\begin_inset Quotes srd
547\end_inset
548
549 to
550\begin_inset Quotes sld
551\end_inset
552
553Z
554\begin_inset Quotes srd
555\end_inset
556
557 and
558\begin_inset Quotes sld
559\end_inset
560
561a
562\begin_inset Quotes srd
563\end_inset
564
565 to
566\begin_inset Quotes sld
567\end_inset
568
569z
570\begin_inset Quotes srd
571\end_inset
572
573)
574\layout Subsection
575
576The VisibleString type
577\layout Standard
578
579The character string with the alphabet which is more or less a subset of
580 ASCII between space and
581\begin_inset Quotes sld
582\end_inset
583
584~
585\begin_inset Quotes srd
586\end_inset
587
588 (tilde).
589 Alternatively, the alphabet may be represented as the PrintableString alphabet
590 described earlier, plus the following characters:
591\begin_inset Quotes sld
592\end_inset
593
594
595\series bold
596!
597\series default
598
599\begin_inset Quotes srd
600\end_inset
601
602,
603\begin_inset Quotes sld
604\end_inset
605
606
607\series bold
608
609\begin_inset Quotes srd
610\end_inset
611
612
613\series default
614
615\begin_inset Quotes srd
616\end_inset
617
618,
619\begin_inset Quotes sld
620\end_inset
621
622
623\series bold
624#
625\series default
626
627\begin_inset Quotes srd
628\end_inset
629
630,
631\begin_inset Quotes sld
632\end_inset
633
634
635\series bold
636$
637\series default
638
639\begin_inset Quotes srd
640\end_inset
641
642,
643\begin_inset Quotes sld
644\end_inset
645
646
647\series bold
648%
649\series default
650
651\begin_inset Quotes srd
652\end_inset
653
654,
655\begin_inset Quotes sld
656\end_inset
657
658
659\series bold
660&
661\series default
662
663\begin_inset Quotes srd
664\end_inset
665
666,
667\begin_inset Quotes sld
668\end_inset
669
670
671\series bold
672*
673\series default
674
675\begin_inset Quotes srd
676\end_inset
677
678,
679\begin_inset Quotes sld
680\end_inset
681
682
683\series bold
684;
685\series default
686
687\begin_inset Quotes srd
688\end_inset
689
690,
691\begin_inset Quotes sld
692\end_inset
693
694
695\series bold
696<
697\series default
698
699\begin_inset Quotes srd
700\end_inset
701
702,
703\begin_inset Quotes sld
704\end_inset
705
706
707\series bold
708>
709\series default
710
711\begin_inset Quotes srd
712\end_inset
713
714,
715\begin_inset Quotes sld
716\end_inset
717
718
719\series bold
720[
721\series default
722
723\begin_inset Quotes srd
724\end_inset
725
726,
727\begin_inset Quotes sld
728\end_inset
729
730
731\series bold
732
733\backslash
734
735\series default
736
737\begin_inset Quotes srd
738\end_inset
739
740,
741\begin_inset Quotes sld
742\end_inset
743
744
745\series bold
746]
747\series default
748
749\begin_inset Quotes srd
750\end_inset
751
752,
753\begin_inset Quotes sld
754\end_inset
755
756
757\series bold
758^
759\series default
760
761\begin_inset Quotes srd
762\end_inset
763
764,
765\begin_inset Quotes sld
766\end_inset
767
768
769\series bold
770_
771\series default
772
773\begin_inset Quotes srd
774\end_inset
775
776,
777\begin_inset Quotes sld
778\end_inset
779
780
781\series bold
782`
783\series default
784
785\begin_inset Quotes srd
786\end_inset
787
788 (single left quote),
789\begin_inset Quotes sld
790\end_inset
791
792
793\series bold
794{
795\series default
796
797\begin_inset Quotes srd
798\end_inset
799
800,
801\begin_inset Quotes sld
802\end_inset
803
804
805\series bold
806|
807\series default
808
809\begin_inset Quotes srd
810\end_inset
811
812,
813\begin_inset Quotes sld
814\end_inset
815
816
817\series bold
818}
819\series default
820
821\begin_inset Quotes srd
822\end_inset
823
824,
825\begin_inset Quotes sld
826\end_inset
827
828~
829\begin_inset Quotes srd
830\end_inset
831
832.
833\layout Section
834
835ASN.1 Constructed Types
836\layout Subsection
837
838The SEQUENCE type
839\layout Standard
840
841This is an ordered collection of other simple or constructed types.
842 The SEQUENCE constructed type resembles the C
843\begin_inset Quotes sld
844\end_inset
845
846struct
847\begin_inset Quotes srd
848\end_inset
849
850 statement.
851\layout LyX-Code
852
853Address ::= SEQUENCE {
854\layout LyX-Code
855
856 -- The apartment number may be omitted
857\layout LyX-Code
858
859 apartmentNumber NumericString OPTIONAL,
860\layout LyX-Code
861
862 streetName PrintableString,
863\layout LyX-Code
864
865 cityName PrintableString,
866\layout LyX-Code
867
868 stateName PrintableString,
869\layout LyX-Code
870
871 -- This one may be omitted too
872\layout LyX-Code
873
874 zipNo NumericString OPTIONAL
875\layout LyX-Code
876
877}
878\layout Subsection
879
880The SET type
881\layout Standard
882
883This is a collection of other simple or constructed types.
884 Ordering is not important.
885 The data may arrive in the order which is different from the order of specifica
886tion.
887 Data is encoded in the order not necessarily corresponding to the order
888 of specification.
889\layout Subsection
890
891The CHOICE type
892\layout Standard
893
894This type is just a choice between the subtypes specified in it.
895 The CHOICE type contains at most one of the subtypes specified, and it
896 is always implicitly known which choice is being decoded or encoded.
897 This one resembles the C
898\begin_inset Quotes sld
899\end_inset
900
901union
902\begin_inset Quotes srd
903\end_inset
904
905 statement.
906\layout Standard
907
908The following type defines a response code, which may be either an integer
909 code or a boolean
910\begin_inset Quotes sld
911\end_inset
912
913true
914\begin_inset Quotes srd
915\end_inset
916
917/
918\begin_inset Quotes srd
919\end_inset
920
921false
922\begin_inset Quotes srd
923\end_inset
924
925 code.
926\layout LyX-Code
927
928ResponseCode ::= CHOICE {
929\layout LyX-Code
930
931 intCode INTEGER,
932\layout LyX-Code
933
934 boolCode BOOLEAN
935\layout LyX-Code
936
937}
938\layout LyX-Code
939
940\layout Subsection
941
942The SEQUENCE OF type
943\layout Standard
944
945This one is the list (array) of simple or constructed types:
946\layout LyX-Code
947
948-- Example 1
949\layout LyX-Code
950
951ManyIntegers ::= SEQUENCE OF INTEGER
952\layout LyX-Code
953
954-- Example 2
955\layout LyX-Code
956
957ManyRectangles ::= SEQUENCE OF Rectangle
958\layout LyX-Code
959
960-- More complex example:
961\layout LyX-Code
962
963-- an array of structures defined in place.
964\layout LyX-Code
965
966ManyCircles ::= SEQUENCE OF SEQUENCE {
967\layout LyX-Code
968
969 radius INTEGER
970\layout LyX-Code
971
972 }
973\layout Subsection
974
975The SET OF type
976\layout Standard
977
978The SET OF type models the bag of structures.
979 It resembles the SEQUENCE OF type, but the order is not important: i.e.
980 the elements may arrive in the order which is not necessarily the same
981 as the in-memory order on the remote machines.
982\layout LyX-Code
983
984-- A set of structures defined elsewhere
985\layout LyX-Code
986
987SetOfApples :: SET OF Apple
988\layout LyX-Code
989
990-- Set of integers encoding the kind of a fruit
991\layout LyX-Code
992
993FruitBag ::= SET OF ENUMERATED { apple, orange }
994\layout Chapter
995
996ASN.1 Compiler Usage
997\layout Standard
998
999The purpose of the ASN.1 compiler, of which this document is part, is to
1000 convert the ASN.1 specifications to some other target language (currently,
1001 only C is supported
1002\begin_inset Foot
1003collapsed false
1004
1005\layout Standard
1006
1007C++ is
1008\begin_inset Quotes sld
1009\end_inset
1010
1011supported
1012\begin_inset Quotes srd
1013\end_inset
1014
1015 too, as long as an object-oriented approach is not a definitive factor.
1016\end_inset
1017
1018).
1019 The compiler reads the specification and emits a series of target language
1020 structures and surrounding maintenance code.
1021 For example, the C structure which may be created by compiler to represent
1022 the simple
1023\emph on
1024Rectangle
1025\emph default
1026 specification defined earlier in this document, may look like this
1027\begin_inset Foot
1028collapsed false
1029
1030\layout Standard
1031
1032
1033\emph on
1034-fnative-integers
1035\emph default
1036 compiler option is used to produce basic C
1037\emph on
1038int
1039\emph default
1040 types instead of generic INTEGER_t.
1041\end_inset
1042
1043:
1044\layout LyX-Code
1045
1046typedef struct Rectangle_s {
1047\layout LyX-Code
1048
1049 int height;
1050\layout LyX-Code
1051
1052 int width;
1053\layout LyX-Code
1054
1055} Rectangle_t;
1056\layout Standard
1057
1058This would not be of much value for such a simple specification, so the
1059 compiler goes further and actually produces the code which fills in this
1060 structure by parsing the binary
1061\begin_inset Foot
1062collapsed false
1063
1064\layout Standard
1065
1066BER, CER and DER encodings are binary.
1067 However, the XER encoding is text (XML) based.
1068\end_inset
1069
1070 data provided in some buffer.
1071 It also produces the code that takes this structure as an argument and
1072 performs structure serialization by emitting a series of bytes.
1073\layout Section
1074
1075Quick start
1076\layout Standard
1077
1078After building and installing the compiler, the asn1c command may be used
1079 to compile the ASN.1 specification
1080\begin_inset Foot
1081collapsed false
1082
1083\layout Standard
1084
1085This is probably
1086\series bold
1087not
1088\series default
1089 what you want to try out right now -- read through the rest of this chapter
1090 to find out about -P option.
1091\end_inset
1092
1093:
1094\layout LyX-Code
1095
1096asn1c
1097\emph on
1098<spec.asn1>
1099\layout Standard
1100
1101If several specifications contain interdependencies, all of them must be
1102 specified:
1103\layout LyX-Code
1104
1105asn1c
1106\emph on
1107<spec1.asn1> <spec2.asn1> ...
1108\layout Standard
1109
1110The compiler -E and -EF options are used for testing the parser and the
1111 semantic fixer, respectively.
1112 These options will instruct the compiler to dump out the parsed (and fixed)
1113 ASN.1 specification as it was "understood" by the compiler.
1114 It might might be useful to check whether a particular syntactic construction
1115 is properly supported by the compiler.
1116\layout LyX-Code
1117
1118asn1c -EF
1119\emph on
1120<spec-to-test.asn1>
1121\layout Standard
1122
1123The -P option is used to dump the compiled output on the screen instead
1124 of creating a bunch of .c and .h files on disk in the current directory.
1125 You would probably want to start with -P option instead of creating a mess
1126 in your current directory.
1127\layout Section
1128
1129Slow start
1130\layout Subsection
1131
1132Recognizing compiler output
1133\layout Standard
1134
1135After compiling, the following entities will be created in your current
1136 directory:
1137\layout Itemize
1138
1139A set of .c and .h files, generally a single pair for each type defined in
1140 the ASN.1 specifications.
1141 These files will be named similarly to the ASN.1 types (
1142\emph on
1143Rectangle.c
1144\emph default
1145 and
1146\emph on
1147Rectangle.h
1148\emph default
1149 for the specification defined in the beginning of this document).
1150\layout Itemize
1151
1152A set of helper .c and .h files which contain generic encoders, decoders and
1153 other useful routines.
1154 There will be many of them, some of them even not necessary
1155\begin_inset Foot
1156collapsed false
1157
1158\layout Standard
1159
1160Soon the compiler will be modified to emit the smallest subset of necessary
1161 files.
1162\end_inset
1163
1164, but the overall amount of code after compiling will be rather small anyway.
1165\layout Standard
1166
1167It is your responsibility to create .c file with the
1168\emph on
1169 int main()
1170\emph default
1171 routine and the Makefile (if needed).
1172 Compiler helps you with the latter by creating the Makefile.am.sample, containing
1173 the skeleton definition for the automake, should you want to use autotools.
1174\layout Standard
1175
1176In other words, after compiling the Rectangle module, you have the following
1177 set of files: { Makefile.am.sample, Rectangle.c, Rectangle.h,
1178\series bold
1179\SpecialChar \ldots{}
1180
1181\series default
1182 }, where
1183\series bold
1184
1185\begin_inset Quotes sld
1186\end_inset
1187
1188\SpecialChar \ldots{}
1189
1190\begin_inset Quotes srd
1191\end_inset
1192
1193
1194\series default
1195 stands for the set of additional
1196\begin_inset Quotes sld
1197\end_inset
1198
1199helper
1200\begin_inset Quotes srd
1201\end_inset
1202
1203 files created by the compiler.
1204 If you add the simple file with the
1205\emph on
1206int main()
1207\emph default
1208 routine, it would even be possible to compile everything with the single
1209 instruction:
1210\layout LyX-Code
1211
1212cc -o rectangle *.c # It could be
1213\emph on
1214that
1215\emph default
1216 simple
1217\begin_inset Foot
1218collapsed false
1219
1220\layout Standard
1221
1222Provided that you've also created a .c file with the
1223\emph on
1224int main()
1225\emph default
1226 routine.
1227\end_inset
1228
1229
1230\layout Subsection
1231
1232Invoking the ASN.1 helper code from the application
1233\layout Standard
1234
1235First of all, you would want to include one or more header files into your
1236 application.
1237 For the Rectangle module, including the Rectangle.h file is enough:
1238\layout LyX-Code
1239
1240#include <Rectangle.h>
1241\layout Standard
1242
1243The header files defines the C structure corresponding to the ASN.1 definition
1244 of a rectangle and the declaration of the ASN.1 type descriptor, which is
1245 used as an argument to most of the functions provided by the ASN.1 module.
1246 For example, here is the code which frees the Rectangle_t structure:
1247\layout LyX-Code
1248
1249Rectangle_t *rect = ;
1250\layout LyX-Code
1251
1252asn1_DEF_Rectangle->free_struct(&asn1_DEF_Rectangle,
1253\layout LyX-Code
1254
1255 rect, 0);
1256\layout Standard
1257
1258This code defines a
1259\emph on
1260rect
1261\emph default
1262 pointer which points to the Rectangle_t structure which needs to be freed.
1263 The second line invokes the generic free_struct routine created specifically
1264 for this Rectangle_t structure.
1265 The
1266\emph on
1267asn1_DEF_Rectangle
1268\emph default
1269 is the type descriptor, which holds a collection of generic routines to
1270 deal with the Rectangle_t structure.
1271\layout Standard
1272
1273There are several generic functions available:
1274\layout Description
1275
1276check_constraints Check that the contents of the target structure are semantical
1277ly valid and constrained to appropriate implicit or explicit subtype constraints.
1278 Please refer to Section
1279\begin_inset LatexCommand \vref{sub:Validating-the-target}
1280
1281\end_inset
1282
1283.
1284\layout Description
1285
1286ber_decoder This is the generic
1287\emph on
1288restartable
1289\begin_inset Foot
1290collapsed false
1291
1292\layout Standard
1293
1294Restartable means that if the decoder encounters the end of the buffer,
1295 it will fail, but may later be invoked again with the rest of the buffer
1296 to continue decoding.
1297\end_inset
1298
1299
1300\emph default
1301BER decoder (Basic Encoding Rules).
1302 This decoder would create and/or fill the target structure for you.
1303 Please refer to Section
1304\begin_inset LatexCommand \ref{sub:Decoding-BER}
1305
1306\end_inset
1307
1308.
1309\layout Description
1310
1311der_encoder This is the generic DER encoder (Distinguished Encoding Rules).
1312 This decoder will take the target structure and encode it into a series
1313 of bytes.
1314 Please refer to Section
1315\begin_inset LatexCommand \ref{sub:Encoding-DER}
1316
1317\end_inset
1318
1319.
1320\layout Description
1321
1322print_struct This function convert the contents of the passed target structure
1323 into human readable form.
1324 This form is not formal and cannot be converted back into the structure,
1325 but it may turn out to be useful for debugging or quick-n-dirty printing.
1326 Please refer to Section
1327\begin_inset LatexCommand \ref{sub:Printing-the-target}
1328
1329\end_inset
1330
1331.
1332\layout Description
1333
1334free_struct This is a generic disposal which frees the target structure.
1335 Please refer to Section
1336\begin_inset LatexCommand \ref{sub:Freeing-the-target}
1337
1338\end_inset
1339
1340.
1341\layout Standard
1342
1343Each of the above function takes the type descriptor (
1344\emph on
1345asn1_DEF_\SpecialChar \ldots{}
1346
1347\emph default
1348) and the target structure (
1349\emph on
1350rect
1351\emph default
1352, in the above example).
1353 The target structure is typically created by the generic BER decoder or
1354 by the application itself.
1355\layout Standard
1356
1357Here is how the buffer can be deserialized into the structure:
1358\layout LyX-Code
1359
1360Rectangle_t *
1361\layout LyX-Code
1362
1363simple_deserializer(void *buffer, size_t buf_size) {
1364\layout LyX-Code
1365
1366 Rectangle_t *rect = 0; /* Note this 0! */
1367\layout LyX-Code
1368
1369 ber_dec_rval_t rval;
1370\layout LyX-Code
1371
1372
1373\layout LyX-Code
1374
1375 rval = asn1_DEF_Rectangle->ber_decoder(
1376\layout LyX-Code
1377
1378 &asn1_DEF_Rectangle,
1379\layout LyX-Code
1380
1381 (void **)&rect,
1382\layout LyX-Code
1383
1384 buffer, buf_size,
1385\layout LyX-Code
1386
1387 0);
1388\layout LyX-Code
1389
1390
1391\layout LyX-Code
1392
1393 if(rval
1394\series bold
1395.code
1396\series default
1397 == RC_OK) {
1398\layout LyX-Code
1399
1400 return rect; /* Decoding succeeded */
1401\layout LyX-Code
1402
1403 } else {
1404\layout LyX-Code
1405
1406 asn1_DEF_Rectangle->free_struct(
1407\layout LyX-Code
1408
1409 &asn1_DEF_Rectangle, rect, 0);
1410\layout LyX-Code
1411
1412 return 0;
1413\layout LyX-Code
1414
1415 }
1416\layout LyX-Code
1417
1418}
1419\layout Standard
1420
1421The code above defines a function,
1422\emph on
1423simple_deserializer
1424\emph default
1425, which takes a buffer and its length and expected to return a pointer to
1426 the Rectangle_t structure.
1427 Inside, it tries to convert the bytes passed into the target structure
1428 (rect) using the generic BER decoder and returns the rect pointer afterwards.
1429 If the structure cannot be deserialized, it frees the memory which might
1430 be left allocated by the unfinished
1431\emph on
1432ber_decoder
1433\emph default
1434 routine and returns NULL.
1435
1436\series bold
1437This freeing is necessary
1438\series default
1439 because the ber_decoder is a restartable procedure, and may fail just because
1440 there is more data needs to be provided before decoding could be finalized.
1441 The code above obviously does not take into account the way the
1442\emph on
1443ber_decoder
1444\emph default
1445 failed, so the freeing is necessary because the part of the buffer may
1446 already be decoded into the structure by the time something goes wrong.
1447\layout Standard
1448
1449Restartable decoding is a little bit trickier: you need to provide the old
1450 target structure pointer (which might be already half-decoded) and react
1451 on RC_WMORE return code.
1452 This will be explained later in Section
1453\begin_inset LatexCommand \vref{sub:Decoding-BER}
1454
1455\end_inset
1456
1457
1458\layout Subsubsection
1459
1460
1461\begin_inset LatexCommand \label{sub:Decoding-BER}
1462
1463\end_inset
1464
1465Decoding BER
1466\layout Standard
1467
1468The Basic Encoding Rules describe the basic way how the structure can be
1469 encoded and decoded.
1470 Several other encoding rules (CER, DER) define a more restrictive versions
1471 of BER, so the generic BER parser is also capable of decoding the data
1472 encoded by CER and DER encoders.
1473 The opposite is not true.
1474\layout Standard
1475
1476The ASN.1 compiler provides the generic BER decoder which is implicitly capable
1477 of decoding BER, CER and DER encoded data.
1478\layout Standard
1479
1480The decoder is restartable (stream-oriented), which means that in case the
1481 buffer has less data than it is expected, the decoder will process whatever
1482 it is available and ask for more data to be provided.
1483 Please note that the decoder may actually process less data than it is
1484 given in the buffer, which means that you should be able to make the next
1485 buffer contain the unprocessed part of the previous buffer.
1486\layout Standard
1487
1488Suppose, you have two buffers of encoded data: 100 bytes and 200 bytes.
1489\layout Itemize
1490
1491You may concatenate these buffers and feed the BER decoder with 300 bytes
1492 of data, or
1493\layout Itemize
1494
1495You may feed it the first buffer of 100 bytes of data, realize that the
1496 ber_decoder consumed only 95 bytes from it and later feed the decoder with
1497 205 bytes buffer which consists of 5 unprocessed bytes from the first buffer
1498 and the latter 200 bytes from the second buffer.
1499\layout Standard
1500
1501This is not as convenient as it could be (like, the BER encoder would consume
1502 the whole 100 bytes and keep these 5 bytes in some temporary storage),
1503 but in case of stream-based processing it might actually be OK.
1504 Suggestions are welcome.
1505\layout Standard
1506
1507There are two ways to invoke a BER decoder.
1508 The first one is a direct reference of the type-specific decoder.
1509 This way was shown in the previous example of
1510\emph on
1511simple_deserializer
1512\emph default
1513 function.
1514 The second way is to invoke a
1515\emph on
1516ber_decode
1517\emph default
1518 function, which is just a simple wrapper of the former approach into a
1519 less wordy notation:
1520\layout LyX-Code
1521
1522rval = ber_decode(&asn1_DEF_Rectangle, (void **)&rect,
1523\layout LyX-Code
1524
1525 buffer, buf_size);
1526\layout Standard
1527
1528Note that the initial (asn1_DEF_Rectangle->ber_decoder) reference is gone,
1529 and also the last argument (0) is no longer necessary.
1530\layout Standard
1531
1532These two ways of invocations are fully equivalent.
1533\layout Standard
1534
1535The BER de
1536\emph on
1537coder
1538\emph default
1539 may fail because (
1540\emph on
1541the following RC_\SpecialChar \ldots{}
1542 codes are defined in ber_decoder.h
1543\emph default
1544):
1545\layout Itemize
1546
1547RC_WMORE: There is more data expected than it is provided (stream mode continuat
1548ion feature);
1549\layout Itemize
1550
1551RC_FAIL: General failure to decode the buffer;
1552\layout Itemize
1553
1554\SpecialChar \ldots{}
1555 other codes may be defined as well.
1556\layout Standard
1557
1558Together with the return code (.code) the ber_dec_rval_t type contains the
1559 number of bytes which is consumed from the buffer.
1560 In the previous hypothetical example of two buffers (of 100 and 200 bytes),
1561 the first call to ber_decode() would return with .code = RC_WMORE and .consumed
1562 = 95.
1563 The .consumed field of the BER decoder return value is
1564\series bold
1565always
1566\series default
1567 valid, even if the decoder succeeds or fails with any other return code.
1568\layout Standard
1569
1570Please look into ber_decoder.h for the precise definition of ber_decode()
1571 and related types.
1572\layout Subsubsection
1573
1574
1575\begin_inset LatexCommand \label{sub:Encoding-DER}
1576
1577\end_inset
1578
1579Encoding DER
1580\layout Standard
1581
1582The Distinguished Encoding Rules is the variant of BER encoding rules which
1583 is oriented on representing the structures with length known beforehand.
1584 This is probably exactly how you want to encode: either after a BER decoding
1585 or after a manual fill-up, the target structure contains the data which
1586 size is implicitly known before encoding.
1587 The DER encoding is used, for example, to encode X.509 certificates.
1588\layout Standard
1589
1590As with BER decoder, the DER encoder may be invoked either directly from
1591 the ASN.1 type descriptor (asn1_DEF_Rectangle) or from the stand-alone function,
1592 which is somewhat simpler:
1593\layout LyX-Code
1594
1595/*
1596\layout LyX-Code
1597
1598 * This is a custom function which writes the
1599\layout LyX-Code
1600
1601 * encoded output into some FILE stream.
1602\layout LyX-Code
1603
1604 */
1605\layout LyX-Code
1606
1607int _write_stream(void *buffer, size_t size, void *app_key) {
1608\layout LyX-Code
1609
1610 FILE *ostream = app_key;
1611\layout LyX-Code
1612
1613 size_t wrote;
1614\layout LyX-Code
1615
1616
1617\layout LyX-Code
1618
1619 wrote = fwrite(buffer, 1, size, ostream);
1620\layout LyX-Code
1621
1622
1623\layout LyX-Code
1624
1625 return (wrote == size) ? 0 : -1;
1626\layout LyX-Code
1627
1628}
1629\layout LyX-Code
1630
1631
1632\layout LyX-Code
1633
1634/*
1635\layout LyX-Code
1636
1637 * This is the serializer itself,
1638\layout LyX-Code
1639
1640 * it supplies the DER encoder with the
1641\layout LyX-Code
1642
1643 * pointer to the custom output function.
1644\layout LyX-Code
1645
1646 */
1647\layout LyX-Code
1648
1649ssize_t
1650\layout LyX-Code
1651
1652simple_serializer(FILE *ostream, Rectangle_t *rect) {
1653\layout LyX-Code
1654
1655 der_enc_rval_t rval; /* Return value */
1656\layout LyX-Code
1657
1658
1659\layout LyX-Code
1660
1661 rval = der_encode(&asn1_DEF_Rect, rect,
1662\layout LyX-Code
1663
1664 _write_stream, ostream);
1665\layout LyX-Code
1666
1667 if(rval
1668\series bold
1669.encoded
1670\series default
1671 == -1) {
1672\layout LyX-Code
1673
1674 /*
1675\layout LyX-Code
1676
1677 * Failure to encode the rectangle data.
1678\layout LyX-Code
1679
1680 */
1681\layout LyX-Code
1682
1683 fprintf(stderr,
1684\begin_inset Quotes sld
1685\end_inset
1686
1687Cannot encode %s: %s
1688\backslash
1689n
1690\begin_inset Quotes srd
1691\end_inset
1692
1693,
1694\layout LyX-Code
1695
1696 rval
1697\series bold
1698.failed_type
1699\series default
1700->name,
1701\layout LyX-Code
1702
1703 strerror(errno));
1704\layout LyX-Code
1705
1706 return -1;
1707\layout LyX-Code
1708
1709 } else {
1710\layout LyX-Code
1711
1712 /* Return the number of bytes */
1713\layout LyX-Code
1714
1715 return rval.encoded;
1716\layout LyX-Code
1717
1718 }
1719\layout LyX-Code
1720
1721}
1722\layout Standard
1723
1724As you see, the DER encoder does not write into some sort of buffer or something.
1725 It just invokes the custom function (possible, multiple times) which would
1726 save the data into appropriate storage.
1727 The optional argument
1728\emph on
1729app_key
1730\emph default
1731 is opaque for the DER encoder code and just used by
1732\emph on
1733_write_stream()
1734\emph default
1735 as the pointer to the appropriate output stream to be used.
1736\layout Standard
1737
1738If the custom write function is not given (passed as 0), then the DER encoder
1739 will essentially do the same thing (i.e., encode the data) but no callbacks
1740 will be invoked (so the data goes nowhere).
1741 It may prove useful to determine the size of the structure's encoding before
1742 actually doing the encoding
1743\begin_inset Foot
1744collapsed false
1745
1746\layout Standard
1747
1748It is actually faster too: the encoder might skip over some computations
1749 which aren't important for the size determination.
1750\end_inset
1751
1752.
1753\layout Standard
1754
1755Please look into der_encoder.h for the precise definition of der_encode()
1756 and related types.
1757\layout Subsubsection
1758
1759
1760\begin_inset LatexCommand \label{sub:Validating-the-target}
1761
1762\end_inset
1763
1764Validating the target structure
1765\layout Standard
1766
1767Sometimes the target structure needs to be validated.
1768 For example, if the structure was created by the application (as opposed
1769 to being decoded from some external source), some important information
1770 required by the ASN.1 specification might be missing.
1771 On the other hand, the successful decoding of the data from some external
1772 source does not necessarily mean that the data is fully valid either.
1773 It might well be the case that the specification describes some subtype
1774 constraints that were not taken into account during decoding, and it would
1775 actually be useful to perform the last check when the data is ready to
1776 be encoded or when the data has just been decoded to ensure its validity
1777 according to some stricter rules.
1778\layout Standard
1779
1780The asn_check_constraints() function checks the type for various implicit
1781 and explicit constraints.
1782 It is recommended to use asn_check_constraints() function after each decoding
1783 and before each encoding.
1784\layout Standard
1785
1786Please look into constraints.h for the precise definition of asn_check_constraint
1787s() and related types.
1788\layout Subsubsection
1789
1790
1791\begin_inset LatexCommand \label{sub:Printing-the-target}
1792
1793\end_inset
1794
1795Printing the target structure
1796\layout Standard
1797
1798There are two ways to print the target structure: either invoke the print_struct
1799 member of the ASN.1 type descriptor, or using the asn_fprint() function,
1800 which is a simpler wrapper of the former:
1801\layout LyX-Code
1802
1803asn_fprint(stdout, &asn1_DEF_Rectangle, rect);
1804\layout Standard
1805
1806Please look into constr_TYPE.h for the precise definition of asn_fprint()
1807 and related types.
1808\layout Subsubsection
1809
1810
1811\begin_inset LatexCommand \label{sub:Freeing-the-target}
1812
1813\end_inset
1814
1815Freeing the target structure
1816\layout Standard
1817
1818Freeing the structure is slightly more complex than it may seem to.
1819 When the ASN.1 structure is freed, all the members of the structure and
1820 their submembers etc etc are recursively freed too.
1821 But it might not be feasible to free the structure itself.
1822 Consider the following case:
1823\layout LyX-Code
1824
1825struct my_figure { /* The custom structure */
1826\layout LyX-Code
1827
1828 int flags; /* <some custom member> */
1829\layout LyX-Code
1830
1831 /* The type is generated by the ASN.1 compiler */
1832\layout LyX-Code
1833
1834
1835\emph on
1836Rectangle_t rect;
1837\layout LyX-Code
1838
1839 /* other members of the structure */
1840\layout LyX-Code
1841
1842};
1843\layout Standard
1844
1845In this example, the application programmer defined a custom structure with
1846 one ASN.1-derived member (rect).
1847 This member is not a reference to the Rectangle_t, but an in-place inclusion
1848 of the Rectangle_t structure.
1849 If the freeing is necessary, the usual procedure of freeing everything
1850 must not be applied to the &rect pointer itself, because it does not point
1851 to the memory block directly allocated by memory allocation routine, but
1852 instead lies within such a block allocated for my_figure structure.
1853\layout Standard
1854
1855To solve this problem, the free_struct routine has the additional argument
1856 (besides the intuitive type descriptor and target structure pointers),
1857 which is the flag specifying whether the outer pointer itself must be freed
1858 (0, default) or it should be left intact (non-zero value).
1859\layout LyX-Code
1860
1861/* Rectangle_t is defined within my_figure */
1862\layout LyX-Code
1863
1864struct my_figure *mf =
1865\series bold
1866...
1867\series default
1868;
1869\layout LyX-Code
1870
1871/*
1872\layout LyX-Code
1873
1874 * Freeing the Rectangle_td
1875\layout LyX-Code
1876
1877 * without freeing the mf->rect pointer
1878\layout LyX-Code
1879
1880 */
1881\layout LyX-Code
1882
1883asn1_DEF_Rectangle->free_struct(
1884\layout LyX-Code
1885
1886 &asn1_DEF_Rectangle, &mf->rect,
1887\emph on
18881
1889\emph default
1890 /* !free */);
1891\layout LyX-Code
1892
1893
1894\layout LyX-Code
1895
1896/* Rectangle_t is a stand-alone pointer */
1897\layout LyX-Code
1898
1899Rectangle_t *rect =
1900\series bold
1901...
1902\series default
1903;
1904\layout LyX-Code
1905
1906/*
1907\layout LyX-Code
1908
1909 * Freeing the Rectangle_t
1910\layout LyX-Code
1911
1912 * and freeing the rect pointer
1913\layout LyX-Code
1914
1915 */
1916\layout LyX-Code
1917
1918asn1_DEF_Rectangle->free_struct(
1919\layout LyX-Code
1920
1921 &asn1_DEF_Rectangle, rect,
1922\emph on
19230
1924\emph default
1925 /* free the pointer too */);
1926\layout Standard
1927
1928It is safe to invoke the
1929\emph on
1930free_struct
1931\emph default
1932 function with the target structure pointer set to 0 (NULL), the function
1933 will do nothing.
1934\layout Bibliography
1935\bibitem [Dub00]{Dub00}
1936
1937Olivier Dubuisson --
1938\emph on
1939ASN.1 Communication between heterogeneous systems
1940\emph default
1941 -- Morgan Kaufmann Publishers, 2000.
1942
1943\begin_inset LatexCommand \htmlurl{http://asn1.elibel.tm.fr/en/book/}
1944
1945\end_inset
1946
1947.
1948 ISBN:0-12-6333361-0.
1949\layout Bibliography
1950\bibitem [ITU-T/ASN.1]{ITU-T/ASN.1}
1951
1952ITU-T Study Group 17 -- Languages for Telecommunication Systems
1953\begin_inset LatexCommand \url{http://www.itu.int/ITU-T/studygroups/com17/languages/}
1954
1955\end_inset
1956
1957
1958\the_end