askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
ca.ts · 4.8 KB · 128 lines
| 1 | /** |
| 2 | * A local certificate authority for MITM. |
| 3 | * |
| 4 | * To pay a 402 on an https:// subresource we have to see the plaintext request and |
| 5 | * swap the response — which means terminating TLS ourselves. That requires presenting |
| 6 | * a cert the client trusts for the target host, so we run a local root CA, install it |
| 7 | * once in the OS trust store, and mint short-lived leaf certs per host on demand. |
| 8 | * |
| 9 | * This is the same mechanism every debugging proxy (Charles, mitmproxy, Fiddler) uses. |
| 10 | * The root key never leaves the machine; treat ~/.dbbasic-browser/ca-key.pem like a |
| 11 | * password — anyone with it can impersonate any site to this user. |
| 12 | */ |
| 13 | |
| 14 | import { mkdirSync, readFileSync, writeFileSync, existsSync } from "node:fs"; |
| 15 | import { homedir } from "node:os"; |
| 16 | import { join } from "node:path"; |
| 17 | import forge from "node-forge"; |
| 18 | |
| 19 | export interface CaFiles { |
| 20 | certPem: string; |
| 21 | keyPem: string; |
| 22 | } |
| 23 | |
| 24 | export class CertificateAuthority { |
| 25 | private caCert: forge.pki.Certificate; |
| 26 | private caKey: forge.pki.rsa.PrivateKey; |
| 27 | private leafCache = new Map<string, { cert: string; key: string }>(); |
| 28 | |
| 29 | private constructor(caCert: forge.pki.Certificate, caKey: forge.pki.rsa.PrivateKey) { |
| 30 | this.caCert = caCert; |
| 31 | this.caKey = caKey; |
| 32 | } |
| 33 | |
| 34 | /** Load the CA from `dir`, generating and persisting it on first run. */ |
| 35 | static loadOrCreate(dir = join(homedir(), ".dbbasic-browser")): CertificateAuthority { |
| 36 | mkdirSync(dir, { recursive: true }); |
| 37 | const certPath = join(dir, "ca-cert.pem"); |
| 38 | const keyPath = join(dir, "ca-key.pem"); |
| 39 | |
| 40 | if (existsSync(certPath) && existsSync(keyPath)) { |
| 41 | const cert = forge.pki.certificateFromPem(readFileSync(certPath, "utf8")); |
| 42 | const key = forge.pki.privateKeyFromPem(readFileSync(keyPath, "utf8")) as forge.pki.rsa.PrivateKey; |
| 43 | return new CertificateAuthority(cert, key); |
| 44 | } |
| 45 | |
| 46 | const { cert, key } = generateRootCa(); |
| 47 | writeFileSync(certPath, forge.pki.certificateToPem(cert), { mode: 0o644 }); |
| 48 | writeFileSync(keyPath, forge.pki.privateKeyToPem(key), { mode: 0o600 }); |
| 49 | return new CertificateAuthority(cert, key); |
| 50 | } |
| 51 | |
| 52 | /** In-memory CA for tests — nothing touches disk. */ |
| 53 | static ephemeral(): CertificateAuthority { |
| 54 | const { cert, key } = generateRootCa(); |
| 55 | return new CertificateAuthority(cert, key); |
| 56 | } |
| 57 | |
| 58 | rootCertPem(): string { |
| 59 | return forge.pki.certificateToPem(this.caCert); |
| 60 | } |
| 61 | |
| 62 | /** A leaf cert+key for `host`, signed by the root. Cached per host. */ |
| 63 | certFor(host: string): { cert: string; key: string } { |
| 64 | const cached = this.leafCache.get(host); |
| 65 | if (cached) return cached; |
| 66 | |
| 67 | const keys = forge.pki.rsa.generateKeyPair(2048); |
| 68 | const cert = forge.pki.createCertificate(); |
| 69 | cert.publicKey = keys.publicKey; |
| 70 | cert.serialNumber = serial(); |
| 71 | cert.validity.notBefore = new Date(Date.now() - 24 * 3600 * 1000); |
| 72 | cert.validity.notAfter = new Date(Date.now() + 397 * 24 * 3600 * 1000); // CA/B max |
| 73 | |
| 74 | const attrs = [{ name: "commonName", value: host }]; |
| 75 | cert.setSubject(attrs); |
| 76 | cert.setIssuer(this.caCert.subject.attributes); |
| 77 | cert.setExtensions([ |
| 78 | { name: "basicConstraints", cA: false }, |
| 79 | { name: "keyUsage", digitalSignature: true, keyEncipherment: true }, |
| 80 | { name: "extKeyUsage", serverAuth: true }, |
| 81 | { name: "subjectAltName", altNames: altNamesFor(host) }, |
| 82 | ]); |
| 83 | cert.sign(this.caKey, forge.md.sha256.create()); |
| 84 | |
| 85 | const out = { |
| 86 | cert: forge.pki.certificateToPem(cert), |
| 87 | key: forge.pki.privateKeyToPem(keys.privateKey), |
| 88 | }; |
| 89 | this.leafCache.set(host, out); |
| 90 | return out; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | function generateRootCa(): { cert: forge.pki.Certificate; key: forge.pki.rsa.PrivateKey } { |
| 95 | const keys = forge.pki.rsa.generateKeyPair(2048); |
| 96 | const cert = forge.pki.createCertificate(); |
| 97 | cert.publicKey = keys.publicKey; |
| 98 | cert.serialNumber = serial(); |
| 99 | cert.validity.notBefore = new Date(Date.now() - 24 * 3600 * 1000); |
| 100 | cert.validity.notAfter = new Date(Date.now() + 10 * 365 * 24 * 3600 * 1000); |
| 101 | |
| 102 | const attrs = [ |
| 103 | { name: "commonName", value: "dbbasic-browser x402 proxy CA" }, |
| 104 | { name: "organizationName", value: "dbbasic-browser" }, |
| 105 | ]; |
| 106 | cert.setSubject(attrs); |
| 107 | cert.setIssuer(attrs); // self-signed |
| 108 | cert.setExtensions([ |
| 109 | { name: "basicConstraints", cA: true, critical: true }, |
| 110 | { name: "keyUsage", keyCertSign: true, cRLSign: true, critical: true }, |
| 111 | ]); |
| 112 | cert.sign(keys.privateKey, forge.md.sha256.create()); |
| 113 | return { cert, key: keys.privateKey }; |
| 114 | } |
| 115 | |
| 116 | function altNamesFor(host: string): Array<{ type: number; value?: string; ip?: string }> { |
| 117 | // type 7 = IP, type 2 = DNS |
| 118 | const isIp = /^\d{1,3}(\.\d{1,3}){3}$/.test(host); |
| 119 | return isIp ? [{ type: 7, ip: host }] : [{ type: 2, value: host }]; |
| 120 | } |
| 121 | |
| 122 | let counter = 0; |
| 123 | function serial(): string { |
| 124 | // Positive, unique-per-process hex serial. Avoids Math.random (unavailable here |
| 125 | // in some sandboxes) and keeps leaf serials distinct within a run. |
| 126 | counter += 1; |
| 127 | return (Date.now() * 1000 + counter).toString(16); |
| 128 | } |