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"
28 NSString* const CKKSSOSPeerPrefix = @"spid-";
30 @implementation CKKSSelves
31 - (instancetype)initWithCurrent:(id<CKKSSelfPeer>)selfPeer
32 allSelves:(NSSet<id<CKKSSelfPeer>>*)allSelves {
33 if((self = [super init])) {
34 _currentSelf = selfPeer;
36 // Ensure allSelves contains selfPeer
37 _allSelves = allSelves ? [allSelves setByAddingObject:selfPeer] :
38 (selfPeer ? [NSSet setWithObject:selfPeer] : [NSSet set]);
43 - (NSString*)description {
44 NSMutableSet* pastSelves = [self.allSelves mutableCopy];
45 [pastSelves removeObject:self.currentSelf];
46 return [NSString stringWithFormat:@"<CKKSSelves: %@ %@>", self.currentSelf, pastSelves.count == 0u ? @"(no past selves)" : pastSelves ];
51 @interface CKKSSOSPeer ()
52 @property NSString* spid;
55 @implementation CKKSSOSPeer
56 - (NSString*)description {
57 // Return the first 16 bytes of the public keys (for reading purposes)
58 return [NSString stringWithFormat:@"<CKKSSOSPeer(%@): pubEnc:%@ pubSign:%@>",
60 [self.publicEncryptionKey.keyData subdataWithRange:NSMakeRange(0, MIN(16u,self.publicEncryptionKey.keyData.length))],
61 [self.publicSigningKey.keyData subdataWithRange:NSMakeRange(0, MIN(16u,self.publicSigningKey.keyData.length))]];
64 - (instancetype)initWithSOSPeerID:(NSString*)syncingPeerID
65 encryptionPublicKey:(SFECPublicKey*)encryptionKey
66 signingPublicKey:(SFECPublicKey*)signingKey
68 if((self = [super init])) {
69 if([syncingPeerID hasPrefix:CKKSSOSPeerPrefix]) {
70 _spid = [syncingPeerID substringFromIndex:CKKSSOSPeerPrefix.length];
72 _spid = syncingPeerID;
74 _publicEncryptionKey = encryptionKey;
75 _publicSigningKey = signingKey;
81 return [NSString stringWithFormat:@"%@%@", CKKSSOSPeerPrefix, self.spid];
84 - (bool)matchesPeer:(id<CKKSPeer>)peer {
85 return (self.peerID == nil && peer.peerID == nil) ||
86 [self.peerID isEqualToString:peer.peerID];
90 @interface CKKSSOSSelfPeer ()
91 @property NSString* spid;
94 @implementation CKKSSOSSelfPeer
95 - (NSString*)description {
96 return [NSString stringWithFormat:@"<CKKSSOSSelfPeer(%@): pubEnc:%@ pubSign:%@>",
98 [self.publicEncryptionKey.keyData subdataWithRange:NSMakeRange(0, MIN(16u,self.publicEncryptionKey.keyData.length))],
99 [self.publicSigningKey.keyData subdataWithRange:NSMakeRange(0, MIN(16u,self.publicSigningKey.keyData.length))]];
102 - (instancetype)initWithSOSPeerID:(NSString*)syncingPeerID
103 encryptionKey:(SFECKeyPair*)encryptionKey
104 signingKey:(SFECKeyPair*)signingKey
106 if((self = [super init])) {
107 if([syncingPeerID hasPrefix:CKKSSOSPeerPrefix]) {
108 _spid = [syncingPeerID substringFromIndex:CKKSSOSPeerPrefix.length];
110 _spid = syncingPeerID;
112 _encryptionKey = encryptionKey;
113 _signingKey = signingKey;
118 -(SFECPublicKey*)publicEncryptionKey {
119 return self.encryptionKey.publicKey;
121 -(SFECPublicKey*)publicSigningKey {
122 return self.signingKey.publicKey;
124 - (NSString*)peerID {
125 return [NSString stringWithFormat:@"%@%@", CKKSSOSPeerPrefix, self.spid];
128 - (bool)matchesPeer:(id<CKKSPeer>)peer {
129 return (self.peerID == nil && peer.peerID == nil) ||
130 [self.peerID isEqualToString:peer.peerID];