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