]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/secitem/si-40-seckey-custom.c
Security-58286.51.6.tar.gz
[apple/security.git] / OSX / sec / Security / Regressions / secitem / si-40-seckey-custom.c
1 /*
2 * Copyright (c) 2007-2008,2010,2012-2014 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 <CoreFoundation/CoreFoundation.h>
26 #include <Security/SecKeyPriv.h>
27
28 #include "Security_regressions.h"
29
30 #define CFReleaseNull(CF) { CFTypeRef _cf = (CF); if (_cf) { (CF) = NULL; CFRelease(_cf); } }
31
32 static SecKeyRef customKey;
33 static SecKeyRef initedCustomKey;
34
35 static OSStatus CustomKeyInit(SecKeyRef key, const uint8_t *key_data,
36 CFIndex key_len, SecKeyEncoding encoding)
37 {
38 ok(key, "CustomKeyInit");
39 ok(key && key->key == NULL, "key->key is NULL");
40 initedCustomKey = key;
41 return errSecSuccess;
42 }
43
44 static void CustomKeyDestroy(SecKeyRef key)
45 {
46 is(customKey, key, "CustomKeyDestroy");
47 }
48
49 static OSStatus CustomKeyRawSign(SecKeyRef key, SecPadding padding,
50 const uint8_t *dataToSign, size_t dataToSignLen,
51 uint8_t *sig, size_t *sigLen)
52 {
53 is(customKey, key, "CustomKeyRawSign");
54 return errSecSuccess;
55 }
56
57 static OSStatus CustomKeyRawVerify(
58 SecKeyRef key, SecPadding padding, const uint8_t *signedData,
59 size_t signedDataLen, const uint8_t *sig, size_t sigLen)
60 {
61 is(customKey, key, "CustomKeyRawVerify");
62 return errSecSuccess;
63 }
64
65 static OSStatus CustomKeyEncrypt(SecKeyRef key, SecPadding padding,
66 const uint8_t *plainText, size_t plainTextLen,
67 uint8_t *cipherText, size_t *cipherTextLen)
68 {
69 is(customKey, key, "CustomKeyEncrypt");
70 return errSecSuccess;
71 }
72
73 static OSStatus CustomKeyDecrypt(SecKeyRef key, SecPadding padding,
74 const uint8_t *cipherText, size_t cipherTextLen,
75 uint8_t *plainText, size_t *plainTextLen)
76 {
77 is(customKey, key, "CustomKeyDecrypt");
78 return errSecSuccess;
79 }
80
81 static OSStatus CustomKeyCompute(SecKeyRef key,
82 const uint8_t *pub_key, size_t pub_key_len,
83 uint8_t *computed_key, size_t *computed_key_len)
84 {
85 is(customKey, key, "CustomKeyCompute");
86 return errSecSuccess;
87 }
88
89 static size_t CustomKeyBlockSize(SecKeyRef key)
90 {
91 return 5;
92 }
93
94 static CFDictionaryRef CustomKeyCopyAttributeDictionary(SecKeyRef key)
95 {
96 is(customKey, key, "CustomKeyCopyAttributeDictionary");
97 CFDictionaryRef dict = CFDictionaryCreate(kCFAllocatorDefault, NULL, NULL,
98 0, NULL, NULL);
99 return dict;
100 }
101
102 static CFStringRef CustomKeyCopyDescribe(SecKeyRef key)
103 {
104 return CFStringCreateWithFormat(NULL, NULL, CFSTR("%s"), key->key_class->name);
105 }
106
107
108 SecKeyDescriptor kCustomKeyDescriptor_version0 = {
109 0,
110 "CustomKeyVersion0",
111 0, /* extraBytes */
112 CustomKeyInit,
113 CustomKeyDestroy,
114 CustomKeyRawSign,
115 CustomKeyRawVerify,
116 CustomKeyEncrypt,
117 CustomKeyDecrypt,
118 CustomKeyCompute,
119 CustomKeyBlockSize,
120 CustomKeyCopyAttributeDictionary,
121 CustomKeyCopyDescribe,
122 (void *)abort,
123 (void *)abort,
124 (void *)abort,
125 (void *)abort,
126 };
127
128 SecKeyDescriptor kCustomKeyDescriptor_version1 = {
129 1,
130 "CustomKeyVersion1",
131 0, /* extraBytes */
132 CustomKeyInit,
133 CustomKeyDestroy,
134 CustomKeyRawSign,
135 CustomKeyRawVerify,
136 CustomKeyEncrypt,
137 CustomKeyDecrypt,
138 CustomKeyCompute,
139 CustomKeyBlockSize,
140 CustomKeyCopyAttributeDictionary,
141 CustomKeyCopyDescribe,
142 NULL,
143 (void *)abort,
144 (void *)abort,
145 (void *)abort,
146 };
147
148 SecKeyDescriptor kCustomKeyDescriptor_version2 = {
149 2,
150 "CustomKeyVersion2",
151 0, /* extraBytes */
152 CustomKeyInit,
153 CustomKeyDestroy,
154 CustomKeyRawSign,
155 CustomKeyRawVerify,
156 CustomKeyEncrypt,
157 CustomKeyDecrypt,
158 CustomKeyCompute,
159 CustomKeyBlockSize,
160 CustomKeyCopyAttributeDictionary,
161 CustomKeyCopyDescribe,
162 NULL,
163 NULL,
164 (void *)abort,
165 (void *)abort,
166 };
167
168 SecKeyDescriptor kCustomKeyDescriptor_version3 = {
169 3,
170 "CustomKeyVersion3",
171 0, /* extraBytes */
172 CustomKeyInit,
173 CustomKeyDestroy,
174 CustomKeyRawSign,
175 CustomKeyRawVerify,
176 CustomKeyEncrypt,
177 CustomKeyDecrypt,
178 CustomKeyCompute,
179 CustomKeyBlockSize,
180 CustomKeyCopyAttributeDictionary,
181 CustomKeyCopyDescribe,
182 NULL,
183 NULL,
184 NULL,
185 NULL,
186 };
187
188 /* Test basic add delete update copy matching stuff. */
189 static void tests(SecKeyDescriptor *descriptor)
190 {
191 const uint8_t *keyData = (const uint8_t *)"abc";
192 CFIndex keyDataLength = 3;
193 SecKeyEncoding encoding = kSecKeyEncodingRaw;
194 ok(customKey = SecKeyCreate(kCFAllocatorDefault,
195 descriptor, keyData, keyDataLength, encoding),
196 "create custom key");
197 is(customKey, initedCustomKey, "CustomKeyInit got the right key");
198
199 SecPadding padding = kSecPaddingPKCS1;
200 const uint8_t *src = (const uint8_t *)"defgh";
201 size_t srcLen = 5;
202 uint8_t dst[5];
203 size_t dstLen = 5;
204
205 ok_status(SecKeyDecrypt(customKey, padding, src, srcLen, dst, &dstLen),
206 "SecKeyDecrypt");
207 ok_status(SecKeyEncrypt(customKey, padding, src, srcLen, dst, &dstLen),
208 "SecKeyEncrypt");
209 ok_status(SecKeyRawSign(customKey, padding, src, srcLen, dst, &dstLen),
210 "SecKeyRawSign");
211 ok_status(SecKeyRawVerify(customKey, padding, src, srcLen, dst, dstLen),
212 "SecKeyRawVerify");
213 is(SecKeyGetSize(customKey, kSecKeyKeySizeInBits), (size_t)5*8, "SecKeyGetSize");
214
215 CFDictionaryRef attrDict = NULL;
216 ok(attrDict = SecKeyCopyAttributeDictionary(customKey),
217 "SecKeyCopyAttributeDictionary");
218 CFReleaseNull(attrDict);
219
220 CFDataRef pubdata = NULL;
221 ok(SecKeyCopyPublicBytes(customKey, &pubdata) != 0, "SecKeyCopyPublicBytes");
222 CFReleaseNull(pubdata);
223
224 CFDataRef wrapped;
225 wrapped = _SecKeyCopyWrapKey(customKey, kSecKeyWrapPublicKeyPGP, pubdata, NULL, NULL, NULL);
226 ok(wrapped == NULL, "_SecKeyCopyWrapKey");
227 CFReleaseNull(wrapped);
228
229 wrapped = _SecKeyCopyUnwrapKey(customKey, kSecKeyWrapPublicKeyPGP, pubdata, NULL, NULL, NULL);
230 ok(wrapped == NULL, "_SecKeyCopyUnwrapKey");
231 CFReleaseNull(wrapped);
232
233 //ok(SecKeyGeneratePair(customKey, ), "SecKeyGeneratePair");
234 ok(SecKeyGetTypeID() != 0, "SecKeyGetTypeID works");
235
236 if (customKey) {
237 CFRelease(customKey);
238 customKey = NULL;
239 }
240 }
241
242 int si_40_seckey_custom(int argc, char *const *argv)
243 {
244 plan_tests(20 * 4);
245
246 tests(&kCustomKeyDescriptor_version0);
247 tests(&kCustomKeyDescriptor_version1);
248 tests(&kCustomKeyDescriptor_version2);
249 tests(&kCustomKeyDescriptor_version3);
250
251 return 0;
252 }