File size: 9,034 Bytes
34810d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Proxy capture CA helpers create and inspect local capture CA certificates.
import { createHash, createPrivateKey, randomBytes, X509Certificate } from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { parseCanonicalIpAddress } from "@openclaw/net-policy/ip";
import { type FileLockOptions, withFileLock } from "../infra/file-lock.js";
import { resolveSystemBin } from "../infra/resolve-system-bin.js";
import { KeyedAsyncQueue } from "../plugin-sdk/keyed-async-queue.js";
import { runExec } from "../process/exec.js";

const DEBUG_PROXY_CA_GENERATION_TIMEOUT_MS = 30_000;
const LOCAL_PROXY_CERT_GENERATION_TIMEOUT_MS = 30_000;
const LOCAL_PROXY_DIR_MODE = 0o700;
const LOCAL_PROXY_PRIVATE_KEY_MODE = 0o600;

function buildLocalProxyCaOpenSslConfig(commonName: string): string {
  return [
    "[req]",
    "distinguished_name = subject",
    "prompt = no",
    "",
    "[subject]",
    `CN = ${commonName}`,
    "",
    "[v3_ca]",
    "basicConstraints = critical, CA:TRUE",
    "keyUsage = critical, keyCertSign, cRLSign",
    "",
  ].join("\n");
}
const DEBUG_PROXY_CA_LOCK_OPTIONS: FileLockOptions = {
  retries: {
    // About 36s of minimum backoff covers one full 30s OpenSSL deadline.
    retries: 80,
    factor: 1.3,
    minTimeout: 25,
    maxTimeout: 500,
    randomize: true,
  },
  stale: 60_000,
  staleRecovery: "remove-if-unchanged",
};
const debugProxyCaGenerationQueue = new KeyedAsyncQueue();

function isValidDebugProxyCaPair(certPath: string, keyPath: string): boolean {
  try {
    const certStat = fs.lstatSync(certPath);
    const keyStat = fs.lstatSync(keyPath);
    if (!certStat.isFile() || !keyStat.isFile() || certStat.size === 0 || keyStat.size === 0) {
      return false;
    }
    const cert = new X509Certificate(fs.readFileSync(certPath));
    const key = createPrivateKey(fs.readFileSync(keyPath));
    return cert.ca && cert.checkPrivateKey(key);
  } catch {
    return false;
  }
}

function removeStagingDirBestEffort(stagingDir: string): void {
  try {
    fs.rmSync(stagingDir, { recursive: true, force: true });
  } catch {
    // Cleanup failure must not replace a successful publication result.
  }
}

type LocalProxyCaOptions = {
  commonName: string;
  purpose: string;
  validityDays: number;
};

type LocalProxyCaPair = {
  certPath: string;
  keyPath: string;
};

async function ensureLocalProxyCa(
  certDir: string,
  options: LocalProxyCaOptions,
): Promise<LocalProxyCaPair> {
  fs.mkdirSync(certDir, { recursive: true, mode: LOCAL_PROXY_DIR_MODE });
  fs.chmodSync(certDir, LOCAL_PROXY_DIR_MODE);
  const certPath = path.join(certDir, "root-ca.pem");
  const keyPath = path.join(certDir, "root-ca-key.pem");
  const canonicalKeyPath = path.join(fs.realpathSync(certDir), "root-ca-key.pem");
  return await debugProxyCaGenerationQueue.enqueue(canonicalKeyPath, async () =>
    withFileLock(canonicalKeyPath, DEBUG_PROXY_CA_LOCK_OPTIONS, async () => {
      if (isValidDebugProxyCaPair(certPath, keyPath)) {
        return { certPath, keyPath };
      }
      const openssl = resolveSystemBin("openssl");
      if (!openssl) {
        throw new Error(`openssl is required to generate ${options.purpose} certificates`);
      }
      const stagingDir = fs.mkdtempSync(path.join(certDir, ".root-ca-"));
      const stagedConfigPath = path.join(stagingDir, "openssl.cnf");
      const stagedCertPath = path.join(stagingDir, "root-ca.pem");
      const stagedKeyPath = path.join(stagingDir, "root-ca-key.pem");
      try {
        fs.writeFileSync(stagedConfigPath, buildLocalProxyCaOpenSslConfig(options.commonName), {
          mode: LOCAL_PROXY_PRIVATE_KEY_MODE,
        });
        await runExec(
          openssl,
          [
            "req",
            "-config",
            stagedConfigPath,
            "-extensions",
            "v3_ca",
            "-x509",
            "-newkey",
            "rsa:2048",
            "-sha256",
            "-days",
            String(options.validityDays),
            "-nodes",
            "-keyout",
            stagedKeyPath,
            "-out",
            stagedCertPath,
          ],
          { logOutput: false, timeoutMs: DEBUG_PROXY_CA_GENERATION_TIMEOUT_MS },
        );
        if (!isValidDebugProxyCaPair(stagedCertPath, stagedKeyPath)) {
          throw new Error(`openssl generated invalid ${options.purpose} certificate material`);
        }
        fs.chmodSync(stagedKeyPath, LOCAL_PROXY_PRIVATE_KEY_MODE);
        fs.chmodSync(stagedCertPath, 0o644);
        // All OpenClaw writers hold this lock. Same-directory renames replace each
        // file atomically; validation repairs a pair interrupted between renames.
        fs.renameSync(stagedKeyPath, keyPath);
        fs.renameSync(stagedCertPath, certPath);
        return { certPath, keyPath };
      } finally {
        removeStagingDirBestEffort(stagingDir);
      }
    }),
  );
}

