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 "trusted_cert_dump.h"
27 #include "trusted_cert_utils.h"
31 #include <Security/Security.h>
32 #include <Security/cssmapple.h>
33 #include <Security/SecTrustSettings.h>
34 #include <Security/oidsalg.h>
35 #include <security_cdsa_utils/cuFileIo.h>
36 #include <CoreFoundation/CoreFoundation.h>
38 // SecCertificateInferLabel
39 #include <Security/SecCertificatePriv.h>
42 /* print cert's label (the one SecCertificate infers) */
43 static OSStatus
printCertLabel(
44 SecCertificateRef certRef
)
49 ortn
= SecCertificateInferLabel(certRef
, &label
);
51 cssmPerror("SecCertificateInferLabel", ortn
);
60 * Display a Trust Settings array as obtained from
61 * SecTrustSettingsCopyTrustSettings().
63 static int displayTrustSettings(
64 CFArrayRef trustSettings
)
66 /* must always be there though it may be empty */
67 if(trustSettings
== NULL
) {
68 fprintf(stderr
, "***displayTrustSettings: missing trust settings array");
71 if(CFGetTypeID(trustSettings
) != CFArrayGetTypeID()) {
72 fprintf(stderr
, "***displayTrustSettings: malformed trust settings array");
77 CFIndex numUseConstraints
= CFArrayGetCount(trustSettings
);
79 indent(); printf("Number of trust settings : %ld\n", (long)numUseConstraints
);
81 SecPolicyRef certPolicy
;
82 SecTrustedApplicationRef certApp
;
83 CFDictionaryRef ucDict
;
84 CFStringRef policyStr
;
88 /* grind thru the trust settings dictionaries */
89 for(ucDex
=0; ucDex
<numUseConstraints
; ucDex
++) {
90 indent(); printf("Trust Setting %ld:\n", (long)ucDex
);
93 ucDict
= (CFDictionaryRef
)CFArrayGetValueAtIndex(trustSettings
, ucDex
);
94 if(CFGetTypeID(ucDict
) != CFDictionaryGetTypeID()) {
95 fprintf(stderr
, "***displayTrustSettings: malformed usage constraints dictionary");
100 /* policy - optional */
101 certPolicy
= (SecPolicyRef
)CFDictionaryGetValue(ucDict
, kSecTrustSettingsPolicy
);
102 if(certPolicy
!= NULL
) {
103 if(CFGetTypeID(certPolicy
) != SecPolicyGetTypeID()) {
104 fprintf(stderr
, "***displayTrustSettings: malformed certPolicy");
109 ortn
= SecPolicyGetOID(certPolicy
, &policyOid
);
111 cssmPerror("SecPolicyGetOID", ortn
);
115 indent(); printf("Policy OID : %s\n",
116 oidToOidString(&policyOid
));
120 certApp
= (SecTrustedApplicationRef
)CFDictionaryGetValue(ucDict
,
121 kSecTrustSettingsApplication
);
122 if(certApp
!= NULL
) {
123 if(CFGetTypeID(certApp
) != SecTrustedApplicationGetTypeID()) {
124 fprintf(stderr
, "***displayTrustSettings: malformed certApp");
128 CFDataRef appPath
= NULL
;
129 ortn
= SecTrustedApplicationCopyData(certApp
, &appPath
);
131 cssmPerror("SecTrustedApplicationCopyData", ortn
);
135 indent(); printf("Application : %s", CFDataGetBytePtr(appPath
));
141 policyStr
= (CFStringRef
)CFDictionaryGetValue(ucDict
, kSecTrustSettingsPolicyString
);
142 if(policyStr
!= NULL
) {
143 if(CFGetTypeID(policyStr
) != CFStringGetTypeID()) {
144 fprintf(stderr
, "***displayTrustSettings: malformed policyStr");
148 indent(); printf("Policy String : ");
149 printCfStr(policyStr
); printf("\n");
153 cfNum
= (CFNumberRef
)CFDictionaryGetValue(ucDict
, kSecTrustSettingsAllowedError
);
155 if(CFGetTypeID(cfNum
) != CFNumberGetTypeID()) {
156 fprintf(stderr
, "***displayTrustSettings: malformed allowedError");
160 indent(); printf("Allowed Error : ");
161 printCssmErr(cfNum
); printf("\n");
165 cfNum
= (CFNumberRef
)CFDictionaryGetValue(ucDict
, kSecTrustSettingsResult
);
167 if(CFGetTypeID(cfNum
) != CFNumberGetTypeID()) {
168 fprintf(stderr
, "***displayTrustSettings: malformed ResultType");
172 indent(); printf("Result Type : ");
173 printResultType(cfNum
); printf("\n");
177 cfNum
= (CFNumberRef
)CFDictionaryGetValue(ucDict
, kSecTrustSettingsKeyUsage
);
179 if(CFGetTypeID(cfNum
) != CFNumberGetTypeID()) {
180 fprintf(stderr
, "***displayTrustSettings: malformed keyUsage");
184 indent(); printf("Key Usage : ");
185 printKeyUsage(cfNum
); printf("\n");
196 trusted_cert_dump(int argc
, char * const *argv
)
198 CFArrayRef certArray
= NULL
;
199 OSStatus ortn
= noErr
;
202 CFArrayRef trustSettings
;
204 SecTrustSettingsDomain domain
= kSecTrustSettingsDomainUser
;
211 while ((arg
= getopt(argc
, argv
, "sdh")) != -1) {
214 domain
= kSecTrustSettingsDomainSystem
;
217 domain
= kSecTrustSettingsDomainAdmin
;
221 return 2; /* @@@ Return 2 triggers usage message. */
226 return 2; /* @@@ Return 2 triggers usage message. */
229 ortn
= SecTrustSettingsCopyCertificates(domain
, &certArray
);
231 cssmPerror("SecTrustSettingsCopyCertificates", ortn
);
234 numCerts
= CFArrayGetCount(certArray
);
235 printf("Number of trusted certs = %ld\n", (long)numCerts
);
237 for(dex
=0; dex
<numCerts
; dex
++) {
238 SecCertificateRef certRef
=
239 (SecCertificateRef
)CFArrayGetValueAtIndex(certArray
, dex
);
240 if(CFGetTypeID(certRef
) != SecCertificateGetTypeID()) {
241 fprintf(stderr
, "***Bad CFGetTypeID for cert %ld\n", (long)dex
);
246 /* always print the cert's label */
247 printf("Cert %ld: ", dex
);
248 printCertLabel(certRef
);
251 /* see if the cert has any usage constraints (it should!) */
252 ortn
= SecTrustSettingsCopyTrustSettings(certRef
, domain
, &trustSettings
);
254 cssmPerror("SecTrustSettingsCopyTrustSettings", ortn
);
258 if(displayTrustSettings(trustSettings
)) {
262 CFRelease(certArray
);