-
Notifications
You must be signed in to change notification settings - Fork 309
Add CSR common name to SANs if no other SANs are defined in CSR #1172
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
22dce0e
Add CSR common name to SANs if no other SANs are defined in CSR
dopey 9e6ac49
Add flag to enable and disable default CN -> SAN behavior
dopey 542a219
omit-cn-san
dopey c870540
Merge branch 'master' into max/common-name-name
dopey 25633cc
Update command/certificate/sign.go
dopey 71a8a36
Do not add empty common name to SANs
dopey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -37,8 +37,8 @@ func signCommand() cli.Command { | |
| Action: cli.ActionFunc(signAction), | ||
| Usage: "sign a certificate signing request (CSR)", | ||
| UsageText: `**step certificate sign** <csr-file> <crt-file> <key-file> | ||
| [**--profile**=<profile>] [**--template**=<file>] | ||
| [**--set**=<key=value>] [**--set-file**=<file>] | ||
| [**--profile**=<profile>] [**--template**=<file>] | ||
| [**--set**=<key=value>] [**--set-file**=<file>] [**--add-cn-san**] | ||
| [**--password-file**=<file>] [**--path-len**=<maximum>] | ||
| [**--not-before**=<time|duration>] [**--not-after**=<time|duration>] | ||
| [**--bundle**]`, | ||
|
|
@@ -79,6 +79,11 @@ Sign a CSR with custom validity and bundle the new certificate with the issuer: | |
| $ step certificate sign --bundle --not-before -1m --not-after 16h leaf.csr issuer.crt issuer.key | ||
| ''' | ||
|
|
||
| Sign a CSR but do not add the Common Name to the SANs extension of the certificate: | ||
| ''' | ||
| $ step certificate sign --add-cn-san=false leaf.csr issuer.crt issuer.key | ||
| ''' | ||
|
|
||
| Sign an intermediate ca: | ||
| ''' | ||
| $ step certificate sign --profile intermediate-ca intermediate.csr issuer.crt issuer.key | ||
|
|
@@ -174,6 +179,12 @@ $ step certificate sign \ | |
| flags.Template, | ||
| flags.TemplateSet, | ||
| flags.TemplateSetFile, | ||
| cli.BoolTFlag{ | ||
| Name: "add-cn-san", | ||
| Usage: `Ensure that Common Name from CSR subject is added to Subject Alternative | ||
| Name (SAN) extension of the certificate. This flag is enabled by default. To | ||
| disable default behavior pass the flag as '--add-cn-san=false'.`, | ||
| }, | ||
| flags.PasswordFile, | ||
| cli.StringFlag{ | ||
| Name: "not-before", | ||
|
|
@@ -327,7 +338,7 @@ func signAction(ctx *cli.Context) error { | |
| } | ||
|
|
||
| // Create certificate template from csr. | ||
| data := createTemplateData(csr, maxPathLen) | ||
| data := createTemplateData(csr, maxPathLen, ctx.Bool("add-cn-san")) | ||
| data.SetUserData(userData) | ||
| tpl, err := x509util.NewCertificate(csr, x509util.WithTemplate(template, data)) | ||
| if err != nil { | ||
|
|
@@ -424,7 +435,7 @@ func validateIssuer(crt *x509.Certificate, profile string, maxPathLen int) error | |
| // createTemplateData create a new template data with subject and sans based on | ||
| // the information in the certificate request, and the maxPathLen for | ||
| // intermediate certificates. | ||
| func createTemplateData(cr *x509.CertificateRequest, maxPathLen int) x509util.TemplateData { | ||
| func createTemplateData(cr *x509.CertificateRequest, maxPathLen int, addCNSAN bool) x509util.TemplateData { | ||
| var sans []string | ||
| sans = append(sans, cr.DNSNames...) | ||
| sans = append(sans, cr.EmailAddresses...) | ||
|
|
@@ -435,6 +446,19 @@ func createTemplateData(cr *x509.CertificateRequest, maxPathLen int) x509util.Te | |
| sans = append(sans, v.String()) | ||
| } | ||
|
|
||
| if addCNSAN { | ||
| cnInSANs := false | ||
| for _, v := range sans { | ||
| if v == cr.Subject.CommonName { | ||
| cnInSANs = true | ||
| break | ||
| } | ||
| } | ||
| if !cnInSANs { | ||
| sans = append(sans, cr.Subject.CommonName) | ||
| } | ||
|
dopey marked this conversation as resolved.
Outdated
|
||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want to do this only if there are no SANs. |
||
|
|
||
| data := x509util.NewTemplateData() | ||
| data.SetCertificateRequest(cr) | ||
| data.Set("MaxPathLen", maxPathLen) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do something like
--omit-cn-sanas @hslatman suggested.