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(ds
, fail
);
49 require_quiet(publicPeerId
, fail
);
50 privateAccount
= (privateAccountData
== NULL
) ? CFRetainSafe(SOSKeychainAccountGetSharedAccount()) : SOSAccountCreateFromData(kCFAllocatorDefault
, privateAccountData
, ds
, error
);
51 require_quiet(privateAccount
, fail
);
53 privateKeyRef
= SOSAccountCopyDeviceKey(privateAccount
, error
);
54 require_quiet(privateKeyRef
, fail
);
55 CFReleaseNull(privateAccount
);
57 privateIdentity
= SecOTRFullIdentityCreateFromSecKeyRef(kCFAllocatorDefault
, privateKeyRef
, error
);
58 require_quiet(privateIdentity
, fail
);
59 CFReleaseNull(privateKeyRef
);
62 publicKeyString
= CFStringCreateFromExternalRepresentation(kCFAllocatorDefault
, publicPeerId
, kCFStringEncodingUTF8
);
63 require_quiet(publicKeyString
, fail
);
66 publicAccount
= (publicAccountData
== NULL
) ? CFRetainSafe(SOSKeychainAccountGetSharedAccount()) : SOSAccountCreateFromData(kCFAllocatorDefault
, publicAccountData
, ds
, error
);
67 require_quiet(publicAccount
, fail
);
69 publicKeyRef
= SOSAccountCopyPublicKeyForPeer(publicAccount
, publicKeyString
, error
);
70 require_quiet(publicKeyRef
, fail
);
71 CFReleaseNull(publicAccount
);
73 publicIdentity
= SecOTRPublicIdentityCreateFromSecKeyRef(kCFAllocatorDefault
, publicKeyRef
, error
);
74 require_quiet(publicIdentity
, fail
);
75 CFReleaseNull(publicKeyRef
);
77 ourSession
= SecOTRSessionCreateFromID(kCFAllocatorDefault
, privateIdentity
, publicIdentity
);
79 CFMutableDataRef exportSession
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
80 SecOTRSAppendSerialization(ourSession
, exportSession
);
82 result
= exportSession
;
86 CFReleaseNull(ourSession
);
87 CFReleaseNull(publicKeyString
);
88 CFReleaseNull(privateAccount
);
89 CFReleaseNull(publicAccount
);
90 CFReleaseNull(privateKeyRef
);
91 CFReleaseNull(publicKeyRef
);
92 CFReleaseNull(publicIdentity
);
93 CFReleaseNull(privateIdentity
);
98 CFDataRef
_SecOTRSessionCreateRemote(CFDataRef publicPeerId
, CFErrorRef
*error
) {
99 return SecOTRSessionCreateRemote_internal(NULL
, publicPeerId
, NULL
, error
);
102 bool _SecOTRSessionProcessPacketRemote(CFDataRef sessionData
, CFDataRef inputPacket
, CFDataRef
* outputSessionData
, CFDataRef
* outputPacket
, bool *readyForMessages
, CFErrorRef
*error
) {
104 SecOTRSessionRef session
= SecOTRSessionCreateFromData(kCFAllocatorDefault
, sessionData
);
106 CFMutableDataRef negotiationResponse
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
109 SecOTRSProcessPacket(session
, inputPacket
, negotiationResponse
);
111 SecOTRSAppendStartPacket(session
, negotiationResponse
);
114 CFMutableDataRef outputSession
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
116 SecOTRSAppendSerialization(session
, outputSession
);
117 *outputSessionData
= outputSession
;
119 *outputPacket
= negotiationResponse
;
121 *readyForMessages
= SecOTRSGetIsReadyForMessages(session
);