2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 #import "keychain/ckks/CKKSPeer.h"
27 #import "keychain/ckks/CKKSViewManager.h"
29 NSString* const CKKSSOSPeerPrefix = @"spid-";
31 @implementation CKKSSelves
32 - (instancetype)initWithCurrent:(id<CKKSSelfPeer>)selfPeer
33 allSelves:(NSSet<id<CKKSSelfPeer>>*)allSelves {
34 if((self = [super init])) {
35 _currentSelf = selfPeer;
37 // Ensure allSelves contains selfPeer
38 _allSelves = allSelves ? [allSelves setByAddingObject:selfPeer] :
39 (selfPeer ? [NSSet setWithObject:selfPeer] : [NSSet set]);
44 - (NSString*)description {
45 NSMutableSet* pastSelves = [self.allSelves mutableCopy];
46 [pastSelves removeObject:self.currentSelf];
47 return [NSString stringWithFormat:@"<CKKSSelves: %@ %@>", self.currentSelf, pastSelves.count == 0u ? @"(no past selves)" : pastSelves ];
52 #pragma mark - CKKSActualPeer
54 @implementation CKKSActualPeer
55 - (NSString*)description {
56 // Return the first 16 bytes of the public keys (for reading purposes)
57 return [NSString stringWithFormat:@"<CKKSActualPeer(%@): pubEnc:%@ pubSign:%@ views:%d>",
59 [self.publicEncryptionKey.keyData subdataWithRange:NSMakeRange(0, MIN(16u,self.publicEncryptionKey.keyData.length))],
60 [self.publicSigningKey.keyData subdataWithRange:NSMakeRange(0, MIN(16u,self.publicSigningKey.keyData.length))],
61 (int)self.viewList.count];
64 - (instancetype)initWithPeerID:(NSString*)syncingPeerID
65 encryptionPublicKey:(SFECPublicKey*)encryptionKey
66 signingPublicKey:(SFECPublicKey*)signingKey
67 viewList:(NSSet<NSString*>*)viewList
69 if((self = [super init])) {
70 _peerID = syncingPeerID;
72 _publicEncryptionKey = encryptionKey;
73 _publicSigningKey = signingKey;
79 - (bool)matchesPeer:(id<CKKSPeer>)peer {
80 return (self.peerID == nil && peer.peerID == nil) ||
81 [self.peerID isEqualToString:peer.peerID];
84 - (BOOL)shouldHaveView:(NSString *)viewName
86 return [self.viewList containsObject:viewName];
89 + (BOOL)supportsSecureCoding {
93 - (void)encodeWithCoder:(nonnull NSCoder*)coder
95 [coder encodeObject:self.peerID forKey:@"peerID"];
96 [coder encodeObject:self.publicEncryptionKey.encodeSubjectPublicKeyInfo forKey:@"encryptionKey"];
97 [coder encodeObject:self.publicSigningKey.encodeSubjectPublicKeyInfo forKey:@"signingKey"];
98 [coder encodeObject:self.viewList forKey:@"viewList"];
101 - (nullable instancetype)initWithCoder:(nonnull NSCoder*)decoder
103 if ((self = [super init])) {
104 _peerID = [decoder decodeObjectOfClass:[NSString class] forKey:@"peerID"];
106 NSData* encryptionSPKI = [decoder decodeObjectOfClass:[NSData class] forKey:@"encryptionKey"];
108 _publicEncryptionKey = [SFECPublicKey keyWithSubjectPublicKeyInfo:encryptionSPKI];
111 NSData* signingSPKI = [decoder decodeObjectOfClass:[NSData class] forKey:@"signingKey"];
113 _publicSigningKey = [SFECPublicKey keyWithSubjectPublicKeyInfo:signingSPKI];
116 _viewList = [decoder decodeObjectOfClasses:[NSSet setWithArray:@[[NSSet class], [NSString class]]] forKey:@"viewList"];
122 #pragma mark - CKKSSOSPeer
124 @interface CKKSSOSPeer ()
125 @property NSString* spid;
126 @property NSSet<NSString*>* viewList;
129 @implementation CKKSSOSPeer
130 @synthesize publicEncryptionKey = _publicEncryptionKey;
131 @synthesize publicSigningKey = _publicSigningKey;
133 - (NSString*)description {
134 // Return the first 16 bytes of the public keys (for reading purposes)
135 return [NSString stringWithFormat:@"<CKKSSOSPeer(%@): pubEnc:%@ pubSign:%@ views:%d>",
137 [self.publicEncryptionKey.keyData subdataWithRange:NSMakeRange(0, MIN(16u,self.publicEncryptionKey.keyData.length))],
138 [self.publicSigningKey.keyData subdataWithRange:NSMakeRange(0, MIN(16u,self.publicSigningKey.keyData.length))],
139 (int)self.viewList.count];
142 - (instancetype)initWithSOSPeerID:(NSString*)syncingPeerID
143 encryptionPublicKey:(SFECPublicKey*)encryptionKey
144 signingPublicKey:(SFECPublicKey*)signingKey
145 viewList:(NSSet<NSString*>* _Nullable)viewList
147 if((self = [super init])) {
148 if([syncingPeerID hasPrefix:CKKSSOSPeerPrefix]) {
149 _spid = [syncingPeerID substringFromIndex:CKKSSOSPeerPrefix.length];
151 _spid = syncingPeerID;
153 _publicEncryptionKey = encryptionKey;
154 _publicSigningKey = signingKey;
155 _viewList = viewList;
160 - (NSString*)peerID {
161 return [NSString stringWithFormat:@"%@%@", CKKSSOSPeerPrefix, self.spid];
164 - (bool)matchesPeer:(id<CKKSPeer>)peer {
165 return (self.peerID == nil && peer.peerID == nil) ||
166 [self.peerID isEqualToString:peer.peerID];
169 - (BOOL)shouldHaveView:(NSString *)viewName
171 return [self.viewList containsObject:viewName];
174 + (BOOL)supportsSecureCoding {
178 - (void)encodeWithCoder:(nonnull NSCoder*)coder
180 [coder encodeObject:self.spid forKey:@"spid"];
181 [coder encodeObject:self.publicEncryptionKey.encodeSubjectPublicKeyInfo forKey:@"encryptionKey"];
182 [coder encodeObject:self.publicSigningKey.encodeSubjectPublicKeyInfo forKey:@"signingKey"];
185 - (nullable instancetype)initWithCoder:(nonnull NSCoder*)decoder
187 if ((self = [super init])) {
188 _spid = [decoder decodeObjectOfClass:[NSString class] forKey:@"spid"];
190 NSData* encryptionSPKI = [decoder decodeObjectOfClass:[NSData class] forKey:@"encryptionKey"];
192 _publicEncryptionKey = [SFECPublicKey keyWithSubjectPublicKeyInfo:encryptionSPKI];
195 NSData* signingSPKI = [decoder decodeObjectOfClass:[NSData class] forKey:@"signingKey"];
197 _publicSigningKey = [SFECPublicKey keyWithSubjectPublicKeyInfo:signingSPKI];
204 @interface CKKSSOSSelfPeer ()
205 @property NSString* spid;
208 @implementation CKKSSOSSelfPeer
209 - (NSString*)description {
210 return [NSString stringWithFormat:@"<CKKSSOSSelfPeer(%@): pubEnc:%@ pubSign:%@ views:%d>",
212 [self.publicEncryptionKey.keyData subdataWithRange:NSMakeRange(0, MIN(16u,self.publicEncryptionKey.keyData.length))],
213 [self.publicSigningKey.keyData subdataWithRange:NSMakeRange(0, MIN(16u,self.publicSigningKey.keyData.length))],
214 (int)self.viewList.count];
217 - (instancetype)initWithSOSPeerID:(NSString*)syncingPeerID
218 encryptionKey:(SFECKeyPair*)encryptionKey
219 signingKey:(SFECKeyPair*)signingKey
220 viewList:(NSSet<NSString*>* _Nullable)viewList
222 if((self = [super init])) {
223 if([syncingPeerID hasPrefix:CKKSSOSPeerPrefix]) {
224 _spid = [syncingPeerID substringFromIndex:CKKSSOSPeerPrefix.length];
226 _spid = syncingPeerID;
228 _encryptionKey = encryptionKey;
229 _signingKey = signingKey;
230 _viewList = viewList;
235 -(SFECPublicKey*)publicEncryptionKey {
236 return self.encryptionKey.publicKey;
238 -(SFECPublicKey*)publicSigningKey {
239 return self.signingKey.publicKey;
241 - (NSString*)peerID {
242 return [NSString stringWithFormat:@"%@%@", CKKSSOSPeerPrefix, self.spid];
245 - (bool)matchesPeer:(id<CKKSPeer>)peer {
246 return (self.peerID == nil && peer.peerID == nil) ||
247 [self.peerID isEqualToString:peer.peerID];
250 - (BOOL)shouldHaveView:(NSString *)viewName
252 return [self.viewList containsObject:viewName];
256 NSSet<Class>* CKKSPeerClasses(void)
258 return [NSSet setWithArray:@[[CKKSSOSPeer class], [CKKSActualPeer class]]];