FreeRDP
der.c
1 
20 #include <freerdp/config.h>
21 
22 #include <winpr/crt.h>
23 #include <winpr/assert.h>
24 #include <winpr/cast.h>
25 
26 #include <freerdp/crypto/der.h>
27 
28 int _der_skip_length(int length)
29 {
30  if (length > 0x7F && length <= 0xFF)
31  return 2;
32  else if (length > 0xFF)
33  return 3;
34  else
35  return 1;
36 }
37 
38 int der_write_length(wStream* s, int length)
39 {
40  if (length > 0x7F && length <= 0xFF)
41  {
42  Stream_Write_UINT8(s, 0x81);
43  Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(UINT8, length));
44  return 2;
45  }
46  else if (length > 0xFF)
47  {
48  Stream_Write_UINT8(s, 0x82);
49  Stream_Write_UINT16_BE(s, WINPR_ASSERTING_INT_CAST(UINT16, length));
50  return 3;
51  }
52  else
53  {
54  Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(UINT8, length));
55  return 1;
56  }
57 }
58 
59 int der_get_content_length(int length)
60 {
61  if (length > 0x81 && length <= 0x102)
62  return length - 3;
63  else if (length > 0x102)
64  return length - 4;
65  else
66  return length - 2;
67 }
68 
69 int der_skip_contextual_tag(int length)
70 {
71  return _der_skip_length(length) + 1;
72 }
73 
74 int der_write_contextual_tag(wStream* s, BYTE tag, int length, BOOL pc)
75 {
76  Stream_Write_UINT8(s, (ER_CLASS_CTXT | ER_PC(pc)) | (ER_TAG_MASK & tag));
77  return der_write_length(s, length) + 1;
78 }
79 
80 static void der_write_universal_tag(wStream* s, BYTE tag, BOOL pc)
81 {
82  Stream_Write_UINT8(s, (ER_CLASS_UNIV | ER_PC(pc)) | (ER_TAG_MASK & tag));
83 }
84 
85 int der_skip_octet_string(int length)
86 {
87  return 1 + _der_skip_length(length) + length;
88 }
89 
90 void der_write_octet_string(wStream* s, BYTE* oct_str, int length)
91 {
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));
95 }
96 
97 int der_skip_sequence_tag(int length)
98 {
99  return 1 + _der_skip_length(length);
100 }
101 
102 int der_write_sequence_tag(wStream* s, int length)
103 {
104  Stream_Write_UINT8(s, (ER_CLASS_UNIV | ER_CONSTRUCT) | (ER_TAG_MASK & ER_TAG_SEQUENCE));
105  return der_write_length(s, length) + 1;
106 }