2 * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // AppleCSPUtils.h - CSP-wide utility functions
23 #ifndef _H_APPLE_CSP_UTILS
24 #define _H_APPLE_CSP_UTILS
26 #include "cspdebugging.h"
27 #include <Security/cssmtype.h>
28 #include <security_utilities/alloc.h>
29 #include <security_cdsa_utilities/context.h>
42 /* Key storage type returned from cspParseKeyAttr() */
49 #define KEY_ATTR_RETURN_MASK (CSSM_KEYATTR_RETURN_DATA | \
50 CSSM_KEYATTR_RETURN_REF | \
51 CSSM_KEYATTR_RETURN_NONE)
54 * Validate key attribute bits per specified key type.
56 * Used to check requested key attributes for new keys and for validating
57 * incoming existing keys. For checking key attributes for new keys,
58 * assumes that KEYATTR_RETURN_xxx bits have been checked elsewhere
59 * and stripped off before coming here.
61 void cspValidateKeyAttr(
66 * Perform sanity check of incoming key attribute bits for a given
67 * key type, and return a malKeyStorage value.
69 * Called from any routine which generates a new key. This specifically
72 cspKeyStorage
cspParseKeyAttr(
77 * Validate key usage bits for specified key type.
79 void cspValidateKeyUsageBits (
84 * Validate existing key's usage bits against intended use.
86 void cspValidateIntendedKeyUsage(
87 const CSSM_KEYHEADER
*hdr
,
88 CSSM_KEYUSE intendedUsage
);
91 * Set up a key header.
97 CSSM_KEYCLASS keyClass
,
98 CSSM_KEYATTR_FLAGS attrs
,
102 * Ensure that indicated CssmData can handle 'length' bytes
103 * of data. Malloc the Data ptr if necessary.
108 Allocator
&allocator
);
113 Allocator
&allocator
);
117 Allocator
&allocator
);
121 Allocator
&allocator
,
122 bool freeStruct
); // free the CSSM_DATA itself
125 * Copy source to destination, mallocing destination if necessary.
130 Allocator
&allocator
);
133 const CSSM_DATA
&src
,
135 Allocator
&allocator
);
138 * Compare two CSSM_DATAs, return CSSM_TRUE if identical.
140 CSSM_BOOL
cspCompareCssmData(
141 const CSSM_DATA
*data1
,
142 const CSSM_DATA
*data2
);
145 * This takes care of mallocing the and KeyLabel field.
148 const CssmKey::Header
&src
,
149 CssmKey::Header
&dst
,
150 Allocator
&allocator
);
153 * Given a wrapped key, infer its raw format.
154 * This is a real kludge; it only works as long as each {algorithm, keyClass}
155 * maps to exactly one format.
157 CSSM_KEYBLOB_FORMAT
inferFormat(
158 const CssmKey
&wrappedKey
);
161 * Given a key and a Context, obtain the optional associated
162 * CSSM_ATTRIBUTE_{PUBLIC,PRIVATE,SYMMETRIC}_KEY_FORMAT attribute as a
163 * CSSM_KEYBLOB_FORMAT.
165 CSSM_KEYBLOB_FORMAT
requestedKeyFormat(
166 const Context
&context
,
169 /* stateless function to calculate SHA-1 hash of a blob */
171 #define SHA1_DIGEST_SIZE 20
175 void *out
); // caller mallocs, digest goes here
177 void cspVerifyKeyTimes(
178 const CSSM_KEYHEADER
&hdr
);
184 #endif // _H_APPLE_CSP_UTILS