2 * Copyright (c) 2006-2013 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 * CMSEncoder.cpp - encode, sign, and/or encrypt CMS messages.
28 #include "CMSEncoder.h"
29 #include "CMSPrivate.h"
31 #include <Security/SecBase.h>
32 #include <Security/SecCmsEncoder.h>
33 #include <Security/SecCmsEnvelopedData.h>
34 #include <Security/SecCmsMessage.h>
35 #include <Security/SecCmsRecipientInfo.h>
36 #include <Security/SecCmsSignedData.h>
37 #include <Security/SecCmsSignerInfo.h>
38 #include <Security/SecCmsContentInfo.h>
39 #include <Security/SecCertificate.h>
40 #include <Security/SecIdentity.h>
41 #include <Security/SecKeychain.h>
42 #include <Security/SecKeychainItem.h>
43 #include <Security/SecSMIME.h>
44 #include <Security/oidsattr.h>
45 #include <Security/SecAsn1Coder.h>
46 #include <Security/SecAsn1Types.h>
47 #include <Security/SecAsn1Templates.h>
48 #include <CoreFoundation/CFRuntime.h>
50 #include <utilities/SecCFRelease.h>
52 #include <security_smime/tsaSupport.h>
53 #include <security_smime/cmspriv.h>
55 #pragma mark --- Private types and definitions ---
61 ES_Init
, /* between CMSEncoderCreate and earlier of CMSEncoderUpdateContent
62 * and CMSEncodeGetCmsMessage */
63 ES_Msg
, /* created cmsMsg in CMSEncodeGetCmsMessage, but no encoder yet */
64 ES_Updating
, /* between first CMSEncoderUpdateContent and CMSEncoderCopyEncodedContent */
65 ES_Final
/* CMSEncoderCopyEncodedContent has been called */
69 * High-level operation: what are we doing?
78 * Caller's CMSEncoderRef points to one of these.
82 CMSEncoderState encState
;
84 Boolean detachedContent
;
85 CSSM_OID eContentType
;
86 CFMutableArrayRef signers
;
87 CFMutableArrayRef recipients
;
88 CFMutableArrayRef otherCerts
;
89 CMSSignedAttributes signedAttributes
;
90 CFAbsoluteTime signingTime
;
91 SecCmsMessageRef cmsMsg
;
92 SecArenaPoolRef arena
; /* the encoder's arena */
93 SecCmsEncoderRef encoder
;
94 CSSM_DATA encoderOut
; /* output goes here... */
95 bool customCoder
; /* unless this is set by
96 * CMSEncoderSetEncoder */
97 SECOidTag digestalgtag
;
99 CMSCertificateChainMode chainMode
;
100 CFDataRef hashAgilityAttrValue
;
101 CFDictionaryRef hashAgilityV2AttrValues
;
102 CFAbsoluteTime expirationTime
;
105 static void cmsEncoderInit(CFTypeRef enc
);
106 static void cmsEncoderFinalize(CFTypeRef enc
);
108 static CFRuntimeClass cmsEncoderRuntimeClass
=
115 NULL
, /* equal - just use pointer equality */
116 NULL
, /* hash, ditto */
117 NULL
, /* copyFormattingDesc */
118 NULL
/* copyDebugDesc */
122 CmsMessageSetTSACallback(CMSEncoderRef cmsEncoder
, SecCmsTSACallback tsaCallback
);
124 #pragma mark --- Private routines ---
127 * Decode a CFStringRef representation of an integer
129 static int cfStringToNumber(
134 if (!inStr
|| !CFStringGetCString(inStr
, buf
, max
-1, kCFStringEncodingASCII
))
140 * Encode an integer component of an OID, return resulting number of bytes;
141 * actual bytes are mallocd and returned in *encodeArray.
143 static unsigned encodeNumber(
145 unsigned char **encodeArray
) // mallocd and RETURNED
147 unsigned char *result
;
149 unsigned numDigits
= 0;
152 /* trival case - 0 maps to 0 */
154 *encodeArray
= (unsigned char *)malloc(1);
159 /* first calculate the number of digits in num, base 128 */
160 scratch
= (unsigned)num
;
161 while(scratch
!= 0) {
166 result
= (unsigned char *)malloc(numDigits
);
167 scratch
= (unsigned)num
;
168 for(dex
=0; dex
<numDigits
; dex
++) {
169 result
[numDigits
- dex
- 1] = scratch
& 0x7f;
173 /* all digits except the last one have m.s. bit set */
174 for(dex
=0; dex
<(numDigits
- 1); dex
++) {
178 *encodeArray
= result
;
183 * Given an OID in dotted-decimal string representation, convert to binary
184 * DER format. Returns a pointer in outOid which the caller must free(),
185 * as well as the length of the data in outLen.
186 * Function returns 0 if successful, non-zero otherwise.
188 static int encodeOid(
189 const unsigned char *inStr
,
190 unsigned char **outOid
,
191 unsigned int *outLen
)
193 unsigned char **digits
= NULL
; /* array of char * from encodeNumber */
194 unsigned *numDigits
= NULL
; /* array of unsigned from encodeNumber */
196 unsigned numDigitBytes
; /* total #of output chars */
197 unsigned char firstByte
;
199 CFIndex numsToProcess
;
200 CFStringRef oidStr
= NULL
;
201 CFArrayRef argvRef
= NULL
;
205 /* parse input string into array of substrings */
206 if (!inStr
|| !outOid
|| !outLen
) goto cleanExit
;
207 oidStr
= CFStringCreateWithCString(NULL
, (const char *)inStr
, kCFStringEncodingASCII
);
208 if (!oidStr
) goto cleanExit
;
209 argvRef
= CFStringCreateArrayBySeparatingStrings(NULL
, oidStr
, CFSTR("."));
210 if (!argvRef
) goto cleanExit
;
211 argc
= CFArrayGetCount(argvRef
);
212 if (argc
< 3) goto cleanExit
;
214 /* first two numbers in OID munge together */
215 num
= cfStringToNumber((CFStringRef
)CFArrayGetValueAtIndex(argvRef
, 0));
216 if (num
< 0) goto cleanExit
;
217 firstByte
= (40 * num
);
218 num
= cfStringToNumber((CFStringRef
)CFArrayGetValueAtIndex(argvRef
, 1));
219 if (num
< 0) goto cleanExit
;
223 numsToProcess
= argc
- 2;
224 if(numsToProcess
> 0) {
225 /* skip this loop in the unlikely event that input is only two numbers */
226 digits
= (unsigned char **) malloc(numsToProcess
* sizeof(unsigned char *));
227 numDigits
= (unsigned *) malloc(numsToProcess
* sizeof(unsigned));
228 for(digit
=0; digit
<numsToProcess
; digit
++) {
229 num
= cfStringToNumber((CFStringRef
)CFArrayGetValueAtIndex(argvRef
, digit
+2));
230 if (num
< 0) goto cleanExit
;
231 numDigits
[digit
] = encodeNumber(num
, &digits
[digit
]);
232 numDigitBytes
+= numDigits
[digit
];
235 *outLen
= (2 + numDigitBytes
);
236 *outOid
= outP
= (unsigned char *) malloc(*outLen
);
238 *outP
++ = numDigitBytes
;
240 for(digit
=0; digit
<numsToProcess
; digit
++) {
241 unsigned int byteDex
;
242 for(byteDex
=0; byteDex
<numDigits
[digit
]; byteDex
++) {
243 *outP
++ = digits
[digit
][byteDex
];
247 for(digit
=0; digit
<numsToProcess
; digit
++) {
254 if (digits
) free(digits
);
255 if (numDigits
) free(numDigits
);
256 if (oidStr
) CFRelease(oidStr
);
257 if (argvRef
) CFRelease(argvRef
);
263 * Given a CF object reference describing an OID, convert to binary DER format
264 * and fill out the CSSM_OID structure provided by the caller. Caller is
265 * responsible for freeing the data pointer in outOid->Data.
267 * Function returns 0 if successful, non-zero otherwise.
270 static int convertOid(
274 if (!inRef
|| !outOid
)
277 unsigned char *oidData
= NULL
;
278 unsigned int oidLen
= 0;
280 if (CFGetTypeID(inRef
) == CFStringGetTypeID()) {
281 // CFStringRef: OID representation is a dotted-decimal string
282 CFStringRef inStr
= (CFStringRef
)inRef
;
283 CFIndex max
= CFStringGetLength(inStr
) * 3;
284 char *buf
= (char *)malloc(max
);
286 return errSecMemoryError
;
288 if (!CFStringGetCString(inStr
, buf
, max
-1, kCFStringEncodingASCII
)) {
293 if (encodeOid((unsigned char *)buf
, &oidData
, &oidLen
) != 0) {
299 else if (CFGetTypeID(inRef
) == CFDataGetTypeID()) {
300 // CFDataRef: OID representation is in binary DER format
301 CFDataRef inData
= (CFDataRef
)inRef
;
302 oidLen
= (unsigned int) CFDataGetLength(inData
);
303 oidData
= (unsigned char *) malloc(oidLen
);
304 memcpy(oidData
, CFDataGetBytePtr(inData
), oidLen
);
307 // Not in a format we understand
310 outOid
->Length
= oidLen
;
311 outOid
->Data
= (uint8
*)oidData
;
315 static CFTypeID cmsEncoderTypeID
= _kCFRuntimeNotATypeID
;
317 /* one time only class init, called via pthread_once() in CMSEncoderGetTypeID() */
318 static void cmsEncoderClassInitialize(void)
321 _CFRuntimeRegisterClass((const CFRuntimeClass
* const)&cmsEncoderRuntimeClass
);
324 /* init called out from _CFRuntimeCreateInstance() */
325 static void cmsEncoderInit(CFTypeRef enc
)
327 char *start
= ((char *)enc
) + sizeof(CFRuntimeBase
);
328 memset(start
, 0, sizeof(struct _CMSEncoder
) - sizeof(CFRuntimeBase
));
332 * Dispose of a CMSEncoder. Called out from CFRelease().
334 static void cmsEncoderFinalize(
337 CMSEncoderRef cmsEncoder
= (CMSEncoderRef
)enc
;
338 if(cmsEncoder
== NULL
) {
341 if(cmsEncoder
->eContentType
.Data
!= NULL
) {
342 free(cmsEncoder
->eContentType
.Data
);
344 CFRELEASE(cmsEncoder
->signers
);
345 CFRELEASE(cmsEncoder
->recipients
);
346 CFRELEASE(cmsEncoder
->otherCerts
);
347 if(cmsEncoder
->cmsMsg
!= NULL
) {
348 SecCmsMessageDestroy(cmsEncoder
->cmsMsg
);
349 cmsEncoder
->cmsMsg
= NULL
;
351 if(cmsEncoder
->arena
!= NULL
) {
352 SecArenaPoolFree(cmsEncoder
->arena
, false);
354 if(cmsEncoder
->encoder
!= NULL
) {
356 * Normally this gets freed in SecCmsEncoderFinish - this is
359 SecCmsEncoderDestroy(cmsEncoder
->encoder
);
363 static OSStatus
cmsSetupEncoder(
364 CMSEncoderRef cmsEncoder
)
368 ASSERT(cmsEncoder
->arena
== NULL
);
369 ASSERT(cmsEncoder
->encoder
== NULL
);
371 ortn
= SecArenaPoolCreate(1024, &cmsEncoder
->arena
);
373 return cmsRtnToOSStatus(ortn
);
375 ortn
= SecCmsEncoderCreate(cmsEncoder
->cmsMsg
,
376 NULL
, NULL
, // no callback
377 &cmsEncoder
->encoderOut
, // data goes here
379 NULL
, NULL
, // no password callback (right?)
380 NULL
, NULL
, // decrypt key callback
381 NULL
, NULL
, // detached digests
382 &cmsEncoder
->encoder
);
384 return cmsRtnToOSStatus(ortn
);
386 return errSecSuccess
;
390 * Set up a SecCmsMessageRef for a SignedData creation.
392 static OSStatus
cmsSetupForSignedData(
393 CMSEncoderRef cmsEncoder
)
395 ASSERT((cmsEncoder
->signers
!= NULL
) || (cmsEncoder
->otherCerts
!= NULL
));
397 SecCmsContentInfoRef contentInfo
= NULL
;
398 SecCmsSignedDataRef signedData
= NULL
;
401 /* build chain of objects: message->signedData->data */
402 if(cmsEncoder
->cmsMsg
!= NULL
) {
403 SecCmsMessageDestroy(cmsEncoder
->cmsMsg
);
405 cmsEncoder
->cmsMsg
= SecCmsMessageCreate(NULL
);
406 if(cmsEncoder
->cmsMsg
== NULL
) {
407 return errSecInternalComponent
;
410 signedData
= SecCmsSignedDataCreate(cmsEncoder
->cmsMsg
);
411 if(signedData
== NULL
) {
412 return errSecInternalComponent
;
414 contentInfo
= SecCmsMessageGetContentInfo(cmsEncoder
->cmsMsg
);
415 ortn
= SecCmsContentInfoSetContentSignedData(cmsEncoder
->cmsMsg
, contentInfo
,
418 return cmsRtnToOSStatus(ortn
);
420 contentInfo
= SecCmsSignedDataGetContentInfo(signedData
);
421 if(cmsEncoder
->eContentType
.Data
!= NULL
) {
422 /* Override the default eContentType of id-data */
423 ortn
= SecCmsContentInfoSetContentOther(cmsEncoder
->cmsMsg
,
425 NULL
, /* data - provided to encoder, not here */
426 cmsEncoder
->detachedContent
,
427 &cmsEncoder
->eContentType
);
430 ortn
= SecCmsContentInfoSetContentData(cmsEncoder
->cmsMsg
,
432 NULL
, /* data - provided to encoder, not here */
433 cmsEncoder
->detachedContent
);
436 ortn
= cmsRtnToOSStatus(ortn
);
437 CSSM_PERROR("SecCmsContentInfoSetContent*", ortn
);
441 /* optional 'global' (per-SignedData) certs */
442 if(cmsEncoder
->otherCerts
!= NULL
) {
443 ortn
= SecCmsSignedDataAddCertList(signedData
, cmsEncoder
->otherCerts
);
445 ortn
= cmsRtnToOSStatus(ortn
);
446 CSSM_PERROR("SecCmsSignedDataAddCertList", ortn
);
451 /* SignerInfos, one per signer */
452 CFIndex numSigners
= 0;
453 if(cmsEncoder
->signers
!= NULL
) {
454 /* this is optional...in case we're just creating a cert bundle */
455 numSigners
= CFArrayGetCount(cmsEncoder
->signers
);
458 SecCertificateRef ourCert
= NULL
;
459 SecCmsCertChainMode chainMode
= SecCmsCMCertChain
;
461 switch(cmsEncoder
->chainMode
) {
462 case kCMSCertificateNone
:
463 chainMode
= SecCmsCMNone
;
465 case kCMSCertificateSignerOnly
:
466 chainMode
= SecCmsCMCertOnly
;
468 case kCMSCertificateChainWithRoot
:
469 chainMode
= SecCmsCMCertChainWithRoot
;
474 for(dex
=0; dex
<numSigners
; dex
++) {
475 SecCmsSignerInfoRef signerInfo
;
477 SecIdentityRef ourId
=
478 (SecIdentityRef
)CFArrayGetValueAtIndex(cmsEncoder
->signers
, dex
);
479 ortn
= SecIdentityCopyCertificate(ourId
, &ourCert
);
481 CSSM_PERROR("SecIdentityCopyCertificate", ortn
);
484 signerInfo
= SecCmsSignerInfoCreate(cmsEncoder
->cmsMsg
, ourId
, cmsEncoder
->digestalgtag
);
485 if (signerInfo
== NULL
) {
486 ortn
= errSecInternalComponent
;
490 /* we want the cert chain included for this one */
491 /* NOTE the usage parameter is currently unused by the SMIME lib */
492 ortn
= SecCmsSignerInfoIncludeCerts(signerInfo
, chainMode
,
493 certUsageEmailSigner
);
495 ortn
= cmsRtnToOSStatus(ortn
);
496 CSSM_PERROR("SecCmsSignerInfoIncludeCerts", ortn
);
501 if(cmsEncoder
->signedAttributes
& kCMSAttrSmimeCapabilities
) {
502 ortn
= SecCmsSignerInfoAddSMIMECaps(signerInfo
);
504 ortn
= cmsRtnToOSStatus(ortn
);
505 CSSM_PERROR("SecCmsSignerInfoAddSMIMEEncKeyPrefs", ortn
);
509 if(cmsEncoder
->signedAttributes
& kCMSAttrSmimeEncryptionKeyPrefs
) {
510 ortn
= SecCmsSignerInfoAddSMIMEEncKeyPrefs(signerInfo
, ourCert
, NULL
);
512 ortn
= cmsRtnToOSStatus(ortn
);
513 CSSM_PERROR("SecCmsSignerInfoAddSMIMEEncKeyPrefs", ortn
);
517 if(cmsEncoder
->signedAttributes
& kCMSAttrSmimeMSEncryptionKeyPrefs
) {
518 ortn
= SecCmsSignerInfoAddMSSMIMEEncKeyPrefs(signerInfo
, ourCert
, NULL
);
520 ortn
= cmsRtnToOSStatus(ortn
);
521 CSSM_PERROR("SecCmsSignerInfoAddMSSMIMEEncKeyPrefs", ortn
);
525 if(cmsEncoder
->signedAttributes
& kCMSAttrSigningTime
) {
526 if (cmsEncoder
->signingTime
== 0)
527 cmsEncoder
->signingTime
= CFAbsoluteTimeGetCurrent();
528 ortn
= SecCmsSignerInfoAddSigningTime(signerInfo
, cmsEncoder
->signingTime
);
530 ortn
= cmsRtnToOSStatus(ortn
);
531 CSSM_PERROR("SecCmsSignerInfoAddSigningTime", ortn
);
535 if(cmsEncoder
->signedAttributes
& kCMSAttrAppleCodesigningHashAgility
) {
536 ortn
= SecCmsSignerInfoAddAppleCodesigningHashAgility(signerInfo
, cmsEncoder
->hashAgilityAttrValue
);
537 /* libsecurity_smime made a copy of the attribute value. We don't need it anymore. */
538 CFReleaseNull(cmsEncoder
->hashAgilityAttrValue
);
540 ortn
= cmsRtnToOSStatus(ortn
);
541 CSSM_PERROR("SecCmsSignerInfoAddAppleCodesigningHashAgility", ortn
);
545 if(cmsEncoder
->signedAttributes
& kCMSAttrAppleCodesigningHashAgilityV2
) {
546 ortn
= SecCmsSignerInfoAddAppleCodesigningHashAgilityV2(signerInfo
, cmsEncoder
->hashAgilityV2AttrValues
);
547 /* libsecurity_smime made a copy of the attribute value. We don't need it anymore. */
548 CFReleaseNull(cmsEncoder
->hashAgilityV2AttrValues
);
550 ortn
= cmsRtnToOSStatus(ortn
);
551 CSSM_PERROR("SecCmsSignerInfoAddAppleCodesigningHashAgilityV2", ortn
);
555 if (cmsEncoder
->signedAttributes
& kCMSAttrAppleExpirationTime
) {
556 ortn
= SecCmsSignerInfoAddAppleExpirationTime(signerInfo
, cmsEncoder
->expirationTime
);
558 ortn
= cmsRtnToOSStatus(ortn
);
559 CSSM_PERROR("SecCmsSignerInfoAddAppleExpirationTime", ortn
);
564 ortn
= SecCmsSignedDataAddSignerInfo(signedData
, signerInfo
);
566 ortn
= cmsRtnToOSStatus(ortn
);
567 CSSM_PERROR("SecCmsSignedDataAddSignerInfo", ortn
);
581 * Set up a SecCmsMessageRef for a EnvelopedData creation.
583 static OSStatus
cmsSetupForEnvelopedData(
584 CMSEncoderRef cmsEncoder
)
586 ASSERT(cmsEncoder
->op
== EO_Encrypt
);
587 ASSERT(cmsEncoder
->recipients
!= NULL
);
589 SecCmsContentInfoRef contentInfo
= NULL
;
590 SecCmsEnvelopedDataRef envelopedData
= NULL
;
591 SECOidTag algorithmTag
;
596 * Find encryption algorithm...unfortunately we need a NULL-terminated array
597 * of SecCertificateRefs for this.
599 CFIndex numCerts
= CFArrayGetCount(cmsEncoder
->recipients
);
601 SecCertificateRef
*certArray
= (SecCertificateRef
*)malloc(
602 (numCerts
+1) * sizeof(SecCertificateRef
));
604 for(dex
=0; dex
<numCerts
; dex
++) {
605 certArray
[dex
] = (SecCertificateRef
)CFArrayGetValueAtIndex(
606 cmsEncoder
->recipients
, dex
);
608 certArray
[numCerts
] = NULL
;
609 ortn
= SecSMIMEFindBulkAlgForRecipients(certArray
, &algorithmTag
, &keySize
);
612 CSSM_PERROR("SecSMIMEFindBulkAlgForRecipients", ortn
);
616 /* build chain of objects: message->envelopedData->data */
617 if(cmsEncoder
->cmsMsg
!= NULL
) {
618 SecCmsMessageDestroy(cmsEncoder
->cmsMsg
);
620 cmsEncoder
->cmsMsg
= SecCmsMessageCreate(NULL
);
621 if(cmsEncoder
->cmsMsg
== NULL
) {
622 return errSecInternalComponent
;
624 envelopedData
= SecCmsEnvelopedDataCreate(cmsEncoder
->cmsMsg
,
625 algorithmTag
, keySize
);
626 if(envelopedData
== NULL
) {
627 return errSecInternalComponent
;
629 contentInfo
= SecCmsMessageGetContentInfo(cmsEncoder
->cmsMsg
);
630 ortn
= SecCmsContentInfoSetContentEnvelopedData(cmsEncoder
->cmsMsg
,
631 contentInfo
, envelopedData
);
633 ortn
= cmsRtnToOSStatus(ortn
);
634 CSSM_PERROR("SecCmsContentInfoSetContentEnvelopedData", ortn
);
637 contentInfo
= SecCmsEnvelopedDataGetContentInfo(envelopedData
);
638 if(cmsEncoder
->eContentType
.Data
!= NULL
) {
639 /* Override the default ContentType of id-data */
640 ortn
= SecCmsContentInfoSetContentOther(cmsEncoder
->cmsMsg
,
642 NULL
, /* data - provided to encoder, not here */
643 FALSE
, /* detachedContent */
644 &cmsEncoder
->eContentType
);
647 ortn
= SecCmsContentInfoSetContentData(cmsEncoder
->cmsMsg
,
649 NULL
/* data - provided to encoder, not here */,
650 cmsEncoder
->detachedContent
);
653 ortn
= cmsRtnToOSStatus(ortn
);
654 CSSM_PERROR("SecCmsContentInfoSetContentData*", ortn
);
659 * create & attach recipient information, one for each recipient
661 for(dex
=0; dex
<numCerts
; dex
++) {
662 SecCmsRecipientInfoRef recipientInfo
= NULL
;
664 SecCertificateRef thisRecip
= (SecCertificateRef
)CFArrayGetValueAtIndex(
665 cmsEncoder
->recipients
, dex
);
666 recipientInfo
= SecCmsRecipientInfoCreate(cmsEncoder
->cmsMsg
, thisRecip
);
667 ortn
= SecCmsEnvelopedDataAddRecipient(envelopedData
, recipientInfo
);
669 ortn
= cmsRtnToOSStatus(ortn
);
670 CSSM_PERROR("SecCmsEnvelopedDataAddRecipient", ortn
);
674 return errSecSuccess
;
678 * Set up cmsMsg. Called from either the first call to CMSEncoderUpdateContent, or
679 * from CMSEncodeGetCmsMessage().
681 static OSStatus
cmsSetupCmsMsg(
682 CMSEncoderRef cmsEncoder
)
684 ASSERT(cmsEncoder
!= NULL
);
685 ASSERT(cmsEncoder
->encState
== ES_Init
);
687 /* figure out what high-level operation we're doing */
688 if((cmsEncoder
->signers
!= NULL
) || (cmsEncoder
->otherCerts
!= NULL
)) {
689 if(cmsEncoder
->recipients
!= NULL
) {
690 cmsEncoder
->op
= EO_SignEncrypt
;
693 cmsEncoder
->op
= EO_Sign
;
696 else if(cmsEncoder
->recipients
!= NULL
) {
697 cmsEncoder
->op
= EO_Encrypt
;
700 dprintf("CMSEncoderUpdateContent: nothing to do\n");
704 OSStatus ortn
= errSecSuccess
;
706 switch(cmsEncoder
->op
) {
709 /* If we're signing & encrypting, do the signing first */
710 ortn
= cmsSetupForSignedData(cmsEncoder
);
713 ortn
= cmsSetupForEnvelopedData(cmsEncoder
);
716 cmsEncoder
->encState
= ES_Msg
;
721 * ASN.1 template for decoding a ContentInfo.
724 CSSM_OID contentType
;
728 static const SecAsn1Template cmsSimpleContentInfoTemplate
[] = {
729 { SEC_ASN1_SEQUENCE
, 0, NULL
, sizeof(SimpleContentInfo
) },
730 { SEC_ASN1_OBJECT_ID
, offsetof(SimpleContentInfo
, contentType
) },
731 { SEC_ASN1_EXPLICIT
| SEC_ASN1_CONSTRUCTED
| SEC_ASN1_CONTEXT_SPECIFIC
| 0,
732 offsetof(SimpleContentInfo
, content
),
733 kSecAsn1AnyTemplate
},
738 * Obtain the content of a contentInfo, This basically strips off the contentType OID
739 * and returns its ASN_ANY content, allocated the provided coder's memory space.
741 static OSStatus
cmsContentInfoContent(
742 SecAsn1CoderRef asn1Coder
,
743 const CSSM_DATA
*contentInfo
,
744 CSSM_DATA
*content
) /* RETURNED */
747 SimpleContentInfo decodedInfo
;
749 memset(&decodedInfo
, 0, sizeof(decodedInfo
));
750 ortn
= SecAsn1DecodeData(asn1Coder
, contentInfo
,
751 cmsSimpleContentInfoTemplate
, &decodedInfo
);
755 if(decodedInfo
.content
.Data
== NULL
) {
756 dprintf("***Error decoding contentInfo: no content\n");
757 return errSecInternalComponent
;
759 *content
= decodedInfo
.content
;
760 return errSecSuccess
;
763 #pragma mark --- Start of Public API ---
765 CFTypeID
CMSEncoderGetTypeID(void)
767 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
769 if(cmsEncoderTypeID
== _kCFRuntimeNotATypeID
) {
770 pthread_once(&once
, &cmsEncoderClassInitialize
);
772 return cmsEncoderTypeID
;
776 * Create a CMSEncoder. Result must eventually be freed via CFRelease().
778 OSStatus
CMSEncoderCreate(
779 CMSEncoderRef
*cmsEncoderOut
) /* RETURNED */
781 CMSEncoderRef cmsEncoder
= NULL
;
783 uint32_t extra
= sizeof(*cmsEncoder
) - sizeof(cmsEncoder
->base
);
784 cmsEncoder
= (CMSEncoderRef
)_CFRuntimeCreateInstance(NULL
, CMSEncoderGetTypeID(),
786 if(cmsEncoder
== NULL
) {
787 return errSecAllocate
;
789 cmsEncoder
->encState
= ES_Init
;
790 cmsEncoder
->chainMode
= kCMSCertificateChain
;
791 cmsEncoder
->digestalgtag
= SEC_OID_SHA1
;
792 *cmsEncoderOut
= cmsEncoder
;
793 return errSecSuccess
;
796 #pragma mark --- Getters & Setters ---
798 const CFStringRef __nonnull kCMSEncoderDigestAlgorithmSHA1
= CFSTR("sha1");
799 const CFStringRef __nonnull kCMSEncoderDigestAlgorithmSHA256
= CFSTR("sha256");
802 OSStatus
CMSEncoderSetSignerAlgorithm(
803 CMSEncoderRef cmsEncoder
,
804 CFStringRef digestAlgorithm
)
806 if (CFEqual(digestAlgorithm
, kCMSEncoderDigestAlgorithmSHA1
)) {
807 cmsEncoder
->digestalgtag
= SEC_OID_SHA1
;
808 } else if (CFEqual(digestAlgorithm
, kCMSEncoderDigestAlgorithmSHA256
)) {
809 cmsEncoder
->digestalgtag
= SEC_OID_SHA256
;
814 return errSecSuccess
;
818 * Specify signers of the CMS message; implies that the message will be signed.
820 OSStatus
CMSEncoderAddSigners(
821 CMSEncoderRef cmsEncoder
,
822 CFTypeRef signerOrArray
)
824 if(cmsEncoder
== NULL
) {
827 if(cmsEncoder
->encState
!= ES_Init
) {
830 return cmsAppendToArray(signerOrArray
, &cmsEncoder
->signers
, SecIdentityGetTypeID());
834 * Obtain an array of signers as specified in CMSEncoderSetSigners().
836 OSStatus
CMSEncoderCopySigners(
837 CMSEncoderRef cmsEncoder
,
840 if((cmsEncoder
== NULL
) || (signers
== NULL
)) {
843 if(cmsEncoder
->signers
!= NULL
) {
844 CFRetain(cmsEncoder
->signers
);
846 *signers
= cmsEncoder
->signers
;
847 return errSecSuccess
;
851 * Specify recipients of the message. Implies that the message will be encrypted.
853 OSStatus
CMSEncoderAddRecipients(
854 CMSEncoderRef cmsEncoder
,
855 CFTypeRef recipientOrArray
)
857 if(cmsEncoder
== NULL
) {
860 if(cmsEncoder
->encState
!= ES_Init
) {
863 return cmsAppendToArray(recipientOrArray
, &cmsEncoder
->recipients
,
864 SecCertificateGetTypeID());
868 * Obtain an array of recipients as specified in CMSEncoderSetRecipients().
870 OSStatus
CMSEncoderCopyRecipients(
871 CMSEncoderRef cmsEncoder
,
872 CFArrayRef
*recipients
)
874 if((cmsEncoder
== NULL
) || (recipients
== NULL
)) {
877 if(cmsEncoder
->recipients
!= NULL
) {
878 CFRetain(cmsEncoder
->recipients
);
880 *recipients
= cmsEncoder
->recipients
;
881 return errSecSuccess
;
885 * Specify additional certs to include in a signed message.
887 OSStatus
CMSEncoderAddSupportingCerts(
888 CMSEncoderRef cmsEncoder
,
889 CFTypeRef certOrArray
)
891 if(cmsEncoder
== NULL
) {
894 if(cmsEncoder
->encState
!= ES_Init
) {
897 return cmsAppendToArray(certOrArray
, &cmsEncoder
->otherCerts
,
898 SecCertificateGetTypeID());
902 * Obtain the SecCertificates provided in CMSEncoderAddSupportingCerts().
904 OSStatus
CMSEncoderCopySupportingCerts(
905 CMSEncoderRef cmsEncoder
,
906 CFArrayRef
*certs
) /* RETURNED */
908 if((cmsEncoder
== NULL
) || (certs
== NULL
)) {
911 if(cmsEncoder
->otherCerts
!= NULL
) {
912 CFRetain(cmsEncoder
->otherCerts
);
914 *certs
= cmsEncoder
->otherCerts
;
915 return errSecSuccess
;
918 OSStatus
CMSEncoderSetHasDetachedContent(
919 CMSEncoderRef cmsEncoder
,
920 Boolean detachedContent
)
922 if(cmsEncoder
== NULL
) {
925 if(cmsEncoder
->encState
!= ES_Init
) {
928 cmsEncoder
->detachedContent
= detachedContent
;
929 return errSecSuccess
;
932 OSStatus
CMSEncoderGetHasDetachedContent(
933 CMSEncoderRef cmsEncoder
,
934 Boolean
*detachedContent
) /* RETURNED */
936 if((cmsEncoder
== NULL
) || (detachedContent
== NULL
)) {
939 *detachedContent
= cmsEncoder
->detachedContent
;
940 return errSecSuccess
;
944 * Optionally specify an eContentType OID for the inner EncapsulatedData for
945 * a signed message. The default eContentType, used of this function is not
946 * called, is id-data.
948 OSStatus
CMSEncoderSetEncapsulatedContentType(
949 CMSEncoderRef cmsEncoder
,
950 const CSSM_OID
*eContentType
)
952 if((cmsEncoder
== NULL
) || (eContentType
== NULL
)) {
955 if(cmsEncoder
->encState
!= ES_Init
) {
959 CSSM_OID
*ecOid
= &cmsEncoder
->eContentType
;
960 if(ecOid
->Data
!= NULL
) {
963 cmsCopyCmsData(eContentType
, ecOid
);
964 return errSecSuccess
;
967 OSStatus
CMSEncoderSetEncapsulatedContentTypeOID(
968 CMSEncoderRef cmsEncoder
,
969 CFTypeRef eContentTypeOID
)
971 // convert eContentTypeOID to a CSSM_OID
972 CSSM_OID contentType
= { 0, NULL
};
973 if (!eContentTypeOID
|| convertOid(eContentTypeOID
, &contentType
) != 0)
975 OSStatus result
= CMSEncoderSetEncapsulatedContentType(cmsEncoder
, &contentType
);
976 if (contentType
.Data
)
977 free(contentType
.Data
);
982 * Obtain the eContentType OID specified in CMSEncoderSetEncapsulatedContentType().
984 OSStatus
CMSEncoderCopyEncapsulatedContentType(
985 CMSEncoderRef cmsEncoder
,
986 CFDataRef
*eContentType
)
988 if((cmsEncoder
== NULL
) || (eContentType
== NULL
)) {
992 CSSM_OID
*ecOid
= &cmsEncoder
->eContentType
;
993 if(ecOid
->Data
== NULL
) {
994 *eContentType
= NULL
;
997 *eContentType
= CFDataCreate(NULL
, ecOid
->Data
, ecOid
->Length
);
999 return errSecSuccess
;
1003 * Optionally specify signed attributes. Only meaningful when creating a
1004 * signed message. If this is called, it must be called before
1005 * CMSEncoderUpdateContent().
1007 OSStatus
CMSEncoderAddSignedAttributes(
1008 CMSEncoderRef cmsEncoder
,
1009 CMSSignedAttributes signedAttributes
)
1011 if(cmsEncoder
== NULL
) {
1014 if(cmsEncoder
->encState
!= ES_Init
) {
1017 cmsEncoder
->signedAttributes
|= signedAttributes
;
1018 return errSecSuccess
;
1022 * Set the signing time for a CMSEncoder.
1023 * This is only used if the kCMSAttrSigningTime attribute is included.
1025 OSStatus
CMSEncoderSetSigningTime(
1026 CMSEncoderRef cmsEncoder
,
1027 CFAbsoluteTime time
)
1029 if(cmsEncoder
== NULL
) {
1032 if(cmsEncoder
->encState
!= ES_Init
) {
1035 cmsEncoder
->signingTime
= time
;
1036 return errSecSuccess
;
1040 * Set the hash agility attribute for a CMSEncoder.
1041 * This is only used if the kCMSAttrAppleCodesigningHashAgility attribute
1044 OSStatus
CMSEncoderSetAppleCodesigningHashAgility(
1045 CMSEncoderRef cmsEncoder
,
1046 CFDataRef hashAgilityAttrValue
)
1048 if (cmsEncoder
== NULL
|| cmsEncoder
->encState
!= ES_Init
) {
1051 cmsEncoder
->hashAgilityAttrValue
= CFRetainSafe(hashAgilityAttrValue
);
1052 return errSecSuccess
;
1056 * Set the hash agility attribute for a CMSEncoder.
1057 * This is only used if the kCMSAttrAppleCodesigningHashAgilityV2 attribute
1060 OSStatus
CMSEncoderSetAppleCodesigningHashAgilityV2(
1061 CMSEncoderRef cmsEncoder
,
1062 CFDictionaryRef hashAgilityV2AttrValues
)
1064 if (cmsEncoder
== NULL
|| cmsEncoder
->encState
!= ES_Init
) {
1067 cmsEncoder
->hashAgilityV2AttrValues
= CFRetainSafe(hashAgilityV2AttrValues
);
1068 return errSecSuccess
;
1072 * Set the expiration time for a CMSEncoder.
1073 * This is only used if the kCMSAttrAppleExpirationTime attribute is included.
1075 OSStatus
CMSEncoderSetAppleExpirationTime(
1076 CMSEncoderRef cmsEncoder
,
1077 CFAbsoluteTime time
)
1079 if(cmsEncoder
== NULL
) {
1082 if(cmsEncoder
->encState
!= ES_Init
) {
1085 cmsEncoder
->expirationTime
= time
;
1086 return errSecSuccess
;
1089 OSStatus
CMSEncoderSetCertificateChainMode(
1090 CMSEncoderRef cmsEncoder
,
1091 CMSCertificateChainMode chainMode
)
1093 if(cmsEncoder
== NULL
) {
1096 if(cmsEncoder
->encState
!= ES_Init
) {
1100 case kCMSCertificateNone
:
1101 case kCMSCertificateSignerOnly
:
1102 case kCMSCertificateChain
:
1103 case kCMSCertificateChainWithRoot
:
1108 cmsEncoder
->chainMode
= chainMode
;
1109 return errSecSuccess
;
1112 OSStatus
CMSEncoderGetCertificateChainMode(
1113 CMSEncoderRef cmsEncoder
,
1114 CMSCertificateChainMode
*chainModeOut
)
1116 if(cmsEncoder
== NULL
) {
1119 *chainModeOut
= cmsEncoder
->chainMode
;
1120 return errSecSuccess
;
1124 CmsMessageSetTSACallback(CMSEncoderRef cmsEncoder
, SecCmsTSACallback tsaCallback
)
1126 if (cmsEncoder
->cmsMsg
)
1127 SecCmsMessageSetTSACallback(cmsEncoder
->cmsMsg
, tsaCallback
);
1131 CmsMessageSetTSAContext(CMSEncoderRef cmsEncoder
, CFTypeRef tsaContext
)
1133 if (cmsEncoder
->cmsMsg
)
1134 SecCmsMessageSetTSAContext(cmsEncoder
->cmsMsg
, tsaContext
);
1137 #pragma mark --- Action ---
1140 * Feed content bytes into the encoder.
1141 * Can be called multiple times.
1142 * No 'setter' routines can be called after this function has been called.
1144 OSStatus
CMSEncoderUpdateContent(
1145 CMSEncoderRef cmsEncoder
,
1146 const void *content
,
1149 if(cmsEncoder
== NULL
) {
1153 OSStatus ortn
= errSecSuccess
;
1154 switch(cmsEncoder
->encState
) {
1157 * First time thru: do the CmsMsg setup.
1159 ortn
= cmsSetupCmsMsg(cmsEncoder
);
1163 /* fall thru to set up the encoder */
1166 /* We have a cmsMsg but no encoder; create one */
1167 ASSERT(cmsEncoder
->cmsMsg
!= NULL
);
1168 ASSERT(cmsEncoder
->encoder
== NULL
);
1169 ortn
= cmsSetupEncoder(cmsEncoder
);
1173 /* only legal calls now are update and finalize */
1174 cmsEncoder
->encState
= ES_Updating
;
1178 ASSERT(cmsEncoder
->encoder
!= NULL
);
1182 /* Too late for another update */
1186 return errSecInternalComponent
;
1189 /* FIXME - CFIndex same size as size_t on 64bit? */
1190 ortn
= SecCmsEncoderUpdate(cmsEncoder
->encoder
, content
, (CFIndex
)contentLen
);
1192 ortn
= cmsRtnToOSStatus(ortn
);
1193 CSSM_PERROR("SecCmsEncoderUpdate", ortn
);
1199 * Finish encoding the message and obtain the encoded result.
1200 * Caller must CFRelease the result.
1202 OSStatus
CMSEncoderCopyEncodedContent(
1203 CMSEncoderRef cmsEncoder
,
1204 CFDataRef
*encodedContent
)
1206 if((cmsEncoder
== NULL
) || (encodedContent
== NULL
)) {
1212 switch(cmsEncoder
->encState
) {
1214 /* normal termination */
1217 /* already been called */
1222 * The only time these are legal is when we're doing a SignedData
1223 * with certificates only (no signers, no content).
1225 if((cmsEncoder
->signers
!= NULL
) ||
1226 (cmsEncoder
->recipients
!= NULL
) ||
1227 (cmsEncoder
->otherCerts
== NULL
)) {
1231 /* Set up for certs only */
1232 ortn
= cmsSetupForSignedData(cmsEncoder
);
1236 /* and an encoder */
1237 ortn
= cmsSetupEncoder(cmsEncoder
);
1245 ASSERT(cmsEncoder
->encoder
!= NULL
);
1246 ortn
= SecCmsEncoderFinish(cmsEncoder
->encoder
);
1247 /* regardless of the outcome, the encoder itself has been freed */
1248 cmsEncoder
->encoder
= NULL
;
1250 return cmsRtnToOSStatus(ortn
);
1252 cmsEncoder
->encState
= ES_Final
;
1254 if((cmsEncoder
->encoderOut
.Data
== NULL
) && !cmsEncoder
->customCoder
) {
1255 /* not sure how this could happen... */
1256 dprintf("Successful encode, but no data\n");
1257 return errSecInternalComponent
;
1259 if(cmsEncoder
->customCoder
) {
1261 *encodedContent
= NULL
;
1262 return errSecSuccess
;
1265 /* in two out of three cases, we're done */
1266 switch(cmsEncoder
->op
) {
1269 *encodedContent
= CFDataCreate(NULL
, (const UInt8
*)cmsEncoder
->encoderOut
.Data
,
1270 cmsEncoder
->encoderOut
.Length
);
1271 return errSecSuccess
;
1272 case EO_SignEncrypt
:
1273 /* proceed, more work to do */
1278 * Signing & encrypting.
1279 * Due to bugs in the libsecurity_smime encoder, it can't encode nested
1280 * ContentInfos in one shot. So we do another pass, specifying the SignedData
1281 * inside of the ContentInfo we just created as the data to encrypt.
1283 SecAsn1CoderRef asn1Coder
= NULL
;
1284 CSSM_DATA signedData
= {0, NULL
};
1286 ortn
= SecAsn1CoderCreate(&asn1Coder
);
1290 ortn
= cmsContentInfoContent(asn1Coder
, &cmsEncoder
->encoderOut
, &signedData
);
1295 /* now just encrypt that, one-shot */
1296 ortn
= CMSEncode(NULL
, /* no signers this time */
1297 cmsEncoder
->recipients
,
1298 &CSSMOID_PKCS7_SignedData
, /* fake out encoder so it doesn't try to actually
1299 * encode the signedData - this asserts the
1300 * SEC_OID_OTHER OID tag in the EnvelopedData's
1302 FALSE
, /* detachedContent */
1303 kCMSAttrNone
, /* signedAttributes - none this time */
1304 signedData
.Data
, signedData
.Length
,
1309 SecAsn1CoderRelease(asn1Coder
);
1314 #pragma mark --- High-level API ---
1317 * High-level, one-shot encoder function.
1321 CFTypeRef recipients
,
1322 const CSSM_OID
*eContentType
,
1323 Boolean detachedContent
,
1324 CMSSignedAttributes signedAttributes
,
1325 const void *content
,
1327 CFDataRef
*encodedContent
) /* RETURNED */
1329 if((signers
== NULL
) && (recipients
== NULL
)) {
1332 if(encodedContent
== NULL
) {
1336 CMSEncoderRef cmsEncoder
;
1339 /* set up the encoder */
1340 ortn
= CMSEncoderCreate(&cmsEncoder
);
1345 /* subsequent errors to errOut: */
1347 ortn
= CMSEncoderAddSigners(cmsEncoder
, signers
);
1353 ortn
= CMSEncoderAddRecipients(cmsEncoder
, recipients
);
1359 ortn
= CMSEncoderSetEncapsulatedContentType(cmsEncoder
, eContentType
);
1364 if(detachedContent
) {
1365 ortn
= CMSEncoderSetHasDetachedContent(cmsEncoder
, detachedContent
);
1370 if(signedAttributes
) {
1371 ortn
= CMSEncoderAddSignedAttributes(cmsEncoder
, signedAttributes
);
1377 ortn
= CMSEncoderUpdateContent(cmsEncoder
, content
, contentLen
);
1381 ortn
= CMSEncoderCopyEncodedContent(cmsEncoder
, encodedContent
);
1384 CFRelease(cmsEncoder
);
1388 OSStatus
CMSEncodeContent(
1390 CFTypeRef recipients
,
1391 CFTypeRef eContentTypeOID
,
1392 Boolean detachedContent
,
1393 CMSSignedAttributes signedAttributes
,
1394 const void *content
,
1396 CFDataRef
*encodedContentOut
) /* RETURNED */
1398 // convert eContentTypeOID to a CSSM_OID
1399 CSSM_OID contentType
= { 0, NULL
};
1400 if (eContentTypeOID
&& convertOid(eContentTypeOID
, &contentType
) != 0)
1402 const CSSM_OID
*contentTypePtr
= (eContentTypeOID
) ? &contentType
: NULL
;
1403 OSStatus result
= CMSEncode(signers
, recipients
, contentTypePtr
,
1404 detachedContent
, signedAttributes
,
1405 content
, contentLen
, encodedContentOut
);
1406 if (contentType
.Data
)
1407 free(contentType
.Data
);
1411 #pragma mark --- SPI routines declared in CMSPrivate.h ---
1414 * Obtain the SecCmsMessageRef associated with a CMSEncoderRef.
1415 * If we don't have a SecCmsMessageRef yet, we create one now.
1416 * This is the only place where we go to state ES_Msg.
1418 OSStatus
CMSEncoderGetCmsMessage(
1419 CMSEncoderRef cmsEncoder
,
1420 SecCmsMessageRef
*cmsMessage
) /* RETURNED */
1422 if((cmsEncoder
== NULL
) || (cmsMessage
== NULL
)) {
1425 if(cmsEncoder
->cmsMsg
!= NULL
) {
1426 ASSERT(cmsEncoder
->encState
!= ES_Init
);
1427 *cmsMessage
= cmsEncoder
->cmsMsg
;
1428 return errSecSuccess
;
1431 OSStatus ortn
= cmsSetupCmsMsg(cmsEncoder
);
1435 *cmsMessage
= cmsEncoder
->cmsMsg
;
1437 /* Don't set up encoder yet; caller might do that via CMSEncoderSetEncoder */
1438 cmsEncoder
->encState
= ES_Msg
;
1439 return errSecSuccess
;
1443 * Optionally specify a SecCmsEncoderRef to use with a CMSEncoderRef.
1444 * If this is called, it must be called before the first call to
1445 * CMSEncoderUpdateContent(). The CMSEncoderRef takes ownership of the
1446 * incoming SecCmsEncoderRef.
1448 OSStatus
CMSEncoderSetEncoder(
1449 CMSEncoderRef cmsEncoder
,
1450 SecCmsEncoderRef encoder
)
1452 if((cmsEncoder
== NULL
) || (encoder
== NULL
)) {
1458 switch(cmsEncoder
->encState
) {
1460 /* No message, no encoder */
1461 ASSERT(cmsEncoder
->cmsMsg
== NULL
);
1462 ASSERT(cmsEncoder
->encoder
== NULL
);
1463 ortn
= cmsSetupCmsMsg(cmsEncoder
);
1467 /* drop thru to set encoder */
1469 /* cmsMsg but no encoder */
1470 ASSERT(cmsEncoder
->cmsMsg
!= NULL
);
1471 ASSERT(cmsEncoder
->encoder
== NULL
);
1472 cmsEncoder
->encoder
= encoder
;
1473 cmsEncoder
->encState
= ES_Updating
;
1474 cmsEncoder
->customCoder
= true; /* we won't see data */
1475 return errSecSuccess
;
1477 /* no can do, too late */
1483 * Obtain the SecCmsEncoderRef associated with a CMSEncoderRef.
1484 * Returns a NULL SecCmsEncoderRef if neither CMSEncoderSetEncoder nor
1485 * CMSEncoderUpdateContent() has been called.
1486 * The CMSEncoderRef retains ownership of the SecCmsEncoderRef.
1488 OSStatus
CMSEncoderGetEncoder(
1489 CMSEncoderRef cmsEncoder
,
1490 SecCmsEncoderRef
*encoder
) /* RETURNED */
1492 if((cmsEncoder
== NULL
) || (encoder
== NULL
)) {
1496 /* any state, whether we have an encoder or not is OK */
1497 *encoder
= cmsEncoder
->encoder
;
1498 return errSecSuccess
;
1501 #include <AssertMacros.h>
1504 * Obtain the timestamp of signer 'signerIndex' of a CMS message, if
1505 * present. This timestamp is an authenticated timestamp provided by
1506 * a timestamping authority.
1508 * Returns errSecParam if the CMS message was not signed or if signerIndex
1509 * is greater than the number of signers of the message minus one.
1511 * This cannot be called until after CMSEncoderCopyEncodedContent() is called.
1513 OSStatus
CMSEncoderCopySignerTimestamp(
1514 CMSEncoderRef cmsEncoder
,
1515 size_t signerIndex
, /* usually 0 */
1516 CFAbsoluteTime
*timestamp
) /* RETURNED */
1518 return CMSEncoderCopySignerTimestampWithPolicy(
1525 OSStatus
CMSEncoderCopySignerTimestampWithPolicy(
1526 CMSEncoderRef cmsEncoder
,
1527 CFTypeRef timeStampPolicy
,
1528 size_t signerIndex
, /* usually 0 */
1529 CFAbsoluteTime
*timestamp
) /* RETURNED */
1531 OSStatus status
= errSecParam
;
1532 SecCmsMessageRef cmsg
;
1533 SecCmsSignedDataRef signedData
= NULL
;
1534 int numContentInfos
= 0;
1536 require(cmsEncoder
&& timestamp
, xit
);
1537 require_noerr(CMSEncoderGetCmsMessage(cmsEncoder
, &cmsg
), xit
);
1538 numContentInfos
= SecCmsMessageContentLevelCount(cmsg
);
1539 for (int dex
= 0; !signedData
&& dex
< numContentInfos
; dex
++)
1541 SecCmsContentInfoRef ci
= SecCmsMessageContentLevel(cmsg
, dex
);
1542 SECOidTag tag
= SecCmsContentInfoGetContentTypeTag(ci
);
1543 if (tag
== SEC_OID_PKCS7_SIGNED_DATA
)
1544 if ((signedData
= SecCmsSignedDataRef(SecCmsContentInfoGetContent(ci
))))
1545 if (SecCmsSignerInfoRef signerInfo
= SecCmsSignedDataGetSignerInfo(signedData
, (int)signerIndex
))
1547 status
= SecCmsSignerInfoGetTimestampTimeWithPolicy(signerInfo
, timeStampPolicy
, timestamp
);