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

Populate ExtraNames from Names #42

Merged
merged 5 commits into from
May 17, 2022
Merged

Populate ExtraNames from Names #42

merged 5 commits into from
May 17, 2022

Conversation

maraino
Copy link
Contributor

@maraino maraino commented May 16, 2022

Description

This commit will populate the subject ExtraNames in a certificate request from the information present in Names as long as it is not one of the default fields. This way, we can extract non-default fields as well as populate them in the final certificate if necessary.

It also uses the proper type for the deprecated email address attribute in the subject.

Related to smallstep/certificates#916

This commit will populate the subject ExtraNames in a certificate
request from the information present in Names as long as it is not
one of the default fields. This way, we can extract non-default fields
as well as populate them in the final certificate if necessary.

It also uses the proper type for the deprecated email address
attribute in the subject.

Related to smallstep/certificates#916
x509util/name.go Outdated
Comment on lines 12 to 14
// attributeTypeNames are the subject attributes managed by Go and this package.
// newDistinguisedNames will populate .Insecure.CR.ExtraNames with the
// attributes not present on this map.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps these should be called distinguishedNameAttributeNames to make clear that's what these are? They can be used for both Subject as well as Issuer names.

newDistinguisednames -> newDistinguishedNames. This typo is present several times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a copy&paste from Go's source code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's the right name for this map.

ExtraNames: []pkix.AttributeTypeAndValue{
{Type: asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}, Value: "jane@example.com"},
Names: []pkix.AttributeTypeAndValue{
{Type: asn1.ObjectIdentifier{2, 5, 4, 3}, Value: "The commonName"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This instance of the The commonName is ignored, right? It may be clearer to use a different value than the one that's already set in CommonName, so that a change in the function will result in a different test outcome.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to "imitate" a real case, Names will have all the attributes present in the subject C, O, OU, CN, ... I will add all of them to make sure the map is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with 4434f23

x509util/name.go Outdated
Comment on lines 152 to 154
// newDistinguisedNames returns a list of DistinguishedName with the attributes not
// present in attributeTypeNames.
func newDistinguisedNames(atvs []pkix.AttributeTypeAndValue) []DistinguishedName {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the function name can reflect the fact that it excludes the attribute names? It might get overly long doing so though, like newDistinguishedNamesExcludingExistingAttributes. Don't have a better proposal atm 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with c93d3e3 (newExtraNames)

x509util/name.go Outdated
@@ -35,7 +53,7 @@ func newName(n pkix.Name) Name {
PostalCode: n.PostalCode,
SerialNumber: n.SerialNumber,
CommonName: n.CommonName,
ExtraNames: newDistinguisedNames(n.ExtraNames),
ExtraNames: newDistinguisedNames(n.Names),
Copy link
Member

@hslatman hslatman May 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you kept the ExtraNames property for backwards compatibility? Should we maybe also provide the Names property, which could contain the same data? Or doesn't that make any sense from a user's viewpoint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with that approach is that it will be more difficult to create a template directly from a CSR. You mentioned in smallstep/certificates#916 a template with "subject": {{ toJson .Insecure.CR.Subject }}, if we do your change, Go will ignore the values of Names when it creates a certificate, so the extra ones from the CSR won't be present in the certificate.

@maraino maraino requested a review from hslatman May 17, 2022 18:26
Copy link
Member

@hslatman hslatman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Just the suggestion and usage of the function to be fixed.

@@ -35,7 +53,7 @@ func newName(n pkix.Name) Name {
PostalCode: n.PostalCode,
SerialNumber: n.SerialNumber,
CommonName: n.CommonName,
ExtraNames: newDistinguisedNames(n.ExtraNames),
ExtraNames: newExtraNames(n.Names),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better name 😄

x509util/name.go Outdated
Comment on lines 167 to 170
// fromDistinguisedNames converts a list of DistinguisedName to
// []pkix.AttributeTypeAndValue. Note that this method has a special case to
// encode the emailAddress deprecated field (1.2.840.113549.1.9.1).
func fromDistinguisedNames(dns []DistinguishedName) []pkix.AttributeTypeAndValue {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs update on the calling side too.

Suggested change
// fromDistinguisedNames converts a list of DistinguisedName to
// []pkix.AttributeTypeAndValue. Note that this method has a special case to
// encode the emailAddress deprecated field (1.2.840.113549.1.9.1).
func fromDistinguisedNames(dns []DistinguishedName) []pkix.AttributeTypeAndValue {
// fromDistinguishedNames converts a list of DistinguishedName to
// []pkix.AttributeTypeAndValue. Note that this method has a special case to
// encode the deprecated emailAddress field (1.2.840.113549.1.9.1).
func fromDistinguishedNames(dns []DistinguishedName) []pkix.AttributeTypeAndValue {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with 487ffed

@maraino maraino merged commit 06971bd into master May 17, 2022
@maraino maraino deleted the fix-subject-names branch May 17, 2022 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants