-
Notifications
You must be signed in to change notification settings - Fork 7
/
node-passbook
executable file
·170 lines (152 loc) · 5.19 KB
/
node-passbook
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
#!/usr/bin/env node
//vi:set ft=javascript:
var Async = require("async");
var CLI = require("cli");
var File = require("fs");
var Path = require("path");
var spawn = require("child_process").spawn;
CLI.enable("version");
CLI.setApp(__dirname + "/package.json");
CLI.parse({
path: ["p", "Directory holding private keys", "path", "keys"]
}, {
"prepare-keys": "Prepare private keys for signing passbook",
"server-keys": "Generates self-signed keys for TLS (HTTPS)"
});
CLI.main(function(args, options) {
switch (this.command) {
case "prepare-keys":
Async.waterfall([
createKeysDirectory.bind(null, options.path),
extractWWDRKey.bind(null, options.path),
generateKeyFromP12Files.bind(null, options.path)
], function(error) {
if (error) {
CLI.error(error.message || error);
process.exit(1);
} else {
CLI.info("Done");
}
});
break;
case "server-keys":
Async.waterfall([
createKeysDirectory.bind(null, options.path),
generateServerKeys.bind(null, options.path)
], function(error) {
if (error) {
CLI.error(error.message || error);
process.exit(1);
} else {
CLI.info("Done");
}
});
break;
default:
throw new Error("Unknown command " + this.command);
}
});
// Create the directory used for storing all keys.
function createKeysDirectory(keysPath, callback) {
File.exists(keysPath, function(exists) {
if (exists)
callback();
else
File.mkdir(keysPath, callback);
});
}
// Extract the Apple Worldwide Developer Relations Certification Authority from
// Keychain and store it as wwdr.pem in the keys directory.
function extractWWDRKey(keysPath, callback) {
var destination = Path.resolve(keysPath, "wwdr.pem");
File.exists(destination, function(exists) {
if (exists) {
callback();
} else {
CLI.info("Extracting Apple WWDR certificate into directory " + destination);
var command = "security find-certificate -p -c 'Apple Worldwide Developer Relations Certification Authority' login.keychain";
CLI.exec(command, function(keyContents) {
File.writeFile(destination, keyContents.join("\n"), callback);
}, callback);
}
});
}
// Convert all P12 files in the keys directory into PEM files.
//
// When exporting the Passbook certificate from Keychain, we get a P12 files,
// but to sign the certificate we need a PEM file.
function generateKeyFromP12Files(keysPath, callback) {
File.readdir(keysPath, function(error, files) {
if (error) {
callback(error);
} else {
var p12Files = files.filter(function(fn) { return Path.extname(fn) === ".p12" }).
map(function(fn) { return Path.resolve(keysPath, fn) });
if (p12Files.length == 0) {
CLI.info("No P12 files found in directory " + keysPath);
callback();
} else
generateNextKeyFile(p12Files);
}
});
// Process the next file in p12Files
function generateNextKeyFile(p12Files) {
var p12File = p12Files[0];
if (p12File) {
var pemFile = p12File.replace(/p12$/, "pem");
File.exists(pemFile, function(exists) {
if (exists && false) {
CLI.info("Skipping " + pemFile);
generateNextKeyFile(p12Files.slice(1));
} else {
CLI.info("Generating " + pemFile);
spawn("openssl", ["pkcs12", "-in", p12File, "-out", pemFile]).
on("exit", function(code) {
if (code == 0)
generateNextKeyFile(p12Files.slice(1));
else
callback(new Error("Failed to generate " + pemFile));
});
}
});
} else
callback();
}
}
function generateServerKeys(keysPath, callback) {
var keyFile = Path.resolve(keysPath, "server.key");
var certFile = Path.resolve(keysPath, "server.crt");
Async.waterfall([
function(done) {
CLI.info("Generating private/public key pair");
spawn("openssl", ["genrsa", "-des3", "-out", "server.key", "1024"]).
on("exit", function(code) {
done(code == 0 ? null : new Error("Could not generate server.key"));
});
},
function(done) {
CLI.info("Generating CSR");
spawn("openssl", ["req", "-new", "-key", "server.key", "-out", "server.csr"], { stdio: "inherit" }).
on("exit", function(code) {
done(code == 0 ? null : new Error("Could not generate server.csr"));
});
},
function(done) {
CLI.info("Removing password from server.key");
spawn("openssl", ["rsa", "-in", "server.key", "-out", keyFile], { stdio: "inherit" }).
on("exit", function(code) {
File.unlink("server.key");
done(code == 0 ? null : new Error("Could not generate " + keyFile));
});
},
function(done) {
CLI.info("Generating server certificate");
spawn("openssl", ["x509", "-req", "-days", "365", "-in", "server.csr",
"-signkey", keyFile, "-out", certFile], { stdio: "inherit" }).
on("exit", function(code) {
File.unlink("server.csr");
done(code == 0 ? null : new Error("Could not generate " + certFile));
});
}
], callback);
}