Hi all. I've been able to parse the first counter signature of each signature. At the end of this e-mail, you'll find the code I use to parse the file (no error handling, it's just a working snippet).
Attached you'll find my test P7M. The signature/countersignature tree is as follows: SIGNATURE 1 COUNTER SIGNATURE 1 COUNTER SIGNATURE 2 SIGNATURE 2 COUNTER SIGNATURE 1 Can someone please give me an advice about the changes I've to make to the following code to parse the COUNTER SIGNATURE 2 too? Many thanks. #include <openssl/pkcs7.h> #include <openssl/bio.h> #include <openssl/err.h> #include <openssl/x509.h> void parse(char *pszFileName) { // Parsing PKCS#7 file BIO *foo = BIO_new_file(pszFileName, "rb"); PKCS7* pP7 = d2i_PKCS7_bio(foo,NULL); STACK_OF(X509) *pSigners = PKCS7_get0_signers(pP7, NULL, 0); STACK_OF(PKCS7_SIGNER_INFO) *pSignerInfos = PKCS7_get_signer_info(pP7); // Looping through the signatures for (int i = 0; i < sk_PKCS7_SIGNER_INFO_num(pSignerInfos); i++) { PKCS7_SIGNER_INFO *si = sk_PKCS7_SIGNER_INFO_value(pSignerInfos, i); X509* pSignerCert = sk_X509_value(pSigners, i); printf ("FOUND SIGNATURE : %p\n", si); // Parsing counter signatures ASN1_TYPE *pCounterSignatureAttribute = PKCS7_get_attribute(si, NID_pkcs9_countersignature); ASN1_STRING *pSequence = pCounterSignatureAttribute->value.sequence; if (pSequence != NULL) { unsigned char *ps = NULL; ps = pSequence->data; PKCS7_SIGNER_INFO *cs = d2i_PKCS7_SIGNER_INFO(NULL, (const unsigned char**)&ps, pSequence->length); printf ("FOUND COUNTER SIGNATURE: %p\n", si); } } } Regards, Massimiliano Ziccardi
test.txt.p7m
Description: S/MIME encrypted message