/ OSX / libsecurity_keychain / regressions / kc-keychain-file-helpers.h
kc-keychain-file-helpers.h
  1  /*
  2   * Copyright (c) 2016 Apple Inc. All Rights Reserved.
  3   *
  4   * @APPLE_LICENSE_HEADER_START@
  5   *
  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
 11   * file.
 12   *
 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.
 20   *
 21   * @APPLE_LICENSE_HEADER_END@
 22   */
 23  
 24  #ifndef kc_file_helpers_h
 25  #define kc_file_helpers_h
 26  
 27  #include <stdio.h>
 28  #include <stdlib.h>
 29  #include <unistd.h>
 30  #include <Security/SecItem.h>
 31  #include <Security/SecKeychain.h>
 32  #include "keychain_regressions.h"
 33  
 34  
 35  #pragma clang diagnostic push
 36  #pragma clang diagnostic ignored "-Wunused-variable"
 37  #pragma clang diagnostic ignored "-Wunused-function"
 38  
 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
 42      unlink(basename);
 43      char * dbFilename = NULL;
 44      asprintf(&dbFilename, "%s-db", basename);
 45      unlink(dbFilename);
 46      free(dbFilename);
 47  }
 48  
 49  static SecKeychainRef createNewKeychainAt(const char * filename, const char * password) {
 50      deleteKeychainFiles(filename);
 51  
 52      SecKeychainRef keychain = NULL;
 53      ok_status(SecKeychainCreate(filename, (UInt32) strlen(password), password, FALSE, NULL, &keychain), "SecKeychainCreate");
 54      return keychain;
 55  }
 56  
 57  static SecKeychainRef createNewKeychain(const char * name, const char * password) {
 58      const char *home_dir = getenv("HOME");
 59      char * filename;
 60  
 61      asprintf(&filename, "%s/Library/Keychains/%s", home_dir, name);
 62      SecKeychainRef keychain = createNewKeychainAt(filename, password);
 63      free(filename);
 64      return keychain;
 65  }
 66  
 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);
 70      fclose(fp);
 71      sync();
 72  }
 73  
 74  SecKeychainRef CF_RETURNS_RETAINED getPopulatedTestKeychain(void);
 75  #define getPopulatedTestKeychainTests 2
 76  
 77  SecKeychainRef CF_RETURNS_RETAINED getEmptyTestKeychain(void);
 78  #define getEmptyTestKeychainTests 1
 79  
 80  // The following keychain includes:
 81  //
 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
 86  
 87  // With certificate assistant, added a:
 88  //   Code Signing identity
 89  //   S/MIME identity
 90  
 91  extern const char * test_keychain_password;
 92  
 93  extern unsigned char test_keychain[];
 94  
 95  extern unsigned int test_keychain_len;
 96  
 97  
 98  
 99  #pragma clang diagnostic pop
100  
101  #endif /* kc_file_helpers_h */