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
)
94 static CFDictionaryRef
CustomKeyCopyAttributeDictionary(SecKeyRef key
)
96 is(customKey
, key
, "CustomKeyCopyAttributeDictionary");
97 CFDictionaryRef dict
= CFDictionaryCreate(kCFAllocatorDefault
, NULL
, NULL
,
102 static CFStringRef
CustomKeyCopyDescribe(SecKeyRef key
)
104 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%s"), key
->key_class
->name
);
108 SecKeyDescriptor kCustomKeyDescriptor_version0
= {
120 CustomKeyCopyAttributeDictionary
,
121 CustomKeyCopyDescribe
,
128 SecKeyDescriptor kCustomKeyDescriptor_version1
= {
140 CustomKeyCopyAttributeDictionary
,
141 CustomKeyCopyDescribe
,
148 SecKeyDescriptor kCustomKeyDescriptor_version2
= {
160 CustomKeyCopyAttributeDictionary
,
161 CustomKeyCopyDescribe
,
168 SecKeyDescriptor kCustomKeyDescriptor_version3
= {
180 CustomKeyCopyAttributeDictionary
,
181 CustomKeyCopyDescribe
,
188 /* Test basic add delete update copy matching stuff. */
189 static void tests(SecKeyDescriptor
*descriptor
)
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");
199 SecPadding padding
= kSecPaddingPKCS1
;
200 const uint8_t *src
= (const uint8_t *)"defgh";
205 ok_status(SecKeyDecrypt(customKey
, padding
, src
, srcLen
, dst
, &dstLen
),
207 ok_status(SecKeyEncrypt(customKey
, padding
, src
, srcLen
, dst
, &dstLen
),
209 ok_status(SecKeyRawSign(customKey
, padding
, src
, srcLen
, dst
, &dstLen
),
211 ok_status(SecKeyRawVerify(customKey
, padding
, src
, srcLen
, dst
, dstLen
),
213 is(SecKeyGetSize(customKey
, kSecKeyKeySizeInBits
), (size_t)5*8, "SecKeyGetSize");
215 CFDictionaryRef attrDict
= NULL
;
216 ok(attrDict
= SecKeyCopyAttributeDictionary(customKey
),
217 "SecKeyCopyAttributeDictionary");
218 CFReleaseNull(attrDict
);
220 CFDataRef pubdata
= NULL
;
221 ok(SecKeyCopyPublicBytes(customKey
, &pubdata
) != 0, "SecKeyCopyPublicBytes");
222 CFReleaseNull(pubdata
);
225 wrapped
= _SecKeyCopyWrapKey(customKey
, kSecKeyWrapPublicKeyPGP
, pubdata
, NULL
, NULL
, NULL
);
226 ok(wrapped
== NULL
, "_SecKeyCopyWrapKey");
227 CFReleaseNull(wrapped
);
229 wrapped
= _SecKeyCopyUnwrapKey(customKey
, kSecKeyWrapPublicKeyPGP
, pubdata
, NULL
, NULL
, NULL
);
230 ok(wrapped
== NULL
, "_SecKeyCopyUnwrapKey");
231 CFReleaseNull(wrapped
);
233 //ok(SecKeyGeneratePair(customKey, ), "SecKeyGeneratePair");
234 ok(SecKeyGetTypeID() != 0, "SecKeyGetTypeID works");
237 CFRelease(customKey
);
242 int si_40_seckey_custom(int argc
, char *const *argv
)
246 tests(&kCustomKeyDescriptor_version0
);
247 tests(&kCustomKeyDescriptor_version1
);
248 tests(&kCustomKeyDescriptor_version2
);
249 tests(&kCustomKeyDescriptor_version3
);