2 * Copyright (c) 2003-2007,2009-2010 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 <TargetConditionals.h>
27 #if TARGET_OS_EMBEDDED
29 #include "Securitycommands.h"
36 #include <CoreFoundation/CFNumber.h>
37 #include <CoreFoundation/CFString.h>
38 #include <Security/SecCertificatePriv.h>
39 #include <Security/SecTrustStore.h>
41 #include <SecurityTool/readline.h>
42 #include <SecurityTool/tool_errors.h>
45 do_add_certificates(const char *keychainName
, bool trustSettings
,
46 int argc
, char * const *argv
)
51 CFMutableDictionaryRef attributes
=
52 CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
53 CFDictionarySetValue(attributes
, kSecClass
, kSecClassCertificate
);
55 for (ix
= 0; ix
< argc
; ++ix
) {
56 CFDataRef data
= copyFileContents(argv
[ix
]);
58 SecCertificateRef cert
= SecCertificateCreateWithData(
59 kCFAllocatorDefault
, data
);
61 cert
= SecCertificateCreateWithPEM(kCFAllocatorDefault
, data
);
66 SecTrustStoreSetTrustSettings(
67 SecTrustStoreForDomain(kSecTrustStoreDomainUser
),
70 CFDictionarySetValue(attributes
, kSecValueRef
, cert
);
71 status
= SecItemAdd(attributes
, NULL
);
74 fprintf(stderr
, "file %s: SecItemAdd %s",
75 argv
[ix
], sec_errstr(status
));
81 fprintf(stderr
, "file %s: does not contain a valid certificate",
94 keychain_add_certificates(int argc
, char * const *argv
)
97 const char *keychainName
= NULL
;
98 bool trustSettings
= false;
99 while ((ch
= getopt(argc
, argv
, "hk:t")) != -1)
104 keychainName
= optarg
;
105 if (*keychainName
== '\0')
109 trustSettings
= true;
113 return 2; /* Return 2 triggers usage message. */
123 result
= do_add_certificates(keychainName
, trustSettings
, argc
, argv
);
128 #endif // TARGET_OS_EMBEDDED