20 #include <freerdp/config.h>
22 #include <winpr/crt.h>
23 #include <winpr/assert.h>
24 #include <winpr/cast.h>
26 #include <freerdp/crypto/der.h>
28 int _der_skip_length(
int length)
30 if (length > 0x7F && length <= 0xFF)
32 else if (length > 0xFF)
38 int der_write_length(
wStream* s,
int length)
40 if (length > 0x7F && length <= 0xFF)
42 Stream_Write_UINT8(s, 0x81);
43 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(UINT8, length));
46 else if (length > 0xFF)
48 Stream_Write_UINT8(s, 0x82);
49 Stream_Write_UINT16_BE(s, WINPR_ASSERTING_INT_CAST(UINT16, length));
54 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(UINT8, length));
59 int der_get_content_length(
int length)
61 if (length > 0x81 && length <= 0x102)
63 else if (length > 0x102)
69 int der_skip_contextual_tag(
int length)
71 return _der_skip_length(length) + 1;
74 int der_write_contextual_tag(
wStream* s, BYTE tag,
int length, BOOL pc)
76 Stream_Write_UINT8(s, (ER_CLASS_CTXT | ER_PC(pc)) | (ER_TAG_MASK & tag));
77 return der_write_length(s, length) + 1;
80 static void der_write_universal_tag(
wStream* s, BYTE tag, BOOL pc)
82 Stream_Write_UINT8(s, (ER_CLASS_UNIV | ER_PC(pc)) | (ER_TAG_MASK & tag));
85 int der_skip_octet_string(
int length)
87 return 1 + _der_skip_length(length) + length;
90 void der_write_octet_string(
wStream* s, BYTE* oct_str,
int length)
92 der_write_universal_tag(s, ER_TAG_OCTET_STRING, FALSE);
93 der_write_length(s, length);
94 Stream_Write(s, oct_str, WINPR_ASSERTING_INT_CAST(
size_t, length));
97 int der_skip_sequence_tag(
int length)
99 return 1 + _der_skip_length(length);
102 int der_write_sequence_tag(
wStream* s,
int length)
104 Stream_Write_UINT8(s, (ER_CLASS_UNIV | ER_CONSTRUCT) | (ER_TAG_MASK & ER_TAG_SEQUENCE));
105 return der_write_length(s, length) + 1;