Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4 from soramitsukhmer/feature/add-path-length-to-…
Browse files Browse the repository at this point in the history
…sign

Add 'path-length' flag to 'sign' command
  • Loading branch information
socheatsok78 authored Jan 13, 2022
2 parents c497876 + dd31e8e commit 6b13803
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 11 additions & 1 deletion cmd/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func NewSignCommand() cli.Command {
Name: "intermediate",
Usage: "Whether generated certificate should be a intermediate",
},
cli.IntFlag{
Name: "path-length",
Value: 0,
Usage: "Maximum number of non-self-issued intermediate certificates that may follow this CA certificate in a valid certification path",
},
},
Action: newSignAction,
}
Expand Down Expand Up @@ -140,8 +145,13 @@ func newSignAction(c *cli.Context) {
var crtOut *pkix.Certificate
if c.Bool("intermediate") {
fmt.Fprintln(os.Stderr, "Building intermediate")
crtOut, err = pkix.CreateIntermediateCertificateAuthority(crt, key, csr, expiresTime)
crtOut, err = pkix.CreateIntermediateCertificateAuthority(crt, key, csr, expiresTime, c.Int("path-length"))
} else {
if c.IsSet("path-length") {
fmt.Fprintln(os.Stderr, "The 'path-length' can only be used with 'intermediate' flag.")
os.Exit(1)
}

crtOut, err = pkix.CreateCertificateHost(crt, key, csr, expiresTime)
}

Expand Down
8 changes: 6 additions & 2 deletions pkix/cert_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func CreateCertificateAuthority(key *Key, organizationalUnit string, expiry time

// CreateIntermediateCertificateAuthority creates an intermediate
// CA certificate signed by the given authority.
func CreateIntermediateCertificateAuthority(crtAuth *Certificate, keyAuth *Key, csr *CertificateSigningRequest, proposedExpiry time.Time) (*Certificate, error) {
func CreateIntermediateCertificateAuthority(crtAuth *Certificate, keyAuth *Key, csr *CertificateSigningRequest, proposedExpiry time.Time, pathlen int) (*Certificate, error) {
authTemplate := newAuthTemplate()

serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
Expand All @@ -87,7 +87,11 @@ func CreateIntermediateCertificateAuthority(crtAuth *Certificate, keyAuth *Key,
return nil, err
}
authTemplate.SerialNumber.Set(serialNumber)
authTemplate.MaxPathLenZero = false

if pathlen > 0 {
authTemplate.MaxPathLen = pathlen
authTemplate.MaxPathLenZero = false
}

rawCsr, err := csr.GetRawCertificateSigningRequest()
if err != nil {
Expand Down

0 comments on commit 6b13803

Please sign in to comment.