FreeRDP
asn1.h
1 
20 #ifndef WINPR_ASN1_H_
21 #define WINPR_ASN1_H_
22 
23 #include <winpr/winpr.h>
24 #include <winpr/wtypes.h>
25 #include <winpr/stream.h>
26 
27 #define ER_TAG_MASK 0x1F
28 
29 enum
30 {
31  ER_TAG_BOOLEAN = 0x01,
32  ER_TAG_INTEGER = 0x02,
33  ER_TAG_BIT_STRING = 0x03,
34  ER_TAG_OCTET_STRING = 0x04,
35  ER_TAG_NULL = 0x05,
36  ER_TAG_OBJECT_IDENTIFIER = 0x06,
37  ER_TAG_ENUMERATED = 0x0A,
38  ER_TAG_UTF8STRING = 0x0C,
39  ER_TAG_PRINTABLE_STRING = 0x13,
40  ER_TAG_IA5STRING = 0x16,
41  ER_TAG_UTCTIME = 0x17,
42  ER_TAG_GENERAL_STRING = 0x1B,
43  ER_TAG_GENERALIZED_TIME = 0x18,
44 
45  ER_TAG_APP = 0x60,
46  ER_TAG_SEQUENCE = 0x30,
47  ER_TAG_SEQUENCE_OF = 0x30,
48  ER_TAG_SET = 0x31,
49  ER_TAG_SET_OF = 0x31,
50 
51  ER_TAG_CONTEXTUAL = 0xA0
52 };
53 
55 typedef enum
56 {
57  WINPR_ASN1_BER,
58  WINPR_ASN1_DER
59 } WinPrAsn1EncodingRule;
60 
61 typedef struct WinPrAsn1Encoder WinPrAsn1Encoder;
62 
64 {
65  WinPrAsn1EncodingRule encoding;
66  wStream source;
67 };
68 
69 typedef struct WinPrAsn1Decoder WinPrAsn1Decoder;
70 
71 typedef BYTE WinPrAsn1_tag;
72 typedef BYTE WinPrAsn1_tagId;
73 typedef BOOL WinPrAsn1_BOOL;
74 typedef INT32 WinPrAsn1_INTEGER;
75 typedef INT32 WinPrAsn1_ENUMERATED;
76 typedef char* WinPrAsn1_STRING;
77 typedef char* WinPrAsn1_IA5STRING;
78 typedef struct
79 {
80  size_t len;
81  BYTE* data;
83 
86 
87 typedef struct
88 {
89  UINT16 year;
90  UINT8 month;
91  UINT8 day;
92  UINT8 hour;
93  UINT8 minute;
94  UINT8 second;
95  char tz;
97 
98 #ifdef __cplusplus
99 extern "C"
100 {
101 #endif /* __cplusplus */
102 
103  WINPR_API void WinPrAsn1FreeOID(WinPrAsn1_OID* poid);
104  WINPR_API void WinPrAsn1FreeOctetString(WinPrAsn1_OctetString* octets);
105 
106  /* decoder functions */
107 
108  WINPR_API void WinPrAsn1Decoder_Init(WinPrAsn1Decoder* dec, WinPrAsn1EncodingRule encoding,
109  wStream* source);
110  WINPR_API void WinPrAsn1Decoder_InitMem(WinPrAsn1Decoder* dec, WinPrAsn1EncodingRule encoding,
111  const BYTE* source, size_t len);
112 
113  WINPR_API BOOL WinPrAsn1DecPeekTag(WinPrAsn1Decoder* dec, WinPrAsn1_tag* tag);
114  WINPR_API size_t WinPrAsn1DecReadTagAndLen(WinPrAsn1Decoder* dec, WinPrAsn1_tag* tag,
115  size_t* len);
116  WINPR_API size_t WinPrAsn1DecPeekTagAndLen(WinPrAsn1Decoder* dec, WinPrAsn1_tag* tag,
117  size_t* len);
118  WINPR_API size_t WinPrAsn1DecReadTagLenValue(WinPrAsn1Decoder* dec, WinPrAsn1_tag* tag,
119  size_t* len, WinPrAsn1Decoder* value);
120  WINPR_API size_t WinPrAsn1DecReadBoolean(WinPrAsn1Decoder* dec, WinPrAsn1_BOOL* target);
121  WINPR_API size_t WinPrAsn1DecReadInteger(WinPrAsn1Decoder* dec, WinPrAsn1_INTEGER* target);
122  WINPR_API size_t WinPrAsn1DecReadEnumerated(WinPrAsn1Decoder* dec,
123  WinPrAsn1_ENUMERATED* target);
124  WINPR_API size_t WinPrAsn1DecReadOID(WinPrAsn1Decoder* dec, WinPrAsn1_OID* target,
125  BOOL allocate);
126  WINPR_API size_t WinPrAsn1DecReadOctetString(WinPrAsn1Decoder* dec,
127  WinPrAsn1_OctetString* target, BOOL allocate);
128  WINPR_API size_t WinPrAsn1DecReadIA5String(WinPrAsn1Decoder* dec, WinPrAsn1_IA5STRING* target);
129  WINPR_API size_t WinPrAsn1DecReadGeneralString(WinPrAsn1Decoder* dec, WinPrAsn1_STRING* target);
130  WINPR_API size_t WinPrAsn1DecReadUtcTime(WinPrAsn1Decoder* dec, WinPrAsn1_UTCTIME* target);
131  WINPR_API size_t WinPrAsn1DecReadNull(WinPrAsn1Decoder* dec);
132 
133  WINPR_API size_t WinPrAsn1DecReadApp(WinPrAsn1Decoder* dec, WinPrAsn1_tagId* tagId,
134  WinPrAsn1Decoder* setDec);
135  WINPR_API size_t WinPrAsn1DecReadSequence(WinPrAsn1Decoder* dec, WinPrAsn1Decoder* seqDec);
136  WINPR_API size_t WinPrAsn1DecReadSet(WinPrAsn1Decoder* dec, WinPrAsn1Decoder* setDec);
137 
138  WINPR_API size_t WinPrAsn1DecReadContextualTag(WinPrAsn1Decoder* dec, WinPrAsn1_tagId* tagId,
139  WinPrAsn1Decoder* ctxtDec);
140  WINPR_API size_t WinPrAsn1DecPeekContextualTag(WinPrAsn1Decoder* dec, WinPrAsn1_tagId* tagId,
141  WinPrAsn1Decoder* ctxtDec);
142 
143  WINPR_API size_t WinPrAsn1DecReadContextualBool(WinPrAsn1Decoder* dec, WinPrAsn1_tagId tagId,
144  BOOL* error, WinPrAsn1_BOOL* target);
145  WINPR_API size_t WinPrAsn1DecReadContextualInteger(WinPrAsn1Decoder* dec, WinPrAsn1_tagId tagId,
146  BOOL* error, WinPrAsn1_INTEGER* target);
147  WINPR_API size_t WinPrAsn1DecReadContextualOID(WinPrAsn1Decoder* dec, WinPrAsn1_tagId tagId,
148  BOOL* error, WinPrAsn1_OID* target,
149  BOOL allocate);
150  WINPR_API size_t WinPrAsn1DecReadContextualOctetString(WinPrAsn1Decoder* dec,
151  WinPrAsn1_tagId tagId, BOOL* error,
152  WinPrAsn1_OctetString* target,
153  BOOL allocate);
154  WINPR_API size_t WinPrAsn1DecReadContextualSequence(WinPrAsn1Decoder* dec,
155  WinPrAsn1_tagId tagId, BOOL* error,
156  WinPrAsn1Decoder* target);
157  WINPR_API wStream WinPrAsn1DecGetStream(WinPrAsn1Decoder* dec);
158 
159  /* encoder functions */
160 
161  WINPR_API void WinPrAsn1Encoder_Free(WinPrAsn1Encoder** penc);
162  WINPR_API WinPrAsn1Encoder* WinPrAsn1Encoder_New(WinPrAsn1EncodingRule encoding);
163 
164  WINPR_API void WinPrAsn1Encoder_Reset(WinPrAsn1Encoder* enc);
165 
166  WINPR_API BOOL WinPrAsn1EncAppContainer(WinPrAsn1Encoder* enc, WinPrAsn1_tagId tagId);
167  WINPR_API BOOL WinPrAsn1EncSeqContainer(WinPrAsn1Encoder* enc);
168  WINPR_API BOOL WinPrAsn1EncContextualSeqContainer(WinPrAsn1Encoder* enc, WinPrAsn1_tagId tagId);
169  WINPR_API BOOL WinPrAsn1EncSetContainer(WinPrAsn1Encoder* enc);
170  WINPR_API BOOL WinPrAsn1EncContextualSetContainer(WinPrAsn1Encoder* enc, WinPrAsn1_tagId tagId);
171  WINPR_API BOOL WinPrAsn1EncContextualContainer(WinPrAsn1Encoder* enc, WinPrAsn1_tagId tagId);
172  WINPR_API BOOL WinPrAsn1EncOctetStringContainer(WinPrAsn1Encoder* enc);
173  WINPR_API BOOL WinPrAsn1EncContextualOctetStringContainer(WinPrAsn1Encoder* enc,
174  WinPrAsn1_tagId tagId);
175  WINPR_API size_t WinPrAsn1EncEndContainer(WinPrAsn1Encoder* enc);
176 
177  WINPR_API size_t WinPrAsn1EncRawContent(WinPrAsn1Encoder* enc, const WinPrAsn1_MemoryChunk* c);
178  WINPR_API size_t WinPrAsn1EncContextualRawContent(WinPrAsn1Encoder* enc, WinPrAsn1_tagId tagId,
179  const WinPrAsn1_MemoryChunk* c);
180  WINPR_API size_t WinPrAsn1EncInteger(WinPrAsn1Encoder* enc, WinPrAsn1_INTEGER integer);
181  WINPR_API size_t WinPrAsn1EncContextualInteger(WinPrAsn1Encoder* enc, WinPrAsn1_tagId tagId,
182  WinPrAsn1_INTEGER integer);
183  WINPR_API size_t WinPrAsn1EncBoolean(WinPrAsn1Encoder* enc, WinPrAsn1_BOOL b);
184  WINPR_API size_t WinPrAsn1EncContextualBoolean(WinPrAsn1Encoder* enc, WinPrAsn1_tagId tagId,
185  WinPrAsn1_BOOL b);
186  WINPR_API size_t WinPrAsn1EncEnumerated(WinPrAsn1Encoder* enc, WinPrAsn1_ENUMERATED e);
187  WINPR_API size_t WinPrAsn1EncContextualEnumerated(WinPrAsn1Encoder* enc, WinPrAsn1_tagId tagId,
188  WinPrAsn1_ENUMERATED e);
189 
190  WINPR_API size_t WinPrAsn1EncOID(WinPrAsn1Encoder* enc, const WinPrAsn1_OID* oid);
191  WINPR_API size_t WinPrAsn1EncContextualOID(WinPrAsn1Encoder* enc, WinPrAsn1_tagId tagId,
192  const WinPrAsn1_OID* oid);
193  WINPR_API size_t WinPrAsn1EncOctetString(WinPrAsn1Encoder* enc,
194  const WinPrAsn1_OctetString* oid);
195  WINPR_API size_t WinPrAsn1EncContextualOctetString(WinPrAsn1Encoder* enc, WinPrAsn1_tagId tagId,
196  const WinPrAsn1_OctetString* oid);
197  WINPR_API size_t WinPrAsn1EncIA5String(WinPrAsn1Encoder* enc, WinPrAsn1_IA5STRING ia5);
198  WINPR_API size_t WinPrAsn1EncGeneralString(WinPrAsn1Encoder* enc, WinPrAsn1_STRING str);
199  WINPR_API size_t WinPrAsn1EncContextualIA5String(WinPrAsn1Encoder* enc, WinPrAsn1_tagId tagId,
200  WinPrAsn1_IA5STRING ia5);
201  WINPR_API size_t WinPrAsn1EncUtcTime(WinPrAsn1Encoder* enc, const WinPrAsn1_UTCTIME* utc);
202  WINPR_API size_t WinPrAsn1EncContextualUtcTime(WinPrAsn1Encoder* enc, WinPrAsn1_tagId tagId,
203  const WinPrAsn1_UTCTIME* utc);
204 
205  WINPR_API BOOL WinPrAsn1EncStreamSize(WinPrAsn1Encoder* enc, size_t* s);
206  WINPR_API BOOL WinPrAsn1EncToStream(WinPrAsn1Encoder* enc, wStream* s);
207 
208 #ifdef __cplusplus
209 }
210 #endif /* __cplusplus */
211 
212 #endif /* WINPR_ASN1_H_ */