TrustCore SDK NanoCert API reference  version 7.0
cms.h
Go to the documentation of this file.
1 /*
2  * cms.h
3  *
4  * CMS Parser and utilities routines
5  *
6  * Copyright 2019-2024 DigiCert, Inc. All Rights Reserved.
7  * Proprietary and Confidential Material.
8  *
9  */
19 #ifndef __CMS_HEADER__
20 #define __CMS_HEADER__
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 
27 /* Content type of a received CMS cf. CMS_getContentType() */
28 typedef enum CMS_ContentType
29 {
30  E_CMS_undetermined = 0,
31  E_CMS_data = 1,
32  E_CMS_signedData = 2,
33  E_CMS_envelopedData = 3,
34  /* E_PCKS7S_signedAndEnvelopedData = 4, */
35  E_CMS_digestedData = 5,
36  E_CMS_encryptedData = 6,
37  E_CMS_ct_authData = 102,
38 } CMS_ContentType;
39 
47 typedef void* CMS_context; /* opaque structure used when parsing a CMS */
48 
49 /* opaque structures used when creating a CMS */
57 typedef void* CMS_signedDataContext;
65 typedef void* CMS_signerInfo;
74 
75 
76 #define NO_TAG (0xFFFFFFFF)
77 
78 /* data structures used in callbacks */
101 typedef struct CMSIssuerSerialNumber
102 {
103  ASN1_ITEMPTR pIssuer;
104  ASN1_ITEMPTR pSerialNumber;
106 
154 {
155  ubyte4 type;
156  union
157  {
158  CMSIssuerSerialNumber issuerAndSerialNumber; /* type = NO_TAG */
159  ASN1_ITEMPTR subjectKeyIdentifier; /* type = 0 OCTETSTRING */
160  } u;
162 
163 
208 {
209  ASN1_ITEMPTR pAlgoOID; /* AlgorithmIdentifier: algorithm OID */
210  ASN1_ITEMPTR pAlgoParameters; /* AlgorithmIdentifier: parameters ANY */
211  ASN1_ITEMPTR pPublicKey; /* BIT STRING */
213 
271 {
278  ubyte4 type;
283  union
284  {
285  CMSIssuerSerialNumber issuerAndSerialNumber; /* type = NO_TAG */
286  ASN1_ITEMPTR subjectKeyIdentifier; /* type = 0 OCTETSTRING */
287  CMSOriginatorPublicKey originatorKey; /* type = 1 */
288  } u;
290 
291 #if 0 /* this is not supported yet */
292 typedef struct CMSKEKRecipientId
293 {
294  ASN1_ITEMPTR keyIdentifer;
295  ASN1_ITEMPTR date; /* can be NULL */
296  ASN1_ITEMPTR other; /* can be NULL */
297 } CMSKEKRecipientId;
298 #endif
299 
300 /* data structure used in the CMS_GetPrivateKey callback. The callback implementer
301 should use the content of this structure to determine which key is requested */
342 typedef struct CMSRecipientId
343 {
344  ubyte4 type;
345  union
346  {
347  CMSKeyTransRecipientId ktrid; /* type = NO_TAG */
348  CMSKeyAgreeRecipientId karid; /* type = 1 */
349 #if 0
350  CMSKEKRecipientId kekrid; /* type = 2 */
351  CMSPasswordRecipientId pwrdi; /* type = 3 */
352  CMSOtherRecipientId orid; /* type = 4 */
353 #endif
354  } ri;
356 
357 /* this callback is used to retrieve the private key that */
358 /* corresponds to a CMSRecipientId */
423 typedef MSTATUS (*CMS_GetPrivateKey)(const void* arg,
424  CStream cs,
425  const CMSRecipientId* pRecipientId,
426  AsymmetricKey* pKey);
427 
428 /* this callback is used to verify that this certificate is recognized
429 as valid */
467 typedef MSTATUS (*CMS_ValidateRootCertificate)(const void* arg,
468  CStream cs,
469  ASN1_ITEM* pCertificate);
470 
471 /* this callback is used to get a certificate given the issuer name and
472 serial number */
510 typedef MSTATUS (*CMS_GetCertificate)(const void* arg, CStream cs,
511  ASN1_ITEM* pSerialNumber,
512  ASN1_ITEM* pIssuerName,
513  ubyte** ppCertificate,
514  ubyte4* certificateLen);
515 
516 /* all the callbacks that the CMS parser might need. */
530 typedef struct CMS_Callbacks
531 {
547 
548 } CMS_Callbacks;
549 
576 typedef MSTATUS (*CMS_SignData)(void *pCbInfo,
577  const ubyte *digestAlgoOID,
578  const ubyte *pDataToSign,
579  ubyte4 dataToSignLen,
580  ubyte *pSigBuffer,
581  ubyte4 sigBufferLen);
582 
630 MOC_EXTERN MSTATUS CMS_newContext(CMS_context* pNewContext,
631  const void* callbackArg,
632  const CMS_Callbacks* pCallbacks);
633 
668 MOC_EXTERN MSTATUS CMS_updateContext( CMS_context context, const ubyte* input,
669  ubyte4 inputLen, ubyte** ppOutput,
670  ubyte4* pOutputLen, intBoolean* pFinished);
671 
695 MOC_EXTERN MSTATUS CMS_deleteContext( CMS_context* pContext);
696 
697 /*======== other functions to query the context for more information =======*/
698 
699 /* These functions can return OK (success), ERR_EOF (more data must be provided by calling
700 CMS_updateContext) or some other error message (invalid data) */
704 MOC_EXTERN MSTATUS CMS_createContentInfo(const ubyte* contentType, DER_ITEMPTR *ppContentInfo, DER_ITEMPTR *ppContent);
737 MOC_EXTERN MSTATUS CMS_getContentType( CMS_context context, CMS_ContentType* cmsContentType);
738 
774 MOC_EXTERN MSTATUS CMS_getContentTypeOnly( CMS_context context,
775  const ubyte* pInput,
776  ubyte4 inputLen, CMS_ContentType* cType);
777 
809 MOC_EXTERN MSTATUS CMS_getEncapContentType( CMS_context context, ubyte** ppOID);
810 
811 /********* EnvelopedData recipients **********/
812 
842 MOC_EXTERN MSTATUS CMS_getNumRecipients( CMS_context context,
843  sbyte4* numRecipients);
844 
890 MOC_EXTERN MSTATUS CMS_getRecipientInfo( CMS_context context,
891  sbyte4 recipientIndexZeroBased,
892  const ASN1_ITEM** pRecipientInfo,
893  CStream* pCS);
894 
925 MOC_EXTERN MSTATUS CMS_getDecryptingRecipient( CMS_context context,
926  sbyte4* recipientIndexZeroBased);
927 
963 MOC_EXTERN MSTATUS CMS_getEncryptionAlgo( CMS_context context,
964  ubyte** ppEncryptionAlgoOID);
965 
966 /******* SignedData signers **********/
967 
1001 MOC_EXTERN MSTATUS CMS_getNumSigners( CMS_context context,
1002  sbyte4* numSigners);
1003 
1041 MOC_EXTERN MSTATUS CMS_getSignerInfo( CMS_context context,
1042  sbyte4 index,
1043  const ASN1_ITEM** ppRecipientInfo,
1044  CStream* pCS);
1045 
1089 MOC_EXTERN MSTATUS CMS_getReceiptInfo( const ubyte* receipt, ubyte4 receiptLen,
1090  const ubyte** messageId, ubyte4* messageIdLen,
1091  const ubyte** signature, ubyte4* signatureLen);
1092 
1121 MOC_EXTERN MSTATUS CMS_getReceiptMsgDigest( CMS_context context,
1122  const ubyte** ppDigest, ubyte4* pDigestLen);
1123 
1164 MOC_EXTERN MSTATUS CMS_getFirstCertificate( CMS_context context,
1165  const ASN1_ITEM** ppCertificate,
1166  CStream* pCS);
1167 
1196 MOC_EXTERN MSTATUS CMS_detachedSignature(CMS_context context, intBoolean* detached);
1197 
1235 MOC_EXTERN MSTATUS CMS_setDetachedSignatureData( CMS_context context, const ubyte* payload,
1236  ubyte4 payloadLen, intBoolean final);
1237 
1295 MOC_EXTERN MSTATUS CMS_createSignedReceipt( CMS_context context,
1296  sbyte4 index,
1297  RNGFun rngFun, void* rngFunArg,
1298  const ubyte* signerCert, ubyte4 signerCertLen,
1299  const AsymmetricKey* pKey, const ubyte* hashAlgoOID,
1300  ubyte** ppReceipt, ubyte4* pReceiptLen);
1301 
1302 /************ CREATING CMS API **************************************/
1303 /* 2 distinct APIs are provided for creating CMS, one for signed data and the other
1304 for enveloped data */
1305 
1306 
1366 MOC_EXTERN MSTATUS CMS_signedNewContext( CMS_signedDataContext* pNewCtx,
1367  const ubyte* payloadTypeOID,
1368  intBoolean detached, RNGFun rngFun,
1369  void* rngFunArg);
1370 
1398 MOC_EXTERN MSTATUS CMS_signedAddCertificate( CMS_signedDataContext myCtx, const ubyte* cert,
1399  ubyte4 certLen);
1400 
1427 MOC_EXTERN MSTATUS CMS_signedAddCRL( CMS_signedDataContext myCtx, const ubyte* crl,
1428  ubyte4 crlLen);
1429 
1430 /* flags in CMS_signedAddSigner are a combination of the following values */
1431 enum {
1432  e_cms_signer_addCert = 0x0001, /* add the certificate to the CMS */
1433  e_cms_signer_forceAuthAttr = 0x0002 /* this signer wants to add some authenticated attributes */
1434 };
1435 
1477 MOC_EXTERN MSTATUS CMS_signedAddSigner( CMS_signedDataContext myCtx,
1478  const ubyte* cert,
1479  ubyte4 certLen,
1480  const AsymmetricKey* pKey,
1481  const ubyte* digestAlgoOID,
1482  ubyte4 flags,
1483  CMS_signerInfo* pNewSignerInfo);
1484 
1485 
1529 MOC_EXTERN MSTATUS CMS_signedAddSignerWithCallback( MOC_HW(hwAccelDescr hwAccelCtx)
1530  CMS_signedDataContext myCtx,
1531  const ubyte* cert,
1532  ubyte4 certLen,
1533  CMS_SignData signCallback,
1534  void* pCbInfo,
1535  const ubyte* digestAlgoOID,
1536  ubyte4 flags,
1537  CMS_signerInfo* pNewSignerInfo);
1596 MOC_EXTERN MSTATUS CMS_signedAddSignerAttribute( CMS_signedDataContext myCtx,
1597  CMS_signerInfo signerInfo,
1598  const ubyte* typeOID,
1599  ubyte4 type, /* id|tag */
1600  const ubyte* value,
1601  ubyte4 valueLen,
1602  intBoolean authenticated);
1603 
1635 MOC_EXTERN MSTATUS CMS_signedAddReceiptRequest( MOC_HASH(hwAccelDescr hwAccelCtx)
1636  CMS_signedDataContext myCtx,
1637  const ubyte** receiptFrom, /* Array of recipient email addresses from which receipts are requested*/
1638  sbyte4 numReceiptFrom, /* -1 for all, 0 for not on mailing list or > 0 to use the receiptFrom arg */
1639  const ubyte** receiptTo, /* Array of email addresses that receipts are to be sent to */
1640  sbyte4 numReceiptTo);
1641 
1689 MOC_EXTERN MSTATUS CMS_signedGetRequestInfo( CMS_signedDataContext myCtx,
1690  CMS_signerInfo signerInfo,
1691  const ubyte** messageId, ubyte4* messageIdLen,
1692  const ubyte** digest, ubyte4* digestLen,
1693  const ubyte** signature, ubyte4* signatureLen);
1694 
1732 MOC_EXTERN MSTATUS CMS_signedUpdateContext( MOC_ASYM(hwAccelDescr hwAccelCtx)
1733  CMS_signedDataContext myCtx,
1734  const ubyte* data, ubyte4 dataLen,
1735  ubyte** ppOutput, ubyte4* pOutputLen,
1736  intBoolean finished);
1737 
1763 MOC_EXTERN MSTATUS CMS_signedDeleteContext(MOC_HASH(hwAccelDescr hwAccelCtx)
1764  CMS_signedDataContext* ppCtx);
1765 
1766 
1822 MOC_EXTERN MSTATUS CMS_envelopedNewContext( CMS_envelopedDataContext* pNewCtx,
1823  const ubyte* encryptAlgoOID,
1824  RNGFun rngFun, void* rngFunArg);
1825 
1854 MOC_EXTERN MSTATUS CMS_envelopedAddRecipient( CMS_envelopedDataContext myCtx,
1855  const ubyte* cert, ubyte4 certLen);
1856 
1857 
1888 MOC_EXTERN MSTATUS CMS_envelopedAddUnauthAttribute( CMS_envelopedDataContext myCtx,
1889  const ubyte* typeOID,
1890  ubyte4 type, /* id|tag */
1891  const ubyte* value,
1892  ubyte4 valueLen);
1893 
1931 MOC_EXTERN MSTATUS CMS_envelopedUpdateContext(MOC_HW(hwAccelDescr hwAccelCtx)
1932  CMS_envelopedDataContext myCtx,
1933  const ubyte* data, ubyte4 dataLen,
1934  ubyte** ppOutput, ubyte4* pOutputLen,
1935  intBoolean finished);
1936 
1962 MOC_EXTERN MSTATUS CMS_envelopedDeleteContext(MOC_SYM(hwAccelDescr hwAccelCtx)
1963  CMS_envelopedDataContext* ppCtx);
1964 
1965 
1966 #ifdef __cplusplus
1967 }
1968 #endif
1969 
1970 #endif /*#ifndef __CMS_HEADER__ */
MOC_EXTERN MSTATUS CMS_getSignerInfo(CMS_context context, sbyte4 index, const ASN1_ITEM **ppRecipientInfo, CStream *pCS)
Get the SignerInfo object for a given verified signer.
MOC_EXTERN MSTATUS CMS_envelopedAddRecipient(CMS_envelopedDataContext myCtx, const ubyte *cert, ubyte4 certLen)
Add a recipient, identified by its DER-encoded certificate, to a CMS EnvelopedData object...
MOC_EXTERN MSTATUS CMS_createSignedReceipt(CMS_context context, sbyte4 index, RNGFun rngFun, void *rngFunArg, const ubyte *signerCert, ubyte4 signerCertLen, const AsymmetricKey *pKey, const ubyte *hashAlgoOID, ubyte **ppReceipt, ubyte4 *pReceiptLen)
Create a signed Receipt on the given message for a given signer.
Union that identifies a recipient for whom a transient key is wanted.
Definition: cms.h:153
MOC_EXTERN MSTATUS CMS_getEncapContentType(CMS_context context, ubyte **ppOID)
Get the OID (with length prefix) of the encapsulated content type.
MOC_EXTERN MSTATUS CMS_signedAddSignerWithCallback(MOC_HW(hwAccelDescr hwAccelCtx) CMS_signedDataContext myCtx, const ubyte *cert, ubyte4 certLen, CMS_SignData signCallback, void *pCbInfo, const ubyte *digestAlgoOID, ubyte4 flags, CMS_signerInfo *pNewSignerInfo)
Add a signer to a given SignedData object when the private key is unavailable.
MOC_EXTERN MSTATUS CMS_signedAddSignerAttribute(CMS_signedDataContext myCtx, CMS_signerInfo signerInfo, const ubyte *typeOID, ubyte4 type, const ubyte *value, ubyte4 valueLen, intBoolean authenticated)
Add an attribute (authenticated or non-authenticated) to a CMS SignedData object's signer(s)...
MOC_EXTERN MSTATUS CMS_deleteContext(CMS_context *pContext)
Free a CMS context structure.
MSTATUS(* CMS_SignData)(void *pCbInfo, const ubyte *digestAlgoOID, const ubyte *pDataToSign, ubyte4 dataToSignLen, ubyte *pSigBuffer, ubyte4 sigBufferLen)
This callback is used to perform a signature operation.
Definition: cms.h:576
MOC_EXTERN MSTATUS CMS_getEncryptionAlgo(CMS_context context, ubyte **ppEncryptionAlgoOID)
Get the OID (with prefix length) of the encryption algorithm of a given CMS_context.
CMS recipient identification information.
Definition: cms.h:342
MOC_EXTERN MSTATUS CMS_signedUpdateContext(MOC_ASYM(hwAccelDescr hwAccelCtx) CMS_signedDataContext myCtx, const ubyte *data, ubyte4 dataLen, ubyte **ppOutput, ubyte4 *pOutputLen, intBoolean finished)
Add data to a CMS SignedData object.
Union that identifies a recipient for whom a transient key is wanted.
Definition: cms.h:207
MOC_EXTERN MSTATUS CMS_signedAddCertificate(CMS_signedDataContext myCtx, const ubyte *cert, ubyte4 certLen)
Add a signed certificate to a CMS SignedData object.
MOC_EXTERN MSTATUS CMS_newContext(CMS_context *pNewContext, const void *callbackArg, const CMS_Callbacks *pCallbacks)
Create a CMS context structure for parsing a received CMS object.
MOC_EXTERN MSTATUS CMS_getContentType(CMS_context context, CMS_ContentType *cmsContentType)
Get the ContentType (CMS_contentType enumerated value from cms.h) of a given CMS object.
MOC_EXTERN MSTATUS CMS_envelopedNewContext(CMS_envelopedDataContext *pNewCtx, const ubyte *encryptAlgoOID, RNGFun rngFun, void *rngFunArg)
Create a CMS EnvelopedData object.
MOC_EXTERN MSTATUS CMS_getReceiptInfo(const ubyte *receipt, ubyte4 receiptLen, const ubyte **messageId, ubyte4 *messageIdLen, const ubyte **signature, ubyte4 *signatureLen)
Get message ID and signature of a given signed Receipt.
Information about a SignedData object's signer.
Definition: pkcs7.h:91
Pointers to functions required by the internal CMS parser.
Definition: cms.h:530
MOC_EXTERN MSTATUS CMS_getNumRecipients(CMS_context context, sbyte4 *numRecipients)
Get the number of recipients in a given CMS EnvelopedData object.
MOC_EXTERN MSTATUS CMS_getNumSigners(CMS_context context, sbyte4 *numSigners)
Get the number of verified signers of a given SignedData object.
MOC_EXTERN MSTATUS CMS_signedGetRequestInfo(CMS_signedDataContext myCtx, CMS_signerInfo signerInfo, const ubyte **messageId, ubyte4 *messageIdLen, const ubyte **digest, ubyte4 *digestLen, const ubyte **signature, ubyte4 *signatureLen)
Extract receipt request information.
ASN1_ITEMPTR pointers for a certificate Issuer ID and serial number.
Definition: cms.h:101
MOC_EXTERN MSTATUS CMS_signedAddSigner(CMS_signedDataContext myCtx, const ubyte *cert, ubyte4 certLen, const AsymmetricKey *pKey, const ubyte *digestAlgoOID, ubyte4 flags, CMS_signerInfo *pNewSignerInfo)
Add a signer to a given SignedData object.
MOC_EXTERN MSTATUS CMS_signedDeleteContext(MOC_HASH(hwAccelDescr hwAccelCtx) CMS_signedDataContext *ppCtx)
Delete and free a CMS SignedData object that was allocated by CMS_signedNewContext().
MOC_EXTERN MSTATUS CMS_getDecryptingRecipient(CMS_context context, sbyte4 *recipientIndexZeroBased)
Gets the index of the decrypting recipient in a given EnvelopedData object.
MOC_EXTERN MSTATUS CMS_signedNewContext(CMS_signedDataContext *pNewCtx, const ubyte *payloadTypeOID, intBoolean detached, RNGFun rngFun, void *rngFunArg)
Create a CMS SignedData object.
CMS_GetPrivateKey getPrivKeyFun
Pointer to the CMS_GetPrivateKey() callback function.
Definition: cms.h:536
ubyte4 type
Tells you how to interpret the u member of a CMSKeyAgreeRecipientId structure.
Definition: cms.h:278
void * CMS_signedDataContext
Definition: cms.h:57
MOC_EXTERN MSTATUS CMS_envelopedAddUnauthAttribute(CMS_envelopedDataContext myCtx, const ubyte *typeOID, ubyte4 type, const ubyte *value, ubyte4 valueLen)
Add an unauthenticated attribute to a CMS EnvelopedData object.
MOC_EXTERN MSTATUS CMS_setDetachedSignatureData(CMS_context context, const ubyte *payload, ubyte4 payloadLen, intBoolean final)
Add payload information to a CMS_context so that it can verify a detached signature.
MSTATUS(* CMS_GetCertificate)(const void *arg, CStream cs, ASN1_ITEM *pSerialNumber, ASN1_ITEM *pIssuerName, ubyte **ppCertificate, ubyte4 *certificateLen)
Return a message's certificate that matches a given serial number and issuer name.
Definition: cms.h:510
MSTATUS(* CMS_GetPrivateKey)(const void *arg, CStream cs, const CMSRecipientId *pRecipientId, AsymmetricKey *pKey)
Get the private key associated with a given certificate in a CMS message stream.
Definition: cms.h:423
MOC_EXTERN MSTATUS CMS_getRecipientInfo(CMS_context context, sbyte4 recipientIndexZeroBased, const ASN1_ITEM **pRecipientInfo, CStream *pCS)
Get offset and length information of a given RecipientInfo object in a given EnvelopedData object...
MOC_EXTERN MSTATUS CMS_updateContext(CMS_context context, const ubyte *input, ubyte4 inputLen, ubyte **ppOutput, ubyte4 *pOutputLen, intBoolean *pFinished)
Add data to a CMS_context object.
MOC_EXTERN MSTATUS CMS_getContentTypeOnly(CMS_context context, const ubyte *pInput, ubyte4 inputLen, CMS_ContentType *cType)
Get the ContentType (CMS_contentType enumerated value from cms.h) of a given data buffer input...
Union that identifies a CMS recipient.
Definition: cms.h:270
void * CMS_context
Definition: cms.h:47
CMS_ValidateRootCertificate valCertFun
Pointer to the CMS_ValidateRootCertificate() callback function.
Definition: cms.h:541
MOC_EXTERN MSTATUS CMS_signedAddCRL(CMS_signedDataContext myCtx, const ubyte *crl, ubyte4 crlLen)
Add a signed CRL to a CMS SignedData object.
MSTATUS(* CMS_ValidateRootCertificate)(const void *arg, CStream cs, ASN1_ITEM *pCertificate)
Validate the certificates in a CMS message.
Definition: cms.h:467
MOC_EXTERN MSTATUS CMS_signedAddReceiptRequest(MOC_HASH(hwAccelDescr hwAccelCtx) CMS_signedDataContext myCtx, const ubyte **receiptFrom, sbyte4 numReceiptFrom, const ubyte **receiptTo, sbyte4 numReceiptTo)
Request a receipt for a given message.
MOC_EXTERN MSTATUS CMS_envelopedDeleteContext(MOC_SYM(hwAccelDescr hwAccelCtx) CMS_envelopedDataContext *ppCtx)
Delete and free a CMS EnvelopedData object that was allocated by CMS_envelopedNewContext().
MOC_EXTERN MSTATUS CMS_envelopedUpdateContext(MOC_HW(hwAccelDescr hwAccelCtx) CMS_envelopedDataContext myCtx, const ubyte *data, ubyte4 dataLen, ubyte **ppOutput, ubyte4 *pOutputLen, intBoolean finished)
Add data to a CMS EnvelopedData object.
MOC_EXTERN MSTATUS CMS_detachedSignature(CMS_context context, intBoolean *detached)
Determine whether a given CMS_context is a detached signature.
CMS_GetCertificate getCertFun
Pointer to the CMS_GetCertificate() callback function.
Definition: cms.h:546
void * CMS_envelopedDataContext
Definition: cms.h:73
MOC_EXTERN MSTATUS CMS_getReceiptMsgDigest(CMS_context context, const ubyte **ppDigest, ubyte4 *pDigestLen)
Get the message digest of a given signed Receipt.
void * CMS_signerInfo
Definition: cms.h:65
MOC_EXTERN MSTATUS CMS_getFirstCertificate(CMS_context context, const ASN1_ITEM **ppCertificate, CStream *pCS)
Get the first certificate (its ASN1_ITEM structure) in a given CMS_context SignedData object...