2 * Copyright (c) 2003-2009,2012,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 "security_tool.h"
28 #include "trusted_cert_dump.h"
29 #include "trusted_cert_utils.h"
33 #include <Security/Security.h>
34 #include <Security/cssmapple.h>
35 #include <Security/SecTrustSettings.h>
36 #include <Security/oidsalg.h>
37 #include <security_cdsa_utils/cuFileIo.h>
38 #include <CoreFoundation/CoreFoundation.h>
41 * Display a Trust Settings array as obtained from
42 * SecTrustSettingsCopyTrustSettings().
44 static int displayTrustSettings(
45 CFArrayRef trustSettings
)
47 /* must always be there though it may be empty */
48 if(trustSettings
== NULL
) {
49 fprintf(stderr
, "***displayTrustSettings: missing trust settings array");
52 if(CFGetTypeID(trustSettings
) != CFArrayGetTypeID()) {
53 fprintf(stderr
, "***displayTrustSettings: malformed trust settings array");
58 CFIndex numUseConstraints
= CFArrayGetCount(trustSettings
);
60 indent(); printf("Number of trust settings : %ld\n", (long)numUseConstraints
);
62 SecPolicyRef certPolicy
;
63 SecTrustedApplicationRef certApp
;
64 CFDictionaryRef ucDict
;
65 CFStringRef policyStr
;
69 /* grind thru the trust settings dictionaries */
70 for(ucDex
=0; ucDex
<numUseConstraints
; ucDex
++) {
71 indent(); printf("Trust Setting %ld:\n", (long)ucDex
);
74 ucDict
= (CFDictionaryRef
)CFArrayGetValueAtIndex(trustSettings
, ucDex
);
75 if(CFGetTypeID(ucDict
) != CFDictionaryGetTypeID()) {
76 fprintf(stderr
, "***displayTrustSettings: malformed usage constraints dictionary");
81 /* policy - optional */
82 certPolicy
= (SecPolicyRef
)CFDictionaryGetValue(ucDict
, kSecTrustSettingsPolicy
);
83 if(certPolicy
!= NULL
) {
84 if(CFGetTypeID(certPolicy
) != SecPolicyGetTypeID()) {
85 fprintf(stderr
, "***displayTrustSettings: malformed certPolicy");
90 ortn
= SecPolicyGetOID(certPolicy
, &policyOid
);
92 cssmPerror("SecPolicyGetOID", ortn
);
96 indent(); printf("Policy OID : %s\n",
97 oidToOidString(&policyOid
));
101 certApp
= (SecTrustedApplicationRef
)CFDictionaryGetValue(ucDict
,
102 kSecTrustSettingsApplication
);
103 if(certApp
!= NULL
) {
104 if(CFGetTypeID(certApp
) != SecTrustedApplicationGetTypeID()) {
105 fprintf(stderr
, "***displayTrustSettings: malformed certApp");
109 CFDataRef appPath
= NULL
;
110 ortn
= SecTrustedApplicationCopyData(certApp
, &appPath
);
112 cssmPerror("SecTrustedApplicationCopyData", ortn
);
116 indent(); printf("Application : %s", CFDataGetBytePtr(appPath
));
122 policyStr
= (CFStringRef
)CFDictionaryGetValue(ucDict
, kSecTrustSettingsPolicyString
);
123 if(policyStr
!= NULL
) {
124 if(CFGetTypeID(policyStr
) != CFStringGetTypeID()) {
125 fprintf(stderr
, "***displayTrustSettings: malformed policyStr");
129 indent(); printf("Policy String : ");
130 printCfStr(policyStr
); printf("\n");
134 cfNum
= (CFNumberRef
)CFDictionaryGetValue(ucDict
, kSecTrustSettingsAllowedError
);
136 if(CFGetTypeID(cfNum
) != CFNumberGetTypeID()) {
137 fprintf(stderr
, "***displayTrustSettings: malformed allowedError");
141 indent(); printf("Allowed Error : ");
142 printCssmErr(cfNum
); printf("\n");
146 cfNum
= (CFNumberRef
)CFDictionaryGetValue(ucDict
, kSecTrustSettingsResult
);
148 if(CFGetTypeID(cfNum
) != CFNumberGetTypeID()) {
149 fprintf(stderr
, "***displayTrustSettings: malformed ResultType");
153 indent(); printf("Result Type : ");
154 printResultType(cfNum
); printf("\n");
158 cfNum
= (CFNumberRef
)CFDictionaryGetValue(ucDict
, kSecTrustSettingsKeyUsage
);
160 if(CFGetTypeID(cfNum
) != CFNumberGetTypeID()) {
161 fprintf(stderr
, "***displayTrustSettings: malformed keyUsage");
165 indent(); printf("Key Usage : ");
166 printKeyUsage(cfNum
); printf("\n");
177 trusted_cert_dump(int argc
, char * const *argv
)
179 CFArrayRef certArray
= NULL
;
180 OSStatus ortn
= noErr
;
183 CFArrayRef trustSettings
;
185 SecTrustSettingsDomain domain
= kSecTrustSettingsDomainUser
;
192 while ((arg
= getopt(argc
, argv
, "sdh")) != -1) {
195 domain
= kSecTrustSettingsDomainSystem
;
198 domain
= kSecTrustSettingsDomainAdmin
;
202 return SHOW_USAGE_MESSAGE
;
207 return SHOW_USAGE_MESSAGE
;
210 ortn
= SecTrustSettingsCopyCertificates(domain
, &certArray
);
212 cssmPerror("SecTrustSettingsCopyCertificates", ortn
);
215 numCerts
= CFArrayGetCount(certArray
);
216 printf("Number of trusted certs = %ld\n", (long)numCerts
);
218 for(dex
=0; dex
<numCerts
; dex
++) {
219 SecCertificateRef certRef
=
220 (SecCertificateRef
)CFArrayGetValueAtIndex(certArray
, dex
);
221 if(CFGetTypeID(certRef
) != SecCertificateGetTypeID()) {
222 fprintf(stderr
, "***Bad CFGetTypeID for cert %ld\n", (long)dex
);
227 /* always print the cert's label */
228 printf("Cert %ld: ", dex
);
229 printCertLabel(certRef
);
232 /* see if the cert has any usage constraints (it should!) */
233 ortn
= SecTrustSettingsCopyTrustSettings(certRef
, domain
, &trustSettings
);
235 cssmPerror("SecTrustSettingsCopyTrustSettings", ortn
);
239 if(displayTrustSettings(trustSettings
)) {
243 CFRelease(certArray
);