allowed_basenames.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*############################################################################
  2. # Copyright 2017 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /// Basename management implementation
  17. /*! \file */
  18. #include "epid/member/src/allowed_basenames.h"
  19. #include <stdint.h>
  20. #include "epid/common/src/memory.h"
  21. typedef struct AllowedBasename {
  22. struct AllowedBasename* next; ///< pointer to the next base name
  23. size_t length; ///< size of base name
  24. uint8_t name[1]; ///< base name (flexible array)
  25. } AllowedBasename;
  26. typedef struct AllowedBasenames { AllowedBasename* data; } AllowedBasenames;
  27. /// Creates empty list of allowed basenames
  28. EpidStatus CreateBasenames(AllowedBasenames** basename_container) {
  29. AllowedBasenames* new_container = NULL;
  30. if (!basename_container) {
  31. return kEpidBadArgErr;
  32. }
  33. new_container = SAFE_ALLOC(sizeof(AllowedBasenames));
  34. if (!new_container) {
  35. return kEpidMemAllocErr;
  36. }
  37. new_container->data = NULL;
  38. *basename_container = new_container;
  39. return kEpidNoErr;
  40. }
  41. /// Checks if given basename is in the allowed list
  42. bool IsBasenameAllowed(AllowedBasenames const* basenames, void const* basename,
  43. size_t length) {
  44. if (!basenames || !length) {
  45. return false;
  46. } else {
  47. AllowedBasename* rootnode = basenames->data;
  48. while (rootnode != NULL) {
  49. if (rootnode->length == length) {
  50. if (!memcmp(rootnode->name, basename, length)) {
  51. return true;
  52. }
  53. }
  54. rootnode = rootnode->next;
  55. }
  56. }
  57. return false;
  58. }
  59. /// Adds a new allowed basename
  60. EpidStatus AllowBasename(AllowedBasenames* basenames, void const* basename,
  61. size_t length) {
  62. AllowedBasename* newnode = NULL;
  63. if (length > (SIZE_MAX - sizeof(AllowedBasename)) + 1) {
  64. return kEpidBadArgErr;
  65. }
  66. if (!basenames || !basename) {
  67. return kEpidBadArgErr;
  68. }
  69. newnode = SAFE_ALLOC(sizeof(AllowedBasename) + (length - 1));
  70. if (!newnode) {
  71. return kEpidMemAllocErr;
  72. }
  73. newnode->next = NULL;
  74. newnode->length = length;
  75. // Memory copy is used to copy a flexible array
  76. if (0 != memcpy_S(newnode->name, length, basename, length)) {
  77. SAFE_FREE(newnode);
  78. return kEpidBadArgErr;
  79. }
  80. if (!basenames->data) {
  81. basenames->data = newnode;
  82. } else {
  83. AllowedBasename* currentnode = basenames->data;
  84. while (NULL != currentnode->next) {
  85. currentnode = currentnode->next;
  86. }
  87. currentnode->next = newnode;
  88. }
  89. return kEpidNoErr;
  90. }
  91. /// Deletes list of allowed basenames
  92. void DeleteBasenames(AllowedBasenames** basename_container) {
  93. if (basename_container && *basename_container) {
  94. AllowedBasename* rootnode = (*basename_container)->data;
  95. while (rootnode) {
  96. AllowedBasename* deletenode = rootnode;
  97. rootnode = rootnode->next;
  98. SAFE_FREE(deletenode);
  99. }
  100. (*basename_container)->data = NULL;
  101. SAFE_FREE(*basename_container);
  102. }
  103. }