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