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@
24 #ifndef kc_file_helpers_h
25 #define kc_file_helpers_h
30 #include <Security/SecItem.h>
31 #include <Security/SecKeychain.h>
32 #include "keychain_regressions.h"
35 #pragma clang diagnostic push
36 #pragma clang diagnostic ignored "-Wunused-variable"
37 #pragma clang diagnostic ignored "-Wunused-function"
39 /* Deletes any keychain files that might exist at this location, and ignore any errors */
40 static void deleteKeychainFiles(const char* basename
) {
41 // remove the keychain if it exists, but ignore any errors
43 char * dbFilename
= NULL
;
44 asprintf(&dbFilename
, "%s-db", basename
);
49 static SecKeychainRef
createNewKeychainAt(const char * filename
, const char * password
) {
50 deleteKeychainFiles(filename
);
52 SecKeychainRef keychain
= NULL
;
53 ok_status(SecKeychainCreate(filename
, (UInt32
) strlen(password
), password
, FALSE
, NULL
, &keychain
), "SecKeychainCreate");
57 static SecKeychainRef
createNewKeychain(const char * name
, const char * password
) {
58 const char *home_dir
= getenv("HOME");
61 asprintf(&filename
, "%s/Library/Keychains/%s", home_dir
, name
);
62 SecKeychainRef keychain
= createNewKeychainAt(filename
, password
);
67 static void writeFile(const char* path
, uint8_t* buf
, size_t len
) {
68 FILE * fp
= fopen(path
, "w+");
69 fwrite(buf
, sizeof(uint8_t), len
, fp
);
74 SecKeychainRef CF_RETURNS_RETAINED
getPopulatedTestKeychain(void);
75 #define getPopulatedTestKeychainTests 2
77 SecKeychainRef CF_RETURNS_RETAINED
getEmptyTestKeychain(void);
78 #define getEmptyTestKeychainTests 1
80 // The following keychain includes:
82 // security add-internet-password -s test_service_restrictive_acl -a test_account -j "a useful comment" -r "htps" -t dflt -w test_password test.keychain
83 // security add-internet-password -s test_service -a test_account -j "a useful comment" -r "htps" -t dflt -w test_password -A test.keychain
84 // security add-generic-password -a test_account -s test_service -j "another useful comment" -w test_password -A test.keychain
85 // security add-generic-password -a test_account -s test_service_restrictive_acl -j "another useful comment" -w test_password test.keychain
87 // With certificate assistant, added a:
88 // Code Signing identity
91 extern const char * test_keychain_password
;
93 extern unsigned char test_keychain
[];
95 extern unsigned int test_keychain_len
;
99 #pragma clang diagnostic pop
101 #endif /* kc_file_helpers_h */