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
;
103 static void cmsEncoderInit(CFTypeRef enc
);
104 static void cmsEncoderFinalize(CFTypeRef enc
);
106 static CFRuntimeClass cmsEncoderRuntimeClass
=
113 NULL
, /* equal - just use pointer equality */
114 NULL
, /* hash, ditto */
115 NULL
, /* copyFormattingDesc */
116 NULL
/* copyDebugDesc */
120 CmsMessageSetTSACallback(CMSEncoderRef cmsEncoder
, SecCmsTSACallback tsaCallback
);
122 #pragma mark --- Private routines ---
125 * Decode a CFStringRef representation of an integer
127 static int cfStringToNumber(
132 if (!inStr
|| !CFStringGetCString(inStr
, buf
, max
-1, kCFStringEncodingASCII
))
138 * Encode an integer component of an OID, return resulting number of bytes;
139 * actual bytes are mallocd and returned in *encodeArray.
141 static unsigned encodeNumber(
143 unsigned char **encodeArray
) // mallocd and RETURNED
145 unsigned char *result
;
147 unsigned numDigits
= 0;
150 /* trival case - 0 maps to 0 */
152 *encodeArray
= (unsigned char *)malloc(1);
157 /* first calculate the number of digits in num, base 128 */
158 scratch
= (unsigned)num
;
159 while(scratch
!= 0) {
164 result
= (unsigned char *)malloc(numDigits
);
165 scratch
= (unsigned)num
;
166 for(dex
=0; dex
<numDigits
; dex
++) {
167 result
[numDigits
- dex
- 1] = scratch
& 0x7f;
171 /* all digits except the last one have m.s. bit set */
172 for(dex
=0; dex
<(numDigits
- 1); dex
++) {
176 *encodeArray
= result
;
181 * Given an OID in dotted-decimal string representation, convert to binary
182 * DER format. Returns a pointer in outOid which the caller must free(),
183 * as well as the length of the data in outLen.
184 * Function returns 0 if successful, non-zero otherwise.
186 static int encodeOid(
187 const unsigned char *inStr
,
188 unsigned char **outOid
,
189 unsigned int *outLen
)
191 unsigned char **digits
= NULL
; /* array of char * from encodeNumber */
192 unsigned *numDigits
= NULL
; /* array of unsigned from encodeNumber */
194 unsigned numDigitBytes
; /* total #of output chars */
195 unsigned char firstByte
;
197 CFIndex numsToProcess
;
198 CFStringRef oidStr
= NULL
;
199 CFArrayRef argvRef
= NULL
;
203 /* parse input string into array of substrings */
204 if (!inStr
|| !outOid
|| !outLen
) goto cleanExit
;
205 oidStr
= CFStringCreateWithCString(NULL
, (const char *)inStr
, kCFStringEncodingASCII
);
206 if (!oidStr
) goto cleanExit
;
207 argvRef
= CFStringCreateArrayBySeparatingStrings(NULL
, oidStr
, CFSTR("."));
208 if (!argvRef
) goto cleanExit
;
209 argc
= CFArrayGetCount(argvRef
);
210 if (argc
< 3) goto cleanExit
;
212 /* first two numbers in OID munge together */
213 num
= cfStringToNumber((CFStringRef
)CFArrayGetValueAtIndex(argvRef
, 0));
214 if (num
< 0) goto cleanExit
;
215 firstByte
= (40 * num
);
216 num
= cfStringToNumber((CFStringRef
)CFArrayGetValueAtIndex(argvRef
, 1));
217 if (num
< 0) goto cleanExit
;
221 numsToProcess
= argc
- 2;
222 if(numsToProcess
> 0) {
223 /* skip this loop in the unlikely event that input is only two numbers */
224 digits
= (unsigned char **) malloc(numsToProcess
* sizeof(unsigned char *));
225 numDigits
= (unsigned *) malloc(numsToProcess
* sizeof(unsigned));
226 for(digit
=0; digit
<numsToProcess
; digit
++) {
227 num
= cfStringToNumber((CFStringRef
)CFArrayGetValueAtIndex(argvRef
, digit
+2));
228 if (num
< 0) goto cleanExit
;
229 numDigits
[digit
] = encodeNumber(num
, &digits
[digit
]);
230 numDigitBytes
+= numDigits
[digit
];
233 *outLen
= (2 + numDigitBytes
);
234 *outOid
= outP
= (unsigned char *) malloc(*outLen
);
236 *outP
++ = numDigitBytes
;
238 for(digit
=0; digit
<numsToProcess
; digit
++) {
239 unsigned int byteDex
;
240 for(byteDex
=0; byteDex
<numDigits
[digit
]; byteDex
++) {
241 *outP
++ = digits
[digit
][byteDex
];
245 for(digit
=0; digit
<numsToProcess
; digit
++) {
254 if (oidStr
) CFRelease(oidStr
);
255 if (argvRef
) CFRelease(argvRef
);
261 * Given a CF object reference describing an OID, convert to binary DER format
262 * and fill out the CSSM_OID structure provided by the caller. Caller is
263 * responsible for freeing the data pointer in outOid->Data.
265 * Function returns 0 if successful, non-zero otherwise.
268 static int convertOid(
272 if (!inRef
|| !outOid
)
275 unsigned char *oidData
= NULL
;
276 unsigned int oidLen
= 0;
278 if (CFGetTypeID(inRef
) == CFStringGetTypeID()) {
279 // CFStringRef: OID representation is a dotted-decimal string
280 CFStringRef inStr
= (CFStringRef
)inRef
;
281 CFIndex max
= CFStringGetLength(inStr
) * 3;
283 if (!CFStringGetCString(inStr
, buf
, max
-1, kCFStringEncodingASCII
))
286 if(encodeOid((unsigned char *)buf
, &oidData
, &oidLen
) != 0)
289 else if (CFGetTypeID(inRef
) == CFDataGetTypeID()) {
290 // CFDataRef: OID representation is in binary DER format
291 CFDataRef inData
= (CFDataRef
)inRef
;
292 oidLen
= (unsigned int) CFDataGetLength(inData
);
293 oidData
= (unsigned char *) malloc(oidLen
);
294 memcpy(oidData
, CFDataGetBytePtr(inData
), oidLen
);
297 // Not in a format we understand
300 outOid
->Length
= oidLen
;
301 outOid
->Data
= (uint8
*)oidData
;
305 static CFTypeID cmsEncoderTypeID
= _kCFRuntimeNotATypeID
;
307 /* one time only class init, called via pthread_once() in CMSEncoderGetTypeID() */
308 static void cmsEncoderClassInitialize(void)
311 _CFRuntimeRegisterClass((const CFRuntimeClass
* const)&cmsEncoderRuntimeClass
);
314 /* init called out from _CFRuntimeCreateInstance() */
315 static void cmsEncoderInit(CFTypeRef enc
)
317 char *start
= ((char *)enc
) + sizeof(CFRuntimeBase
);
318 memset(start
, 0, sizeof(struct _CMSEncoder
) - sizeof(CFRuntimeBase
));
322 * Dispose of a CMSEncoder. Called out from CFRelease().
324 static void cmsEncoderFinalize(
327 CMSEncoderRef cmsEncoder
= (CMSEncoderRef
)enc
;
328 if(cmsEncoder
== NULL
) {
331 if(cmsEncoder
->eContentType
.Data
!= NULL
) {
332 free(cmsEncoder
->eContentType
.Data
);
334 CFRELEASE(cmsEncoder
->signers
);
335 CFRELEASE(cmsEncoder
->recipients
);
336 CFRELEASE(cmsEncoder
->otherCerts
);
337 if(cmsEncoder
->cmsMsg
!= NULL
) {
338 SecCmsMessageDestroy(cmsEncoder
->cmsMsg
);
340 if(cmsEncoder
->arena
!= NULL
) {
341 SecArenaPoolFree(cmsEncoder
->arena
, false);
343 if(cmsEncoder
->encoder
!= NULL
) {
345 * Normally this gets freed in SecCmsEncoderFinish - this is
348 SecCmsEncoderDestroy(cmsEncoder
->encoder
);
352 static OSStatus
cmsSetupEncoder(
353 CMSEncoderRef cmsEncoder
)
357 ASSERT(cmsEncoder
->arena
== NULL
);
358 ASSERT(cmsEncoder
->encoder
== NULL
);
360 ortn
= SecArenaPoolCreate(1024, &cmsEncoder
->arena
);
362 return cmsRtnToOSStatus(ortn
);
364 ortn
= SecCmsEncoderCreate(cmsEncoder
->cmsMsg
,
365 NULL
, NULL
, // no callback
366 &cmsEncoder
->encoderOut
, // data goes here
368 NULL
, NULL
, // no password callback (right?)
369 NULL
, NULL
, // decrypt key callback
370 NULL
, NULL
, // detached digests
371 &cmsEncoder
->encoder
);
373 return cmsRtnToOSStatus(ortn
);
375 return errSecSuccess
;
379 * Set up a SecCmsMessageRef for a SignedData creation.
381 static OSStatus
cmsSetupForSignedData(
382 CMSEncoderRef cmsEncoder
)
384 ASSERT((cmsEncoder
->signers
!= NULL
) || (cmsEncoder
->otherCerts
!= NULL
));
386 SecCmsContentInfoRef contentInfo
= NULL
;
387 SecCmsSignedDataRef signedData
= NULL
;
390 /* build chain of objects: message->signedData->data */
391 if(cmsEncoder
->cmsMsg
!= NULL
) {
392 SecCmsMessageDestroy(cmsEncoder
->cmsMsg
);
394 cmsEncoder
->cmsMsg
= SecCmsMessageCreate(NULL
);
395 if(cmsEncoder
->cmsMsg
== NULL
) {
396 return errSecInternalComponent
;
399 signedData
= SecCmsSignedDataCreate(cmsEncoder
->cmsMsg
);
400 if(signedData
== NULL
) {
401 return errSecInternalComponent
;
403 contentInfo
= SecCmsMessageGetContentInfo(cmsEncoder
->cmsMsg
);
404 ortn
= SecCmsContentInfoSetContentSignedData(cmsEncoder
->cmsMsg
, contentInfo
,
407 return cmsRtnToOSStatus(ortn
);
409 contentInfo
= SecCmsSignedDataGetContentInfo(signedData
);
410 if(cmsEncoder
->eContentType
.Data
!= NULL
) {
411 /* Override the default eContentType of id-data */
412 ortn
= SecCmsContentInfoSetContentOther(cmsEncoder
->cmsMsg
,
414 NULL
, /* data - provided to encoder, not here */
415 cmsEncoder
->detachedContent
,
416 &cmsEncoder
->eContentType
);
419 ortn
= SecCmsContentInfoSetContentData(cmsEncoder
->cmsMsg
,
421 NULL
, /* data - provided to encoder, not here */
422 cmsEncoder
->detachedContent
);
425 ortn
= cmsRtnToOSStatus(ortn
);
426 CSSM_PERROR("SecCmsContentInfoSetContent*", ortn
);
430 /* optional 'global' (per-SignedData) certs */
431 if(cmsEncoder
->otherCerts
!= NULL
) {
432 ortn
= SecCmsSignedDataAddCertList(signedData
, cmsEncoder
->otherCerts
);
434 ortn
= cmsRtnToOSStatus(ortn
);
435 CSSM_PERROR("SecCmsSignedDataAddCertList", ortn
);
440 /* SignerInfos, one per signer */
441 CFIndex numSigners
= 0;
442 if(cmsEncoder
->signers
!= NULL
) {
443 /* this is optional...in case we're just creating a cert bundle */
444 numSigners
= CFArrayGetCount(cmsEncoder
->signers
);
447 SecKeychainRef ourKc
= NULL
;
448 SecCertificateRef ourCert
= NULL
;
449 SecCmsCertChainMode chainMode
= SecCmsCMCertChain
;
451 switch(cmsEncoder
->chainMode
) {
452 case kCMSCertificateNone
:
453 chainMode
= SecCmsCMNone
;
455 case kCMSCertificateSignerOnly
:
456 chainMode
= SecCmsCMCertOnly
;
458 case kCMSCertificateChainWithRoot
:
459 chainMode
= SecCmsCMCertChainWithRoot
;
464 for(dex
=0; dex
<numSigners
; dex
++) {
465 SecCmsSignerInfoRef signerInfo
;
467 SecIdentityRef ourId
=
468 (SecIdentityRef
)CFArrayGetValueAtIndex(cmsEncoder
->signers
, dex
);
469 ortn
= SecIdentityCopyCertificate(ourId
, &ourCert
);
471 CSSM_PERROR("SecIdentityCopyCertificate", ortn
);
474 ortn
= SecKeychainItemCopyKeychain((SecKeychainItemRef
)ourCert
, &ourKc
);
476 CSSM_PERROR("SecKeychainItemCopyKeychain", ortn
);
479 signerInfo
= SecCmsSignerInfoCreate(cmsEncoder
->cmsMsg
, ourId
, cmsEncoder
->digestalgtag
);
480 if (signerInfo
== NULL
) {
481 ortn
= errSecInternalComponent
;
485 /* we want the cert chain included for this one */
486 /* NOTE the usage parameter is currently unused by the SMIME lib */
487 ortn
= SecCmsSignerInfoIncludeCerts(signerInfo
, chainMode
,
488 certUsageEmailSigner
);
490 ortn
= cmsRtnToOSStatus(ortn
);
491 CSSM_PERROR("SecCmsSignerInfoIncludeCerts", ortn
);
496 if(cmsEncoder
->signedAttributes
& kCMSAttrSmimeCapabilities
) {
497 ortn
= SecCmsSignerInfoAddSMIMECaps(signerInfo
);
499 ortn
= cmsRtnToOSStatus(ortn
);
500 CSSM_PERROR("SecCmsSignerInfoAddSMIMEEncKeyPrefs", ortn
);
504 if(cmsEncoder
->signedAttributes
& kCMSAttrSmimeEncryptionKeyPrefs
) {
505 ortn
= SecCmsSignerInfoAddSMIMEEncKeyPrefs(signerInfo
, ourCert
, ourKc
);
507 ortn
= cmsRtnToOSStatus(ortn
);
508 CSSM_PERROR("SecCmsSignerInfoAddSMIMEEncKeyPrefs", ortn
);
512 if(cmsEncoder
->signedAttributes
& kCMSAttrSmimeMSEncryptionKeyPrefs
) {
513 ortn
= SecCmsSignerInfoAddMSSMIMEEncKeyPrefs(signerInfo
, ourCert
, ourKc
);
515 ortn
= cmsRtnToOSStatus(ortn
);
516 CSSM_PERROR("SecCmsSignerInfoAddMSSMIMEEncKeyPrefs", ortn
);
520 if(cmsEncoder
->signedAttributes
& kCMSAttrSigningTime
) {
521 if (cmsEncoder
->signingTime
== 0)
522 cmsEncoder
->signingTime
= CFAbsoluteTimeGetCurrent();
523 ortn
= SecCmsSignerInfoAddSigningTime(signerInfo
, cmsEncoder
->signingTime
);
525 ortn
= cmsRtnToOSStatus(ortn
);
526 CSSM_PERROR("SecCmsSignerInfoAddSigningTime", ortn
);
530 if(cmsEncoder
->signedAttributes
& kCMSAttrAppleCodesigningHashAgility
) {
531 ortn
= SecCmsSignerInfoAddAppleCodesigningHashAgility(signerInfo
, cmsEncoder
->hashAgilityAttrValue
);
532 /* libsecurity_smime made a copy of the attribute value. We don't need it anymore. */
533 CFReleaseNull(cmsEncoder
->hashAgilityAttrValue
);
535 ortn
= cmsRtnToOSStatus(ortn
);
536 CSSM_PERROR("SecCmsSignerInfoAddAppleCodesigningHashAgility", ortn
);
541 ortn
= SecCmsSignedDataAddSignerInfo(signedData
, signerInfo
);
543 ortn
= cmsRtnToOSStatus(ortn
);
544 CSSM_PERROR("SecCmsSignedDataAddSignerInfo", ortn
);
561 * Set up a SecCmsMessageRef for a EnvelopedData creation.
563 static OSStatus
cmsSetupForEnvelopedData(
564 CMSEncoderRef cmsEncoder
)
566 ASSERT(cmsEncoder
->op
== EO_Encrypt
);
567 ASSERT(cmsEncoder
->recipients
!= NULL
);
569 SecCmsContentInfoRef contentInfo
= NULL
;
570 SecCmsEnvelopedDataRef envelopedData
= NULL
;
571 SECOidTag algorithmTag
;
576 * Find encryption algorithm...unfortunately we need a NULL-terminated array
577 * of SecCertificateRefs for this.
579 CFIndex numCerts
= CFArrayGetCount(cmsEncoder
->recipients
);
581 SecCertificateRef
*certArray
= (SecCertificateRef
*)malloc(
582 (numCerts
+1) * sizeof(SecCertificateRef
));
584 for(dex
=0; dex
<numCerts
; dex
++) {
585 certArray
[dex
] = (SecCertificateRef
)CFArrayGetValueAtIndex(
586 cmsEncoder
->recipients
, dex
);
588 certArray
[numCerts
] = NULL
;
589 ortn
= SecSMIMEFindBulkAlgForRecipients(certArray
, &algorithmTag
, &keySize
);
592 CSSM_PERROR("SecSMIMEFindBulkAlgForRecipients", ortn
);
596 /* build chain of objects: message->envelopedData->data */
597 if(cmsEncoder
->cmsMsg
!= NULL
) {
598 SecCmsMessageDestroy(cmsEncoder
->cmsMsg
);
600 cmsEncoder
->cmsMsg
= SecCmsMessageCreate(NULL
);
601 if(cmsEncoder
->cmsMsg
== NULL
) {
602 return errSecInternalComponent
;
604 envelopedData
= SecCmsEnvelopedDataCreate(cmsEncoder
->cmsMsg
,
605 algorithmTag
, keySize
);
606 if(envelopedData
== NULL
) {
607 return errSecInternalComponent
;
609 contentInfo
= SecCmsMessageGetContentInfo(cmsEncoder
->cmsMsg
);
610 ortn
= SecCmsContentInfoSetContentEnvelopedData(cmsEncoder
->cmsMsg
,
611 contentInfo
, envelopedData
);
613 ortn
= cmsRtnToOSStatus(ortn
);
614 CSSM_PERROR("SecCmsContentInfoSetContentEnvelopedData", ortn
);
617 contentInfo
= SecCmsEnvelopedDataGetContentInfo(envelopedData
);
618 if(cmsEncoder
->eContentType
.Data
!= NULL
) {
619 /* Override the default ContentType of id-data */
620 ortn
= SecCmsContentInfoSetContentOther(cmsEncoder
->cmsMsg
,
622 NULL
, /* data - provided to encoder, not here */
623 FALSE
, /* detachedContent */
624 &cmsEncoder
->eContentType
);
627 ortn
= SecCmsContentInfoSetContentData(cmsEncoder
->cmsMsg
,
629 NULL
/* data - provided to encoder, not here */,
630 cmsEncoder
->detachedContent
);
633 ortn
= cmsRtnToOSStatus(ortn
);
634 CSSM_PERROR("SecCmsContentInfoSetContentData*", ortn
);
639 * create & attach recipient information, one for each recipient
641 for(dex
=0; dex
<numCerts
; dex
++) {
642 SecCmsRecipientInfoRef recipientInfo
= NULL
;
644 SecCertificateRef thisRecip
= (SecCertificateRef
)CFArrayGetValueAtIndex(
645 cmsEncoder
->recipients
, dex
);
646 recipientInfo
= SecCmsRecipientInfoCreate(cmsEncoder
->cmsMsg
, thisRecip
);
647 ortn
= SecCmsEnvelopedDataAddRecipient(envelopedData
, recipientInfo
);
649 ortn
= cmsRtnToOSStatus(ortn
);
650 CSSM_PERROR("SecCmsEnvelopedDataAddRecipient", ortn
);
654 return errSecSuccess
;
658 * Set up cmsMsg. Called from either the first call to CMSEncoderUpdateContent, or
659 * from CMSEncodeGetCmsMessage().
661 static OSStatus
cmsSetupCmsMsg(
662 CMSEncoderRef cmsEncoder
)
664 ASSERT(cmsEncoder
!= NULL
);
665 ASSERT(cmsEncoder
->encState
== ES_Init
);
667 /* figure out what high-level operation we're doing */
668 if((cmsEncoder
->signers
!= NULL
) || (cmsEncoder
->otherCerts
!= NULL
)) {
669 if(cmsEncoder
->recipients
!= NULL
) {
670 cmsEncoder
->op
= EO_SignEncrypt
;
673 cmsEncoder
->op
= EO_Sign
;
676 else if(cmsEncoder
->recipients
!= NULL
) {
677 cmsEncoder
->op
= EO_Encrypt
;
680 dprintf("CMSEncoderUpdateContent: nothing to do\n");
684 OSStatus ortn
= errSecSuccess
;
686 switch(cmsEncoder
->op
) {
689 /* If we're signing & encrypting, do the signing first */
690 ortn
= cmsSetupForSignedData(cmsEncoder
);
693 ortn
= cmsSetupForEnvelopedData(cmsEncoder
);
696 cmsEncoder
->encState
= ES_Msg
;
701 * ASN.1 template for decoding a ContentInfo.
704 CSSM_OID contentType
;
708 static const SecAsn1Template cmsSimpleContentInfoTemplate
[] = {
709 { SEC_ASN1_SEQUENCE
, 0, NULL
, sizeof(SimpleContentInfo
) },
710 { SEC_ASN1_OBJECT_ID
, offsetof(SimpleContentInfo
, contentType
) },
711 { SEC_ASN1_EXPLICIT
| SEC_ASN1_CONSTRUCTED
| SEC_ASN1_CONTEXT_SPECIFIC
| 0,
712 offsetof(SimpleContentInfo
, content
),
713 kSecAsn1AnyTemplate
},
718 * Obtain the content of a contentInfo, This basically strips off the contentType OID
719 * and returns its ASN_ANY content, allocated the provided coder's memory space.
721 static OSStatus
cmsContentInfoContent(
722 SecAsn1CoderRef asn1Coder
,
723 const CSSM_DATA
*contentInfo
,
724 CSSM_DATA
*content
) /* RETURNED */
727 SimpleContentInfo decodedInfo
;
729 memset(&decodedInfo
, 0, sizeof(decodedInfo
));
730 ortn
= SecAsn1DecodeData(asn1Coder
, contentInfo
,
731 cmsSimpleContentInfoTemplate
, &decodedInfo
);
735 if(decodedInfo
.content
.Data
== NULL
) {
736 dprintf("***Error decoding contentInfo: no content\n");
737 return errSecInternalComponent
;
739 *content
= decodedInfo
.content
;
740 return errSecSuccess
;
743 #pragma mark --- Start of Public API ---
745 CFTypeID
CMSEncoderGetTypeID(void)
747 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
749 if(cmsEncoderTypeID
== _kCFRuntimeNotATypeID
) {
750 pthread_once(&once
, &cmsEncoderClassInitialize
);
752 return cmsEncoderTypeID
;
756 * Create a CMSEncoder. Result must eventually be freed via CFRelease().
758 OSStatus
CMSEncoderCreate(
759 CMSEncoderRef
*cmsEncoderOut
) /* RETURNED */
761 CMSEncoderRef cmsEncoder
= NULL
;
763 uint32_t extra
= sizeof(*cmsEncoder
) - sizeof(cmsEncoder
->base
);
764 cmsEncoder
= (CMSEncoderRef
)_CFRuntimeCreateInstance(NULL
, CMSEncoderGetTypeID(),
766 if(cmsEncoder
== NULL
) {
767 return errSecAllocate
;
769 cmsEncoder
->encState
= ES_Init
;
770 cmsEncoder
->chainMode
= kCMSCertificateChain
;
771 cmsEncoder
->digestalgtag
= SEC_OID_SHA1
;
772 *cmsEncoderOut
= cmsEncoder
;
773 return errSecSuccess
;
776 #pragma mark --- Getters & Setters ---
778 const CFStringRef __nonnull kCMSEncoderDigestAlgorithmSHA1
= CFSTR("sha1");
779 const CFStringRef __nonnull kCMSEncoderDigestAlgorithmSHA256
= CFSTR("sha256");
782 OSStatus
CMSEncoderSetSignerAlgorithm(
783 CMSEncoderRef cmsEncoder
,
784 CFStringRef digestAlgorithm
)
786 if (CFEqual(digestAlgorithm
, kCMSEncoderDigestAlgorithmSHA1
)) {
787 cmsEncoder
->digestalgtag
= SEC_OID_SHA1
;
788 } else if (CFEqual(digestAlgorithm
, kCMSEncoderDigestAlgorithmSHA256
)) {
789 cmsEncoder
->digestalgtag
= SEC_OID_SHA256
;
794 return errSecSuccess
;
798 * Specify signers of the CMS message; implies that the message will be signed.
800 OSStatus
CMSEncoderAddSigners(
801 CMSEncoderRef cmsEncoder
,
802 CFTypeRef signerOrArray
)
804 if(cmsEncoder
== NULL
) {
807 if(cmsEncoder
->encState
!= ES_Init
) {
810 return cmsAppendToArray(signerOrArray
, &cmsEncoder
->signers
, SecIdentityGetTypeID());
814 * Obtain an array of signers as specified in CMSEncoderSetSigners().
816 OSStatus
CMSEncoderCopySigners(
817 CMSEncoderRef cmsEncoder
,
820 if((cmsEncoder
== NULL
) || (signers
== NULL
)) {
823 if(cmsEncoder
->signers
!= NULL
) {
824 CFRetain(cmsEncoder
->signers
);
826 *signers
= cmsEncoder
->signers
;
827 return errSecSuccess
;
831 * Specify recipients of the message. Implies that the message will be encrypted.
833 OSStatus
CMSEncoderAddRecipients(
834 CMSEncoderRef cmsEncoder
,
835 CFTypeRef recipientOrArray
)
837 if(cmsEncoder
== NULL
) {
840 if(cmsEncoder
->encState
!= ES_Init
) {
843 return cmsAppendToArray(recipientOrArray
, &cmsEncoder
->recipients
,
844 SecCertificateGetTypeID());
848 * Obtain an array of recipients as specified in CMSEncoderSetRecipients().
850 OSStatus
CMSEncoderCopyRecipients(
851 CMSEncoderRef cmsEncoder
,
852 CFArrayRef
*recipients
)
854 if((cmsEncoder
== NULL
) || (recipients
== NULL
)) {
857 if(cmsEncoder
->recipients
!= NULL
) {
858 CFRetain(cmsEncoder
->recipients
);
860 *recipients
= cmsEncoder
->recipients
;
861 return errSecSuccess
;
865 * Specify additional certs to include in a signed message.
867 OSStatus
CMSEncoderAddSupportingCerts(
868 CMSEncoderRef cmsEncoder
,
869 CFTypeRef certOrArray
)
871 if(cmsEncoder
== NULL
) {
874 if(cmsEncoder
->encState
!= ES_Init
) {
877 return cmsAppendToArray(certOrArray
, &cmsEncoder
->otherCerts
,
878 SecCertificateGetTypeID());
882 * Obtain the SecCertificates provided in CMSEncoderAddSupportingCerts().
884 OSStatus
CMSEncoderCopySupportingCerts(
885 CMSEncoderRef cmsEncoder
,
886 CFArrayRef
*certs
) /* RETURNED */
888 if((cmsEncoder
== NULL
) || (certs
== NULL
)) {
891 if(cmsEncoder
->otherCerts
!= NULL
) {
892 CFRetain(cmsEncoder
->otherCerts
);
894 *certs
= cmsEncoder
->otherCerts
;
895 return errSecSuccess
;
898 OSStatus
CMSEncoderSetHasDetachedContent(
899 CMSEncoderRef cmsEncoder
,
900 Boolean detachedContent
)
902 if(cmsEncoder
== NULL
) {
905 if(cmsEncoder
->encState
!= ES_Init
) {
908 cmsEncoder
->detachedContent
= detachedContent
;
909 return errSecSuccess
;
912 OSStatus
CMSEncoderGetHasDetachedContent(
913 CMSEncoderRef cmsEncoder
,
914 Boolean
*detachedContent
) /* RETURNED */
916 if((cmsEncoder
== NULL
) || (detachedContent
== NULL
)) {
919 *detachedContent
= cmsEncoder
->detachedContent
;
920 return errSecSuccess
;
924 * Optionally specify an eContentType OID for the inner EncapsulatedData for
925 * a signed message. The default eContentType, used of this function is not
926 * called, is id-data.
928 OSStatus
CMSEncoderSetEncapsulatedContentType(
929 CMSEncoderRef cmsEncoder
,
930 const CSSM_OID
*eContentType
)
932 if((cmsEncoder
== NULL
) || (eContentType
== NULL
)) {
935 if(cmsEncoder
->encState
!= ES_Init
) {
939 CSSM_OID
*ecOid
= &cmsEncoder
->eContentType
;
940 if(ecOid
->Data
!= NULL
) {
943 cmsCopyCmsData(eContentType
, ecOid
);
944 return errSecSuccess
;
947 OSStatus
CMSEncoderSetEncapsulatedContentTypeOID(
948 CMSEncoderRef cmsEncoder
,
949 CFTypeRef eContentTypeOID
)
951 // convert eContentTypeOID to a CSSM_OID
952 CSSM_OID contentType
= { 0, NULL
};
953 if (!eContentTypeOID
|| convertOid(eContentTypeOID
, &contentType
) != 0)
955 OSStatus result
= CMSEncoderSetEncapsulatedContentType(cmsEncoder
, &contentType
);
956 if (contentType
.Data
)
957 free(contentType
.Data
);
962 * Obtain the eContentType OID specified in CMSEncoderSetEncapsulatedContentType().
964 OSStatus
CMSEncoderCopyEncapsulatedContentType(
965 CMSEncoderRef cmsEncoder
,
966 CFDataRef
*eContentType
)
968 if((cmsEncoder
== NULL
) || (eContentType
== NULL
)) {
972 CSSM_OID
*ecOid
= &cmsEncoder
->eContentType
;
973 if(ecOid
->Data
== NULL
) {
974 *eContentType
= NULL
;
977 *eContentType
= CFDataCreate(NULL
, ecOid
->Data
, ecOid
->Length
);
979 return errSecSuccess
;
983 * Optionally specify signed attributes. Only meaningful when creating a
984 * signed message. If this is called, it must be called before
985 * CMSEncoderUpdateContent().
987 OSStatus
CMSEncoderAddSignedAttributes(
988 CMSEncoderRef cmsEncoder
,
989 CMSSignedAttributes signedAttributes
)
991 if(cmsEncoder
== NULL
) {
994 if(cmsEncoder
->encState
!= ES_Init
) {
997 cmsEncoder
->signedAttributes
|= signedAttributes
;
998 return errSecSuccess
;
1002 * Set the signing time for a CMSEncoder.
1003 * This is only used if the kCMSAttrSigningTime attribute is included.
1005 OSStatus
CMSEncoderSetSigningTime(
1006 CMSEncoderRef cmsEncoder
,
1007 CFAbsoluteTime time
)
1009 if(cmsEncoder
== NULL
) {
1012 if(cmsEncoder
->encState
!= ES_Init
) {
1015 cmsEncoder
->signingTime
= time
;
1016 return errSecSuccess
;
1020 * Set the hash agility attribute for a CMSEncoder.
1021 * This is only used if the kCMSAttrAppleCodesigningHashAgility attribute
1024 OSStatus
CMSEncoderSetAppleCodesigningHashAgility(
1025 CMSEncoderRef cmsEncoder
,
1026 CFDataRef hashAgilityAttrValue
)
1028 if (cmsEncoder
== NULL
|| cmsEncoder
->encState
!= ES_Init
) {
1031 cmsEncoder
->hashAgilityAttrValue
= CFRetainSafe(hashAgilityAttrValue
);
1032 return errSecSuccess
;
1035 OSStatus
CMSEncoderSetCertificateChainMode(
1036 CMSEncoderRef cmsEncoder
,
1037 CMSCertificateChainMode chainMode
)
1039 if(cmsEncoder
== NULL
) {
1042 if(cmsEncoder
->encState
!= ES_Init
) {
1046 case kCMSCertificateNone
:
1047 case kCMSCertificateSignerOnly
:
1048 case kCMSCertificateChain
:
1049 case kCMSCertificateChainWithRoot
:
1054 cmsEncoder
->chainMode
= chainMode
;
1055 return errSecSuccess
;
1058 OSStatus
CMSEncoderGetCertificateChainMode(
1059 CMSEncoderRef cmsEncoder
,
1060 CMSCertificateChainMode
*chainModeOut
)
1062 if(cmsEncoder
== NULL
) {
1065 *chainModeOut
= cmsEncoder
->chainMode
;
1066 return errSecSuccess
;
1070 CmsMessageSetTSACallback(CMSEncoderRef cmsEncoder
, SecCmsTSACallback tsaCallback
)
1072 if (cmsEncoder
->cmsMsg
)
1073 SecCmsMessageSetTSACallback(cmsEncoder
->cmsMsg
, tsaCallback
);
1077 CmsMessageSetTSAContext(CMSEncoderRef cmsEncoder
, CFTypeRef tsaContext
)
1079 if (cmsEncoder
->cmsMsg
)
1080 SecCmsMessageSetTSAContext(cmsEncoder
->cmsMsg
, tsaContext
);
1083 #pragma mark --- Action ---
1086 * Feed content bytes into the encoder.
1087 * Can be called multiple times.
1088 * No 'setter' routines can be called after this function has been called.
1090 OSStatus
CMSEncoderUpdateContent(
1091 CMSEncoderRef cmsEncoder
,
1092 const void *content
,
1095 if(cmsEncoder
== NULL
) {
1099 OSStatus ortn
= errSecSuccess
;
1100 switch(cmsEncoder
->encState
) {
1103 * First time thru: do the CmsMsg setup.
1105 ortn
= cmsSetupCmsMsg(cmsEncoder
);
1109 /* fall thru to set up the encoder */
1112 /* We have a cmsMsg but no encoder; create one */
1113 ASSERT(cmsEncoder
->cmsMsg
!= NULL
);
1114 ASSERT(cmsEncoder
->encoder
== NULL
);
1115 ortn
= cmsSetupEncoder(cmsEncoder
);
1119 /* only legal calls now are update and finalize */
1120 cmsEncoder
->encState
= ES_Updating
;
1124 ASSERT(cmsEncoder
->encoder
!= NULL
);
1128 /* Too late for another update */
1132 return errSecInternalComponent
;
1135 /* FIXME - CFIndex same size as size_t on 64bit? */
1136 ortn
= SecCmsEncoderUpdate(cmsEncoder
->encoder
, content
, (CFIndex
)contentLen
);
1138 ortn
= cmsRtnToOSStatus(ortn
);
1139 CSSM_PERROR("SecCmsEncoderUpdate", ortn
);
1145 * Finish encoding the message and obtain the encoded result.
1146 * Caller must CFRelease the result.
1148 OSStatus
CMSEncoderCopyEncodedContent(
1149 CMSEncoderRef cmsEncoder
,
1150 CFDataRef
*encodedContent
)
1152 if((cmsEncoder
== NULL
) || (encodedContent
== NULL
)) {
1158 switch(cmsEncoder
->encState
) {
1160 /* normal termination */
1163 /* already been called */
1168 * The only time these are legal is when we're doing a SignedData
1169 * with certificates only (no signers, no content).
1171 if((cmsEncoder
->signers
!= NULL
) ||
1172 (cmsEncoder
->recipients
!= NULL
) ||
1173 (cmsEncoder
->otherCerts
== NULL
)) {
1177 /* Set up for certs only */
1178 ortn
= cmsSetupForSignedData(cmsEncoder
);
1182 /* and an encoder */
1183 ortn
= cmsSetupEncoder(cmsEncoder
);
1191 ASSERT(cmsEncoder
->encoder
!= NULL
);
1192 ortn
= SecCmsEncoderFinish(cmsEncoder
->encoder
);
1193 /* regardless of the outcome, the encoder itself has been freed */
1194 cmsEncoder
->encoder
= NULL
;
1196 return cmsRtnToOSStatus(ortn
);
1198 cmsEncoder
->encState
= ES_Final
;
1200 if((cmsEncoder
->encoderOut
.Data
== NULL
) && !cmsEncoder
->customCoder
) {
1201 /* not sure how this could happen... */
1202 dprintf("Successful encode, but no data\n");
1203 return errSecInternalComponent
;
1205 if(cmsEncoder
->customCoder
) {
1207 *encodedContent
= NULL
;
1208 return errSecSuccess
;
1211 /* in two out of three cases, we're done */
1212 switch(cmsEncoder
->op
) {
1215 *encodedContent
= CFDataCreate(NULL
, (const UInt8
*)cmsEncoder
->encoderOut
.Data
,
1216 cmsEncoder
->encoderOut
.Length
);
1217 return errSecSuccess
;
1218 case EO_SignEncrypt
:
1219 /* proceed, more work to do */
1224 * Signing & encrypting.
1225 * Due to bugs in the libsecurity_smime encoder, it can't encode nested
1226 * ContentInfos in one shot. So we do another pass, specifying the SignedData
1227 * inside of the ContentInfo we just created as the data to encrypt.
1229 SecAsn1CoderRef asn1Coder
= NULL
;
1230 CSSM_DATA signedData
= {0, NULL
};
1232 ortn
= SecAsn1CoderCreate(&asn1Coder
);
1236 ortn
= cmsContentInfoContent(asn1Coder
, &cmsEncoder
->encoderOut
, &signedData
);
1241 /* now just encrypt that, one-shot */
1242 ortn
= CMSEncode(NULL
, /* no signers this time */
1243 cmsEncoder
->recipients
,
1244 &CSSMOID_PKCS7_SignedData
, /* fake out encoder so it doesn't try to actually
1245 * encode the signedData - this asserts the
1246 * SEC_OID_OTHER OID tag in the EnvelopedData's
1248 FALSE
, /* detachedContent */
1249 kCMSAttrNone
, /* signedAttributes - none this time */
1250 signedData
.Data
, signedData
.Length
,
1255 SecAsn1CoderRelease(asn1Coder
);
1260 #pragma mark --- High-level API ---
1263 * High-level, one-shot encoder function.
1267 CFTypeRef recipients
,
1268 const CSSM_OID
*eContentType
,
1269 Boolean detachedContent
,
1270 CMSSignedAttributes signedAttributes
,
1271 const void *content
,
1273 CFDataRef
*encodedContent
) /* RETURNED */
1275 if((signers
== NULL
) && (recipients
== NULL
)) {
1278 if(encodedContent
== NULL
) {
1282 CMSEncoderRef cmsEncoder
;
1285 /* set up the encoder */
1286 ortn
= CMSEncoderCreate(&cmsEncoder
);
1291 /* subsequent errors to errOut: */
1293 ortn
= CMSEncoderAddSigners(cmsEncoder
, signers
);
1299 ortn
= CMSEncoderAddRecipients(cmsEncoder
, recipients
);
1305 ortn
= CMSEncoderSetEncapsulatedContentType(cmsEncoder
, eContentType
);
1310 if(detachedContent
) {
1311 ortn
= CMSEncoderSetHasDetachedContent(cmsEncoder
, detachedContent
);
1316 if(signedAttributes
) {
1317 ortn
= CMSEncoderAddSignedAttributes(cmsEncoder
, signedAttributes
);
1323 ortn
= CMSEncoderUpdateContent(cmsEncoder
, content
, contentLen
);
1327 ortn
= CMSEncoderCopyEncodedContent(cmsEncoder
, encodedContent
);
1330 CFRelease(cmsEncoder
);
1334 OSStatus
CMSEncodeContent(
1336 CFTypeRef recipients
,
1337 CFTypeRef eContentTypeOID
,
1338 Boolean detachedContent
,
1339 CMSSignedAttributes signedAttributes
,
1340 const void *content
,
1342 CFDataRef
*encodedContentOut
) /* RETURNED */
1344 // convert eContentTypeOID to a CSSM_OID
1345 CSSM_OID contentType
= { 0, NULL
};
1346 if (eContentTypeOID
&& convertOid(eContentTypeOID
, &contentType
) != 0)
1348 const CSSM_OID
*contentTypePtr
= (eContentTypeOID
) ? &contentType
: NULL
;
1349 OSStatus result
= CMSEncode(signers
, recipients
, contentTypePtr
,
1350 detachedContent
, signedAttributes
,
1351 content
, contentLen
, encodedContentOut
);
1352 if (contentType
.Data
)
1353 free(contentType
.Data
);
1357 #pragma mark --- SPI routines declared in CMSPrivate.h ---
1360 * Obtain the SecCmsMessageRef associated with a CMSEncoderRef.
1361 * If we don't have a SecCmsMessageRef yet, we create one now.
1362 * This is the only place where we go to state ES_Msg.
1364 OSStatus
CMSEncoderGetCmsMessage(
1365 CMSEncoderRef cmsEncoder
,
1366 SecCmsMessageRef
*cmsMessage
) /* RETURNED */
1368 if((cmsEncoder
== NULL
) || (cmsMessage
== NULL
)) {
1371 if(cmsEncoder
->cmsMsg
!= NULL
) {
1372 ASSERT(cmsEncoder
->encState
!= ES_Init
);
1373 *cmsMessage
= cmsEncoder
->cmsMsg
;
1374 return errSecSuccess
;
1377 OSStatus ortn
= cmsSetupCmsMsg(cmsEncoder
);
1381 *cmsMessage
= cmsEncoder
->cmsMsg
;
1383 /* Don't set up encoder yet; caller might do that via CMSEncoderSetEncoder */
1384 cmsEncoder
->encState
= ES_Msg
;
1385 return errSecSuccess
;
1389 * Optionally specify a SecCmsEncoderRef to use with a CMSEncoderRef.
1390 * If this is called, it must be called before the first call to
1391 * CMSEncoderUpdateContent(). The CMSEncoderRef takes ownership of the
1392 * incoming SecCmsEncoderRef.
1394 OSStatus
CMSEncoderSetEncoder(
1395 CMSEncoderRef cmsEncoder
,
1396 SecCmsEncoderRef encoder
)
1398 if((cmsEncoder
== NULL
) || (encoder
== NULL
)) {
1404 switch(cmsEncoder
->encState
) {
1406 /* No message, no encoder */
1407 ASSERT(cmsEncoder
->cmsMsg
== NULL
);
1408 ASSERT(cmsEncoder
->encoder
== NULL
);
1409 ortn
= cmsSetupCmsMsg(cmsEncoder
);
1413 /* drop thru to set encoder */
1415 /* cmsMsg but no encoder */
1416 ASSERT(cmsEncoder
->cmsMsg
!= NULL
);
1417 ASSERT(cmsEncoder
->encoder
== NULL
);
1418 cmsEncoder
->encoder
= encoder
;
1419 cmsEncoder
->encState
= ES_Updating
;
1420 cmsEncoder
->customCoder
= true; /* we won't see data */
1421 return errSecSuccess
;
1423 /* no can do, too late */
1429 * Obtain the SecCmsEncoderRef associated with a CMSEncoderRef.
1430 * Returns a NULL SecCmsEncoderRef if neither CMSEncoderSetEncoder nor
1431 * CMSEncoderUpdateContent() has been called.
1432 * The CMSEncoderRef retains ownership of the SecCmsEncoderRef.
1434 OSStatus
CMSEncoderGetEncoder(
1435 CMSEncoderRef cmsEncoder
,
1436 SecCmsEncoderRef
*encoder
) /* RETURNED */
1438 if((cmsEncoder
== NULL
) || (encoder
== NULL
)) {
1442 /* any state, whether we have an encoder or not is OK */
1443 *encoder
= cmsEncoder
->encoder
;
1444 return errSecSuccess
;
1447 #include <AssertMacros.h>
1450 * Obtain the timestamp of signer 'signerIndex' of a CMS message, if
1451 * present. This timestamp is an authenticated timestamp provided by
1452 * a timestamping authority.
1454 * Returns errSecParam if the CMS message was not signed or if signerIndex
1455 * is greater than the number of signers of the message minus one.
1457 * This cannot be called until after CMSEncoderCopyEncodedContent() is called.
1459 OSStatus
CMSEncoderCopySignerTimestamp(
1460 CMSEncoderRef cmsEncoder
,
1461 size_t signerIndex
, /* usually 0 */
1462 CFAbsoluteTime
*timestamp
) /* RETURNED */
1464 return CMSEncoderCopySignerTimestampWithPolicy(
1471 OSStatus
CMSEncoderCopySignerTimestampWithPolicy(
1472 CMSEncoderRef cmsEncoder
,
1473 CFTypeRef timeStampPolicy
,
1474 size_t signerIndex
, /* usually 0 */
1475 CFAbsoluteTime
*timestamp
) /* RETURNED */
1477 OSStatus status
= errSecParam
;
1478 SecCmsMessageRef cmsg
;
1479 SecCmsSignedDataRef signedData
= NULL
;
1480 int numContentInfos
= 0;
1482 require(cmsEncoder
&& timestamp
, xit
);
1483 require_noerr(CMSEncoderGetCmsMessage(cmsEncoder
, &cmsg
), xit
);
1484 numContentInfos
= SecCmsMessageContentLevelCount(cmsg
);
1485 for (int dex
= 0; !signedData
&& dex
< numContentInfos
; dex
++)
1487 SecCmsContentInfoRef ci
= SecCmsMessageContentLevel(cmsg
, dex
);
1488 SECOidTag tag
= SecCmsContentInfoGetContentTypeTag(ci
);
1489 if (tag
== SEC_OID_PKCS7_SIGNED_DATA
)
1490 if ((signedData
= SecCmsSignedDataRef(SecCmsContentInfoGetContent(ci
))))
1491 if (SecCmsSignerInfoRef signerInfo
= SecCmsSignedDataGetSignerInfo(signedData
, (int)signerIndex
))
1493 status
= SecCmsSignerInfoGetTimestampTimeWithPolicy(signerInfo
, timeStampPolicy
, timestamp
);