-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e641091
commit e0ad2d6
Showing
16 changed files
with
859 additions
and
467 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Vagrant.configure("2") do |config| | ||
|
||
config.vm.define "mac" do |mac| | ||
config.vm.box = "devcert/macos" | ||
config.vm.network "public_network" | ||
|
||
config.vm.define "linux" do |linux| | ||
config.vm.box = "devcert/linux" | ||
config.vm.network "public_network" | ||
|
||
config.vm.define "windows" do |windows| | ||
config.vm.box = "devcert/windows" | ||
config.vm.network "public_network" | ||
|
||
config.vm.provider "virtualbox" do |vb| | ||
# Display the VirtualBox GUI when booting the machine | ||
vb.gui = true | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { readFileSync as readFile, writeFileSync as writeFile } from 'fs'; | ||
import * as createDebug from 'debug'; | ||
import * as eol from 'eol'; | ||
|
||
import { | ||
isMac, | ||
isLinux, | ||
configPath, | ||
rootKeyPath, | ||
rootCertPath, | ||
opensslConfPath, | ||
opensslConfTemplate | ||
} from './constants'; | ||
import addToMacTrustStores from './platforms/macos'; | ||
import addToLinuxTrustStores from './platforms/linux'; | ||
import addToWindowsTrustStores from './platforms/windows'; | ||
import { openssl } from './utils'; | ||
import { generateKey } from './certificates'; | ||
import { Options } from './index'; | ||
|
||
const debug = createDebug('devcert:certificate-authority'); | ||
|
||
/** | ||
* Install the once-per-machine trusted root CA. We'll use this CA to sign | ||
* per-app certs. | ||
*/ | ||
export default async function installCertificateAuthority(options: Options = {}): Promise<void> { | ||
debug(`Generating a root certificate authority`); | ||
|
||
debug(`Generating the OpenSSL configuration needed to setup the certificate authority`); | ||
generateOpenSSLConfFiles(); | ||
|
||
debug(`Generating a private key`); | ||
generateKey(rootKeyPath); | ||
|
||
debug(`Generating a CA certificate`); | ||
openssl(`req -config ${ opensslConfPath } -key ${ rootKeyPath } -out ${ rootCertPath } -new -subj "/CN=devcert" -x509 -days 7000 -extensions v3_ca`); | ||
|
||
debug(`Adding the root certificate authority to trust stores`); | ||
if (isMac) { | ||
await addToMacTrustStores(rootCertPath, options); | ||
} else if (isLinux) { | ||
await addToLinuxTrustStores(rootCertPath, options); | ||
} else { | ||
await addToWindowsTrustStores(rootCertPath, options); | ||
} | ||
} | ||
|
||
/** | ||
* Copy our OpenSSL conf template to the local devcert config folder, and | ||
* update the paths inside that config file to be OS specific. Also initializes | ||
* the files OpenSSL needs to sign certificates as a certificate authority | ||
*/ | ||
function generateOpenSSLConfFiles() { | ||
let confTemplate = readFile(opensslConfTemplate, 'utf-8'); | ||
confTemplate = confTemplate.replace(/DATABASE_PATH/, configPath('index.txt').replace(/\\/g, '\\\\')); | ||
confTemplate = confTemplate.replace(/SERIAL_PATH/, configPath('serial').replace(/\\/g, '\\\\')); | ||
confTemplate = eol.auto(confTemplate); | ||
writeFile(opensslConfPath, confTemplate); | ||
writeFile(configPath('index.txt'), ''); | ||
writeFile(configPath('serial'), '01'); | ||
// This version number lets us write code in the future that intelligently upgrades an existing | ||
// devcert installation. This "ca-version" is independent of the devcert package version, and | ||
// tracks changes to the root certificate setup only. | ||
writeFile(configPath('devcert-ca-version'), '1'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as path from 'path'; | ||
import * as createDebug from 'debug'; | ||
import { chmodSync as chmod } from 'fs'; | ||
import { pathForDomain, opensslConfPath, rootKeyPath, rootCertPath } from './constants'; | ||
import { openssl } from './utils'; | ||
|
||
const debug = createDebug('devcert:certificates'); | ||
|
||
// Generate an app certificate signed by the devcert root CA | ||
export default function generateSignedCertificate(domain: string): void { | ||
debug(`Generating private key for ${ domain }`); | ||
let keyPath = pathForDomain(domain, 'private-key.key'); | ||
generateKey(keyPath); | ||
|
||
debug(`Generating certificate signing request for ${ domain }`); | ||
let csrFile = pathForDomain(domain, `${ domain }.csr`); | ||
openssl(`req -config ${ opensslConfPath } -subj "/CN=${ domain }" -key ${ keyPath } -out ${ csrFile } -new`); | ||
|
||
debug(`Generating certificate for ${ domain } from signing request and signing with root CA`); | ||
let certPath = pathForDomain(`${ domain }.crt`); | ||
openssl(`ca -config ${ opensslConfPath } -in ${ csrFile } -out ${ path.basename(certPath) } -outdir ${ path.dirname(certPath) } -keyfile ${ rootKeyPath } -cert ${ rootCertPath } -notext -md sha256 -days 7000 -batch -extensions server_cert`) | ||
} | ||
|
||
// Generate a cryptographic key, used to sign certificates or certificate signing requests. | ||
export function generateKey(filename: string): void { | ||
debug(`generateKey: ${ filename }`); | ||
openssl(`genrsa -out ${ filename } 2048`); | ||
chmod(filename, 400); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,23 @@ | ||
import * as path from 'path'; | ||
import * as mkdirp from 'mkdirp'; | ||
import applicationConfigPath = require('application-config-path'); | ||
|
||
// Platform shortcuts | ||
export const isMac = process.platform === 'darwin'; | ||
export const isLinux = process.platform === 'linux'; | ||
export const isWindows = process.platform === 'win32'; | ||
|
||
// use %LOCALAPPDATA%/devcert on Windows otherwise use ~/.config/devcert | ||
export let configDir: string; | ||
if (isWindows && process.env.LOCALAPPDATA) { | ||
configDir = path.join(process.env.LOCALAPPDATA, 'devcert', 'config'); | ||
} else { | ||
let uid = process.getuid && process.getuid(); | ||
let userHome = (isLinux && uid === 0) ? path.resolve('/usr/local/share') : require('os').homedir(); | ||
configDir = path.join(userHome, '.config', 'devcert'); | ||
} | ||
// Common paths | ||
export const configDir = applicationConfigPath('devcert'); | ||
export const configPath: (...pathSegments: string[]) => string = path.join.bind(path, configDir); | ||
|
||
export const domainsDir = configPath('domains'); | ||
export const pathForDomain: (domain: string, ...pathSegments: string[]) => string = path.join.bind(path, domainsDir) | ||
|
||
export const opensslConfTemplate = path.join(__dirname, '..', 'openssl.conf'); | ||
export const opensslConfPath = configPath('openssl.conf'); | ||
export const rootKeyPath = configPath('devcert-ca-root.key'); | ||
export const rootCertPath = configPath('devcert-ca-root.crt'); | ||
export const caCertsDir = configPath('certs'); | ||
|
||
mkdirp.sync(configDir); | ||
mkdirp.sync(caCertsDir); | ||
mkdirp.sync(domainsDir); |
Oops, something went wrong.