]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKSPeer.m
Security-58286.251.4.tar.gz
[apple/security.git] / keychain / ckks / CKKSPeer.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 #if OCTAGON
25
26 #import "keychain/ckks/CKKSPeer.h"
27
28 NSString* const CKKSSOSPeerPrefix = @"spid-";
29
30 @implementation CKKSSelves
31 - (instancetype)initWithCurrent:(id<CKKSSelfPeer>)selfPeer
32 allSelves:(NSSet<id<CKKSSelfPeer>>*)allSelves {
33 if((self = [super init])) {
34 _currentSelf = selfPeer;
35
36 // Ensure allSelves contains selfPeer
37 _allSelves = allSelves ? [allSelves setByAddingObject:selfPeer] :
38 (selfPeer ? [NSSet setWithObject:selfPeer] : [NSSet set]);
39 }
40 return self;
41 }
42
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 ];
47 }
48
49 @end
50
51 @interface CKKSSOSPeer ()
52 @property NSString* spid;
53 @end
54
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:%@>",
59 self.peerID,
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))]];
62 }
63
64 - (instancetype)initWithSOSPeerID:(NSString*)syncingPeerID
65 encryptionPublicKey:(SFECPublicKey*)encryptionKey
66 signingPublicKey:(SFECPublicKey*)signingKey
67 {
68 if((self = [super init])) {
69 if([syncingPeerID hasPrefix:CKKSSOSPeerPrefix]) {
70 _spid = [syncingPeerID substringFromIndex:CKKSSOSPeerPrefix.length];
71 } else {
72 _spid = syncingPeerID;
73 }
74 _publicEncryptionKey = encryptionKey;
75 _publicSigningKey = signingKey;
76 }
77 return self;
78 }
79
80 - (NSString*)peerID {
81 return [NSString stringWithFormat:@"%@%@", CKKSSOSPeerPrefix, self.spid];
82 }
83
84 - (bool)matchesPeer:(id<CKKSPeer>)peer {
85 return (self.peerID == nil && peer.peerID == nil) ||
86 [self.peerID isEqualToString:peer.peerID];
87 }
88 @end
89
90 @interface CKKSSOSSelfPeer ()
91 @property NSString* spid;
92 @end
93
94 @implementation CKKSSOSSelfPeer
95 - (NSString*)description {
96 return [NSString stringWithFormat:@"<CKKSSOSSelfPeer(%@): pubEnc:%@ pubSign:%@>",
97 self.peerID,
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))]];
100 }
101
102 - (instancetype)initWithSOSPeerID:(NSString*)syncingPeerID
103 encryptionKey:(SFECKeyPair*)encryptionKey
104 signingKey:(SFECKeyPair*)signingKey
105 {
106 if((self = [super init])) {
107 if([syncingPeerID hasPrefix:CKKSSOSPeerPrefix]) {
108 _spid = [syncingPeerID substringFromIndex:CKKSSOSPeerPrefix.length];
109 } else {
110 _spid = syncingPeerID;
111 }
112 _encryptionKey = encryptionKey;
113 _signingKey = signingKey;
114 }
115 return self;
116 }
117
118 -(SFECPublicKey*)publicEncryptionKey {
119 return self.encryptionKey.publicKey;
120 }
121 -(SFECPublicKey*)publicSigningKey {
122 return self.signingKey.publicKey;
123 }
124 - (NSString*)peerID {
125 return [NSString stringWithFormat:@"%@%@", CKKSSOSPeerPrefix, self.spid];
126 }
127
128 - (bool)matchesPeer:(id<CKKSPeer>)peer {
129 return (self.peerID == nil && peer.peerID == nil) ||
130 [self.peerID isEqualToString:peer.peerID];
131 }
132 @end
133
134 #endif // OCTAGON