26#ifndef WOLF_CRYPT_PKCS7_H
27#define WOLF_CRYPT_PKCS7_H
50#ifndef MAX_PKCS7_CERTS
51 #define MAX_PKCS7_CERTS 4
54#ifndef MAX_ORI_TYPE_SZ
55 #define MAX_ORI_TYPE_SZ MAX_OID_SZ
57#ifndef MAX_ORI_VALUE_SZ
58 #define MAX_ORI_VALUE_SZ 512
61#ifndef MAX_SIGNED_ATTRIBS_SZ
62 #define MAX_SIGNED_ATTRIBS_SZ 7
65#ifndef MAX_AUTH_ATTRIBS_SZ
66 #define MAX_AUTH_ATTRIBS_SZ 7
69#ifndef MAX_UNAUTH_ATTRIBS_SZ
70 #define MAX_UNAUTH_ATTRIBS_SZ 7
79 SIGNED_AND_ENVELOPED_DATA = 654,
82#if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
83 COMPRESSED_DATA = 678,
85 FIRMWARE_PKG_DATA = 685,
86 AUTH_ENVELOPED_DATA = 692
99 WC_PKCS7_VERIFY_STAGE2,
100 WC_PKCS7_VERIFY_STAGE3,
101 WC_PKCS7_VERIFY_STAGE4,
102 WC_PKCS7_VERIFY_STAGE5,
103 WC_PKCS7_VERIFY_STAGE6,
106 WC_PKCS7_INFOSET_START,
107 WC_PKCS7_INFOSET_BER,
108 WC_PKCS7_INFOSET_STAGE1,
109 WC_PKCS7_INFOSET_STAGE2,
110 WC_PKCS7_INFOSET_END,
124 WC_PKCS7_AUTHENV_ATRB,
125 WC_PKCS7_AUTHENV_ATRBEND,
129 WC_PKCS7_DECRYPT_KTRI,
130 WC_PKCS7_DECRYPT_KTRI_2,
131 WC_PKCS7_DECRYPT_KTRI_3,
134 WC_PKCS7_DECRYPT_KARI,
135 WC_PKCS7_DECRYPT_KEKRI,
136 WC_PKCS7_DECRYPT_PWRI,
137 WC_PKCS7_DECRYPT_ORI,
139 WC_PKCS7_DECRYPT_DONE,
145 MAX_ENCRYPTED_KEY_SZ = 512,
146 MAX_CONTENT_KEY_LEN = 32,
147 MAX_CONTENT_IV_SIZE = 16,
149 MAX_CONTENT_BLOCK_LEN = AES_BLOCK_SIZE,
151 MAX_CONTENT_BLOCK_LEN = DES_BLOCK_SIZE,
153 MAX_RECIP_SZ = MAX_VERSION_SZ +
154 MAX_SEQ_SZ + ASN_NAME_MAX + MAX_SN_SZ +
155 MAX_SEQ_SZ + MAX_ALGO_SZ + 1 + MAX_ENCRYPTED_KEY_SZ,
156#if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
157 (HAVE_FIPS_VERSION >= 2)) || defined(HAVE_SELFTEST)
161 GCM_NONCE_MID_SZ = 12,
162 CCM_NONCE_MIN_SZ = 7,
168 CMS_ISSUER_AND_SERIAL_NUMBER = 2,
170#define DEGENERATE_SID 3
173enum Pkcs7_RecipientInfo_Types {
197typedef struct PKCS7State PKCS7State;
198typedef struct Pkcs7Cert Pkcs7Cert;
199typedef struct Pkcs7EncodedRecip Pkcs7EncodedRecip;
202typedef struct PKCS7SignerInfo PKCS7SignerInfo;
205typedef int (*CallbackOriDecrypt)(
PKCS7* pkcs7,
byte* oriType, word32 oriTypeSz,
206 byte* oriValue, word32 oriValueSz,
207 byte* decryptedKey, word32* decryptedKeySz,
209typedef int (*CallbackOriEncrypt)(
PKCS7* pkcs7,
byte* cek, word32 cekSz,
210 byte* oriType, word32* oriTypeSz,
211 byte* oriValue, word32* oriValueSz,
213typedef int (*CallbackDecryptContent)(
PKCS7* pkcs7,
int encryptOID,
214 byte* iv,
int ivSz,
byte* aad, word32 aadSz,
215 byte* authTag, word32 authTagSz,
byte* in,
216 int inSz,
byte* out,
void* ctx);
217typedef int (*CallbackWrapCEK)(
PKCS7* pkcs7,
byte* cek, word32 cekSz,
218 byte* keyId, word32 keyIdSz,
219 byte* originKey, word32 originKeySz,
220 byte* out, word32 outSz,
221 int keyWrapAlgo,
int type,
int dir);
223#if defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && !defined(NO_RSA)
225typedef int (*CallbackRsaSignRawDigest)(
PKCS7* pkcs7,
byte* digest,
226 word32 digestSz,
byte* out, word32 outSz,
227 byte* privateKey, word32 privateKeySz,
228 int devId,
int hashOID);
238 byte* contentDynamic;
247 byte* cert[MAX_PKCS7_CERTS];
258 word32 encryptionKeySz;
259 word32 unprotectedAttribsSz;
268 word32 signedAttribsSz;
275 byte issuerHash[KEYID_SIZE];
276 byte issuerSn[MAX_SN_SZ];
277 byte publicKey[MAX_RSA_INT_SZ + MAX_RSA_E_SZ];
278 word32 certSz[MAX_PKCS7_CERTS];
282 word16 noDegenerate:1;
285 byte contentType[MAX_OID_SZ];
286 word32 contentTypeSz;
291 byte issuerSubjKeyId[KEYID_SIZE];
293 Pkcs7EncodedRecip* recipList;
300 CallbackOriEncrypt oriEncryptCb;
301 CallbackOriDecrypt oriDecryptCb;
306 word32 authAttribsSz;
308 word32 unauthAttribsSz;
310#ifndef NO_PKCS7_STREAM
315 word16 skipDefaultSignedAttribs:1;
318 PKCS7SignerInfo* signerInfo;
319 CallbackDecryptContent decryptionCb;
320 CallbackWrapCEK wrapCEKCb;
327 word32 plainDigestSz;
328 word32 pkcs7DigestSz;
330#if defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && !defined(NO_RSA)
331 CallbackRsaSignRawDigest rsaSignRawDigestCb;
335 byte* cachedEncryptedContent;
336 word32 cachedEncryptedContentSz;
340WOLFSSL_API
PKCS7* wc_PKCS7_New(
void* heap,
int devId);
341WOLFSSL_API
int wc_PKCS7_Init(
PKCS7* pkcs7,
void* heap,
int devId);
343WOLFSSL_API
int wc_PKCS7_AddCertificate(
PKCS7* pkcs7,
byte* der, word32 derSz);
346WOLFSSL_API
int wc_PKCS7_GetAttributeValue(
PKCS7* pkcs7,
const byte* oid,
347 word32 oidSz,
byte* out, word32* outSz);
349WOLFSSL_API
int wc_PKCS7_SetSignerIdentifierType(
PKCS7* pkcs7,
int type);
350WOLFSSL_API
int wc_PKCS7_SetContentType(
PKCS7* pkcs7,
byte* contentType,
352WOLFSSL_API
int wc_PKCS7_GetPadSize(word32 inputSz, word32 blockSz);
353WOLFSSL_API
int wc_PKCS7_PadData(
byte* in, word32 inSz,
byte* out, word32 outSz,
361WOLFSSL_API
int wc_PKCS7_SetDetached(
PKCS7* pkcs7, word16 flag);
362WOLFSSL_API
int wc_PKCS7_NoDefaultSignedAttribs(
PKCS7* pkcs7);
364 byte* output, word32 outputSz);
366 word32 hashSz,
byte* outputHead,
367 word32* outputHeadSz,
369 word32* outputFootSz);
370WOLFSSL_API
void wc_PKCS7_AllowDegenerate(
PKCS7* pkcs7, word16 flag);
372 byte* pkiMsg, word32 pkiMsgSz);
374 word32 hashSz,
byte* pkiMsgHead,
375 word32 pkiMsgHeadSz,
byte* pkiMsgFoot,
376 word32 pkiMsgFootSz);
378WOLFSSL_API
int wc_PKCS7_GetSignerSID(
PKCS7* pkcs7,
byte* out, word32* outSz);
381WOLFSSL_API
int wc_PKCS7_EncodeSignedFPD(
PKCS7* pkcs7,
byte* privateKey,
382 word32 privateKeySz,
int signOID,
383 int hashOID,
byte* content,
386 word32 signedAttribsSz,
byte* output,
388#ifndef NO_PKCS7_ENCRYPTED_DATA
390WOLFSSL_API
int wc_PKCS7_EncodeSignedEncryptedFPD(
PKCS7* pkcs7,
391 byte* encryptKey, word32 encryptKeySz,
392 byte* privateKey, word32 privateKeySz,
393 int encryptOID,
int signOID,
394 int hashOID,
byte* content,
397 word32 unprotectedAttribsSz,
399 word32 signedAttribsSz,
400 byte* output, word32 outputSz);
402#if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
404WOLFSSL_API
int wc_PKCS7_EncodeSignedCompressedFPD(
PKCS7* pkcs7,
405 byte* privateKey, word32 privateKeySz,
406 int signOID,
int hashOID,
407 byte* content, word32 contentSz,
409 word32 signedAttribsSz,
byte* output,
412#ifndef NO_PKCS7_ENCRYPTED_DATA
414WOLFSSL_API
int wc_PKCS7_EncodeSignedEncryptedCompressedFPD(
PKCS7* pkcs7,
415 byte* encryptKey, word32 encryptKeySz,
416 byte* privateKey, word32 privateKeySz,
417 int encryptOID,
int signOID,
418 int hashOID,
byte* content,
421 word32 unprotectedAttribsSz,
423 word32 signedAttribsSz,
424 byte* output, word32 outputSz);
429WOLFSSL_API
int wc_PKCS7_AddRecipient_KTRI(
PKCS7* pkcs7,
const byte* cert,
430 word32 certSz,
int options);
431WOLFSSL_API
int wc_PKCS7_AddRecipient_KARI(
PKCS7* pkcs7,
const byte* cert,
432 word32 certSz,
int keyWrapOID,
433 int keyAgreeOID,
byte* ukm,
434 word32 ukmSz,
int options);
436WOLFSSL_API
int wc_PKCS7_SetKey(
PKCS7* pkcs7,
byte* key, word32 keySz);
437WOLFSSL_API
int wc_PKCS7_AddRecipient_KEKRI(
PKCS7* pkcs7,
int keyWrapOID,
438 byte* kek, word32 kekSz,
439 byte* keyID, word32 keyIdSz,
440 void* timePtr,
byte* otherOID,
441 word32 otherOIDSz,
byte* other,
442 word32 otherSz,
int options);
444WOLFSSL_API
int wc_PKCS7_SetPassword(
PKCS7* pkcs7,
byte* passwd, word32 pLen);
445WOLFSSL_API
int wc_PKCS7_AddRecipient_PWRI(
PKCS7* pkcs7,
byte* passwd,
446 word32 pLen,
byte* salt,
447 word32 saltSz,
int kdfOID,
448 int prfOID,
int iterations,
449 int kekEncryptOID,
int options);
450WOLFSSL_API
int wc_PKCS7_SetOriEncryptCtx(
PKCS7* pkcs7,
void* ctx);
451WOLFSSL_API
int wc_PKCS7_SetOriDecryptCtx(
PKCS7* pkcs7,
void* ctx);
452WOLFSSL_API
int wc_PKCS7_SetOriDecryptCb(
PKCS7* pkcs7, CallbackOriDecrypt cb);
453WOLFSSL_API
int wc_PKCS7_AddRecipient_ORI(
PKCS7* pkcs7, CallbackOriEncrypt cb,
455WOLFSSL_API
int wc_PKCS7_SetWrapCEKCb(
PKCS7* pkcs7,
456 CallbackWrapCEK wrapCEKCb);
458#if defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && !defined(NO_RSA)
459WOLFSSL_API
int wc_PKCS7_SetRsaSignRawDigestCb(
PKCS7* pkcs7,
460 CallbackRsaSignRawDigest cb);
465 byte* output, word32 outputSz);
467 word32 pkiMsgSz,
byte* output,
471WOLFSSL_API
int wc_PKCS7_EncodeAuthEnvelopedData(
PKCS7* pkcs7,
472 byte* output, word32 outputSz);
473WOLFSSL_API
int wc_PKCS7_DecodeAuthEnvelopedData(
PKCS7* pkcs7,
byte* pkiMsg,
474 word32 pkiMsgSz,
byte* output,
478#ifndef NO_PKCS7_ENCRYPTED_DATA
479WOLFSSL_API
int wc_PKCS7_EncodeEncryptedData(
PKCS7* pkcs7,
480 byte* output, word32 outputSz);
481WOLFSSL_API
int wc_PKCS7_DecodeEncryptedData(
PKCS7* pkcs7,
byte* pkiMsg,
482 word32 pkiMsgSz,
byte* output,
484WOLFSSL_API
int wc_PKCS7_SetDecodeEncryptedCb(
PKCS7* pkcs7,
485 CallbackDecryptContent decryptionCb);
486WOLFSSL_API
int wc_PKCS7_SetDecodeEncryptedCtx(
PKCS7* pkcs7,
void* ctx);
490#if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
491WOLFSSL_API
int wc_PKCS7_EncodeCompressedData(
PKCS7* pkcs7,
byte* output,
493WOLFSSL_API
int wc_PKCS7_DecodeCompressedData(
PKCS7* pkcs7,
byte* pkiMsg,
494 word32 pkiMsgSz,
byte* output,
WOLFSSL_API int wc_PKCS7_InitWithCert(PKCS7 *pkcs7, byte *cert, word32 certSz)
This function initializes a PKCS7 structure with a DER-formatted certificate. To initialize an empty ...
Definition pkcs7.c:915
WOLFSSL_API void wc_PKCS7_Free(PKCS7 *pkcs7)
This function releases any memory allocated by a PKCS7 initializer.
Definition pkcs7.c:1133
WOLFSSL_API int wc_PKCS7_EncodeData(PKCS7 *pkcs7, byte *output, word32 outputSz)
This function builds the PKCS7 data content type, encoding the PKCS7 structure into a buffer containi...
Definition pkcs7.c:1274
WOLFSSL_API int wc_PKCS7_VerifySignedData_ex(PKCS7 *pkcs7, const byte *hashBuf, word32 hashSz, byte *pkiMsgHead, word32 pkiMsgHeadSz, byte *pkiMsgFoot, word32 pkiMsgFootSz)
This function takes in a transmitted PKCS7 signed data message as hash/header/footer,...
Definition pkcs7.c:5046
WOLFSSL_API int wc_PKCS7_VerifySignedData(PKCS7 *pkcs7, byte *pkiMsg, word32 pkiMsgSz)
This function takes in a transmitted PKCS7 signed data message, extracts the certificate list and cer...
Definition pkcs7.c:5054
WOLFSSL_API int wc_PKCS7_EncodeEnvelopedData(PKCS7 *pkcs7, byte *output, word32 outputSz)
This function builds the PKCS7 enveloped data content type, encoding the PKCS7 structure into a buffe...
Definition pkcs7.c:7691
WOLFSSL_API int wc_PKCS7_EncodeSignedData(PKCS7 *pkcs7, byte *output, word32 outputSz)
This function builds the PKCS7 signed data content type, encoding the PKCS7 structure into a buffer c...
Definition pkcs7.c:2589
WOLFSSL_API int wc_PKCS7_EncodeSignedData_ex(PKCS7 *pkcs7, const byte *hashBuf, word32 hashSz, byte *outputHead, word32 *outputHeadSz, byte *outputFoot, word32 *outputFootSz)
This function builds the PKCS7 signed data content type, encoding the PKCS7 structure into a header a...
Definition pkcs7.c:2510
WOLFSSL_API int wc_PKCS7_DecodeEnvelopedData(PKCS7 *pkcs7, byte *pkiMsg, word32 pkiMsgSz, byte *output, word32 outputSz)
This function unwraps and decrypts a PKCS7 enveloped data content type, decoding the message into out...
Definition pkcs7.c:10022