2 * Copyright (c) 2014 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 #include "SecOTRRemote.h"
27 #include <Security/SecOTRSession.h>
28 #include <Security/SecOTRIdentityPriv.h>
29 #include <securityd/SecItemServer.h>
30 #include <securityd/SOSCloudCircleServer.h>
33 #include "SOSAccountPriv.h"
35 CFDataRef
SecOTRSessionCreateRemote_internal(CFDataRef publicAccountData
, CFDataRef publicPeerId
, CFDataRef privateAccountData
, CFErrorRef
*error
) {
36 SOSDataSourceFactoryRef ds
= SecItemDataSourceFactoryGetDefault();
38 SOSAccountRef privateAccount
= NULL
;
39 SOSAccountRef publicAccount
= NULL
;
40 CFStringRef publicKeyString
= NULL
;
41 SecKeyRef privateKeyRef
= NULL
;
42 SecKeyRef publicKeyRef
= NULL
;
43 SecOTRFullIdentityRef privateIdentity
= NULL
;
44 SecOTRPublicIdentityRef publicIdentity
= NULL
;
45 CFDataRef result
= NULL
;
46 SecOTRSessionRef ourSession
= NULL
;
48 require_quiet(publicPeerId
, fail
);
49 privateAccount
= (privateAccountData
== NULL
) ? CFRetainSafe(SOSKeychainAccountGetSharedAccount()) : SOSAccountCreateFromData(kCFAllocatorDefault
, privateAccountData
, ds
, error
);
50 require_quiet(privateAccount
, fail
);
52 privateKeyRef
= SOSAccountCopyDeviceKey(privateAccount
, error
);
53 require_quiet(privateKeyRef
, fail
);
54 CFReleaseNull(privateAccount
);
56 privateIdentity
= SecOTRFullIdentityCreateFromSecKeyRef(kCFAllocatorDefault
, privateKeyRef
, error
);
57 require_quiet(privateIdentity
, fail
);
58 CFReleaseNull(privateKeyRef
);
61 publicKeyString
= CFStringCreateFromExternalRepresentation(kCFAllocatorDefault
, publicPeerId
, kCFStringEncodingUTF8
);
62 require_quiet(publicKeyString
, fail
);
65 publicAccount
= (publicAccountData
== NULL
) ? CFRetainSafe(SOSKeychainAccountGetSharedAccount()) : SOSAccountCreateFromData(kCFAllocatorDefault
, publicAccountData
, ds
, error
);
66 require_quiet(publicAccount
, fail
);
68 publicKeyRef
= SOSAccountCopyPublicKeyForPeer(publicAccount
, publicKeyString
, error
);
69 require_quiet(publicKeyRef
, fail
);
70 CFReleaseNull(publicAccount
);
72 publicIdentity
= SecOTRPublicIdentityCreateFromSecKeyRef(kCFAllocatorDefault
, publicKeyRef
, error
);
73 require_quiet(publicIdentity
, fail
);
74 CFReleaseNull(publicKeyRef
);
76 ourSession
= SecOTRSessionCreateFromID(kCFAllocatorDefault
, privateIdentity
, publicIdentity
);
78 CFMutableDataRef exportSession
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
79 SecOTRSAppendSerialization(ourSession
, exportSession
);
81 result
= exportSession
;
85 CFReleaseNull(ourSession
);
86 CFReleaseNull(publicKeyString
);
87 CFReleaseNull(privateAccount
);
88 CFReleaseNull(publicAccount
);
89 CFReleaseNull(privateKeyRef
);
90 CFReleaseNull(publicKeyRef
);
91 CFReleaseNull(publicIdentity
);
92 CFReleaseNull(privateIdentity
);
97 CFDataRef
_SecOTRSessionCreateRemote(CFDataRef publicPeerId
, CFErrorRef
*error
) {
98 return SecOTRSessionCreateRemote_internal(NULL
, publicPeerId
, NULL
, error
);
101 bool _SecOTRSessionProcessPacketRemote(CFDataRef sessionData
, CFDataRef inputPacket
, CFDataRef
* outputSessionData
, CFDataRef
* outputPacket
, bool *readyForMessages
, CFErrorRef
*error
) {
103 SecOTRSessionRef session
= SecOTRSessionCreateFromData(kCFAllocatorDefault
, sessionData
);
105 CFMutableDataRef negotiationResponse
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
108 SecOTRSProcessPacket(session
, inputPacket
, negotiationResponse
);
110 SecOTRSAppendStartPacket(session
, negotiationResponse
);
113 CFMutableDataRef outputSession
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
115 SecOTRSAppendSerialization(session
, outputSession
);
116 *outputSessionData
= outputSession
;
118 *outputPacket
= negotiationResponse
;
120 *readyForMessages
= SecOTRSGetIsReadyForMessages(session
);