]> git.cameronkatri.com Git - ldid.git/blobdiff - ldid.cpp
ldid v2.1.5-procursus5
[ldid.git] / ldid.cpp
index ac4d17aa50e6134a850da324125ef41f043b4c8a..4d19c93e84bdd00b398739aeba788a064fbc7bd2 100644 (file)
--- a/ldid.cpp
+++ b/ldid.cpp
@@ -1716,7 +1716,7 @@ class Buffer {
         Buffer()
     {
         if (i2d_PKCS7_bio(bio_, pkcs) == 0){
-            fprintf(stderr, "ldid: An error occured while getting the PKCS12 file: %s\n", ERR_error_string(ERR_get_error(), NULL));
+            fprintf(stderr, "ldid: An error occured while getting the PKCS7 file: %s\n", ERR_error_string(ERR_get_error(), NULL));
             exit(1);
         }
     }
@@ -1855,14 +1855,18 @@ class Signature {
         X509_ATTRIBUTE *attribute = X509_ATTRIBUTE_new();
         ASN1_OBJECT *obj2 = OBJ_txt2obj("1.2.840.113635.100.9.2", 1);
         X509_ATTRIBUTE_set1_object(attribute, obj2);
-        // xina fix;
-        SEQUENCE_hash_sha1 seq1;
-        memcpy((void *)seq1.hash,(void *)alternateCDSHA1.data() ,alternateCDSHA1.size());
-        X509_ATTRIBUTE_set1_data(attribute, V_ASN1_SEQUENCE,&seq1, sizeof(seq1));
-        // xina fix;
-        SEQUENCE_hash_sha256 seq256;
-        memcpy((void *)seq256.hash,(void *)alternateCDSHA256.data() ,alternateCDSHA256.size());
-        X509_ATTRIBUTE_set1_data(attribute, V_ASN1_SEQUENCE,&seq256, sizeof(seq256));
+        if (alternateCDSHA1.size() != 0) {
+            // xina fix;
+            SEQUENCE_hash_sha1 seq1;
+            memcpy((void *)seq1.hash, (void *)alternateCDSHA1.data(), alternateCDSHA1.size());
+            X509_ATTRIBUTE_set1_data(attribute, V_ASN1_SEQUENCE, &seq1, sizeof(seq1));
+        }
+        if (alternateCDSHA256.size() != 0) {
+            // xina fix;
+            SEQUENCE_hash_sha256 seq256;
+            memcpy((void *)seq256.hash, (void *)alternateCDSHA256.data(), alternateCDSHA256.size());
+            X509_ATTRIBUTE_set1_data(attribute, V_ASN1_SEQUENCE, &seq256, sizeof(seq256));
+        }
 
         STACK_OF(X509_ATTRIBUTE) *sk = PKCS7_get_signed_attributes(info);
         if (!sk_X509_ATTRIBUTE_push(sk, attribute)) {
@@ -1938,8 +1942,8 @@ class HashBuffer :
         sha1_ = EVP_MD_CTX_new();
         sha256_ = EVP_MD_CTX_new();
         
-        EVP_DigestInit_ex2(sha1_, EVP_get_digestbyname("sha1"), nullptr);
-        EVP_DigestInit_ex2(sha256_, EVP_get_digestbyname("sha256"), nullptr);
+        EVP_DigestInit_ex(sha1_, EVP_get_digestbyname("sha1"), nullptr);
+        EVP_DigestInit_ex(sha256_, EVP_get_digestbyname("sha256"), nullptr);
     }
 
     ~HashBuffer() {
@@ -2089,7 +2093,7 @@ static void req(std::streambuf &buffer, uint8_t (&&data)[Size_]) {
     put(buffer, zeros, 3 - (Size_ + 3) % 4);
 }
 
-Hash Sign(const void *idata, size_t isize, std::streambuf &output, const std::string &identifier, const std::string &entitlements, bool merge, const std::string &requirements, const std::string &key, const Slots &slots, uint32_t flags, bool platform, const Progress &progress) {
+Hash Sign(const void *idata, size_t isize, std::streambuf &output, const std::string &identifier, const std::string &entitlements, bool merge, const std::string &requirements, const std::string &key, const Slots &slots, uint32_t flags, uint8_t platform, const Progress &progress) {
     Hash hash;
 
 
@@ -2323,7 +2327,7 @@ Hash Sign(const void *idata, size_t isize, std::streambuf &output, const std::st
             directory.nCodeSlots = Swap(normal);
             directory.hashSize = algorithm.size_;
             directory.hashType = algorithm.type_;
-            directory.platform = platform ? 0x01 : 0x00;
+            directory.platform = platform;
             directory.pageSize = PageShift_;
             directory.spare2 = Swap(uint32_t(0));
             directory.scatterOffset = Swap(uint32_t(0));
@@ -2793,7 +2797,7 @@ struct RuleCode {
     }
 };
 
-static Hash Sign(const uint8_t *prefix, size_t size, std::streambuf &buffer, Hash &hash, std::streambuf &save, const std::string &identifier, const std::string &entitlements, bool merge, const std::string &requirements, const std::string &key, const Slots &slots, size_t length, uint32_t flags, bool platform, const Progress &progress) {
+static Hash Sign(const uint8_t *prefix, size_t size, std::streambuf &buffer, Hash &hash, std::streambuf &save, const std::string &identifier, const std::string &entitlements, bool merge, const std::string &requirements, const std::string &key, const Slots &slots, size_t length, uint32_t flags, uint8_t platform, const Progress &progress) {
     // XXX: this is a miserable fail
     std::stringbuf temp;
     put(temp, prefix, size);
@@ -2818,7 +2822,7 @@ struct State {
     }
 };
 
-Bundle Sign(const std::string &root, Folder &parent, const std::string &key, State &local, const std::string &requirements, const Functor<std::string (const std::string &, const std::string &)> &alter, const Progress &progress) {
+Bundle Sign(const std::string &root, Folder &parent, const std::string &key, State &local, const std::string &requirements, const Functor<std::string (const std::string &, const std::string &)> &alter, bool merge, uint8_t platform, const Progress &progress) {
     std::string executable;
     std::string identifier;
 
@@ -2926,7 +2930,7 @@ Bundle Sign(const std::string &root, Folder &parent, const std::string &key, Sta
         State remote;
         bundles[nested[1]] = Sign(root + bundle, subfolder, key, remote, "", Starts(name, "PlugIns/") ? alter :
             static_cast<const Functor<std::string (const std::string &, const std::string &)> &>(fun([&](const std::string &, const std::string &) -> std::string { return entitlements; }))
-        , progress);
+        , merge, platform, progress);
         local.Merge(bundle, remote);
     }), fun([&](const std::string &name, const Functor<std::string ()> &read) {
     }));
@@ -2980,7 +2984,7 @@ Bundle Sign(const std::string &root, Folder &parent, const std::string &key, Sta
                     case MH_CIGAM: case MH_CIGAM_64:
                         folder.Save(name, true, flag, fun([&](std::streambuf &save) {
                             Slots slots;
-                            Sign(header.bytes, size, data, hash, save, identifier, "", false, "", key, slots, length, 0, false, Progression(progress, root + name));
+                            Sign(header.bytes, size, data, hash, save, identifier, "", false, "", key, slots, length, 0, platform, Progression(progress, root + name));
                         }));
                         return;
                 }
@@ -3110,16 +3114,16 @@ Bundle Sign(const std::string &root, Folder &parent, const std::string &key, Sta
             Slots slots;
             slots[1] = local.files.at(info);
             slots[3] = local.files.at(signature);
-            bundle.hash = Sign(NULL, 0, buffer, local.files[executable], save, identifier, entitlements, false, requirements, key, slots, length, 0, false, Progression(progress, root + executable));
+            bundle.hash = Sign(NULL, 0, buffer, local.files[executable], save, identifier, entitlements, merge, requirements, key, slots, length, 0, platform, Progression(progress, root + executable));
         }));
     }));
 
     return bundle;
 }
 
