]> git.saurik.com Git - apple/security.git/blob - keychain/securityd/Regressions/secd-36-ks-encrypt.m
Security-59306.41.2.tar.gz
[apple/security.git] / keychain / securityd / Regressions / secd-36-ks-encrypt.m
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
25 #include "secd_regressions.h"
26
27 #include <Security/Security.h>
28
29 #include <utilities/SecCFWrappers.h>
30 #include "SecDbKeychainItem.h"
31
32 #include <TargetConditionals.h>
33
34 #if USE_KEYSTORE
35 #include "OSX/utilities/SecAKSWrappers.h"
36
37 #include "SecdTestKeychainUtilities.h"
38
39 int secd_36_ks_encrypt(int argc, char *const *argv)
40 {
41 plan_tests(9);
42
43 secd_test_setup_temp_keychain("secd_36_ks_encrypt", NULL);
44
45 keybag_handle_t keybag;
46 keybag_state_t state;
47 CFDictionaryRef data = NULL;
48 CFDataRef enc = NULL;
49 CFErrorRef error = NULL;
50 SecAccessControlRef ac = NULL;
51 bool ret;
52
53 char passcode[] = "password";
54 int passcode_len = sizeof(passcode) - 1;
55
56
57 /* Create and lock custom keybag */
58 is(kAKSReturnSuccess, aks_create_bag(passcode, passcode_len, kAppleKeyStoreDeviceBag, &keybag), "create keybag");
59 is(kAKSReturnSuccess, aks_get_lock_state(keybag, &state), "get keybag state");
60 is(0, (int)(state&keybag_state_locked), "keybag unlocked");
61
62 data = (__bridge CFDictionaryRef)@{
63 (id)kSecValueData : @"secret here",
64 };
65
66 ok(ac = SecAccessControlCreate(NULL, &error), "SecAccessControlCreate: %@", error);
67 ok(SecAccessControlSetProtection(ac, kSecAttrAccessibleWhenUnlocked, &error), "SecAccessControlSetProtection: %@", error);
68
69 ret = ks_encrypt_data(keybag, ac, NULL, data, (__bridge CFDictionaryRef)@{@"persistref" : @"aaa-bbb-ccc"}, NULL, &enc, true, &error);
70 is(true, ret);
71
72 CFReleaseNull(ac);
73
74 {
75 CFMutableDictionaryRef attributes = NULL;
76 uint32_t version = 0;
77
78 ret = ks_decrypt_data(keybag, kAKSKeyOpDecrypt, &ac, NULL, enc, NULL, NULL, &attributes, &version, true, NULL, &error);
79 is(true, ret, "ks_decrypt_data: %@", error);
80
81 CFTypeRef aclProtection = ac ? SecAccessControlGetProtection(ac) : NULL;
82 ok(aclProtection && CFEqual(aclProtection, kSecAttrAccessibleWhenUnlocked), "AccessControl protection is: %@", aclProtection);
83
84 CFReleaseNull(ac);
85 }
86
87 CFReleaseNull(error);
88 CFReleaseNull(enc);
89
90 return 0;
91 }
92
93 #else /* !USE_KEYSTORE */
94
95 int secd_36_ks_encrypt(int argc, char *const *argv)
96 {
97 plan_tests(1);
98 ok(true);
99 return 0;
100 }
101 #endif /* USE_KEYSTORE */