]> git.saurik.com Git - apple/security.git/blob - keychain/trust/TrustedPeers/TPPeerDynamicInfo.m
Security-58286.1.32.tar.gz
[apple/security.git] / keychain / trust / TrustedPeers / TPPeerDynamicInfo.m
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #import "TPPeerDynamicInfo.h"
25 #import "TPUtils.h"
26
27 static const NSString *kCircleID = @"circleID";
28 static const NSString *kClique = @"clique";
29 static const NSString *kRemovals = @"removals";
30 static const NSString *kClock = @"clock";
31
32
33 @interface TPPeerDynamicInfo ()
34
35 @property (nonatomic, strong) NSString *circleID;
36 @property (nonatomic, strong) NSString *clique;
37 @property (nonatomic, assign) TPCounter removals;
38 @property (nonatomic, assign) TPCounter clock;
39 @property (nonatomic, strong) NSData *dynamicInfoPList;
40 @property (nonatomic, strong) NSData *dynamicInfoSig;
41
42 @end
43
44
45 @implementation TPPeerDynamicInfo
46
47 + (instancetype)dynamicInfoWithCircleID:(NSString *)circleID
48 clique:(NSString *)clique
49 removals:(TPCounter)removals
50 clock:(TPCounter)clock
51 trustSigningKey:(id<TPSigningKey>)trustSigningKey
52 error:(NSError **)error
53 {
54 NSDictionary *dict = @{
55 kCircleID: circleID,
56 kClique: clique,
57 kRemovals: @(removals),
58 kClock: @(clock)
59 };
60 NSData *data = [TPUtils serializedPListWithDictionary:dict];
61 NSData *sig = [trustSigningKey signatureForData:data withError:error];
62 if (nil == sig) {
63 return nil;
64 }
65 TPPeerDynamicInfo* info = [self dynamicInfoWithPListData:data dynamicInfoSig:sig];
66 assert(info);
67 return info;
68 }
69
70 + (instancetype)dynamicInfoWithPListData:(NSData *)dynamicInfoPList
71 dynamicInfoSig:(NSData *)dynamicInfoSig
72 {
73 id dict = [NSPropertyListSerialization propertyListWithData:dynamicInfoPList
74 options:NSPropertyListImmutable
75 format:nil
76 error:NULL];
77 if (![dict isKindOfClass:[NSDictionary class]]) {
78 return nil;
79 }
80
81 TPPeerDynamicInfo* info = [[TPPeerDynamicInfo alloc] init];
82
83 if (![dict[kCircleID] isKindOfClass:[NSString class]]) {
84 return nil;
85 }
86 info.circleID = dict[kCircleID];
87
88 if (![dict[kClique] isKindOfClass:[NSString class]]) {
89 return nil;
90 }
91 info.clique = dict[kClique];
92
93 if (![dict[kRemovals] isKindOfClass:[NSNumber class]]) {
94 return nil;
95 }
96 info.removals = [dict[kRemovals] unsignedLongLongValue];
97
98 if (![dict[kClock] isKindOfClass:[NSNumber class]]) {
99 return nil;
100 }
101 info.clock = [dict[kClock] unsignedLongLongValue];
102
103 info.dynamicInfoPList = [dynamicInfoPList copy];
104 info.dynamicInfoSig = [dynamicInfoSig copy];
105
106 return info;
107 }
108
109 - (BOOL)isEqualToPeerDynamicInfo:(TPPeerDynamicInfo *)other
110 {
111 if (other == self) {
112 return YES;
113 }
114 return [self.dynamicInfoPList isEqualToData:other.dynamicInfoPList]
115 && [self.dynamicInfoSig isEqualToData:other.dynamicInfoSig];
116 }
117
118 #pragma mark - NSObject
119
120 - (BOOL)isEqual:(id)object
121 {
122 if (self == object) {
123 return YES;
124 }
125 if (![object isKindOfClass:[TPPeerDynamicInfo class]]) {
126 return NO;
127 }
128 return [self isEqualToPeerDynamicInfo:object];
129 }
130
131 @end