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@
24 #import "OTBottledPeer.h"
27 #import <SecurityFoundation/SFEncryptionOperation.h>
28 #import <SecurityFoundation/SFSigningOperation.h>
29 #import <SecurityFoundation/SFDigestOperation.h>
30 #import <SecurityFoundation/SFKey.h>
31 #import <SecurityFoundation/SFKey_Private.h>
33 #import <Security/SecKeyPriv.h>
35 #import <corecrypto/cchkdf.h>
36 #import <corecrypto/ccsha2.h>
37 #import <corecrypto/ccec.h>
39 #import <utilities/debugging.h>
41 #import <CommonCrypto/CommonRandomSPI.h>
45 #import "OTBottleContents.h"
49 #import "OTPrivateKey.h"
50 #import "OTPrivateKey+SF.h"
51 #import "OTAuthenticatedCiphertext.h"
52 #import "OTAuthenticatedCiphertext+SF.h"
54 @interface OTBottledPeer ()
56 @property (nonatomic, strong) NSString* peerID;
57 @property (nonatomic, strong) NSString* spID;
58 @property (nonatomic, strong) SFECKeyPair* peerSigningKey;
59 @property (nonatomic, strong) SFECKeyPair* peerEncryptionKey;
60 @property (nonatomic, strong) NSData* data;
64 @implementation OTBottledPeer
66 + (SFAuthenticatedEncryptionOperation *) encryptionOperation
68 SFAESKeySpecifier *keySpecifier = [[SFAESKeySpecifier alloc] initWithBitSize:SFAESKeyBitSize256];
69 return [[SFAuthenticatedEncryptionOperation alloc] initWithKeySpecifier:keySpecifier];
72 // Given a peer's details including private key material, and
73 // the keys generated from the escrow secret, encrypt the peer private keys,
74 // make a bottled peer object and serialize it into data.
75 - (nullable instancetype) initWithPeerID:(NSString * _Nullable)peerID
76 spID:(NSString * _Nullable)spID
77 peerSigningKey:(SFECKeyPair *)peerSigningKey
78 peerEncryptionKey:(SFECKeyPair *)peerEncryptionKey
79 escrowKeys:(OTEscrowKeys *)escrowKeys
80 error:(NSError**)error
85 // Serialize the peer private keys into "contents"
86 OTBottleContents *contentsObj = [[OTBottleContents alloc] init];
87 contentsObj.peerSigningPrivKey = [OTPrivateKey fromECKeyPair:peerSigningKey];
88 contentsObj.peerEncryptionPrivKey = [OTPrivateKey fromECKeyPair:peerEncryptionKey];
89 NSData *clearContentsData = contentsObj.data;
91 // Encrypt the contents
92 SFAuthenticatedEncryptionOperation *op = [OTBottledPeer encryptionOperation];
93 SFAuthenticatedCiphertext* cipher = [op encrypt:clearContentsData withKey:escrowKeys.symmetricKey error:error];
98 // Serialize the whole thing
99 OTBottle *obj = [[OTBottle alloc] init];
102 obj.escrowedSigningSPKI = [escrowKeys.signingKey.publicKey encodeSubjectPublicKeyInfo];
103 obj.escrowedEncryptionSPKI = [escrowKeys.encryptionKey.publicKey encodeSubjectPublicKeyInfo];
104 obj.peerSigningSPKI = [peerSigningKey.publicKey encodeSubjectPublicKeyInfo];
105 obj.peerEncryptionSPKI = [peerEncryptionKey.publicKey encodeSubjectPublicKeyInfo];
106 obj.contents = [OTAuthenticatedCiphertext fromSFAuthenticatedCiphertext:cipher];
108 _peerID = [peerID copy];
110 _peerSigningKey = peerSigningKey;
111 _peerEncryptionKey = peerEncryptionKey;
118 // Deserialize a bottle and decrypt the contents (peer keys)
119 // using the keys generated from the escrow secret.
120 - (nullable instancetype) initWithData:(NSData *)data
121 escrowKeys:(OTEscrowKeys *)escrowKeys
122 error:(NSError**)error
127 NSError* localError =nil;
128 // Deserialize the whole thing
129 OTBottle *obj = [[OTBottle alloc] initWithData:data];
131 secerror("octagon: failed to deserialize data into OTBottle");
133 *error = [NSError errorWithDomain:OctagonErrorDomain code:OTErrorDeserializationFailure userInfo:@{NSLocalizedDescriptionKey: @"Failed to deserialize bottle peer"}];
139 SFAuthenticatedEncryptionOperation *op = [OTBottledPeer encryptionOperation];
140 SFAuthenticatedCiphertext* ciphertext = [obj.contents asSFAuthenticatedCiphertext];
141 NSData* clearContentsData = [op decrypt:ciphertext withKey:escrowKeys.symmetricKey error:&localError];
142 if (!clearContentsData || clearContentsData.length == 0) {
143 secerror("octagon: could not decrypt bottle contents: %@", localError);
150 // Deserialize contents into private peer keys
151 OTBottleContents *contentsObj = [[OTBottleContents alloc] initWithData:clearContentsData];
153 secerror("octagon: could not deserialize bottle contents");
155 *error = [NSError errorWithDomain:OctagonErrorDomain code:OTErrorDeserializationFailure userInfo:@{NSLocalizedDescriptionKey: @"Failed to deserialize bottle contents"}];
160 _peerID = obj.peerID;
162 if (self.keyType != OTPrivateKey_KeyType_EC_NIST_CURVES) {
165 return [[SFECKeyPair alloc] initWithSecKey:[EscrowKeys createSecKey:self.keyData]];
167 _peerSigningKey = [contentsObj.peerSigningPrivKey asECKeyPair];
168 _peerEncryptionKey = [contentsObj.peerEncryptionPrivKey asECKeyPair];
169 if (!_peerSigningKey || !_peerEncryptionKey) {
170 secerror("octagon: could not get private EC keys from bottle contents");
172 *error = [NSError errorWithDomain:OctagonErrorDomain code:OTErrorPrivateKeyFailure userInfo:@{NSLocalizedDescriptionKey: @"Failed to instantiate octagon peer keys"}];
178 SFECPublicKey *peerSigningPubKey = [SFECPublicKey keyWithSubjectPublicKeyInfo:obj.peerSigningSPKI];
179 SFECPublicKey *peerEncryptionPubKey = [SFECPublicKey keyWithSubjectPublicKeyInfo:obj.peerEncryptionSPKI];
181 // Check the private keys match the public keys
182 if (![_peerSigningKey.publicKey isEqual:peerSigningPubKey]) {
183 secerror("octagon: public and private peer signing keys do not match");
185 *error = [NSError errorWithDomain:OctagonErrorDomain code:OTErrorPrivateKeyFailure userInfo:@{NSLocalizedDescriptionKey: @"public and private peer signing keys do not match"}];
189 if (![_peerEncryptionKey.publicKey isEqual:peerEncryptionPubKey]) {
190 secerror("octagon: public and private peer encryption keys do not match");
192 *error = [NSError errorWithDomain:OctagonErrorDomain code:OTErrorPrivateKeyFailure userInfo:@{NSLocalizedDescriptionKey: @"public and private peer encryption keys do not match"}];