oid.c
Go to the documentation of this file.
1 /**
2  * @file oid.c
3  * @brief OID (Object Identifier)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2020 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.8
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "encoding/oid.h"
37 #include "debug.h"
38 
39 //Check crypto library configuration
40 #if (OID_SUPPORT == ENABLED)
41 
42 
43 /**
44  * @brief Check whether the specified object identifier is valid
45  * @param[in] oid Pointer to the object identifier
46  * @param[in] oidLen Length of the OID, in bytes
47  * @return Error code
48  **/
49 
50 error_t oidCheck(const uint8_t *oid, size_t oidLen)
51 {
52  size_t i;
53  size_t n;
54 
55  //Check parameters
56  if(oid == NULL)
58 
59  //Check the length of the OID
60  if(oidLen == 0)
61  {
62  //Report an error
63  return ERROR_INVALID_SYNTAX;
64  }
65  else if(oidLen > 1)
66  {
67  //Parse the object identifier
68  for(i = 1, n = 2; i < oidLen; i++)
69  {
70  //Update the total number of nodes
71  if(!(oid[i] & OID_MORE_FLAG))
72  n++;
73 
74  //SNMP limits object identifier values to a maximum of 128 nodes
75  if(n > 128)
76  return ERROR_INVALID_SYNTAX;
77  }
78 
79  //Ensure that the last sub-identifier is valid
80  if(oid[oidLen - 1] & OID_MORE_FLAG)
81  return ERROR_INVALID_SYNTAX;
82  }
83 
84  //The specified OID is valid
85  return NO_ERROR;
86 }
87 
88 
89 /**
90  * @brief Compare object identifiers
91  * @param[in] oid1 Pointer the first OID
92  * @param[in] oidLen1 Length of the first OID, in bytes
93  * @param[in] oid2 Pointer the second OID
94  * @param[in] oidLen2 Length of the second OID, in bytes
95  * @return Comparison result
96  * @retval 0 Objects identifiers are equal
97  * @retval -1 The first OID lexicographically precedes the second OID
98  * @retval 1 The second OID lexicographically precedes the first OID
99  **/
100 
101 int_t oidComp(const uint8_t *oid1, size_t oidLen1, const uint8_t *oid2,
102  size_t oidLen2)
103 {
104  size_t i;
105 
106  //Perform lexicographical comparison
107  for(i = 0; i < oidLen1 && i < oidLen2; i++)
108  {
109  //Compare current byte
110  if(oid1[i] < oid2[i])
111  {
112  return -1;
113  }
114  else if(oid1[i] > oid2[i])
115  {
116  return 1;
117  }
118  }
119 
120  //Compare length
121  if(oidLen1 < oidLen2)
122  {
123  return -1;
124  }
125  else if(oidLen1 > oidLen2)
126  {
127  return 1;
128  }
129 
130  //Object identifiers are equal
131  return 0;
132 }
133 
134 
135 /**
136  * @brief Check whether an OID matches the specified subtree
137  * @param[in] oid Pointer to the object identifier
138  * @param[in] oidLen Length of the OID, in bytes
139  * @param[in] subtree Pointer to the subtree
140  * @param[in] subtreeLen Length of the subtree, in bytes
141  * @param[in] mask Pointer to the mask
142  * @param[in] maskLen Length of the mask, in bytes
143  * @return TRUE if the OID matches the specified subtree, else FALSE
144  **/
145 
146 bool_t oidMatch(const uint8_t *oid, size_t oidLen, const uint8_t *subtree,
147  size_t subtreeLen, const uint8_t *mask, size_t maskLen)
148 {
149  size_t i;
150  uint8_t flag;
151  size_t oidPos;
152  size_t subtreePos;
153 
154  //Initialize variables
155  oidPos = 0;
156  subtreePos = 0;
157 
158  //Check whether the OID matches the specified subtree
159  for(i = 0; subtreePos < subtreeLen; i++)
160  {
161  //Check the length of the OID
162  if(oidPos >= oidLen)
163  return FALSE;
164 
165  //The bit mask is extended with 1's to be the required length
166  if((i / 8) < maskLen)
167  {
168  flag = mask[i / 8] & (1 << (7 - (i % 8)));
169  }
170  else
171  {
172  flag = 1;
173  }
174 
175  //First node?
176  if(i == 0)
177  {
178  //The mask allows for a simple form of wildcarding
179  if(flag)
180  {
181  //Compare the first sub-identifier
182  if((oid[0] / 40) != (subtree[0] / 40))
183  return FALSE;
184  }
185  }
186  //Second node?
187  else if(i == 1)
188  {
189  //The mask allows for a simple form of wildcarding
190  if(flag)
191  {
192  //Compare the second sub-identifier
193  if((oid[0] % 40) != (subtree[0] % 40))
194  return FALSE;
195  }
196 
197  //Jump to the next node
198  oidPos = 1;
199  subtreePos = 1;
200  }
201  //Remaining nodes?
202  else
203  {
204  //The mask allows for a simple form of wildcarding
205  if(flag)
206  {
207  //Compare sub-identifiers
208  while(1)
209  {
210  //Compare the current byte
211  if(oid[oidPos] != subtree[subtreePos])
212  return FALSE;
213 
214  //Bit b8 is set to zero to indicate the last byte
215  flag = oid[oidPos] & OID_MORE_FLAG;
216 
217  //Next byte
218  oidPos++;
219  subtreePos++;
220 
221  //Last byte of the sub-identifier?
222  if(!flag)
223  break;
224 
225  //Check the length of the OID
226  if(oidPos >= oidLen)
227  return FALSE;
228 
229  //Check the length of the subtree
230  if(subtreePos > subtreeLen)
231  return FALSE;
232  }
233  }
234  else
235  {
236  //Read the OID until the last byte of the sub-identifier is found
237  while(1)
238  {
239  //Bit b8 is set to zero to indicate the last byte
240  flag = oid[oidPos] & OID_MORE_FLAG;
241 
242  //Next byte
243  oidPos++;
244 
245  //Last byte of the sub-identifier?
246  if(!flag)
247  break;
248 
249  //Check the length of the OID
250  if(oidPos >= oidLen)
251  return FALSE;
252  }
253 
254  //Read the subtree until the last byte of the sub-identifier is found
255  while(1)
256  {
257  //Bit b8 is set to zero to indicate the last byte
258  flag = subtree[subtreePos] & OID_MORE_FLAG;
259 
260  //Next byte
261  subtreePos++;
262 
263  //Last byte of the sub-identifier?
264  if(!flag)
265  break;
266 
267  //Check the length of the subtree
268  if(subtreePos >= subtreeLen)
269  return FALSE;
270  }
271  }
272  }
273  }
274 
275  //The OID matches the specified subtree
276  return TRUE;
277 }
278 
279 
280 /**
281  * @brief Calculate the number of sub-identifiers
282  * @param[in] oid Pointer to the object identifier
283  * @param[in] oidLen Length of the OID, in bytes
284  * @return Number of sub-identifiers
285  **/
286 
287 uint_t oidCountSubIdentifiers(const uint8_t *oid, size_t oidLen)
288 {
289  size_t i;
290  uint_t n;
291 
292  //Check the length of the OID
293  if(oidLen == 0)
294  {
295  //The OID is empty
296  n = 0;
297  }
298  else
299  {
300  //The first byte encodes two sub-identifiers
301  n = 2;
302 
303  //Parse the object identifier
304  for(i = 1; i < oidLen; i++)
305  {
306  //Update the total number of sub-identifiers
307  if(!(oid[i] & OID_MORE_FLAG))
308  n++;
309  }
310  }
311 
312  //Return the total number of sub-identifiers
313  return n;
314 }
315 
316 
317 /**
318  * @brief Encode OID sub-identifier
319  * @param[in] oid Pointer to the object identifier
320  * @param[in] maxOidLen Maximum number of bytes the OID can hold
321  * @param[in,out] pos Offset where to write the sub-identifier
322  * @param[in] value Value of the sub-identifier
323  * @return Error code
324  **/
325 
326 error_t oidEncodeSubIdentifier(uint8_t *oid, size_t maxOidLen,
327  size_t *pos, uint32_t value)
328 {
329  size_t i;
330  size_t n;
331  uint8_t temp[5];
332 
333  //Encode the first byte of the sub-identifier
334  temp[0] = value & OID_VALUE_MASK;
335  //Shift the value to the right
336  value >>= 7;
337 
338  //Encode the remaining bytes
339  for(n = 1; value != 0; n++)
340  {
341  //Encode current byte
342  temp[n] = OID_MORE_FLAG | (value & OID_VALUE_MASK);
343  //Shift the value to the right
344  value >>= 7;
345  }
346 
347  //Sanity check
348  if((*pos + n) > maxOidLen)
349  return ERROR_BUFFER_OVERFLOW;
350 
351  //Write the current sub-identifier
352  for(i = 0; i < n; i++)
353  {
354  oid[*pos + i] = temp[n - i - 1];
355  }
356 
357  //Update offset value
358  *pos += n;
359 
360  //Successful processing
361  return NO_ERROR;
362 }
363 
364 
365 /**
366  * @brief Decode OID sub-identifier
367  * @param[in] oid Pointer to the object identifier
368  * @param[in] oidLen Length of the OID, in bytes
369  * @param[in,out] pos Offset where to read the sub-identifier
370  * @param[out] value Value of the sub-identifier
371  * @return Error code
372  **/
373 
374 error_t oidDecodeSubIdentifier(const uint8_t *oid, size_t oidLen,
375  size_t *pos, uint32_t *value)
376 {
377  size_t i;
378 
379  //Initialize the value of the sub-identifier
380  *value = 0;
381 
382  //Sanity check
383  if(*pos >= oidLen)
385 
386  //Read the OID until the last byte of the sub-identifier is found
387  for(i = *pos; i < oidLen; i++)
388  {
389  //Shift the value to the left
390  *value <<= 7;
391  //Update value of the sub-identifier
392  *value |= oid[i] & OID_VALUE_MASK;
393 
394  //Bit b8 is set to zero to indicate the last byte
395  if(!(oid[i] & OID_MORE_FLAG))
396  {
397  //Update offset value
398  *pos = i + 1;
399  //Successful processing
400  return NO_ERROR;
401  }
402  }
403 
404  //The specified OID is not valid
405  return ERROR_INVALID_SYNTAX;
406 }
407 
408 
409 /**
410  * @brief Convert a string representation of an OID to a binary OID
411  * @param[in] str NULL-terminated string representing the OID
412  * @param[out] oid Object identifier
413  * @param[in] maxOidLen Maximum number of bytes the OID can hold
414  * @param[out] oidLen Length of the object identifier
415  * @return Error code
416  **/
417 
418 error_t oidFromString(const char_t *str, uint8_t *oid, size_t maxOidLen,
419  size_t *oidLen)
420 {
421  error_t error;
422  size_t i;
423  size_t j;
424  size_t n;
425  uint32_t value;
426  uint8_t temp[5];
427 
428  //Reset the length of the OID
429  *oidLen = 0;
430 
431  //Number of nodes
432  i = 0;
433  //Initialize the value of the sub-identifier
434  value = 0;
435 
436  //Parse input string
437  while(1)
438  {
439  //Digit found?
440  if(osIsdigit(*str))
441  {
442  //Update the value of the sub-identifier
443  value = (value * 10) + (*str - '0');
444  }
445  //Separator or end of string found?
446  else if(*str == '.' || *str == '\0')
447  {
448  //First node?
449  if(i == 0)
450  {
451  //Check value
452  if(value > 6)
453  {
454  //The conversion failed
455  error = ERROR_INVALID_SYNTAX;
456  break;
457  }
458 
459  //Encode the first sub-identifier
460  temp[0] = value * 40;
461  //Prepare to decode the next node
462  value = 0;
463  //Do not write current sub-identifier yet
464  n = 0;
465  }
466  //Second node?
467  else if(i == 1)
468  {
469  //Check value
470  if(value > 39)
471  {
472  //The conversion failed
473  error = ERROR_INVALID_SYNTAX;
474  break;
475  }
476 
477  //Encode the second sub-identifier
478  temp[0] += value;
479  //Prepare to decode the next node
480  value = 0;
481  //Write the first two sub-identifiers
482  n = 1;
483  }
484  //Remaining nodes?
485  else
486  {
487  //Encode the first byte of the sub-identifier
488  temp[0] = value & OID_VALUE_MASK;
489  //Shift the value to the right
490  value >>= 7;
491 
492  //Encode the remaining bytes
493  for(n = 1; value != 0; n++)
494  {
495  //Encode current byte
496  temp[n] = OID_MORE_FLAG | (value & OID_VALUE_MASK);
497  //Shift the value to the right
498  value >>= 7;
499  }
500  }
501 
502  //Sanity check
503  if(n > maxOidLen)
504  {
505  //Report an error
506  error = ERROR_BUFFER_OVERFLOW;
507  break;
508  }
509 
510  //Write the current sub-identifier
511  for(j = 0; j < n; j++)
512  {
513  oid[j] = temp[n - j - 1];
514  }
515 
516  //Advance write pointer
517  oid += n;
518  *oidLen += n;
519  maxOidLen -= n;
520 
521  //Number of sub-identifiers
522  i++;
523 
524  //End of string detected?
525  if(*str == '\0')
526  {
527  //The conversion succeeded
528  error = NO_ERROR;
529  break;
530  }
531  }
532  //Invalid character...
533  else
534  {
535  //The conversion failed
536  error = ERROR_INVALID_SYNTAX;
537  break;
538  }
539 
540  //Point to the next character
541  str++;
542  }
543 
544  //Return status code
545  return error;
546 }
547 
548 
549 /**
550  * @brief Convert a binary OID to a string representation
551  * @param[in] oid Object identifier
552  * @param[in] oidLen Length of the object identifier, in bytes
553  * @param[out] str NULL-terminated string representing the OID
554  * @param[in] maxStrLen Maximum length of the resulting string
555  * @return Pointer to the formatted string
556  **/
557 
558 char_t *oidToString(const uint8_t *oid, size_t oidLen, char_t *str,
559  size_t maxStrLen)
560 {
561  static char_t buffer[128];
562  size_t i;
563  size_t n;
564  uint32_t value;
565  char_t *p;
566  char_t temp[12];
567 
568  //The str parameter is optional
569  if(str == NULL)
570  {
571  //Point to the internal buffer
572  str = buffer;
573  //Maximum length of the resulting string
574  maxStrLen = sizeof(buffer) - 1;
575  }
576 
577  //Point to the beginning of the string
578  p = str;
579  //Properly terminate the string
580  *p = '\0';
581 
582  //Check the length of the OID
583  if(oidLen > 0)
584  {
585  //Convert the first 2 bytes
586  n = osSprintf(temp, "%" PRIu8 ".%" PRIu8 "", oid[0] / 40, oid[0] % 40);
587 
588  //Sanity check
589  if(n <= maxStrLen)
590  {
591  //Copy the resulting string
592  osStrcpy(p, temp);
593  //Advance write pointer
594  p += n;
595  maxStrLen -= n;
596  }
597 
598  //Initialize the value of the sub-identifier
599  value = 0;
600 
601  //Convert the rest of the OID
602  for(i = 1; i < oidLen; i++)
603  {
604  //Shift the value to the left
605  value <<= 7;
606  //Update the current value
607  value |= oid[i] & OID_VALUE_MASK;
608 
609  //Bit b8 is set to zero to indicate the last byte
610  if(!(oid[i] & OID_MORE_FLAG))
611  {
612  //Dump current value
613  n = osSprintf(temp, ".%" PRIu32, value);
614 
615  //Sanity check
616  if(n <= maxStrLen)
617  {
618  //Copy the resulting string
619  osStrcpy(p, temp);
620  //Advance write pointer
621  p += n;
622  maxStrLen -= n;
623  }
624 
625  //Prepare to decode the next value
626  value = 0;
627  }
628  }
629  }
630 
631  //Return a pointer to the formatted string
632  return str;
633 }
634 
635 
636 /**
637  * @brief Convert a bit mask to binary representation
638  * @param[in] str NULL-terminated string representing the bit mask
639  * @param[out] mask Pointer to the buffer where to store the resulting mask
640  * @param[in] maxMaskLen Maximum number of bytes the buffer can hold
641  * @param[out] maskLen Length of the mask
642  * @return Error code
643  **/
644 
645 error_t maskFromString(const char_t *str, uint8_t *mask, size_t maxMaskLen,
646  size_t *maskLen)
647 {
648  error_t error;
649  size_t i;
650  size_t j;
651 
652  //Point to the first byte
653  i = 0;
654  //Point to the most-significant bit
655  j = 8;
656 
657  //Parse input string
658  while(1)
659  {
660  //Check current character
661  if(*str == '0' || *str == '1')
662  {
663  //Make sure the output buffer is large enough
664  if(i >= maxMaskLen)
665  {
666  //Report an error
667  error = ERROR_BUFFER_OVERFLOW;
668  break;
669  }
670 
671  //Update bit mask
672  if(*str == '1')
673  {
674  mask[i] |= (1 << (j - 1));
675  }
676  else
677  {
678  mask[i] &= ~(1 << (j - 1));
679  }
680 
681  //Next bit
682  j--;
683  }
684  else if(*str == '\0')
685  {
686  //End of string detected
687  error = NO_ERROR;
688  break;
689  }
690  else
691  {
692  //Discard any other characters
693  }
694 
695  //Check whether the least-significant bit has been reached
696  if(j == 0)
697  {
698  //Point to the most-significant bit of the next byte
699  j = 8;
700  i++;
701  }
702 
703  //Point to the next character
704  str++;
705  }
706 
707  //Check status code
708  if(!error)
709  {
710  //Incomplete byte?
711  if(j < 8)
712  {
713  //The bit mask is extended with 1's to be the required length
714  mask[i] |= (1 << j) - 1;
715  //Save the length of the resulting mask
716  *maskLen = i + 1;
717  }
718  else
719  {
720  //Save the length of the resulting mask
721  *maskLen = i;
722  }
723  }
724 
725  //Return status code
726  return error;
727 }
728 
729 #endif
int bool_t
Definition: compiler_port.h:49
signed int int_t
Definition: compiler_port.h:44
error_t maskFromString(const char_t *str, uint8_t *mask, size_t maxMaskLen, size_t *maskLen)
Convert a bit mask to binary representation.
Definition: oid.c:645
error_t oidFromString(const char_t *str, uint8_t *oid, size_t maxOidLen, size_t *oidLen)
Convert a string representation of an OID to a binary OID.
Definition: oid.c:418
OID (Object Identifier)
uint8_t p
Definition: ndp.h:298
#define TRUE
Definition: os_port.h:50
error_t oidEncodeSubIdentifier(uint8_t *oid, size_t maxOidLen, size_t *pos, uint32_t value)
Encode OID sub-identifier.
Definition: oid.c:326
int_t oidComp(const uint8_t *oid1, size_t oidLen1, const uint8_t *oid2, size_t oidLen2)
Compare object identifiers.
Definition: oid.c:101
error_t oidDecodeSubIdentifier(const uint8_t *oid, size_t oidLen, size_t *pos, uint32_t *value)
Decode OID sub-identifier.
Definition: oid.c:374
#define FALSE
Definition: os_port.h:46
Invalid parameter.
Definition: error.h:47
#define OID_MORE_FLAG
Definition: oid.h:38
error_t
Error codes.
Definition: error.h:42
#define osSprintf(dest,...)
Definition: os_port.h:206
uint8_t value[]
Definition: tcp.h:332
bool_t oidMatch(const uint8_t *oid, size_t oidLen, const uint8_t *subtree, size_t subtreeLen, const uint8_t *mask, size_t maskLen)
Check whether an OID matches the specified subtree.
Definition: oid.c:146
General definitions for cryptographic algorithms.
uint8_t oid[1]
Definition: mib_common.h:186
uint8_t mask
Definition: web_socket.h:317
#define osIsdigit(c)
Definition: os_port.h:242
char char_t
Definition: compiler_port.h:43
error_t oidCheck(const uint8_t *oid, size_t oidLen)
Check whether the specified object identifier is valid.
Definition: oid.c:50
uint_t oidCountSubIdentifiers(const uint8_t *oid, size_t oidLen)
Calculate the number of sub-identifiers.
Definition: oid.c:287
uint8_t n
char_t * oidToString(const uint8_t *oid, size_t oidLen, char_t *str, size_t maxStrLen)
Convert a binary OID to a string representation.
Definition: oid.c:558
#define OID_VALUE_MASK
Definition: oid.h:39
unsigned int uint_t
Definition: compiler_port.h:45
#define osStrcpy(s1, s2)
Definition: os_port.h:182
Success.
Definition: error.h:44
Debugging facilities.