]> git.saurik.com Git - apple/security.git/blob - keychain/trust/TrustedPeers/TPPeer.m
Security-58286.41.2.tar.gz
[apple/security.git] / keychain / trust / TrustedPeers / TPPeer.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 "TPPeer.h"
25 #import "TPPeerPermanentInfo.h"
26 #import "TPPeerStableInfo.h"
27 #import "TPPeerDynamicInfo.h"
28 #import "TPCircle.h"
29 #import "TPVoucher.h"
30
31 @interface TPPeer ()
32
33 @property (nonatomic, strong) TPPeerPermanentInfo* permanentInfo;
34 @property (nonatomic, strong) TPPeerStableInfo* stableInfo;
35 @property (nonatomic, strong) TPPeerDynamicInfo* dynamicInfo;
36
37 @end
38
39
40 @implementation TPPeer
41
42 - (NSString *)peerID
43 {
44 return self.permanentInfo.peerID;
45 }
46
47 - (instancetype)initWithPermanentInfo:(TPPeerPermanentInfo *)permanentInfo
48 {
49 self = [super init];
50 if (self) {
51 _permanentInfo = permanentInfo;
52 }
53 return self;
54 }
55
56 - (TPResult)updateStableInfo:(TPPeerStableInfo *)stableInfo
57 {
58 if (![self.permanentInfo.trustSigningKey checkSignature:stableInfo.stableInfoSig
59 matchesData:stableInfo.stableInfoPList]) {
60 return TPResultSignatureMismatch;
61 }
62 if ([self.stableInfo isEqualToPeerStableInfo:stableInfo]) {
63 return TPResultOk;
64 }
65 if (self.stableInfo != nil && stableInfo.clock <= self.stableInfo.clock) {
66 return TPResultClockViolation;
67 }
68 self.stableInfo = stableInfo;
69 return TPResultOk;
70 }
71
72 - (TPResult)updateDynamicInfo:(TPPeerDynamicInfo *)dynamicInfo
73 {
74 if (![self.permanentInfo.trustSigningKey checkSignature:dynamicInfo.dynamicInfoSig
75 matchesData:dynamicInfo.dynamicInfoPList]) {
76 return TPResultSignatureMismatch;
77 }
78 if ([self.dynamicInfo isEqualToPeerDynamicInfo:dynamicInfo]) {
79 return TPResultOk;
80 }
81 if (self.dynamicInfo != nil && dynamicInfo.clock <= self.dynamicInfo.clock) {
82 return TPResultClockViolation;
83 }
84 self.dynamicInfo = dynamicInfo;
85 self.circle = nil;
86 return TPResultOk;
87 }
88
89 - (void)setCircle:(TPCircle *)circle
90 {
91 if (nil != circle) {
92 NSAssert([circle.circleID isEqualToString:self.dynamicInfo.circleID],
93 @"circle property must match dynamicInfo.circleID");
94 }
95 _circle = circle;
96 }
97
98 - (NSSet<NSString*> *)trustedPeerIDs
99 {
100 if (self.dynamicInfo) {
101 NSAssert(self.circle, @"dynamicInfo needs corresponding circle");
102 return self.circle.includedPeerIDs;
103 } else {
104 return [NSSet setWithObject:self.peerID];
105 }
106 }
107
108 @end