+static void get(std::string &value, X509_NAME *name, int nid) {
+ auto index(X509_NAME_get_index_by_NID(name, nid, -1));
+ _assert(index >= 0);
+ auto next(X509_NAME_get_index_by_NID(name, nid, index));
+ _assert(next == -1);
+ auto entry(X509_NAME_get_entry(name, index));
+ _assert(entry != NULL);
+ auto asn(X509_NAME_ENTRY_get_data(entry));
+ _assert(asn != NULL);
+ value.assign(reinterpret_cast<char *>(ASN1_STRING_data(asn)), ASN1_STRING_length(asn));
+}
+
+static void req(std::streambuf &buffer, uint32_t value) {
+ value = Swap(value);
+ put(buffer, &value, sizeof(value));
+}
+
+static void req(std::streambuf &buffer, const std::string &value) {
+ req(buffer, value.size());
+ put(buffer, value.data(), value.size());
+ static uint8_t zeros[] = {0,0,0,0};
+ put(buffer, zeros, 3 - (value.size() + 3) % 4);
+}
+
+template <size_t Size_>
+static void req(std::streambuf &buffer, uint8_t (&&data)[Size_]) {
+ req(buffer, Size_);
+ put(buffer, data, Size_);
+ static uint8_t zeros[] = {0,0,0,0};
+ 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, const std::string &requirements, const std::string &key, const Slots &slots, const Functor<void (double)> &percent) {
+ Hash hash;
+
+
+ std::string team;
+ std::string common;
+
+#ifndef LDID_NOSMIME
+ if (!key.empty()) {
+ Stuff stuff(key);
+ auto name(X509_get_subject_name(stuff));
+ _assert(name != NULL);
+ get(team, name, NID_organizationalUnitName);
+ get(common, name, NID_commonName);
+ }
+#endif
+
+
+ std::stringbuf backing;
+
+ if (!requirements.empty()) {
+ put(backing, requirements.data(), requirements.size());
+ } else {
+ Blobs blobs;
+
+ std::stringbuf requirement;
+ req(requirement, exprForm);
+ req(requirement, opAnd);
+ req(requirement, opIdent);
+ req(requirement, identifier);
+ req(requirement, opAnd);
+ req(requirement, opAppleGenericAnchor);
+ req(requirement, opAnd);
+ req(requirement, opCertField);
+ req(requirement, 0);
+ req(requirement, "subject.CN");
+ req(requirement, matchEqual);
+ req(requirement, common);
+ req(requirement, opCertGeneric);
+ req(requirement, 1);
+ req(requirement, (uint8_t []) {APPLE_EXTENSION_OID, 2, 1});
+ req(requirement, matchExists);
+ insert(blobs, 3, CSMAGIC_REQUIREMENT, requirement);
+
+ put(backing, CSMAGIC_REQUIREMENTS, blobs);
+ }
+
+
+ // XXX: this is just a "sufficiently large number"
+ size_t certificate(0x3000);
+
+ Allocate(idata, isize, output, fun([&](const MachHeader &mach_header, size_t size) -> size_t {