FreeRDP
per.c
1 
20 #include <winpr/assert.h>
21 #include <winpr/print.h>
22 
23 #include <freerdp/config.h>
24 #include <freerdp/crypto/per.h>
25 
26 #include <freerdp/log.h>
27 #define TAG FREERDP_TAG("crypto.per")
28 
38 BOOL per_read_length(wStream* s, UINT16* length)
39 {
40  BYTE byte = 0;
41 
42  WINPR_ASSERT(length);
43  if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
44  return FALSE;
45 
46  Stream_Read_UINT8(s, byte);
47 
48  if (byte & 0x80)
49  {
50  if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
51  return FALSE;
52 
53  byte &= ~(0x80);
54  *length = (byte << 8);
55  Stream_Read_UINT8(s, byte);
56  *length += byte;
57  }
58  else
59  {
60  *length = byte;
61  }
62 
63  return TRUE;
64 }
65 
74 BOOL per_write_length(wStream* s, UINT16 length)
75 {
76  if (length > 0x7F)
77  {
78  if (!Stream_EnsureRemainingCapacity(s, 2))
79  return FALSE;
80  Stream_Write_UINT16_BE(s, (length | 0x8000));
81  }
82  else
83  {
84  if (!Stream_EnsureRemainingCapacity(s, 1))
85  return FALSE;
86  Stream_Write_UINT8(s, (UINT8)length);
87  }
88  return TRUE;
89 }
90 
99 BOOL per_read_choice(wStream* s, BYTE* choice)
100 {
101  if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
102  return FALSE;
103 
104  Stream_Read_UINT8(s, *choice);
105  return TRUE;
106 }
107 
116 BOOL per_write_choice(wStream* s, BYTE choice)
117 {
118  if (!Stream_EnsureRemainingCapacity(s, 1))
119  return FALSE;
120  Stream_Write_UINT8(s, choice);
121  return TRUE;
122 }
123 
132 BOOL per_read_selection(wStream* s, BYTE* selection)
133 {
134  if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
135  return FALSE;
136 
137  WINPR_ASSERT(selection);
138  Stream_Read_UINT8(s, *selection);
139  return TRUE;
140 }
141 
150 BOOL per_write_selection(wStream* s, BYTE selection)
151 {
152  if (!Stream_EnsureRemainingCapacity(s, 1))
153  return FALSE;
154  Stream_Write_UINT8(s, selection);
155  return TRUE;
156 }
157 
166 BOOL per_read_number_of_sets(wStream* s, BYTE* number)
167 {
168  if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
169  return FALSE;
170 
171  WINPR_ASSERT(number);
172  Stream_Read_UINT8(s, *number);
173  return TRUE;
174 }
175 
185 BOOL per_write_number_of_sets(wStream* s, BYTE number)
186 {
187  if (!Stream_EnsureRemainingCapacity(s, 1))
188  return FALSE;
189  Stream_Write_UINT8(s, number);
190  return TRUE;
191 }
192 
202 BOOL per_read_padding(wStream* s, UINT16 length)
203 {
204  if (!Stream_CheckAndLogRequiredLength(TAG, s, length))
205  return FALSE;
206 
207  Stream_Seek(s, length);
208  return TRUE;
209 }
210 
219 BOOL per_write_padding(wStream* s, UINT16 length)
220 {
221  if (!Stream_EnsureRemainingCapacity(s, length))
222  return FALSE;
223  Stream_Zero(s, length);
224  return TRUE;
225 }
226 
235 BOOL per_read_integer(wStream* s, UINT32* integer)
236 {
237  UINT16 length = 0;
238 
239  WINPR_ASSERT(integer);
240 
241  if (!per_read_length(s, &length))
242  return FALSE;
243 
244  if (!Stream_CheckAndLogRequiredLength(TAG, s, length))
245  return FALSE;
246 
247  if (length == 0)
248  *integer = 0;
249  else if (length == 1)
250  Stream_Read_UINT8(s, *integer);
251  else if (length == 2)
252  Stream_Read_UINT16_BE(s, *integer);
253  else
254  return FALSE;
255 
256  return TRUE;
257 }
258 
267 BOOL per_write_integer(wStream* s, UINT32 integer)
268 {
269  if (integer <= UINT8_MAX)
270  {
271  if (!per_write_length(s, 1))
272  return FALSE;
273  if (!Stream_EnsureRemainingCapacity(s, 1))
274  return FALSE;
275  Stream_Write_UINT8(s, integer);
276  }
277  else if (integer <= UINT16_MAX)
278  {
279  if (!per_write_length(s, 2))
280  return FALSE;
281  if (!Stream_EnsureRemainingCapacity(s, 2))
282  return FALSE;
283  Stream_Write_UINT16_BE(s, integer);
284  }
285  else if (integer <= UINT32_MAX)
286  {
287  if (!per_write_length(s, 4))
288  return FALSE;
289  if (!Stream_EnsureRemainingCapacity(s, 4))
290  return FALSE;
291  Stream_Write_UINT32_BE(s, integer);
292  }
293  return TRUE;
294 }
295 
306 BOOL per_read_integer16(wStream* s, UINT16* integer, UINT16 min)
307 {
308  if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
309  return FALSE;
310 
311  Stream_Read_UINT16_BE(s, *integer);
312 
313  if (*integer > UINT16_MAX - min)
314  {
315  WLog_WARN(TAG, "PER uint16 invalid value %" PRIu16 " > %" PRIu16, *integer,
316  UINT16_MAX - min);
317  return FALSE;
318  }
319 
320  *integer += min;
321 
322  return TRUE;
323 }
324 
334 BOOL per_write_integer16(wStream* s, UINT16 integer, UINT16 min)
335 {
336  if (min > integer)
337  return FALSE;
338  if (!Stream_EnsureRemainingCapacity(s, 2))
339  return FALSE;
340  Stream_Write_UINT16_BE(s, integer - min);
341  return TRUE;
342 }
343 
354 BOOL per_read_enumerated(wStream* s, BYTE* enumerated, BYTE count)
355 {
356  if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
357  return FALSE;
358 
359  WINPR_ASSERT(enumerated);
360  Stream_Read_UINT8(s, *enumerated);
361 
362  /* check that enumerated value falls within expected range */
363  if (*enumerated + 1 > count)
364  {
365  WLog_WARN(TAG, "PER invalid data, expected %" PRIu8 " < %" PRIu8, *enumerated, count);
366  return FALSE;
367  }
368 
369  return TRUE;
370 }
371 
382 BOOL per_write_enumerated(wStream* s, BYTE enumerated, BYTE count)
383 {
384  if (!Stream_EnsureRemainingCapacity(s, 1))
385  return FALSE;
386  Stream_Write_UINT8(s, enumerated);
387  return TRUE;
388 }
389 
390 static BOOL per_check_oid_and_log_mismatch(const BYTE* got, const BYTE* expect, size_t length)
391 {
392  if (memcmp(got, expect, length) == 0)
393  {
394  return TRUE;
395  }
396  else
397  {
398  char* got_str = winpr_BinToHexString(got, length, TRUE);
399  char* expect_str = winpr_BinToHexString(expect, length, TRUE);
400 
401  WLog_WARN(TAG, "PER OID mismatch, got %s, expected %s", got_str, expect_str);
402  free(got_str);
403  free(expect_str);
404  return FALSE;
405  }
406 }
407 
418 BOOL per_read_object_identifier(wStream* s, const BYTE oid[6])
419 {
420  BYTE t12 = 0;
421  UINT16 length = 0;
422  BYTE a_oid[6] = { 0 };
423 
424  if (!per_read_length(s, &length))
425  return FALSE;
426 
427  if (length != 5)
428  {
429  WLog_WARN(TAG, "PER length, got %" PRIu16 ", expected 5", length);
430  return FALSE;
431  }
432 
433  if (!Stream_CheckAndLogRequiredLength(TAG, s, length))
434  return FALSE;
435 
436  Stream_Read_UINT8(s, t12); /* first two tuples */
437  a_oid[0] = t12 / 40;
438  a_oid[1] = t12 % 40;
439 
440  Stream_Read_UINT8(s, a_oid[2]); /* tuple 3 */
441  Stream_Read_UINT8(s, a_oid[3]); /* tuple 4 */
442  Stream_Read_UINT8(s, a_oid[4]); /* tuple 5 */
443  Stream_Read_UINT8(s, a_oid[5]); /* tuple 6 */
444 
445  return per_check_oid_and_log_mismatch(a_oid, oid, sizeof(a_oid));
446 }
447 
457 BOOL per_write_object_identifier(wStream* s, const BYTE oid[6])
458 {
459  BYTE t12 = oid[0] * 40 + oid[1];
460  if (!Stream_EnsureRemainingCapacity(s, 6))
461  return FALSE;
462  Stream_Write_UINT8(s, 5); /* length */
463  Stream_Write_UINT8(s, t12); /* first two tuples */
464  Stream_Write_UINT8(s, oid[2]); /* tuple 3 */
465  Stream_Write_UINT8(s, oid[3]); /* tuple 4 */
466  Stream_Write_UINT8(s, oid[4]); /* tuple 5 */
467  Stream_Write_UINT8(s, oid[5]); /* tuple 6 */
468  return TRUE;
469 }
470 
478 static void per_write_string(wStream* s, BYTE* str, int length)
479 {
480  for (int i = 0; i < length; i++)
481  Stream_Write_UINT8(s, str[i]);
482 }
483 
495 BOOL per_read_octet_string(wStream* s, const BYTE* oct_str, UINT16 length, UINT16 min)
496 {
497  UINT16 mlength = 0;
498 
499  if (!per_read_length(s, &mlength))
500  return FALSE;
501 
502  if (mlength + min != length)
503  {
504  WLog_ERR(TAG, "length mismatch: %" PRIu16 "!= %" PRIu16, mlength + min, length);
505  return FALSE;
506  }
507 
508  if (!Stream_CheckAndLogRequiredLength(TAG, s, length))
509  return FALSE;
510 
511  const BYTE* a_oct_str = Stream_ConstPointer(s);
512  Stream_Seek(s, length);
513 
514  return per_check_oid_and_log_mismatch(a_oct_str, oct_str, length);
515 }
516 
527 BOOL per_write_octet_string(wStream* s, const BYTE* oct_str, UINT16 length, UINT16 min)
528 {
529  UINT16 mlength = 0;
530 
531  mlength = (length >= min) ? length - min : min;
532 
533  if (!per_write_length(s, mlength))
534  return FALSE;
535 
536  if (!Stream_EnsureRemainingCapacity(s, length))
537  return FALSE;
538  for (UINT16 i = 0; i < length; i++)
539  Stream_Write_UINT8(s, oct_str[i]);
540  return TRUE;
541 }
542 
551 BOOL per_read_numeric_string(wStream* s, UINT16 min)
552 {
553  size_t length = 0;
554  UINT16 mlength = 0;
555 
556  if (!per_read_length(s, &mlength))
557  return FALSE;
558 
559  length = (mlength + min + 1) / 2;
560 
561  if (!Stream_CheckAndLogRequiredLength(TAG, s, length))
562  return FALSE;
563 
564  Stream_Seek(s, length);
565  return TRUE;
566 }
567 
578 BOOL per_write_numeric_string(wStream* s, const BYTE* num_str, UINT16 length, UINT16 min)
579 {
580  WINPR_ASSERT(num_str || (length == 0));
581 
582  const UINT16 mlength = (length >= min) ? length - min : min;
583 
584  if (!per_write_length(s, mlength))
585  return FALSE;
586 
587  if (!Stream_EnsureRemainingCapacity(s, length))
588  return FALSE;
589  for (UINT16 i = 0; i < length; i += 2)
590  {
591  BYTE c1 = num_str[i];
592  BYTE c2 = ((i + 1) < length) ? num_str[i + 1] : 0x30;
593 
594  if ((c1 < 0x30) || (c2 < 0x30))
595  return FALSE;
596 
597  c1 = (c1 - 0x30) % 10;
598  c2 = (c2 - 0x30) % 10;
599  const BYTE num = (c1 << 4) | c2;
600 
601  Stream_Write_UINT8(s, num); /* string */
602  }
603  return TRUE;
604 }