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
19 #include "epid/member/src/allowed_basenames.h"
20
21 #include <stdint.h>
22
23 #include "epid/common/src/memory.h"
24
25 typedef struct AllowedBasename {
26 struct AllowedBasename* next; ///< pointer to the next base name
27 size_t length; ///< size of base name
28 uint8_t name[1]; ///< base name (flexible array)
29 } AllowedBasename;
30
31 typedef struct AllowedBasenames { AllowedBasename* data; } AllowedBasenames;
32
33 /// Creates empty list of allowed basenames
CreateBasenames(AllowedBasenames ** basename_container)34 EpidStatus CreateBasenames(AllowedBasenames** basename_container) {
35 AllowedBasenames* new_container = NULL;
36 if (!basename_container) {
37 return kEpidBadArgErr;
38 }
39 new_container = SAFE_ALLOC(sizeof(AllowedBasenames));
40 if (!new_container) {
41 return kEpidMemAllocErr;
42 }
43 new_container->data = NULL;
44 *basename_container = new_container;
45
46 return kEpidNoErr;
47 }
48
49 /// Checks if given basename is in the allowed list
IsBasenameAllowed(AllowedBasenames const * basenames,void const * basename,size_t length)50 bool IsBasenameAllowed(AllowedBasenames const* basenames, void const* basename,
51 size_t length) {
52 if (!basenames || !length) {
53 return false;
54 } else {
55 AllowedBasename* rootnode = basenames->data;
56 while (rootnode != NULL) {
57 if (rootnode->length == length) {
58 if (!memcmp(rootnode->name, basename, length)) {
59 return true;
60 }
61 }
62 rootnode = rootnode->next;
63 }
64 }
65 return false;
66 }
67
68 /// Adds a new allowed basename
AllowBasename(AllowedBasenames * basenames,void const * basename,size_t length)69 EpidStatus AllowBasename(AllowedBasenames* basenames, void const* basename,
70 size_t length) {
71 AllowedBasename* newnode = NULL;
72
73 if (length > (SIZE_MAX - sizeof(AllowedBasename)) + 1) {
74 return kEpidBadArgErr;
75 }
76 if (!basenames || !basename) {
77 return kEpidBadArgErr;
78 }
79
80 newnode = SAFE_ALLOC(sizeof(AllowedBasename) + (length - 1));
81 if (!newnode) {
82 return kEpidMemAllocErr;
83 }
84
85 newnode->next = NULL;
86 newnode->length = length;
87 // Memory copy is used to copy a flexible array
88 if (0 != memcpy_S(newnode->name, length, basename, length)) {
89 SAFE_FREE(newnode);
90 return kEpidBadArgErr;
91 }
92
93 if (!basenames->data) {
94 basenames->data = newnode;
95 } else {
96 AllowedBasename* currentnode = basenames->data;
97 while (NULL != currentnode->next) {
98 currentnode = currentnode->next;
99 }
100 currentnode->next = newnode;
101 }
102 return kEpidNoErr;
103 }
104
105 /// Deletes list of allowed basenames
DeleteBasenames(AllowedBasenames ** basename_container)106 void DeleteBasenames(AllowedBasenames** basename_container) {
107 if (basename_container && *basename_container) {
108 AllowedBasename* rootnode = (*basename_container)->data;
109 while (rootnode) {
110 AllowedBasename* deletenode = rootnode;
111 rootnode = rootnode->next;
112 SAFE_FREE(deletenode);
113 }
114 (*basename_container)->data = NULL;
115 SAFE_FREE(*basename_container);
116 }
117 }
118