|
| 1 | +/** |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 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 | + |
| 17 | +#import "MTRDefines_Internal.h" |
| 18 | +#import "MTRLogging_Internal.h" |
| 19 | +#import <Matter/MTRAccessGrant.h> |
| 20 | + |
| 21 | +#include <lib/core/CASEAuthTag.h> |
| 22 | +#include <lib/core/GroupId.h> |
| 23 | +#include <lib/core/NodeId.h> |
| 24 | +#include <lib/support/SafeInt.h> |
| 25 | + |
| 26 | +using namespace chip; |
| 27 | + |
| 28 | +MTR_DIRECT_MEMBERS |
| 29 | +@implementation MTRAccessGrant |
| 30 | + |
| 31 | ++ (nullable MTRAccessGrant *)accessGrantForNodeID:(NSNumber *)nodeID privilege:(MTRAccessControlEntryPrivilege)privilege |
| 32 | +{ |
| 33 | + NodeId id = nodeID.unsignedLongLongValue; |
| 34 | + if (!IsOperationalNodeId(id)) { |
| 35 | + MTR_LOG_ERROR("MTRAccessGrant provided non-operational node ID: 0x%llx", id); |
| 36 | + return nil; |
| 37 | + } |
| 38 | + |
| 39 | + return [[MTRAccessGrant alloc] initWithSubject:[nodeID copy] privilege:privilege authenticationMode:MTRAccessControlEntryAuthModeCASE]; |
| 40 | +} |
| 41 | + |
| 42 | ++ (nullable MTRAccessGrant *)accessGrantForCASEAuthenticatedTag:(NSNumber *)caseAuthenticatedTag privilege:(MTRAccessControlEntryPrivilege)privilege |
| 43 | +{ |
| 44 | + auto value = caseAuthenticatedTag.unsignedLongLongValue; |
| 45 | + if (!CanCastTo<CASEAuthTag>(value)) { |
| 46 | + MTR_LOG_ERROR("MTRAccessGrant provided too-large CAT value: 0x%llx", value); |
| 47 | + return nil; |
| 48 | + } |
| 49 | + |
| 50 | + CASEAuthTag tag = static_cast<CASEAuthTag>(value); |
| 51 | + if (!IsValidCASEAuthTag(tag)) { |
| 52 | + MTR_LOG_ERROR("MTRAccessGrant provided invalid CAT value: 0x%" PRIx32, tag); |
| 53 | + return nil; |
| 54 | + } |
| 55 | + |
| 56 | + return [[MTRAccessGrant alloc] initWithSubject:@(NodeIdFromCASEAuthTag(tag)) privilege:privilege authenticationMode:MTRAccessControlEntryAuthModeCASE]; |
| 57 | +} |
| 58 | + |
| 59 | ++ (nullable MTRAccessGrant *)accessGrantForGroupID:(NSNumber *)groupID privilege:(MTRAccessControlEntryPrivilege)privilege |
| 60 | +{ |
| 61 | + auto value = groupID.unsignedLongLongValue; |
| 62 | + if (!CanCastTo<GroupId>(value)) { |
| 63 | + MTR_LOG_ERROR("MTRAccessGrant provided too-large group id: 0x%llx", value); |
| 64 | + return nil; |
| 65 | + } |
| 66 | + |
| 67 | + GroupId id = static_cast<GroupId>(value); |
| 68 | + if (!IsValidGroupId(id)) { |
| 69 | + MTR_LOG_ERROR("MTRAccessGrant provided invalid group id: 0x%" PRIx32, id); |
| 70 | + return nil; |
| 71 | + } |
| 72 | + |
| 73 | + return [[MTRAccessGrant alloc] initWithSubject:@(NodeIdFromGroupId(id)) privilege:privilege authenticationMode:MTRAccessControlEntryAuthModeGroup]; |
| 74 | +} |
| 75 | + |
| 76 | ++ (MTRAccessGrant *)accessGrantForAllNodesWithPrivilege:(MTRAccessControlEntryPrivilege)privilege |
| 77 | +{ |
| 78 | + return [[MTRAccessGrant alloc] initWithSubject:nil privilege:privilege authenticationMode:MTRAccessControlEntryAuthModeCASE]; |
| 79 | +} |
| 80 | + |
| 81 | +// initWithSubject assumes that the subject has already been validated and, if |
| 82 | +// needed, copied from the input. |
| 83 | +- (nullable instancetype)initWithSubject:(nullable NSNumber *)subject privilege:(MTRAccessControlEntryPrivilege)privilege authenticationMode:(MTRAccessControlEntryAuthMode)authenticationMode |
| 84 | +{ |
| 85 | + if (!(self = [super init])) { |
| 86 | + return nil; |
| 87 | + } |
| 88 | + |
| 89 | + _subjectID = subject; |
| 90 | + _grantedPrivilege = privilege; |
| 91 | + _authenticationMode = authenticationMode; |
| 92 | + return self; |
| 93 | +} |
| 94 | + |
| 95 | +- (id)copyWithZone:(NSZone *)zone |
| 96 | +{ |
| 97 | + // We have no mutable state. |
| 98 | + return self; |
| 99 | +} |
| 100 | + |
| 101 | +- (BOOL)isEqual:(id)object |
| 102 | +{ |
| 103 | + if ([object class] != [self class]) { |
| 104 | + return NO; |
| 105 | + } |
| 106 | + |
| 107 | + MTRAccessGrant * other = object; |
| 108 | + |
| 109 | + BOOL sameSubjectID = (_subjectID == nil && other.subjectID == nil) || [_subjectID isEqual:other.subjectID]; |
| 110 | + return sameSubjectID && _grantedPrivilege == other.grantedPrivilege && _authenticationMode == other.authenticationMode; |
| 111 | +} |
| 112 | + |
| 113 | +- (NSUInteger)hash |
| 114 | +{ |
| 115 | + return _subjectID.unsignedLongLongValue ^ _grantedPrivilege ^ _authenticationMode; |
| 116 | +} |
| 117 | + |
| 118 | +- (NSString *)description |
| 119 | +{ |
| 120 | + NSString * privilege = @"Unknown"; |
| 121 | + switch (_grantedPrivilege) { |
| 122 | + case MTRAccessControlEntryPrivilegeView: |
| 123 | + privilege = @"View"; |
| 124 | + break; |
| 125 | + case MTRAccessControlEntryPrivilegeProxyView: |
| 126 | + privilege = @"ProxyView"; |
| 127 | + break; |
| 128 | + case MTRAccessControlEntryPrivilegeOperate: |
| 129 | + privilege = @"Operate"; |
| 130 | + break; |
| 131 | + case MTRAccessControlEntryPrivilegeManage: |
| 132 | + privilege = @"Manage"; |
| 133 | + break; |
| 134 | + case MTRAccessControlEntryPrivilegeAdminister: |
| 135 | + privilege = @"Administer"; |
| 136 | + break; |
| 137 | + } |
| 138 | + |
| 139 | + if (_subjectID == nil) { |
| 140 | + return [NSString stringWithFormat:@"<%@ all nodes can %@>", self.class, privilege]; |
| 141 | + } |
| 142 | + |
| 143 | + NodeId nodeId = static_cast<NodeId>(_subjectID.unsignedLongLongValue); |
| 144 | + if (IsGroupId(nodeId)) { |
| 145 | + return [NSString stringWithFormat:@"<%@ group 0x%x can %@>", self.class, GroupIdFromNodeId(nodeId), privilege]; |
| 146 | + } |
| 147 | + |
| 148 | + if (IsCASEAuthTag(nodeId)) { |
| 149 | + return [NSString stringWithFormat:@"<%@ nodes with CASE Authenticated Tag 0x%08x can %@>", self.class, CASEAuthTagFromNodeId(nodeId), privilege]; |
| 150 | + } |
| 151 | + |
| 152 | + return [NSString stringWithFormat:@"<%@ node 0x%016llx can %@>", self.class, nodeId, privilege]; |
| 153 | +} |
| 154 | + |
| 155 | +@end |
0 commit comments