Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom CA name and hosts as Subjects #240

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pem
mkcert
66 changes: 50 additions & 16 deletions cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package main

import (
"bufio"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
Expand All @@ -15,6 +16,7 @@ import (
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"math/big"
Expand All @@ -32,18 +34,27 @@ import (
pkcs12 "software.sslmate.com/src/go-pkcs12"
)

var userAndHostname string
var caName string

func init() {
u, err := user.Current()
if err == nil {
userAndHostname = u.Username + "@"
}
if h, err := os.Hostname(); err == nil {
userAndHostname += h
}
if err == nil && u.Name != "" && u.Name != u.Username {
userAndHostname += " (" + u.Name + ")"
root := fmt.Sprintf("%s/%s", getCAROOT(), rootName)
_, err := ioutil.ReadFile(root)
if err != nil {
caName = customCaName()
if caName == "" {
u, err := user.Current()
if err == nil {
caName = u.Username + "@"
}
if h, err := os.Hostname(); err == nil {
caName += h
}
if err == nil && u.Name != "" && u.Name != u.Username {
caName += " (" + u.Name + ")"
}
}
} else {
log.Printf("Using %s as CA root", root)
}
}

Expand All @@ -59,11 +70,11 @@ func (m *mkcert) makeCert(hosts []string) {
tpl := &x509.Certificate{
SerialNumber: randomSerialNumber(),
Subject: pkix.Name{
Organization: []string{"mkcert development certificate"},
OrganizationalUnit: []string{userAndHostname},
Organization: hosts,
OrganizationalUnit: hosts,
},

NotAfter: time.Now().AddDate(10, 0, 0),
NotAfter: time.Now().AddDate(10, 0, 0),

// Fix the notBefore to temporarily bypass macOS Catalina's limit on
// certificate lifespan. Once mkcert provides an ACME server, automation
Expand Down Expand Up @@ -307,13 +318,13 @@ func (m *mkcert) newCA() {
tpl := &x509.Certificate{
SerialNumber: randomSerialNumber(),
Subject: pkix.Name{
Organization: []string{"mkcert development CA"},
OrganizationalUnit: []string{userAndHostname},
Organization: []string{caName + " via mkcert"},
OrganizationalUnit: []string{caName},

// The CommonName is required by iOS to show the certificate in the
// "Certificate Trust Settings" menu.
// https://github.com/FiloSottile/mkcert/issues/47
CommonName: "mkcert " + userAndHostname,
CommonName: caName,
},
SubjectKeyId: skid[:],

Expand Down Expand Up @@ -346,3 +357,26 @@ func (m *mkcert) newCA() {
func (m *mkcert) caUniqueName() string {
return "mkcert development CA " + m.caCert.SerialNumber.String()
}

func customCaName() string {
reader := bufio.NewReader(os.Stdin)
var ca string
var custom bool

fmt.Println("Would you like a custom CA name?(y/n) Default is 'mkcert user@host')")

var s string
fmt.Scan(&s)
s = strings.TrimSpace(s)
s = strings.ToLower(s)
if s == "y" || s == "yes" {
custom = true
}

if custom {
fmt.Print("CA Name: ")
ca, _ = reader.ReadString('\n')
ca = strings.Replace(ca, "\n", "", -1)
}
return ca
}