21 #include <freerdp/config.h>
23 #include <winpr/crt.h>
24 #include <winpr/assert.h>
26 #include <freerdp/crypto/er.h>
27 #include <freerdp/crypto/ber.h>
28 #include <freerdp/crypto/der.h>
30 void er_read_length(
wStream* s,
int* length)
34 Stream_Read_UINT8(s,
byte);
48 Stream_Read_UINT8(s, *length);
50 Stream_Read_UINT16_BE(s, *length);
64 int er_write_length(
wStream* s,
int length, BOOL flag)
66 WINPR_ASSERT(length >= 0);
68 return der_write_length(s, length);
70 return (
int)ber_write_length(s, (
size_t)length);
73 int _er_skip_length(
int length)
81 int er_get_content_length(
int length)
83 if (length - 1 > 0x7F)
96 BOOL er_read_universal_tag(
wStream* s, BYTE tag, BOOL pc)
100 Stream_Read_UINT8(s,
byte);
102 if (
byte != (ER_CLASS_UNIV | ER_PC(pc) | (ER_TAG_MASK & tag)))
115 void er_write_universal_tag(
wStream* s, BYTE tag, BOOL pc)
117 Stream_Write_UINT8(s, (ER_CLASS_UNIV | ER_PC(pc)) | (ER_TAG_MASK & tag));
127 BOOL er_read_application_tag(
wStream* s, BYTE tag,
int* length)
133 Stream_Read_UINT8(s,
byte);
135 if (
byte != ((ER_CLASS_APPL | ER_CONSTRUCT) | ER_TAG_MASK))
138 Stream_Read_UINT8(s,
byte);
143 er_read_length(s, length);
147 Stream_Read_UINT8(s,
byte);
149 if (
byte != ((ER_CLASS_APPL | ER_CONSTRUCT) | (ER_TAG_MASK & tag)))
152 er_read_length(s, length);
165 void er_write_application_tag(
wStream* s, BYTE tag,
int length, BOOL flag)
169 Stream_Write_UINT8(s, (ER_CLASS_APPL | ER_CONSTRUCT) | ER_TAG_MASK);
170 Stream_Write_UINT8(s, tag);
171 er_write_length(s, length, flag);
175 Stream_Write_UINT8(s, (ER_CLASS_APPL | ER_CONSTRUCT) | (ER_TAG_MASK & tag));
176 er_write_length(s, length, flag);
180 BOOL er_read_contextual_tag(
wStream* s, BYTE tag,
int* length, BOOL pc)
184 Stream_Read_UINT8(s,
byte);
186 if (
byte != ((ER_CLASS_CTXT | ER_PC(pc)) | (ER_TAG_MASK & tag)))
192 er_read_length(s, length);
197 int er_write_contextual_tag(
wStream* s, BYTE tag,
int length, BOOL pc, BOOL flag)
199 Stream_Write_UINT8(s, (ER_CLASS_CTXT | ER_PC(pc)) | (ER_TAG_MASK & tag));
200 return er_write_length(s, length, flag) + 1;
203 int er_skip_contextual_tag(
int length)
205 return _er_skip_length(length) + 1;
208 BOOL er_read_sequence_tag(
wStream* s,
int* length)
212 Stream_Read_UINT8(s,
byte);
214 if (
byte != ((ER_CLASS_UNIV | ER_CONSTRUCT) | (ER_TAG_SEQUENCE_OF)))
217 er_read_length(s, length);
228 int er_write_sequence_tag(
wStream* s,
int length, BOOL flag)
230 Stream_Write_UINT8(s, (ER_CLASS_UNIV | ER_CONSTRUCT) | (ER_TAG_MASK & ER_TAG_SEQUENCE));
231 return er_write_length(s, length, flag) + 1;
234 int er_skip_sequence(
int length)
236 return 1 + _er_skip_length(length) + length;
239 int er_skip_sequence_tag(
int length)
241 return 1 + _er_skip_length(length);
244 BOOL er_read_enumerated(
wStream* s, BYTE* enumerated, BYTE count)
248 er_read_universal_tag(s, ER_TAG_ENUMERATED, FALSE);
249 er_read_length(s, &length);
252 Stream_Read_UINT8(s, *enumerated);
257 if (*enumerated + 1 > count)
263 void er_write_enumerated(
wStream* s, BYTE enumerated, BYTE count, BOOL flag)
265 er_write_universal_tag(s, ER_TAG_ENUMERATED, FALSE);
266 er_write_length(s, 1, flag);
267 Stream_Write_UINT8(s, enumerated);
270 BOOL er_read_bit_string(
wStream* s,
int* length, BYTE* padding)
272 er_read_universal_tag(s, ER_TAG_BIT_STRING, FALSE);
273 er_read_length(s, length);
274 Stream_Read_UINT8(s, *padding);
279 BOOL er_write_bit_string_tag(
wStream* s, UINT32 length, BYTE padding, BOOL flag)
281 er_write_universal_tag(s, ER_TAG_BIT_STRING, FALSE);
282 er_write_length(s, length, flag);
283 Stream_Write_UINT8(s, padding);
287 BOOL er_read_octet_string(
wStream* s,
int* length)
289 if (!er_read_universal_tag(s, ER_TAG_OCTET_STRING, FALSE))
291 er_read_length(s, length);
303 void er_write_octet_string(
wStream* s, BYTE* oct_str,
int length, BOOL flag)
305 er_write_universal_tag(s, ER_TAG_OCTET_STRING, FALSE);
306 er_write_length(s, length, flag);
307 Stream_Write(s, oct_str, length);
310 int er_write_octet_string_tag(
wStream* s,
int length, BOOL flag)
312 er_write_universal_tag(s, ER_TAG_OCTET_STRING, FALSE);
313 er_write_length(s, length, flag);
314 return 1 + _er_skip_length(length);
317 int er_skip_octet_string(
int length)
319 return 1 + _er_skip_length(length) + length;
328 BOOL er_read_BOOL(
wStream* s, BOOL* value)
333 if (!er_read_universal_tag(s, ER_TAG_BOOLEAN, FALSE))
335 er_read_length(s, &length);
338 Stream_Read_UINT8(s, v);
339 *value = (v ? TRUE : FALSE);
349 void er_write_BOOL(
wStream* s, BOOL value)
351 er_write_universal_tag(s, ER_TAG_BOOLEAN, FALSE);
352 er_write_length(s, 1, FALSE);
353 Stream_Write_UINT8(s, (value == TRUE) ? 0xFF : 0);
356 BOOL er_read_integer(
wStream* s, UINT32* value)
360 er_read_universal_tag(s, ER_TAG_INTEGER, FALSE);
361 er_read_length(s, &length);
365 Stream_Seek(s, length);
371 Stream_Read_UINT8(s, *value);
373 else if (length == 2)
375 Stream_Read_UINT16_BE(s, *value);
377 else if (length == 3)
380 Stream_Read_UINT8(s,
byte);
381 Stream_Read_UINT16_BE(s, *value);
382 *value += (
byte << 16);
384 else if (length == 4)
386 Stream_Read_UINT32_BE(s, *value);
402 int er_write_integer(
wStream* s, INT32 value)
404 er_write_universal_tag(s, ER_TAG_INTEGER, FALSE);
406 if (value <= 127 && value >= -128)
408 er_write_length(s, 1, FALSE);
409 Stream_Write_UINT8(s, value);
412 else if (value <= 32767 && value >= -32768)
414 er_write_length(s, 2, FALSE);
415 Stream_Write_INT16_BE(s, value);
420 er_write_length(s, 4, FALSE);
421 Stream_Write_INT32_BE(s, value);
426 int er_skip_integer(INT32 value)
428 if (value <= 127 && value >= -128)
430 return _er_skip_length(1) + 2;
432 else if (value <= 32767 && value >= -32768)
434 return _er_skip_length(2) + 3;
438 return _er_skip_length(4) + 5;
442 BOOL er_read_integer_length(
wStream* s,
int* length)
444 er_read_universal_tag(s, ER_TAG_INTEGER, FALSE);
445 er_read_length(s, length);