2 * trustApps.cpp - set list of trusted apps for specified executable
4 #include <Security/Security.h>
7 #include <utilLib/common.h>
8 #include <clAppUtils/identPicker.h>
10 static void usage(char **argv
)
12 printf("Usage: %s keychain [-q(uiet)] executable ...\n", argv
[0]);
16 int main(int argc
, char **argv
)
22 const char *keychainName
= argv
[1];
27 for(nextArg
=2; nextArg
<argc
; ) {
28 char *argp
= argv
[nextArg
];
44 /* create an array of SecTrustedApplications */
45 CFMutableArrayRef appList
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
46 for(; nextArg
<argc
; nextArg
++) {
47 SecTrustedApplicationRef appRef
;
48 ortn
= SecTrustedApplicationCreateFromPath(argv
[nextArg
], &appRef
);
50 cssmPerror("SecTrustedApplicationCreateFromPath", ortn
);
53 CFArrayAppendValue(appList
, appRef
);
56 /* Find a signing identity; extract its private key */
58 ortn
= SecKeychainOpen(keychainName
, &kcRef
);
60 cssmPerror("SecKeychainOpen", ortn
);
63 SecIdentityRef identRef
;
64 ortn
= sslSimpleIdentPicker(kcRef
, &identRef
);
70 ortn
= SecIdentityCopyPrivateKey(identRef
, &keyRef
);
72 cssmPerror("SecIdentityCopyPrivateKey", ortn
);
77 * Get existing ACL list (may be empty)
79 SecAccessRef accessRef
;
80 CFArrayRef aclList
= NULL
;
81 ortn
= SecKeychainItemCopyAccess((SecKeychainItemRef
)keyRef
, &accessRef
);
83 cssmPerror("SecIdentityCopyPrivateKey", ortn
);
86 ortn
= SecAccessCopySelectedACLList(accessRef
, CSSM_ACL_AUTHORIZATION_DECRYPT
,
89 cssmPerror("SecAccessCopySelectedACLList", ortn
);
92 if((aclList
== NULL
) || (CFArrayGetCount(aclList
) == 0)) {
93 printf("No ACL list found. I don't know how to set the trusted app list.\n");
97 /* append our app list to each ACL's trusted app list */
98 for(int aclDex
=0; aclDex
<CFArrayGetCount(aclList
); aclDex
++) {
100 /* get existing app list */
101 SecACLRef aclRef
= (SecACLRef
)CFArrayGetValueAtIndex(aclList
, aclDex
);
102 CFArrayRef existApps
= NULL
;
103 CSSM_ACL_KEYCHAIN_PROMPT_SELECTOR promptSelector
;
104 CFStringRef promptDescription
;
106 ortn
= SecACLCopySimpleContents(aclRef
, &existApps
, &promptDescription
,
109 cssmPerror("SecACLCopySimpleContents", ortn
);
113 /* appends its contents to our list */
114 if(existApps
!= NULL
) {
115 for(int i
=0; i
<CFArrayGetCount(existApps
); i
++) {
116 CFArrayAppendValue(appList
, CFArrayGetValueAtIndex(existApps
, i
));
120 /* turn off possible keychain prompt flag */
121 promptSelector
.flags
&= ~CSSM_ACL_KEYCHAIN_PROMPT_REQUIRE_PASSPHRASE
;
124 ortn
= SecACLSetSimpleContents(aclRef
, appList
, promptDescription
,
127 cssmPerror("SecACLCopySimpleContents", ortn
);
130 if(existApps
!= NULL
) {
131 CFRelease(existApps
);
135 /* presumably we're been operating on "the" ACL list in "the" SecAccess,
136 * not a separate copy... */
137 ortn
= SecKeychainItemSetAccess((SecKeychainItemRef
)keyRef
, accessRef
);
139 cssmPerror("SecKeychainItemSetAccess", ortn
);
148 CFRelease(accessRef
);
151 printf("...success\n");