-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathidentity.cpp
More file actions
177 lines (147 loc) · 4.15 KB
/
identity.cpp
File metadata and controls
177 lines (147 loc) · 4.15 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "identity.h"
#include "minimal/stdlib.h"
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdio>
#include <wordexp.h>
using namespace std;
class Identity_impl
{
string _identityPath;
string _identity;
public:
Identity_impl(const string &name);
bool isValid();
vector<uint8_t> signMessage(const char *msg, int len);
};
Identity::Identity(const string &name) : impl(0)
{
Identity_impl *imp = new Identity_impl(name);
if (imp->isValid())
this->impl = imp;
else
delete imp;
}
Identity::~Identity()
{
delete (Identity_impl *)this->impl;
}
bool Identity::found()
{
return !!impl;
}
vector<uint8_t> Identity::signMessage(const char *msg, int len)
{
return ((Identity_impl *)this->impl)->signMessage(msg, len);
}
// Mediocre pseudo-keychain system.
Identity_impl::Identity_impl(const string &name) : _identity(name)
{
if (!name.size())
return;
string path = "~/.codesign2/identities/" + _identity;
char buf[4096];
wordexp_t exp;
wordexp(path.c_str(), &exp, WRDE_NOCMD);
if (!exp.we_wordc) {
wordfree(&exp);
return;
}
path = exp.we_wordv[0];
wordfree(&exp);
_identityPath = realpath(path.c_str(), buf);
}
bool Identity_impl::isValid()
{
if (!_identity.size() || !_identityPath.size())
return false;
struct stat st;
if (stat(_identityPath.c_str(), &st))
return false;
if (!(st.st_mode & S_IFDIR))
return false;
return true;
}
vector<uint8_t> Identity_impl::signMessage(const char *msg, int len)
{
vector<uint8_t> data;
string keypath, certpath;
string inname, outname;
FILE *inf, *outf;
char buf[4096];
size_t read;
// Determine file names.
keypath = _identityPath + "/privateKey.pem";
certpath = _identityPath + "/publicCert.pem";
inname = tmpnam(buf);
outname = tmpnam(buf);
// Resolve the paths
realpath(keypath.c_str(), buf);
keypath = buf;
realpath(certpath.c_str(), buf);
certpath = buf;
fprintf(stderr, "Signing infile: %s\n", inname.c_str());
fprintf(stderr, "Signing outfile: %s\n", outname.c_str());
// Write the data to a temp file.
inf = fopen(inname.c_str(), "w");
fwrite(msg, 1, len, inf);
fclose(inf);
// Fork and openssl to generate the signature.
pid_t pid = fork();
_syscall(pid);
if (pid == 0) {
std::vector<const char *> args;
args.push_back("openssl");
args.push_back("cms");
args.push_back("-sign");
args.push_back("-binary");
args.push_back("-outform");
args.push_back("DER");
args.push_back("-md");
args.push_back("SHA1");
args.push_back("-signer");
args.push_back(certpath.c_str());
args.push_back("-inkey");
args.push_back(keypath.c_str());
args.push_back("-in");
args.push_back(inname.c_str());
args.push_back("-out");
args.push_back(outname.c_str());
string passpath = _identityPath + "/password";
struct stat tmp;
if (!stat(passpath.c_str(), &tmp))
{
args.push_back("-passin");
passpath = "file:" + passpath;
args.push_back(passpath.c_str());
}
// The real codesign also adds the Apple Root CA and Apple
// Worldwide Developer Relations certificates.
//args.push_back("-certfile");
//args.push_back("AppleRootAndWWDRCerts.pem");
args.push_back(NULL);
if (true) {
printf("run:");
_foreach (arg, args)
printf(" %s", arg);
printf("\n");
}
execvp("openssl", (char **) &args[0]);
_assert(false);
}
int status;
_syscall(waitpid(pid, &status, 0));
_assert(WIFEXITED(status));
_assert(WEXITSTATUS(status) == 0);
outf = fopen(outname.c_str(), "r");
while ((read = fread(buf, 1, sizeof(buf), outf)))
data.insert(data.end(), (uint8_t*)buf, (uint8_t*)&buf[read]);
_assert(!ferror(outf));
fclose(outf);
remove(inname.c_str());
remove(outname.c_str());
return data;
}