-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsignature.cpp
164 lines (139 loc) · 4.73 KB
/
signature.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <vector>
#include <memory>
#include <arpa/inet.h>
#include <cmath>
#include <limits>
#include <cassert>
#include "signature.h"
#include "emit.h"
namespace SigTool {
CodeDirectory::CodeDirectory() noexcept {
data.magic = CSMAGIC_CODEDIRECTORY;
data.version = 0x020400;
data.flags = CS_ADHOC;
data.hashSize = Hash::hashSize;
data.hashType = Hash::hashType;
}
size_t CodeDirectory::length() {
size_t length = sizeof(data);
length += identifier.length() + 1;
length += sizeof(Hash::bytes) * (data.nSpecialSlots + data.nCodeSlots);
return length;
}
void CodeDirectory::emit(std::ostream& os) {
// Layout variable length components
off_t tail = sizeof(data);
data.identOffset = tail;
tail += identifier.length() + 1;
data.hashOffset = tail + sizeof(Hash::bytes) * data.nSpecialSlots;
tail += sizeof(Hash::bytes) * (data.nSpecialSlots + data.nCodeSlots);
data.length = tail;
// Stream all fixed components
EmitBE::writeUInt32(os, data.magic);
EmitBE::writeUInt32(os, data.length);
EmitBE::writeUInt32(os, data.version);
EmitBE::writeUInt32(os, data.flags);
EmitBE::writeUInt32(os, data.hashOffset);
EmitBE::writeUInt32(os, data.identOffset);
EmitBE::writeUInt32(os, data.nSpecialSlots);
EmitBE::writeUInt32(os, data.nCodeSlots);
EmitBE::writeUInt32(os, data.codeLimit);
EmitBE::writeBytes<uint8_t>(os, data.hashSize);
EmitBE::writeBytes<uint8_t>(os, data.hashType);
EmitBE::writeBytes<uint8_t>(os, data.platform);
EmitBE::writeBytes<uint8_t>(os, data.pageSize);
EmitBE::writeUInt32(os, data.spare2);
EmitBE::writeUInt32(os, data.scatterOffset);
EmitBE::writeUInt32(os, data.teamOffset);
EmitBE::writeUInt32(os, data.spare3);
EmitBE::writeUInt64(os, data.codeLimit64);
EmitBE::writeUInt64(os, data.execSegBase);
EmitBE::writeUInt64(os, data.execSegLimit);
EmitBE::writeUInt64(os, data.execSegFlags);
// Followed by variable length components
os.write(identifier.c_str(), identifier.length());
os.put(0);
if (data.nSpecialSlots > 0) {
for (int specialIndex = (int) data.nSpecialSlots - 1; specialIndex >= 0; specialIndex--) {
os.write(specialHashes[specialIndex].bytes, sizeof(Hash::bytes));
}
}
for (const Hash& hash : codeHashes) {
os.write(hash.bytes, sizeof(Hash::bytes));
}
}
void CodeDirectory::setSpecialHash(int index, const Hash& value) {
// index is in the range of (1 to 5), with 1 being the first, etc
// for convenience, map that to a regular (0..4) array
unsigned int storage = (unsigned int)index - 1;
data.nSpecialSlots = std::max(data.nSpecialSlots, storage + 1);
specialHashes[storage] = value;
}
void CodeDirectory::setPageSize(uint16_t pageSize) {
data.pageSize = log2(pageSize);
}
void CodeDirectory::setCodeLimit(uint64_t codeLimit) {
if (codeLimit >= std::numeric_limits<uint32_t>::max()) {
data.codeLimit = std::numeric_limits<uint32_t>::max();
data.codeLimit64 = codeLimit;
} else {
data.codeLimit = codeLimit;
data.codeLimit64 = 0;
}
}
void CodeDirectory::addCodeHash(const Hash& value) {
codeHashes.push_back(value);
data.nCodeSlots = codeHashes.size();
}
void SuperBlob::emit(std::ostream& os) {
EmitBE::writeUInt32(os, CSMAGIC_EMBEDDED_SIGNATURE);
EmitBE::writeUInt32(os, length());
EmitBE::writeUInt32(os, blobs.size());
off_t blobDataOffset =
SuperBlob::headerSize +
2 * sizeof(uint32_t) * blobs.size(); // blob index entry
// blob index
for (const auto& blob : blobs) {
EmitBE::writeUInt32(os, blob->slotType());
EmitBE::writeUInt32(os, blobDataOffset);
blobDataOffset += blob->length();
}
for (const auto& blob : blobs) {
// blob data
blob->emit(os);
}
}
size_t SuperBlob::length() {
size_t length =
SuperBlob::headerSize +
2 * sizeof(uint32_t) * blobs.size();
for (const auto& blob : blobs) {
length += blob->length();
}
return length;
}
void Requirements::emit(std::ostream &os) {
EmitBE::writeUInt32(os, CSMAGIC_REQUIREMENTS);
EmitBE::writeUInt32(os, length());
EmitBE::writeUInt32(os, 0); // count
}
size_t Requirements::length() {
return 3 * sizeof(uint32_t);
}
void Signature::emit(std::ostream &os) {
EmitBE::writeUInt32(os, CSMAGIC_BLOBWRAPPER);
EmitBE::writeUInt32(os, length());
}
size_t Signature::length() {
return 2 * sizeof(uint32_t);
}
void Entitlements::emit(std::ostream &os) {
EmitBE::writeUInt32(os, CSMAGIC_EMBEDDED_ENTITLEMENTS);
EmitBE::writeUInt32(os, length());
os << entitlements;
}
size_t Entitlements::length() {
return entitlements.length() + 8;
}
};