]> git.saurik.com Git - apple/security.git/blob - keychain/trust/TrustedPeers/TPPeerPermanentInfo.m
Security-58286.1.32.tar.gz
[apple/security.git] / keychain / trust / TrustedPeers / TPPeerPermanentInfo.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 "TPPeerPermanentInfo.h"
25 #import "TPUtils.h"
26
27 static const NSString *kMachineID = @"machineID";
28 static const NSString *kModelID = @"modelID";
29 static const NSString *kEpoch = @"epoch";
30 static const NSString *kTrustSigningKey = @"trustSigningKey";
31
32
33 @interface TPPeerPermanentInfo ()
34
35 @property (nonatomic, strong) NSString* machineID;
36 @property (nonatomic, strong) NSString* modelID;
37 @property (nonatomic, assign) TPCounter epoch;
38 @property (nonatomic, strong) id<TPSigningKey> trustSigningKey;
39 @property (nonatomic, strong) NSData *permanentInfoPList;
40 @property (nonatomic, strong) NSData *permanentInfoSig;
41 @property (nonatomic, strong) NSString *peerID;
42
43 @end
44
45
46 @implementation TPPeerPermanentInfo
47
48 + (instancetype)permanentInfoWithMachineID:(NSString *)machineID
49 modelID:(NSString *)modelID
50 epoch:(TPCounter)epoch
51 trustSigningKey:(id<TPSigningKey>)trustSigningKey
52 peerIDHashAlgo:(TPHashAlgo)peerIDHashAlgo
53 error:(NSError **)error
54 {
55 TPPeerPermanentInfo* info = [[TPPeerPermanentInfo alloc] init];
56 info.machineID = [machineID copy];
57 info.modelID = [modelID copy];
58 info.epoch = epoch;
59 info.trustSigningKey = trustSigningKey;
60
61 NSDictionary *dict = @{
62 kMachineID: machineID,
63 kModelID: modelID,
64 kEpoch: @(epoch),
65 kTrustSigningKey: [trustSigningKey publicKey]
66 };
67 NSData *data = [TPUtils serializedPListWithDictionary:dict];
68 NSData *sig = [trustSigningKey signatureForData:data withError:error];
69 if (nil == sig) {
70 return nil;
71 }
72 info.permanentInfoPList = data;
73 info.permanentInfoSig = sig;
74 info.peerID = [TPPeerPermanentInfo peerIDForPermanentInfoPList:data
75 permanentInfoSig:sig
76 peerIDHashAlgo:peerIDHashAlgo];
77 return info;
78 }
79
80 + (NSString *)peerIDForPermanentInfoPList:(NSData *)permanentInfoPList
81 permanentInfoSig:(NSData *)permanentInfoSig
82 peerIDHashAlgo:(TPHashAlgo)peerIDHashAlgo
83
84 {
85 TPHashBuilder *hasher = [[TPHashBuilder alloc] initWithAlgo:peerIDHashAlgo];
86 [hasher updateWithData:permanentInfoPList];
87 [hasher updateWithData:permanentInfoSig];
88 return [hasher finalHash];
89 }
90
91 + (instancetype)permanentInfoWithPeerID:(NSString *)peerID
92 permanentInfoPList:(NSData *)permanentInfoPList
93 permanentInfoSig:(NSData *)permanentInfoSig
94 keyFactory:(id<TPSigningKeyFactory>)keyFactory
95 {
96 id obj = [NSPropertyListSerialization propertyListWithData:permanentInfoPList
97 options:NSPropertyListImmutable
98 format:nil
99 error:NULL];
100 if (![obj isKindOfClass:[NSDictionary class]]) {
101 return nil;
102 }
103 NSDictionary *dict = obj;
104
105 TPPeerPermanentInfo *info = [[TPPeerPermanentInfo alloc] init];
106 info.peerID = peerID;
107 info.permanentInfoPList = permanentInfoPList;
108 info.permanentInfoSig = permanentInfoSig;
109
110 if (![dict[kMachineID] isKindOfClass:[NSString class]]) {
111 return nil;
112 }
113 info.machineID = dict[kMachineID];
114
115 if (![dict[kModelID] isKindOfClass:[NSString class]]) {
116 return nil;
117 }
118 info.modelID = dict[kModelID];
119
120 if (![dict[kEpoch] isKindOfClass:[NSNumber class]]) {
121 return nil;
122 }
123 info.epoch = [dict[kEpoch] unsignedLongLongValue];
124
125 if (![dict[kTrustSigningKey] isKindOfClass:[NSData class]]) {
126 return nil;
127 }
128 info.trustSigningKey = [keyFactory keyWithPublicKeyData:dict[kTrustSigningKey]];
129 if (nil == info.trustSigningKey) {
130 return nil;
131 }
132 if (![info.trustSigningKey checkSignature:permanentInfoSig matchesData:permanentInfoPList]) {
133 return nil;
134 }
135
136 // check peerID is hash of (permanentInfoPList + permanentInfoSig)
137 TPHashAlgo algo = [TPHashBuilder algoOfHash:peerID];
138 if (algo == kTPHashAlgoUnknown) {
139 return nil;
140 }
141 NSString* checkHash = [TPPeerPermanentInfo peerIDForPermanentInfoPList:info.permanentInfoPList
142 permanentInfoSig:info.permanentInfoSig
143 peerIDHashAlgo:algo];
144 if (![checkHash isEqualToString:peerID]) {
145 return nil;
146 }
147
148 return info;
149 }
150
151 @end