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@
26 The functions provided in SecKeyPriv.h implement and manage a particular
27 type of keychain item that represents a key. A key can be stored in a
28 keychain, but a key can also be a transient object.
30 You can use a key as a keychain item in most functions.
33 #ifndef _SECURITY_SECKEYPRIV_H_
34 #define _SECURITY_SECKEYPRIV_H_
36 #include <Security/SecKey.h>
37 #include <Security/SecAsn1Types.h>
38 #include <CoreFoundation/CFRuntime.h>
39 #include <CoreFoundation/CoreFoundation.h>
40 #include <CoreFoundation/CFDictionary.h>
44 typedef struct __SecDERKey
{
49 CFIndex parametersLength
;
51 /* Contents of BIT STRING in DER Encoding */
57 typedef uint32_t SecKeyEncoding
;
59 /* Typically only used for symmetric keys. */
60 kSecKeyEncodingRaw
= 0,
62 /* RSA keys are DER-encoded according to PKCS1. */
63 kSecKeyEncodingPkcs1
= 1,
65 /* RSA keys are DER-encoded according to PKCS1 with Apple Extensions. */
66 kSecKeyEncodingApplePkcs1
= 2,
68 /* RSA public key in SecRSAPublicKeyParams format. keyData is a pointer
69 to a SecRSAPublicKeyParams and keyDataLength is
70 sizeof(SecRSAPublicKeyParams). */
71 kSecKeyEncodingRSAPublicParams
= 3,
73 /* RSA public key in SecRSAPublicKeyParams format. keyData is a pointer
74 to a SecRSAPublicKeyParams and keyDataLength is
75 sizeof(SecRSAPublicKeyParams). */
76 kSecDERKeyEncoding
= 4,
78 /* Internal "encodings to send other data" */
80 kSecExtractPublicFromPrivate
= 6,
82 /* Encoding came from SecKeyCopyPublicBytes for a public key,
83 or internally from a private key */
84 kSecKeyEncodingBytes
= 7,
86 /* Handing in a private key from corecrypto directly. */
87 kSecKeyCoreCrypto
= 8,
91 typedef uint32_t SecKeyWrapType
;
93 /* wrap key in RFC3394 (AESWrap) */
94 kSecKeyWrapRFC3394
= 0,
96 /* wrap key in PGP style (support EC keys only right now) */
97 kSecKeyWrapPublicKeyPGP
= 1,
101 typedef CF_ENUM(CFIndex
, SecKeyOperationMode
) {
102 kSecKeyOperationModePerform
= 0,
103 kSecKeyOperationModeCheckIfSupported
= 1,
106 typedef OSStatus (*SecKeyInitMethod
)(SecKeyRef
, const uint8_t *, CFIndex
,
108 typedef void (*SecKeyDestroyMethod
)(SecKeyRef
);
109 typedef OSStatus (*SecKeyRawSignMethod
)(SecKeyRef key
, SecPadding padding
,
110 const uint8_t *dataToSign
, size_t dataToSignLen
,
111 uint8_t *sig
, size_t *sigLen
);
112 typedef OSStatus (*SecKeyRawVerifyMethod
)(
113 SecKeyRef key
, SecPadding padding
, const uint8_t *signedData
,
114 size_t signedDataLen
, const uint8_t *sig
, size_t sigLen
);
115 typedef OSStatus (*SecKeyEncryptMethod
)(SecKeyRef key
, SecPadding padding
,
116 const uint8_t *plainText
, size_t plainTextLen
,
117 uint8_t *cipherText
, size_t *cipherTextLen
);
118 typedef OSStatus (*SecKeyDecryptMethod
)(SecKeyRef key
, SecPadding padding
,
119 const uint8_t *cipherText
, size_t cipherTextLen
,
120 uint8_t *plainText
, size_t *plainTextLen
);
121 typedef OSStatus (*SecKeyComputeMethod
)(SecKeyRef key
,
122 const uint8_t *pub_key
, size_t pub_key_len
,
123 uint8_t *computed_key
, size_t *computed_key_len
);
124 typedef size_t (*SecKeyBlockSizeMethod
)(SecKeyRef key
);
125 typedef CFDictionaryRef (*SecKeyCopyDictionaryMethod
)(SecKeyRef key
);
126 typedef CFIndex (*SecKeyGetAlgorithmIDMethod
)(SecKeyRef key
);
127 typedef OSStatus (*SecKeyCopyPublicBytesMethod
)(SecKeyRef key
, CFDataRef
*serialization
);
128 typedef CFDataRef (*SecKeyCopyWrapKeyMethod
)(SecKeyRef key
, SecKeyWrapType type
, CFDataRef unwrappedKey
, CFDictionaryRef parameters
, CFDictionaryRef
*outParam
, CFErrorRef
*error
);
129 typedef CFDataRef (*SecKeyCopyUnwrapKeyMethod
)(SecKeyRef key
, SecKeyWrapType type
, CFDataRef wrappedKey
, CFDictionaryRef parameters
, CFDictionaryRef
*outParam
, CFErrorRef
*error
);
130 typedef CFStringRef (*SecKeyDescribeMethod
)(SecKeyRef key
);
132 typedef CFDataRef (*SecKeyCopyExternalRepresentationMethod
)(SecKeyRef key
, CFErrorRef
*error
);
133 typedef SecKeyRef (*SecKeyCopyPublicKeyMethod
)(SecKeyRef key
);
134 typedef Boolean (*SecKeyIsEqualMethod
)(SecKeyRef key1
, SecKeyRef key2
);
137 @abstract Performs cryptographic operation with the key.
138 @param key Key to perform the operation on.
139 @param operation Type of operation to be performed.
140 @param algorithm Algorithm identifier for the operation. Determines format of input and output data.
141 @param allAlgorithms Array of algorithms which were traversed until we got to this operation. The last member of this array is always the same as @c algorithm parameter.
142 @param mode Mode in which the operation is performed. Two available modes are checking only if the operation can be performed or actually performing the operation.
143 @param in1 First input parameter for the operation, meaningful only in ModePerform.
144 @param in2 Second input parameter for the operation, meaningful only in ModePerform.
145 @param error Error details when NULL is returned.
146 @return NULL if some failure occured. kCFNull if operation/algorithm/key combination is not supported, otherwise the result of the operation or kCFBooleanTrue in ModeCheckIfSupported.
148 typedef CFTypeRef(*SecKeyCopyOperationResultMethod
)(SecKeyRef key
, SecKeyOperationType operation
, SecKeyAlgorithm algorithm
, CFArrayRef allAlgorithms
, SecKeyOperationMode mode
, CFTypeRef in1
, CFTypeRef in2
, CFErrorRef
*error
);
150 #define kSecKeyDescriptorVersion (4)
152 typedef struct __SecKeyDescriptor
{
153 /* Version of this SecKeyDescriptor. Must be kSecKeyDescriptorVersion. */
156 /* Name of this key class for use by SecKeyShow(). */
159 /* If nonzero, SecKeyCreate will allocate this many bytes for the key
160 field in the SecKeyRef it creates. If zero key is NULL and the
161 implementor can choose to dynamically allocate it in the init
162 function and free it in the destroy function. */
165 /* Called by SecKeyCreate(). */
166 SecKeyInitMethod init
;
167 /* Called by destructor (final CFRelease() or gc if using). */
168 SecKeyDestroyMethod destroy
;
169 /* Called by SecKeyRawSign(). */
170 SecKeyRawSignMethod rawSign
;
171 /* Called by SecKeyRawVerify(). */
172 SecKeyRawVerifyMethod rawVerify
;
173 /* Called by SecKeyEncrypt(). */
174 SecKeyEncryptMethod encrypt
;
175 /* Called by SecKeyDecrypt(). */
176 SecKeyDecryptMethod decrypt
;
177 /* Reserved for future use. */
178 SecKeyComputeMethod compute
;
179 /* Called by SecKeyGetBlockSize(). */
180 SecKeyBlockSizeMethod blockSize
;
181 /* Called by SecKeyCopyAttributeDictionary(), which is private. */
182 SecKeyCopyDictionaryMethod copyDictionary
;
183 /* Called by SecKeyDescribeMethod(). */
184 SecKeyDescribeMethod describe
;
185 #if kSecKeyDescriptorVersion > 0
186 /* Called by SecKeyCopyAttributeDictionary(), which is private. */
187 SecKeyGetAlgorithmIDMethod getAlgorithmID
;
189 #if kSecKeyDescriptorVersion > 1
190 SecKeyCopyPublicBytesMethod copyPublic
;
192 #if kSecKeyDescriptorVersion > 2
193 SecKeyCopyWrapKeyMethod copyWrapKey
;
194 SecKeyCopyUnwrapKeyMethod copyUnwrapKey
;
196 #if kSecKeyDescriptorVersion > 3
197 SecKeyCopyExternalRepresentationMethod copyExternalRepresentation
;
198 SecKeyCopyPublicKeyMethod copyPublicKey
;
199 SecKeyCopyOperationResultMethod copyOperationResult
;
200 SecKeyIsEqualMethod isEqual
;
207 const SecKeyDescriptor
*key_class
;
209 #if !TARGET_OS_IPHONE
210 // On OSX, keep optional SecKeyRef which holds dynamically, on-demand created CSSM-based key with the same
211 // key material. It is used to implement SecKeyGetCSSMKey().
215 /* The actual key handled by class. */
220 @function SecKeyCreate
221 @abstract Given a private key and data to sign, generate a digital signature.
222 @param allocator allocator to use when allocating this key instance.
223 @param key_class pointer to a SecKeyDescriptor.
224 @param keyData The second argument to the init() function in the key_class.
225 @param keyDataLength The third argument to the init() function in the key_class.
226 @param encoding The fourth argument to the init() function in the key_class.
227 @result A newly allocated SecKeyRef.
229 SecKeyRef
SecKeyCreate(CFAllocatorRef allocator
,
230 const SecKeyDescriptor
*key_class
, const uint8_t *keyData
,
231 CFIndex keyDataLength
, SecKeyEncoding encoding
);
233 /* Create a public key from an oid, params and keyData all in DER format. */
234 SecKeyRef
SecKeyCreatePublicFromDER(CFAllocatorRef allocator
,
235 const SecAsn1Oid
*oid1
, const SecAsn1Item
*params
,
236 const SecAsn1Item
*keyData
);
238 /* Create a public key from a CFData containing a SubjectPublicKeyInfo in DER format. */
239 SecKeyRef
SecKeyCreateFromSubjectPublicKeyInfoData(CFAllocatorRef allocator
,
240 CFDataRef subjectPublicKeyInfoData
);
242 /* Create public key from private key */
243 SecKeyRef
SecKeyCreatePublicFromPrivate(SecKeyRef privateKey
);
245 /* Get Private Key (if present) by publicKey. */
246 SecKeyRef
SecKeyCopyMatchingPrivateKey(SecKeyRef publicKey
, CFErrorRef
*error
);
247 OSStatus
SecKeyGetMatchingPrivateKeyStatus(SecKeyRef publicKey
, CFErrorRef
*error
);
249 CFDataRef
SecKeyCreatePersistentRefToMatchingPrivateKey(SecKeyRef publicKey
, CFErrorRef
*error
);
251 /* Return an attribute dictionary used to find a private key by public key hash */
252 CFDictionaryRef
CreatePrivateKeyMatchingQuery(SecKeyRef publicKey
, bool returnPersistentRef
);
254 /* Return an attribute dictionary used to store this item in a keychain. */
255 CFDictionaryRef
SecKeyCopyAttributeDictionary(SecKeyRef key
);
257 /* Return a key from an attribute dictionary that was used to store this item
259 SecKeyRef
SecKeyCreateFromAttributeDictionary(CFDictionaryRef refAttributes
);
261 OSStatus
SecKeyDigestAndVerify(
262 SecKeyRef key
, /* Public key */
263 const SecAsn1AlgId
*algId
, /* algorithm oid/params */
264 const uint8_t *dataToDigest
, /* signature over this data */
265 size_t dataToDigestLen
,/* length of dataToDigest */
266 const uint8_t *sig
, /* signature to verify */
267 size_t sigLen
); /* length of sig */
269 OSStatus
SecKeyDigestAndSign(
270 SecKeyRef key
, /* Private key */
271 const SecAsn1AlgId
*algId
, /* algorithm oid/params */
272 const uint8_t *dataToDigest
, /* signature over this data */
273 size_t dataToDigestLen
,/* length of dataToDigest */
274 uint8_t *sig
, /* signature, RETURNED */
275 size_t *sigLen
); /* IN/OUT */
277 OSStatus
SecKeyVerifyDigest(
278 SecKeyRef key
, /* Private key */
279 const SecAsn1AlgId
*algId
, /* algorithm oid/params */
280 const uint8_t *digestData
, /* signature over this digest */
281 size_t digestDataLen
,/* length of dataToDigest */
282 const uint8_t *sig
, /* signature to verify */
283 size_t sigLen
); /* length of sig */
285 OSStatus
SecKeySignDigest(
286 SecKeyRef key
, /* Private key */
287 const SecAsn1AlgId
*algId
, /* algorithm oid/params */
288 const uint8_t *digestData
, /* signature over this digest */
289 size_t digestDataLen
,/* length of digestData */
290 uint8_t *sig
, /* signature, RETURNED */
291 size_t *sigLen
); /* IN/OUT */
293 OSStatus
SecKeyCopyPublicBytes(SecKeyRef key
, CFDataRef
* serializedPublic
);
294 SecKeyRef
SecKeyCreateFromPublicBytes(CFAllocatorRef allocator
, CFIndex algorithmID
, const uint8_t *keyData
, CFIndex keyDataLength
);
295 SecKeyRef
SecKeyCreateFromPublicData(CFAllocatorRef allocator
, CFIndex algorithmID
, CFDataRef serialized
);
296 CFDataRef
SecKeyCopyPublicKeyHash(SecKeyRef key
);
298 /* This function directly creates an iOS-format SecKeyRef from public key bytes. */
299 SecKeyRef
SecKeyCreateRSAPublicKey_ios(CFAllocatorRef allocator
,
300 const uint8_t *keyData
, CFIndex keyDataLength
,
301 SecKeyEncoding encoding
);
305 CFDictionaryRef
SecKeyGeneratePrivateAttributeDictionary(SecKeyRef key
,
307 CFDataRef privateBlob
);
309 CFDictionaryRef
SecKeyGeneratePublicAttributeDictionary(SecKeyRef key
, CFTypeRef keyType
);
312 kSecNullAlgorithmID
= 0,
313 kSecRSAAlgorithmID
= 1,
314 kSecDSAAlgorithmID
= 2, /* unsupported, just here for reference. */
315 kSecECDSAAlgorithmID
= 3,
319 @function SecKeyGetAlgorithmID
320 @abstract Returns an enumerated constant value which identifies the algorithm for the given key.
321 @param key A key reference.
322 @result An algorithm identifier.
323 @discussion Deprecated in iOS 9.0. Note that SecKeyGetAlgorithmID also exists on OS X
324 with different arguments for CDSA-based SecKeyRefs, and returns different values.
325 For compatibility, your code should migrate to use SecKeyGetAlgorithmId instead.
327 CFIndex
SecKeyGetAlgorithmID(SecKeyRef key
)
328 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_2
, __MAC_10_8
, __IPHONE_5_0
, __IPHONE_9_0
);
331 @function SecKeyGetAlgorithmId
332 @abstract Returns an enumerated constant value which identifies the algorithm for the given key.
333 @param key A key reference.
334 @result An algorithm identifier.
336 CFIndex
SecKeyGetAlgorithmId(SecKeyRef key
)
337 __OSX_AVAILABLE_STARTING(__MAC_10_8
, __IPHONE_9_0
);
341 kSecKeyKeySizeInBits
= 0,
342 kSecKeySignatureSize
= 1,
343 kSecKeyEncryptedDataSize
= 2,
344 // More might belong here, but we aren't settled on how
345 // to take into account padding and/or digest types.
349 @function SecKeyGetSize
350 @abstract Returns a size in bytes.
351 @param key The key for which the block length is requested.
352 @param whichSize The size that you want evaluated.
353 @result The block length of the key in bytes.
354 @discussion If for example key is an RSA key the value returned by
355 this function is the size of the modulus.
357 size_t SecKeyGetSize(SecKeyRef key
, SecKeySize whichSize
)
358 __OSX_AVAILABLE_STARTING(__MAC_10_8
, __IPHONE_5_0
);
362 @function SecKeyLookupPersistentRef
363 @abstract Looks up a SecKeyRef via persistent ref.
364 @param persistentRef The persistent ref data for looking up.
365 @param lookedUpData retained SecKeyRef for the found object.
366 @result Errors when using SecItemFind for the persistent ref.
368 OSStatus
SecKeyFindWithPersistentRef(CFDataRef persistentRef
, SecKeyRef
* lookedUpData
)
369 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_7_0
);
372 @function SecKeyCopyPersistentRef
373 @abstract Gets a persistent reference for a key.
374 @param key Key to make a persistent ref for.
375 @param persistentRef Allocated data representing the persistent ref.
376 @result Errors when using SecItemFind for the persistent ref.
378 OSStatus
SecKeyCopyPersistentRef(SecKeyRef key
, CFDataRef
* persistentRef
)
379 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_7_0
);
386 extern const CFStringRef _kSecKeyWrapPGPSymAlg
; /* CFNumber */
387 extern const CFStringRef _kSecKeyWrapPGPFingerprint
; /* CFDataRef, at least 20 bytes */
388 extern const CFStringRef _kSecKeyWrapPGPWrapAlg
; /* kSecKeyWrapRFC6637WrapNNN, or any of the other PGP wrap algs */
389 extern const CFStringRef _kSecKeyWrapRFC6637Flags
;
390 extern const CFStringRef _kSecKeyWrapRFC6637WrapDigestSHA256KekAES128
;
391 extern const CFStringRef _kSecKeyWrapRFC6637WrapDigestSHA512KekAES256
;
393 enum { kSecKeyWrapPGPFingerprintMinSize
= 20 };
395 @function _SecKeyCopyWrapKey
400 _SecKeyCopyWrapKey(SecKeyRef key
, SecKeyWrapType type
, CFDataRef unwrappedKey
, CFDictionaryRef parameters
, CFDictionaryRef
*outParam
, CFErrorRef
*error
)
401 __OSX_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
);
404 @function _SecKeyWrapKey
405 @abstract Unwrap a key
409 _SecKeyCopyUnwrapKey(SecKeyRef key
, SecKeyWrapType type
, CFDataRef wrappedKey
, CFDictionaryRef parameters
, CFDictionaryRef
*outParam
, CFErrorRef
*error
)
410 __OSX_AVAILABLE_STARTING(__MAC_10_10
, __IPHONE_8_0
);
413 @enum SecKeyAttestationKeyType
414 @abstract Defines types of builtin attestation keys.
416 typedef CF_ENUM(uint32_t, SecKeyAttestationKeyType
)
418 kSecKeyAttestationKeyTypeSIK
= 0,
419 kSecKeyAttestationKeyTypeGID
420 } __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
423 @function SecKeyCopyAttestationKey
424 @abstract Returns a copy of a builtin attestation key.
426 @param keyType Type of the requested builtin key.
427 @param error An optional pointer to a CFErrorRef. This value is set if an error occurred.
429 @result On success a SecKeyRef containing the requested key is returned, on failure it returns NULL.
431 SecKeyRef
SecKeyCopyAttestationKey(SecKeyAttestationKeyType keyType
, CFErrorRef
*error
)
432 __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
435 @function SecKeyCreateAttestation
436 @abstract Attests a key with another key.
438 @param key The attesting key.
439 @param keyToAttest The key which is to be attested.
440 @param error An optional pointer to a CFErrorRef. This value is set if an error occurred.
442 @result On success a CFDataRef containing the attestation data is returned, on failure it returns NULL.
444 @discussion Key attestation only works for CTK SEP keys, i.e. keys created with kSecAttrTokenID=kSecAttrTokenIDSecureEnclave.
446 CFDataRef
SecKeyCreateAttestation(SecKeyRef key
, SecKeyRef keyToAttest
, CFErrorRef
*error
)
447 __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
450 @function SecKeySetParameter
451 @abstract Sets unspecified key parameter for the backend.
453 @param key Key to set the parameter to.
454 @param name Identifies parameter to be set.
455 @param value New value for the parameter.
456 @param error Error which gathers more information when something went wrong.
458 @discussion Serves as channel between SecKey client and backend for passing additional sideband data send from SecKey caller
459 to SecKey implementation backend (currently only CTK-based token backend is supported). Parameter names and types are
460 a contract between SecKey user (application) and backend and are not interpreted by SecKey layer in any way.
462 Boolean
SecKeySetParameter(SecKeyRef key
, CFStringRef name
, CFPropertyListRef value
, CFErrorRef
*error
)
463 __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);
466 Algorithms for converting between bigendian and core-crypto ccunit data representation.
468 extern const SecKeyAlgorithm kSecKeyAlgorithmRSASignatureRawCCUnit
;
469 extern const SecKeyAlgorithm kSecKeyAlgorithmRSAEncryptionRawCCUnit
;
472 Internal algorithm for RSA-MD5. We do not want to export MD5 in new API, but we need it
473 for implementing legacy interfaces.
475 extern const SecKeyAlgorithm kSecKeyAlgorithmRSASignatureDigestPKCS1v15MD5
;
476 extern const SecKeyAlgorithm kSecKeyAlgorithmRSASignatureMessagePKCS1v15MD5
;
479 Algorithms for interoperability with libaks smartcard support.
481 extern const SecKeyAlgorithm kSecKeyAlgorithmECIESEncryptionAKSSmartCard
;
485 #endif /* !_SECURITY_SECKEYPRIV_H_ */