1 /**
2     This file contains the constants and $(D enum)s used by the library.
3 
4     Authors:
5     $(UL
6         $(LI $(PERSON Jonathan M. Wilbur, jonathan@wilbur.space, http://jonathan.wilbur.space))
7     )
8     Copyright: Copyright (C) Jonathan M. Wilbur
9     License: $(LINK https://mit-license.org/, MIT License)
10     Standards:
11         $(LINK https://www.itu.int/rec/T-REC-X.680/en, X.680 - Abstract Syntax Notation One (ASN.1))
12     See_Also:
13         $(LINK https://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One, The Wikipedia Page on ASN.1)
14         $(LINK https://www.strozhevsky.com/free_docs/asn1_in_simple_words.pdf, ASN.1 By Simple Words)
15         $(LINK http://www.oss.com/asn1/resources/books-whitepapers-pubs/dubuisson-asn1-book.PDF, ASN.1: Communication Between Heterogeneous Systems)
16 */
17 module asn1.constants;
18 
19 /*
20     Done to avoid the problems associated with CVE-2009-0789. I don't know this
21     to be a bug with this code, but it is better to play on the safe side.
22     Remove at your own peril.
23 */
24 static assert(!(long.sizeof < (void *).sizeof));
25 
26 debug (asn1)
27 {
28     public import std.stdio : write, writefln, writeln;
29 }
30 
31 version (unittest)
32 {
33     public import core.exception : AssertError, RangeError;
34     public import std.exception : assertNotThrown, assertThrown;
35     public import std.math : approxEqual;
36     public import std.stdio : write, writefln, writeln;
37 }
38 
39 // Check fundamental assumptions of this library.
40 static assert(char.sizeof == 1u);
41 static assert(wchar.sizeof == 2u);
42 static assert(dchar.sizeof == 4u);
43 static assert(double.sizeof > float.sizeof);
44 static assert(real.sizeof >= double.sizeof);
45 
46 ///
47 public alias ASN1Exception = AbstractSyntaxNotation1Exception;
48 /// A Generic Exception from which all other ASN.1 Exceptions will inherit.
49 class AbstractSyntaxNotation1Exception : Exception
50 {
51     private import std.exception : basicExceptionCtors;
52     mixin basicExceptionCtors;
53 }
54 
55 ///
56 public alias ASN1TagClass = AbstractSyntaxNotation1TagClass;
57 ///
58 immutable public
59 enum AbstractSyntaxNotation1TagClass : ubyte
60 {
61     universal = 0b00000000u, // Native to ASN.1
62     application = 0b01000000u, // Only valid for one specific application
63     contextSpecific = 0b10000000u, // Specific to a sequence, set, or choice
64     privatelyDefined = 0b11000000u// Defined in private specifications
65 }
66 
67 ///
68 public alias ASN1Construction = AbstractSyntaxNotation1Construction;
69 ///
70 immutable public
71 enum AbstractSyntaxNotation1Construction : ubyte
72 {
73     primitive = 0b00000000u, // The content octets directly encode the element value
74     constructed = 0b00100000u // The content octets contain 0, 1, or more element encodings
75 }
76 
77 ///
78 public alias ASN1UniversalType = AbstractSyntaxNotation1UniversalType;
79 /**
80     The data types, as well as their permitted construction and numeric
81     identifiers, according to the
82     $(LINK https://www.itu.int/en/pages/default.aspx,
83     International Telecommunications Union)'s
84     $(LINK http://www.itu.int/rec/T-REC-X.690/en, X.690 - ASN.1 encoding rules)
85 
86     $(TABLE
87         $(TR $(TH Type)                 $(TH Construction)      $(TH Hexadecimal Value))
88         $(TR $(TD End-of-Content)       $(TD Primitive)         $(TD 0x00))
89         $(TR $(TD BOOLEAN)	            $(TD Primitive)         $(TD 0x01))
90         $(TR $(TD INTEGER)	            $(TD Primitive)         $(TD 0x02))
91         $(TR $(TD BIT STRING)           $(TD Both)              $(TD 0x03))
92         $(TR $(TD OCTET STRING)         $(TD Both)              $(TD 0x04))
93         $(TR $(TD NULL)                 $(TD Primitive)         $(TD 0x05))
94         $(TR $(TD OBJECT IDENTIFIER)	$(TD Primitive)         $(TD 0x06))
95         $(TR $(TD Object Descriptor)    $(TD Both)              $(TD 0x07))
96         $(TR $(TD EXTERNAL)	            $(TD Constructed)       $(TD 0x08))
97         $(TR $(TD REAL)            	    $(TD Primitive)         $(TD 0x09))
98         $(TR $(TD ENUMERATED)	        $(TD Primitive)         $(TD 0x0A))
99         $(TR $(TD EmbeddedPDV)	        $(TD Constructed)       $(TD 0x0B))
100         $(TR $(TD UTF8String)	        $(TD Both)              $(TD 0x0C))
101         $(TR $(TD RELATIVE-OID)	        $(TD Primitive)         $(TD 0x0D))
102         $(TR $(TD SEQUENCE)	            $(TD Constructed)       $(TD 0x10))
103         $(TR $(TD SET)	                $(TD Constructed)       $(TD 0x11))
104         $(TR $(TD NumericString)	    $(TD Both)              $(TD 0x12))
105         $(TR $(TD PrintableString)	    $(TD Both)              $(TD 0x13))
106         $(TR $(TD T61String)	        $(TD Both)              $(TD 0x14))
107         $(TR $(TD VideotexString)	    $(TD Both)              $(TD 0x15))
108         $(TR $(TD IA5String)	        $(TD Both)              $(TD 0x16))
109         $(TR $(TD UTCTime)	            $(TD Both)              $(TD 0x17))
110         $(TR $(TD GeneralizedTime)	    $(TD Both)              $(TD 0x18))
111         $(TR $(TD GraphicString)	    $(TD Both)              $(TD 0x19))
112         $(TR $(TD VisibleString)	    $(TD Both)              $(TD 0x1A))
113         $(TR $(TD GeneralString)	    $(TD Both)              $(TD 0x1B))
114         $(TR $(TD UniversalString)	    $(TD Both)              $(TD 0x1C))
115         $(TR $(TD CHARACTER STRING)	    $(TD Both)              $(TD 0x1D))
116         $(TR $(TD BMPString)	        $(TD Both)              $(TD 0x1E))
117     )
118 */
119 immutable public
120 enum AbstractSyntaxNotation1UniversalType : ubyte
121 {
122     endOfContent = 0x00u,
123     eoc = endOfContent,
124     boolean = 0x01u,
125     integer = 0x02u,
126     bitString = 0x03u,
127     octetString = 0x04u,
128     nill = 0x05u,
129     objectIdentifier = 0x06u,
130     oid = objectIdentifier,
131     objectDescriptor = 0x07u,
132     external = 0x08u,
133     ext = external,
134     realNumber = 0x09u,
135     enumerated = 0x0Au,
136     embeddedPresentationDataValue = 0x0Bu,
137     embeddedPDV = embeddedPresentationDataValue,
138     pdv = embeddedPresentationDataValue,
139     unicodeTransformationFormat8String = 0x0Cu,
140     utf8String = unicodeTransformationFormat8String,
141     utf8 = unicodeTransformationFormat8String,
142     relativeObjectIdentifier = 0x0Du,
143     relativeOID = relativeObjectIdentifier,
144     roid = relativeObjectIdentifier,
145     reserved14 = 0x0Eu,
146     reserved15 = 0x0Fu,
147     sequence = 0x10u,
148     set = 0x11u,
149     numericString = 0x12u,
150     numeric = numericString,
151     printableString = 0x13u,
152     printable = printableString,
153     teletexString = 0x14u,
154     t61String = teletexString,
155     videotexString = 0x15u,
156     internationalAlphabetNumber5String = 0x16u,
157     ia5String = internationalAlphabetNumber5String,
158     coordinatedUniversalTime = 0x17u,
159     utcTime = coordinatedUniversalTime,
160     generalizedTime = 0x18u,
161     graphicString = 0x19u,
162     graphic = graphicString,
163     visibleString = 0x1Au,
164     visible = visibleString,
165     generalString = 0x1Bu,
166     general = generalString,
167     universalString = 0x1Cu,
168     universal = universalString,
169     characterString = 0x1Du,
170     basicMultilingualPlaneString = 0x1Eu,
171     bmpString = basicMultilingualPlaneString
172 }
173 
174 ///
175 public alias ASN1LengthEncoding = AbstractSyntaxNotation1LengthEncoding;
176 ///
177 public
178 enum AbstractSyntaxNotation1LengthEncoding : ubyte
179 {
180     definiteShort = 0b00000000u, // Least significant seven bits of length octet encode content length of 0 - 127 bytes
181     indefinite = 0b10000000u, // Content ends when two endOfContent bytes are encountered.
182     definiteLong = 0b10000001u, // Least significant seven bits of length octet encode how many more length octets
183     reserved = 0b11111111u
184 }
185 
186 ///
187 public alias ASN1RealEncodingBase = AbstractSyntaxNotation1RealEncodingBase;
188 ///
189 immutable public
190 enum AbstractSyntaxNotation1RealEncodingBase : ubyte
191 {
192     base2 = 0x02u,
193     base8 = 0x08u,
194     base10 = 0x0Au,
195     base16 = 0x10u
196 }
197 
198 ///
199 public alias ASN1RealEncodingScale = AbstractSyntaxNotation1RealEncodingScale;
200 ///
201 immutable public
202 enum AbstractSyntaxNotation1RealEncodingScale : ubyte
203 {
204     scale0 = 0x00u,
205     scale1 = 0x01u,
206     scale2 = 0x02u,
207     scale3 = 0x03u
208 }
209 
210 ///
211 public alias ASN1RealExponentEncoding = AbstractSyntaxNotation1RealExponentEncoding;
212 ///
213 immutable public
214 enum AbstractSyntaxNotation1RealExponentEncoding : ubyte
215 {
216     followingOctet = 0b00000000u,
217     following2Octets = 0b00000001u,
218     following3Octets = 0b00000010u,
219     complicated = 0b00000011u // Just calling it as I see it.
220 }
221 
222 ///
223 public alias ASN1SpecialRealValue = AbstractSyntaxNotation1SpecialRealValue;
224 /**
225     Special values for REALs, as assigned in section 8.5.9 of X.690.
226 
227     Note that NOT-A-NUMBER and minus zero were added in the 2015 version.
228 */
229 immutable public
230 enum AbstractSyntaxNotation1SpecialRealValue : ubyte
231 {
232     plusInfinity = 0b01000000u,
233     minusInfinity = 0b01000001u,
234     notANumber = 0b01000010u,
235     minusZero = 0b01000011u
236 }
237 
238 ///
239 public alias ASN1Base10RealNumericalRepresentation = AbstractSyntaxNotation1Base10RealNumericalRepresentation;
240 /**
241     The standardized string representations of floating point numbers, as
242     specified in $(LINK https://www.iso.org/standard/12285.html, ISO 6093).
243 
244     $(TABLE
245         $(TR $(TH Representation) $(TH Description) $(TH Examples))
246         $(TR $(TD NR1) $(TD Implicit decimal point) $(TD "3", "-1", "+1000"))
247         $(TR $(TD NR2) $(TD Explicit decimal) $(TD "3.0", "-1.3", "-.3"))
248         $(TR $(TD NR3) $(TD Explicit exponent) $(TD "3.0E1", "123E+100"))
249     )
250 
251     Citations:
252         Dubuisson, Olivier. “Character String Types.” ASN.1:
253             Communication between Heterogeneous Systems, Morgan
254             Kaufmann, 2001, p. 143.
255 */
256 immutable public
257 enum AbstractSyntaxNotation1Base10RealNumericalRepresentation : ubyte
258 {
259     nr1 = 0b00000001u,
260     nr2 = 0b00000010u,
261     nr3 = 0b00000011
262 }
263 
264 /// The acceptable characters for a NumericString
265 immutable public string numericStringCharacters = "0123456789 ";
266 
267 /**
268     The acceptable characters for a printableString.
269 
270     The sorting of letters below is a slight optimization:
271     they are sorted in order of decreasing frequency in the English
272     language, so that canFind will usually have to iterate through
273     fewer letters before finding a match.
274 */
275 immutable public string printableStringCharacters =
276     "etaoinsrhdlucmfywgpbvkxqjzETAOINSRHDLUCMFYWGPBVKXQJZ0123456789 '()+,-./:=?";