]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/SecOTRRemote.c
Security-57740.60.18.tar.gz
[apple/security.git] / OSX / sec / securityd / SecOTRRemote.c
1 /*
2 * Copyright (c) 2014 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
25 #include <stdio.h>
26 #include "SecOTRRemote.h"
27 #include <Security/SecOTRSession.h>
28 #include <Security/SecOTRIdentityPriv.h>
29 #include <securityd/SecItemServer.h>
30 #include <securityd/SOSCloudCircleServer.h>
31
32
33 #include "SOSAccountPriv.h"
34
35 CFDataRef SecOTRSessionCreateRemote_internal(CFDataRef publicAccountData, CFDataRef publicPeerId, CFDataRef privateAccountData, CFErrorRef *error) {
36 SOSDataSourceFactoryRef ds = SecItemDataSourceFactoryGetDefault();
37
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;
47
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);
52
53 privateKeyRef = SOSAccountCopyDeviceKey(privateAccount, error);
54 require_quiet(privateKeyRef, fail);
55 CFReleaseNull(privateAccount);
56
57 privateIdentity = SecOTRFullIdentityCreateFromSecKeyRef(kCFAllocatorDefault, privateKeyRef, error);
58 require_quiet(privateIdentity, fail);
59 CFReleaseNull(privateKeyRef);
60
61
62 publicKeyString = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, publicPeerId, kCFStringEncodingUTF8);
63 require_quiet(publicKeyString, fail);
64
65
66 publicAccount = (publicAccountData == NULL) ? CFRetainSafe(SOSKeychainAccountGetSharedAccount()) : SOSAccountCreateFromData(kCFAllocatorDefault, publicAccountData, ds, error);
67 require_quiet(publicAccount, fail);
68
69 publicKeyRef = SOSAccountCopyPublicKeyForPeer(publicAccount, publicKeyString, error);
70 require_quiet(publicKeyRef, fail);
71 CFReleaseNull(publicAccount);
72
73 publicIdentity = SecOTRPublicIdentityCreateFromSecKeyRef(kCFAllocatorDefault, publicKeyRef, error);
74 require_quiet(publicIdentity, fail);
75 CFReleaseNull(publicKeyRef);
76
77 ourSession = SecOTRSessionCreateFromID(kCFAllocatorDefault, privateIdentity, publicIdentity);
78
79 CFMutableDataRef exportSession = CFDataCreateMutable(kCFAllocatorDefault, 0);
80 SecOTRSAppendSerialization(ourSession, exportSession);
81
82 result = exportSession;
83 exportSession = NULL;
84
85 fail:
86 CFReleaseNull(ourSession);
87 CFReleaseNull(publicKeyString);
88 CFReleaseNull(privateAccount);
89 CFReleaseNull(publicAccount);
90 CFReleaseNull(privateKeyRef);
91 CFReleaseNull(publicKeyRef);
92 CFReleaseNull(publicIdentity);
93 CFReleaseNull(privateIdentity);
94
95 return result;
96 }
97
98 CFDataRef _SecOTRSessionCreateRemote(CFDataRef publicPeerId, CFErrorRef *error) {
99 return SecOTRSessionCreateRemote_internal(NULL, publicPeerId, NULL, error);
100 }
101
102 bool _SecOTRSessionProcessPacketRemote(CFDataRef sessionData, CFDataRef inputPacket, CFDataRef* outputSessionData, CFDataRef* outputPacket, bool *readyForMessages, CFErrorRef *error) {
103
104 SecOTRSessionRef session = SecOTRSessionCreateFromData(kCFAllocatorDefault, sessionData);
105
106 CFMutableDataRef negotiationResponse = CFDataCreateMutable(kCFAllocatorDefault, 0);
107
108 if (inputPacket) {
109 SecOTRSProcessPacket(session, inputPacket, negotiationResponse);
110 } else {
111 SecOTRSAppendStartPacket(session, negotiationResponse);
112 }
113
114 CFMutableDataRef outputSession = CFDataCreateMutable(kCFAllocatorDefault, 0);
115
116 SecOTRSAppendSerialization(session, outputSession);
117 *outputSessionData = outputSession;
118
119 *outputPacket = negotiationResponse;
120
121 *readyForMessages = SecOTRSGetIsReadyForMessages(session);
122
123 return true;
124 }
125