2 * Copyright (c) 2004,2011,2013-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@
23 * SecImportExportUtils.h - misc. utilities for import/export module
26 #ifndef _SECURITY_SEC_IMPORT_EXPORT_UTILS_H_
27 #define _SECURITY_SEC_IMPORT_EXPORT_UTILS_H_
29 #include "SecImportExport.h"
30 #include "SecKeychainPriv.h"
31 #include "SecBasePriv.h"
32 #include <security_utilities/debugging.h>
33 #include <security_utilities/errors.h>
40 * Macros for begin/end of public functions. Although the import/export
41 * module does not intentionally throw C++ exceptions which are intended to
42 * be caught like this, we catch possible exceptions which can conceivably
43 * be thrown here or below.
46 #define BEGIN_IMP_EXP_SECAPI \
49 #define END_IMP_EXP_SECAPI \
51 catch (const MacOSError &err) { return err.osStatus(); } \
52 catch (const CommonError &err) { return SecKeychainErrFromOSStatus(err.osStatus()); } \
53 catch (const std::bad_alloc &) { return errSecAllocate; }\
54 catch (...) { return errSecInternalComponent; }
62 #define impExpExtFormatStr(f)
63 #define impExpExtItemTypeStr(t)
67 extern const char *impExpExtFormatStr(SecExternalFormat format
);
68 extern const char *impExpExtItemTypeStr(SecExternalItemType itemType
);
72 #define SecImpExpDbg(args...) secdebug("SecImpExp", ## args)
73 #define SecImpInferDbg(args...) secdebug("SecImpInfer", ## args)
76 * Parse file extension and attempt to map it to format and type. Returns true
79 bool impExpImportParseFileExten(
81 SecExternalFormat
*inputFormat
, // RETURNED
82 SecExternalItemType
*itemType
); // RETURNED
84 /* do a [NSString stringByDeletingPathExtension] equivalent */
85 CFStringRef
impExpImportDeleteExtension(
89 * map {algorithm, class, SecExternalFormat} to a CSSM_KEYBLOB_FORMAT.
90 * Returns errSecUnsupportedFormat in the rare appropriate case.
92 OSStatus
impExpKeyForm(
93 SecExternalFormat externForm
,
94 SecExternalItemType itemType
,
96 CSSM_KEYBLOB_FORMAT
*cssmForm
, // RETURNED
97 CSSM_KEYCLASS
*cssmClass
); // RETRUNED
100 * Guess an incoming blob's type, format and (for keys only) algorithm
101 * by examining its contents. Returns true on success, in which case
102 * *inputFormat and *itemType, and *keyAlg are valid. Caller optionally
103 * passes in valid values any number of these as a clue.
105 bool impExpImportGuessByExamination(
107 SecExternalFormat
*inputFormat
, // may be kSecFormatUnknown on entry
108 SecExternalItemType
*itemType
, // may be kSecItemTypeUnknown on entry
109 CSSM_ALGORITHMS
*keyAlg
); // CSSM_ALGID_NONE - unknown
112 * Obtain the CDSA-layer CSSM_RESOURCE_CONTROL_CONTEXT given a SecAccessRef.
114 extern OSStatus
impExpAccessToRcc(
115 SecAccessRef accessRef
,
116 const CSSM_RESOURCE_CONTROL_CONTEXT
**rcc
);
120 /* low-level crypto/CSP support */
123 * Given a context specified via a CSSM_CC_HANDLE, add a new
124 * CSSM_CONTEXT_ATTRIBUTE to the context as specified by AttributeType,
125 * AttributeLength, and an untyped pointer.
127 CSSM_RETURN
impExpAddContextAttribute(CSSM_CC_HANDLE CCHandle
,
128 uint32 AttributeType
,
129 uint32 AttributeLength
,
130 const void *AttributePtr
);
133 * Free memory via specified plugin's app-level allocator
135 void impExpFreeCssmMemory(
140 * Calculate digest of any CSSM_KEY. Unlike older implementations
141 * of this logic, you can actually calculate the public key hash
142 * on any class of key, any format, raw CSP or CSPDL.
144 * Caller must free keyDigest->Data using impExpFreeCssmMemory() since
145 * this is allocated by the CSP's app-specified allocator.
147 CSSM_RETURN
impExpKeyDigest(
148 CSSM_CSP_HANDLE cspHand
,
150 CSSM_DATA_PTR keyDigest
); // contents allocd and RETURNED
153 * Given a CFTypeRef passphrase which may be a CFDataRef or a CFStringRef,
154 * return a refcounted CFStringRef suitable for use with the PKCS12 library.
155 * PKCS12 passphrases in CFData format must be UTF8 encoded.
157 OSStatus
impExpPassphraseToCFString(
159 CFStringRef
*passout
); // may be the same as passin, but refcounted
162 * Given a CFTypeRef passphrase which may be a CFDataRef or a CFStringRef,
163 * return a refcounted CFDataRef whose bytes are suitable for use with
164 * PKCS5 (v1.5 and v2.0) key derivation.
166 OSStatus
impExpPassphraseToCFData(
168 CFDataRef
*passout
); // may be the same as passin, but refcounted
171 * Obtain passphrase, given a SecKeyImportExportParameters.
173 * Passphrase comes from one of two places: app-specified, in
174 * SecKeyImportExportParameters.passphrase (either as CFStringRef
175 * or CFDataRef); or via the secure passphrase mechanism.
177 * Passphrase is returned in one of two forms:
179 * -- Secure passphrase is returned as a CSSM_KEY_PTR, which the
180 * caller must CSSM_FreeKey later. THe CSSM_KEY_PTR must also
183 * -- CFTypeRef for app-supplied passphrases. This can be one of
186 * -- CFStringRef, for use with P12
187 * -- CFDataRef, for more general use (e.g. for PKCS5).
189 * In either case the caller must CFRelease the result.
192 SPF_String
, // CFStringRef, P12
193 SPF_Data
// CFDataRef, PKCS5
194 } impExpPassphraseForm
;
197 VP_Export
, // verify passphrase
198 VP_Import
// no verify
199 } impExpVerifyPhrase
;
201 OSStatus
impExpPassphraseCommon(
202 const SecKeyImportExportParameters
*keyParams
,
203 CSSM_CSP_HANDLE cspHand
, // MUST be CSPDL, for passKey generation
204 impExpPassphraseForm phraseForm
,
205 impExpVerifyPhrase verifyPhrase
, // for secure passphrase
206 CFTypeRef
*phrase
, // RETURNED, or
207 CSSM_KEY_PTR
*passKey
); // mallocd and RETURNED
209 CSSM_KEYATTR_FLAGS
ConvertArrayToKeyAttributes(SecKeyRef aKey
, CFArrayRef usage
);
211 Boolean
ConvertSecKeyImportExportParametersToSecImportExportKeyParameters(SecKeyRef aKey
,
212 const SecItemImportExportKeyParameters
* newPtr
, SecKeyImportExportParameters
* oldPtr
);
218 #endif /* _SECURITY_SEC_IMPORT_EXPORT_UTILS_H_ */