2 * Copyright (c) 2010-2015 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 * SecECKey.m - CoreFoundation based ECDSA key object
29 #include "SecECKeyPriv.h"
31 #import <Foundation/Foundation.h>
33 #include <Security/SecKeyInternal.h>
34 #include <Security/SecItem.h>
35 #include <Security/SecBasePriv.h>
36 #include <AssertMacros.h>
37 #include <Security/SecureTransport.h> /* For error codes. */
38 #include <CoreFoundation/CFData.h> /* For error codes. */
39 #include <CoreFoundation/CFNumber.h>
40 #include <Security/SecFramework.h>
41 #include <Security/SecRandom.h>
42 #include <utilities/debugging.h>
43 #include <Security/SecItemPriv.h>
44 #include <Security/SecInternal.h>
45 #include <utilities/SecCFError.h>
46 #include <utilities/SecCFWrappers.h>
47 #include <utilities/array_size.h>
48 #include <corecrypto/ccec.h>
49 #include <corecrypto/ccsha1.h>
50 #include <corecrypto/ccsha2.h>
51 #include <corecrypto/ccrng.h>
52 #include <corecrypto/ccder_decode_eckey.h>
54 #define kMaximumECKeySize 521
56 static CFIndex SecECKeyGetAlgorithmID(SecKeyRef key) {
57 return kSecECDSAAlgorithmID;
67 /* Public key static functions. */
68 static void SecECPublicKeyDestroy(SecKeyRef key) {
69 /* Zero out the public key */
70 ccec_pub_ctx_t pubkey = key->key;
71 if (ccec_ctx_cp(pubkey))
72 cc_clear(ccec_pub_ctx_size(ccn_sizeof_n(ccec_ctx_n(pubkey))), pubkey);
75 static ccec_const_cp_t getCPForPublicSize(CFIndex encoded_length)
77 size_t keysize = ccec_x963_import_pub_size(encoded_length);
78 if(ccec_keysize_is_supported(keysize)) {
79 return ccec_get_cp(keysize);
84 static ccec_const_cp_t getCPForPrivateSize(CFIndex encoded_length)
86 size_t keysize = ccec_x963_import_priv_size(encoded_length);
87 if(ccec_keysize_is_supported(keysize)) {
88 return ccec_get_cp(keysize);
93 static ccoid_t ccoid_secp192r1 = CC_EC_OID_SECP192R1;
94 static ccoid_t ccoid_secp256r1 = CC_EC_OID_SECP256R1;
95 static ccoid_t ccoid_secp224r1 = CC_EC_OID_SECP224R1;
96 static ccoid_t ccoid_secp384r1 = CC_EC_OID_SECP384R1;
97 static ccoid_t ccoid_secp521r1 = CC_EC_OID_SECP521R1;
99 // <rdar://problem/66864716> OID_CERTICOM is wrong
100 static ccoid_t ccoid_libder_secp384r1 = ((unsigned char *)"\x06\x04\x2B\x84\x00\x22");
101 static ccoid_t ccoid_libder_secp521r1 = ((unsigned char *)"\x06\x04\x2B\x84\x00\x23");
103 static ccec_const_cp_t ccec_cp_for_oid(const unsigned char *oid)
106 if (ccoid_equal(oid, ccoid_secp192r1)) {
107 return ccec_cp_192();
108 } else if (ccoid_equal(oid, ccoid_secp256r1)) {
109 return ccec_cp_256();
110 } else if (ccoid_equal(oid, ccoid_secp224r1)) {
111 return ccec_cp_224();
112 } else if (ccoid_equal(oid, ccoid_secp384r1) || ccoid_equal(oid, ccoid_libder_secp384r1)) {
113 return ccec_cp_384();
114 } else if (ccoid_equal(oid, ccoid_secp521r1) || ccoid_equal(oid, ccoid_libder_secp521r1)) {
115 return ccec_cp_521();
118 return (ccec_const_cp_t){NULL};
121 static OSStatus SecECPublicKeyInit(SecKeyRef key,
122 const uint8_t *keyData, CFIndex keyDataLength, SecKeyEncoding encoding) {
123 ccec_pub_ctx_t pubkey = key->key;
124 OSStatus err = errSecParam;
127 case kSecDERKeyEncoding:
129 const SecDERKey *derKey = (const SecDERKey *)keyData;
130 if (keyDataLength != sizeof(SecDERKey)) {
135 require_action_quiet(derKey->parameters && derKey->parametersLength > 2 &&
136 derKey->parameters[1] <= derKey->parametersLength - 2, errOut, err = errSecDecode);
137 ccec_const_cp_t cp = ccec_cp_for_oid(derKey->parameters);
138 require_action_quiet(cp, errOut, err = errSecDecode);
140 err = (ccec_import_pub(cp, derKey->keyLength, derKey->key, pubkey)
141 ? errSecDecode : errSecSuccess);
144 case kSecKeyEncodingBytes:
146 ccec_const_cp_t cp = getCPForPublicSize(keyDataLength);
147 require_action_quiet(cp, errOut, err = errSecDecode);
148 err = (ccec_import_pub(cp, keyDataLength, keyData, pubkey)
149 ? errSecDecode : errSecSuccess);
152 case kSecExtractPublicFromPrivate:
154 ccec_full_ctx_t fullKey = (ccec_full_ctx_t)keyData;
156 cc_size fullKeyN = ccec_ctx_n(fullKey);
157 require_quiet(fullKeyN <= ccn_nof(kMaximumECKeySize), errOut);
158 memcpy(pubkey, fullKey, ccec_pub_ctx_size(ccn_sizeof_n(fullKeyN)));
162 case kSecKeyEncodingApplePkcs1:
172 static CFTypeRef SecECPublicKeyCopyOperationResult(SecKeyRef key, SecKeyOperationType operation, SecKeyAlgorithm algorithm,
173 CFArrayRef algorithms, SecKeyOperationMode mode,
174 CFTypeRef in1, CFTypeRef in2, CFErrorRef *error) {
175 if (operation != kSecKeyOperationTypeVerify || !CFEqual(algorithm, kSecKeyAlgorithmECDSASignatureDigestX962)) {
176 // EC public key supports only signature verification with X962 algorithm.
180 if (mode == kSecKeyOperationModePerform) {
183 size_t sigLen = CFDataGetLength(in2);
184 uint8_t *sig = (uint8_t *)CFDataGetBytePtr(in2);
185 ccec_pub_ctx_t pubkey = key->key;
187 err = ccec_verify(pubkey, CFDataGetLength(in1), CFDataGetBytePtr(in1), sigLen, sig, &valid);
189 SecError(errSecVerifyFailed, error, CFSTR("EC signature verification failed (ccerr %d)"), err);
192 SecError(errSecVerifyFailed, error, CFSTR("EC signature verification failed, no match"));
195 return kCFBooleanTrue;
198 // Algorithm is supported.
199 return kCFBooleanTrue;
203 static size_t SecECPublicKeyBlockSize(SecKeyRef key) {
204 /* Get key size in octets */
205 return ccec_ctx_size(ccec_ctx_pub(key->key));
208 /* Encode the public key and return it in a newly allocated CFDataRef. */
209 static CFDataRef SecECPublicKeyExport(CFAllocatorRef allocator,
210 ccec_pub_ctx_t pubkey) {
211 size_t pub_size = ccec_export_pub_size(pubkey);
212 CFMutableDataRef blob = CFDataCreateMutableWithScratch(allocator, pub_size);
213 ccec_export_pub(pubkey, CFDataGetMutableBytePtr(blob));
217 static CFDataRef SecECPublicKeyCopyExternalRepresentation(SecKeyRef key, CFErrorRef *error) {
218 ccec_pub_ctx_t pubkey = key->key;
219 return SecECPublicKeyExport(NULL, pubkey);
222 static OSStatus SecECPublicKeyCopyPublicOctets(SecKeyRef key, CFDataRef *serailziation)
224 ccec_pub_ctx_t pubkey = key->key;
226 CFAllocatorRef allocator = CFGetAllocator(key);
227 *serailziation = SecECPublicKeyExport(allocator, pubkey);
229 if (NULL == *serailziation)
232 return errSecSuccess;
235 static CFDictionaryRef SecECPublicKeyCopyAttributeDictionary(SecKeyRef key) {
236 CFDictionaryRef dict = SecKeyGeneratePublicAttributeDictionary(key, kSecAttrKeyTypeEC);
237 CFMutableDictionaryRef mutableDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
238 CFDictionarySetValue(mutableDict, kSecAttrCanDerive, kCFBooleanFalse);
239 CFAssignRetained(dict, mutableDict);
244 getCurveName(SecKeyRef key)
246 SecECNamedCurve curveType = SecECKeyGetNamedCurve(key);
250 case kSecECCurveSecp256r1:
251 return "kSecECCurveSecp256r1";
253 case kSecECCurveSecp384r1:
254 return "kSecECCurveSecp384r1";
256 case kSecECCurveSecp521r1:
257 return "kSecECCurveSecp521r1";
259 return "kSecECCurveNone";
263 static CFStringRef SecECPublicKeyCopyKeyDescription(SecKeyRef key)
265 NSMutableString *strings[2];
266 const char* curve = getCurveName(key);
268 ccec_pub_ctx_t ecPubkey = key->key;
269 size_t len = ccec_ctx_size(ecPubkey);
270 NSMutableData *buffer = [NSMutableData dataWithLength:len];
271 for (int i = 0; i < 2; ++i) {
272 ccn_write_uint(ccec_ctx_n(ecPubkey), (i == 0) ? ccec_ctx_x(ecPubkey) : ccec_ctx_y(ecPubkey), len, buffer.mutableBytes);
273 strings[i] = [NSMutableString stringWithCapacity:len * 2];
274 for (size_t byteIndex = 0; byteIndex < len; ++byteIndex) {
275 [strings[i] appendFormat:@"%02X", ((const uint8_t *)buffer.bytes)[byteIndex]];
279 NSString *description = [NSString stringWithFormat:@"<SecKeyRef curve type: %s, algorithm id: %lu, key type: %s, version: %d, block size: %zu bits, y: %@, x: %@, addr: %p>",
280 curve, (long)SecKeyGetAlgorithmId(key), key->key_class->name, key->key_class->version,
281 8 * SecKeyGetBlockSize(key), strings[1], strings[0], key];
282 return CFBridgingRetain(description);
285 static const struct ccec_rfc6637_curve * get_rfc6637_curve(SecKeyRef key)
287 SecECNamedCurve curveType = SecECKeyGetNamedCurve(key);
289 if (curveType == kSecECCurveSecp256r1) {
290 return &ccec_rfc6637_dh_curve_p256;
291 } else if (curveType == kSecECCurveSecp521r1) {
292 return &ccec_rfc6637_dh_curve_p521;
297 static CFDataRef SecECKeyCopyWrapKey(SecKeyRef key, SecKeyWrapType type, CFDataRef unwrappedKey, CFDictionaryRef parameters, CFDictionaryRef *outParam, CFErrorRef *error)
299 ccec_pub_ctx_t pubkey = key->key;
300 int err = errSecUnimplemented;
301 const struct ccec_rfc6637_curve *curve;
302 const struct ccec_rfc6637_wrap *wrap = NULL;
306 if (type != kSecKeyWrapPublicKeyPGP) {
307 SecError(errSecUnsupportedOperation, error, CFSTR("unsupported key wrapping algorithm"));
311 curve = get_rfc6637_curve(key);
313 SecError(errSecUnsupportedOperation, error, CFSTR("unsupported curve"));
317 CFNumberRef num = CFDictionaryGetValue(parameters, _kSecKeyWrapPGPSymAlg);
318 if (!isNumber(num) || !CFNumberGetValue(num, kCFNumberSInt8Type, &sym_alg)) {
319 SecError(errSecUnsupportedOperation, error, CFSTR("unknown symalg given"));
323 CFDataRef fingerprint = CFDictionaryGetValue(parameters, _kSecKeyWrapPGPFingerprint);
324 if (!isData(fingerprint) || CFDataGetLength(fingerprint) < kSecKeyWrapPGPFingerprintMinSize) {
325 SecError(errSecUnsupportedOperation, error, CFSTR("invalid fingerprint"));
329 CFTypeRef wrapAlg = CFDictionaryGetValue(parameters, _kSecKeyWrapPGPWrapAlg);
330 if (wrapAlg == NULL) {
331 SecError(errSecUnsupportedOperation, error, CFSTR("no wrap alg"));
333 } else if (CFEqual(wrapAlg, _kSecKeyWrapRFC6637WrapDigestSHA256KekAES128)) {
334 wrap = &ccec_rfc6637_wrap_sha256_kek_aes128;
335 } else if (CFEqual(wrapAlg, _kSecKeyWrapRFC6637WrapDigestSHA512KekAES256)) {
336 wrap = &ccec_rfc6637_wrap_sha512_kek_aes256;
338 SecError(errSecUnsupportedOperation, error, CFSTR("unknown wrap alg"));
342 num = CFDictionaryGetValue(parameters, _kSecKeyWrapRFC6637Flags);
344 if (!CFNumberGetValue(num, kCFNumberSInt32Type, &flags)) {
345 SecError(errSecUnsupportedOperation, error, CFSTR("invalid flags: %@"), num);
349 SecError(errSecUnsupportedOperation, error, CFSTR("unknown flags"));
353 CFIndex unwrappedKey_size = CFDataGetLength(unwrappedKey);
355 CFIndex output_size = ccec_rfc6637_wrap_key_size(pubkey, flags, unwrappedKey_size);
356 if (output_size == 0) {
357 SecError(errSecUnsupportedOperation, error, CFSTR("can't wrap that key, can't build size"));
361 CFMutableDataRef data = CFDataCreateMutableWithScratch(NULL, output_size);
362 require_quiet(data, errOut);
364 err = ccec_rfc6637_wrap_key(pubkey, CFDataGetMutableBytePtr(data), flags,
365 sym_alg, CFDataGetLength(unwrappedKey), CFDataGetBytePtr(unwrappedKey),
366 curve, wrap, CFDataGetBytePtr(fingerprint),
369 SecError(errSecUnsupportedOperation, error, CFSTR("Failed to wrap key"));
377 SecKeyDescriptor kSecECPublicKeyDescriptor = {
378 .version = kSecKeyDescriptorVersion,
379 .name = "ECPublicKey",
380 .extraBytes = ccec_pub_ctx_size(ccn_sizeof(kMaximumECKeySize)),
381 .init = SecECPublicKeyInit,
382 .destroy = SecECPublicKeyDestroy,
383 .blockSize = SecECPublicKeyBlockSize,
384 .copyDictionary = SecECPublicKeyCopyAttributeDictionary,
385 .copyExternalRepresentation = SecECPublicKeyCopyExternalRepresentation,
386 .describe = SecECPublicKeyCopyKeyDescription,
387 .getAlgorithmID = SecECKeyGetAlgorithmID,
388 .copyPublic = SecECPublicKeyCopyPublicOctets,
389 .copyWrapKey = SecECKeyCopyWrapKey,
390 .copyOperationResult = SecECPublicKeyCopyOperationResult,
393 /* Public Key API functions. */
394 SecKeyRef SecKeyCreateECPublicKey(CFAllocatorRef allocator,
395 const uint8_t *keyData, CFIndex keyDataLength,
396 SecKeyEncoding encoding) {
397 return SecKeyCreate(allocator, &kSecECPublicKeyDescriptor, keyData,
398 keyDataLength, encoding);
409 /* Private key static functions. */
410 static void SecECPrivateKeyDestroy(SecKeyRef key) {
411 /* Zero out the public key */
412 ccec_full_ctx_t fullkey = key->key;
414 if (ccec_ctx_cp(fullkey))
415 cc_clear(ccec_full_ctx_size(ccn_sizeof_n(ccec_ctx_n(fullkey))), fullkey);
419 static OSStatus SecECPrivateKeyInit(SecKeyRef key,
420 const uint8_t *keyData, CFIndex keyDataLength, SecKeyEncoding encoding) {
421 ccec_full_ctx_t fullkey = key->key;
422 OSStatus err = errSecParam;
425 case kSecKeyEncodingPkcs1:
427 /* TODO: DER import size (and thus cp), pub.x, pub.y and k. */
428 //err = ecc_import(keyData, keyDataLength, fullkey);
430 /* DER != PKCS#1, but we'll go along with it */
431 const unsigned char *oid;
435 require_noerr_quiet(ccec_der_import_priv_keytype(keyDataLength, keyData, (ccoid_t*)&oid, &n), abort);
436 cp = ccec_cp_for_oid(oid);
438 cp = ccec_curve_for_length_lookup(n * 8 /* bytes -> bits */,
439 ccec_cp_192(), ccec_cp_224(), ccec_cp_256(), ccec_cp_384(), ccec_cp_521(), NULL);
441 require_action_quiet(cp != NULL, abort, err = errSecDecode);
442 ccec_ctx_init(cp, fullkey);
444 require_noerr_quiet(ccec_der_import_priv(cp, keyDataLength, keyData, fullkey), abort);
448 case kSecKeyEncodingBytes:
450 ccec_const_cp_t cp = getCPForPrivateSize(keyDataLength);
451 require_quiet(cp != NULL, abort);
453 ccec_ctx_init(cp, fullkey);
454 size_t pubSize = ccec_export_pub_size(ccec_ctx_pub(fullkey));
456 require_quiet(pubSize < (size_t) keyDataLength, abort);
457 require_noerr_action_quiet(ccec_import_pub(cp, pubSize, keyData, ccec_ctx_pub(fullkey)),
463 keyDataLength -= pubSize;
465 cc_unit *k = ccec_ctx_k(fullkey);
466 require_noerr_action_quiet(ccn_read_uint(ccec_ctx_n(fullkey), k, keyDataLength, keyData),
474 case kSecGenerateKey:
476 CFDictionaryRef parameters = (CFDictionaryRef) keyData;
478 CFTypeRef ksize = CFDictionaryGetValue(parameters, kSecAttrKeySizeInBits);
479 CFIndex keyLengthInBits = getIntValue(ksize);
481 ccec_const_cp_t cp = ccec_get_cp(keyLengthInBits);
484 secwarning("Invalid or missing key size in: %@", parameters);
485 return errSecKeySizeNotAllowed;
488 if (!ccec_generate_key_fips(cp, ccrng_seckey, fullkey))
500 static CFTypeRef SecECPrivateKeyCopyOperationResult(SecKeyRef key, SecKeyOperationType operation, SecKeyAlgorithm algorithm,
501 CFArrayRef allAlgorithms, SecKeyOperationMode mode,
502 CFTypeRef in1, CFTypeRef in2, CFErrorRef *error) {
503 // Default answer is 'unsupported', unless we find out that we can support it.
504 CFTypeRef result = kCFNull;
506 ccec_full_ctx_t fullkey = key->key;
508 case kSecKeyOperationTypeSign: {
509 if (CFEqual(algorithm, kSecKeyAlgorithmECDSASignatureDigestX962)) {
510 if (mode == kSecKeyOperationModePerform) {
511 // Perform x962 mode of signature.
512 size_t size = ccec_sign_max_size(ccec_ctx_cp(fullkey));
513 result = CFDataCreateMutableWithScratch(NULL, size);
514 int err = ccec_sign(fullkey, CFDataGetLength(in1), CFDataGetBytePtr(in1),
515 &size, CFDataGetMutableBytePtr((CFMutableDataRef)result), ccrng_seckey);
516 require_action_quiet(err == 0, out, (CFReleaseNull(result),
517 SecError(errSecParam, error, CFSTR("%@: X962 signing failed (ccerr %d)"),
519 CFDataSetLength((CFMutableDataRef)result, size);
521 // Operation is supported.
522 result = kCFBooleanTrue;
527 case kSecKeyOperationTypeKeyExchange:
528 if (CFEqual(algorithm, kSecKeyAlgorithmECDHKeyExchangeStandard) ||
529 CFEqual(algorithm, kSecKeyAlgorithmECDHKeyExchangeCofactor)) {
530 if (mode == kSecKeyOperationModePerform) {
532 ccec_const_cp_t cp = getCPForPublicSize(CFDataGetLength(in1));
533 require_action_quiet(cp != NULL, out,
534 SecError(errSecParam, error, CFSTR("ECpriv sharedsecret: bad public key")));
535 ccec_pub_ctx_decl_cp(cp, pubkey);
536 err = ccec_import_pub(cp, CFDataGetLength(in1), CFDataGetBytePtr(in1), pubkey);
537 require_noerr_action_quiet(err, out, SecError(errSecParam, error,
538 CFSTR("ECpriv sharedsecret: bad public key (err %d)"), err));
539 size_t size = ccec_ccn_size(cp);
540 result = CFDataCreateMutableWithScratch(NULL, size);
541 err = ccecdh_compute_shared_secret(fullkey, pubkey, &size,
542 CFDataGetMutableBytePtr((CFMutableDataRef)result), ccrng_seckey);
543 require_noerr_action_quiet(err, out, (CFReleaseNull(result),
544 SecError(errSecDecode, error,
545 CFSTR("ECpriv failed to compute shared secret (err %d)"), err)));
546 CFDataSetLength((CFMutableDataRef)result, size);
548 // Operation is supported.
549 result = kCFBooleanTrue;
561 static size_t SecECPrivateKeyBlockSize(SecKeyRef key) {
562 ccec_full_ctx_t fullkey = key->key;
563 /* Get key size in octets */
564 return ccec_ctx_size(fullkey);
567 static OSStatus SecECPrivateKeyCopyPublicOctets(SecKeyRef key, CFDataRef *serailziation)
569 ccec_full_ctx_t fullkey = key->key;
571 CFAllocatorRef allocator = CFGetAllocator(key);
572 *serailziation = SecECPublicKeyExport(allocator, ccec_ctx_pub(fullkey));
574 if (NULL == *serailziation)
577 return errSecSuccess;
580 static CFDataRef SecECPrivateKeyCopyExternalRepresentation(SecKeyRef key, CFErrorRef *error) {
581 ccec_full_ctx_t fullkey = key->key;
582 size_t prime_size = ccec_cp_prime_size(ccec_ctx_cp(fullkey));
583 size_t key_size = ccec_export_pub_size(ccec_ctx_pub(fullkey)) + prime_size;
584 CFMutableDataRef blob = CFDataCreateMutableWithScratch(NULL, key_size);
585 ccec_export_pub(ccec_ctx_pub(fullkey), CFDataGetMutableBytePtr(blob));
586 UInt8 *dest = CFDataGetMutableBytePtr(blob) + ccec_export_pub_size(ccec_ctx_pub(fullkey));
587 const cc_unit *k = ccec_ctx_k(fullkey);
588 ccn_write_uint_padded(ccec_ctx_n(fullkey), k, prime_size, dest);
592 static CFDictionaryRef SecECPrivateKeyCopyAttributeDictionary(SecKeyRef key) {
593 /* Export the full ec key pair. */
594 CFDataRef fullKeyBlob = SecECPrivateKeyCopyExternalRepresentation(key, NULL);
596 CFDictionaryRef dict = SecKeyGeneratePrivateAttributeDictionary(key, kSecAttrKeyTypeEC, fullKeyBlob);
597 CFReleaseSafe(fullKeyBlob);
600 static CFStringRef SecECPrivateKeyCopyKeyDescription(SecKeyRef key) {
602 const char* curve = getCurveName(key);
604 return CFStringCreateWithFormat(kCFAllocatorDefault,NULL,CFSTR( "<SecKeyRef curve type: %s, algorithm id: %lu, key type: %s, version: %d, block size: %zu bits, addr: %p>"), curve, (long)SecKeyGetAlgorithmId(key), key->key_class->name, key->key_class->version, (8*SecKeyGetBlockSize(key)), key);
608 static CFDataRef SecECKeyCopyUnwrapKey(SecKeyRef key, SecKeyWrapType type, CFDataRef wrappedKey, CFDictionaryRef parameters, CFDictionaryRef *outParam, CFErrorRef *error)
610 const struct ccec_rfc6637_curve *curve;
611 const struct ccec_rfc6637_unwrap *unwrap;
612 ccec_full_ctx_t fullkey = key->key;
613 CFMutableDataRef data;
618 curve = get_rfc6637_curve(key);
620 SecError(errSecUnsupportedOperation, error, CFSTR("unsupported curve"));
624 CFTypeRef wrapAlg = CFDictionaryGetValue(parameters, _kSecKeyWrapPGPWrapAlg);
625 if (wrapAlg == NULL) {
626 SecError(errSecUnsupportedOperation, error, CFSTR("no wrap alg"));
628 } else if (CFEqual(wrapAlg, _kSecKeyWrapRFC6637WrapDigestSHA256KekAES128)) {
629 unwrap = &ccec_rfc6637_unwrap_sha256_kek_aes128;
630 } else if (CFEqual(wrapAlg, _kSecKeyWrapRFC6637WrapDigestSHA512KekAES256)) {
631 unwrap = &ccec_rfc6637_unwrap_sha512_kek_aes256;
633 SecError(errSecUnsupportedOperation, error, CFSTR("unknown wrap alg"));
637 CFDataRef fingerprint = CFDictionaryGetValue(parameters, _kSecKeyWrapPGPFingerprint);
638 if (!isData(fingerprint) || CFDataGetLength(fingerprint) < kSecKeyWrapPGPFingerprintMinSize) {
639 SecError(errSecUnsupportedOperation, error, CFSTR("invalid fingerprint"));
643 CFNumberRef num = CFDictionaryGetValue(parameters, _kSecKeyWrapRFC6637Flags);
645 if (!CFNumberGetValue(num, kCFNumberSInt32Type, &flags)) {
646 SecError(errSecUnsupportedOperation, error, CFSTR("invalid flags: %@"), num);
650 SecError(errSecUnsupportedOperation, error, CFSTR("unknown flags"));
654 size_t keysize = CFDataGetLength(wrappedKey);
655 data = CFDataCreateMutableWithScratch(NULL, keysize);
659 res = ccec_rfc6637_unwrap_key(fullkey, &keysize, CFDataGetMutableBytePtr(data),
660 flags, &sym_alg, curve, unwrap,
661 CFDataGetBytePtr(fingerprint),
662 CFDataGetLength(wrappedKey), CFDataGetBytePtr(wrappedKey));
665 SecError(errSecUnsupportedOperation, error, CFSTR("failed to wrap key"));
668 assert(keysize <= (size_t)CFDataGetLength(data));
669 CFDataSetLength(data, keysize);
672 CFMutableDictionaryRef out = CFDictionaryCreateMutableForCFTypes(NULL);
674 CFNumberRef num = CFNumberCreate(NULL, kCFNumberSInt8Type, &sym_alg);
676 CFDictionarySetValue(out, _kSecKeyWrapPGPSymAlg, num);
686 SecKeyDescriptor kSecECPrivateKeyDescriptor = {
687 .version = kSecKeyDescriptorVersion,
688 .name = "ECPrivateKey",
689 .extraBytes = ccec_full_ctx_size(ccn_sizeof(kMaximumECKeySize)),
691 .init = SecECPrivateKeyInit,
692 .destroy = SecECPrivateKeyDestroy,
693 .blockSize = SecECPrivateKeyBlockSize,
694 .copyDictionary = SecECPrivateKeyCopyAttributeDictionary,
695 .describe = SecECPrivateKeyCopyKeyDescription,
696 .getAlgorithmID = SecECKeyGetAlgorithmID,
697 .copyPublic = SecECPrivateKeyCopyPublicOctets,
698 .copyExternalRepresentation = SecECPrivateKeyCopyExternalRepresentation,
699 .copyWrapKey = SecECKeyCopyWrapKey,
700 .copyUnwrapKey = SecECKeyCopyUnwrapKey,
701 .copyOperationResult = SecECPrivateKeyCopyOperationResult,
704 /* Private Key API functions. */
705 SecKeyRef SecKeyCreateECPrivateKey(CFAllocatorRef allocator,
706 const uint8_t *keyData, CFIndex keyDataLength,
707 SecKeyEncoding encoding) {
708 return SecKeyCreate(allocator, &kSecECPrivateKeyDescriptor, keyData,
709 keyDataLength, encoding);
713 OSStatus SecECKeyGeneratePair(CFDictionaryRef parameters,
714 SecKeyRef *publicKey, SecKeyRef *privateKey) {
715 OSStatus status = errSecParam;
717 CFAllocatorRef allocator = NULL; /* @@@ get from parameters. */
718 SecKeyRef pubKey = NULL;
720 SecKeyRef privKey = SecKeyCreate(allocator, &kSecECPrivateKeyDescriptor,
721 (const void*) parameters, 0, kSecGenerateKey);
723 require_quiet(privKey, errOut);
725 /* Create SecKeyRef's from the pkcs1 encoded keys. */
726 pubKey = SecKeyCreate(allocator, &kSecECPublicKeyDescriptor,
727 privKey->key, 0, kSecExtractPublicFromPrivate);
729 require_quiet(pubKey, errOut);
736 *privateKey = privKey;
740 status = errSecSuccess;
743 CFReleaseSafe(pubKey);
744 CFReleaseSafe(privKey);
750 /* It's debatable whether this belongs here or in the ssl code since the
751 curve values come from a tls related rfc4492. */
752 SecECNamedCurve SecECKeyGetNamedCurve(SecKeyRef key) {
753 SecECNamedCurve result = kSecECCurveNone;
754 CFDictionaryRef attributes = NULL;
755 require_quiet(SecKeyGetAlgorithmId(key) == kSecECDSAAlgorithmID, out);
756 require_quiet(attributes = SecKeyCopyAttributes(key), out);
757 CFTypeRef bitsRef = CFDictionaryGetValue(attributes, kSecAttrKeySizeInBits);
759 require_quiet(bitsRef != NULL && CFGetTypeID(bitsRef) == CFNumberGetTypeID() &&
760 CFNumberGetValue(bitsRef, kCFNumberCFIndexType, &bits), out);
764 result = kSecECCurveSecp192r1;
767 result = kSecECCurveSecp224r1;
771 result = kSecECCurveSecp256r1;
774 result = kSecECCurveSecp384r1;
777 result = kSecECCurveSecp521r1;
782 CFReleaseSafe(attributes);
786 CFDataRef SecECKeyCopyPublicBits(SecKeyRef key) {
787 CFDataRef bytes = NULL;
788 SecKeyCopyPublicBytes(key, &bytes);
792 /* Vile accessors that get us the pub or priv key to use temporarily */
794 bool SecECDoWithFullKey(SecKeyRef key, CFErrorRef* error, void (^action)(ccec_full_ctx_t private)) {
795 if (key->key_class == &kSecECPrivateKeyDescriptor) {
798 return SecError(errSecParam, error, CFSTR("Not an EC Full Key object, sorry can't do."));
804 bool SecECDoWithPubKey(SecKeyRef key, CFErrorRef* error, void (^action)(ccec_pub_ctx_t public)) {
805 if (key->key_class == &kSecECPublicKeyDescriptor) {
808 return SecError(errSecParam, error, CFSTR("Not an EC Public Key object, sorry can't do."));