This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
openssl.ts
306 lines (251 loc) · 7.86 KB
/
openssl.ts
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { getLogger, Logger } from "https://deno.land/[email protected]/log/mod.ts";
import { join } from "https://deno.land/[email protected]/path/mod.ts";
import {
firstMessages,
Rule,
validate,
validateArray,
} from "https://deno.land/x/[email protected]/src/mod.ts";
import * as Valid from "https://deno.land/x/[email protected]/src/rules.ts";
import { runCommand } from "./utils.ts";
type Filenames = "ini" | "crt" | "csr" | "key" | "p12";
export interface OpenSSLConfig {
filename: string;
destination: string;
domains?: string[];
ips?: string[];
// OpenSSL
commonName: string;
countryName: string;
stateOrProvinceName: string;
localityName: string;
organizationName: string;
organizationalUnitName: string;
emailAddress: string;
// macOS Keychain
// addToKeychain?: boolean
// removeFromKeychain?: boolean
// keychainName?: string
}
export class OpenSSL {
private logger: Logger;
config: OpenSSLConfig;
/**
* Generates a valid set of certificates to use in
* web and mobile development.
*
* @param {Config} config the config
*/
constructor(config: OpenSSLConfig) {
this.logger = getLogger("openssl");
this.config = config;
}
async generate() {
this.logger.info("Starting OpenSSL generator");
try {
await this.validateConfig();
} catch (error) {
throw new Error(error);
}
const destinationNotEmpty = await this.checkDestination();
if (destinationNotEmpty) {
await this.clearDestination();
}
// Those must be run in this order!
await this.createExtensionConf();
await this.createPrivateKey();
await this.createCertificateSigningRequest();
await this.checkingCertificateSigningRequest();
await this.generateCertificate();
await this.generatePKCS12();
}
getFiles<T extends Filenames>(keyArray: T[]): Record<T, string> {
const pathArray: Record<string, string> = {};
for (const key of keyArray) {
pathArray[key] = join(
this.config.destination,
this.config.filename + "." + key,
);
}
return pathArray;
}
async validateConfig() {
this.logger.debug("Validating:", JSON.stringify(this.config, null, 2));
const scheme: Record<keyof OpenSSLConfig, Rule | Rule[]> = {
filename: [Valid.required, Valid.isString],
destination: [Valid.required, Valid.isString],
domains: validateArray(false, [Valid.isString], { minLength: 1 }),
ips: validateArray(false, [Valid.isString], { minLength: 1 }),
commonName: [Valid.required, Valid.isString],
countryName: [Valid.required, Valid.isString],
stateOrProvinceName: [Valid.required, Valid.isString],
localityName: [Valid.required, Valid.isString],
organizationName: [Valid.required, Valid.isString],
organizationalUnitName: [Valid.required, Valid.isString],
emailAddress: [Valid.required, Valid.isString],
};
const [passes, errors] = await validate(this.config, scheme);
if (!passes) {
for (const error of Object.values(firstMessages(errors))) {
this.logger.error(error);
}
throw new SyntaxError("Config validation failed");
}
this.logger.info("Config validation passed");
}
async checkDestination() {
this.logger.info("Checking destination path");
try {
for await (const iter of Deno.readDir(this.config.destination)) {
return iter; // at least one item means folder is not empty
}
} catch (error) {
this.logger.warning("Destination path does not exist, creating...");
if (error instanceof Deno.errors.NotFound) {
await Deno.mkdir(this.config.destination, { recursive: true });
this.logger.info("Created destination:", this.config.destination);
return;
}
throw error;
}
}
async clearDestination() {
this.logger.warning("Clearing destination");
for (
const file of Object.values(
this.getFiles(["crt", "csr", "ini", "key", "p12"]),
)
) {
try {
await Deno.lstat(file);
await Deno.remove(file);
this.logger.warning("Removed:", file);
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
return;
}
throw error;
}
}
}
private createExtensionConf() {
this.logger.info("Creating extension conf");
let conf = `
[ req ]
default_bits = 4096
distinguished_name = req_distinguished_name
req_extensions = req_extensions_section
x509_extensions = req_extensions_section
prompt = no
[ req_distinguished_name ]
countryName = ${this.config.countryName}
stateOrProvinceName = ${this.config.stateOrProvinceName}
localityName = ${this.config.localityName}
organizationName = ${this.config.organizationName}
organizationalUnitName = ${this.config.organizationalUnitName}
commonName = ${this.config.commonName}
emailAddress = ${this.config.emailAddress}
[ req_extensions_section ]
basicConstraints = critical,CA:TRUE,pathlen:1
subjectAltName = @subject_alternative_name_section
[ subject_alternative_name_section ]
IP.1 = 127.0.0.1
IP.2 = ::1
DNS.1 = localhost
`.trim();
// Add dns domains
if (this.config.domains) {
const dnsMap = this.config.domains.map((d, i) => `DNS.${i + 2} = ${d}`);
conf += "\n" + dnsMap.join("\n");
}
// Add ip addresses
if (this.config.ips) {
const ipMap = this.config.ips.map((d, i) => `IP.${i + 3} = ${d}`);
conf += "\n" + ipMap.join("\n");
}
this.logger.debug("Extension conf:\n" + conf);
const { ini } = this.getFiles(["ini"]);
return Deno.writeTextFile(ini, conf);
}
private async createPrivateKey() {
this.logger.info("Creating private key");
const { key } = this.getFiles(["key"]);
// openssl genrsa -out "$DEST/dev.key" 4096
await runCommand("openssl", ["genrsa", "-out", key, "4096"]);
}
private async createCertificateSigningRequest() {
this.logger.info("Creating certificate signing request");
const { key, csr, ini } = this.getFiles(["key", "csr", "ini"]);
// openssl req -new -sha256 \
// -key "$SSL_DIR/dev.key" \
// -out "$SSL_DIR/dev.csr" \
// -config "$SSL_DIR/conf.ini"
await runCommand("openssl", [
"req",
"-new",
"-sha256",
"-key",
key,
"-out",
csr,
"-config",
ini,
]);
}
private async checkingCertificateSigningRequest() {
this.logger.info("Checking certificate signing request");
const { csr } = this.getFiles(["csr"]);
// openssl req -text -noout -in "$SSL_DIR/dev.csr"
await runCommand("openssl", ["req", "-text", "-noout", "-in", csr]);
}
private async generateCertificate() {
this.logger.info("Generate the certificate");
const { key, csr, ini, crt } = this.getFiles(["key", "csr", "ini", "crt"]);
// openssl x509 -req \
// -sha256 \
// -days 365 \
// -in "$SSL_DIR/dev.csr" \
// -signkey "$SSL_DIR/dev.key" \
// -out "$SSL_DIR/dev.crt" \
// -extensions req_extensions_section \
// -extfile "$SSL_DIR/conf.ini"
await runCommand("openssl", [
"x509",
"-req",
"-sha256",
"-days",
"365",
"-in",
csr,
"-signkey",
key,
"-out",
crt,
"-extensions",
"req_extensions_section",
"-extfile",
ini,
]);
}
private async generatePKCS12() {
this.logger.info("Generate PKCS12");
const { key, crt, p12 } = this.getFiles(["key", "crt", "p12"]);
// openssl pkcs12 -export \
// -inkey "$SSL_DIR/dev.key" \
// -in "$SSL_DIR/dev.crt" \
// -out "$SSL_DIR/dev.p12" \
// -passout pass:1234
await runCommand("openssl", [
"pkcs12",
"-export",
"-inkey",
key,
"-in",
crt,
"-out",
p12,
"-passout",
"pass:1234",
]);
}
}