]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cms/lib/CMSEncoder.h
a39bcbb9bd411cdacac48ec2b9da1dc00d628c73
[apple/security.git] / OSX / libsecurity_cms / lib / CMSEncoder.h
1 /*
2 * Copyright (c) 2006-2012 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * CMSEncoder.h - encode, sign, and/or encrypt messages in the Cryptographic
26 * Message Syntax (CMS), per RFC 3852.
27 *
28 * A CMS message can be signed, encrypted, or both. A message can be signed by
29 * an arbitrary number of signers; in this module, signers are expressed as
30 * SecIdentityRefs. A message can be encrypted for an arbitrary number of
31 * recipients; recipients are expressed here as SecCertificateRefs.
32 *
33 * In CMS terminology, this module performs encryption using the EnvelopedData
34 * ContentType and signing using the SignedData ContentType.
35 *
36 * If the message is both signed and encrypted, it uses "nested ContentInfos"
37 * in CMS terminology; in this implementation, signed & encrypted messages
38 * are implemented as an EnvelopedData containing a SignedData.
39 */
40
41 #ifndef _CMS_ENCODER_H_
42 #define _CMS_ENCODER_H_
43
44 #include <CoreFoundation/CoreFoundation.h>
45 #include <Security/cssmtype.h>
46 #include <stdint.h>
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 CF_ASSUME_NONNULL_BEGIN
53
54 /*
55 * Opaque reference to a CMS encoder object.
56 * This is a CF object, with standard CF semantics; dispose of it
57 * with CFRelease().
58 */
59 typedef struct CF_BRIDGED_TYPE(id) _CMSEncoder *CMSEncoderRef;
60
61 CFTypeID CMSEncoderGetTypeID(void)
62 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
63
64 /*
65 * Create a CMSEncoder. Result must eventually be freed via CFRelease().
66 */
67 OSStatus CMSEncoderCreate(
68 CMSEncoderRef * __nonnull CF_RETURNS_RETAINED cmsEncoderOut) /* RETURNED */
69 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
70
71 extern const CFStringRef kCMSEncoderDigestAlgorithmSHA1;
72 extern const CFStringRef kCMSEncoderDigestAlgorithmSHA256;
73
74 OSStatus CMSEncoderSetSignerAlgorithm(
75 CMSEncoderRef cmsEncoder,
76 CFStringRef digestAlgorithm)
77 __OSX_AVAILABLE_STARTING(__MAC_10_11, __IPHONE_NA);
78
79 /*
80 * Specify signers of the CMS message; implies that the message will be signed.
81 *
82 * -- Caller can pass in one signer, as a SecIdentityRef, or an array of
83 * signers, as a CFArray of SecIdentityRefs.
84 * -- Can be called multiple times.
85 * -- If the message is not to be signed, don't call this.
86 * -- If this is called, it must be called before the first call to
87 * CMSEncoderUpdateContent().
88 */
89 OSStatus CMSEncoderAddSigners(
90 CMSEncoderRef cmsEncoder,
91 CFTypeRef signerOrArray)
92 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
93
94 /*
95 * Obtain an array of signers as specified in CMSEncoderSetSigners().
96 * Returns a NULL signers array if CMSEncoderSetSigners() has not been called.
97 * Caller must CFRelease the result.
98 */
99 OSStatus CMSEncoderCopySigners(
100 CMSEncoderRef cmsEncoder,
101 CFArrayRef * __nonnull CF_RETURNS_RETAINED signersOut) /* RETURNED */
102 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
103
104 /*
105 * Specify recipients of the message. Implies that the message will
106 * be encrypted.
107 *
108 * -- Caller can pass in one recipient, as a SecCertificateRef, or an
109 * array of recipients, as a CFArray of SecCertificateRefs.
110 * -- Can be called multiple times.
111 * -- If the message is not to be encrypted, don't call this.
112 * -- If this is called, it must be called before the first call to
113 * CMSEncoderUpdateContent().
114 */
115 OSStatus CMSEncoderAddRecipients(
116 CMSEncoderRef cmsEncoder,
117 CFTypeRef recipientOrArray)
118 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
119
120 /*
121 * Obtain an array of recipients as specified in CMSEncoderSetRecipients().
122 * Returns a NULL recipients array if CMSEncoderSetRecipients() has not been
123 * called.
124 * Caller must CFRelease the result.
125 */
126 OSStatus CMSEncoderCopyRecipients(
127 CMSEncoderRef cmsEncoder,
128 CFArrayRef * __nonnull CF_RETURNS_RETAINED recipientsOut) /* RETURNED */
129 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
130
131 /*
132 * A signed message optionally includes the data to be signed. If the message
133 * is *not* to include the data to be signed, call this function with a value
134 * of TRUE for detachedContent. The default, if this function is not called,
135 * is detachedContent=FALSE, i.e., the message contains the data to be signed.
136 *
137 * -- Encrypted messages can not use detached content. (This restriction
138 * also applies to messages that are both signed and encrypted.)
139 * -- If this is called, it must be called before the first call to
140 * CMSEncoderUpdateContent().
141 */
142 OSStatus CMSEncoderSetHasDetachedContent(
143 CMSEncoderRef cmsEncoder,
144 Boolean detachedContent)
145 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
146
147 /*
148 * Obtain a Boolean indicating whether the current message will have detached
149 * content.
150 * Returns the value specified in CMSEncoderHasDetachedContent() if that
151 * function has been called; else returns the default FALSE.
152 */
153 OSStatus CMSEncoderGetHasDetachedContent(
154 CMSEncoderRef cmsEncoder,
155 Boolean *detachedContentOut) /* RETURNED */
156 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
157
158 /*
159 * Optionally specify an eContentType OID for the inner EncapsulatedData for
160 * a signed message. The default eContentType, used if this function is not
161 * called, is id-data (which is the normal eContentType for applications such
162 * as SMIME).
163 *
164 * If this is called, it must be called before the first call to
165 * CMSEncoderUpdateContent().
166 *
167 * NOTE: This function is deprecated in Mac OS X 10.7 and later;
168 * please use CMSEncoderSetEncapsulatedContentTypeOID() instead.
169 */
170 OSStatus CMSEncoderSetEncapsulatedContentType(
171 CMSEncoderRef cmsEncoder,
172 const CSSM_OID *eContentType)
173 /* DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER; */
174 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
175
176 /*
177 * Optionally specify an eContentType OID for the inner EncapsulatedData for
178 * a signed message. The default eContentTypeOID, used if this function is not
179 * called, is id-data (which is the normal eContentType for applications such
180 * as SMIME).
181 *
182 * The eContentTypeOID parameter may be specified as a CF string, e.g.:
183 * CFSTR("1.2.840.113549.1.7.1")
184 *
185 * If this is called, it must be called before the first call to
186 * CMSEncoderUpdateContent().
187 */
188 OSStatus CMSEncoderSetEncapsulatedContentTypeOID(
189 CMSEncoderRef cmsEncoder,
190 CFTypeRef eContentTypeOID)
191 __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA);
192
193 /*
194 * Obtain the eContentType OID specified in CMSEncoderSetEncapsulatedContentType().
195 * If CMSEncoderSetEncapsulatedContentType() has not been called this returns a
196 * NULL pointer.
197 * The returned OID's data is in the same format as the data provided to
198 * CMSEncoderSetEncapsulatedContentType;, i.e., it's the encoded content of
199 * the OID, not including the tag and length bytes.
200 */
201 OSStatus CMSEncoderCopyEncapsulatedContentType(
202 CMSEncoderRef cmsEncoder,
203 CFDataRef * __nonnull CF_RETURNS_RETAINED eContentTypeOut) /* RETURNED */
204 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
205
206 /*
207 * Signed CMS messages can contain arbitrary sets of certificates beyond those
208 * indicating the identity of the signer(s). This function provides a means of
209 * adding these other certs. For normal signed messages it is not necessary to
210 * call this; the signer cert(s) and the intermediate certs needed to verify the
211 * signer(s) will be included in the message implicitly.
212 *
213 * -- Caller can pass in one cert, as a SecCertificateRef, or an array of certs,
214 * as a CFArray of SecCertificateRefs.
215 * -- If this is called, it must be called before the first call to
216 * CMSEncoderUpdateContent().
217 * -- There is a "special case" use of CMS messages which involves neither
218 * signing nor encryption, but does include certificates. This is commonly
219 * used to transport "bags" of certificates. When constructing such a
220 * message, all an application needs to do is to create a CMSEncoderRef,
221 * call CMSEncoderAddSupportingCerts() one or more times, and then call
222 * CMSEncoderCopyEncodedContent() to get the resulting cert bag. No 'content'
223 * need be specified. (This is in fact the primary intended use for
224 * this function.)
225 */
226 OSStatus CMSEncoderAddSupportingCerts(
227 CMSEncoderRef cmsEncoder,
228 CFTypeRef certOrArray)
229 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
230
231 /*
232 * Obtain the SecCertificates provided in CMSEncoderAddSupportingCerts().
233 * If CMSEncoderAddSupportingCerts() has not been called this will return a
234 * NULL value for *certs.
235 * Caller must CFRelease the result.
236 */
237 OSStatus CMSEncoderCopySupportingCerts(
238 CMSEncoderRef cmsEncoder,
239 CFArrayRef * __nonnull CF_RETURNS_RETAINED certsOut) /* RETURNED */
240 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
241
242 /*
243 * Standard signed attributes, optionally specified in
244 * CMSEncoderAddSignedAttributes().
245 */
246 typedef CF_OPTIONS(uint32_t, CMSSignedAttributes) {
247 kCMSAttrNone = 0x0000,
248 /*
249 * S/MIME Capabilities - identifies supported signature, encryption, and
250 * digest algorithms.
251 */
252 kCMSAttrSmimeCapabilities = 0x0001,
253 /*
254 * Indicates that a cert is the preferred cert for S/MIME encryption.
255 */
256 kCMSAttrSmimeEncryptionKeyPrefs = 0x0002,
257 /*
258 * Same as kCMSSmimeEncryptionKeyPrefs, using an attribute OID preferred
259 * by Microsoft.
260 */
261 kCMSAttrSmimeMSEncryptionKeyPrefs = 0x0004,
262 /*
263 * Include the signing time.
264 */
265 kCMSAttrSigningTime = 0x0008,
266 /*
267 * Include the Apple Codesigning Hash Agility.
268 */
269 kCMSAttrAppleCodesigningHashAgility = 0x0010,
270 kCMSAttrAppleCodesigningHashAgilityV2 = 0x0020,
271 };
272
273 /*
274 * Optionally specify signed attributes. Only meaningful when creating a
275 * signed message. If this is called, it must be called before
276 * CMSEncoderUpdateContent().
277 */
278 OSStatus CMSEncoderAddSignedAttributes(
279 CMSEncoderRef cmsEncoder,
280 CMSSignedAttributes signedAttributes)
281 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
282
283 /*
284 * Specification of what certificates to include in a signed message.
285 */
286 typedef CF_ENUM(uint32_t, CMSCertificateChainMode) {
287 kCMSCertificateNone = 0, /* don't include any certificates */
288 kCMSCertificateSignerOnly, /* only include signer certificate(s) */
289 kCMSCertificateChain, /* signer certificate chain up to but not
290 * including root certiticate */
291 kCMSCertificateChainWithRoot /* signer certificate chain including root */
292 };
293
294 /*
295 * Optionally specify which certificates, if any, to include in a
296 * signed CMS message. The default, if this is not called, is
297 * kCMSCertificateChain, in which case the signer cert plus all CA
298 * certs needed to verify the signer cert, except for the root
299 * cert, are included.
300 * If this is called, it must be called before
301 * CMSEncoderUpdateContent().
302 */
303 OSStatus CMSEncoderSetCertificateChainMode(
304 CMSEncoderRef cmsEncoder,
305 CMSCertificateChainMode chainMode)
306 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
307
308 /*
309 * Obtain indication of which signer certs are to be included
310 * in a signed CMS message.
311 */
312 OSStatus CMSEncoderGetCertificateChainMode(
313 CMSEncoderRef cmsEncoder,
314 CMSCertificateChainMode *chainModeOut)
315 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
316
317 /*
318 * Feed content bytes into the encoder.
319 * Can be called multiple times.
320 * No 'setter' routines can be called after this function has been called.
321 */
322 OSStatus CMSEncoderUpdateContent(
323 CMSEncoderRef cmsEncoder,
324 const void *content,
325 size_t contentLen)
326 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
327
328 /*
329 * Finish encoding the message and obtain the encoded result.
330 * Caller must CFRelease the result.
331 */
332 OSStatus CMSEncoderCopyEncodedContent(
333 CMSEncoderRef cmsEncoder,
334 CFDataRef * __nonnull CF_RETURNS_RETAINED encodedContentOut) /* RETURNED */
335 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
336
337 /*
338 * High-level, one-shot encoder function.
339 *
340 * Inputs (all except for content optional, though at least one
341 * of {signers, recipients} must be non-NULL)
342 * ------------------------------------------------------------
343 * signers : signer identities. Either a SecIdentityRef, or a
344 * CFArray of them.
345 * recipients : recipient certificates. Either a SecCertificateRef,
346 * or a CFArray of them.
347 * eContentType : contentType for inner EncapsulatedData.
348 * detachedContent : when true, do not include the signed data in the message.
349 * signedAttributes : Specifies which standard signed attributes are to be
350 * included in the message.
351 * content : raw content to be signed and/or encrypted.
352 *
353 * Output
354 * ------
355 * encodedContent : the result of the encoding.
356 *
357 * NOTE: This function is deprecated in Mac OS X 10.7 and later;
358 * please use CMSEncodeContent() instead.
359 */
360 OSStatus CMSEncode(
361 CFTypeRef __nullable signers,
362 CFTypeRef __nullable recipients,
363 const CSSM_OID * __nullable eContentType,
364 Boolean detachedContent,
365 CMSSignedAttributes signedAttributes,
366 const void * content,
367 size_t contentLen,
368 CFDataRef * __nonnull CF_RETURNS_RETAINED encodedContentOut) /* RETURNED */
369 /* DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER; */
370 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);
371
372
373 /*
374 * High-level, one-shot encoder function.
375 *
376 * Inputs (all except for content optional, though at least one
377 * of {signers, recipients} must be non-NULL)
378 * ------------------------------------------------------------
379 * signers : signer identities. Either a SecIdentityRef, or a
380 * CFArray of them.
381 * recipients : recipient certificates. Either a SecCertificateRef,
382 * or a CFArray of them.
383 * eContentTypeOID : contentType OID for inner EncapsulatedData, e.g.:
384 * CFSTR("1.2.840.113549.1.7.1")
385 * detachedContent : when true, do not include the signed data in the message.
386 * signedAttributes : Specifies which standard signed attributes are to be
387 * included in the message.
388 * content : raw content to be signed and/or encrypted.
389 *
390 * Output
391 * ------
392 * encodedContent : the result of the encoding.
393 */
394 OSStatus CMSEncodeContent(
395 CFTypeRef __nullable signers,
396 CFTypeRef __nullable recipients,
397 CFTypeRef __nullable eContentTypeOID,
398 Boolean detachedContent,
399 CMSSignedAttributes signedAttributes,
400 const void *content,
401 size_t contentLen,
402 CFDataRef * __nullable CF_RETURNS_RETAINED encodedContentOut) /* RETURNED */
403 __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA);
404
405 OSStatus CMSEncoderCopySignerTimestamp(
406 CMSEncoderRef cmsEncoder,
407 size_t signerIndex, /* usually 0 */
408 CFAbsoluteTime *timestamp) /* RETURNED */
409 __OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_NA);
410
411 OSStatus CMSEncoderCopySignerTimestampWithPolicy(
412 CMSEncoderRef cmsEncoder,
413 CFTypeRef __nullable timeStampPolicy,
414 size_t signerIndex, /* usually 0 */
415 CFAbsoluteTime *timestamp) /* RETURNED */
416 __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_NA);
417
418 CF_ASSUME_NONNULL_END
419
420 #ifdef __cplusplus
421 }
422 #endif
423
424 #endif /* _CMS_ENCODER_H_ */
425