2 * Copyright (c) 2006-2010,2012-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 * SecRSAKey.c - CoreFoundation based rsa key object
29 #include "SecRSAKey.h"
30 #include "SecRSAKeyPriv.h"
31 #include <Security/SecKeyInternal.h>
32 #include <Security/SecItem.h>
33 #include <Security/SecBasePriv.h>
34 #include <AssertMacros.h>
35 #include <Security/SecureTransport.h> /* For error codes. */
36 #include <CoreFoundation/CFData.h> /* For error codes. */
38 #include <sys/types.h>
40 #include <CoreFoundation/CFNumber.h>
41 #include <Security/SecFramework.h>
42 #include <Security/SecRandom.h>
43 #include <utilities/debugging.h>
44 #include <utilities/SecCFWrappers.h>
45 #include <utilities/SecCFError.h>
46 #include <utilities/array_size.h>
47 #include "SecItemPriv.h"
48 #include <Security/SecInternal.h>
50 #include <corecrypto/ccn.h>
51 #include <corecrypto/ccrsa.h>
52 #include <corecrypto/ccsha1.h>
53 #include <corecrypto/ccsha2.h>
55 #include <libDER/asn1Types.h>
56 #include <libDER/DER_Keys.h>
57 #include <libDER/DER_Encode.h>
59 #include <CommonCrypto/CommonDigest.h>
61 #include <corecrypto/ccrsa_priv.h>
66 #define kMaximumRSAKeyBits (1024 * 8)
68 #define RSA_PKCS1_PAD_SIGN 0x01
69 #define RSA_PKCS1_PAD_ENCRYPT 0x02
77 /* Public key static functions. */
78 static void SecRSAPublicKeyDestroy(SecKeyRef key
) {
79 /* Zero out the public key */
81 ccrsa_pub_ctx_t pubkey
;
82 pubkey
.pub
= key
->key
;
83 cc_clear(ccrsa_pub_ctx_size(ccn_sizeof_n(ccrsa_ctx_n(pubkey
))), pubkey
.pub
);
89 #define cc_skip_zeros(size, ptr) { while (size > 0 && *ptr == 0) { ++ptr; --size; } }
92 // pubkey is initilaized with an n which is the maximum it can hold
93 // We set the n to its correct value given m.
95 static int ccrsa_pub_init(ccrsa_pub_ctx_t pubkey
,
96 size_t m_size
, const uint8_t* m
,
97 size_t e_size
, const uint8_t* e
)
99 cc_skip_zeros(m_size
, m
);
101 cc_size nm
= ccn_nof_size(m_size
);
102 if (nm
> ccrsa_ctx_n(pubkey
))
105 ccrsa_ctx_n(pubkey
) = nm
;
107 ccn_read_uint(nm
, ccrsa_ctx_m(pubkey
), m_size
, m
);
108 cczp_init(ccrsa_ctx_zm(pubkey
));
110 return ccn_read_uint(nm
, ccrsa_ctx_e(pubkey
), e_size
, e
);
114 static OSStatus
ccrsa_pub_decode_apple(ccrsa_pub_ctx_t pubkey
, size_t pkcs1_size
, const uint8_t* pkcs1
)
116 OSStatus result
= errSecParam
;
118 DERItem keyItem
= {(DERByte
*)pkcs1
, pkcs1_size
};
119 DERRSAPubKeyApple decodedKey
;
121 require_noerr_action_quiet(DERParseSequence(&keyItem
,
122 DERNumRSAPubKeyAppleItemSpecs
, DERRSAPubKeyAppleItemSpecs
,
123 &decodedKey
, sizeof(decodedKey
)),
124 errOut
, result
= errSecDecode
);
126 // We could honor the reciprocal, but we don't think this is used enough to care.
127 // Don't bother exploding the below function to try to handle this case, it computes.
129 require_noerr_quiet(ccrsa_pub_init(pubkey
,
130 decodedKey
.modulus
.length
, decodedKey
.modulus
.data
,
131 decodedKey
.pubExponent
.length
, decodedKey
.pubExponent
.data
),
134 result
= errSecSuccess
;
141 static void ccasn_encode_int(cc_size n
, const cc_unit
*s
, size_t s_size
, uint8_t **buffer
)
143 **buffer
= ASN1_INTEGER
;
146 DERSize itemLength
= 4;
147 DEREncodeLength(s_size
, *buffer
, &itemLength
);
148 *buffer
+= itemLength
;
150 ccn_write_int(n
, s
, s_size
, *buffer
);
156 static OSStatus
SecRSAPublicKeyInit(SecKeyRef key
,
157 const uint8_t *keyData
, CFIndex keyDataLength
, SecKeyEncoding encoding
) {
159 OSStatus result
= errSecParam
;
160 ccrsa_pub_ctx_t pubkey
;
164 case kSecKeyEncodingBytes
: // Octets is PKCS1
165 case kSecKeyEncodingPkcs1
: {
166 size_n
= ccrsa_import_pub_n(keyDataLength
, keyData
);
167 require_quiet(size_n
!= 0, errOut
);
168 require_quiet(size_n
<= ccn_nof(kMaximumRSAKeyBits
), errOut
);
170 key
->key
= calloc(1, ccrsa_pub_ctx_size(ccn_sizeof_n(size_n
)));
171 require_action_quiet(key
->key
, errOut
, result
= errSecAllocate
);
173 pubkey
.pub
= key
->key
;
174 ccrsa_ctx_n(pubkey
) = size_n
;
176 require_noerr_quiet(ccrsa_import_pub(pubkey
, keyDataLength
, keyData
), errOut
);
178 result
= errSecSuccess
;
182 case kSecKeyEncodingApplePkcs1
:
183 /* for the few uses (I can't find any) that uses kSecKeyEncodingApplePkcs1, force largest keys */
184 size_n
= ccn_nof(kMaximumRSAKeyBits
);
186 key
->key
= calloc(1, ccrsa_pub_ctx_size(ccn_sizeof_n(size_n
)));
187 require_action_quiet(key
->key
, errOut
, result
= errSecAllocate
);
189 pubkey
.pub
= key
->key
;
190 ccrsa_ctx_n(pubkey
) = size_n
;
192 result
= ccrsa_pub_decode_apple(pubkey
, keyDataLength
, keyData
);
194 case kSecKeyEncodingRSAPublicParams
:
196 SecRSAPublicKeyParams
*params
= (SecRSAPublicKeyParams
*)keyData
;
198 size_n
= ccn_nof_size(params
->modulusLength
);
200 key
->key
= calloc(1, ccrsa_pub_ctx_size(ccn_sizeof_n(size_n
)));
201 require_action_quiet(key
->key
, errOut
, result
= errSecAllocate
);
203 pubkey
.pub
= key
->key
;
204 ccrsa_ctx_n(pubkey
) = size_n
;
206 require_noerr_quiet(ccrsa_pub_init(pubkey
,
207 params
->modulusLength
, params
->modulus
,
208 params
->exponentLength
, params
->exponent
), errOut
);
210 result
= errSecSuccess
;
213 case kSecExtractPublicFromPrivate
:
215 ccrsa_full_ctx_t fullKey
;
216 fullKey
.full
= (ccrsa_full_ctx
*) keyData
;
218 size_n
= ccrsa_ctx_n(fullKey
);
220 key
->key
= calloc(1, ccrsa_pub_ctx_size(ccn_sizeof_n(size_n
)));
221 require_action_quiet(key
->key
, errOut
, result
= errSecAllocate
);
223 pubkey
.pub
= key
->key
;
224 ccrsa_ctx_n(pubkey
) = size_n
;
226 memcpy(pubkey
.pub
, ccrsa_ctx_public(fullKey
).pub
, ccrsa_pub_ctx_size(ccn_sizeof_n(size_n
)));
227 result
= errSecSuccess
;
238 static CFTypeRef
SecRSAPublicKeyCopyOperationResult(SecKeyRef key
, SecKeyOperationType operation
, SecKeyAlgorithm algorithm
,
239 CFArrayRef allAlgorithms
, SecKeyOperationMode mode
,
240 CFTypeRef in1
, CFTypeRef in2
, CFErrorRef
*error
) {
242 require_action_quiet(CFEqual(algorithm
, kSecKeyAlgorithmRSAEncryptionRawCCUnit
), out
, result
= kCFNull
);
244 ccrsa_pub_ctx_t pubkey
;
245 pubkey
.pub
= key
->key
;
246 result
= kCFBooleanTrue
;
248 case kSecKeyOperationTypeEncrypt
:
249 if (mode
== kSecKeyOperationModePerform
) {
250 // Verify that plaintext is smaller than modulus.
251 require_action_quiet(ccn_cmpn(ccn_nof_size(CFDataGetLength(in1
)), (const cc_unit
*)CFDataGetBytePtr(in1
),
252 ccrsa_ctx_n(pubkey
), ccrsa_ctx_m(pubkey
)) < 0, out
,
254 SecError(errSecParam
, error
, CFSTR("RSApubkey wrong size of buffer to encrypt"))));
256 // Encrypt into output buffer.
257 result
= CFDataCreateMutableWithScratch(NULL
, ccrsa_block_size(pubkey
));
258 ccrsa_pub_crypt(pubkey
, (cc_unit
*)CFDataGetMutableBytePtr((CFMutableDataRef
)result
),
259 (const cc_unit
*)CFDataGetBytePtr(in1
));
262 case kSecKeyOperationTypeDecrypt
:
263 if (mode
== kSecKeyOperationModePerform
) {
264 // Decrypt into output buffer.
265 result
= CFDataCreateMutableWithScratch(NULL
, ccrsa_block_size(pubkey
));
266 ccrsa_pub_crypt(pubkey
, (cc_unit
*)CFDataGetMutableBytePtr((CFMutableDataRef
)result
),
267 (const cc_unit
*)CFDataGetBytePtr(in1
));
279 static size_t SecRSAPublicKeyBlockSize(SecKeyRef key
) {
280 ccrsa_pub_ctx_t pubkey
;
281 pubkey
.pub
= key
->key
;
282 return ccrsa_block_size(pubkey
);
286 static CFDataRef
SecRSAPublicKeyCreatePKCS1(CFAllocatorRef allocator
, ccrsa_pub_ctx_t pubkey
)
288 size_t m_size
= ccn_write_int_size(ccrsa_ctx_n(pubkey
), ccrsa_ctx_m(pubkey
));
289 size_t e_size
= ccn_write_int_size(ccrsa_ctx_n(pubkey
), ccrsa_ctx_e(pubkey
));
291 const size_t seq_size
= DERLengthOfItem(ASN1_INTEGER
, m_size
) +
292 DERLengthOfItem(ASN1_INTEGER
, e_size
);
294 const size_t result_size
= DERLengthOfItem(ASN1_SEQUENCE
, seq_size
);
296 CFMutableDataRef pkcs1
= CFDataCreateMutableWithScratch(allocator
, result_size
);
297 uint8_t *bytes
= CFDataGetMutableBytePtr(pkcs1
);
299 *bytes
++ = ONE_BYTE_ASN1_CONSTR_SEQUENCE
;
301 DERSize itemLength
= 4;
302 DEREncodeLength(seq_size
, bytes
, &itemLength
);
305 ccasn_encode_int(ccrsa_ctx_n(pubkey
), ccrsa_ctx_m(pubkey
), m_size
, &bytes
);
306 ccasn_encode_int(ccrsa_ctx_n(pubkey
), ccrsa_ctx_e(pubkey
), e_size
, &bytes
);
311 static OSStatus
SecRSAPublicKeyCopyPublicSerialization(SecKeyRef key
, CFDataRef
* serialized
)
313 ccrsa_pub_ctx_t pubkey
;
314 pubkey
.pub
= key
->key
;
316 CFAllocatorRef allocator
= CFGetAllocator(key
);
317 *serialized
= SecRSAPublicKeyCreatePKCS1(allocator
, pubkey
);
319 if (NULL
== *serialized
)
322 return errSecSuccess
;
325 static CFDictionaryRef
SecRSAPublicKeyCopyAttributeDictionary(SecKeyRef key
) {
326 CFDictionaryRef dict
= SecKeyGeneratePublicAttributeDictionary(key
, kSecAttrKeyTypeRSA
);
327 CFMutableDictionaryRef mutableDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
328 CFDictionarySetValue(mutableDict
, kSecAttrCanDecrypt
, kCFBooleanTrue
);
329 CFDictionarySetValue(mutableDict
, kSecAttrCanDerive
, kCFBooleanFalse
);
330 CFAssignRetained(dict
, mutableDict
);
334 static CFDataRef
SecRSAPublicKeyCopyExternalRepresentation(SecKeyRef key
, CFErrorRef
*error
) {
335 ccrsa_pub_ctx_t pubkey
;
336 pubkey
.pub
= key
->key
;
337 return SecRSAPublicKeyCreatePKCS1(CFGetAllocator(key
), pubkey
);
340 static CFStringRef
SecRSAPublicKeyCopyDescription(SecKeyRef key
) {
342 CFStringRef keyDescription
= NULL
;
343 CFDataRef modRef
= SecKeyCopyModulus(key
);
345 ccrsa_pub_ctx_t pubkey
;
346 pubkey
.pub
= key
->key
;
348 CFStringRef modulusString
= CFDataCopyHexString(modRef
);
349 require_quiet(modulusString
, fail
);
351 keyDescription
= CFStringCreateWithFormat(kCFAllocatorDefault
,NULL
,CFSTR( "<SecKeyRef algorithm id: %lu, key type: %s, version: %d, block size: %zu bits, exponent: {hex: %llx, decimal: %lld}, modulus: %@, addr: %p>"), SecKeyGetAlgorithmId(key
), key
->key_class
->name
, key
->key_class
->version
, (8*SecKeyGetBlockSize(key
)), (long long)*ccrsa_ctx_e(pubkey
), (long long)*ccrsa_ctx_e(pubkey
), modulusString
, key
);
354 CFReleaseSafe(modRef
);
355 CFReleaseSafe(modulusString
);
357 keyDescription
= CFStringCreateWithFormat(kCFAllocatorDefault
,NULL
,CFSTR("<SecKeyRef algorithm id: %lu, key type: %s, version: %d, block size: %zu bits, addr: %p>"), (long)SecKeyGetAlgorithmId(key
), key
->key_class
->name
, key
->key_class
->version
, (8*SecKeyGetBlockSize(key
)), key
);
359 return keyDescription
;
362 SecKeyDescriptor kSecRSAPublicKeyDescriptor
= {
363 .version
= kSecKeyDescriptorVersion
,
364 .name
= "RSAPublicKey",
366 .init
= SecRSAPublicKeyInit
,
367 .destroy
= SecRSAPublicKeyDestroy
,
368 .blockSize
= SecRSAPublicKeyBlockSize
,
369 .copyDictionary
= SecRSAPublicKeyCopyAttributeDictionary
,
370 .copyExternalRepresentation
= SecRSAPublicKeyCopyExternalRepresentation
,
371 .describe
= SecRSAPublicKeyCopyDescription
,
372 .copyPublic
= SecRSAPublicKeyCopyPublicSerialization
,
373 .copyOperationResult
= SecRSAPublicKeyCopyOperationResult
,
376 /* Public Key API functions. */
377 SecKeyRef
SecKeyCreateRSAPublicKey_ios(CFAllocatorRef allocator
,
378 const uint8_t *keyData
, CFIndex keyDataLength
,
379 SecKeyEncoding encoding
) {
380 return SecKeyCreate(allocator
, &kSecRSAPublicKeyDescriptor
, keyData
,
381 keyDataLength
, encoding
);
384 SecKeyRef
SecKeyCreateRSAPublicKey(CFAllocatorRef allocator
,
385 const uint8_t *keyData
, CFIndex keyDataLength
,
386 SecKeyEncoding encoding
) {
387 return SecKeyCreateRSAPublicKey_ios(allocator
, keyData
,
388 keyDataLength
, encoding
);
391 CFDataRef
SecKeyCopyModulus(SecKeyRef key
) {
392 CFDataRef modulus
= NULL
;
393 if (key
->key_class
== &kSecRSAPublicKeyDescriptor
) {
394 ccrsa_pub_ctx_t pubkey
;
395 pubkey
.pub
= key
->key
;
397 size_t m_size
= ccn_write_uint_size(ccrsa_ctx_n(pubkey
), ccrsa_ctx_m(pubkey
));
399 CFAllocatorRef allocator
= CFGetAllocator(key
);
400 CFMutableDataRef modulusData
= CFDataCreateMutable(allocator
, m_size
);
402 if (modulusData
== NULL
)
405 CFDataSetLength(modulusData
, m_size
);
407 ccn_write_uint(ccrsa_ctx_n(pubkey
), ccrsa_ctx_m(pubkey
), m_size
, CFDataGetMutableBytePtr(modulusData
));
408 modulus
= modulusData
;
409 } else if (key
->key_class
->copyDictionary
!= NULL
) {
410 CFDictionaryRef dict
= key
->key_class
->copyDictionary(key
);
412 modulus
= CFRetainSafe(CFDictionaryGetValue(dict
, CFSTR("_rsam")));
420 CFDataRef
SecKeyCopyExponent(SecKeyRef key
) {
421 CFDataRef exponent
= NULL
;
422 if (key
->key_class
== &kSecRSAPublicKeyDescriptor
) {
423 ccrsa_pub_ctx_t pubkey
;
424 pubkey
.pub
= key
->key
;
426 size_t e_size
= ccn_write_uint_size(ccrsa_ctx_n(pubkey
), ccrsa_ctx_e(pubkey
));
428 CFAllocatorRef allocator
= CFGetAllocator(key
);
429 CFMutableDataRef exponentData
= CFDataCreateMutable(allocator
, e_size
);
431 if (exponentData
== NULL
)
434 CFDataSetLength(exponentData
, e_size
);
436 ccn_write_uint(ccrsa_ctx_n(pubkey
), ccrsa_ctx_e(pubkey
), e_size
, CFDataGetMutableBytePtr(exponentData
));
437 exponent
= exponentData
;
438 } else if (key
->key_class
->copyDictionary
!= NULL
) {
439 CFDictionaryRef dict
= key
->key_class
->copyDictionary(key
);
441 exponent
= CFRetainSafe(CFDictionaryGetValue(dict
, CFSTR("_rsae")));
456 /* Private key static functions. */
457 static void SecRSAPrivateKeyDestroy(SecKeyRef key
) {
458 /* Zero out the public key */
460 ccrsa_full_ctx_t fullkey
;
461 fullkey
.full
= key
->key
;
462 cc_clear(ccrsa_full_ctx_size(ccn_sizeof_n(ccrsa_ctx_n(fullkey
))), fullkey
.full
);
468 static OSStatus
SecRSAPrivateKeyInit(SecKeyRef key
, const uint8_t *keyData
, CFIndex keyDataLength
, SecKeyEncoding encoding
) {
469 OSStatus result
= errSecParam
;
470 ccrsa_full_ctx_t fullkey
;
474 case kSecKeyEncodingBytes
: // Octets is PKCS1
475 case kSecKeyEncodingPkcs1
:
477 size_n
= ccrsa_import_priv_n(keyDataLength
,keyData
);
478 require_quiet(size_n
!= 0, errOut
);
479 require_quiet(size_n
<= ccn_nof(kMaximumRSAKeyBits
), errOut
);
481 key
->key
= calloc(1, ccrsa_full_ctx_size(ccn_sizeof_n(size_n
)));
482 require_action_quiet(key
->key
, errOut
, result
= errSecAllocate
);
484 fullkey
.full
= key
->key
;
485 ccrsa_ctx_n(fullkey
) = size_n
;
487 require_quiet(ccrsa_import_priv(fullkey
, keyDataLength
, keyData
)==0, errOut
);
489 result
= errSecSuccess
;
492 case kSecGenerateKey
:
494 CFDictionaryRef parameters
= (CFDictionaryRef
) keyData
;
496 CFTypeRef ksize
= CFDictionaryGetValue(parameters
, kSecAttrKeySizeInBits
);
497 CFIndex keyLengthInBits
= getIntValue(ksize
);
499 if (keyLengthInBits
< 512 || keyLengthInBits
> kMaximumRSAKeyBits
) {
500 secwarning("Invalid or missing key size in: %@", parameters
);
501 result
= errSecKeySizeNotAllowed
;
505 size_n
= ccn_nof(keyLengthInBits
);
507 key
->key
= calloc(1, ccrsa_full_ctx_size(ccn_sizeof_n(size_n
)));
508 require_action_quiet(key
->key
, errOut
, result
= errSecAllocate
);
510 fullkey
.full
= key
->key
;
511 ccrsa_ctx_n(fullkey
) = size_n
;
513 /* TODO: Add support for kSecPublicExponent parameter. */
514 static uint8_t e
[] = { 0x01, 0x00, 0x01 }; // Default is 65537
515 if (!ccrsa_generate_fips186_key(keyLengthInBits
, fullkey
.full
, sizeof(e
), e
, ccrng_seckey
,ccrng_seckey
))
516 result
= errSecSuccess
;
526 static CFTypeRef
SecRSAPrivateKeyCopyOperationResult(SecKeyRef key
, SecKeyOperationType operation
, SecKeyAlgorithm algorithm
,
527 CFArrayRef allAlgorithms
, SecKeyOperationMode mode
,
528 CFTypeRef in1
, CFTypeRef in2
, CFErrorRef
*error
) {
529 CFTypeRef result
= kCFNull
;
531 ccrsa_full_ctx_t fullkey
= { .full
= key
->key
};
533 case kSecKeyOperationTypeSign
:
534 if (CFEqual(algorithm
, kSecKeyAlgorithmRSASignatureRawCCUnit
)) {
535 if (mode
== kSecKeyOperationModePerform
) {
536 // Verify that data is smaller than modulus.
537 require_action_quiet(ccn_cmpn(ccn_nof_size(CFDataGetLength(in1
)), (const cc_unit
*)CFDataGetBytePtr(in1
),
538 ccrsa_ctx_n(fullkey
), ccrsa_ctx_m(fullkey
)) < 0, out
,
540 SecError(errSecParam
, error
, CFSTR("%@: sign - digest too big (%d bytes)"),
541 (int)CFDataGetLength(in1
))));
543 // Encrypt buffer and write it to output data.
544 result
= CFDataCreateMutableWithScratch(kCFAllocatorDefault
, ccrsa_block_size(ccrsa_ctx_public(fullkey
)));
545 ccrsa_priv_crypt(fullkey
, (cc_unit
*)CFDataGetMutableBytePtr((CFMutableDataRef
)result
),
546 (const cc_unit
*)CFDataGetBytePtr(in1
));
548 // Operation is supported.
549 result
= kCFBooleanTrue
;
553 case kSecKeyOperationTypeDecrypt
:
554 if (CFEqual(algorithm
, kSecKeyAlgorithmRSAEncryptionRawCCUnit
)) {
555 if (mode
== kSecKeyOperationModePerform
) {
556 // Decrypt buffer and write it to output data.
557 result
= CFDataCreateMutableWithScratch(NULL
, ccrsa_block_size(fullkey
));
558 ccrsa_priv_crypt(fullkey
, (cc_unit
*)CFDataGetMutableBytePtr((CFMutableDataRef
)result
),
559 (const cc_unit
*)CFDataGetBytePtr(in1
));
561 // Operation is supported.
562 result
= kCFBooleanTrue
;
574 static size_t SecRSAPrivateKeyBlockSize(SecKeyRef key
) {
575 ccrsa_full_ctx_t fullkey
;
576 fullkey
.full
= key
->key
;
578 return ccn_write_uint_size(ccrsa_ctx_n(fullkey
), ccrsa_ctx_m(fullkey
));
581 static CFDataRef
SecRSAPrivateKeyCreatePKCS1(CFAllocatorRef allocator
, ccrsa_full_ctx_t fullkey
)
583 const size_t result_size
= ccrsa_export_priv_size(fullkey
);
585 CFMutableDataRef pkcs1
= CFDataCreateMutable(allocator
, result_size
);
590 CFDataSetLength(pkcs1
, result_size
);
592 uint8_t *bytes
= CFDataGetMutableBytePtr(pkcs1
);
594 if (ccrsa_export_priv(fullkey
,result_size
,bytes
)!=0) {
595 /* Decoding failed */
596 CFReleaseNull(pkcs1
);
603 static CFDataRef
SecRSAPrivateKeyCopyPKCS1(SecKeyRef key
)
605 ccrsa_full_ctx_t fullkey
;
606 fullkey
.full
= key
->key
;
608 CFAllocatorRef allocator
= CFGetAllocator(key
);
609 return SecRSAPrivateKeyCreatePKCS1(allocator
, fullkey
);
612 static OSStatus
SecRSAPrivateKeyCopyPublicSerialization(SecKeyRef key
, CFDataRef
* serialized
)
614 ccrsa_full_ctx_t fullkey
;
615 fullkey
.full
= key
->key
;
617 CFAllocatorRef allocator
= CFGetAllocator(key
);
618 *serialized
= SecRSAPublicKeyCreatePKCS1(allocator
, fullkey
);
620 if (NULL
== *serialized
)
623 return errSecSuccess
;
627 static CFDictionaryRef
SecRSAPrivateKeyCopyAttributeDictionary(SecKeyRef key
) {
628 CFDictionaryRef dict
= NULL
;
629 CFDataRef fullKeyBlob
= NULL
;
631 /* PKCS1 encode the key pair. */
632 fullKeyBlob
= SecRSAPrivateKeyCopyPKCS1(key
);
633 require_quiet(fullKeyBlob
, errOut
);
635 dict
= SecKeyGeneratePrivateAttributeDictionary(key
, kSecAttrKeyTypeRSA
, fullKeyBlob
);
636 CFMutableDictionaryRef mutableDict
= CFDictionaryCreateMutableCopy(NULL
, 0, dict
);
637 CFDictionarySetValue(mutableDict
, kSecAttrCanDerive
, kCFBooleanFalse
);
638 CFAssignRetained(dict
, mutableDict
);
641 CFReleaseSafe(fullKeyBlob
);
646 static CFDataRef
SecRSAPrivateKeyCopyExternalRepresentation(SecKeyRef key
, CFErrorRef
*error
) {
647 return SecRSAPrivateKeyCopyPKCS1(key
);
650 static CFStringRef
SecRSAPrivateKeyCopyDescription(SecKeyRef key
){
652 return CFStringCreateWithFormat(kCFAllocatorDefault
,NULL
,CFSTR( "<SecKeyRef algorithm id: %lu, key type: %s, version: %d, block size: %zu bits, addr: %p>"), SecKeyGetAlgorithmId(key
), key
->key_class
->name
, key
->key_class
->version
, (8*SecKeyGetBlockSize(key
)), key
);
656 SecKeyDescriptor kSecRSAPrivateKeyDescriptor
= {
657 .version
= kSecKeyDescriptorVersion
,
658 .name
= "RSAPrivateKey",
660 .init
= SecRSAPrivateKeyInit
,
661 .destroy
= SecRSAPrivateKeyDestroy
,
662 .blockSize
= SecRSAPrivateKeyBlockSize
,
663 .copyExternalRepresentation
= SecRSAPrivateKeyCopyExternalRepresentation
,
664 .copyDictionary
= SecRSAPrivateKeyCopyAttributeDictionary
,
665 .describe
= SecRSAPrivateKeyCopyDescription
,
666 .copyPublic
= SecRSAPrivateKeyCopyPublicSerialization
,
667 .copyOperationResult
= SecRSAPrivateKeyCopyOperationResult
,
670 /* Private Key API functions. */
671 SecKeyRef
SecKeyCreateRSAPrivateKey(CFAllocatorRef allocator
,
672 const uint8_t *keyData
, CFIndex keyDataLength
,
673 SecKeyEncoding encoding
) {
674 return SecKeyCreate(allocator
, &kSecRSAPrivateKeyDescriptor
, keyData
,
675 keyDataLength
, encoding
);
679 OSStatus
SecRSAKeyGeneratePair(CFDictionaryRef parameters
,
680 SecKeyRef
*rsaPublicKey
, SecKeyRef
*rsaPrivateKey
) {
681 OSStatus status
= errSecParam
;
683 CFAllocatorRef allocator
= NULL
; /* @@@ get from parameters. */
685 SecKeyRef pubKey
= NULL
;
686 SecKeyRef privKey
= SecKeyCreate(allocator
, &kSecRSAPrivateKeyDescriptor
,
687 (const void*) parameters
, 0, kSecGenerateKey
);
689 require_quiet(privKey
, errOut
);
691 /* Create SecKeyRef's from the pkcs1 encoded keys. */
692 pubKey
= SecKeyCreate(allocator
, &kSecRSAPublicKeyDescriptor
,
693 privKey
->key
, 0, kSecExtractPublicFromPrivate
);
695 require_quiet(pubKey
, errOut
);
698 *rsaPublicKey
= pubKey
;
702 *rsaPrivateKey
= privKey
;
706 status
= errSecSuccess
;
709 CFReleaseSafe(pubKey
);
710 CFReleaseSafe(privKey
);