Skip to content

Commit

Permalink
Simplify the automatic process to trust the certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
daquinoaldo committed Jan 25, 2019
1 parent a3038e7 commit 477d80b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 54 deletions.
40 changes: 13 additions & 27 deletions cert/generate.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
const exec = require("child_process").exec

// noinspection FallThroughInSwitchStatementJS
switch (process.platform) {
case "darwin": // MacOS
case "linux":
console.log("\n----------------------------------------------\n" +
"Please input your sudo password when required.\n" +
"----------------------------------------------\n")
exec("bash cert/generate.sh", (error, stdout, stderr) => {
console.log(stdout)
console.error(stderr)
if (error !== null) console.error(`exec error: ${error}`)
})
break
case "win32":
console.warn("Cannot generate the localhost certificate on Windows.")
process.exit(0)
case "freebsd":
console.warn("Cannot generate the localhost certificate on freebsd. " +
"Help wanted.")
process.exit(0)
case "sunos":
console.warn("Cannot generate the localhost certificate on sunos. " +
"Help wanted.")
process.exit(0)
default:
console.warn("Cannot generate the localhost certificate on your " +
"platform. Contact the developer.")
process.exit(0)
if (process.platform === "darwin" || process.platform === "linux") {
console.log("\n----------------------------------------------\n" +
"Please input your sudo password if required.\n" +
"----------------------------------------------\n")
exec("bash cert/generate.sh", (error, stdout, stderr) => {
console.log(stdout)
console.error(stderr)
if (error !== null) console.error(`exec error: ${error}`)
})
} else {
console.warn("Cannot generate the localhost certificate on your " +
"platform. Contact the developer if you can help.")
process.exit(0)
}
44 changes: 17 additions & 27 deletions cert/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,22 @@ trap 'if [[ $? -ne 0 ]]; then echo "ERROR: something went wrong."; fi' EXIT

# check the os
case "$(uname -s)" in
Darwin*) machine=MacOS;;
Linux*) machine=Linux
echo "WARNING: Only Ubuntu is supported. No guarantee for other Linux distributions.";;
CYGWIN*) machine=Linux
echo "WARNING: Support for Cygwin not guaranteed. Trying with the Linux script (coming soon).";;
MINGW*) machine=Linux
echo "WARNING: Support for MinGw not guaranteed. Trying with the Linux script (coming soon).";;
*) echo "Unknown operating system."; exit 1;;
esac

# generate the CA
echo "Creating a certification authority to sign the certificate..."
openssl req -x509 -newkey rsa:4096 -keyout cert/CA.key -out cert/CA.pem -days 1024 -nodes -subj "/C=US/ST=None/L=None/O=None/OU=None/CN=localhost"
echo "Generated CA.key and CA.pem."
Darwin*)
# generate the CA
echo "Creating a certification authority to sign the certificate..."
openssl req -x509 -newkey rsa:4096 -keyout cert/CA.key -out cert/CA.pem -days 1024 -nodes -subj "/C=US/ST=None/L=None/O=None/OU=None/CN=localhost"
echo "Generated CA.key and CA.pem."

# crate the certificate
echo "Creating a certificate for localhost and signing with out CA..."
openssl req -new -sha256 -nodes -out cert/server.csr -newkey rsa:2048 -keyout cert/localhost.key -config cert/server.conf
openssl x509 -req -in cert/server.csr -CAkey cert/CA.key -CA cert/CA.pem -CAcreateserial -out cert/localhost.crt -days 1024 -sha256 -extfile cert/x509.ext
echo "Generated localhost.key and localhost.crt."
# crate the certificate
echo "Creating a certificate for localhost and signing with out CA..."
openssl req -new -sha256 -nodes -out cert/server.csr -newkey rsa:2048 -keyout cert/localhost.key -config cert/server.conf
openssl x509 -req -in cert/server.csr -CAkey cert/CA.key -CA cert/CA.pem -CAcreateserial -out cert/localhost.crt -days 1024 -sha256 -extfile cert/x509.ext
echo "Generated localhost.key and localhost.crt."

# install the CA
echo "Installing the certificate..."
case ${machine} in
MacOS*)
# install the CA
echo "Installing the certificate..."
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain cert/CA.pem
echo "Certificate installed."
;;
Linux*)
echo "Using mkcert on linux."
Expand All @@ -43,8 +33,8 @@ case ${machine} in
#sudo chmod 664 /usr/local/share/ca-certificates/localhost.crt
#sudo update-ca-certificates
;;
*) exit 1;;
*)
echo "Unsupported system."
exit 1
;;
esac
echo "Certificate installed."


0 comments on commit 477d80b

Please sign in to comment.