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