- Signature(const Stuff &stuff, const Buffer &data, const std::string &xml) {
- value_ = PKCS7_new();
- if(value_ == NULL){
- fprintf(stderr, "ldid: An error occured while getting creating PKCS7 file: %s\n", ERR_error_string(ERR_get_error(), NULL));
- exit(1);
+ Signature(const Stuff &stuff, const Buffer &data, const std::string &xml,const std::vector<char>& alternateCDSHA256) {
+ //
+ int flags = CMS_PARTIAL | CMS_DETACHED | CMS_NOSMIMECAP | CMS_BINARY;
+ //--------------------------------------------
+ auto issuer_name(X509_get_issuer_name(stuff));
+ _assert(issuer_name != NULL);
+ std::string issuer;
+ auto index(X509_NAME_get_index_by_NID(issuer_name, NID_commonName, -1));
+ _assert(index >= 0);
+ auto next(X509_NAME_get_index_by_NID(issuer_name, NID_commonName, index));
+ _assert(next == -1);
+ auto entry(X509_NAME_get_entry(issuer_name, index));
+ _assert(entry != NULL);
+ auto asn(X509_NAME_ENTRY_get_data(entry));
+ _assert(asn != NULL);
+ issuer.assign(reinterpret_cast<const char *>(ASN1_STRING_get0_data(asn)), ASN1_STRING_length(asn));
+
+ CMS_ContentInfo *stream = CMS_sign(NULL, NULL, stuff, NULL, flags);
+
+ CMS_SignerInfo *info = CMS_add1_signer(stream, stuff, stuff, EVP_sha256(), flags);
+
+
+ // Hash Agility
+ ASN1_OBJECT *obj = OBJ_txt2obj("1.2.840.113635.100.9.1", 1);
+ CMS_signed_add1_attr_by_OBJ(info, obj, 0x4, xml.c_str(), (int)xml.size());
+
+ // CDHashes (iOS 15.1+)
+ std::string sha256;
+ for (size_t i = 0; i < alternateCDSHA256.size(); i++)
+ {
+ char buf[16] = {0};
+ sprintf(buf, "%02X", (uint8_t)alternateCDSHA256[i]);
+ sha256 += buf;