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

Add option for creating code-signing certificates. #90

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cmd/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package cmd

import (
"crypto/x509"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -67,6 +68,10 @@ func NewSignCommand() cli.Command {
Name: "intermediate",
Usage: "Whether generated certificate should be a intermediate",
},
cli.BoolFlag{
Name: "codesigning",
Usage: "Whether generated certificate should include the codeSigning extended key usage extension",
},
},
Action: newSignAction,
}
Expand Down Expand Up @@ -141,6 +146,12 @@ func newSignAction(c *cli.Context) {
if c.Bool("intermediate") {
fmt.Fprintln(os.Stderr, "Building intermediate")
crtOut, err = pkix.CreateIntermediateCertificateAuthority(crt, key, csr, expiresTime)
} else if c.Bool("codesigning") {
fmt.Fprintln(os.Stderr, "Including codeSigning extended key usage")
extKeyUsage := []x509.ExtKeyUsage{
x509.ExtKeyUsageCodeSigning,
}
crtOut, err = pkix.CreateCertificateHostWithExtUsage(crt, key, csr, expiresTime, extKeyUsage)
} else {
crtOut, err = pkix.CreateCertificateHost(crt, key, csr, expiresTime)
}
Expand Down
10 changes: 10 additions & 0 deletions pkix/cert_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ import (
// CreateCertificateHost creates certificate for host.
// The arguments include CA certificate, CA key, certificate request.
func CreateCertificateHost(crtAuth *Certificate, keyAuth *Key, csr *CertificateSigningRequest, proposedExpiry time.Time) (*Certificate, error) {
return CreateCertificateHostWithExtUsage(crtAuth, keyAuth, csr, proposedExpiry, nil)
}

// CreateCertificateHostWithExtUsage creates certificate for host with optional key usage extensions.
// The arguments include CA certificate, CA key, certificate request, and any key usage extensions.
func CreateCertificateHostWithExtUsage(crtAuth *Certificate, keyAuth *Key, csr *CertificateSigningRequest, proposedExpiry time.Time, extKeyUsage []x509.ExtKeyUsage) (*Certificate, error) {
// Build CA based on RFC5280
hostTemplate := x509.Certificate{
// **SHOULD** be filled in a unique number
Expand Down Expand Up @@ -61,6 +67,10 @@ func CreateCertificateHost(crtAuth *Certificate, keyAuth *Key, csr *CertificateS
PermittedDNSDomains: nil,
}

if extKeyUsage != nil {
hostTemplate.ExtKeyUsage = append(hostTemplate.ExtKeyUsage, extKeyUsage...)
}

serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions pkix/cert_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package pkix

import (
"crypto/x509"

"bytes"
"testing"
"time"
Expand Down Expand Up @@ -66,4 +68,29 @@ func TestCreateCertificateHost(t *testing.T) {
if err = rawCrt.CheckSignatureFrom(rawCrtAuth); err != nil {
t.Fatal("Failed to check signature:", err)
}

extKeyUsage := []x509.ExtKeyUsage{
x509.ExtKeyUsageCodeSigning,
}
cscrt, err := CreateCertificateHostWithExtUsage(crtAuth, key, csr, time.Now().AddDate(5000, 0, 0), extKeyUsage)
if err != nil {
t.Fatal("Failed creating certificate with codesigning for host:", err)
}

csrawCrt, err := cscrt.GetRawCertificate()
if err != nil {
t.Fatal("Failed to get x509.Certificate with codesigning:", err)
}

hasCodeSigning := false
for _, eku := range csrawCrt.ExtKeyUsage {
if eku == x509.ExtKeyUsageCodeSigning {
hasCodeSigning = true
}
}

if !hasCodeSigning {
t.Fatal("x509.Certificate does not include codesigning extra key usage")
}

}