2 * Copyright (c) 2016 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@
23 #include <Security/CodeSigning.h>
24 #include <Security/SecCodePriv.h>
27 #include <security_utilities/cfutilities.h>
28 #include <security_utilities/cfmunge.h>
29 #include <security_utilities/logging.h>
30 #include "codedirectory.h"
35 request(xpc_connection_t peer
, xpc_object_t event
)
39 pid_t pid
= (pid_t
)xpc_dictionary_get_int64(event
, "pid");
43 xpc_object_t reply
= xpc_dictionary_create_reply(event
);
47 CFTemp
<CFDictionaryRef
> attributes("{%O=%d}", kSecGuestAttributePid
, pid
);
48 CFRef
<SecCodeRef
> code
;
49 if ((rc
= SecCodeCopyGuestWithAttributes(NULL
, attributes
, kSecCSDefaultFlags
, &code
.aref())) == noErr
) {
51 // path to base of client code
52 CFRef
<CFURLRef
> codePath
;
53 if ((rc
= SecCodeCopyPath(code
, kSecCSDefaultFlags
, &codePath
.aref())) == noErr
) {
54 CFRef
<CFDataRef
> data
= CFURLCreateData(NULL
, codePath
, kCFStringEncodingUTF8
, true);
55 xpc_dictionary_set_data(reply
, "bundleURL", CFDataGetBytePtr(data
), CFDataGetLength(data
));
58 // if the caller wants the Info.plist, get it and verify the hash passed by the caller
60 if (const void *iphash
= xpc_dictionary_get_data(event
, "infohash", &iphLength
)) {
61 if (CFRef
<CFDataRef
> data
= SecCodeCopyComponent(code
, Security::CodeSigning::cdInfoSlot
, CFTempData(iphash
, iphLength
))) {
62 xpc_dictionary_set_data(reply
, "infoPlist", CFDataGetBytePtr(data
), CFDataGetLength(data
));
66 xpc_connection_send_message(peer
, reply
);
71 static void CodeSigningHelper_peer_event_handler(xpc_connection_t peer
, xpc_object_t event
)
73 xpc_type_t type
= xpc_get_type(event
);
74 if (type
== XPC_TYPE_ERROR
)
77 assert(type
== XPC_TYPE_DICTIONARY
);
79 const char *cmd
= xpc_dictionary_get_string(event
, "command");
81 xpc_connection_cancel(peer
);
82 } else if (strcmp(cmd
, "fetchData") == 0)
85 Syslog::error("peer sent invalid command %s", cmd
);
86 xpc_connection_cancel(peer
);
91 static void CodeSigningHelper_event_handler(xpc_connection_t peer
)
93 xpc_connection_set_event_handler(peer
, ^(xpc_object_t event
) {
94 CodeSigningHelper_peer_event_handler(peer
, event
);
96 xpc_connection_resume(peer
);
99 int main(int argc
, const char *argv
[])
102 if (sandbox_init("com.apple.CodeSigningHelper", SANDBOX_NAMED
, &error
)) {
103 Syslog::error("failed to enter sandbox: %s", error
);
106 xpc_main(CodeSigningHelper_event_handler
);