// Ensure a short-lived root CA for local MITM debug proxy runs. Existing certs
// are reused within the cert dir so repeated starts do not prompt regeneration.
export async function ensureDebugProxyCa(certDir: string): Promise<{
  certPath: string;
  keyPath: string;
}> {
  return await ensureLocalProxyCa(certDir, {
    commonName: "OpenClaw Debug Proxy",
    purpose: "debug proxy",
    validityDays: 7,
  });
}

/** Generates the root CA for one Gateway-lifetime secret egress proxy. */
export async function ensureSecretEgressProxyCa(certDir: string): Promise<LocalProxyCaPair> {
  return await ensureLocalProxyCa(certDir, {
    commonName: "OpenClaw Secret Egress Proxy",
    purpose: "secret egress proxy",
    // Trust is loaded once by subprocesses. Key retention is still limited to
    // this Gateway process; certificate expiry must not impose daily restarts.
    validityDays: 3650,
  });
}

function isValidLeafPair(params: { certPath: string; keyPath: string; hostname: string }): boolean {
  try {
    const cert = new X509Certificate(fs.readFileSync(params.certPath));
    const key = createPrivateKey(fs.readFileSync(params.keyPath));
    const hostMatches = parseCanonicalIpAddress(params.hostname)
      ? cert.checkIP(params.hostname) === params.hostname
      : cert.checkHost(params.hostname) === params.hostname;
    return !cert.ca && cert.checkPrivateKey(key) && hostMatches;
  } catch {
    return false;
  }
}

async function generateLocalProxyLeafQueued(params: {
  certDir: string;
  ca: LocalProxyCaPair;
  hostname: string;
}): Promise<{ cert: Buffer; key: Buffer }> {
  const openssl = resolveSystemBin("openssl");
  if (!openssl) {
    throw new Error("openssl is required to generate local proxy certificates");
  }
  const leafKeyPath = path.join(params.certDir, "leaf-key.pem");
  if (!fs.existsSync(leafKeyPath)) {
    await runExec(openssl, ["genrsa", "-out", leafKeyPath, "2048"], {
      logOutput: false,
      timeoutMs: LOCAL_PROXY_CERT_GENERATION_TIMEOUT_MS,
    });
    fs.chmodSync(leafKeyPath, LOCAL_PROXY_PRIVATE_KEY_MODE);
  }
  const leafId = createHash("sha256").update(params.hostname).digest("hex");
  const stagingDir = fs.mkdtempSync(path.join(params.certDir, `.leaf-${leafId.slice(0, 12)}-`));
  const csrPath = path.join(stagingDir, "leaf.csr");
  const certPath = path.join(stagingDir, "leaf.pem");
  const extPath = path.join(stagingDir, "leaf.ext");
  try {
    const sanKind = parseCanonicalIpAddress(params.hostname) ? "IP" : "DNS";
    fs.writeFileSync(
      extPath,
      `subjectAltName=${sanKind}:${params.hostname}\nextendedKeyUsage=serverAuth\n`,
      { mode: LOCAL_PROXY_PRIVATE_KEY_MODE },
    );
    await runExec(
      openssl,
      ["req", "-new", "-key", leafKeyPath, "-subj", `/CN=${params.hostname}`, "-out", csrPath],
      { logOutput: false, timeoutMs: LOCAL_PROXY_CERT_GENERATION_TIMEOUT_MS },
    );
    await runExec(
      openssl,
      [
        "x509",
        "-req",
        "-in",
        csrPath,
        "-CA",
        params.ca.certPath,
        "-CAkey",
        params.ca.keyPath,
        "-set_serial",
        `0x${randomBytes(16).toString("hex")}`,
        "-out",
        certPath,
        "-days",
        "1",
        "-sha256",
        "-extfile",
        extPath,
      ],
      { logOutput: false, timeoutMs: LOCAL_PROXY_CERT_GENERATION_TIMEOUT_MS },
    );
    if (!isValidLeafPair({ certPath, keyPath: leafKeyPath, hostname: params.hostname })) {
      throw new Error("openssl generated invalid local proxy leaf certificate material");
    }
    return {
      cert: fs.readFileSync(certPath),
      key: fs.readFileSync(leafKeyPath),
    };
  } finally {
    removeStagingDirBestEffort(stagingDir);
  }
}

/** Mints one on-demand TLS leaf signed by a local proxy CA. */
export async function generateLocalProxyLeaf(params: {
  certDir: string;
  ca: LocalProxyCaPair;
  hostname: string;
}): Promise<{ cert: Buffer; key: Buffer }> {
  const queueKey = path.join(fs.realpathSync(params.certDir), "leaf-key.pem");
  return await debugProxyCaGenerationQueue.enqueue(queueKey, () =>
    generateLocalProxyLeafQueued(params),
  );
}