2 * Copyright (c) 2007-2008,2010,2012-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <CoreFoundation/CoreFoundation.h>
26 #include <Security/SecKeyPriv.h>
28 #include "Security_regressions.h"
30 #define CFReleaseNull(CF) { CFTypeRef _cf = (CF); if (_cf) { (CF) = NULL; CFRelease(_cf); } }
32 static SecKeyRef customKey
;
33 static SecKeyRef initedCustomKey
;
35 static OSStatus
CustomKeyInit(SecKeyRef key
, const uint8_t *key_data
,
36 CFIndex key_len
, SecKeyEncoding encoding
)
38 ok(key
, "CustomKeyInit");
39 ok(key
&& key
->key
== NULL
, "key->key is NULL");
40 initedCustomKey
= key
;
44 static void CustomKeyDestroy(SecKeyRef key
)
46 is(customKey
, key
, "CustomKeyDestroy");
49 static OSStatus
CustomKeyRawSign(SecKeyRef key
, SecPadding padding
,
50 const uint8_t *dataToSign
, size_t dataToSignLen
,
51 uint8_t *sig
, size_t *sigLen
)
53 is(customKey
, key
, "CustomKeyRawSign");
57 static OSStatus
CustomKeyRawVerify(
58 SecKeyRef key
, SecPadding padding
, const uint8_t *signedData
,
59 size_t signedDataLen
, const uint8_t *sig
, size_t sigLen
)
61 is(customKey
, key
, "CustomKeyRawVerify");
65 static OSStatus
CustomKeyEncrypt(SecKeyRef key
, SecPadding padding
,
66 const uint8_t *plainText
, size_t plainTextLen
,
67 uint8_t *cipherText
, size_t *cipherTextLen
)
69 is(customKey
, key
, "CustomKeyEncrypt");
73 static OSStatus
CustomKeyDecrypt(SecKeyRef key
, SecPadding padding
,
74 const uint8_t *cipherText
, size_t cipherTextLen
,
75 uint8_t *plainText
, size_t *plainTextLen
)
77 is(customKey
, key
, "CustomKeyDecrypt");
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
)
85 is(customKey
, key
, "CustomKeyCompute");
89 static size_t CustomKeyBlockSize(SecKeyRef key
)
91 is(customKey
, key
, "CustomKeyBlockSize");
95 static CFDictionaryRef
CustomKeyCopyAttributeDictionary(SecKeyRef key
)
97 is(customKey
, key
, "CustomKeyCopyAttributeDictionary");
98 CFDictionaryRef dict
= CFDictionaryCreate(kCFAllocatorDefault
, NULL
, NULL
,
103 static CFStringRef
CustomKeyCopyDescribe(SecKeyRef key
)
105 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), key
->key_class
->name
);
109 SecKeyDescriptor kCustomKeyDescriptor_version0
= {
121 CustomKeyCopyAttributeDictionary
,
122 CustomKeyCopyDescribe
,
129 SecKeyDescriptor kCustomKeyDescriptor_version1
= {
141 CustomKeyCopyAttributeDictionary
,
142 CustomKeyCopyDescribe
,
149 SecKeyDescriptor kCustomKeyDescriptor_version2
= {
161 CustomKeyCopyAttributeDictionary
,
162 CustomKeyCopyDescribe
,
169 SecKeyDescriptor kCustomKeyDescriptor_version3
= {
181 CustomKeyCopyAttributeDictionary
,
182 CustomKeyCopyDescribe
,
189 /* Test basic add delete update copy matching stuff. */
190 static void tests(SecKeyDescriptor
*descriptor
)
192 const uint8_t *keyData
= (const uint8_t *)"abc";
193 CFIndex keyDataLength
= 3;
194 SecKeyEncoding encoding
= kSecKeyEncodingRaw
;
195 ok(customKey
= SecKeyCreate(kCFAllocatorDefault
,
196 descriptor
, keyData
, keyDataLength
, encoding
),
197 "create custom key");
198 is(customKey
, initedCustomKey
, "CustomKeyInit got the right key");
200 SecPadding padding
= kSecPaddingPKCS1
;
201 const uint8_t *src
= NULL
;
206 ok_status(SecKeyDecrypt(customKey
, padding
, src
, srcLen
, dst
, &dstLen
),
208 ok_status(SecKeyEncrypt(customKey
, padding
, src
, srcLen
, dst
, &dstLen
),
210 ok_status(SecKeyRawSign(customKey
, padding
, src
, srcLen
, dst
, &dstLen
),
212 ok_status(SecKeyRawVerify(customKey
, padding
, src
, srcLen
, dst
, dstLen
),
214 is(SecKeyGetSize(customKey
, kSecKeyKeySizeInBits
), (size_t)42*8, "SecKeyGetSize");
216 CFDictionaryRef attrDict
= NULL
;
217 ok(attrDict
= SecKeyCopyAttributeDictionary(customKey
),
218 "SecKeyCopyAttributeDictionary");
219 CFReleaseNull(attrDict
);
221 CFDataRef pubdata
= NULL
;
222 ok(SecKeyCopyPublicBytes(customKey
, &pubdata
) != 0, "SecKeyCopyPublicBytes");
223 CFReleaseNull(pubdata
);
226 wrapped
= _SecKeyCopyWrapKey(customKey
, kSecKeyWrapPublicKeyPGP
, pubdata
, NULL
, NULL
, NULL
);
227 ok(wrapped
== NULL
, "_SecKeyCopyWrapKey");
228 CFReleaseNull(wrapped
);
230 wrapped
= _SecKeyCopyUnwrapKey(customKey
, kSecKeyWrapPublicKeyPGP
, pubdata
, NULL
, NULL
, NULL
);
231 ok(wrapped
== NULL
, "_SecKeyCopyUnwrapKey");
232 CFReleaseNull(wrapped
);
234 //ok(SecKeyGeneratePair(customKey, ), "SecKeyGeneratePair");
235 ok(SecKeyGetTypeID() != 0, "SecKeyGetTypeID works");
238 CFRelease(customKey
);
243 int si_40_seckey_custom(int argc
, char *const *argv
)
247 tests(&kCustomKeyDescriptor_version0
);
248 tests(&kCustomKeyDescriptor_version1
);
249 tests(&kCustomKeyDescriptor_version2
);
250 tests(&kCustomKeyDescriptor_version3
);