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