-Bundle Sign(const std::string &root, Folder &folder, const std::string &key, const std::string &requirements, const Functor<std::string (const std::string &, const std::string &)> &alter, const Progress &progress) {
+Bundle Sign(const std::string &root, Folder &folder, const std::string &key, const std::string &requirements, const Functor<std::string (const std::string &, const std::string &)> &alter, bool merge, uint8_t platform, const Progress &progress) {
     State local;
-    return Sign(root, folder, key, local, requirements, alter, progress);
+    return Sign(root, folder, key, local, requirements, alter, merge, platform, progress);
 }
 
 #endif
@@ -3140,9 +3144,9 @@ static void usage(const char *argv0) {
     fprintf(stderr, "Usage: %s [-Acputype:subtype] [-a] [-C[adhoc | enforcement | expires | hard |\n", argv0);
     fprintf(stderr, "            host | kill | library-validation | restrict | runtime]] [-D] [-d]\n");
     fprintf(stderr, "            [-Enum:file] [-e] [-H[sha1 | sha256]] [-h] [-Iname]\n");
-    fprintf(stderr, "            [-Kkey.p12 [-Upassword]] [-M] [-P] [-Qrequirements.xml] [-q]\n");
+    fprintf(stderr, "            [-Kkey.p12 [-Upassword]] [-M] [-P[num]] [-Qrequirements.xml] [-q]\n");
     fprintf(stderr, "            [-r | -Sfile.xml | -s] [-u] [-arch arch_type] file ...\n");
-    fprintf(stderr, "Options:\n");
+    fprintf(stderr, "Common Options:\n");
     fprintf(stderr, "   -S[file.xml]  Pseudo-sign using the entitlements in file.xml\n");
     fprintf(stderr, "   -Kkey.p12     Sign using private key in key.p12\n");
     fprintf(stderr, "   -Upassword    Use password to unlock key.p12\n");
@@ -3194,7 +3198,7 @@ int main(int argc, char *argv[]) {
     bool flag_M(false);
 
     uint32_t flags(0);
-    bool platform(false);
+    uint8_t platform(0);
 
     uint32_t flag_CPUType(_not(uint32_t));
     uint32_t flag_CPUSubtype(_not(uint32_t));
@@ -3357,7 +3361,13 @@ int main(int argc, char *argv[]) {
             } break;
 
             case 'P':
-                platform = true;
+                if (argv[argi][2] != '\0') {
+                    char *platformchar = argv[argi] + 2;
+                    char *arge;
+                    platform = strtoul(platformchar, &arge, 0);
+                } else {
+                    platform = 13;
+                }
             break;
 
             case 's':
@@ -3430,13 +3440,13 @@ int main(int argc, char *argv[]) {
         }
 
         if (S_ISDIR(info.st_mode)) {
-            if (!flag_S) {
-                fprintf(stderr, "ldid: Only -S can be used on directories\n");
+            if (!flag_S && !flag_s) {
+                fprintf(stderr, "ldid: Only -S and -s can be used on directories\n");
                 exit(1);
             }
             ldid::DiskFolder folder(path + "/");
-            path += "/" + Sign("", folder, key, requirements, ldid::fun([&](const std::string &, const std::string &) -> std::string { return entitlements; }), dummy_).path;
-        } else if (flag_S || flag_r) {
+            path += "/" + Sign("", folder, key, requirements, ldid::fun([&](const std::string &, const std::string &) -> std::string { return entitlements; }), flag_M, platform, dummy_).path;
+        } else if (flag_S || flag_r || flag_s) {
             Map input(path, O_RDONLY, PROT_READ, MAP_PRIVATE);
 
             std::filebuf output;
@@ -3539,6 +3549,10 @@ int main(int argc, char *argv[]) {
             }
 
             if (flag_h) {
+                char *buf = _syscall(realpath(file.c_str(), NULL));
+                printf("Executable=%s\n", buf);
+                free(buf);
+
                 auto algorithms(GetAlgorithms());
 
                 uint32_t data = mach_header.Swap(signature->dataoff);
@@ -3552,9 +3566,11 @@ int main(int argc, char *argv[]) {
                     size_t size_;
                     Algorithm &algorithm_;
                     std::string hash_;
+                    uint32_t offset;
                 };
 
                 std::map<uint8_t, Candidate> candidates;
+                uint32_t cmsBegin = 0, cmsEnd = 0;
 
                 for (size_t index(0); index != Swap(super->count); ++index) {
                     auto type(Swap(super->index[index].type));
@@ -3567,7 +3583,10 @@ int main(int argc, char *argv[]) {
                         auto &algorithm(*algorithms[type - 1]);
                         uint8_t hash[algorithm.size_];
                         algorithm(hash, blob + begin, end - begin);
-                        candidates.insert({type, {directory, end - begin, algorithm, Hex(hash, 20)}});
+                        candidates.insert({type, {directory, end - begin, algorithm, Hex(hash, algorithm.size_), begin}});
+                    } else if (type == CSSLOT_SIGNATURESLOT) {
+                        cmsBegin = Swap(super->index[index].offset);
+                        cmsEnd = index + 1 == Swap(super->count) ? Swap(super->blob.length) : Swap(super->index[index + 1].offset);
                     }
                 }
 
@@ -3578,6 +3597,8 @@ int main(int argc, char *argv[]) {
                 const auto directory(best->second.directory_);
                 const auto flags(Swap(directory->flags));
 
+                printf("Identifier=%s\n", blob + best->second.offset + Swap(directory->identOffset));
+
                 std::string names;
                 if (flags & kSecCodeSignatureHost)
                     names += ",host";
@@ -3607,11 +3628,63 @@ int main(int argc, char *argv[]) {
                     auto choice(candidate.second.algorithm_.name());
                     choices += ',';
                     choices += choice;
-                    printf("CandidateCDHash %s=%s\n", choice, candidate.second.hash_.c_str());
+                    printf("CandidateCDHash %s=%.40s\n", choice, candidate.second.hash_.c_str());
+                    printf("CandidateCDHashFull %s=%s\n", choice, candidate.second.hash_.c_str());
                 }
                 printf("Hash choices=%s\n", choices.c_str() + 1);
 
-                printf("CDHash=%s\n", best->second.hash_.c_str());
+                printf("CDHash=%.40s\n", best->second.hash_.c_str());
+
+                if (cmsBegin != 0 && cmsEnd != 0) {
+                    // This loads the CMS blob and parses each X509 cert in the blob to extract the
+                    // common name and print it as "Authority=%s"
+                    Buffer bio(reinterpret_cast<const char *>(blob) + cmsBegin + sizeof(Blob), cmsEnd - cmsBegin);
+                    PKCS7 *p7 = NULL;
+                    if ((p7 = d2i_PKCS7_bio(bio, NULL)) == NULL) {
+                        // In order to follow codesign, we just ignore errors
+                        printf("Authority=(unavailable)\n");
+                    } else {
+                        STACK_OF(X509) *certs = NULL;
+                        switch (OBJ_obj2nid(p7->type)) {
+                            case NID_pkcs7_signed:
+                                if (p7->d.sign != NULL)
+                                    certs = p7->d.sign->cert;
+                                break;
+                            case NID_pkcs7_signedAndEnveloped:
+                                if (p7->d.signed_and_enveloped != NULL)
+                                    certs = p7->d.signed_and_enveloped->cert;
+                                break;
+                            default:
+                                break;
+                        }
+                        if (certs != NULL) {
+                            X509 *x;
+                            for (int i = 0; i < sk_X509_num(certs); i++) {
+                                x = sk_X509_value(certs, i);
+                                int lastpos = -1;
+                                X509_NAME *nm = X509_get_subject_name(x);
+                                X509_NAME_ENTRY *e;
+
+                                for (;;) {
+                                    lastpos = X509_NAME_get_index_by_NID(nm, NID_commonName, lastpos);
+                                    if (lastpos == -1)
+                                        break;
+                                    e = X509_NAME_get_entry(nm, lastpos);
+                                    ASN1_STRING *s = X509_NAME_ENTRY_get_data(e);
+                                    printf("Authority=%s\n", reinterpret_cast<const char *>(ASN1_STRING_get0_data(s)));
+                                }
+                            }
+                        } else {
+                            printf("Authority=(unavailable)\n");
+                        }
+                    }
+                    PKCS7_free(p7);
+                }
+
+                if (Swap(directory->teamIDOffset) > 0)
+                    printf("TeamIdentifier=%s\n", blob + best->second.offset + Swap(directory->teamIDOffset));
+                else
+                    printf("TeamIdentifier=not set\n");
             }
         }